summaryrefslogtreecommitdiff
path: root/xfa/fxfa/parser
diff options
context:
space:
mode:
authorRyan Harrison <rharrison@chromium.org>2017-08-23 10:39:35 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-08-23 15:11:19 +0000
commit12db7515f17228798d1aa38fce0fee3e7d2d36b6 (patch)
treee291daf9e6a88ba0248670b9f1ba3a555f052538 /xfa/fxfa/parser
parent3bb0a34cc75abe49a59c6390353957bbb5c5ab38 (diff)
downloadpdfium-12db7515f17228798d1aa38fce0fee3e7d2d36b6.tar.xz
Convert string Find methods to return an Optional
The Find and ReverseFind methods for WideString, WideStringC, ByteString, and ByteStringC have been converted from returning a raw FX_STRSIZE, to returning Optional<FX_STRSIZE>, so that success/failure can be indicated without using FX_STRNPOS. This allows for removing FX_STRNPOS and by association makes the conversion of FX_STRSIZE to size_t easier, since it forces checking the return value of Find to be explictly done as well as taking the error value out of the range of FX_STRSIZE. New Contains methods have been added for cases where the success or failure is all the call site to Find cared about, and the actual position was ignored. BUG=pdfium:828 Change-Id: Id827e508c8660affa68cc08a13d96121369364b7 Reviewed-on: https://pdfium-review.googlesource.com/11350 Commit-Queue: Ryan Harrison <rharrison@chromium.org> Reviewed-by: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'xfa/fxfa/parser')
-rw-r--r--xfa/fxfa/parser/cxfa_dataexporter.cpp12
-rw-r--r--xfa/fxfa/parser/cxfa_document.cpp33
-rw-r--r--xfa/fxfa/parser/cxfa_layoutpagemgr.cpp10
-rw-r--r--xfa/fxfa/parser/cxfa_localevalue.cpp18
-rw-r--r--xfa/fxfa/parser/cxfa_node.cpp13
-rw-r--r--xfa/fxfa/parser/cxfa_simple_parser.cpp14
-rw-r--r--xfa/fxfa/parser/cxfa_widgetdata.cpp43
7 files changed, 74 insertions, 69 deletions
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<CFX_WideString> 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<FX_STRSIZE> 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<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 =