Use span<>/size_t in GetNumTrailingLimit().

- convert some pFoo variables to spFoo where Foo is a span<>.
- rename some gs_ variables to k-constant names as well.
- check negative earlier in ParseTimeZone() so optimizer can
  remove implicit bounds check later on.

Change-Id: I4eaa222e870dc31480abb99a14faaf7c9a5d9ee8
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/51990
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/xfa/fgas/crt/cfgas_stringformatter.cpp b/xfa/fgas/crt/cfgas_stringformatter.cpp
index d3da5ee..60b8c46 100644
--- a/xfa/fgas/crt/cfgas_stringformatter.cpp
+++ b/xfa/fgas/crt/cfgas_stringformatter.cpp
@@ -74,29 +74,32 @@
     {L"MDT", -6, 0}, {L"MST", -7, 0}, {L"PDT", -7, 0}, {L"PST", -8, 0},
 };
 
-const wchar_t gs_wsTimeSymbols[] = L"hHkKMSFAzZ";
-const wchar_t gs_wsDateSymbols[] = L"DJMEeGgYwW";
-const wchar_t gs_wsConstChars[] = L",-:/. ";
+const wchar_t kTimeSymbols[] = L"hHkKMSFAzZ";
+const wchar_t kDateSymbols[] = L"DJMEeGgYwW";
+const wchar_t kConstChars[] = L",-:/. ";
 
