Bug 1449082 - Make NS_IsAscii constexpr. r=froydnj
authorMasatoshi Kimura <VYV03354@nifty.ne.jp>
Tue, 27 Mar 2018 23:59:55 +0900
changeset 775158 89678976aa895c43739b2abc9e3fff0940eb4cde
parent 775157 d964f93d4fb2e549b5d267fa720e4585d54a5d0b
child 775159 577c204175529bf4f23c7736bd67552b393dcc27
push id104638
push userbmo:jlorenzo@mozilla.com
push dateFri, 30 Mar 2018 13:27:33 +0000
reviewersfroydnj
bugs1449082
milestone61.0a1
Bug 1449082 - Make NS_IsAscii constexpr. r=froydnj MozReview-Commit-ID: GbXFwCzUwgV
xpcom/base/nsCRTGlue.cpp
xpcom/base/nsCRTGlue.h
--- a/xpcom/base/nsCRTGlue.cpp
+++ b/xpcom/base/nsCRTGlue.cpp
@@ -212,22 +212,16 @@ NS_IsUpper(char aChar)
 
 bool
 NS_IsLower(char aChar)
 {
   return aChar != (char)nsLowerUpperUtils::kLower2Upper[(unsigned char)aChar];
 }
 
 bool
-NS_IsAscii(char16_t aChar)
-{
-  return (0x0080 > aChar);
-}
-
-bool
 NS_IsAscii(const char16_t* aString)
 {
   while (*aString) {
     if (0x0080 <= *aString) {
       return false;
     }
     aString++;
   }
@@ -254,38 +248,16 @@ NS_IsAscii(const char* aString, uint32_t
     if (0x80 & *aString) {
       return false;
     }
     ++aString;
   }
   return true;
 }
 
-bool
-NS_IsAsciiAlpha(char16_t aChar)
-{
-  return (aChar >= 'A' && aChar <= 'Z') ||
-         (aChar >= 'a' && aChar <= 'z');
-}
-
-bool
-NS_IsAsciiWhitespace(char16_t aChar)
-{
-  return aChar == ' ' ||
-         aChar == '\r' ||
-         aChar == '\n' ||
-         aChar == '\t';
-}
-
-bool
-NS_IsAsciiDigit(char16_t aChar)
-{
-  return aChar >= '0' && aChar <= '9';
-}
-
 #ifndef XPCOM_GLUE_AVOID_NSPR
 
 void
 NS_MakeRandomString(char* aBuf, int32_t aBufLen)
 {
 #define TABLE_SIZE 36
   static const char table[] = {
     'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
--- a/xpcom/base/nsCRTGlue.h
+++ b/xpcom/base/nsCRTGlue.h
@@ -93,24 +93,74 @@ inline char
 NS_ToLower(char aChar)
 {
   return (char)nsLowerUpperUtils::kUpper2Lower[(unsigned char)aChar];
 }
 
 bool NS_IsUpper(char aChar);
 bool NS_IsLower(char aChar);
 
-bool NS_IsAscii(char16_t aChar);
+constexpr bool
+NS_IsAscii(char16_t aChar)
+{
+  return (0x0080 > aChar);
+}
+
 bool NS_IsAscii(const char16_t* aString);
-bool NS_IsAsciiAlpha(char16_t aChar);
-bool NS_IsAsciiDigit(char16_t aChar);
-bool NS_IsAsciiWhitespace(char16_t aChar);
 bool NS_IsAscii(const char* aString);
 bool NS_IsAscii(const char* aString, uint32_t aLength);
 
+// These three functions are `constexpr` alternatives to NS_IsAscii. It should
+// only be used for compile-time computation because it uses recursion.
+// XXX: once support for GCC 4.9 is dropped, this function should be removed
+// and NS_IsAscii should be made `constexpr`.
+constexpr bool
+NS_ConstExprIsAscii(const char16_t* aString)
+{
+  return !*aString ? true :
+    !NS_IsAscii(*aString) ? false : NS_ConstExprIsAscii(aString + 1);
+}
+
+constexpr bool
+NS_ConstExprIsAscii(const char* aString)
+{
+  return !*aString ? true :
+    !NS_IsAscii(*aString) ? false : NS_ConstExprIsAscii(aString + 1);
+}
+
+constexpr bool
+NS_ConstExprIsAscii(const char* aString, uint32_t aLength)
+{
+  return aLength == 0 ? true :
+    !NS_IsAscii(*aString) ? false :
+    NS_ConstExprIsAscii(aString + 1, aLength - 1);
+}
+
+constexpr bool
+NS_IsAsciiAlpha(char16_t aChar)
+{
+  return (aChar >= 'A' && aChar <= 'Z') ||
+         (aChar >= 'a' && aChar <= 'z');
+}
+
+constexpr bool
+NS_IsAsciiWhitespace(char16_t aChar)
+{
+  return aChar == ' ' ||
+         aChar == '\r' ||
+         aChar == '\n' ||
+         aChar == '\t';
+}
+
+constexpr bool
+NS_IsAsciiDigit(char16_t aChar)
+{
+  return aChar >= '0' && aChar <= '9';
+}
+
 #ifndef XPCOM_GLUE_AVOID_NSPR
 void NS_MakeRandomString(char* aBuf, int32_t aBufLen);
 #endif
 
 #define FF '\f'
 #define TAB '\t'
 
 #define CRSTR "\015"