Use fx_extension.h utilities in more places.

Change-Id: Iba1aa793567e69acc3cc1acbd5b9a9f531c80b7a
Reviewed-on: https://pdfium-review.googlesource.com/4453
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdftext/cpdf_textpage.cpp b/core/fpdftext/cpdf_textpage.cpp
index a1f056c..c64b8e1 100644
--- a/core/fpdftext/cpdf_textpage.cpp
+++ b/core/fpdftext/cpdf_textpage.cpp
@@ -1226,12 +1226,8 @@
   if (0x2D == wcTmp || 0xAD == wcTmp) {
     if (--nIndex > 0) {
       wchar_t preChar = strCurText.GetAt((nIndex));
-      if (((preChar >= L'A' && preChar <= L'Z') ||
-           (preChar >= L'a' && preChar <= L'z')) &&
-          ((curChar >= L'A' && curChar <= L'Z') ||
-           (curChar >= L'a' && curChar <= L'z'))) {
+      if (FXSYS_iswalpha(preChar) && FXSYS_iswalpha(curChar))
         return true;
-      }
     }
     const PAGECHAR_INFO* preInfo;
     if (!m_TempCharList.empty())
diff --git a/core/fxcrt/cfx_bytestring.cpp b/core/fxcrt/cfx_bytestring.cpp
index ab35e5d..5c53450 100644
--- a/core/fxcrt/cfx_bytestring.cpp
+++ b/core/fxcrt/cfx_bytestring.cpp
@@ -13,6 +13,7 @@
 
 #include "core/fxcrt/cfx_string_pool_template.h"
 #include "core/fxcrt/fx_basic.h"
+#include "core/fxcrt/fx_extension.h"
 #include "core/fxcrt/fx_safe_types.h"
 #include "third_party/base/numerics/safe_math.h"
 #include "third_party/base/stl_util.h"
@@ -266,14 +267,8 @@
   const uint8_t* pThat = str.raw_str();
   for (FX_STRSIZE i = 0; i < len; i++) {
     if ((*pThis) != (*pThat)) {
-      uint8_t bThis = *pThis;
-      if (bThis >= 'A' && bThis <= 'Z')
-        bThis += 'a' - 'A';
-
-      uint8_t bThat = *pThat;
-      if (bThat >= 'A' && bThat <= 'Z')
-        bThat += 'a' - 'A';
-
+      uint8_t bThis = FXSYS_tolower(*pThis);
+      uint8_t bThat = FXSYS_tolower(*pThat);
       if (bThis != bThat)
         return false;
     }
diff --git a/core/fxcrt/cfx_bytestring_unittest.cpp b/core/fxcrt/cfx_bytestring_unittest.cpp
index 42627c2..14d9393 100644
--- a/core/fxcrt/cfx_bytestring_unittest.cpp
+++ b/core/fxcrt/cfx_bytestring_unittest.cpp
@@ -1239,3 +1239,16 @@
   EXPECT_TRUE(pdfium::ContainsValue(str, 'b'));
   EXPECT_FALSE(pdfium::ContainsValue(str, 'z'));
 }
+
+TEST(fxcrt, EqualNoCase) {
+  CFX_ByteString str("aaa");
+  EXPECT_TRUE(str.EqualNoCase("aaa"));
+  EXPECT_TRUE(str.EqualNoCase("AAA"));
+  EXPECT_TRUE(str.EqualNoCase("aaA"));
+  EXPECT_TRUE(str.EqualNoCase("Aaa"));
+  EXPECT_FALSE(str.EqualNoCase("aab"));
+  EXPECT_FALSE(str.EqualNoCase("aaaa"));
+  EXPECT_FALSE(str.EqualNoCase("BBBB"));
+  EXPECT_FALSE(str.EqualNoCase("a"));
+  EXPECT_FALSE(str.EqualNoCase(""));
+}
diff --git a/core/fxcrt/cfx_decimal.cpp b/core/fxcrt/cfx_decimal.cpp
index 095978c..a463305 100644
--- a/core/fxcrt/cfx_decimal.cpp
+++ b/core/fxcrt/cfx_decimal.cpp
@@ -9,6 +9,8 @@
 #include <algorithm>
 #include <utility>
 
+#include "core/fxcrt/fx_extension.h"
+
 #define FXMATH_DECIMAL_SCALELIMIT 0x1c
 #define FXMATH_DECIMAL_NEGMASK (0x80000000L)
 #define FXMATH_DECIMAL_FORCEBOOL(x) (!!(x))
@@ -310,7 +312,7 @@
     str++;
   }
 
