summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Harrison <rharrison@chromium.org>2017-08-28 13:36:18 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-08-28 19:17:03 +0000
commit61cb1121729d7e5f53c95077dcc57a61b3f575e8 (patch)
tree8c5dd34af353a53c7e781ed696e6a773a9e55949
parent2b6e2a7c92a8c2756364a40a22e6f91b910fc514 (diff)
downloadpdfium-61cb1121729d7e5f53c95077dcc57a61b3f575e8.tar.xz
Convert find markers to Optionals in CPDF_TextPageFind
Currently these use -1 as a special value to indicate not set. This creates the same issues that FX_STRNPOS created for converting FX_STRSIZE to size_t, so this code has been rewritten. BUG=pdfium:828 Change-Id: Iaaa96af0dcb2eb8b600f3ea39060a398ac9a3800 Reviewed-on: https://pdfium-review.googlesource.com/12130 Reviewed-by: Tom Sepez <tsepez@chromium.org> Commit-Queue: Ryan Harrison <rharrison@chromium.org>
-rw-r--r--core/fpdftext/cpdf_textpagefind.cpp60
-rw-r--r--core/fpdftext/cpdf_textpagefind.h14
-rw-r--r--fpdfsdk/fpdftext.cpp4
3 files changed, 39 insertions, 39 deletions
diff --git a/core/fpdftext/cpdf_textpagefind.cpp b/core/fpdftext/cpdf_textpagefind.cpp
index fd6e3a0d00..3678e42515 100644
--- a/core/fpdftext/cpdf_textpagefind.cpp
+++ b/core/fpdftext/cpdf_textpagefind.cpp
@@ -36,8 +36,6 @@ bool IsIgnoreSpaceCharacter(wchar_t curChar) {
CPDF_TextPageFind::CPDF_TextPageFind(const CPDF_TextPage* pTextPage)
: m_pTextPage(pTextPage),
m_flags(0),
- m_findNextStart(-1),
- m_findPreStart(-1),
m_bMatchCase(false),
m_bMatchWholeWord(false),
m_resStart(0),
@@ -83,7 +81,7 @@ int CPDF_TextPageFind::GetCharIndex(int index) const {
bool CPDF_TextPageFind::FindFirst(const CFX_WideString& findwhat,
int flags,
- int startPos) {
+ pdfium::Optional<FX_STRSIZE> startPos) {
if (!m_pTextPage)
return false;
if (m_strText.IsEmpty() || m_bMatchCase != (flags & FPDFTEXT_MATCHCASE))
@@ -103,12 +101,15 @@ bool CPDF_TextPageFind::FindFirst(const CFX_WideString& findwhat,
}
m_bMatchWholeWord = !!(flags & FPDFTEXT_MATCHWHOLEWORD);
m_findNextStart = startPos;
- if (startPos == -1)
- m_findPreStart = m_strText.GetLength() - 1;
- else
+ if (!startPos.has_value()) {
+ if (m_strText.GetLength() > 0)
+ m_findPreStart = m_strText.GetLength() - 1;
+ } else {
m_findPreStart = startPos;
+ }
+
m_csFindWhatArray.clear();
- int i = 0;
+ FX_STRSIZE i = 0;
while (i < len) {
if (findwhatStr[i] != ' ')
break;
@@ -130,20 +131,20 @@ bool CPDF_TextPageFind::FindNext() {
if (!m_pTextPage)
return false;
m_resArray.clear();
- if (m_findNextStart == -1)
+ if (!m_findNextStart.has_value())
return false;
if (m_strText.IsEmpty()) {
m_IsFind = false;
return m_IsFind;
}
- int strLen = m_strText.GetLength();
- if (m_findNextStart > strLen - 1) {
+ FX_STRSIZE strLen = m_strText.GetLength();
+ if (m_findNextStart.value() > strLen - 1) {
m_IsFind = false;
return m_IsFind;
}
int nCount = pdfium::CollectionSize<int>(m_csFindWhatArray);
pdfium::Optional<FX_STRSIZE> nResultPos = 0;
- int nStartPos = m_findNextStart;
+ FX_STRSIZE nStartPos = m_findNextStart.value();
bool bSpaceStart = false;
for (int iWord = 0; iWord < nCount; iWord++) {
CFX_WideString csWord = m_csFindWhatArray[iWord];
@@ -161,7 +162,7 @@ bool CPDF_TextPageFind::FindNext() {
}
continue;
}
- int endIndex;
+ FX_STRSIZE endIndex;
nResultPos = m_strText.Find(csWord.c_str(), nStartPos);
if (!nResultPos.has_value()) {
m_IsFind = false;
@@ -172,7 +173,7 @@ bool CPDF_TextPageFind::FindNext() {
m_resStart = nResultPos.value();
bool bMatch = true;
if (iWord != 0 && !bSpaceStart) {
- int PreResEndPos = nStartPos;
+ FX_STRSIZE PreResEndPos = nStartPos;
int curChar = csWord[0];
CFX_WideString lastWord = m_csFindWhatArray[iWord - 1];
int lastChar = lastWord[lastWord.GetLength() - 1];
@@ -181,7 +182,7 @@ bool CPDF_TextPageFind::FindNext() {
IsIgnoreSpaceCharacter(curChar))) {
bMatch = false;
}
- for (int d = PreResEndPos; d < nResultPos.value(); d++) {
+ for (FX_STRSIZE d = PreResEndPos; d < nResultPos.value(); d++) {
wchar_t strInsert = m_strText[d];
if (strInsert != TEXT_LINEFEED_CHAR && strInsert != TEXT_SPACE_CHAR &&
strInsert != TEXT_RETURN_CHAR && strInsert != 160) {
@@ -232,12 +233,13 @@ bool CPDF_TextPageFind::FindPrev() {
if (!m_pTextPage)
return false;
m_resArray.clear();
- if (m_strText.IsEmpty() || m_findPreStart < 0) {
+ if (m_strText.IsEmpty() || !m_findPreStart.has_value()) {
m_IsFind = false;
return m_IsFind;
}
CPDF_TextPageFind findEngine(m_pTextPage.Get());
- bool ret = findEngine.FindFirst(m_findWhat, m_flags);
+ bool ret = findEngine.FindFirst(m_findWhat, m_flags,
+ pdfium::Optional<FX_STRSIZE>(0));
if (!ret) {
m_IsFind = false;
return m_IsFind;
@@ -248,7 +250,8 @@ bool CPDF_TextPageFind::FindPrev() {
if (ret) {
int order1 = findEngine.GetCurOrder();
int MatchedCount1 = findEngine.GetMatchedCount();
- if (((order1 + MatchedCount1) - 1) > m_findPreStart)
+ if (static_cast<FX_STRSIZE>((order1 + MatchedCount1)) >
+ m_findPreStart.value() + 1)
break;
order = order1;
MatchedCount = MatchedCount1;
@@ -289,7 +292,7 @@ void CPDF_TextPageFind::ExtractFindWhat(const CFX_WideString& findwhat) {
break;
}
}
- int pos = 0;
+ FX_STRSIZE pos = 0;
while (pos < csWord.GetLength()) {
CFX_WideString curStr = csWord.Mid(pos, 1);
wchar_t curChar = csWord[pos];
@@ -318,16 +321,18 @@ void CPDF_TextPageFind::ExtractFindWhat(const CFX_WideString& findwhat) {
}
bool CPDF_TextPageFind::IsMatchWholeWord(const CFX_WideString& csPageText,
- int startPos,
- int endPos) {
+ FX_STRSIZE startPos,
+ FX_STRSIZE endPos) {
+ if (startPos > endPos)
+ return false;
wchar_t char_left = 0;
wchar_t char_right = 0;
- int char_count = endPos - startPos + 1;
- if (char_count < 1)
+ FX_STRSIZE char_count = endPos - startPos + 1;
+ if (char_count == 0)
return false;
if (char_count == 1 && csPageText[startPos] > 255)
return true;
- if (startPos - 1 >= 0)
+ if (startPos >= 1)
char_left = csPageText[startPos - 1];
if (startPos + char_count < csPageText.GetLength())
char_right = csPageText[startPos + char_count];
@@ -380,15 +385,6 @@ bool CPDF_TextPageFind::ExtractSubString(CFX_WideString& rString,
return true;
}
-CFX_WideString CPDF_TextPageFind::MakeReverse(const CFX_WideString& str) {
- CFX_WideString str2;
- str2.clear();
- int nlen = str.GetLength();
- for (int i = nlen - 1; i >= 0; i--)
- str2 += str[i];
- return str2;
-}
-
int CPDF_TextPageFind::GetCurOrder() const {
return GetCharIndex(m_resStart);
}
diff --git a/core/fpdftext/cpdf_textpagefind.h b/core/fpdftext/cpdf_textpagefind.h
index ed0620605a..cf8d3d1702 100644
--- a/core/fpdftext/cpdf_textpagefind.h
+++ b/core/fpdftext/cpdf_textpagefind.h
@@ -13,6 +13,7 @@
#include "core/fxcrt/fx_coordinates.h"
#include "core/fxcrt/fx_string.h"
#include "core/fxcrt/fx_system.h"
+#include "third_party/base/optional.h"
class CPDF_TextPage;
@@ -21,7 +22,9 @@ class CPDF_TextPageFind {
explicit CPDF_TextPageFind(const CPDF_TextPage* pTextPage);
~CPDF_TextPageFind();
- bool FindFirst(const CFX_WideString& findwhat, int flags, int startPos = 0);
+ bool FindFirst(const CFX_WideString& findwhat,
+ int flags,
+ pdfium::Optional<FX_STRSIZE> startPos);
bool FindNext();
bool FindPrev();
int GetCurOrder() const;
@@ -30,13 +33,12 @@ class CPDF_TextPageFind {
protected:
void ExtractFindWhat(const CFX_WideString& findwhat);
bool IsMatchWholeWord(const CFX_WideString& csPageText,
- int startPos,
- int endPos);
+ FX_STRSIZE startPos,
+ FX_STRSIZE endPos);
bool ExtractSubString(CFX_WideString& rString,
const wchar_t* lpszFullString,
int iSubString,
wchar_t chSep);
- CFX_WideString MakeReverse(const CFX_WideString& str);
int GetCharIndex(int index) const;
private:
@@ -46,8 +48,8 @@ class CPDF_TextPageFind {
CFX_WideString m_findWhat;
int m_flags;
std::vector<CFX_WideString> m_csFindWhatArray;
- int m_findNextStart;
- int m_findPreStart;
+ pdfium::Optional<FX_STRSIZE> m_findNextStart;
+ pdfium::Optional<FX_STRSIZE> m_findPreStart;
bool m_bMatchCase;
bool m_bMatchWholeWord;
int m_resStart;
diff --git a/fpdfsdk/fpdftext.cpp b/fpdfsdk/fpdftext.cpp
index 2ea06de5d8..8ed76ea7c1 100644
--- a/fpdfsdk/fpdftext.cpp
+++ b/fpdfsdk/fpdftext.cpp
@@ -250,7 +250,9 @@ FPDFText_FindStart(FPDF_TEXTPAGE text_page,
new CPDF_TextPageFind(CPDFTextPageFromFPDFTextPage(text_page));
FX_STRSIZE len = CFX_WideString::WStringLength(findwhat);
textpageFind->FindFirst(CFX_WideString::FromUTF16LE(findwhat, len), flags,
- start_index);
+ start_index >= 0
+ ? pdfium::Optional<FX_STRSIZE>(start_index)
+ : pdfium::Optional<FX_STRSIZE>());
return textpageFind;
}