tokenizer_allowSeparatorToBeOptional: use parameter instead of template to choose SeparatorChar
authorDaniel Holbert <dholbert@cs.stanford.edu>
Sun, 02 May 2010 22:29:38 -0700
changeset 1548 dbdedad4ba92918b6bc50dc593d576134da757d9
parent 1547 e5609ef14f29fe4f88c18f0447f6780ddfc6d35d
child 1549 d7586d01c70260b1a48876a0a399f9c7d89a5b49
push id1249
push userdholbert@mozilla.com
push dateMon, 03 May 2010 05:42:33 +0000
tokenizer_allowSeparatorToBeOptional: use parameter instead of template to choose SeparatorChar
tokenizer_allowSeparatorToBeOptional
--- a/tokenizer_allowSeparatorToBeOptional
+++ b/tokenizer_allowSeparatorToBeOptional
@@ -1,52 +1,53 @@
 From: Daniel Holbert <dholbert@cs.stanford.edu>
 Bug 562310 Part 3: Extend nsCharSeparatedTokenizer to allow separator to be optional. r=NOT_REVIEWED_YET
 
 diff --git a/xpcom/ds/nsCharSeparatedTokenizer.h b/xpcom/ds/nsCharSeparatedTokenizer.h
 --- a/xpcom/ds/nsCharSeparatedTokenizer.h
 +++ b/xpcom/ds/nsCharSeparatedTokenizer.h
-@@ -55,17 +55,20 @@
+@@ -55,18 +55,20 @@
   * "foo, ,bar,baz" ->      "foo" "" "bar" "baz"
   * "foo,,bar,baz" ->       "foo" "" "bar" "baz"
   * "foo,bar,baz," ->       "foo" "bar" "baz"
   */
- template<PRUnichar SeparatorChar>
  class nsCharSeparatedTokenizer
  {
  public:
--    nsCharSeparatedTokenizer(const nsSubstring& aSource)
-+    nsCharSeparatedTokenizer(const nsSubstring& aSource,
-+                             PRUnichar aIsSeparatorRequired)
+     nsCharSeparatedTokenizer(const nsSubstring& aSource,
+-                             PRUnichar aSeparatorChar)
+-        : mLastTokenEndedWithSeparator(PR_FALSE),
++                             PRUnichar aSeparatorChar,
++                             PRBool aIsSeparatorRequired)
 +        : mIsSeparatorRequired(aIsSeparatorRequired),
-+          mLastTokenEndedWithSeparator(PR_FALSE)
++          mLastTokenEndedWithSeparator(PR_FALSE),
+           mSeparatorChar(aSeparatorChar)
      {
          aSource.BeginReading(mIter);
          aSource.EndReading(mEnd);
  
          // Skip initial whitespace
          while (mIter != mEnd && isWhitespace(*mIter)) {
              ++mIter;
-         }
-@@ -92,62 +95,75 @@ public:
+@@ -94,63 +96,76 @@ public:
       */
      const nsDependentSubstring nextToken()
      {
          nsSubstring::const_char_iterator end = mIter, begin = mIter;
  
          NS_ASSERTION(mIter == mEnd || !isWhitespace(*mIter),
                       "Should be at beginning of token if there is one");
  
 -        // Search until we hit separator or end.
 +        // Search until we hit separator or end (or whitespace, if separator
-+        // isn't required -- see clause with 'break' below)
-         while (mIter != mEnd && *mIter != SeparatorChar) {
++        // isn't required -- see clause with 'break' below).
+         while (mIter != mEnd && *mIter != mSeparatorChar) {
            // Skip to end of current word.
            while (mIter != mEnd &&
-                  !isWhitespace(*mIter) && *mIter != SeparatorChar) {
+                  !isWhitespace(*mIter) && *mIter != mSeparatorChar) {
                ++mIter;
            }
            end = mIter;
  
            // Skip whitespace after current word.
            while (mIter != mEnd && isWhitespace(*mIter)) {
                ++mIter;
            }
@@ -55,55 +56,56 @@ diff --git a/xpcom/ds/nsCharSeparatedTok
 +            // our token, regardless of whether we've reached a SeparatorChar.
 +            break;
 +          } // (else, we'll keep looping until we hit mEnd or SeparatorChar)
          }
 -        mLastTokenEndedWithSeparator = mIter != mEnd;
  
 -        // Skip separator (and any whitespace after it).
 +        mLastTokenEndedWithSeparator = (mIter != mEnd &&
-+                                        *mIter == SeparatorChar);
++                                        *mIter == mSeparatorChar);
 +        NS_ASSERTION(!mIsSeparatorRequired ||
 +                     (mLastTokenEndedWithSeparator == (mIter != mEnd)),
 +                     "If we require a separator and haven't hit the end of "
 +                     "our string, then we shouldn't have left the loop "
 +                     "unless we hit a separator");
 +
 +        // Skip separator (and any whitespace after it), if we're at one.
          if (mLastTokenEndedWithSeparator) {
--            NS_ASSERTION(*mIter == SeparatorChar, "Ended loop too soon");
+-            NS_ASSERTION(*mIter == mSeparatorChar, "Ended loop too soon");
              ++mIter;
  
              while (mIter != mEnd && isWhitespace(*mIter)) {
                  ++mIter;
              }
          }
          
          return Substring(begin, end);
      }
  
  private:
      nsSubstring::const_char_iterator mIter, mEnd;
 +    PRPackedBool mIsSeparatorRequired;
      PRPackedBool mLastTokenEndedWithSeparator;
+     PRUnichar mSeparatorChar;
  
      PRBool isWhitespace(PRUnichar aChar)
      {
          return aChar <= ' ' &&
                 (aChar == ' ' || aChar == '\n' ||
                  aChar == '\r'|| aChar == '\t');
      }
  };
  
- class nsCommaSeparatedTokenizer : public nsCharSeparatedTokenizer<','>
+ class nsCommaSeparatedTokenizer : public nsCharSeparatedTokenizer
  {
  public:
      nsCommaSeparatedTokenizer(const nsSubstring& aSource)
--        : nsCharSeparatedTokenizer<','>(aSource) {}
-+        : nsCharSeparatedTokenizer<','>(aSource, PR_TRUE) {}
+-        : nsCharSeparatedTokenizer(aSource, ',') {}
++        : nsCharSeparatedTokenizer(aSource, ',', PR_TRUE) {}
  };
  
  
  class nsCCommaSeparatedTokenizer
  {
  public:
      nsCCommaSeparatedTokenizer(const nsCSubstring& aSource)
      {