From da129ab38c3fb6ed3de85ffb6f8938eb31130a53 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Tue, 1 Aug 2017 15:16:59 -0400 Subject: Replace raw value for constant error value in string operations Currently Find() and other methods that return a FX_STRSIZE return -1 to indicate error/failure. This means that there is a lot of magic numbers and magic checks floating around. The standard library for similar operations uses a npos constant. This CL implements FX_STRNPOS, and replaces usages of magic number checking. It also does some type cleanup along the way where it was obvious that FX_STRSIZE should be being used. Removing the magic numbers should make eventually changing FX_STRSIZE to be unsigned easier in the future. BUG=pdfium:828 Change-Id: I67e481e44cf2f75a1698afa8fbee4f375a74c490 Reviewed-on: https://pdfium-review.googlesource.com/9651 Commit-Queue: Ryan Harrison Reviewed-by: Tom Sepez --- xfa/fxfa/parser/cxfa_widgetdata.cpp | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 'xfa/fxfa/parser/cxfa_widgetdata.cpp') diff --git a/xfa/fxfa/parser/cxfa_widgetdata.cpp b/xfa/fxfa/parser/cxfa_widgetdata.cpp index 5efc0ea481..34e3aeaf0f 100644 --- a/xfa/fxfa/parser/cxfa_widgetdata.cpp +++ b/xfa/fxfa/parser/cxfa_widgetdata.cpp @@ -42,11 +42,10 @@ bool SplitDateTime(const CFX_WideString& wsDateTime, if (wsDateTime.IsEmpty()) return false; - int nSplitIndex = -1; - nSplitIndex = wsDateTime.Find('T'); - if (nSplitIndex < 0) + FX_STRSIZE nSplitIndex = wsDateTime.Find('T'); + if (nSplitIndex == FX_STRNPOS) nSplitIndex = wsDateTime.Find(' '); - if (nSplitIndex < 0) + if (nSplitIndex == FX_STRNPOS) return false; wsDate = wsDateTime.Left(nSplitIndex); @@ -848,17 +847,17 @@ std::vector CXFA_WidgetData::GetSelectedItemsValue() { CFX_WideString wsValue = GetRawValue(); if (GetChoiceListOpen() == XFA_ATTRIBUTEENUM_MultiSelect) { if (!wsValue.IsEmpty()) { - int32_t iStart = 0; - int32_t iLength = wsValue.GetLength(); - int32_t iEnd = wsValue.Find(L'\n', iStart); - iEnd = (iEnd == -1) ? iLength : iEnd; + FX_STRSIZE iStart = 0; + FX_STRSIZE iLength = wsValue.GetLength(); + FX_STRSIZE iEnd = wsValue.Find(L'\n', iStart); + iEnd = (iEnd == FX_STRNPOS) ? iLength : iEnd; while (iEnd >= iStart) { wsSelTextArray.push_back(wsValue.Mid(iStart, iEnd - iStart)); iStart = iEnd + 1; if (iStart >= iLength) break; iEnd = wsValue.Find(L'\n', iStart); - if (iEnd < 0) + if (iEnd == FX_STRNPOS) wsSelTextArray.push_back(wsValue.Mid(iStart, iLength - iStart)); } } @@ -1318,7 +1317,7 @@ bool CXFA_WidgetData::GetBarcodeAttribute_WideNarrowRatio(float* val) { if (pUIChild->TryCData(XFA_ATTRIBUTE_WideNarrowRatio, wsWideNarrowRatio)) { FX_STRSIZE ptPos = wsWideNarrowRatio.Find(':'); float fRatio = 0; - if (ptPos >= 0) { + if (ptPos != FX_STRNPOS) { fRatio = (float)FXSYS_wtoi(wsWideNarrowRatio.c_str()); } else { int32_t fA, fB; @@ -1743,9 +1742,9 @@ void CXFA_WidgetData::NormalizeNumStr(const CFX_WideString& wsValue, wsOutput = wsValue; wsOutput.TrimLeft('0'); - int32_t dot_index = wsOutput.Find('.'); + FX_STRSIZE dot_index = wsOutput.Find('.'); int32_t iFracDigits = 0; - if (!wsOutput.IsEmpty() && dot_index >= 0 && + if (!wsOutput.IsEmpty() && dot_index != FX_STRNPOS && (!GetFracDigits(iFracDigits) || iFracDigits != -1)) { wsOutput.TrimRight(L"0"); wsOutput.TrimRight(L"."); @@ -1769,8 +1768,8 @@ void CXFA_WidgetData::FormatNumStr(const CFX_WideString& wsValue, wsSrcNum.Delete(0, 1); } int32_t len = wsSrcNum.GetLength(); - int32_t dot_index = wsSrcNum.Find('.'); - if (dot_index == -1) + FX_STRSIZE dot_index = wsSrcNum.Find('.'); + if (dot_index == FX_STRNPOS) dot_index = len; int32_t cc = dot_index - 1; -- cgit v1.2.3