Use spans in CFGAS_FormatString, part 3

Change-Id: Ide193c76a9400cdbeba6616fb22400ef5ec03b52
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/51651
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/xfa/fgas/crt/cfgas_formatstring.cpp b/xfa/fgas/crt/cfgas_formatstring.cpp
index 46594dd..1d4fd55 100644
--- a/xfa/fgas/crt/cfgas_formatstring.cpp
+++ b/xfa/fgas/crt/cfgas_formatstring.cpp
@@ -182,14 +182,14 @@
                         const WideString& wsDotSymbol,
                         int32_t* iDotIndex) {
   pdfium::span<const wchar_t> spNum = wsNum.AsSpan();
-  size_t iLenDot = wsDotSymbol.GetLength();
+  pdfium::span<const wchar_t> spDotSymbol = wsDotSymbol.AsSpan();
   for (size_t ccf = 0; ccf < spNum.size(); ++ccf) {
     if (spNum[ccf] == '\'') {
       GetLiteralText(spNum, &ccf);
       continue;
     }
-    if (ccf + iLenDot <= spNum.size() &&
-        wcsncmp(&spNum[ccf], wsDotSymbol.c_str(), iLenDot) == 0) {
+    if (ccf + spDotSymbol.size() <= spNum.size() &&
+        wcsncmp(&spNum[ccf], spDotSymbol.data(), spDotSymbol.size()) == 0) {
       *iDotIndex = ccf;
       return true;
     }
@@ -233,14 +233,14 @@
   uint32_t day = 1;
   size_t ccf = 0;
   pdfium::span<const wchar_t> spDate = wsDate.AsSpan();
-  int32_t len = wsDate.GetLength();
   pdfium::span<const wchar_t> spDatePattern = wsDatePattern.AsSpan();
   WideStringView wsDateSymbols(gs_wsDateSymbols);
-  while (*cc < len && ccf < spDatePattern.size()) {
+  while (*cc < pdfium::CollectionSize<int32_t>(spDate) &&
+         ccf < spDatePattern.size()) {
     if (spDatePattern[ccf] == '\'') {
       WideString wsLiteral = GetLiteralText(spDatePattern, &ccf);
       int32_t iLiteralLen = wsLiteral.GetLength();
-      if (*cc + iLiteralLen > len ||
+      if (*cc + iLiteralLen > pdfium::CollectionSize<int32_t>(spDate) ||
           wcsncmp(spDate.data() + *cc, wsLiteral.c_str(), iLiteralLen) != 0) {
         return false;
       }
@@ -299,7 +299,7 @@
         }
       }
     } else if (symbol.EqualsASCII("YY") || symbol.EqualsASCII("YYYY")) {
-      if (*cc + pdfium::base::checked_cast<int32_t>(symbol.GetLength()) > len)
+      if (*cc + symbol.GetLength() > spDate.size())
         return false;
 
       year = 0;
@@ -318,7 +318,7 @@
       *cc += symbol.GetLength();
     }
   }
-  if (*cc < len)
+  if (*cc < pdfium::CollectionSize<int32_t>(spDate))
     return false;
 
   datetime->SetDate(year, month, day);
@@ -1071,18 +1071,19 @@
   if (wsTextFormat.IsEmpty())
     return false;
 
-  int32_t iText = 0;
-  size_t iPattern = 0;
-  const wchar_t* pStrText = wsSrcText.c_str();
-  int32_t iLenText = wsSrcText.GetLength();
+  pdfium::span<const wchar_t> spSrcText = wsSrcText.AsSpan();
   pdfium::span<const wchar_t> spTextFormat = wsTextFormat.AsSpan();
-  while (iPattern < spTextFormat.size() && iText < iLenText) {
+
+  size_t iText = 0;
+  size_t iPattern = 0;
+  while (iPattern < spTextFormat.size() && iText < spSrcText.size()) {
     switch (spTextFormat[iPattern]) {
       case '\'': {
         WideString wsLiteral = GetLiteralText(spTextFormat, &iPattern);
         int32_t iLiteralLen = wsLiteral.GetLength();
-        if (iText + iLiteralLen > iLenText ||
-            wcsncmp(pStrText + iText, wsLiteral.c_str(), iLiteralLen)) {
+        if (iText + iLiteralLen > spSrcText.size() ||
+            wcsncmp(spSrcText.data() + iText, wsLiteral.c_str(), iLiteralLen) !=
+                0) {
           *wsValue = wsSrcText;
           return false;
         }
@@ -1091,35 +1092,35 @@
         break;
       }
       case 'A':
-        if (FXSYS_iswalpha(pStrText[iText])) {
-          *wsValue += pStrText[iText];
+        if (FXSYS_iswalpha(spSrcText[iText])) {
+          *wsValue += spSrcText[iText];
           iText++;
         }
         iPattern++;
         break;
       case 'X':
-        *wsValue += pStrText[iText];
+        *wsValue += spSrcText[iText];
         iText++;
         iPattern++;
         break;
       case 'O':
       case '0':
-        if (FXSYS_IsDecimalDigit(pStrText[iText]) ||
-            FXSYS_iswalpha(pStrText[iText])) {
-          *wsValue += pStrText[iText];
+        if (FXSYS_IsDecimalDigit(spSrcText[iText]) ||
+            FXSYS_iswalpha(spSrcText[iText])) {
+          *wsValue += spSrcText[iText];
           iText++;
         }
         iPattern++;
         break;
       case '9':
-        if (FXSYS_IsDecimalDigit(pStrText[iText])) {
-          *wsValue += pStrText[iText];
+        if (FXSYS_IsDecimalDigit(spSrcText[iText])) {
+          *wsValue += spSrcText[iText];
           iText++;
         }
         iPattern++;
         break;
       default:
-        if (spTextFormat[iPattern] != pStrText[iText]) {
+        if (spTextFormat[iPattern] != spSrcText[iText]) {
           *wsValue = wsSrcText;
           return false;
         }
@@ -1128,7 +1129,7 @@
         break;
     }
   }
-  return iPattern == spTextFormat.size() && iText == iLenText;
+  return iPattern == spTextFormat.size() && iText == spSrcText.size();
 }
 
 bool CFGAS_FormatString::ParseNum(const WideString& wsSrcNum,
@@ -1152,10 +1153,10 @@
   int32_t iGroupLen = wsGroupSymbol.GetLength();
   WideString wsMinus = pLocale->GetMinusSymbol();
   int32_t iMinusLen = wsMinus.GetLength();
-  const wchar_t* str = wsSrcNum.c_str();
-  int len = wsSrcNum.GetLength();
 
+  pdfium::span<const wchar_t> spSrcNum = wsSrcNum.AsSpan();
   pdfium::span<const wchar_t> spNumFormat = wsNumFormat.AsSpan();
+
   bool bHavePercentSymbol = false;
   bool bNeg = false;
   bool bReverseParse = false;
@@ -1183,26 +1184,27 @@
         WideString wsLiteral = GetLiteralTextReverse(spNumFormat, &ccf);
         int32_t iLiteralLen = wsLiteral.GetLength();
         cc -= iLiteralLen - 1;
-        if (cc < 0 || wcsncmp(str + cc, wsLiteral.c_str(), iLiteralLen))
+        if (cc < 0 || wcsncmp(spSrcNum.data() + cc, wsLiteral.c_str(),
+                              iLiteralLen) != 0) {
           return false;
-
+        }
         cc--;
         ccf--;
         break;
       }
       case '9':
-        if (!FXSYS_IsDecimalDigit(str[cc]))
+        if (!FXSYS_IsDecimalDigit(spSrcNum[cc]))
           return false;
 
-        wsValue->InsertAtFront(str[cc]);
+        wsValue->InsertAtFront(spSrcNum[cc]);
         cc--;
         ccf--;
         break;
       case 'z':
       case 'Z':
-        if (spNumFormat[ccf] == 'z' || str[cc] != ' ') {
-          if (FXSYS_IsDecimalDigit(str[cc])) {
-            wsValue->InsertAtFront(str[cc]);
+        if (spNumFormat[ccf] == 'z' || spSrcNum[cc] != ' ') {
+          if (FXSYS_IsDecimalDigit(spSrcNum[cc])) {
+            wsValue->InsertAtFront(spSrcNum[cc]);
             cc--;
           }
         } else {
@@ -1212,13 +1214,15 @@
         break;
       case 'S':
       case 's':
-        if (str[cc] == '+' || (spNumFormat[ccf] == 'S' && str[cc] == ' ')) {
+        if (spSrcNum[cc] == '+' ||
+            (spNumFormat[ccf] == 'S' && spSrcNum[cc] == ' ')) {
           cc--;
         } else {
           cc -= iMinusLen - 1;
-          if (cc < 0 || wcsncmp(str + cc, wsMinus.c_str(), iMinusLen))
+          if (cc < 0 ||
+              wcsncmp(spSrcNum.data() + cc, wsMinus.c_str(), iMinusLen) != 0) {
             return false;
-
+          }
           cc--;
           bNeg = true;
         }
@@ -1227,19 +1231,20 @@
       case 'E': {
         bool bExpSign = false;
         while (cc >= 0) {
-          if (str[cc] == 'E' || str[cc] == 'e')
+          if (spSrcNum[cc] == 'E' || spSrcNum[cc] == 'e')
             break;
-          if (FXSYS_IsDecimalDigit(str[cc])) {
-            iExponent = iExponent + FXSYS_DecimalCharToInt(str[cc]) * 10;
+          if (FXSYS_IsDecimalDigit(spSrcNum[cc])) {
+            iExponent = iExponent + FXSYS_DecimalCharToInt(spSrcNum[cc]) * 10;
             cc--;
             continue;
           }
-          if (str[cc] == '+') {
+          if (spSrcNum[cc] == '+') {
             cc--;
             continue;
           }
-          if (cc - iMinusLen + 1 > 0 && !wcsncmp(str + (cc - iMinusLen + 1),
-                                                 wsMinus.c_str(), iMinusLen)) {
+          if (cc - iMinusLen + 1 > 0 &&
+              wcsncmp(spSrcNum.data() + (cc - iMinusLen + 1), wsMinus.c_str(),
+                      iMinusLen) == 0) {
             bExpSign = true;
             cc -= iMinusLen;
             continue;
@@ -1256,9 +1261,10 @@
         WideString wsSymbol = pLocale->GetCurrencySymbol();
         int32_t iSymbolLen = wsSymbol.GetLength();
         cc -= iSymbolLen - 1;
-        if (cc < 0 || wcsncmp(str + cc, wsSymbol.c_str(), iSymbolLen))
+        if (cc < 0 ||
+            wcsncmp(spSrcNum.data() + cc, wsSymbol.c_str(), iSymbolLen) != 0) {
           return false;
-
+        }
         cc--;
         ccf--;
         break;
@@ -1268,9 +1274,10 @@
         if (ccf - 1 >= 0 &&
             ((spNumFormat[ccf] == 'R' && spNumFormat[ccf - 1] == 'C') ||
              (spNumFormat[ccf] == 'r' && spNumFormat[ccf - 1] == 'c'))) {
-          if (spNumFormat[ccf] == 'R' && str[cc] == ' ') {
+          if (spNumFormat[ccf] == 'R' && spSrcNum[cc] == ' ') {
             cc -= 2;
-          } else if (str[cc] == 'R' && cc - 1 >= 0 && str[cc - 1] == 'C') {
+          } else if (spSrcNum[cc] == 'R' && cc - 1 >= 0 &&
+                     spSrcNum[cc - 1] == 'C') {
             bNeg = true;
             cc -= 2;
           }
@@ -1284,9 +1291,10 @@
         if (ccf - 1 >= 0 &&
             ((spNumFormat[ccf] == 'B' && spNumFormat[ccf - 1] == 'D') ||
              (spNumFormat[ccf] == 'b' && spNumFormat[ccf - 1] == 'd'))) {
-          if (spNumFormat[ccf] == 'B' && str[cc] == ' ') {
+          if (spNumFormat[ccf] == 'B' && spSrcNum[cc] == ' ') {
             cc -= 2;
-          } else if (str[cc] == 'B' && cc - 1 >= 0 && str[cc - 1] == 'D') {
+          } else if (spSrcNum[cc] == 'B' && cc - 1 >= 0 &&
+                     spSrcNum[cc - 1] == 'D') {
             bNeg = true;
             cc -= 2;
           }
@@ -1299,9 +1307,10 @@
         WideString wsSymbol = pLocale->GetPercentSymbol();
         int32_t iSysmbolLen = wsSymbol.GetLength();
         cc -= iSysmbolLen - 1;
-        if (cc < 0 || wcsncmp(str + cc, wsSymbol.c_str(), iSysmbolLen))
+        if (cc < 0 ||
+            wcsncmp(spSrcNum.data() + cc, wsSymbol.c_str(), iSysmbolLen) != 0) {
           return false;
-
+        }
         cc--;
         ccf--;
         bHavePercentSymbol = true;
@@ -1315,8 +1324,8 @@
       case ',': {
         if (cc >= 0) {
           cc -= iGroupLen - 1;
-          if (cc >= 0 &&
-              wcsncmp(str + cc, wsGroupSymbol.c_str(), iGroupLen) == 0) {
+          if (cc >= 0 && wcsncmp(spSrcNum.data() + cc, wsGroupSymbol.c_str(),
+                                 iGroupLen) == 0) {
             cc--;
           } else {
             cc += iGroupLen - 1;
@@ -1327,16 +1336,16 @@
       }
       case '(':
       case ')':
-        if (str[cc] == spNumFormat[ccf])
+        if (spSrcNum[cc] == spNumFormat[ccf])
           bNeg = true;
-        else if (str[cc] != L' ')
+        else if (spSrcNum[cc] != L' ')
           return false;
 
         cc--;
         ccf--;
         break;
       default:
-        if (spNumFormat[ccf] != str[cc])
+        if (spNumFormat[ccf] != spSrcNum[cc])
           return false;
 
         cc--;
@@ -1344,42 +1353,49 @@
     }
   }
   if (cc >= 0) {
-    if (str[cc] == '-') {
+    if (spSrcNum[cc] == '-') {
       bNeg = true;
       cc--;
     }
     if (cc >= 0)
       return false;
   }
-  if (dot_index < len && (dwFormatStyle & FX_NUMSTYLE_DotVorv))
+  if ((dwFormatStyle & FX_NUMSTYLE_DotVorv) &&
+      dot_index < pdfium::CollectionSize<int32_t>(spSrcNum)) {
     *wsValue += '.';
+  }
   if (!bReverseParse) {
-    cc = (dot_index == len) ? len : dot_index + 1;
-    for (size_t ccf = dot_index_f + 1; cc < len && ccf < spNumFormat.size();
+    cc = (dot_index == pdfium::CollectionSize<int32_t>(spSrcNum))
+             ? spSrcNum.size()
+             : dot_index + 1;
+    for (size_t ccf = dot_index_f + 1;
+         cc < pdfium::CollectionSize<int32_t>(spSrcNum) &&
+         ccf < spNumFormat.size();
          ++ccf) {
       switch (spNumFormat[ccf]) {
         case '\'': {
           WideString wsLiteral = GetLiteralText(spNumFormat, &ccf);
           int32_t iLiteralLen = wsLiteral.GetLength();
-          if (cc + iLiteralLen > len ||
-              wcsncmp(str + cc, wsLiteral.c_str(), iLiteralLen)) {
+          if (cc + iLiteralLen > pdfium::CollectionSize<int32_t>(spSrcNum) ||
+              wcsncmp(spSrcNum.data() + cc, wsLiteral.c_str(), iLiteralLen) !=
+                  0) {
             return false;
           }
           cc += iLiteralLen;
           break;
         }
         case '9':
-          if (!FXSYS_IsDecimalDigit(str[cc]))
+          if (!FXSYS_IsDecimalDigit(spSrcNum[cc]))
             return false;
 
-          *wsValue += str[cc];
+          *wsValue += spSrcNum[cc];
           cc++;
           break;
         case 'z':
         case 'Z':
-          if (spNumFormat[ccf] == 'z' || str[cc] != ' ') {
-            if (FXSYS_IsDecimalDigit(str[cc])) {
-              *wsValue += str[cc];
+          if (spNumFormat[ccf] == 'z' || spSrcNum[cc] != ' ') {
+            if (FXSYS_IsDecimalDigit(spSrcNum[cc])) {
+              *wsValue += spSrcNum[cc];
               cc++;
             }
           } else {
@@ -1388,11 +1404,13 @@
           break;
         case 'S':
         case 's':
-          if (str[cc] == '+' || (spNumFormat[ccf] == 'S' && str[cc] == ' ')) {
+          if (spSrcNum[cc] == '+' ||
+              (spNumFormat[ccf] == 'S' && spSrcNum[cc] == ' ')) {
             cc++;
           } else {
-            if (cc + iMinusLen > len ||
-                wcsncmp(str + cc, wsMinus.c_str(), iMinusLen)) {
+            if (cc + iMinusLen > pdfium::CollectionSize<int32_t>(spSrcNum) ||
+                wcsncmp(spSrcNum.data() + cc, wsMinus.c_str(), iMinusLen) !=
+                    0) {
               return false;
             }
             bNeg = true;
@@ -1400,24 +1418,25 @@
           }
           break;
         case 'E': {
-          if (cc >= len || (str[cc] != 'E' && str[cc] != 'e'))
+          if (cc >= pdfium::CollectionSize<int32_t>(spSrcNum) ||
+              (spSrcNum[cc] != 'E' && spSrcNum[cc] != 'e')) {
             return false;
-
+          }
           bool bExpSign = false;
           cc++;
-          if (cc < len) {
-            if (str[cc] == '+') {
+          if (cc < pdfium::CollectionSize<int32_t>(spSrcNum)) {
+            if (spSrcNum[cc] == '+') {
               cc++;
-            } else if (str[cc] == '-') {
+            } else if (spSrcNum[cc] == '-') {
               bExpSign = true;
               cc++;
             }
           }
-          while (cc < len) {
-            if (!FXSYS_IsDecimalDigit(str[cc]))
+          while (cc < pdfium::CollectionSize<int32_t>(spSrcNum)) {
+            if (!FXSYS_IsDecimalDigit(spSrcNum[cc]))
               break;
 
-            iExponent = iExponent * 10 + FXSYS_DecimalCharToInt(str[cc]);
+            iExponent = iExponent * 10 + FXSYS_DecimalCharToInt(spSrcNum[cc]);
             cc++;
           }
           iExponent = bExpSign ? -iExponent : iExponent;
@@ -1426,8 +1445,9 @@
         case '$': {
           WideString wsSymbol = pLocale->GetCurrencySymbol();
           int32_t iSymbolLen = wsSymbol.GetLength();
-          if (cc + iSymbolLen > len ||
-              wcsncmp(str + cc, wsSymbol.c_str(), iSymbolLen)) {
+          if (cc + iSymbolLen > pdfium::CollectionSize<int32_t>(spSrcNum) ||
+              wcsncmp(spSrcNum.data() + cc, wsSymbol.c_str(), iSymbolLen) !=
+                  0) {
             return false;
           }
           cc += iSymbolLen;
@@ -1438,9 +1458,11 @@
           if (ccf + 1 < spNumFormat.size() &&
               ((spNumFormat[ccf] == 'C' && spNumFormat[ccf + 1] == 'R') ||
                (spNumFormat[ccf] == 'c' && spNumFormat[ccf + 1] == 'r'))) {
-            if (spNumFormat[ccf] == 'C' && str[cc] == ' ') {
+            if (spNumFormat[ccf] == 'C' && spSrcNum[cc] == ' ') {
               cc++;
-            } else if (str[cc] == 'C' && cc + 1 < len && str[cc + 1] == 'R') {
+            } else if (spSrcNum[cc] == 'C' &&
+                       cc + 1 < pdfium::CollectionSize<int32_t>(spSrcNum) &&
+                       spSrcNum[cc + 1] == 'R') {
               bNeg = true;
               cc += 2;
             }
@@ -1452,9 +1474,11 @@
           if (ccf + 1 < spNumFormat.size() &&
               ((spNumFormat[ccf] == 'D' && spNumFormat[ccf + 1] == 'B') ||
                (spNumFormat[ccf] == 'd' && spNumFormat[ccf + 1] == 'b'))) {
-            if (spNumFormat[ccf] == 'D' && str[cc] == ' ') {
+            if (spNumFormat[ccf] == 'D' && spSrcNum[cc] == ' ') {
               cc++;
-            } else if (str[cc] == 'D' && cc + 1 < len && str[cc + 1] == 'B') {
+            } else if (spSrcNum[cc] == 'D' &&
+                       cc + 1 < pdfium::CollectionSize<int32_t>(spSrcNum) &&
+                       spSrcNum[cc + 1] == 'B') {
               bNeg = true;
               cc += 2;
             }
@@ -1468,8 +1492,9 @@
         case '%': {
           WideString wsSymbol = pLocale->GetPercentSymbol();
           int32_t iSysmbolLen = wsSymbol.GetLength();
-          if (cc + iSysmbolLen <= len &&
-              !wcsncmp(str + cc, wsSymbol.c_str(), iSysmbolLen)) {
+          if (cc + iSysmbolLen <= pdfium::CollectionSize<int32_t>(spSrcNum) &&
+              wcsncmp(spSrcNum.data() + cc, wsSymbol.c_str(), iSysmbolLen) ==
+                  0) {
             cc += iSysmbolLen;
           }
           bHavePercentSymbol = true;
@@ -1478,35 +1503,37 @@
           while (ccf + 1 < spNumFormat.size() && spNumFormat[ccf + 1] == '8')
             ccf++;
 
-          while (cc < len && FXSYS_IsDecimalDigit(str[cc])) {
-            *wsValue += str[cc];
+          while (cc < pdfium::CollectionSize<int32_t>(spSrcNum) &&
+                 FXSYS_IsDecimalDigit(spSrcNum[cc])) {
+            *wsValue += spSrcNum[cc];
             cc++;
           }
         } break;
         case ',': {
-          if (cc + iGroupLen <= len &&
-              wcsncmp(str + cc, wsGroupSymbol.c_str(), iGroupLen) == 0) {
+          if (cc + iGroupLen <= pdfium::CollectionSize<int32_t>(spSrcNum) &&
+              wcsncmp(spSrcNum.data() + cc, wsGroupSymbol.c_str(), iGroupLen) ==
+                  0) {
             cc += iGroupLen;
           }
           break;
         }
         case '(':
         case ')':
-          if (str[cc] == spNumFormat[ccf])
+          if (spSrcNum[cc] == spNumFormat[ccf])
             bNeg = true;
-          else if (str[cc] != L' ')
+          else if (spSrcNum[cc] != L' ')
             return false;
 
           cc++;
           break;
         default:
-          if (spNumFormat[ccf] != str[cc])
+          if (spNumFormat[ccf] != spSrcNum[cc])
             return false;
 
           cc++;
       }
     }
-    if (cc != len)
+    if (cc != pdfium::CollectionSize<int32_t>(spSrcNum))
       return false;
   }
   if (iExponent || bHavePercentSymbol) {
@@ -1711,79 +1738,73 @@
 bool CFGAS_FormatString::ParseZero(const WideString& wsSrcText,
                                    const WideString& wsPattern) const {
   WideString wsTextFormat = GetTextFormat(wsPattern, L"zero");
-
-  int32_t iText = 0;
-  size_t iPattern = 0;
-  const wchar_t* pStrText = wsSrcText.c_str();
-  int32_t iLenText = wsSrcText.GetLength();
+  pdfium::span<const wchar_t> spSrcText = wsSrcText.AsSpan();
   pdfium::span<const wchar_t> spTextFormat = wsTextFormat.AsSpan();
-  while (iPattern < spTextFormat.size() && iText < iLenText) {
+
+  size_t iText = 0;
+  size_t iPattern = 0;
+  while (iPattern < spTextFormat.size() && iText < spSrcText.size()) {
     if (spTextFormat[iPattern] == '\'') {
       WideString wsLiteral = GetLiteralText(spTextFormat, &iPattern);
       int32_t iLiteralLen = wsLiteral.GetLength();
-      if (iText + iLiteralLen > iLenText ||
-          wcsncmp(pStrText + iText, wsLiteral.c_str(), iLiteralLen)) {
+      if (iText + iLiteralLen > spSrcText.size() ||
+          wcsncmp(spSrcText.data() + iText, wsLiteral.c_str(), iLiteralLen)) {
         return false;
       }
       iText += iLiteralLen;
       iPattern++;
       continue;
     }
-    if (spTextFormat[iPattern] != pStrText[iText])
+    if (spTextFormat[iPattern] != spSrcText[iText])
       return false;
 
     iText++;
     iPattern++;
   }
-  return iPattern == spTextFormat.size() && iText == iLenText;
+  return iPattern == spTextFormat.size() && iText == spSrcText.size();
 }
 
 bool CFGAS_FormatString::ParseNull(const WideString& wsSrcText,
                                    const WideString& wsPattern) const {
   WideString wsTextFormat = GetTextFormat(wsPattern, L"null");
-
-  int32_t iText = 0;
-  size_t iPattern = 0;
-  const wchar_t* pStrText = wsSrcText.c_str();
-  int32_t iLenText = wsSrcText.GetLength();
+  pdfium::span<const wchar_t> spSrcText = wsSrcText.AsSpan();
   pdfium::span<const wchar_t> spTextFormat = wsTextFormat.AsSpan();
-  while (iPattern < spTextFormat.size() && iText < iLenText) {
+
+  size_t iText = 0;
+  size_t iPattern = 0;
+  while (iPattern < spTextFormat.size() && iText < spSrcText.size()) {
     if (spTextFormat[iPattern] == '\'') {
       WideString wsLiteral = GetLiteralText(spTextFormat, &iPattern);
       int32_t iLiteralLen = wsLiteral.GetLength();
-      if (iText + iLiteralLen > iLenText ||
-          wcsncmp(pStrText + iText, wsLiteral.c_str(), iLiteralLen)) {
+      if (iText + iLiteralLen > spSrcText.size() ||
+          wcsncmp(spSrcText.data() + iText, wsLiteral.c_str(), iLiteralLen)) {
         return false;
       }
       iText += iLiteralLen;
       iPattern++;
       continue;
     }
-    if (spTextFormat[iPattern] != pStrText[iText])
+    if (spTextFormat[iPattern] != spSrcText[iText])
       return false;
 
     iText++;
     iPattern++;
   }
-  return iPattern == spTextFormat.size() && iText == iLenText;
+  return iPattern == spTextFormat.size() && iText == spSrcText.size();
 }
 
 bool CFGAS_FormatString::FormatText(const WideString& wsSrcText,
                                     const WideString& wsPattern,
                                     WideString* wsOutput) const {
-  if (wsPattern.IsEmpty())
-    return false;
-
-  int32_t iLenText = wsSrcText.GetLength();
-  if (iLenText == 0)
+  if (wsPattern.IsEmpty() || wsSrcText.IsEmpty())
     return false;
 
   WideString wsTextFormat = GetTextFormat(wsPattern, L"text");
-
-  int32_t iText = 0;
-  size_t iPattern = 0;
-  const wchar_t* pStrText = wsSrcText.c_str();
+  pdfium::span<const wchar_t> spSrcText = wsSrcText.AsSpan();
   pdfium::span<const wchar_t> spTextFormat = wsTextFormat.AsSpan();
+
+  size_t iText = 0;
+  size_t iPattern = 0;
   while (iPattern < spTextFormat.size()) {
     switch (spTextFormat[iPattern]) {
       case '\'': {
@@ -1792,33 +1813,35 @@
         break;
       }
       case 'A':
-        if (iText >= iLenText || !FXSYS_iswalpha(pStrText[iText]))
+        if (iText >= spSrcText.size() || !FXSYS_iswalpha(spSrcText[iText]))
           return false;
 
-        *wsOutput += pStrText[iText++];
+        *wsOutput += spSrcText[iText++];
         iPattern++;
         break;
       case 'X':
-        if (iText >= iLenText)
+        if (iText >= spSrcText.size())
           return false;
 
-        *wsOutput += pStrText[iText++];
+        *wsOutput += spSrcText[iText++];
         iPattern++;
         break;
       case 'O':
       case '0':
-        if (iText >= iLenText || (!FXSYS_IsDecimalDigit(pStrText[iText]) &&
-                                  !FXSYS_iswalpha(pStrText[iText]))) {
+        if (iText >= spSrcText.size() ||
+            (!FXSYS_IsDecimalDigit(spSrcText[iText]) &&
+             !FXSYS_iswalpha(spSrcText[iText]))) {
           return false;
         }
-        *wsOutput += pStrText[iText++];
+        *wsOutput += spSrcText[iText++];
         iPattern++;
         break;
       case '9':
-        if (iText >= iLenText || !FXSYS_IsDecimalDigit(pStrText[iText]))
+        if (iText >= spSrcText.size() ||
+            !FXSYS_IsDecimalDigit(spSrcText[iText]))
           return false;
 
-        *wsOutput += pStrText[iText++];
+        *wsOutput += spSrcText[iText++];
         iPattern++;
         break;
       default:
@@ -1826,7 +1849,7 @@
         break;
     }
   }
-  return iText == iLenText;
+  return iText == spSrcText.size();
 }
 
 bool CFGAS_FormatString::FormatStrNum(WideStringView wsInputNum,
@@ -1923,21 +1946,20 @@
   }
 
   bool bAddNeg = false;
-  const wchar_t* str = wsSrcNum.c_str();
-  int len = wsSrcNum.GetLength();
+  pdfium::span<const wchar_t> spSrcNum = wsSrcNum.AsSpan();
   auto dot_index = wsSrcNum.Find('.');
   if (!dot_index.has_value())
-    dot_index = len;
+    dot_index = spSrcNum.size();
 
   cc = dot_index.value() - 1;
   for (int ccf = dot_index_f - 1; ccf >= 0; --ccf) {
     switch (spNumFormat[ccf]) {
       case '9':
         if (cc >= 0) {
-          if (!FXSYS_IsDecimalDigit(str[cc]))
+          if (!FXSYS_IsDecimalDigit(spSrcNum[cc]))
             return false;
 
-          wsOutput->InsertAtFront(str[cc]);
+          wsOutput->InsertAtFront(spSrcNum[cc]);
           cc--;
         } else {
           wsOutput->InsertAtFront(L'0');
@@ -1945,20 +1967,20 @@
         break;
       case 'z':
         if (cc >= 0) {
-          if (!FXSYS_IsDecimalDigit(str[cc]))
+          if (!FXSYS_IsDecimalDigit(spSrcNum[cc]))
             return false;
 
-          if (str[0] != '0')
-            wsOutput->InsertAtFront(str[cc]);
+          if (spSrcNum[0] != '0')
+            wsOutput->InsertAtFront(spSrcNum[cc]);
           cc--;
         }
         break;
       case 'Z':
         if (cc >= 0) {
-          if (!FXSYS_IsDecimalDigit(str[cc]))
+          if (!FXSYS_IsDecimalDigit(spSrcNum[cc]))
             return false;
 
-          wsOutput->InsertAtFront(str[0] == '0' ? L' ' : str[cc]);
+          wsOutput->InsertAtFront(spSrcNum[0] == '0' ? L' ' : spSrcNum[cc]);
           cc--;
         } else {
           wsOutput->InsertAtFront(L' ');
@@ -2054,9 +2076,9 @@
         *wsOutput += wsGroupSymbol;
       *wsOutput += wsSrcNum[i];
     }
-    if (pdfium::base::checked_cast<int32_t>(dot_index.value()) < len) {
+    if (dot_index.value() < spSrcNum.size()) {
       *wsOutput += pLocale->GetDecimalSymbol();
-      *wsOutput += wsSrcNum.Right(len - dot_index.value() - 1);
+      *wsOutput += wsSrcNum.Right(spSrcNum.size() - dot_index.value() - 1);
     }
     if (bNeg)
       *wsOutput = pLocale->GetMinusSymbol() + *wsOutput;
@@ -2073,11 +2095,12 @@
   if (spNumFormat[dot_index_f] == 'V') {
     *wsOutput += wsDotSymbol;
   } else if (spNumFormat[dot_index_f] == '.') {
-    if (pdfium::base::checked_cast<int32_t>(dot_index.value()) < len)
+    if (dot_index.value() < spSrcNum.size()) {
       *wsOutput += wsDotSymbol;
-    else if (spNumFormat[dot_index_f + 1] == '9' ||
-             spNumFormat[dot_index_f + 1] == 'Z')
+    } else if (spNumFormat[dot_index_f + 1] == '9' ||
+               spNumFormat[dot_index_f + 1] == 'Z') {
       *wsOutput += wsDotSymbol;
+    }
   }
 
   cc = dot_index.value() + 1;
@@ -2088,31 +2111,31 @@
         *wsOutput += GetLiteralText(spNumFormat, &ccf);
         break;
       case '9':
-        if (cc < len) {
-          if (!FXSYS_IsDecimalDigit(str[cc]))
+        if (cc < pdfium::CollectionSize<int32_t>(spSrcNum)) {
+          if (!FXSYS_IsDecimalDigit(spSrcNum[cc]))
             return false;
 
-          *wsOutput += str[cc];
+          *wsOutput += spSrcNum[cc];
           cc++;
         } else {
           *wsOutput += L'0';
         }
         break;
       case 'z':
-        if (cc < len) {
-          if (!FXSYS_IsDecimalDigit(str[cc]))
+        if (cc < pdfium::CollectionSize<int32_t>(spSrcNum)) {
+          if (!FXSYS_IsDecimalDigit(spSrcNum[cc]))
             return false;
 
-          *wsOutput += str[cc];
+          *wsOutput += spSrcNum[cc];
           cc++;
         }
         break;
       case 'Z':
-        if (cc < len) {
-          if (!FXSYS_IsDecimalDigit(str[cc]))
+        if (cc < pdfium::CollectionSize<int32_t>(spSrcNum)) {
+          if (!FXSYS_IsDecimalDigit(spSrcNum[cc]))
             return false;
 
-          *wsOutput += str[cc];
+          *wsOutput += spSrcNum[cc];
           cc++;
         } else {
           *wsOutput += L'0';
@@ -2161,8 +2184,9 @@
       case '8':
         while (ccf + 1 < spNumFormat.size() && spNumFormat[ccf + 1] == '8')
           ccf++;
-        while (cc < len && FXSYS_IsDecimalDigit(str[cc])) {
-          *wsOutput += str[cc];
+        while (cc < pdfium::CollectionSize<int32_t>(spSrcNum) &&
+               FXSYS_IsDecimalDigit(spSrcNum[cc])) {
+          *wsOutput += spSrcNum[cc];
           cc++;
         }
         break;