summaryrefslogtreecommitdiff
path: root/core
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
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')
-rw-r--r--core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp26
-rw-r--r--core/fpdfapi/page/cpdf_streamcontentparser.cpp8
-rw-r--r--core/fpdfapi/parser/fpdf_parser_utility.cpp6
-rw-r--r--core/fpdfdoc/cpdf_action.cpp4
-rw-r--r--core/fpdftext/cpdf_linkextract.cpp74
-rw-r--r--core/fpdftext/cpdf_textpagefind.cpp27
-rw-r--r--core/fxcrt/cfx_bytestring.cpp47
-rw-r--r--core/fxcrt/cfx_bytestring.h16
-rw-r--r--core/fxcrt/cfx_bytestring_unittest.cpp117
-rw-r--r--core/fxcrt/cfx_string_c_template.h10
-rw-r--r--core/fxcrt/cfx_widestring.cpp39
-rw-r--r--core/fxcrt/cfx_widestring.h15
-rw-r--r--core/fxcrt/cfx_widestring_unittest.cpp82
-rw-r--r--core/fxcrt/fx_basic_util.cpp2
-rw-r--r--core/fxcrt/fx_system.h4
-rw-r--r--core/fxcrt/xml/cfx_xmlelement.cpp14
-rw-r--r--core/fxcrt/xml/cxml_parser.cpp8
-rw-r--r--core/fxge/android/cfpf_skiafontmgr.cpp4
-rw-r--r--core/fxge/apple/fx_mac_imp.cpp4
-rw-r--r--core/fxge/cfx_folderfontinfo.cpp9
-rw-r--r--core/fxge/cfx_font.cpp2
-rw-r--r--core/fxge/cfx_fontmapper.cpp47
-rw-r--r--core/fxge/cfx_renderdevice.cpp2
-rw-r--r--core/fxge/fx_ge_linux.cpp14
-rw-r--r--core/fxge/win32/fx_win32_device.cpp32
25 files changed, 398 insertions, 215 deletions
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
index 1f4d821dae..c7a98c9493 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
@@ -186,15 +186,18 @@ TEST_F(CPDF_PageContentGeneratorTest, ProcessStandardText) {
std::ostringstream buf;
TestProcessText(&generator, &buf, pTextObj.get());
CFX_ByteString textString(buf);
- FX_STRSIZE firstResourceAt = textString.Find('/') + 1;
- FX_STRSIZE secondResourceAt = textString.ReverseFind('/') + 1;
- EXPECT_NE(FX_STRNPOS, firstResourceAt);
- EXPECT_NE(FX_STRNPOS, secondResourceAt);
- CFX_ByteString firstString = textString.Left(firstResourceAt);
+ auto firstResourceAt = textString.Find('/');
+ ASSERT_TRUE(firstResourceAt.has_value());
+ firstResourceAt = firstResourceAt.value() + 1;
+ auto secondResourceAt = textString.ReverseFind('/');
+ ASSERT_TRUE(secondResourceAt.has_value());
+ secondResourceAt = secondResourceAt.value() + 1;
+ CFX_ByteString firstString = textString.Left(firstResourceAt.value());
CFX_ByteString midString =
- textString.Mid(firstResourceAt, secondResourceAt - firstResourceAt);
+ textString.Mid(firstResourceAt.value(),
+ secondResourceAt.value() - firstResourceAt.value());
CFX_ByteString lastString =
- textString.Right(textString.GetLength() - secondResourceAt);
+ textString.Right(textString.GetLength() - secondResourceAt.value());
// q and Q must be outside the BT .. ET operations
CFX_ByteString compareString1 =
"q 0.501961 0.701961 0.34902 rg 1 0.901961 0 RG /";
@@ -255,11 +258,12 @@ TEST_F(CPDF_PageContentGeneratorTest, ProcessText) {
}
CFX_ByteString textString(buf);
- FX_STRSIZE firstResourceAt = textString.Find('/') + 1;
- EXPECT_NE(FX_STRNPOS, firstResourceAt);
- CFX_ByteString firstString = textString.Left(firstResourceAt);
+ auto firstResourceAt = textString.Find('/');
+ ASSERT_TRUE(firstResourceAt.has_value());
+ firstResourceAt = firstResourceAt.value() + 1;
+ CFX_ByteString firstString = textString.Left(firstResourceAt.value());
CFX_ByteString lastString =
- textString.Right(textString.GetLength() - firstResourceAt);
+ textString.Right(textString.GetLength() - firstResourceAt.value());
// q and Q must be outside the BT .. ET operations
CFX_ByteString compareString1 = "q BT 1 0 0 1 0 0 Tm /";
CFX_ByteString compareString2 =
diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.cpp b/core/fpdfapi/page/cpdf_streamcontentparser.cpp
index 42ed3fc3b4..6f09f92c60 100644
--- a/core/fpdfapi/page/cpdf_streamcontentparser.cpp
+++ b/core/fpdfapi/page/cpdf_streamcontentparser.cpp
@@ -315,13 +315,13 @@ void CPDF_StreamContentParser::AddNameParam(const CFX_ByteStringC& bsName) {
m_pDocument->GetByteStringPool(), PDF_NameDecode(bsName));
} else {
param.m_Type = ContentParam::NAME;
- if (bsName.Find('#') == FX_STRNPOS) {
- memcpy(param.m_Name.m_Buffer, bsName.raw_str(), bsName.GetLength());
- param.m_Name.m_Len = bsName.GetLength();
- } else {
+ if (bsName.Contains('#')) {
CFX_ByteString str = PDF_NameDecode(bsName);
memcpy(param.m_Name.m_Buffer, str.c_str(), str.GetLength());
param.m_Name.m_Len = str.GetLength();
+ } else {
+ memcpy(param.m_Name.m_Buffer, bsName.raw_str(), bsName.GetLength());
+ param.m_Name.m_Len = bsName.GetLength();
}
}
}
diff --git a/core/fpdfapi/parser/fpdf_parser_utility.cpp b/core/fpdfapi/parser/fpdf_parser_utility.cpp
index 7cea16569f..a36d640da3 100644
--- a/core/fpdfapi/parser/fpdf_parser_utility.cpp
+++ b/core/fpdfapi/parser/fpdf_parser_utility.cpp
@@ -89,7 +89,7 @@ int32_t GetDirectInteger(CPDF_Dictionary* pDict, const CFX_ByteString& key) {
}
CFX_ByteString PDF_NameDecode(const CFX_ByteStringC& bstr) {
- if (bstr.Find('#') == FX_STRNPOS)
+ if (!bstr.Contains('#'))
return CFX_ByteString(bstr);
int size = bstr.GetLength();
@@ -110,9 +110,7 @@ CFX_ByteString PDF_NameDecode(const CFX_ByteStringC& bstr) {
}
CFX_ByteString PDF_NameDecode(const CFX_ByteString& orig) {
- if (orig.Find('#') == FX_STRNPOS)
- return orig;
- return PDF_NameDecode(orig.AsStringC());
+ return orig.Contains("#") ? PDF_NameDecode(orig.AsStringC()) : orig;
}
CFX_ByteString PDF_NameEncode(const CFX_ByteString& orig) {
diff --git a/core/fpdfdoc/cpdf_action.cpp b/core/fpdfdoc/cpdf_action.cpp
index dae7a7180e..557fbdafa9 100644
--- a/core/fpdfdoc/cpdf_action.cpp
+++ b/core/fpdfdoc/cpdf_action.cpp
@@ -96,8 +96,8 @@ CFX_ByteString CPDF_Action::GetURI(CPDF_Document* pDoc) const {
CPDF_Dictionary* pRoot = pDoc->GetRoot();
CPDF_Dictionary* pURI = pRoot->GetDictFor("URI");
if (pURI) {
- FX_STRSIZE ret = csURI.Find(":");
- if (ret == 0 || ret == FX_STRNPOS)
+ auto result = csURI.Find(":");
+ if (!result.has_value() || result.value() == 0)
csURI = pURI->GetStringFor("Base") + csURI;
}
return csURI;
diff --git a/core/fpdftext/cpdf_linkextract.cpp b/core/fpdftext/cpdf_linkextract.cpp
index fb228ec832..cfa9dbba7f 100644
--- a/core/fpdftext/cpdf_linkextract.cpp
+++ b/core/fpdftext/cpdf_linkextract.cpp
@@ -22,7 +22,7 @@ namespace {
FX_STRSIZE FindWebLinkEnding(const CFX_WideString& str,
FX_STRSIZE start,
FX_STRSIZE end) {
- if (str.Find(L'/', start) != FX_STRNPOS) {
+ if (str.Contains(L'/', start)) {
// When there is a path and query after '/', most ASCII chars are allowed.
// We don't sanitize in this case.
return end;
@@ -33,16 +33,20 @@ FX_STRSIZE FindWebLinkEnding(const CFX_WideString& str,
if (str[start] == L'[') {
// IPv6 reference.
// Find the end of the reference.
- end = str.Find(L']', start + 1);
- if (end != -1 && end > start + 1) { // Has content inside brackets.
- FX_STRSIZE len = str.GetLength();
- FX_STRSIZE off = end + 1;
- if (off < len && str[off] == L':') {
- off++;
- while (off < len && str[off] >= L'0' && str[off] <= L'9')
+ auto result = str.Find(L']', start + 1);
+ if (result.has_value()) {
+ end = result.value();
+ if (end > start + 1) { // Has content inside brackets.
+ FX_STRSIZE len = str.GetLength();
+ FX_STRSIZE off = end + 1;
+ if (off < len && str[off] == L':') {
off++;
- if (off > end + 2 && off <= len) // At least one digit in port number.
- end = off - 1; // |off| is offset of the first invalid char.
+ while (off < len && str[off] >= L'0' && str[off] <= L'9')
+ off++;
+ if (off > end + 2 &&
+ off <= len) // At least one digit in port number.
+ end = off - 1; // |off| is offset of the first invalid char.
+ }
}
}
return end;
@@ -196,20 +200,20 @@ bool CPDF_LinkExtract::CheckWebLink(CFX_WideString* strBeCheck,
FX_STRSIZE len = str.GetLength();
// First, try to find the scheme.
- FX_STRSIZE start = str.Find(kHttpScheme);
- if (start != FX_STRNPOS) {
- FX_STRSIZE off = start + kHttpSchemeLen; // move after "http".
+ auto start = str.Find(kHttpScheme);
+ if (start.has_value()) {
+ FX_STRSIZE off = start.value() + kHttpSchemeLen; // move after "http".
if (len > off + 4) { // At least "://<char>" follows.
if (str[off] == L's') // "https" scheme is accepted.
off++;
if (str[off] == L':' && str[off + 1] == L'/' && str[off + 2] == L'/') {
off += 3;
- FX_STRSIZE end =
- TrimExternalBracketsFromWebLink(str, start, str.GetLength() - 1);
+ FX_STRSIZE end = TrimExternalBracketsFromWebLink(str, start.value(),
+ str.GetLength() - 1);
end = FindWebLinkEnding(str, off, end);
if (end > off) { // Non-empty host name.
- *nStart = start;
- *nCount = end - start + 1;
+ *nStart = start.value();
+ *nCount = end - start.value() + 1;
*strBeCheck = strBeCheck->Mid(*nStart, *nCount);
return true;
}
@@ -219,13 +223,13 @@ bool CPDF_LinkExtract::CheckWebLink(CFX_WideString* strBeCheck,
// When there is no scheme, try to find url starting with "www.".
start = str.Find(kWWWAddrStart);
- if (start != FX_STRNPOS && len > start + kWWWAddrStartLen) {
- FX_STRSIZE end =
- TrimExternalBracketsFromWebLink(str, start, str.GetLength() - 1);
- end = FindWebLinkEnding(str, start, end);
- if (end > start + kWWWAddrStartLen) {
- *nStart = start;
- *nCount = end - start + 1;
+ if (start.has_value() && len > start.value() + kWWWAddrStartLen) {
+ FX_STRSIZE end = TrimExternalBracketsFromWebLink(str, start.value(),
+ str.GetLength() - 1);
+ end = FindWebLinkEnding(str, start.value(), end);
+ if (end > start.value() + kWWWAddrStartLen) {
+ *nStart = start.value();
+ *nCount = end - start.value() + 1;
*strBeCheck = L"http://" + strBeCheck->Mid(*nStart, *nCount);
return true;
}
@@ -234,20 +238,20 @@ bool CPDF_LinkExtract::CheckWebLink(CFX_WideString* strBeCheck,
}
bool CPDF_LinkExtract::CheckMailLink(CFX_WideString* str) {
- FX_STRSIZE aPos = str->Find(L'@');
+ auto aPos = str->Find(L'@');
// Invalid when no '@' or when starts/ends with '@'.
- if (aPos == FX_STRNPOS || aPos == 0 || aPos == str->GetLength() - 1)
+ if (!aPos.has_value() || aPos.value() == 0 || aPos == str->GetLength() - 1)
return false;
// Check the local part.
- int pPos = aPos; // Used to track the position of '@' or '.'.
- for (int i = aPos - 1; i >= 0; i--) {
+ int pPos = aPos.value(); // Used to track the position of '@' or '.'.
+ for (int i = aPos.value() - 1; i >= 0; i--) {
wchar_t ch = (*str)[i];
if (ch == L'_' || ch == L'-' || FXSYS_iswalnum(ch))
continue;
if (ch != L'.' || i == pPos - 1 || i == 0) {
- if (i == aPos - 1) {
+ if (i == aPos.value() - 1) {
// There is '.' or invalid char before '@'.
return false;
}
@@ -263,21 +267,21 @@ bool CPDF_LinkExtract::CheckMailLink(CFX_WideString* str) {
// Check the domain name part.
aPos = str->Find(L'@');
- if (aPos < 1 || aPos == FX_STRNPOS)
+ if (!aPos.has_value() || aPos.value() == 0)
return false;
str->TrimRight(L'.');
// At least one '.' in domain name, but not at the beginning.
// TODO(weili): RFC5322 allows domain names to be a local name without '.'.
// Check whether we should remove this check.
- FX_STRSIZE ePos = str->Find(L'.', aPos + 1);
- if (ePos == FX_STRNPOS || ePos == aPos + 1)
+ auto ePos = str->Find(L'.', aPos.value() + 1);
+ if (!ePos.has_value() || ePos.value() == aPos.value() + 1)
return false;
// Validate all other chars in domain name.
int nLen = str->GetLength();
pPos = 0; // Used to track the position of '.'.
- for (int i = aPos + 1; i < nLen; i++) {
+ for (int i = aPos.value() + 1; i < nLen; i++) {
wchar_t wch = (*str)[i];
if (wch == L'-' || FXSYS_iswalnum(wch))
continue;
@@ -285,7 +289,7 @@ bool CPDF_LinkExtract::CheckMailLink(CFX_WideString* str) {
if (wch != L'.' || i == pPos + 1) {
// Domain name should end before invalid char.
int host_end = i == pPos + 1 ? i - 2 : i - 1;
- if (pPos > 0 && host_end - aPos >= 3) {
+ if (pPos > 0 && host_end - aPos.value() >= 3) {
// Trim the ending invalid chars if there is at least one '.' and name.
*str = str->Left(host_end + 1);
break;
@@ -295,7 +299,7 @@ bool CPDF_LinkExtract::CheckMailLink(CFX_WideString* str) {
pPos = i;
}
- if (str->Find(L"mailto:") == FX_STRNPOS)
+ if (!str->Contains(L"mailto:"))
*str = L"mailto:" + *str;
return true;
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);
diff --git a/core/fxcrt/cfx_bytestring.cpp b/core/fxcrt/cfx_bytestring.cpp
index cd049dcc5d..f6074e961b 100644
--- a/core/fxcrt/cfx_bytestring.cpp
+++ b/core/fxcrt/cfx_bytestring.cpp
@@ -564,43 +564,48 @@ CFX_ByteString CFX_ByteString::Left(FX_STRSIZE nCount) const {
return dest;
}
-FX_STRSIZE CFX_ByteString::Find(char ch, FX_STRSIZE nStart) const {
+pdfium::Optional<FX_STRSIZE> CFX_ByteString::Find(char ch,
+ FX_STRSIZE nStart) const {
if (!m_pData)
- return FX_STRNPOS;
+ return pdfium::Optional<FX_STRSIZE>();
if (nStart < 0 || nStart >= m_pData->m_nDataLength)
- return FX_STRNPOS;
+ return pdfium::Optional<FX_STRSIZE>();
const char* pStr = static_cast<const char*>(
memchr(m_pData->m_String + nStart, ch, m_pData->m_nDataLength - nStart));
- return pStr ? pStr - m_pData->m_String : FX_STRNPOS;
+ return pStr ? pdfium::Optional<FX_STRSIZE>(
+ static_cast<FX_STRSIZE>(pStr - m_pData->m_String))
+ : pdfium::Optional<FX_STRSIZE>();
}
-FX_STRSIZE CFX_ByteString::ReverseFind(char ch) const {
+pdfium::Optional<FX_STRSIZE> CFX_ByteString::Find(const CFX_ByteStringC& pSub,
+ FX_STRSIZE nStart) const {
if (!m_pData)
- return FX_STRNPOS;
-
- FX_STRSIZE nLength = m_pData->m_nDataLength;
- while (nLength--) {
- if (m_pData->m_String[nLength] == ch)
- return nLength;
- }
- return FX_STRNPOS;
-}
-
-FX_STRSIZE CFX_ByteString::Find(const CFX_ByteStringC& pSub,
- FX_STRSIZE nStart) const {
- if (!m_pData)
- return FX_STRNPOS;
+ return pdfium::Optional<FX_STRSIZE>();
FX_STRSIZE nLength = m_pData->m_nDataLength;
if (nStart > nLength)
- return FX_STRNPOS;
+ return pdfium::Optional<FX_STRSIZE>();
const char* pStr =
FX_strstr(m_pData->m_String + nStart, m_pData->m_nDataLength - nStart,
pSub.unterminated_c_str(), pSub.GetLength());
- return pStr ? (int)(pStr - m_pData->m_String) : FX_STRNPOS;
+ return pStr ? pdfium::Optional<FX_STRSIZE>(
+ static_cast<FX_STRSIZE>(pStr - m_pData->m_String))
+ : pdfium::Optional<FX_STRSIZE>();
+}
+
+pdfium::Optional<FX_STRSIZE> CFX_ByteString::ReverseFind(char ch) const {
+ if (!m_pData)
+ return pdfium::Optional<FX_STRSIZE>();
+
+ FX_STRSIZE nLength = m_pData->m_nDataLength;
+ while (nLength--) {
+ if (m_pData->m_String[nLength] == ch)
+ return pdfium::Optional<FX_STRSIZE>(nLength);
+ }
+ return pdfium::Optional<FX_STRSIZE>();
}
void CFX_ByteString::MakeLower() {
diff --git a/core/fxcrt/cfx_bytestring.h b/core/fxcrt/cfx_bytestring.h
index 0e8e006a3c..10675be7a5 100644
--- a/core/fxcrt/cfx_bytestring.h
+++ b/core/fxcrt/cfx_bytestring.h
@@ -16,6 +16,7 @@
#include "core/fxcrt/cfx_string_data_template.h"
#include "core/fxcrt/fx_memory.h"
#include "core/fxcrt/fx_system.h"
+#include "third_party/base/optional.h"
class CFX_WideString;
@@ -130,9 +131,18 @@ class CFX_ByteString {
CFX_ByteString Left(FX_STRSIZE count) const;
CFX_ByteString Right(FX_STRSIZE count) const;
- FX_STRSIZE Find(const CFX_ByteStringC& lpszSub, FX_STRSIZE start = 0) const;
- FX_STRSIZE Find(char ch, FX_STRSIZE start = 0) const;
- FX_STRSIZE ReverseFind(char ch) const;
+ pdfium::Optional<FX_STRSIZE> Find(const CFX_ByteStringC& lpszSub,
+ FX_STRSIZE start = 0) const;
+ pdfium::Optional<FX_STRSIZE> Find(char ch, FX_STRSIZE start = 0) const;
+ pdfium::Optional<FX_STRSIZE> ReverseFind(char ch) const;
+
+ bool Contains(const CFX_ByteStringC& lpszSub, FX_STRSIZE start = 0) const {
+ return Find(lpszSub, start).has_value();
+ }
+
+ bool Contains(char ch, FX_STRSIZE start = 0) const {
+ return Find(ch, start).has_value();
+ }
void MakeLower();
void MakeUpper();
diff --git a/core/fxcrt/cfx_bytestring_unittest.cpp b/core/fxcrt/cfx_bytestring_unittest.cpp
index 35f407f6ec..e7281546fb 100644
--- a/core/fxcrt/cfx_bytestring_unittest.cpp
+++ b/core/fxcrt/cfx_bytestring_unittest.cpp
@@ -317,7 +317,7 @@ TEST(fxcrt, ByteStringCNull) {
EXPECT_EQ(null_string, assigned_null_string);
CFX_ByteStringC assigned_nullptr_string("initially not nullptr");
- assigned_nullptr_string = (const char*)nullptr;
+ assigned_nullptr_string = nullptr;
EXPECT_FALSE(assigned_nullptr_string.raw_str());
EXPECT_EQ(assigned_nullptr_string.GetLength(), 0);
EXPECT_TRUE(assigned_nullptr_string.IsEmpty());
@@ -568,6 +568,89 @@ TEST(fxcrt, ByteStringRight) {
EXPECT_EQ("", empty.Right(-1));
}
+TEST(fxcrt, ByteStringFind) {
+ CFX_ByteString null_string;
+ EXPECT_FALSE(null_string.Find('a').has_value());
+ EXPECT_FALSE(null_string.Find('\0').has_value());
+
+ CFX_ByteString empty_string("");
+ EXPECT_FALSE(empty_string.Find('a').has_value());
+ EXPECT_FALSE(empty_string.Find('\0').has_value());
+
+ pdfium::Optional<FX_STRSIZE> result;
+ CFX_ByteString single_string("a");
+ result = single_string.Find('a');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ EXPECT_FALSE(single_string.Find('b').has_value());
+ EXPECT_FALSE(single_string.Find('\0').has_value());
+
+ CFX_ByteString longer_string("abccc");
+ result = longer_string.Find('a');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ result = longer_string.Find('c');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
+ result = longer_string.Find('c', 3);
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(3, result.value());
+ EXPECT_FALSE(longer_string.Find('d').has_value());
+ EXPECT_FALSE(longer_string.Find('\0').has_value());
+
+ result = longer_string.Find("ab");
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ result = longer_string.Find("ccc");
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
+ result = longer_string.Find("cc", 3);
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(3, result.value());
+ EXPECT_FALSE(longer_string.Find("d").has_value());
+
+ CFX_ByteString hibyte_string(
+ "ab\x8c"
+ "def");
+ result = hibyte_string.Find('\x8c');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
+}
+
+TEST(fxcrt, ByteStringReverseFind) {
+ CFX_ByteString null_string;
+ EXPECT_FALSE(null_string.ReverseFind('a').has_value());
+ EXPECT_FALSE(null_string.ReverseFind('\0').has_value());
+
+ CFX_ByteString empty_string("");
+ EXPECT_FALSE(empty_string.ReverseFind('a').has_value());
+ EXPECT_FALSE(empty_string.ReverseFind('\0').has_value());
+
+ pdfium::Optional<FX_STRSIZE> result;
+ CFX_ByteString single_string("a");
+ result = single_string.ReverseFind('a');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ EXPECT_FALSE(single_string.ReverseFind('b').has_value());
+ EXPECT_FALSE(single_string.ReverseFind('\0').has_value());
+
+ CFX_ByteString longer_string("abccc");
+ result = longer_string.ReverseFind('a');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ result = longer_string.ReverseFind('c');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(4, result.value());
+ EXPECT_FALSE(longer_string.ReverseFind('\0').has_value());
+
+ CFX_ByteString hibyte_string(
+ "ab\x8c"
+ "def");
+ result = hibyte_string.ReverseFind('\x8c');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
+}
+
TEST(fxcrt, ByteStringUpperLower) {
CFX_ByteString fred("F-Re.42D");
fred.MakeLower();
@@ -882,27 +965,37 @@ TEST(fxcrt, ByteStringCGetID) {
TEST(fxcrt, ByteStringCFind) {
CFX_ByteStringC null_string;
- EXPECT_EQ(FX_STRNPOS, null_string.Find('a'));
- EXPECT_EQ(FX_STRNPOS, null_string.Find(0));
+ EXPECT_FALSE(null_string.Find('a').has_value());
+ EXPECT_FALSE(null_string.Find('\0').has_value());
CFX_ByteStringC empty_string("");
- EXPECT_EQ(FX_STRNPOS, empty_string.Find('a'));
- EXPECT_EQ(FX_STRNPOS, empty_string.Find(0));
+ EXPECT_FALSE(empty_string.Find('a').has_value());
+ EXPECT_FALSE(empty_string.Find('\0').has_value());
+ pdfium::Optional<FX_STRSIZE> result;
CFX_ByteStringC single_string("a");
- EXPECT_EQ(0, single_string.Find('a'));
- EXPECT_EQ(FX_STRNPOS, single_string.Find('b'));
- EXPECT_EQ(FX_STRNPOS, single_string.Find(0));
+ result = single_string.Find('a');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ EXPECT_FALSE(single_string.Find('b').has_value());
+ EXPECT_FALSE(single_string.Find('\0').has_value());
CFX_ByteStringC longer_string("abccc");
- EXPECT_EQ(0, longer_string.Find('a'));
- EXPECT_EQ(2, longer_string.Find('c'));
- EXPECT_EQ(FX_STRNPOS, longer_string.Find(0));
+ result = longer_string.Find('a');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ result = longer_string.Find('c');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
+ EXPECT_FALSE(longer_string.Find('d').has_value());
+ EXPECT_FALSE(longer_string.Find('\0').has_value());
CFX_ByteStringC hibyte_string(
"ab\x8c"
"def");
- EXPECT_EQ(2, hibyte_string.Find('\x8c'));
+ result = hibyte_string.Find('\x8c');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
}
TEST(fxcrt, ByteStringCMid) {
diff --git a/core/fxcrt/cfx_string_c_template.h b/core/fxcrt/cfx_string_c_template.h
index 99948e90a0..db8b274410 100644
--- a/core/fxcrt/cfx_string_c_template.h
+++ b/core/fxcrt/cfx_string_c_template.h
@@ -9,10 +9,12 @@
#include <algorithm>
#include <type_traits>
+#include <utility>
#include <vector>
#include "core/fxcrt/cfx_unowned_ptr.h"
#include "core/fxcrt/fx_system.h"
+#include "third_party/base/optional.h"
#include "third_party/base/stl_util.h"
// An immutable string with caller-provided storage which must outlive the
@@ -129,12 +131,16 @@ class CFX_StringCTemplate {
return static_cast<CharType>(m_Ptr.Get()[index]);
}
- FX_STRSIZE Find(CharType ch) const {
+ pdfium::Optional<FX_STRSIZE> Find(CharType ch) const {
const UnsignedType* found = reinterpret_cast<const UnsignedType*>(FXSYS_chr(
reinterpret_cast<const CharType*>(m_Ptr.Get()), ch, m_Length));
- return found ? found - m_Ptr.Get() : FX_STRNPOS;
+
+ return found ? pdfium::Optional<FX_STRSIZE>(found - m_Ptr.Get())
+ : pdfium::Optional<FX_STRSIZE>();
}
+ bool Contains(CharType ch) const { return Find(ch).has_value(); }
+
CFX_StringCTemplate Mid(FX_STRSIZE index, FX_STRSIZE count) const {
ASSERT(count >= 0);
if (index > m_Length)
diff --git a/core/fxcrt/cfx_widestring.cpp b/core/fxcrt/cfx_widestring.cpp
index 01fc8c764e..76fdf24b2c 100644
--- a/core/fxcrt/cfx_widestring.cpp
+++ b/core/fxcrt/cfx_widestring.cpp
@@ -54,7 +54,8 @@ const wchar_t* FX_wcsstr(const wchar_t* haystack,
return nullptr;
}
-FX_STRSIZE GuessSizeForVSWPrintf(const wchar_t* pFormat, va_list argList) {
+pdfium::Optional<FX_STRSIZE> GuessSizeForVSWPrintf(const wchar_t* pFormat,
+ va_list argList) {
FX_STRSIZE nMaxLen = 0;
for (const wchar_t* pStr = pFormat; *pStr != 0; pStr++) {
if (*pStr != '%' || *(pStr = pStr + 1) == '%') {
@@ -78,7 +79,7 @@ FX_STRSIZE GuessSizeForVSWPrintf(const wchar_t* pFormat, va_list argList) {
++pStr;
}
if (nWidth < 0 || nWidth > 128 * 1024)
- return FX_STRNPOS;
+ return pdfium::Optional<FX_STRSIZE>();
int nPrecision = 0;
if (*pStr == '.') {
pStr++;
@@ -92,7 +93,7 @@ FX_STRSIZE GuessSizeForVSWPrintf(const wchar_t* pFormat, va_list argList) {
}
}
if (nPrecision < 0 || nPrecision > 128 * 1024)
- return FX_STRNPOS;
+ return pdfium::Optional<FX_STRSIZE>();
int nModifier = 0;
if (*pStr == L'I' && *(pStr + 1) == L'6' && *(pStr + 2) == L'4') {
pStr += 3;
@@ -241,7 +242,7 @@ FX_STRSIZE GuessSizeForVSWPrintf(const wchar_t* pFormat, va_list argList) {
nMaxLen += nItemLen;
}
nMaxLen += 32; // Fudge factor.
- return nMaxLen;
+ return pdfium::Optional<FX_STRSIZE>(nMaxLen);
}
#ifndef NDEBUG
@@ -656,9 +657,10 @@ void CFX_WideString::FormatV(const wchar_t* pFormat, va_list argList) {
FX_STRSIZE nMaxLen = vswprintf(nullptr, 0, pFormat, argListCopy);
va_end(argListCopy);
if (nMaxLen <= 0) {
- nMaxLen = GuessSizeForVSWPrintf(pFormat, argListCopy);
- if (nMaxLen == FX_STRNPOS)
+ auto guess = GuessSizeForVSWPrintf(pFormat, argListCopy);
+ if (!guess.has_value())
return;
+ nMaxLen = guess.value();
}
while (nMaxLen < 32 * 1024) {
FX_VA_COPY(argListCopy, argList);
@@ -717,31 +719,36 @@ CFX_WideString CFX_WideString::Left(FX_STRSIZE nCount) const {
return dest;
}
-FX_STRSIZE CFX_WideString::Find(wchar_t ch, FX_STRSIZE nStart) const {
+pdfium::Optional<FX_STRSIZE> CFX_WideString::Find(wchar_t ch,
+ FX_STRSIZE nStart) const {
if (!m_pData)
- return FX_STRNPOS;
+ return pdfium::Optional<FX_STRSIZE>();
if (nStart < 0 || nStart >= m_pData->m_nDataLength)
- return FX_STRNPOS;
+ return pdfium::Optional<FX_STRSIZE>();
const wchar_t* pStr =
wmemchr(m_pData->m_String + nStart, ch, m_pData->m_nDataLength - nStart);
- return pStr ? pStr - m_pData->m_String : FX_STRNPOS;
+ return pStr ? pdfium::Optional<FX_STRSIZE>(
+ static_cast<FX_STRSIZE>(pStr - m_pData->m_String))
+ : pdfium::Optional<FX_STRSIZE>();
}
-FX_STRSIZE CFX_WideString::Find(const CFX_WideStringC& pSub,
- FX_STRSIZE nStart) const {
+pdfium::Optional<FX_STRSIZE> CFX_WideString::Find(const CFX_WideStringC& pSub,
+ FX_STRSIZE nStart) const {
if (!m_pData)
- return FX_STRNPOS;
+ return pdfium::Optional<FX_STRSIZE>();
FX_STRSIZE nLength = m_pData->m_nDataLength;
if (nStart > nLength)
- return FX_STRNPOS;
+ return pdfium::Optional<FX_STRSIZE>();
const wchar_t* pStr =
FX_wcsstr(m_pData->m_String + nStart, m_pData->m_nDataLength - nStart,
pSub.unterminated_c_str(), pSub.GetLength());
- return pStr ? (int)(pStr - m_pData->m_String) : FX_STRNPOS;
+ return pStr ? pdfium::Optional<FX_STRSIZE>(
+ static_cast<FX_STRSIZE>(pStr - m_pData->m_String))
+ : pdfium::Optional<FX_STRSIZE>();
}
void CFX_WideString::MakeLower() {
@@ -942,7 +949,7 @@ void CFX_WideString::TrimRight(const CFX_WideStringC& pTargets) {
return;
FX_STRSIZE pos = GetLength();
- while (pos && pTargets.Find(m_pData->m_String[pos - 1]) != -1)
+ while (pos && pTargets.Contains(m_pData->m_String[pos - 1]))
pos--;
if (pos < m_pData->m_nDataLength) {
diff --git a/core/fxcrt/cfx_widestring.h b/core/fxcrt/cfx_widestring.h
index f9e800d0a2..a6d0eca044 100644
--- a/core/fxcrt/cfx_widestring.h
+++ b/core/fxcrt/cfx_widestring.h
@@ -15,6 +15,7 @@
#include "core/fxcrt/cfx_string_data_template.h"
#include "core/fxcrt/fx_memory.h"
#include "core/fxcrt/fx_system.h"
+#include "third_party/base/optional.h"
class CFX_ByteString;
@@ -142,8 +143,18 @@ class CFX_WideString {
int GetInteger() const;
float GetFloat() const;
- FX_STRSIZE Find(const CFX_WideStringC& pSub, FX_STRSIZE start = 0) const;
- FX_STRSIZE Find(wchar_t ch, FX_STRSIZE start = 0) const;
+ pdfium::Optional<FX_STRSIZE> Find(const CFX_WideStringC& pSub,
+ FX_STRSIZE start = 0) const;
+ pdfium::Optional<FX_STRSIZE> Find(wchar_t ch, FX_STRSIZE start = 0) const;
+
+ bool Contains(const CFX_WideStringC& lpszSub, FX_STRSIZE start = 0) const {
+ return Find(lpszSub, start).has_value();
+ }
+
+ bool Contains(char ch, FX_STRSIZE start = 0) const {
+ return Find(ch, start).has_value();
+ }
+
FX_STRSIZE Replace(const CFX_WideStringC& pOld, const CFX_WideStringC& pNew);
FX_STRSIZE Remove(wchar_t ch);
diff --git a/core/fxcrt/cfx_widestring_unittest.cpp b/core/fxcrt/cfx_widestring_unittest.cpp
index e7206dde06..e688a5334e 100644
--- a/core/fxcrt/cfx_widestring_unittest.cpp
+++ b/core/fxcrt/cfx_widestring_unittest.cpp
@@ -527,6 +527,54 @@ TEST(fxcrt, WideStringRight) {
EXPECT_EQ(L"", empty.Right(-1));
}
+TEST(fxcrt, WideStringFind) {
+ CFX_WideString null_string;
+ EXPECT_FALSE(null_string.Find(L'a').has_value());
+ EXPECT_FALSE(null_string.Find(L'\0').has_value());
+
+ CFX_WideString empty_string(L"");
+ EXPECT_FALSE(empty_string.Find(L'a').has_value());
+ EXPECT_FALSE(empty_string.Find(L'\0').has_value());
+
+ pdfium::Optional<FX_STRSIZE> result;
+ CFX_WideString single_string(L"a");
+ result = single_string.Find(L'a');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ EXPECT_FALSE(single_string.Find(L'b').has_value());
+ EXPECT_FALSE(single_string.Find(L'\0').has_value());
+
+ CFX_WideString longer_string(L"abccc");
+ result = longer_string.Find(L'a');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ result = longer_string.Find(L'c');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
+ result = longer_string.Find(L'c', 3);
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(3, result.value());
+ EXPECT_FALSE(longer_string.Find(L'\0').has_value());
+
+ result = longer_string.Find(L"ab");
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ result = longer_string.Find(L"ccc");
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
+ result = longer_string.Find(L"cc", 3);
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(3, result.value());
+ EXPECT_FALSE(longer_string.Find(L"d").has_value());
+
+ CFX_WideString hibyte_string(
+ L"ab\xff8c"
+ L"def");
+ result = hibyte_string.Find(L'\xff8c');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
+}
+
TEST(fxcrt, WideStringUpperLower) {
CFX_WideString fred(L"F-Re.42D");
fred.MakeLower();
@@ -924,27 +972,37 @@ TEST(fxcrt, WideStringCOperatorNE) {
TEST(fxcrt, WideStringCFind) {
CFX_WideStringC null_string;
- EXPECT_EQ(FX_STRNPOS, null_string.Find(L'a'));
- EXPECT_EQ(FX_STRNPOS, null_string.Find(0));
+ EXPECT_FALSE(null_string.Find(L'a').has_value());
+ EXPECT_FALSE(null_string.Find(L'\0').has_value());
CFX_WideStringC empty_string(L"");
- EXPECT_EQ(FX_STRNPOS, empty_string.Find(L'a'));
- EXPECT_EQ(FX_STRNPOS, empty_string.Find(0));
+ EXPECT_FALSE(empty_string.Find(L'a').has_value());
+ EXPECT_FALSE(empty_string.Find(L'\0').has_value());
+ pdfium::Optional<FX_STRSIZE> result;
CFX_WideStringC single_string(L"a");
- EXPECT_EQ(0, single_string.Find(L'a'));
- EXPECT_EQ(FX_STRNPOS, single_string.Find(L'b'));
- EXPECT_EQ(FX_STRNPOS, single_string.Find(0));
+ result = single_string.Find(L'a');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ EXPECT_FALSE(single_string.Find(L'b').has_value());
+ EXPECT_FALSE(single_string.Find(L'\0').has_value());
CFX_WideStringC longer_string(L"abccc");
- EXPECT_EQ(0, longer_string.Find(L'a'));
- EXPECT_EQ(2, longer_string.Find(L'c'));
- EXPECT_EQ(FX_STRNPOS, longer_string.Find(0));
+ result = longer_string.Find(L'a');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(0, result.value());
+ result = longer_string.Find(L'c');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
+ EXPECT_FALSE(longer_string.Find(L'd').has_value());
+ EXPECT_FALSE(longer_string.Find(L'\0').has_value());
CFX_WideStringC hibyte_string(
- L"ab\xff08"
+ L"ab\xFF8c"
L"def");
- EXPECT_EQ(2, hibyte_string.Find(L'\xff08'));
+ result = hibyte_string.Find(L'\xFF8c');
+ ASSERT_TRUE(result.has_value());
+ EXPECT_EQ(2, result.value());
}
TEST(fxcrt, WideStringCNullIterator) {
diff --git a/core/fxcrt/fx_basic_util.cpp b/core/fxcrt/fx_basic_util.cpp
index 194f6d72ef..05ab20472c 100644
--- a/core/fxcrt/fx_basic_util.cpp
+++ b/core/fxcrt/fx_basic_util.cpp
@@ -14,7 +14,7 @@
#include "third_party/base/ptr_util.h"
bool FX_atonum(const CFX_ByteStringC& strc, void* pData) {
- if (strc.Find('.') != FX_STRNPOS) {
+ if (strc.Contains('.')) {
float* pFloat = static_cast<float*>(pData);
*pFloat = FX_atof(strc);
return false;
diff --git a/core/fxcrt/fx_system.h b/core/fxcrt/fx_system.h
index 47d588c521..ad7e6bbcd7 100644
--- a/core/fxcrt/fx_system.h
+++ b/core/fxcrt/fx_system.h
@@ -121,10 +121,6 @@ typedef int FX_STRSIZE;
#include "third_party/base/numerics/safe_conversions.h"
-// Constant used to indicate failure from find methods and other methods that
-// return FX_STRSIZE.
-constexpr FX_STRSIZE FX_STRNPOS = -1;
-
#define FXSYS_strlen(ptr) pdfium::base::checked_cast<FX_STRSIZE>(strlen(ptr))
#define FXSYS_wcslen(ptr) pdfium::base::checked_cast<FX_STRSIZE>(wcslen(ptr))
diff --git a/core/fxcrt/xml/cfx_xmlelement.cpp b/core/fxcrt/xml/cfx_xmlelement.cpp
index 75bf9ee593..eb60e68893 100644
--- a/core/fxcrt/xml/cfx_xmlelement.cpp
+++ b/core/fxcrt/xml/cfx_xmlelement.cpp
@@ -42,17 +42,15 @@ std::unique_ptr<CFX_XMLNode> CFX_XMLElement::Clone() {
}
CFX_WideString CFX_XMLElement::GetLocalTagName() const {
- FX_STRSIZE iFind = GetName().Find(L':', 0);
- if (iFind == FX_STRNPOS)
- return GetName();
- return GetName().Right(GetName().GetLength() - iFind - 1);
+ auto pos = GetName().Find(L':');
+ return pos.has_value()
+ ? GetName().Right(GetName().GetLength() - pos.value() - 1)
+ : GetName();
}
CFX_WideString CFX_XMLElement::GetNamespacePrefix() const {
- FX_STRSIZE iFind = GetName().Find(L':', 0);
- if (iFind == FX_STRNPOS)
- return CFX_WideString();
- return GetName().Left(iFind);
+ auto pos = GetName().Find(L':');
+ return pos.has_value() ? GetName().Left(pos.value()) : CFX_WideString();
}
CFX_WideString CFX_XMLElement::GetNamespaceURI() const {
diff --git a/core/fxcrt/xml/cxml_parser.cpp b/core/fxcrt/xml/cxml_parser.cpp
index ebd5873b33..18103dfc5e 100644
--- a/core/fxcrt/xml/cxml_parser.cpp
+++ b/core/fxcrt/xml/cxml_parser.cpp
@@ -82,12 +82,12 @@ void FX_XML_SplitQualifiedName(const CFX_ByteStringC& bsFullName,
if (bsFullName.IsEmpty())
return;
- FX_STRSIZE iStart = bsFullName.Find(':');
- if (iStart == FX_STRNPOS) {
+ auto iStart = bsFullName.Find(':');
+ if (!iStart.has_value()) {
bsName = bsFullName;
} else {
- bsSpace = bsFullName.Left(iStart);
- bsName = bsFullName.Right(bsFullName.GetLength() - (iStart + 1));
+ bsSpace = bsFullName.Left(iStart.value());
+ bsName = bsFullName.Right(bsFullName.GetLength() - (iStart.value() + 1));
}
}
diff --git a/core/fxge/android/cfpf_skiafontmgr.cpp b/core/fxge/android/cfpf_skiafontmgr.cpp
index 3cee6decac..16a6df5f8f 100644
--- a/core/fxge/android/cfpf_skiafontmgr.cpp
+++ b/core/fxge/android/cfpf_skiafontmgr.cpp
@@ -194,13 +194,13 @@ bool FPF_SkiaIsCJK(uint8_t uCharset) {
bool FPF_SkiaMaybeSymbol(const CFX_ByteStringC& bsFacename) {
CFX_ByteString bsName(bsFacename);
bsName.MakeLower();
- return bsName.Find("symbol") != FX_STRNPOS;
+ return bsName.Contains("symbol");
}
bool FPF_SkiaMaybeArabic(const CFX_ByteStringC& bsFacename) {
CFX_ByteString bsName(bsFacename);
bsName.MakeLower();
- return bsName.Find("arabic") != FX_STRNPOS;
+ return bsName.Contains("arabic");
}
const uint32_t g_FPFSkiaFontCharsets[] = {
diff --git a/core/fxge/apple/fx_mac_imp.cpp b/core/fxge/apple/fx_mac_imp.cpp
index 54aac7bfa6..2eee8e2e62 100644
--- a/core/fxge/apple/fx_mac_imp.cpp
+++ b/core/fxge/apple/fx_mac_imp.cpp
@@ -52,7 +52,7 @@ const char JAPAN_GOTHIC[] = "Hiragino Kaku Gothic Pro W6";
const char JAPAN_MINCHO[] = "Hiragino Mincho Pro W6";
void GetJapanesePreference(CFX_ByteString* face, int weight, int pitch_family) {
- if (face->Find("Gothic") != FX_STRNPOS) {
+ if (face->Contains("Gothic")) {
*face = JAPAN_GOTHIC;
return;
}
@@ -82,7 +82,7 @@ void* CFX_MacFontInfo::MapFont(int weight,
// Times New Roman. A more sophisticated approach would be to find all the
// fonts in |m_FontList| with |face| in the name, and examine the fonts to
// see which best matches the requested characteristics.
- if (face.Find("Bold") == FX_STRNPOS && face.Find("Italic") == FX_STRNPOS) {
+ if (!face.Contains("Bold") && !face.Contains("Italic")) {
CFX_ByteString new_face = face;
if (weight > 400)
new_face += " Bold";
diff --git a/core/fxge/cfx_folderfontinfo.cpp b/core/fxge/cfx_folderfontinfo.cpp
index f16722c4d6..c82d55982a 100644
--- a/core/fxge/cfx_folderfontinfo.cpp
+++ b/core/fxge/cfx_folderfontinfo.cpp
@@ -251,11 +251,11 @@ void CFX_FolderFontInfo::ReportFace(const CFX_ByteString& path,
m_pMapper->AddInstalledFont(facename, FX_CHARSET_ANSI);
pInfo->m_Charsets |= CHARSET_FLAG_ANSI;
pInfo->m_Styles = 0;
- if (style.Find("Bold") != FX_STRNPOS)
+ if (style.Contains("Bold"))
pInfo->m_Styles |= FXFONT_BOLD;
- if (style.Find("Italic") != FX_STRNPOS || style.Find("Oblique") != FX_STRNPOS)
+ if (style.Contains("Italic") || style.Contains("Oblique"))
pInfo->m_Styles |= FXFONT_ITALIC;
- if (facename.Find("Serif") != FX_STRNPOS)
+ if (facename.Contains("Serif"))
pInfo->m_Styles |= FXFONT_SERIF;
m_FontList[facename] = std::move(pInfo);
@@ -288,8 +288,7 @@ void* CFX_FolderFontInfo::FindFont(int weight,
if (!(pFont->m_Charsets & charset_flag) && charset != FX_CHARSET_Default)
continue;
- int32_t index = bsName.Find(family);
- if (bMatchName && index == FX_STRNPOS)
+ if (bMatchName && !bsName.Contains(family))
continue;
int32_t iSimilarValue =
diff --git a/core/fxge/cfx_font.cpp b/core/fxge/cfx_font.cpp
index 1fd08f2676..8b34819e05 100644
--- a/core/fxge/cfx_font.cpp
+++ b/core/fxge/cfx_font.cpp
@@ -447,7 +447,7 @@ bool CFX_Font::IsItalic() const {
CFX_ByteString str(FXFT_Get_Face_Style_Name(m_Face));
str.MakeLower();
- return str.Find("italic") != FX_STRNPOS;
+ return str.Contains("italic");
}
bool CFX_Font::IsBold() const {
diff --git a/core/fxge/cfx_fontmapper.cpp b/core/fxge/cfx_fontmapper.cpp
index 005c61ae8a..bff9fad5c3 100644
--- a/core/fxge/cfx_fontmapper.cpp
+++ b/core/fxge/cfx_fontmapper.cpp
@@ -172,7 +172,7 @@ const struct CODEPAGE_MAP {
int CompareFontFamilyString(const void* key, const void* element) {
CFX_ByteString str_key((const char*)key);
const AltFontFamily* family = reinterpret_cast<const AltFontFamily*>(element);
- if (str_key.Find(family->m_pFontName) != FX_STRNPOS)
+ if (str_key.Contains(family->m_pFontName))
return 0;
return FXSYS_stricmp(reinterpret_cast<const char*>(key), family->m_pFontName);
}
@@ -187,9 +187,9 @@ CFX_ByteString TT_NormalizeName(const char* family) {
norm.Remove(' ');
norm.Remove('-');
norm.Remove(',');
- FX_STRSIZE pos = norm.Find('+');
- if (pos != 0 && pos != FX_STRNPOS)
- norm = norm.Left(pos);
+ auto pos = norm.Find('+');
+ if (pos.has_value() && pos.value() != 0)
+ norm = norm.Left(pos.value());
norm.MakeLower();
return norm;
}
@@ -208,14 +208,14 @@ uint8_t GetCharsetFromCodePage(uint16_t codepage) {
}
CFX_ByteString GetFontFamily(CFX_ByteString fontName, int nStyle) {
- if (fontName.Find("Script") >= 0) {
+ if (fontName.Contains("Script")) {
if ((nStyle & FX_FONT_STYLE_Bold) == FX_FONT_STYLE_Bold)
fontName = "ScriptMTBold";
- else if (fontName.Find("Palace") >= 0)
+ else if (fontName.Contains("Palace"))
fontName = "PalaceScriptMT";
- else if (fontName.Find("French") >= 0)
+ else if (fontName.Contains("French"))
fontName = "FrenchScriptMT";
- else if (fontName.Find("FreeStyle") >= 0)
+ else if (fontName.Contains("FreeStyle"))
fontName = "FreeStyleScript";
return fontName;
}
@@ -449,11 +449,11 @@ FXFT_Face CFX_FontMapper::FindSubstFont(const CFX_ByteString& name,
CFX_ByteString style;
bool bHasComma = false;
bool bHasHyphen = false;
- int find = SubstName.Find(",", 0);
- if (find >= 0) {
- family = SubstName.Left(find);
+ auto pos = SubstName.Find(",", 0);
+ if (pos.has_value()) {
+ family = SubstName.Left(pos.value());
PDF_GetStandardFontName(&family);
- style = SubstName.Right(SubstName.GetLength() - (find + 1));
+ style = SubstName.Right(SubstName.GetLength() - (pos.value() + 1));
bHasComma = true;
} else {
family = SubstName;
@@ -478,10 +478,10 @@ FXFT_Face CFX_FontMapper::FindSubstFont(const CFX_ByteString& name,
} else {
iBaseFont = kNumStandardFonts;
if (!bHasComma) {
- find = family.ReverseFind('-');
- if (find >= 0) {
- style = family.Right(family.GetLength() - (find + 1));
- family = family.Left(find);
+ pos = family.ReverseFind('-');
+ if (pos.has_value()) {
+ style = family.Right(family.GetLength() - (pos.value() + 1));
+ family = family.Left(pos.value());
bHasHyphen = true;
}
}
@@ -580,15 +580,18 @@ FXFT_Face CFX_FontMapper::FindSubstFont(const CFX_ByteString& name,
weight = old_weight;
}
#if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_
- if (SubstName.Find("Narrow") > 0 || SubstName.Find("Condensed") > 0)
- family = "LiberationSansNarrow";
+ const char* narrow_family = "LiberationSansNarrow";
#elif _FXM_PLATFORM_ == _FXM_PLATFORM_ANDROID_
- if (family.Find("Narrow") > 0 || family.Find("Condensed") > 0)
- family = "RobotoCondensed";
+ const char* narrow_family = "RobotoCondensed";
#else
- if (family.Find("Narrow") > 0 || family.Find("Condensed") > 0)
- family = "ArialNarrow";
+ const char* narrow_family = "ArialNarrow";
#endif // _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_
+ auto pos = SubstName.Find("Narrow");
+ if (pos.has_value() && pos.value() != 0)
+ family = narrow_family;
+ pos = SubstName.Find("Condensed");
+ if (pos.has_value() && pos.value() != 0)
+ family = narrow_family;
} else {
pSubstFont->m_bSubstCJK = true;
if (nStyle)
diff --git a/core/fxge/cfx_renderdevice.cpp b/core/fxge/cfx_renderdevice.cpp
index e088bc4f9a..d0c52e8ade 100644
--- a/core/fxge/cfx_renderdevice.cpp
+++ b/core/fxge/cfx_renderdevice.cpp
@@ -337,7 +337,7 @@ bool ShouldDrawDeviceText(const CFX_Font* pFont, uint32_t text_flags) {
return false;
const CFX_ByteString bsPsName = pFont->GetPsName();
- if (bsPsName.Find("+ZJHL") != FX_STRNPOS)
+ if (bsPsName.Contains("+ZJHL"))
return false;
if (bsPsName == "CNAAJI+cmex10")
diff --git a/core/fxge/fx_ge_linux.cpp b/core/fxge/fx_ge_linux.cpp
index 0552f1c58c..d17ddf2d3d 100644
--- a/core/fxge/fx_ge_linux.cpp
+++ b/core/fxge/fx_ge_linux.cpp
@@ -45,18 +45,16 @@ size_t GetJapanesePreference(const char* facearr,
int weight,
int pitch_family) {
CFX_ByteString face = facearr;
- if (face.Find("Gothic") != FX_STRNPOS ||
- face.Find("\x83\x53\x83\x56\x83\x62\x83\x4e") != FX_STRNPOS) {
- if (face.Find("PGothic") != FX_STRNPOS ||
- face.Find("\x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e") != FX_STRNPOS) {
+ if (face.Contains("Gothic") ||
+ face.Contains("\x83\x53\x83\x56\x83\x62\x83\x4e")) {
+ if (face.Contains("PGothic") ||
+ face.Contains("\x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e")) {
return 0;
}
return 1;
}
- if (face.Find("Mincho") != FX_STRNPOS ||
- face.Find("\x96\xbe\x92\xa9") != FX_STRNPOS) {
- if (face.Find("PMincho") != FX_STRNPOS ||
- face.Find("\x82\x6f\x96\xbe\x92\xa9") != FX_STRNPOS) {
+ if (face.Contains("Mincho") || face.Contains("\x96\xbe\x92\xa9")) {
+ if (face.Contains("PMincho") || face.Contains("\x82\x6f\x96\xbe\x92\xa9")) {
return 2;
}
return 3;
diff --git a/core/fxge/win32/fx_win32_device.cpp b/core/fxge/win32/fx_win32_device.cpp
index fd1944a1b4..9c8da591c6 100644
--- a/core/fxge/win32/fx_win32_device.cpp
+++ b/core/fxge/win32/fx_win32_device.cpp
@@ -503,7 +503,7 @@ void* CFX_Win32FallbackFontInfo::MapFont(int weight,
void CFX_Win32FontInfo::GetGBPreference(CFX_ByteString& face,
int weight,
int picth_family) {
- if (face.Find("KaiTi") != FX_STRNPOS || face.Find("\xbf\xac") != FX_STRNPOS) {
+ if (face.Contains("KaiTi") || face.Contains("\xbf\xac")) {
if (m_KaiTi.IsEmpty()) {
m_KaiTi = FindFont("KaiTi");
if (m_KaiTi.IsEmpty()) {
@@ -511,8 +511,7 @@ void CFX_Win32FontInfo::GetGBPreference(CFX_ByteString& face,
}
}
face = m_KaiTi;
- } else if (face.Find("FangSong") != FX_STRNPOS ||
- face.Find("\xb7\xc2\xcb\xce") != FX_STRNPOS) {
+ } else if (face.Contains("FangSong") || face.Contains("\xb7\xc2\xcb\xce")) {
if (m_FangSong.IsEmpty()) {
m_FangSong = FindFont("FangSong");
if (m_FangSong.IsEmpty()) {
@@ -520,11 +519,9 @@ void CFX_Win32FontInfo::GetGBPreference(CFX_ByteString& face,
}
}
face = m_FangSong;
- } else if (face.Find("SimSun") != FX_STRNPOS ||
- face.Find("\xcb\xce") != FX_STRNPOS) {
+ } else if (face.Contains("SimSun") || face.Contains("\xcb\xce")) {
face = "SimSun";
- } else if (face.Find("SimHei") != FX_STRNPOS ||
- face.Find("\xba\xda") != FX_STRNPOS) {
+ } else if (face.Contains("SimHei") || face.Contains("\xba\xda")) {
face = "SimHei";
} else if (!(picth_family & FF_ROMAN) && weight > 550) {
face = "SimHei";
@@ -536,16 +533,15 @@ void CFX_Win32FontInfo::GetGBPreference(CFX_ByteString& face,
void CFX_Win32FontInfo::GetJapanesePreference(CFX_ByteString& face,
int weight,
int picth_family) {
- if (face.Find("Gothic") != FX_STRNPOS ||
- face.Find("\x83\x53\x83\x56\x83\x62\x83\x4e") != FX_STRNPOS) {
- if (face.Find("PGothic") != FX_STRNPOS ||
- face.Find("\x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e") != FX_STRNPOS) {
+ if (face.Contains("Gothic") ||
+ face.Contains("\x83\x53\x83\x56\x83\x62\x83\x4e")) {
+ if (face.Contains("PGothic") ||
+ face.Contains("\x82\x6f\x83\x53\x83\x56\x83\x62\x83\x4e")) {
face = "MS PGothic";
- } else if (face.Find("UI Gothic") != FX_STRNPOS) {
+ } else if (face.Contains("UI Gothic")) {
face = "MS UI Gothic";
} else {
- if (face.Find("HGSGothicM") != FX_STRNPOS ||
- face.Find("HGMaruGothicMPRO") != FX_STRNPOS) {
+ if (face.Contains("HGSGothicM") || face.Contains("HGMaruGothicMPRO")) {
face = "MS PGothic";
} else {
face = "MS Gothic";
@@ -553,10 +549,8 @@ void CFX_Win32FontInfo::GetJapanesePreference(CFX_ByteString& face,
}
return;
}
- if (face.Find("Mincho") != FX_STRNPOS ||
- face.Find("\x96\xbe\x92\xa9") != FX_STRNPOS) {
- if (face.Find("PMincho") != FX_STRNPOS ||
- face.Find("\x82\x6f\x96\xbe\x92\xa9") != FX_STRNPOS) {
+ if (face.Contains("Mincho") || face.Contains("\x96\xbe\x92\xa9")) {
+ if (face.Contains("PMincho") || face.Contains("\x82\x6f\x96\xbe\x92\xa9")) {
face = "MS PMincho";
} else {
face = "MS Mincho";
@@ -640,7 +634,7 @@ void* CFX_Win32FontInfo::MapFont(int weight,
face = "Gulim";
break;
case FX_CHARSET_ChineseTraditional:
- if (face.Find("MSung") != FX_STRNPOS) {
+ if (face.Contains("MSung")) {
face = "MingLiU";
} else {
face = "PMingLiU";