-  while (str != strBound && ((*str >= '0' && *str <= '9') || *str == '.') &&
+  while (str != strBound && (std::iswdigit(*str) || *str == '.') &&
          scale < FXMATH_DECIMAL_SCALELIMIT) {
     if (*str == '.') {
       if (!pointmet)
diff --git a/core/fxcrt/fx_basic_gcc.cpp b/core/fxcrt/fx_basic_gcc.cpp
index c42b762..ce1f813 100644
--- a/core/fxcrt/fx_basic_gcc.cpp
+++ b/core/fxcrt/fx_basic_gcc.cpp
@@ -183,30 +183,31 @@
   }
   return s;
 }
+
 int FXSYS_stricmp(const char* dst, const char* src) {
-  int f, l;
+  int f;
+  int l;
   do {
-    if (((f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z')) {
-      f -= ('A' - 'a');
-    }
-    if (((l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z')) {
-      l -= ('A' - 'a');
-    }
-  } while (f && (f == l));
-  return (f - l);
+    f = FXSYS_toupper(*dst);
+    l = FXSYS_toupper(*src);
+    ++dst;
+    ++src;
+  } while (f && f == l);
+  return f - l;
 }
+
 int FXSYS_wcsicmp(const wchar_t* dst, const wchar_t* src) {
-  wchar_t f, l;
+  wchar_t f;
+  wchar_t l;
   do {
-    if (((f = (wchar_t)(*(dst++))) >= 'A') && (f <= 'Z')) {
-      f -= ('A' - 'a');
-    }
-    if (((l = (wchar_t)(*(src++))) >= 'A') && (l <= 'Z')) {
-      l -= ('A' - 'a');
-    }
-  } while (f && (f == l));
-  return (f - l);
+    f = FXSYS_toupper(*dst);
+    l = FXSYS_toupper(*src);
+    ++dst;
+    ++src;
+  } while (f && f == l);
+  return f - l;
 }
+
 char* FXSYS_itoa(int value, char* str, int radix) {
   return FXSYS_IntToStr<int32_t, uint32_t, char*>(value, str, radix);
 }
diff --git a/core/fxcrt/fx_extension.h b/core/fxcrt/fx_extension.h
index 00604fb..beda105 100644
--- a/core/fxcrt/fx_extension.h
+++ b/core/fxcrt/fx_extension.h
@@ -38,7 +38,7 @@
 }
 
 inline bool FXSYS_iswalpha(wchar_t wch) {
-  return (wch >= L'A' && wch <= L'Z') || (wch >= L'a' && wch <= L'z');
+  return FXSYS_isupper(wch) || FXSYS_islower(wch);
 }
 
 inline bool FXSYS_iswalnum(wchar_t wch) {
diff --git a/fpdfsdk/fxedit/fxet_list.cpp b/fpdfsdk/fxedit/fxet_list.cpp
index c10b6a2..c8fef94 100644
--- a/fpdfsdk/fxedit/fxet_list.cpp
+++ b/fpdfsdk/fxedit/fxet_list.cpp
@@ -10,6 +10,7 @@
 #include <utility>
 
 #include "core/fpdfdoc/cpvt_word.h"
+#include "core/fxcrt/fx_extension.h"
 #include "fpdfsdk/fxedit/fxet_edit.h"
 #include "fpdfsdk/pdfwindow/PWL_ListBox.h"
 #include "third_party/base/stl_util.h"
@@ -638,12 +639,6 @@
   return -1;
 }
 
-wchar_t CFX_ListCtrl::Toupper(wchar_t c) const {
-  if ((c >= 'a') && (c <= 'z'))
-    c = c - ('a' - 'A');
-  return c;
-}
-
 int32_t CFX_ListCtrl::FindNext(int32_t nIndex, wchar_t nChar) const {
   int32_t nCircleIndex = nIndex;
   int32_t sz = pdfium::CollectionSize<int32_t>(m_ListItems);
@@ -653,7 +648,7 @@
       nCircleIndex = 0;
 
     if (CFX_ListItem* pListItem = m_ListItems[nCircleIndex].get()) {
-      if (Toupper(pListItem->GetFirstChar()) == Toupper(nChar))
+      if (FXSYS_toupper(pListItem->GetFirstChar()) == FXSYS_toupper(nChar))
         return nCircleIndex;
     }
   }
diff --git a/fpdfsdk/fxedit/fxet_list.h b/fpdfsdk/fxedit/fxet_list.h
index 50e43c9..cd6e2dd 100644
--- a/fpdfsdk/fxedit/fxet_list.h
+++ b/fpdfsdk/fxedit/fxet_list.h
@@ -262,7 +262,6 @@
   CFX_WideString GetItemText(int32_t nIndex) const;
   void SetItemSelect(int32_t nItemIndex, bool bSelected);
   int32_t GetLastSelected() const;
-  wchar_t Toupper(wchar_t c) const;
 
   CPWL_List_Notify* m_pNotify;
   bool m_bNotifyFlag;
diff --git a/fpdfsdk/javascript/util.cpp b/fpdfsdk/javascript/util.cpp
index dc34119..bc968a5 100644
--- a/fpdfsdk/javascript/util.cpp
+++ b/fpdfsdk/javascript/util.cpp
@@ -321,9 +321,9 @@
 enum CaseMode { kPreserveCase, kUpperCase, kLowerCase };
 
 static wchar_t TranslateCase(wchar_t input, CaseMode eMode) {
-  if (eMode == kLowerCase && input >= 'A' && input <= 'Z')
+  if (eMode == kLowerCase && FXSYS_isupper(input))
     return input | 0x20;
-  if (eMode == kUpperCase && input >= 'a' && input <= 'z')
+  if (eMode == kUpperCase && FXSYS_islower(input))
     return input & ~0x20;
   return input;
 }
@@ -368,9 +368,7 @@
       } break;
       case 'X': {
         if (iSourceIdx < wsSource.GetLength()) {
-          if ((wsSource[iSourceIdx] >= '0' && wsSource[iSourceIdx] <= '9') ||
-              (wsSource[iSourceIdx] >= 'a' && wsSource[iSourceIdx] <= 'z') ||
-              (wsSource[iSourceIdx] >= 'A' && wsSource[iSourceIdx] <= 'Z')) {
+          if (FXSYS_iswalnum(wsSource[iSourceIdx])) {
             wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
             ++iFormatIdx;
           }
@@ -381,8 +379,7 @@
       } break;
       case 'A': {
         if (iSourceIdx < wsSource.GetLength()) {
-          if ((wsSource[iSourceIdx] >= 'a' && wsSource[iSourceIdx] <= 'z') ||
-              (wsSource[iSourceIdx] >= 'A' && wsSource[iSourceIdx] <= 'Z')) {
+          if (FXSYS_iswalpha(wsSource[iSourceIdx])) {
             wsResult += TranslateCase(wsSource[iSourceIdx], eCaseMode);
             ++iFormatIdx;
           }
@@ -393,7 +390,7 @@
       } break;
       case '9': {
         if (iSourceIdx < wsSource.GetLength()) {
-          if (wsSource[iSourceIdx] >= '0' && wsSource[iSourceIdx] <= '9') {
+          if (std::iswdigit(wsSource[iSourceIdx])) {
             wsResult += wsSource[iSourceIdx];
             ++iFormatIdx;
           }