From 12db7515f17228798d1aa38fce0fee3e7d2d36b6 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Wed, 23 Aug 2017 10:39:35 -0400 Subject: 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, 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 Reviewed-by: dsinclair --- .../edit/cpdf_pagecontentgenerator_unittest.cpp | 26 +++-- core/fpdfapi/page/cpdf_streamcontentparser.cpp | 8 +- core/fpdfapi/parser/fpdf_parser_utility.cpp | 6 +- core/fpdfdoc/cpdf_action.cpp | 4 +- core/fpdftext/cpdf_linkextract.cpp | 74 +++++++------ core/fpdftext/cpdf_textpagefind.cpp | 27 +++-- core/fxcrt/cfx_bytestring.cpp | 47 +++++---- core/fxcrt/cfx_bytestring.h | 16 ++- core/fxcrt/cfx_bytestring_unittest.cpp | 117 ++++++++++++++++++--- core/fxcrt/cfx_string_c_template.h | 10 +- core/fxcrt/cfx_widestring.cpp | 39 ++++--- core/fxcrt/cfx_widestring.h | 15 ++- core/fxcrt/cfx_widestring_unittest.cpp | 82 ++++++++++++--- core/fxcrt/fx_basic_util.cpp | 2 +- core/fxcrt/fx_system.h | 4 - core/fxcrt/xml/cfx_xmlelement.cpp | 14 ++- core/fxcrt/xml/cxml_parser.cpp | 8 +- core/fxge/android/cfpf_skiafontmgr.cpp | 4 +- core/fxge/apple/fx_mac_imp.cpp | 4 +- core/fxge/cfx_folderfontinfo.cpp | 9 +- core/fxge/cfx_font.cpp | 2 +- core/fxge/cfx_fontmapper.cpp | 47 +++++---- core/fxge/cfx_renderdevice.cpp | 2 +- core/fxge/fx_ge_linux.cpp | 14 ++- core/fxge/win32/fx_win32_device.cpp | 32 +++--- fpdfsdk/fpdfedittext.cpp | 2 +- fpdfsdk/fpdfppo.cpp | 22 ++-- fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp | 34 +++--- fpdfsdk/javascript/PublicMethods.cpp | 9 +- fxbarcode/oned/BC_OneDimWriter.cpp | 2 +- xfa/fgas/crt/cfgas_formatstring.cpp | 56 +++++----- xfa/fgas/font/cfgas_fontmgr.cpp | 2 +- xfa/fwl/cfwl_combolist.cpp | 4 +- xfa/fxfa/cxfa_ffdocview.cpp | 2 +- xfa/fxfa/cxfa_pdffontmgr.cpp | 25 +++-- xfa/fxfa/fm2js/cxfa_fm2jscontext.cpp | 39 ++++--- xfa/fxfa/parser/cxfa_dataexporter.cpp | 12 +-- xfa/fxfa/parser/cxfa_document.cpp | 33 +++--- xfa/fxfa/parser/cxfa_layoutpagemgr.cpp | 10 +- xfa/fxfa/parser/cxfa_localevalue.cpp | 18 ++-- xfa/fxfa/parser/cxfa_node.cpp | 13 +-- xfa/fxfa/parser/cxfa_simple_parser.cpp | 14 +-- xfa/fxfa/parser/cxfa_widgetdata.cpp | 43 ++++---- 43 files changed, 574 insertions(+), 379 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 "://" 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(m_csFindWhatArray); - int nResultPos = 0; - int nStartPos = 0; - nStartPos = m_findNextStart; + pdfium::Optional 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 CFX_ByteString::Find(char ch, + FX_STRSIZE nStart) const { if (!m_pData) - return FX_STRNPOS; + return pdfium::Optional(); if (nStart < 0 || nStart >= m_pData->m_nDataLength) - return FX_STRNPOS; + return pdfium::Optional(); const char* pStr = static_cast( 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( + static_cast(pStr - m_pData->m_String)) + : pdfium::Optional(); } -FX_STRSIZE CFX_ByteString::ReverseFind(char ch) const { +pdfium::Optional 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 nLength = m_pData->m_nDataLength; if (nStart > nLength) - return FX_STRNPOS; + return pdfium::Optional(); 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( + static_cast(pStr - m_pData->m_String)) + : pdfium::Optional(); +} + +pdfium::Optional CFX_ByteString::ReverseFind(char ch) const { + if (!m_pData) + return pdfium::Optional(); + + FX_STRSIZE nLength = m_pData->m_nDataLength; + while (nLength--) { + if (m_pData->m_String[nLength] == ch) + return pdfium::Optional(nLength); + } + return pdfium::Optional(); } 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 Find(const CFX_ByteStringC& lpszSub, + FX_STRSIZE start = 0) const; + pdfium::Optional Find(char ch, FX_STRSIZE start = 0) const; + pdfium::Optional 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 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 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 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 #include +#include #include #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(m_Ptr.Get()[index]); } - FX_STRSIZE Find(CharType ch) const { + pdfium::Optional Find(CharType ch) const { const UnsignedType* found = reinterpret_cast(FXSYS_chr( reinterpret_cast(m_Ptr.Get()), ch, m_Length)); - return found ? found - m_Ptr.Get() : FX_STRNPOS; + + return found ? pdfium::Optional(found - m_Ptr.Get()) + : pdfium::Optional(); } + 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 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(); 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(); 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(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 CFX_WideString::Find(wchar_t ch, + FX_STRSIZE nStart) const { if (!m_pData) - return FX_STRNPOS; + return pdfium::Optional(); if (nStart < 0 || nStart >= m_pData->m_nDataLength) - return FX_STRNPOS; + return pdfium::Optional(); 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( + static_cast(pStr - m_pData->m_String)) + : pdfium::Optional(); } -FX_STRSIZE CFX_WideString::Find(const CFX_WideStringC& pSub, - FX_STRSIZE nStart) const { +pdfium::Optional CFX_WideString::Find(const CFX_WideStringC& pSub, + FX_STRSIZE nStart) const { if (!m_pData) - return FX_STRNPOS; + return pdfium::Optional(); FX_STRSIZE nLength = m_pData->m_nDataLength; if (nStart > nLength) - return FX_STRNPOS; + return pdfium::Optional(); 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( + static_cast(pStr - m_pData->m_String)) + : pdfium::Optional(); } 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 Find(const CFX_WideStringC& pSub, + FX_STRSIZE start = 0) const; + pdfium::Optional 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 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 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(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(strlen(ptr)) #define FXSYS_wcslen(ptr) pdfium::base::checked_cast(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_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(element); - if (str_key.Find(family->m_pFontName) != FX_STRNPOS) + if (str_key.Contains(family->m_pFontName)) return 0; return FXSYS_stricmp(reinterpret_cast(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"; diff --git a/fpdfsdk/fpdfedittext.cpp b/fpdfsdk/fpdfedittext.cpp index 47facb4345..38f7cc2562 100644 --- a/fpdfsdk/fpdfedittext.cpp +++ b/fpdfsdk/fpdfedittext.cpp @@ -39,7 +39,7 @@ CPDF_Dictionary* LoadFontDesc(CPDF_Document* pDoc, int flags = 0; if (FXFT_Is_Face_fixedwidth(pFont->GetFace())) flags |= FXFONT_FIXED_PITCH; - if (font_name.Find("Serif") != FX_STRNPOS) + if (font_name.Contains("Serif")) flags |= FXFONT_SERIF; if (FXFT_Is_Face_Italic(pFont->GetFace())) flags |= FXFONT_ITALIC; diff --git a/fpdfsdk/fpdfppo.cpp b/fpdfsdk/fpdfppo.cpp index 905d53af48..861b15c259 100644 --- a/fpdfsdk/fpdfppo.cpp +++ b/fpdfsdk/fpdfppo.cpp @@ -80,35 +80,35 @@ bool ParserPageRangeString(CFX_ByteString rangstring, int nLength = rangstring.GetLength(); CFX_ByteString cbCompareString("0123456789-,"); for (int i = 0; i < nLength; ++i) { - if (cbCompareString.Find(rangstring[i]) == FX_STRNPOS) + if (!cbCompareString.Contains(rangstring[i])) return false; } CFX_ByteString cbMidRange; FX_STRSIZE nStringFrom = 0; - FX_STRSIZE nStringTo = 0; + pdfium::Optional nStringTo = 0; while (nStringTo < nLength) { nStringTo = rangstring.Find(',', nStringFrom); - if (nStringTo == FX_STRNPOS) + if (!nStringTo.has_value()) nStringTo = nLength; - cbMidRange = rangstring.Mid(nStringFrom, nStringTo - nStringFrom); - FX_STRSIZE nMid = cbMidRange.Find('-'); - if (nMid == FX_STRNPOS) { + cbMidRange = rangstring.Mid(nStringFrom, nStringTo.value() - nStringFrom); + auto nMid = cbMidRange.Find('-'); + if (!nMid.has_value()) { long lPageNum = atol(cbMidRange.c_str()); if (lPageNum <= 0 || lPageNum > nCount) return false; pageArray->push_back((uint16_t)lPageNum); } else { - int nStartPageNum = atol(cbMidRange.Left(nMid).c_str()); + int nStartPageNum = atol(cbMidRange.Left(nMid.value()).c_str()); if (nStartPageNum == 0) return false; - ++nMid; - int nEnd = cbMidRange.GetLength() - nMid; + nMid = nMid.value() + 1; + int nEnd = cbMidRange.GetLength() - nMid.value(); if (nEnd == 0) return false; - int nEndPageNum = atol(cbMidRange.Mid(nMid, nEnd).c_str()); + int nEndPageNum = atol(cbMidRange.Mid(nMid.value(), nEnd).c_str()); if (nStartPageNum < 0 || nStartPageNum > nEndPageNum || nEndPageNum > nCount) { return false; @@ -117,7 +117,7 @@ bool ParserPageRangeString(CFX_ByteString rangstring, pageArray->push_back(i); } } - nStringFrom = nStringTo + 1; + nStringFrom = nStringTo.value() + 1; } return true; } diff --git a/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp b/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp index c12314f88d..c0c28db192 100644 --- a/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp +++ b/fpdfsdk/fpdfxfa/cpdfxfa_docenvironment.cpp @@ -809,19 +809,19 @@ bool CPDFXFA_DocEnvironment::ExportSubmitFile(FPDF_FILEHANDLER* pFileHandler, void CPDFXFA_DocEnvironment::ToXFAContentFlags(CFX_WideString csSrcContent, FPDF_DWORD& flag) { - if (csSrcContent.Find(L" config ", 0) != FX_STRNPOS) + if (csSrcContent.Contains(L" config ")) flag |= FXFA_CONFIG; - if (csSrcContent.Find(L" template ", 0) != FX_STRNPOS) + if (csSrcContent.Contains(L" template ")) flag |= FXFA_TEMPLATE; - if (csSrcContent.Find(L" localeSet ", 0) != FX_STRNPOS) + if (csSrcContent.Contains(L" localeSet ")) flag |= FXFA_LOCALESET; - if (csSrcContent.Find(L" datasets ", 0) != FX_STRNPOS) + if (csSrcContent.Contains(L" datasets ")) flag |= FXFA_DATASETS; - if (csSrcContent.Find(L" xmpmeta ", 0) != FX_STRNPOS) + if (csSrcContent.Contains(L" xmpmeta ")) flag |= FXFA_XMPMETA; - if (csSrcContent.Find(L" xfdf ", 0) != FX_STRNPOS) + if (csSrcContent.Contains(L" xfdf ")) flag |= FXFA_XFDF; - if (csSrcContent.Find(L" form ", 0) != FX_STRNPOS) + if (csSrcContent.Contains(L" form ")) flag |= FXFA_FORM; if (flag == 0) { flag = FXFA_CONFIG | FXFA_TEMPLATE | FXFA_LOCALESET | FXFA_DATASETS | @@ -840,16 +840,16 @@ bool CPDFXFA_DocEnvironment::MailToInfo(CFX_WideString& csURL, if (srcURL.Left(7).CompareNoCase(L"mailto:") != 0) return false; - FX_STRSIZE pos = srcURL.Find(L'?', 0); + auto pos = srcURL.Find(L'?'); CFX_WideString tmp; - if (pos == FX_STRNPOS) { - pos = srcURL.Find(L'@', 0); - if (pos == FX_STRNPOS) + if (!pos.has_value()) { + pos = srcURL.Find(L'@'); + if (!pos.has_value()) return false; tmp = srcURL.Right(csURL.GetLength() - 7); } else { - tmp = srcURL.Left(pos); + tmp = srcURL.Left(pos.value()); tmp = tmp.Right(tmp.GetLength() - 7); } tmp.TrimLeft(); @@ -857,13 +857,13 @@ bool CPDFXFA_DocEnvironment::MailToInfo(CFX_WideString& csURL, csToAddress = tmp; - srcURL = srcURL.Right(srcURL.GetLength() - (pos + 1)); + srcURL = srcURL.Right(srcURL.GetLength() - (pos.value() + 1)); while (!srcURL.IsEmpty()) { srcURL.TrimLeft(); srcURL.TrimRight(); - pos = srcURL.Find(L'&', 0); + pos = srcURL.Find(L'&'); - tmp = (pos == FX_STRNPOS) ? srcURL : srcURL.Left(pos); + tmp = (!pos.has_value()) ? srcURL : srcURL.Left(pos.value()); tmp.TrimLeft(); tmp.TrimRight(); if (tmp.GetLength() >= 3 && tmp.Left(3).CompareNoCase(L"cc=") == 0) { @@ -886,7 +886,9 @@ bool CPDFXFA_DocEnvironment::MailToInfo(CFX_WideString& csURL, tmp = tmp.Right(tmp.GetLength() - 5); csMsg += tmp; } - srcURL = (pos == -1) ? L"" : srcURL.Right(csURL.GetLength() - (pos + 1)); + srcURL = !pos.has_value() + ? L"" + : srcURL.Right(csURL.GetLength() - (pos.value() + 1)); } csToAddress.Replace(L",", L";"); csCCAddress.Replace(L",", L";"); diff --git a/fpdfsdk/javascript/PublicMethods.cpp b/fpdfsdk/javascript/PublicMethods.cpp index 09815413ad..ff1d4fb7b7 100644 --- a/fpdfsdk/javascript/PublicMethods.cpp +++ b/fpdfsdk/javascript/PublicMethods.cpp @@ -527,7 +527,7 @@ double CJS_PublicMethods::MakeRegularDate(const CFX_WideString& value, CFX_WideString sFullMonths = fullmonths[m]; sFullMonths.MakeLower(); - if (sFullMonths.Find(sMonth.c_str(), 0) != FX_STRNPOS) { + if (sFullMonths.Contains(sMonth.c_str())) { nMonth = m + 1; i += 4; j += nSkip; @@ -933,8 +933,7 @@ bool CJS_PublicMethods::AFNumber_Keystroke(CJS_Runtime* pRuntime, pEvent->SelEnd() - pEvent->SelStart()); } - bool bHasSign = wstrValue.Find(L'-') != FX_STRNPOS && - wstrSelected.Find(L'-') == FX_STRNPOS; + bool bHasSign = wstrValue.Contains(L'-') && !wstrSelected.Contains(L'-'); if (bHasSign) { // can't insert "change" in front to sign postion. if (pEvent->SelStart() == 0) { @@ -948,7 +947,7 @@ bool CJS_PublicMethods::AFNumber_Keystroke(CJS_Runtime* pRuntime, iSepStyle = 0; const wchar_t cSep = iSepStyle < 2 ? L'.' : L','; - bool bHasSep = wstrValue.Find(cSep) != FX_STRNPOS; + bool bHasSep = wstrValue.Contains(cSep); for (FX_STRSIZE i = 0; i < wstrChange.GetLength(); ++i) { if (wstrChange[i] == cSep) { if (bHasSep) { @@ -1113,7 +1112,7 @@ bool CJS_PublicMethods::AFDate_FormatEx(CJS_Runtime* pRuntime, CFX_WideString sFormat = params[0].ToCFXWideString(pRuntime); double dDate = 0.0f; - if (strValue.Find(L"GMT") != FX_STRNPOS) { + if (strValue.Contains(L"GMT")) { // for GMT format time // such as "Tue Aug 11 14:24:16 GMT+08002009" dDate = MakeInterDate(strValue); diff --git a/fxbarcode/oned/BC_OneDimWriter.cpp b/fxbarcode/oned/BC_OneDimWriter.cpp index 29c360921f..69c031608c 100644 --- a/fxbarcode/oned/BC_OneDimWriter.cpp +++ b/fxbarcode/oned/BC_OneDimWriter.cpp @@ -279,7 +279,7 @@ bool CBC_OneDimWriter::RenderDeviceResult(CFX_RenderDevice* device, FXFILL_WINDING); } - return m_locTextLoc == BC_TEXT_LOC_NONE || contents.Find(' ') == FX_STRNPOS || + return m_locTextLoc == BC_TEXT_LOC_NONE || !contents.Contains(' ') || ShowChars(contents, device, matrix, m_barWidth, m_multiple); } diff --git a/xfa/fgas/crt/cfgas_formatstring.cpp b/xfa/fgas/crt/cfgas_formatstring.cpp index b20fa082c3..9da8bb9583 100644 --- a/xfa/fgas/crt/cfgas_formatstring.cpp +++ b/xfa/fgas/crt/cfgas_formatstring.cpp @@ -190,12 +190,9 @@ bool GetNumericDotIndex(const CFX_WideString& wsNum, } ccf++; } - *iDotIndex = wsNum.Find('.'); - if (*iDotIndex != FX_STRNPOS) - return true; - - *iDotIndex = iLenf; - return false; + auto result = wsNum.Find('.'); + *iDotIndex = result.value_or(iLenf); + return result.has_value(); } bool ExtractCountDigits(const wchar_t* str, @@ -249,7 +246,7 @@ bool ParseLocaleDate(const CFX_WideString& wsDate, *cc += iLiteralLen; ccf++; continue; - } else if (wsDateSymbols.Find(strf[ccf]) == FX_STRNPOS) { + } else if (!wsDateSymbols.Contains(strf[ccf])) { if (strf[ccf] != str[*cc]) return false; (*cc)++; @@ -370,7 +367,7 @@ bool ParseLocaleTime(const CFX_WideString& wsTime, ccf++; continue; } - if (wsTimeSymbols.Find(strf[ccf]) == FX_STRNPOS) { + if (!wsTimeSymbols.Contains(strf[ccf])) { if (strf[ccf] != str[*cc]) return false; (*cc)++; @@ -577,7 +574,7 @@ CFX_WideString DateFormat(const CFX_WideString& wsDatePattern, ccf++; continue; } - if (wsDateSymbols.Find(strf[ccf]) == FX_STRNPOS) { + if (!wsDateSymbols.Contains(strf[ccf])) { wsResult += strf[ccf++]; continue; } @@ -635,7 +632,7 @@ CFX_WideString TimeFormat(const CFX_WideString& wsTimePattern, int32_t lenf = wsTimePattern.GetLength(); uint16_t wHour = hour; bool bPM = false; - if (wsTimePattern.Find('A') != FX_STRNPOS) { + if (wsTimePattern.Contains('A')) { if (wHour >= 12) bPM = true; } @@ -647,7 +644,7 @@ CFX_WideString TimeFormat(const CFX_WideString& wsTimePattern, ccf++; continue; } - if (wsTimeSymbols.Find(strf[ccf]) == FX_STRNPOS) { + if (!wsTimeSymbols.Contains(strf[ccf])) { wsResult += strf[ccf++]; continue; } @@ -874,7 +871,7 @@ FX_LOCALECATEGORY CFGAS_FormatString::GetCategory( while (ccf < iLenf) { if (pStr[ccf] == '\'') { GetLiteralText(pStr, &ccf, iLenf); - } else if (!bBraceOpen && wsConstChars.Find(pStr[ccf]) == FX_STRNPOS) { + } else if (!bBraceOpen && !wsConstChars.Contains(pStr[ccf])) { CFX_WideString wsCategory(pStr[ccf]); ccf++; while (true) { @@ -932,7 +929,7 @@ CFX_WideString CFGAS_FormatString::GetTextFormat( int32_t iCurChar = ccf; GetLiteralText(pStr, &ccf, iLenf); wsPurgePattern += CFX_WideStringC(pStr + iCurChar, ccf - iCurChar + 1); - } else if (!bBrackOpen && wsConstChars.Find(pStr[ccf]) == FX_STRNPOS) { + } else if (!bBrackOpen && !wsConstChars.Contains(pStr[ccf])) { CFX_WideString wsSearchCategory(pStr[ccf]); ccf++; while (ccf < iLenf && pStr[ccf] != '{' && pStr[ccf] != '.' && @@ -984,7 +981,7 @@ IFX_Locale* CFGAS_FormatString::GetNumericFormat( int32_t iCurChar = ccf; GetLiteralText(pStr, &ccf, iLenf); *wsPurgePattern += CFX_WideStringC(pStr + iCurChar, ccf - iCurChar + 1); - } else if (!bBrackOpen && wsConstChars.Find(pStr[ccf]) == FX_STRNPOS) { + } else if (!bBrackOpen && !wsConstChars.Contains(pStr[ccf])) { CFX_WideString wsCategory(pStr[ccf]); ccf++; while (ccf < iLenf && pStr[ccf] != '{' && pStr[ccf] != '.' && @@ -1030,8 +1027,8 @@ IFX_Locale* CFGAS_FormatString::GetNumericFormat( ASSERT(pLocale); wsSubCategory = pLocale->GetNumPattern(eSubCategory); - *iDotIndex = wsSubCategory.Find('.'); - if (*iDotIndex != 0 && *iDotIndex != FX_STRNPOS) { + auto result = wsSubCategory.Find('.'); + if (result.has_value() && result.value() != 0) { *iDotIndex += wsPurgePattern->GetLength(); bFindDot = true; *dwStyle |= FX_NUMSTYLE_DotVorv; @@ -1177,9 +1174,8 @@ bool CFGAS_FormatString::ParseNum(const CFX_WideString& wsSrcNum, // If we're looking for a '.', 'V' or 'v' and the input string does not // have a dot index for one of those, then we disable parsing the decimal. if (!GetNumericDotIndex(wsSrcNum, wsDotSymbol, &dot_index) && - (dwFormatStyle & FX_NUMSTYLE_DotVorv)) { + (dwFormatStyle & FX_NUMSTYLE_DotVorv)) bReverseParse = true; - } // This parse is broken into two parts based on the '.' in the number // (or 'V' or 'v'). |dot_index_f| is the location of the dot in the format and @@ -1570,7 +1566,7 @@ FX_DATETIMETYPE CFGAS_FormatString::GetDateTimeFormat( GetLiteralText(pStr, &ccf, iLenf); wsTempPattern += CFX_WideStringC(pStr + iCurChar, ccf - iCurChar + 1); } else if (!bBraceOpen && iFindCategory != 3 && - wsConstChars.Find(pStr[ccf]) == FX_STRNPOS) { + !wsConstChars.Contains(pStr[ccf])) { CFX_WideString wsCategory(pStr[ccf]); ccf++; while (ccf < iLenf && pStr[ccf] != '{' && pStr[ccf] != '.' && @@ -1950,12 +1946,12 @@ bool CFGAS_FormatString::FormatStrNum(const CFX_WideStringC& wsInputNum, bool bAddNeg = false; const wchar_t* str = wsSrcNum.c_str(); int len = wsSrcNum.GetLength(); - int dot_index = wsSrcNum.Find('.'); - if (dot_index == FX_STRNPOS) + auto dot_index = wsSrcNum.Find('.'); + if (!dot_index.has_value()) dot_index = len; ccf = dot_index_f - 1; - cc = dot_index - 1; + cc = dot_index.value() - 1; while (ccf >= 0) { switch (strf[ccf]) { case '9': @@ -2089,7 +2085,7 @@ bool CFGAS_FormatString::FormatStrNum(const CFX_WideStringC& wsInputNum, } if (cc >= 0) { - int nPos = dot_index % 3; + int nPos = dot_index.value() % 3; wsOutput->clear(); for (int32_t i = 0; i < dot_index; i++) { if (i % 3 == nPos && i != 0) @@ -2098,7 +2094,7 @@ bool CFGAS_FormatString::FormatStrNum(const CFX_WideStringC& wsInputNum, } if (dot_index < len) { *wsOutput += pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Decimal); - *wsOutput += wsSrcNum.Right(len - dot_index - 1); + *wsOutput += wsSrcNum.Right(len - dot_index.value() - 1); } if (bNeg) { *wsOutput = @@ -2126,7 +2122,7 @@ bool CFGAS_FormatString::FormatStrNum(const CFX_WideStringC& wsInputNum, } ccf = dot_index_f + 1; - cc = dot_index + 1; + cc = dot_index.value() + 1; while (ccf < lenf) { switch (strf[ccf]) { case '\'': @@ -2281,8 +2277,8 @@ bool CFGAS_FormatString::FormatDateTime(const CFX_WideString& wsSrcDateTime, return false; CFX_DateTime dt; - int32_t iT = wsSrcDateTime.Find(L"T"); - if (iT == FX_STRNPOS) { + auto iT = wsSrcDateTime.Find(L"T"); + if (!iT.has_value()) { if (eCategory == FX_DATETIMETYPE_Date && FX_DateFromCanonical(wsSrcDateTime, &dt)) { *wsOutput = FormatDateTimeInternal(dt, wsDatePattern, wsTimePattern, true, @@ -2296,9 +2292,9 @@ bool CFGAS_FormatString::FormatDateTime(const CFX_WideString& wsSrcDateTime, return true; } } else { - CFX_WideString wsSrcDate(wsSrcDateTime.c_str(), iT); - CFX_WideStringC wsSrcTime(wsSrcDateTime.c_str() + iT + 1, - wsSrcDateTime.GetLength() - iT - 1); + CFX_WideString wsSrcDate(wsSrcDateTime.c_str(), iT.value()); + CFX_WideStringC wsSrcTime(wsSrcDateTime.c_str() + iT.value() + 1, + wsSrcDateTime.GetLength() - iT.value() - 1); if (wsSrcDate.IsEmpty() || wsSrcTime.IsEmpty()) return false; if (FX_DateFromCanonical(wsSrcDate, &dt) && diff --git a/xfa/fgas/font/cfgas_fontmgr.cpp b/xfa/fgas/font/cfgas_fontmgr.cpp index 813a91dc63..36cf10581a 100644 --- a/xfa/fgas/font/cfgas_fontmgr.cpp +++ b/xfa/fgas/font/cfgas_fontmgr.cpp @@ -1267,7 +1267,7 @@ void CFGAS_FontMgr::GetUSBCSB(FXFT_Face pFace, uint32_t* USB, uint32_t* CSB) { int32_t CFGAS_FontMgr::IsPartName(const CFX_WideString& Name1, const CFX_WideString& Name2) { - if (Name1.Find(Name2.c_str()) != FX_STRNPOS) + if (Name1.Contains(Name2.c_str())) return 1; return 0; } diff --git a/xfa/fwl/cfwl_combolist.cpp b/xfa/fwl/cfwl_combolist.cpp index 5b700a90de..f334f027e7 100644 --- a/xfa/fwl/cfwl_combolist.cpp +++ b/xfa/fwl/cfwl_combolist.cpp @@ -33,8 +33,8 @@ int32_t CFWL_ComboList::MatchItem(const CFX_WideString& wsMatch) { for (int32_t i = 0; i < iCount; i++) { CFWL_ListItem* hItem = GetItem(this, i); CFX_WideString wsText = hItem ? hItem->GetText() : L""; - FX_STRSIZE pos = wsText.Find(wsMatch.c_str()); - if (!pos) + auto pos = wsText.Find(wsMatch.c_str()); + if (pos.has_value() && pos.value() == 0) return i; } return -1; diff --git a/xfa/fxfa/cxfa_ffdocview.cpp b/xfa/fxfa/cxfa_ffdocview.cpp index ff20188a92..95faeb3606 100644 --- a/xfa/fxfa/cxfa_ffdocview.cpp +++ b/xfa/fxfa/cxfa_ffdocview.cpp @@ -279,7 +279,7 @@ int32_t CXFA_FFDocView::ProcessWidgetEvent(CXFA_EventParam* pParam, wsValidateStr = pValidateNode->GetContent(); } - if (wsValidateStr.Find(L"preSubmit") == FX_STRNPOS) + if (!wsValidateStr.Contains(L"preSubmit")) return XFA_EVENTERROR_Success; } diff --git a/xfa/fxfa/cxfa_pdffontmgr.cpp b/xfa/fxfa/cxfa_pdffontmgr.cpp index af94ee8c82..5872078fdf 100644 --- a/xfa/fxfa/cxfa_pdffontmgr.cpp +++ b/xfa/fxfa/cxfa_pdffontmgr.cpp @@ -121,30 +121,29 @@ bool CXFA_PDFFontMgr::PsNameMatchDRFontName(const CFX_ByteStringC& bsPsName, CFX_ByteString bsDRName = bsDRFontName; bsDRName.Remove('-'); FX_STRSIZE iPsLen = bsPsName.GetLength(); - FX_STRSIZE nIndex = bsDRName.Find(bsPsName); - if (nIndex != FX_STRNPOS && !bStrictMatch) + auto nIndex = bsDRName.Find(bsPsName); + if (nIndex.has_value() && !bStrictMatch) return true; - if (nIndex != 0) + if (!nIndex.has_value() || nIndex.value() != 0) return false; int32_t iDifferLength = bsDRName.GetLength() - iPsLen; if (iDifferLength > 1 || (bBold || bItalic)) { - FX_STRSIZE iBoldIndex = bsDRName.Find("Bold"); - bool bBoldFont = iBoldIndex != FX_STRNPOS; - if (bBold != bBoldFont) + auto iBoldIndex = bsDRName.Find("Bold"); + if (bBold != iBoldIndex.has_value()) return false; - if (bBoldFont) { - iDifferLength = - std::min(iDifferLength - 4, bsDRName.GetLength() - iBoldIndex - 4); + if (iBoldIndex.has_value()) { + iDifferLength = std::min(iDifferLength - 4, + bsDRName.GetLength() - iBoldIndex.value() - 4); } bool bItalicFont = true; - if (bsDRName.Find("Italic") != FX_STRNPOS) { + if (bsDRName.Contains("Italic")) { iDifferLength -= 6; - } else if (bsDRName.Find("It") != FX_STRNPOS) { + } else if (bsDRName.Contains("It")) { iDifferLength -= 2; - } else if (bsDRName.Find("Oblique") != FX_STRNPOS) { + } else if (bsDRName.Contains("Oblique")) { iDifferLength -= 7; } else { bItalicFont = false; @@ -158,7 +157,7 @@ bool CXFA_PDFFontMgr::PsNameMatchDRFontName(const CFX_ByteStringC& bsPsName, bsDRTailer == "Regular" || bsDRTailer == "Reg") { return true; } - if (bBoldFont || bItalicFont) + if (iBoldIndex.has_value() || bItalicFont) return false; bool bMatch = false; diff --git a/xfa/fxfa/fm2js/cxfa_fm2jscontext.cpp b/xfa/fxfa/fm2js/cxfa_fm2jscontext.cpp index 5912d26a99..a028538198 100644 --- a/xfa/fxfa/fm2js/cxfa_fm2jscontext.cpp +++ b/xfa/fxfa/fm2js/cxfa_fm2jscontext.cpp @@ -374,8 +374,9 @@ bool PatternStringType(const CFX_ByteStringC& szPattern, return true; } if (L"date" == wsPattern.Left(4)) { - FX_STRSIZE ret = wsPattern.Find(L"time"); - patternType = ret != 0 && ret != FX_STRNPOS ? XFA_VT_DATETIME : XFA_VT_DATE; + auto pos = wsPattern.Find(L"time"); + patternType = + pos.has_value() && pos.value() != 0 ? XFA_VT_DATETIME : XFA_VT_DATE; return true; } if (L"time" == wsPattern.Left(4)) { @@ -1139,12 +1140,12 @@ void CXFA_FM2JSContext::IsoTime2Num(CFXJSE_Value* pThis, CXFA_Document* pDoc = pContext->GetDocument(); CXFA_LocaleMgr* pMgr = pDoc->GetLocalMgr(); CFX_ByteString szArgString = ValueToUTF8String(argOne.get()); - FX_STRSIZE pos = szArgString.Find('T', 0); - szArgString = szArgString.Right(szArgString.GetLength() - (pos + 1)); - if (szArgString.IsEmpty()) { + auto pos = szArgString.Find('T', 0); + if (!pos.has_value() || pos.value() == szArgString.GetLength() - 1) { args.GetReturnValue()->SetInteger(0); return; } + szArgString = szArgString.Right(szArgString.GetLength() - (pos.value() + 1)); CXFA_LocaleValue timeValue( XFA_VT_TIME, CFX_WideString::FromUTF8(szArgString.AsStringC()), pMgr); @@ -3077,8 +3078,8 @@ void CXFA_FM2JSContext::At(CFXJSE_Value* pThis, } CFX_ByteString stringOne = ValueToUTF8String(argOne.get()); - FX_STRSIZE iPosition = stringOne.Find(stringTwo.AsStringC()); - args.GetReturnValue()->SetInteger(iPosition + 1); + auto pos = stringOne.Find(stringTwo.AsStringC()); + args.GetReturnValue()->SetInteger(pos.has_value() ? pos.value() + 1 : 0); } // static @@ -3689,13 +3690,18 @@ void CXFA_FM2JSContext::Format(CFXJSE_Value* pThis, if (!PatternStringType(szPattern.AsStringC(), patternType)) { switch (patternType) { case XFA_VT_DATETIME: { - FX_STRSIZE iTChar = wsPattern.Find(L'T'); + auto iTChar = wsPattern.Find(L'T'); + if (!iTChar.has_value()) { + args.GetReturnValue()->SetString(""); + return; + } CFX_WideString wsDatePattern(L"date{"); - wsDatePattern += wsPattern.Left(iTChar) + L"} "; + wsDatePattern += wsPattern.Left(iTChar.value()) + L"} "; CFX_WideString wsTimePattern(L"time{"); wsTimePattern += - wsPattern.Right(wsPattern.GetLength() - (iTChar + 1)) + L"}"; + wsPattern.Right(wsPattern.GetLength() - (iTChar.value() + 1)) + + L"}"; wsPattern = wsDatePattern + wsTimePattern; } break; case XFA_VT_DATE: { @@ -3878,11 +3884,16 @@ void CXFA_FM2JSContext::Parse(CFXJSE_Value* pThis, switch (patternType) { case XFA_VT_DATETIME: { - FX_STRSIZE iTChar = wsPattern.Find(L'T'); - CFX_WideString wsDatePattern(L"date{" + wsPattern.Left(iTChar) + L"} "); + auto iTChar = wsPattern.Find(L'T'); + if (!iTChar.has_value()) { + args.GetReturnValue()->SetString(""); + return; + } + CFX_WideString wsDatePattern(L"date{" + wsPattern.Left(iTChar.value()) + + L"} "); CFX_WideString wsTimePattern( - L"time{" + wsPattern.Right(wsPattern.GetLength() - (iTChar + 1)) + - L"}"); + L"time{" + + wsPattern.Right(wsPattern.GetLength() - (iTChar.value() + 1)) + L"}"); wsPattern = wsDatePattern + wsTimePattern; CXFA_LocaleValue localeValue(patternType, wsValue, wsPattern, pLocale, pMgr); diff --git a/xfa/fxfa/parser/cxfa_dataexporter.cpp b/xfa/fxfa/parser/cxfa_dataexporter.cpp index b502bbdfa7..05586e12dd 100644 --- a/xfa/fxfa/parser/cxfa_dataexporter.cpp +++ b/xfa/fxfa/parser/cxfa_dataexporter.cpp @@ -226,14 +226,14 @@ void RegenerateFormFile_Changed(CXFA_Node* pNode, std::vector wsSelTextArray; FX_STRSIZE iStart = 0; - FX_STRSIZE iEnd = wsRawValue.Find(L'\n', iStart); - iEnd = (iEnd == FX_STRNPOS) ? wsRawValue.GetLength() : iEnd; - while (iEnd != FX_STRNPOS && iEnd >= iStart) { - wsSelTextArray.push_back(wsRawValue.Mid(iStart, iEnd - iStart)); - iStart = iEnd + 1; + auto iEnd = wsRawValue.Find(L'\n', iStart); + iEnd = !iEnd.has_value() ? wsRawValue.GetLength() : iEnd; + while (iEnd.has_value() && iEnd >= iStart) { + wsSelTextArray.push_back( + wsRawValue.Mid(iStart, iEnd.value() - iStart)); + iStart = iEnd.value() + 1; if (iStart >= wsRawValue.GetLength()) break; - iEnd = wsRawValue.Find(L'\n', iStart); } CXFA_Node* pParentNode = pNode->GetNodeItem(XFA_NODEITEM_Parent); diff --git a/xfa/fxfa/parser/cxfa_document.cpp b/xfa/fxfa/parser/cxfa_document.cpp index f43deaf145..85d2758467 100644 --- a/xfa/fxfa/parser/cxfa_document.cpp +++ b/xfa/fxfa/parser/cxfa_document.cpp @@ -305,15 +305,17 @@ XFA_VERSION CXFA_Document::RecognizeXFAVersionNumber( wsTemplateURIPrefix) { return XFA_VERSION_UNKNOWN; } - FX_STRSIZE nDotPos = wsTemplateNS.Find('.', nPrefixLength); - if (nDotPos == FX_STRNPOS) + auto nDotPos = wsTemplateNS.Find('.', nPrefixLength); + if (!nDotPos.has_value()) return XFA_VERSION_UNKNOWN; int8_t iMajor = FXSYS_wtoi( - wsTemplateNS.Mid(nPrefixLength, nDotPos - nPrefixLength).c_str()); - int8_t iMinor = FXSYS_wtoi( - wsTemplateNS.Mid(nDotPos + 1, wsTemplateNS.GetLength() - nDotPos - 2) - .c_str()); + wsTemplateNS.Mid(nPrefixLength, nDotPos.value() - nPrefixLength).c_str()); + int8_t iMinor = + FXSYS_wtoi(wsTemplateNS + .Mid(nDotPos.value() + 1, + wsTemplateNS.GetLength() - nDotPos.value() - 2) + .c_str()); XFA_VERSION eVersion = (XFA_VERSION)((int32_t)iMajor * 100 + iMinor); if (eVersion < XFA_VERSION_MIN || eVersion > XFA_VERSION_MAX) return XFA_VERSION_UNKNOWN; @@ -367,20 +369,21 @@ void CXFA_Document::DoProtoMerge() { CFX_WideStringC wsURI, wsID, wsSOM; if (pUseHrefNode->TryCData(XFA_ATTRIBUTE_Usehref, wsUseVal) && !wsUseVal.IsEmpty()) { - FX_STRSIZE uSharpPos = wsUseVal.Find('#'); - if (uSharpPos == FX_STRNPOS) { + auto uSharpPos = wsUseVal.Find('#'); + if (!uSharpPos.has_value()) { wsURI = wsUseVal.AsStringC(); } else { - wsURI = CFX_WideStringC(wsUseVal.c_str(), uSharpPos); + wsURI = CFX_WideStringC(wsUseVal.c_str(), uSharpPos.value()); FX_STRSIZE uLen = wsUseVal.GetLength(); - if (uLen >= uSharpPos + 5 && - CFX_WideStringC(wsUseVal.c_str() + uSharpPos, 5) == L"#som(" && + if (uLen >= uSharpPos.value() + 5 && + CFX_WideStringC(wsUseVal.c_str() + uSharpPos.value(), 5) == + L"#som(" && wsUseVal[uLen - 1] == ')') { - wsSOM = CFX_WideStringC(wsUseVal.c_str() + uSharpPos + 5, - uLen - 1 - uSharpPos - 5); + wsSOM = CFX_WideStringC(wsUseVal.c_str() + uSharpPos.value() + 5, + uLen - 1 - uSharpPos.value() - 5); } else { - wsID = CFX_WideStringC(wsUseVal.c_str() + uSharpPos + 1, - uLen - uSharpPos - 1); + wsID = CFX_WideStringC(wsUseVal.c_str() + uSharpPos.value() + 1, + uLen - uSharpPos.value() - 1); } } } else if (pUseHrefNode->TryCData(XFA_ATTRIBUTE_Use, wsUseVal) && diff --git a/xfa/fxfa/parser/cxfa_layoutpagemgr.cpp b/xfa/fxfa/parser/cxfa_layoutpagemgr.cpp index e643bc502f..b02efbb45d 100644 --- a/xfa/fxfa/parser/cxfa_layoutpagemgr.cpp +++ b/xfa/fxfa/parser/cxfa_layoutpagemgr.cpp @@ -148,11 +148,13 @@ CXFA_Node* ResolveBreakTarget(CXFA_Node* pPageSetRoot, bool bTargetAllFind = true; while (iSplitIndex != -1) { CFX_WideString wsExpr; - FX_STRSIZE iSplitNextIndex = 0; + pdfium::Optional iSplitNextIndex = 0; if (!bTargetAllFind) { iSplitNextIndex = wsTargetAll.Find(' ', iSplitIndex); - ASSERT(iSplitNextIndex != FX_STRNPOS); - wsExpr = wsTargetAll.Mid(iSplitIndex, iSplitNextIndex - iSplitIndex); + if (!iSplitNextIndex.has_value()) + return nullptr; + wsExpr = + wsTargetAll.Mid(iSplitIndex, iSplitNextIndex.value() - iSplitIndex); } else { wsExpr = wsTargetAll; } @@ -180,7 +182,7 @@ CXFA_Node* ResolveBreakTarget(CXFA_Node* pPageSetRoot, if (iCount > 0 && rs.objects.front()->IsNode()) return rs.objects.front()->AsNode(); } - iSplitIndex = iSplitNextIndex; + iSplitIndex = iSplitNextIndex.value(); } return nullptr; } diff --git a/xfa/fxfa/parser/cxfa_localevalue.cpp b/xfa/fxfa/parser/cxfa_localevalue.cpp index aa0f74b170..dd56e6d7a1 100644 --- a/xfa/fxfa/parser/cxfa_localevalue.cpp +++ b/xfa/fxfa/parser/cxfa_localevalue.cpp @@ -49,14 +49,14 @@ bool ValueSplitDateTime(const CFX_WideString& wsDateTime, if (wsDateTime.IsEmpty()) return false; - FX_STRSIZE nSplitIndex = wsDateTime.Find('T'); - if (nSplitIndex == FX_STRNPOS) + auto nSplitIndex = wsDateTime.Find('T'); + if (!nSplitIndex.has_value()) nSplitIndex = wsDateTime.Find(' '); - if (nSplitIndex == FX_STRNPOS) + if (!nSplitIndex.has_value()) return false; - wsDate = wsDateTime.Left(nSplitIndex); - wsTime = wsDateTime.Right(wsDateTime.GetLength() - nSplitIndex - 1); + wsDate = wsDateTime.Left(nSplitIndex.value()); + wsTime = wsDateTime.Right(wsDateTime.GetLength() - nSplitIndex.value() - 1); return true; } @@ -444,7 +444,7 @@ bool CXFA_LocaleValue::ValidateCanonicalDate(const CFX_WideString& wsDate, if (nLen < wCountY || nLen > wCountY + wCountM + wCountD + 2) return false; - const bool bSymbol = wsDate.Find(0x2D) != FX_STRNPOS; + const bool bSymbol = wsDate.Contains(0x2D); uint16_t wYear = 0; uint16_t wMonth = 0; uint16_t wDay = 0; @@ -519,7 +519,7 @@ bool CXFA_LocaleValue::ValidateCanonicalTime(const CFX_WideString& wsTime) { const uint16_t wCountM = 2; const uint16_t wCountS = 2; const uint16_t wCountF = 3; - const bool bSymbol = wsTime.Find(':') != FX_STRNPOS; + const bool bSymbol = wsTime.Contains(':'); uint16_t wHour = 0; uint16_t wMinute = 0; uint16_t wSecond = 0; @@ -558,8 +558,8 @@ bool CXFA_LocaleValue::ValidateCanonicalTime(const CFX_WideString& wsTime) { wSecond = pTime[nIndex] - '0' + wSecond * 10; nIndex++; } - FX_STRSIZE ret = wsTime.Find('.'); - if (ret && ret != FX_STRNPOS) { + auto pos = wsTime.Find('.'); + if (pos.has_value() && pos.value() != 0) { if (pTime[nIndex] != '.') return false; nIndex++; diff --git a/xfa/fxfa/parser/cxfa_node.cpp b/xfa/fxfa/parser/cxfa_node.cpp index 2b4bdd22c0..eea7fb2489 100644 --- a/xfa/fxfa/parser/cxfa_node.cpp +++ b/xfa/fxfa/parser/cxfa_node.cpp @@ -4045,16 +4045,17 @@ bool CXFA_Node::SetScriptContent(const CFX_WideString& wsContent, if (!wsContent.IsEmpty()) { FX_STRSIZE iStart = 0; FX_STRSIZE iLength = wsContent.GetLength(); - FX_STRSIZE iEnd = wsContent.Find(L'\n', iStart); - iEnd = (iEnd == FX_STRNPOS) ? iLength : iEnd; - while (iEnd >= iStart) { - wsSaveTextArray.push_back(wsContent.Mid(iStart, iEnd - iStart)); - iStart = iEnd + 1; + auto iEnd = wsContent.Find(L'\n', iStart); + iEnd = !iEnd.has_value() ? iLength : iEnd; + while (iEnd.value() >= iStart) { + wsSaveTextArray.push_back( + wsContent.Mid(iStart, iEnd.value() - iStart)); + iStart = iEnd.value() + 1; if (iStart >= iLength) { break; } iEnd = wsContent.Find(L'\n', iStart); - if (iEnd == FX_STRNPOS) { + if (!iEnd.has_value()) { wsSaveTextArray.push_back( wsContent.Mid(iStart, iLength - iStart)); } diff --git a/xfa/fxfa/parser/cxfa_simple_parser.cpp b/xfa/fxfa/parser/cxfa_simple_parser.cpp index 39dc858b9e..ee2a2444fc 100644 --- a/xfa/fxfa/parser/cxfa_simple_parser.cpp +++ b/xfa/fxfa/parser/cxfa_simple_parser.cpp @@ -92,12 +92,12 @@ bool MatchNodeName(CFX_XMLNode* pNode, bool GetAttributeLocalName(const CFX_WideStringC& wsAttributeName, CFX_WideString& wsLocalAttrName) { CFX_WideString wsAttrName(wsAttributeName); - FX_STRSIZE iFind = wsAttrName.Find(L':', 0); - if (iFind == FX_STRNPOS) { + auto pos = wsAttrName.Find(L':', 0); + if (!pos.has_value()) { wsLocalAttrName = wsAttrName; return false; } - wsLocalAttrName = wsAttrName.Right(wsAttrName.GetLength() - iFind - 1); + wsLocalAttrName = wsAttrName.Right(wsAttrName.GetLength() - pos.value() - 1); return true; } @@ -133,17 +133,17 @@ bool FindAttributeWithNS(CFX_XMLElement* pElement, CFX_WideString wsAttrNS; for (auto it : pElement->GetAttributes()) { - FX_STRSIZE iFind = it.first.Find(L':', 0); + auto pos = it.first.Find(L':', 0); CFX_WideString wsNSPrefix; - if (iFind == FX_STRNPOS) { + if (!pos.has_value()) { if (wsLocalAttributeName != it.first) continue; } else { if (wsLocalAttributeName != - it.first.Right(it.first.GetLength() - iFind - 1)) { + it.first.Right(it.first.GetLength() - pos.value() - 1)) { continue; } - wsNSPrefix = it.first.Left(iFind); + wsNSPrefix = it.first.Left(pos.value()); } if (!XFA_FDEExtension_ResolveNamespaceQualifier( diff --git a/xfa/fxfa/parser/cxfa_widgetdata.cpp b/xfa/fxfa/parser/cxfa_widgetdata.cpp index 5b9f62c93f..2da8820b67 100644 --- a/xfa/fxfa/parser/cxfa_widgetdata.cpp +++ b/xfa/fxfa/parser/cxfa_widgetdata.cpp @@ -42,18 +42,18 @@ bool SplitDateTime(const CFX_WideString& wsDateTime, if (wsDateTime.IsEmpty()) return false; - FX_STRSIZE nSplitIndex = wsDateTime.Find('T'); - if (nSplitIndex == FX_STRNPOS) + auto nSplitIndex = wsDateTime.Find('T'); + if (!nSplitIndex.has_value()) nSplitIndex = wsDateTime.Find(' '); - if (nSplitIndex == FX_STRNPOS) + if (!nSplitIndex.has_value()) return false; - wsDate = wsDateTime.Left(nSplitIndex); + wsDate = wsDateTime.Left(nSplitIndex.value()); if (!wsDate.IsEmpty()) { if (!std::any_of(wsDate.begin(), wsDate.end(), std::iswdigit)) return false; } - wsTime = wsDateTime.Right(wsDateTime.GetLength() - nSplitIndex - 1); + wsTime = wsDateTime.Right(wsDateTime.GetLength() - nSplitIndex.value() - 1); if (!wsTime.IsEmpty()) { if (!std::any_of(wsTime.begin(), wsTime.end(), std::iswdigit)) return false; @@ -849,15 +849,15 @@ std::vector CXFA_WidgetData::GetSelectedItemsValue() { if (!wsValue.IsEmpty()) { FX_STRSIZE iStart = 0; FX_STRSIZE iLength = wsValue.GetLength(); - FX_STRSIZE iEnd = wsValue.Find(L'\n', iStart); - iEnd = (iEnd == FX_STRNPOS) ? iLength : iEnd; + auto iEnd = wsValue.Find(L'\n', iStart); + iEnd = (!iEnd.has_value()) ? iLength : iEnd; while (iEnd >= iStart) { - wsSelTextArray.push_back(wsValue.Mid(iStart, iEnd - iStart)); - iStart = iEnd + 1; + wsSelTextArray.push_back(wsValue.Mid(iStart, iEnd.value() - iStart)); + iStart = iEnd.value() + 1; if (iStart >= iLength) break; iEnd = wsValue.Find(L'\n', iStart); - if (iEnd == FX_STRNPOS) + if (!iEnd.has_value()) wsSelTextArray.push_back(wsValue.Mid(iStart, iLength - iStart)); } } @@ -1315,15 +1315,16 @@ bool CXFA_WidgetData::GetBarcodeAttribute_WideNarrowRatio(float* val) { CXFA_Node* pUIChild = GetUIChild(); CFX_WideString wsWideNarrowRatio; if (pUIChild->TryCData(XFA_ATTRIBUTE_WideNarrowRatio, wsWideNarrowRatio)) { - FX_STRSIZE ptPos = wsWideNarrowRatio.Find(':'); + auto ptPos = wsWideNarrowRatio.Find(':'); float fRatio = 0; - if (ptPos != FX_STRNPOS) { + if (!ptPos.has_value()) { fRatio = (float)FXSYS_wtoi(wsWideNarrowRatio.c_str()); } else { int32_t fA, fB; - fA = FXSYS_wtoi(wsWideNarrowRatio.Left(ptPos).c_str()); + fA = FXSYS_wtoi(wsWideNarrowRatio.Left(ptPos.value()).c_str()); fB = FXSYS_wtoi( - wsWideNarrowRatio.Right(wsWideNarrowRatio.GetLength() - (ptPos + 1)) + wsWideNarrowRatio + .Right(wsWideNarrowRatio.GetLength() - (ptPos.value() + 1)) .c_str()); if (fB) fRatio = (float)fA / fB; @@ -1742,9 +1743,8 @@ void CXFA_WidgetData::NormalizeNumStr(const CFX_WideString& wsValue, wsOutput = wsValue; wsOutput.TrimLeft('0'); - FX_STRSIZE dot_index = wsOutput.Find('.'); int32_t iFracDigits = 0; - if (!wsOutput.IsEmpty() && dot_index != FX_STRNPOS && + if (!wsOutput.IsEmpty() && wsOutput.Contains('.') && (!GetFracDigits(iFracDigits) || iFracDigits != -1)) { wsOutput.TrimRight(L"0"); wsOutput.TrimRight(L"."); @@ -1768,13 +1768,12 @@ void CXFA_WidgetData::FormatNumStr(const CFX_WideString& wsValue, wsSrcNum.Delete(0, 1); } int32_t len = wsSrcNum.GetLength(); - FX_STRSIZE dot_index = wsSrcNum.Find('.'); - if (dot_index == FX_STRNPOS) - dot_index = len; + auto dot_index = wsSrcNum.Find('.'); + dot_index = !dot_index.has_value() ? len : dot_index; - int32_t cc = dot_index - 1; + int32_t cc = dot_index.value() - 1; if (cc >= 0) { - int nPos = dot_index % 3; + int nPos = dot_index.value() % 3; wsOutput.clear(); for (int32_t i = 0; i < dot_index; i++) { if (i % 3 == nPos && i != 0) @@ -1784,7 +1783,7 @@ void CXFA_WidgetData::FormatNumStr(const CFX_WideString& wsValue, } if (dot_index < len) { wsOutput += pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Decimal); - wsOutput += wsSrcNum.Right(len - dot_index - 1); + wsOutput += wsSrcNum.Right(len - dot_index.value() - 1); } if (bNeg) { wsOutput = -- cgit v1.2.3