Combine date/time format methods in CFXJSE_FormCalcContext.

Also fix C++ style errors.

Change-Id: Ia4b815c2fa8430791bb44a218ef93f8efde8c3af
Reviewed-on: https://pdfium-review.googlesource.com/39050
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
diff --git a/fxjs/cfxjse_formcalc_context.cpp b/fxjs/cfxjse_formcalc_context.cpp
index d8f7aff..5a2d581 100644
--- a/fxjs/cfxjse_formcalc_context.cpp
+++ b/fxjs/cfxjse_formcalc_context.cpp
@@ -330,21 +330,22 @@
 static_assert(FX_ArraySize(kAltTableTime) == L'a' - L'A' + 1,
               "Invalid kAltTableTime size.");
 
-void AlternateDateTimeSymbols(WideString& wsPattern,
+void AlternateDateTimeSymbols(WideString* pPattern,
                               const WideString& wsAltSymbols,
-                              const uint8_t* pAltTable) {
-  int32_t nLength = wsPattern.GetLength();
+                              bool bIsDate) {
+  const uint8_t* pAltTable = bIsDate ? kAltTableDate : kAltTableTime;
+  int32_t nLength = pPattern->GetLength();
   bool bInConstRange = false;
   bool bEscape = false;
   int32_t i = 0;
   while (i < nLength) {
-    wchar_t wc = wsPattern[i];
+    wchar_t wc = (*pPattern)[i];
     if (wc == L'\'') {
       bInConstRange = !bInConstRange;
       if (bEscape) {
         i++;
       } else {
-        wsPattern.Delete(i);
+        pPattern->Delete(i);
         nLength--;
       }
       bEscape = !bEscape;
@@ -353,7 +354,7 @@
     if (!bInConstRange && wc >= L'A' && wc <= L'a') {
       uint8_t nAlt = pAltTable[wc - L'A'];
       if (nAlt != 255)
-        wsPattern.SetAt(i, wsAltSymbols[nAlt]);
+        pPattern->SetAt(i, wsAltSymbols[nAlt]);
     }
     i++;
     bEscape = false;
@@ -456,11 +457,6 @@
   return pHostObj ? pHostObj->AsFormCalcContext() : nullptr;
 }
 
-bool IsWhitespace(char c) {
-  return c == 0x20 || c == 0x09 || c == 0x0B || c == 0x0C || c == 0x0A ||
-         c == 0x0D;
-}
-
 LocaleIface* LocaleFromString(CXFA_Document* pDoc,
                               CXFA_LocaleMgr* pMgr,
                               const ByteStringView& szLocale) {
@@ -495,6 +491,30 @@
   }
 }
 
+ByteString GetLocalDateTimeFormat(
+    CXFA_Document* pDoc,
+    int32_t iStyle,
+    const ByteStringView& szLocale,
+    bool bStandard,
+    bool bIsDate) {
+  CXFA_LocaleMgr* pMgr = pDoc->GetLocaleMgr();
+  LocaleIface* pLocale = LocaleFromString(pDoc, pMgr, szLocale);
+  if (!pLocale)
+    return ByteString();
+
+  FX_LOCALEDATETIMESUBCATEGORY category = SubCategoryFromInt(iStyle);
+  WideString strRet = bIsDate ? pLocale->GetDatePattern(category)
+                              : pLocale->GetTimePattern(category);
+  if (!bStandard)
+    AlternateDateTimeSymbols(&strRet, pLocale->GetDateTimeSymbols(), bIsDate);
+  return strRet.UTF8Encode();
+}
+
+bool IsWhitespace(char c) {
+  return c == 0x20 || c == 0x09 || c == 0x0B || c == 0x0C || c == 0x0A ||
+         c == 0x0D;
+}
+
 bool IsPartOfNumber(char ch) {
   return std::isdigit(ch) || ch == '-' || ch == '.';
 }
@@ -2127,17 +2147,8 @@
   if (!pDoc)
     return ByteString();
 
-  CXFA_LocaleMgr* pMgr = pDoc->GetLocaleMgr();
-  LocaleIface* pLocale = LocaleFromString(pDoc, pMgr, szLocale);
-  if (!pLocale)
-    return ByteString();
-
-  WideString strRet = pLocale->GetDatePattern(SubCategoryFromInt(iStyle));
-  if (!bStandard) {
-    AlternateDateTimeSymbols(strRet, pLocale->GetDateTimeSymbols(),
-                             kAltTableDate);
-  }
-  return strRet.UTF8Encode();
+  return GetLocalDateTimeFormat(pDoc, iStyle, szLocale, bStandard,
+                                /*bIsDate=*/true);
 }
 
 // static
@@ -2150,17 +2161,8 @@
   if (!pDoc)
     return ByteString();
 
-  CXFA_LocaleMgr* pMgr = pDoc->GetLocaleMgr();
-  LocaleIface* pLocale = LocaleFromString(pDoc, pMgr, szLocale);
-  if (!pLocale)
-    return ByteString();
-
-  WideString strRet = pLocale->GetTimePattern(SubCategoryFromInt(iStyle));
-  if (!bStandard) {
-    AlternateDateTimeSymbols(strRet, pLocale->GetDateTimeSymbols(),
-                             kAltTableTime);
-  }
-  return strRet.UTF8Encode();
+  return GetLocalDateTimeFormat(pDoc, iStyle, szLocale, bStandard,
+                                /*bIsDate=*/false);
 }
 
 // static