diff options
author | Ryan Harrison <rharrison@chromium.org> | 2017-08-23 10:39:35 -0400 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-08-23 15:11:19 +0000 |
commit | 12db7515f17228798d1aa38fce0fee3e7d2d36b6 (patch) | |
tree | e291daf9e6a88ba0248670b9f1ba3a555f052538 /xfa/fxfa/parser/cxfa_widgetdata.cpp | |
parent | 3bb0a34cc75abe49a59c6390353957bbb5c5ab38 (diff) | |
download | pdfium-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 'xfa/fxfa/parser/cxfa_widgetdata.cpp')
-rw-r--r-- | xfa/fxfa/parser/cxfa_widgetdata.cpp | 43 |
1 files changed, 21 insertions, 22 deletions
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<CFX_WideString> 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 = |