summaryrefslogtreecommitdiff
path: root/xfa/fxfa/fm2js
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/fm2js
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/fm2js')
-rw-r--r--xfa/fxfa/fm2js/cxfa_fm2jscontext.cpp39
1 files changed, 25 insertions, 14 deletions
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);