summaryrefslogtreecommitdiff
path: root/core/fpdftext/cpdf_textpagefind.cpp
diff options
context:
space:
mode:
authorRyan Harrison <rharrison@chromium.org>2017-08-23 10:39:35 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-08-23 15:11:19 +0000
commit12db7515f17228798d1aa38fce0fee3e7d2d36b6 (patch)
treee291daf9e6a88ba0248670b9f1ba3a555f052538 /core/fpdftext/cpdf_textpagefind.cpp
parent3bb0a34cc75abe49a59c6390353957bbb5c5ab38 (diff)
downloadpdfium-12db7515f17228798d1aa38fce0fee3e7d2d36b6.tar.xz
Convert string Find methods to return an Optional
The Find and ReverseFind methods for WideString, WideStringC, ByteString, and ByteStringC have been converted from returning a raw FX_STRSIZE, to returning Optional<FX_STRSIZE>, so that success/failure can be indicated without using FX_STRNPOS. This allows for removing FX_STRNPOS and by association makes the conversion of FX_STRSIZE to size_t easier, since it forces checking the return value of Find to be explictly done as well as taking the error value out of the range of FX_STRSIZE. New Contains methods have been added for cases where the success or failure is all the call site to Find cared about, and the actual position was ignored. BUG=pdfium:828 Change-Id: Id827e508c8660affa68cc08a13d96121369364b7 Reviewed-on: https://pdfium-review.googlesource.com/11350 Commit-Queue: Ryan Harrison <rharrison@chromium.org> Reviewed-by: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'core/fpdftext/cpdf_textpagefind.cpp')
-rw-r--r--core/fpdftext/cpdf_textpagefind.cpp27
1 files changed, 13 insertions, 14 deletions
diff --git a/core/fpdftext/cpdf_textpagefind.cpp b/core/fpdftext/cpdf_textpagefind.cpp
index 55f940cad7..fd6e3a0d00 100644
--- a/core/fpdftext/cpdf_textpagefind.cpp
+++ b/core/fpdftext/cpdf_textpagefind.cpp
@@ -142,9 +142,8 @@ bool CPDF_TextPageFind::FindNext() {
return m_IsFind;
}
int nCount = pdfium::CollectionSize<int>(m_csFindWhatArray);
- int nResultPos = 0;
- int nStartPos = 0;
- nStartPos = m_findNextStart;
+ pdfium::Optional<FX_STRSIZE> nResultPos = 0;
+ int nStartPos = m_findNextStart;
bool bSpaceStart = false;
for (int iWord = 0; iWord < nCount; iWord++) {
CFX_WideString csWord = m_csFindWhatArray[iWord];
@@ -164,25 +163,25 @@ bool CPDF_TextPageFind::FindNext() {
}
int endIndex;
nResultPos = m_strText.Find(csWord.c_str(), nStartPos);
- if (nResultPos == FX_STRNPOS) {
+ if (!nResultPos.has_value()) {
m_IsFind = false;
return m_IsFind;
}
- endIndex = nResultPos + csWord.GetLength() - 1;
+ endIndex = nResultPos.value() + csWord.GetLength() - 1;
if (iWord == 0)
- m_resStart = nResultPos;
+ m_resStart = nResultPos.value();
bool bMatch = true;
if (iWord != 0 && !bSpaceStart) {
int PreResEndPos = nStartPos;
int curChar = csWord[0];
CFX_WideString lastWord = m_csFindWhatArray[iWord - 1];
int lastChar = lastWord[lastWord.GetLength() - 1];
- if (nStartPos == nResultPos &&
+ if (nStartPos == nResultPos.value() &&
!(IsIgnoreSpaceCharacter(lastChar) ||
IsIgnoreSpaceCharacter(curChar))) {
bMatch = false;
}
- for (int d = PreResEndPos; d < nResultPos; d++) {
+ for (int 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) {
@@ -191,19 +190,19 @@ bool CPDF_TextPageFind::FindNext() {
}
}
} else if (bSpaceStart) {
- if (nResultPos > 0) {
- wchar_t strInsert = m_strText[nResultPos - 1];
+ if (nResultPos.value() > 0) {
+ wchar_t strInsert = m_strText[nResultPos.value() - 1];
if (strInsert != TEXT_LINEFEED_CHAR && strInsert != TEXT_SPACE_CHAR &&
strInsert != TEXT_RETURN_CHAR && strInsert != 160) {
bMatch = false;
- m_resStart = nResultPos;
+ m_resStart = nResultPos.value();
} else {
- m_resStart = nResultPos - 1;
+ m_resStart = nResultPos.value() - 1;
}
}
}
if (m_bMatchWholeWord && bMatch) {
- bMatch = IsMatchWholeWord(m_strText, nResultPos, endIndex);
+ bMatch = IsMatchWholeWord(m_strText, nResultPos.value(), endIndex);
}
nStartPos = endIndex + 1;
if (!bMatch) {
@@ -214,7 +213,7 @@ bool CPDF_TextPageFind::FindNext() {
nStartPos = m_resStart + m_csFindWhatArray[0].GetLength();
}
}
- m_resEnd = nResultPos + m_csFindWhatArray.back().GetLength() - 1;
+ m_resEnd = nResultPos.value() + m_csFindWhatArray.back().GetLength() - 1;
m_IsFind = true;
int resStart = GetCharIndex(m_resStart);
int resEnd = GetCharIndex(m_resEnd);