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 --- xfa/fgas/crt/cfgas_formatstring.cpp | 56 +++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 30 deletions(-) (limited to 'xfa/fgas/crt') 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) && -- cgit v1.2.3