Bug 1418847 - Move some constants into class Parser. r=glandium
authorNicholas Nethercote <nnethercote@mozilla.com>
Sun, 19 Nov 2017 20:10:39 +1100
changeset 437527 bb5faea101fb54cd2dfbbe7c723625ef02c03833
parent 437526 6e8b3b78375e136de9636d63011887a4200b0d68
child 437528 b38851147aa63233a4987247476f9ae544cfdbc5
push id117
push userfmarier@mozilla.com
push dateTue, 28 Nov 2017 20:17:16 +0000
reviewersglandium
bugs1418847
milestone59.0a1
Bug 1418847 - Move some constants into class Parser. r=glandium MozReview-Commit-ID: 1eZiyrUWjFp
modules/libpref/Preferences.cpp
--- a/modules/libpref/Preferences.cpp
+++ b/modules/libpref/Preferences.cpp
@@ -1127,16 +1127,26 @@ private:
     eHexEscape,
     eUTF16LowSurrogate,
     eUntilOpenParen,
     eUntilCloseParen,
     eUntilSemicolon,
     eUntilEOL
   };
 
+  static const int kUTF16EscapeNumDigits = 4;
+  static const int kHexEscapeNumDigits = 2;
+  static const int KBitsPerHexDigit = 4;
+
+  static constexpr const char* kUserPref = "user_pref";
+  static constexpr const char* kPref = "pref";
+  static constexpr const char* kStickyPref = "sticky_pref";
+  static constexpr const char* kTrue = "true";
+  static constexpr const char* kFalse = "false";
+
   PrefReader mReader;           // called for each preference
   void* mClosure;               // closure data for mReader
   ParseErrorReporter mReporter; // called for warnings/errors
   State mState;                 // current parse state
   State mNextState;             // sometimes used...
   const char* mStrMatch;        // string to match
   int mStrIndex;                // next char of smatch to check;
                                 // also, counter in \u parsing
@@ -1148,26 +1158,16 @@ private:
   char* mLbCur;                 // line buffer cursor
   char* mLbEnd;                 // line buffer end
   char* mVb;                    // value buffer (ptr into mLb)
   Maybe<PrefType> mVtype;       // pref value type
   bool mIsDefault;              // true if (default) pref
   bool mIsSticky;               // true if (sticky) pref
 };
 
-#define UTF16_ESC_NUM_DIGITS 4
-#define HEX_ESC_NUM_DIGITS 2
-#define BITS_PER_HEX_DIGIT 4
-
-static const char kUserPref[] = "user_pref";
-static const char kPref[] = "pref";
-static const char kStickyPref[] = "sticky_pref";
-static const char kTrue[] = "true";
-static const char kFalse[] = "false";
-
 // This function will increase the size of the buffer owned by the given pref
 // parse state. We currently use a simple doubling algorithm, but the only hard
 // requirement is that it increase the buffer by at least the size of the
 // mEscTmp buffer used for escape processing (currently 6 bytes).
 //
 // The buffer is used to store partial pref lines. It is freed when the parse
 // state is destroyed.
 //
@@ -1472,17 +1472,18 @@ Parser::Parse(const char* aBuf, int aBuf
           case 'n':
             c = '\n';
             break;
           case 'x': // hex escape -- always interpreted as Latin-1
           case 'u': // UTF16 escape
             mEscTmp[0] = c;
             mEscLen = 1;
             mUtf16[0] = mUtf16[1] = 0;
-            mStrIndex = (c == 'x') ? HEX_ESC_NUM_DIGITS : UTF16_ESC_NUM_DIGITS;
+            mStrIndex =
+              (c == 'x') ? kHexEscapeNumDigits : kUTF16EscapeNumDigits;
             state = State::eHexEscape;
             continue;
           default:
             ReportProblem(
               "preserving unexpected JS escape sequence", lineNum, false);
             NS_WARNING("preserving unexpected JS escape sequence");
             // Invalid escape sequence so we do have to write more than one
             // character. Grow line buffer if necessary...
@@ -1521,17 +1522,17 @@ Parser::Parse(const char* aBuf, int aBuf
           // of the loop keeps this safe.)
           --aBuf;
           state = State::eQuotedString;
           continue;
         }
 
         // have a digit
         mEscTmp[mEscLen++] = c; // preserve it
-        mUtf16[1] <<= BITS_PER_HEX_DIGIT;
+        mUtf16[1] <<= KBitsPerHexDigit;
         mUtf16[1] |= udigit;
         mStrIndex--;
         if (mStrIndex == 0) {
           // we have the full escape, convert to UTF8
           int utf16len = 0;
           if (mUtf16[0]) {
             // already have a high surrogate, this is a two char seq
             utf16len = 2;
@@ -1562,17 +1563,17 @@ Parser::Parse(const char* aBuf, int aBuf
         break;
 
       // looking for beginning of mUtf16 low surrogate
       case State::eUTF16LowSurrogate:
         if (mStrIndex == 0 && c == '\\') {
           ++mStrIndex;
         } else if (mStrIndex == 1 && c == 'u') {
           // escape sequence is correct, now parse hex
-          mStrIndex = UTF16_ESC_NUM_DIGITS;
+          mStrIndex = kUTF16EscapeNumDigits;
           mEscTmp[0] = 'u';
           mEscLen = 1;
           state = State::eHexEscape;
         } else {
           // Didn't find expected low surrogate. Ignore high surrogate (it
           // would just get converted to nothing anyway) and start over with
           // this character.
           --aBuf;