Use span<> in more fxjs/xfa code.
Change-Id: I3f4269cc963a9dbb8b94fe762486b5ac2950a8ff
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/52711
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/fxjs/xfa/cfxjse_formcalc_context.cpp b/fxjs/xfa/cfxjse_formcalc_context.cpp
index 879ba02..47b52a9 100644
--- a/fxjs/xfa/cfxjse_formcalc_context.cpp
+++ b/fxjs/xfa/cfxjse_formcalc_context.cpp
@@ -497,8 +497,7 @@
return bsGUID;
}
-bool IsIsoDateFormat(const char* pData,
- int32_t iLength,
+bool IsIsoDateFormat(pdfium::span<const char> pData,
int32_t* pStyle,
int32_t* pYear,
int32_t* pMonth,
@@ -512,7 +511,7 @@
iMonth = 1;
iDay = 1;
- if (iLength < 4)
+ if (pData.size() < 4)
return false;
char szYear[5];
@@ -525,17 +524,16 @@
}
iYear = FXSYS_atoi(szYear);
iStyle = 0;
- if (iLength == 4)
+ if (pData.size() == 4)
return true;
iStyle = pData[4] == '-' ? 1 : 0;
- char szBuffer[3];
- szBuffer[2] = '\0';
- int32_t iPosOff = iStyle == 0 ? 4 : 5;
+ size_t iPosOff = iStyle == 0 ? 4 : 5;
if (!std::isdigit(pData[iPosOff]) || !std::isdigit(pData[iPosOff + 1]))
return false;
+ char szBuffer[3] = {};
szBuffer[0] = pData[iPosOff];
szBuffer[1] = pData[iPosOff + 1];
iMonth = FXSYS_atoi(szBuffer);
@@ -544,11 +542,11 @@
if (iStyle == 0) {
iPosOff += 2;
- if (iLength == 6)
+ if (pData.size() == 6)
return true;
} else {
iPosOff += 3;
- if (iLength == 7)
+ if (pData.size() == 7)
return true;
}
if (!std::isdigit(pData[iPosOff]) || !std::isdigit(pData[iPosOff + 1]))
@@ -557,7 +555,7 @@
szBuffer[0] = pData[iPosOff];
szBuffer[1] = pData[iPosOff + 1];
iDay = FXSYS_atoi(szBuffer);
- if (iPosOff + 2 < iLength)
+ if (iPosOff + 2 < pData.size())
return false;
if (iMonth == 2) {
@@ -570,8 +568,7 @@
return iDay <= (iMonth % 2 == 0 ? 31 : 30);
}
-bool IsIsoTimeFormat(const char* pData,
- int32_t iLength,
+bool IsIsoTimeFormat(pdfium::span<const char> pData,
int32_t* pHour,
int32_t* pMinute,
int32_t* pSecond,
@@ -591,25 +588,25 @@
iMilliSecond = 0;
iZoneHour = 0;
iZoneMinute = 0;
- if (!pData)
+
+ if (pData.empty())
return false;
- char szBuffer[3];
- szBuffer[2] = '\0';
- int32_t iZone = 0;
- int32_t i = 0;
- while (i < iLength) {
+ size_t iZone = 0;
+ size_t i = 0;
+ while (i < pData.size()) {
if (!std::isdigit(pData[i]) && pData[i] != ':') {
iZone = i;
break;
}
++i;
}
- if (i == iLength)
- iZone = iLength;
+ if (i == pData.size())
+ iZone = pData.size();
- int32_t iPos = 0;
- int32_t iIndex = 0;
+ char szBuffer[3] = {};
+ size_t iPos = 0;
+ size_t iIndex = 0;
while (iIndex < iZone) {
if (!std::isdigit(pData[iIndex]))
return false;
@@ -648,9 +645,9 @@
}
}
- if (iIndex < iLength && pData[iIndex] == '.') {
+ if (iIndex < pData.size() && pData[iIndex] == '.') {
constexpr int kSubSecondLength = 3;
- if (iIndex + kSubSecondLength >= iLength)
+ if (iIndex + kSubSecondLength >= pData.size())
return false;
++iIndex;
@@ -671,11 +668,11 @@
iIndex += kSubSecondLength;
}
- if (iIndex < iLength && FXSYS_towlower(pData[iIndex]) == 'z')
+ if (iIndex < pData.size() && FXSYS_towlower(pData[iIndex]) == 'z')
return true;
int32_t iSign = 1;
- if (iIndex < iLength) {
+ if (iIndex < pData.size()) {
if (pData[iIndex] == '+') {
++iIndex;
} else if (pData[iIndex] == '-') {
@@ -684,7 +681,7 @@
}
}
iPos = 0;
- while (iIndex < iLength) {
+ while (iIndex < pData.size()) {
if (!std::isdigit(pData[iIndex]))
return false;
@@ -714,15 +711,14 @@
iIndex += 2;
}
}
- if (iIndex < iLength)
+ if (iIndex < pData.size())
return false;
iZoneHour *= iSign;
return true;
}
-bool IsIsoDateTimeFormat(const char* pData,
- int32_t iLength,
+bool IsIsoDateTimeFormat(pdfium::span<const char> pData,
int32_t* pYear,
int32_t* pMonth,
int32_t* pDay,
@@ -748,12 +744,13 @@
iHour = 0;
iMinute = 0;
iSecond = 0;
- if (!pData)
+
+ if (pData.empty())
return false;
- int32_t iIndex = 0;
+ size_t iIndex = 0;
while (pData[iIndex] != 'T' && pData[iIndex] != 't') {
- if (iIndex >= iLength)
+ if (iIndex >= pData.size())
return false;
++iIndex;
}
@@ -761,14 +758,15 @@
return false;
int32_t iStyle = -1;
- if (!IsIsoDateFormat(pData, iIndex, &iStyle, &iYear, &iMonth, &iDay))
+ if (!IsIsoDateFormat(pData.subspan(0, iIndex), &iStyle, &iYear, &iMonth,
+ &iDay)) {
return false;
+ }
if (pData[iIndex] != 'T' && pData[iIndex] != 't')
return true;
- ++iIndex;
- return IsIsoTimeFormat(pData + iIndex, iLength - iIndex, &iHour, &iMinute,
- &iSecond, &iMilliSecond, &iZoneHour, &iZoneMinute);
+ return IsIsoTimeFormat(pData.subspan(iIndex + 1), &iHour, &iMinute, &iSecond,
+ &iMilliSecond, &iZoneHour, &iZoneMinute);
}
int32_t DateString2Num(ByteStringView bsDate) {
@@ -778,10 +776,8 @@
int32_t iDay = 0;
if (iLength <= 10) {
int32_t iStyle = -1;
- if (!IsIsoDateFormat(bsDate.unterminated_c_str(), iLength, &iStyle, &iYear,
- &iMonth, &iDay)) {
+ if (!IsIsoDateFormat(bsDate.span(), &iStyle, &iYear, &iMonth, &iDay))
return 0;
- }
} else {
int32_t iHour = 0;
int32_t iMinute = 0;
@@ -789,9 +785,9 @@
int32_t iMilliSecond = 0;
int32_t iZoneHour = 0;
int32_t iZoneMinute = 0;
- if (!IsIsoDateTimeFormat(bsDate.unterminated_c_str(), iLength, &iYear,
- &iMonth, &iDay, &iHour, &iMinute, &iSecond,
- &iMilliSecond, &iZoneHour, &iZoneMinute)) {
+ if (!IsIsoDateTimeFormat(bsDate.span(), &iYear, &iMonth, &iDay, &iHour,
+ &iMinute, &iSecond, &iMilliSecond, &iZoneHour,
+ &iZoneMinute)) {
return 0;
}
}
diff --git a/fxjs/xfa/cfxjse_resolveprocessor.cpp b/fxjs/xfa/cfxjse_resolveprocessor.cpp
index a9b9634..190931c 100644
--- a/fxjs/xfa/cfxjse_resolveprocessor.cpp
+++ b/fxjs/xfa/cfxjse_resolveprocessor.cpp
@@ -161,8 +161,8 @@
if (rnd.m_nLevel > 0)
return false;
- XFA_HashCode dwNameHash = static_cast<XFA_HashCode>(FX_HashCode_GetW(
- WideStringView(wsName.c_str() + 1, iNameLen - 1), false));
+ XFA_HashCode dwNameHash = static_cast<XFA_HashCode>(
+ FX_HashCode_GetW(wsName.AsStringView().Right(iNameLen - 1), false));
if (dwNameHash == XFA_HASHCODE_Xfa) {
rnd.m_Objects.emplace_back(rnd.m_pSC->GetDocument()->GetRoot());
} else {
@@ -532,9 +532,9 @@
pdfium::span<wchar_t> pNameBuf = wsName.GetBuffer(iLength - nStart);
pdfium::span<wchar_t> pConditionBuf =
wsCondition.GetBuffer(iLength - nStart);
+ pdfium::span<const wchar_t> pSrc = wsExpression.span();
std::vector<int32_t> stack;
int32_t nType = -1;
- const wchar_t* pSrc = wsExpression.unterminated_c_str();
wchar_t wPrev = 0;
wchar_t wCur;
bool bIsCondition = false;