-size_t ParseTimeZone(pdfium::span<const wchar_t> pStr, FX_TIMEZONE* tz) {
+size_t ParseTimeZone(pdfium::span<const wchar_t> spStr, FX_TIMEZONE* tz) {
   tz->tzHour = 0;
   tz->tzMinute = 0;
-  if (pStr.empty())
+  if (spStr.empty())
     return 0;
 
+  // Keep index by 0 close to empty() check above for optimizer's sake.
+  const bool bNegative = (spStr[0] == '-');
+
   size_t iStart = 1;
   size_t iEnd = iStart + 2;
-  while (iStart < pStr.size() && iStart < iEnd)
-    tz->tzHour = tz->tzHour * 10 + FXSYS_DecimalCharToInt(pStr[iStart++]);
+  while (iStart < spStr.size() && iStart < iEnd)
+    tz->tzHour = tz->tzHour * 10 + FXSYS_DecimalCharToInt(spStr[iStart++]);
 
-  if (iStart < pStr.size() && pStr[iStart] == ':')
+  if (iStart < spStr.size() && spStr[iStart] == ':')
     iStart++;
 
   iEnd = iStart + 2;
-  while (iStart < pStr.size() && iStart < iEnd)
-    tz->tzMinute = tz->tzMinute * 10 + FXSYS_DecimalCharToInt(pStr[iStart++]);
+  while (iStart < spStr.size() && iStart < iEnd)
+    tz->tzMinute = tz->tzMinute * 10 + FXSYS_DecimalCharToInt(spStr[iStart++]);
 
-  if (pStr[0] == '-')
+  if (bNegative)
     tz->tzHour = -tz->tzHour;
 
   return iStart;
@@ -108,30 +111,30 @@
   return iKeyValue;
 }
 
-WideString GetLiteralText(pdfium::span<const wchar_t> pStrPattern,
+WideString GetLiteralText(pdfium::span<const wchar_t> spStrPattern,
                           size_t* iPattern) {
   WideString wsOutput;
-  if (*iPattern >= pStrPattern.size() || pStrPattern[*iPattern] != '\'')
+  if (*iPattern >= spStrPattern.size() || spStrPattern[*iPattern] != '\'')
     return wsOutput;
 
   (*iPattern)++;
   int32_t iQuote = 1;
-  while (*iPattern < pStrPattern.size()) {
-    if (pStrPattern[*iPattern] == '\'') {
+  while (*iPattern < spStrPattern.size()) {
+    if (spStrPattern[*iPattern] == '\'') {
       iQuote++;
-      if ((*iPattern + 1 >= pStrPattern.size()) ||
-          ((pStrPattern[*iPattern + 1] != '\'') && (iQuote % 2 == 0))) {
+      if ((*iPattern + 1 >= spStrPattern.size()) ||
+          ((spStrPattern[*iPattern + 1] != '\'') && (iQuote % 2 == 0))) {
         break;
       }
       iQuote++;
       (*iPattern)++;
-    } else if (pStrPattern[*iPattern] == '\\' &&
-               (*iPattern + 1 < pStrPattern.size()) &&
-               pStrPattern[*iPattern + 1] == 'u') {
+    } else if (spStrPattern[*iPattern] == '\\' &&
+               (*iPattern + 1 < spStrPattern.size()) &&
+               spStrPattern[*iPattern + 1] == 'u') {
       int32_t iKeyValue = 0;
       *iPattern += 2;
-      for (int32_t i = 0; *iPattern < pStrPattern.size() && i < 4; ++i) {
-        wchar_t ch = pStrPattern[(*iPattern)++];
+      for (int32_t i = 0; *iPattern < spStrPattern.size() && i < 4; ++i) {
+        wchar_t ch = spStrPattern[(*iPattern)++];
         iKeyValue = ConvertHex(iKeyValue, ch);
       }
       if (iKeyValue != 0)
@@ -139,32 +142,32 @@
 
       continue;
     }
-    wsOutput += pStrPattern[(*iPattern)++];
+    wsOutput += spStrPattern[(*iPattern)++];
   }
   return wsOutput;
 }
 
-WideString GetLiteralTextReverse(pdfium::span<const wchar_t> pStrPattern,
+WideString GetLiteralTextReverse(pdfium::span<const wchar_t> spStrPattern,
                                  size_t* iPattern) {
   WideString wsOutput;
-  if (*iPattern >= pStrPattern.size() || pStrPattern[*iPattern] != '\'')
+  if (*iPattern >= spStrPattern.size() || spStrPattern[*iPattern] != '\'')
     return wsOutput;
 
   (*iPattern)--;
   int32_t iQuote = 1;
 
-  while (*iPattern < pStrPattern.size()) {
-    if (pStrPattern[*iPattern] == '\'') {
+  while (*iPattern < spStrPattern.size()) {
+    if (spStrPattern[*iPattern] == '\'') {
       iQuote++;
-      if (*iPattern - 1 >= pStrPattern.size() ||
-          ((pStrPattern[*iPattern - 1] != '\'') && (iQuote % 2 == 0))) {
+      if (*iPattern - 1 >= spStrPattern.size() ||
+          ((spStrPattern[*iPattern - 1] != '\'') && (iQuote % 2 == 0))) {
         break;
       }
       iQuote++;
       (*iPattern)--;
-    } else if (pStrPattern[*iPattern] == '\\' &&
-               *iPattern + 1 < pStrPattern.size() &&
-               pStrPattern[*iPattern + 1] == 'u') {
+    } else if (spStrPattern[*iPattern] == '\\' &&
+               *iPattern + 1 < spStrPattern.size() &&
+               spStrPattern[*iPattern + 1] == 'u') {
       (*iPattern)--;
       int32_t iKeyValue = 0;
       int32_t iLen = wsOutput.GetLength();
@@ -179,7 +182,7 @@
       }
       continue;
     }
-    wsOutput = pStrPattern[(*iPattern)--] + wsOutput;
+    wsOutput = spStrPattern[(*iPattern)--] + wsOutput;
   }
   return wsOutput;
 }
@@ -205,25 +208,25 @@
   return result.has_value();
 }
 
-bool ExtractCountDigits(pdfium::span<const wchar_t> str,
+bool ExtractCountDigits(pdfium::span<const wchar_t> spStr,
                         size_t count,
                         size_t* cc,
                         uint32_t* value) {
   for (size_t i = 0; i < count; ++i) {
-    if (*cc >= str.size() || !FXSYS_IsDecimalDigit(str[*cc]))
+    if (*cc >= spStr.size() || !FXSYS_IsDecimalDigit(spStr[*cc]))
       return false;
-    *value = *value * 10 + FXSYS_DecimalCharToInt(str[(*cc)++]);
+    *value = *value * 10 + FXSYS_DecimalCharToInt(spStr[(*cc)++]);
   }
   return true;
 }
 
-bool ExtractCountDigitsWithOptional(pdfium::span<const wchar_t> str,
+bool ExtractCountDigitsWithOptional(pdfium::span<const wchar_t> spStr,
                                     int count,
                                     size_t* cc,
                                     uint32_t* value) {
-  if (!ExtractCountDigits(str, count, cc, value))
+  if (!ExtractCountDigits(spStr, count, cc, value))
     return false;
-  ExtractCountDigits(str, 1, cc, value);
+  ExtractCountDigits(spStr, 1, cc, value);
   return true;
 }
 
@@ -250,7 +253,7 @@
       ccf++;
       continue;
     }
-    if (!pdfium::ContainsValue(gs_wsDateSymbols, spDatePattern[ccf])) {
+    if (!pdfium::ContainsValue(kDateSymbols, spDatePattern[ccf])) {
       if (spDatePattern[ccf] != spDate[*cc])
         return false;
       (*cc)++;
@@ -372,7 +375,7 @@
       ccf++;
       continue;
     }
-    if (!pdfium::ContainsValue(gs_wsTimeSymbols, spTimePattern[ccf])) {
+    if (!pdfium::ContainsValue(kTimeSymbols, spTimePattern[ccf])) {
       if (spTimePattern[ccf] != spTime[*cc])
         return false;
       (*cc)++;
@@ -520,10 +523,10 @@
 }
 
 uint16_t GetWeekDay(uint16_t year, uint16_t month, uint16_t day) {
-  static const uint16_t month_day[] = {0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5};
+  static const uint8_t kMonthDay[] = {0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5};
   uint16_t nDays =
       (year - 1) % 7 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400;
-  nDays += month_day[month - 1] + day;
+  nDays += kMonthDay[month - 1] + day;
   if (FX_IsLeapYear(year) && month > 2)
     nDays++;
   return nDays % 7;
@@ -574,7 +577,7 @@
       ccf++;
       continue;
     }
-    if (!pdfium::ContainsValue(gs_wsDateSymbols, spDatePattern[ccf])) {
+    if (!pdfium::ContainsValue(kDateSymbols, spDatePattern[ccf])) {
       wsResult += spDatePattern[ccf++];
       continue;
     }
@@ -642,7 +645,7 @@
       ccf++;
       continue;
     }
-    if (!pdfium::ContainsValue(gs_wsTimeSymbols, spTimePattern[ccf])) {
+    if (!pdfium::ContainsValue(kTimeSymbols, spTimePattern[ccf])) {
       wsResult += spTimePattern[ccf++];
       continue;
     }
@@ -852,7 +855,7 @@
     if (m_spPattern[ccf] == '\'') {
       GetLiteralText(m_spPattern, &ccf);
     } else if (!bBraceOpen &&
-               !pdfium::ContainsValue(gs_wsConstChars, m_spPattern[ccf])) {
+               !pdfium::ContainsValue(kConstChars, m_spPattern[ccf])) {
       WideString wsCategory(m_spPattern[ccf]);
       ccf++;
       while (true) {
@@ -907,7 +910,7 @@
       wsPurgePattern +=
           WideStringView(m_spPattern.data() + iCurChar, ccf - iCurChar + 1);
     } else if (!bBrackOpen &&
-               !pdfium::ContainsValue(gs_wsConstChars, m_spPattern[ccf])) {
+               !pdfium::ContainsValue(kConstChars, m_spPattern[ccf])) {
       WideString wsSearchCategory(m_spPattern[ccf]);
       ccf++;
       while (ccf < m_spPattern.size() && m_spPattern[ccf] != '{' &&
@@ -957,7 +960,7 @@
       *wsPurgePattern +=
           WideStringView(m_spPattern.data() + iCurChar, ccf - iCurChar + 1);
     } else if (!bBrackOpen &&
-               !pdfium::ContainsValue(gs_wsConstChars, m_spPattern[ccf])) {
+               !pdfium::ContainsValue(kConstChars, m_spPattern[ccf])) {
       WideString wsCategory(m_spPattern[ccf]);
       ccf++;
       while (ccf < m_spPattern.size() && m_spPattern[ccf] != '{' &&
@@ -1545,7 +1548,7 @@
       wsTempPattern +=
           WideStringView(m_spPattern.data() + iCurChar, ccf - iCurChar + 1);
     } else if (!bBraceOpen && iFindCategory != 3 &&
-               !pdfium::ContainsValue(gs_wsConstChars, m_spPattern[ccf])) {
+               !pdfium::ContainsValue(kConstChars, m_spPattern[ccf])) {
       WideString wsCategory(m_spPattern[ccf]);
       ccf++;
       while (ccf < m_spPattern.size() && m_spPattern[ccf] != '{' &&