diff --git a/xpcom/ds/nsCharSeparatedTokenizer.h b/xpcom/ds/nsCharSeparatedTokenizer.h index 21bb1d4ba6d..06e3a08cd0a 100644 --- a/xpcom/ds/nsCharSeparatedTokenizer.h +++ b/xpcom/ds/nsCharSeparatedTokenizer.h @@ -29,9 +29,13 @@ * The function used for whitespace detection is a template argument. * By default, it is NS_IsAsciiWhitespace. */ -template -class nsCharSeparatedTokenizerTemplate +template +class nsTCharSeparatedTokenizer { + typedef typename SubstringType::char_type CharType; + public: // Flags -- only one for now. If we need more, they should be defined to // be 1 << 1, 1 << 2, etc. (They're masks, and aFlags is a bitfield.) @@ -39,9 +43,9 @@ public: SEPARATOR_OPTIONAL = 1 }; - nsCharSeparatedTokenizerTemplate(const nsSubstring& aSource, - char16_t aSeparatorChar, - uint32_t aFlags = 0) + nsTCharSeparatedTokenizer(const SubstringType& aSource, + CharType aSeparatorChar, + uint32_t aFlags = 0) : mIter(aSource.Data(), aSource.Length()), mEnd(aSource.Data() + aSource.Length(), aSource.Data(), aSource.Length()), @@ -98,9 +102,10 @@ public: /** * Returns the next token. */ - const nsDependentSubstring nextToken() + const DependentSubstringType nextToken() { - mozilla::RangedPtr tokenStart = mIter, tokenEnd = mIter; + mozilla::RangedPtr tokenStart = mIter; + mozilla::RangedPtr tokenEnd = mIter; MOZ_ASSERT(mIter == mEnd || !IsWhitespace(*mIter), "Should be at beginning of token if there is one"); @@ -150,165 +155,71 @@ public: } private: - mozilla::RangedPtr mIter; - const mozilla::RangedPtr mEnd; - char16_t mSeparatorChar; + mozilla::RangedPtr mIter; + const mozilla::RangedPtr mEnd; + CharType mSeparatorChar; bool mWhitespaceBeforeFirstToken; bool mWhitespaceAfterCurrentToken; bool mSeparatorAfterCurrentToken; bool mSeparatorOptional; }; -class nsCharSeparatedTokenizer: public nsCharSeparatedTokenizerTemplate<> +template +class nsCharSeparatedTokenizerTemplate + : public nsTCharSeparatedTokenizer { public: - nsCharSeparatedTokenizer(const nsSubstring& aSource, - char16_t aSeparatorChar, - uint32_t aFlags = 0) - : nsCharSeparatedTokenizerTemplate<>(aSource, aSeparatorChar, aFlags) - { - } + nsCharSeparatedTokenizerTemplate(const nsSubstring& aSource, + char16_t aSeparatorChar, + uint32_t aFlags = 0) + : nsTCharSeparatedTokenizer(aSource, aSeparatorChar, aFlags) + { + } +}; + +class nsCharSeparatedTokenizer + : public nsCharSeparatedTokenizerTemplate<> +{ +public: + nsCharSeparatedTokenizer(const nsSubstring& aSource, + char16_t aSeparatorChar, + uint32_t aFlags = 0) + : nsCharSeparatedTokenizerTemplate<>(aSource, aSeparatorChar, aFlags) + { + } }; template class nsCCharSeparatedTokenizerTemplate + : public nsTCharSeparatedTokenizer { public: - // Flags -- only one for now. If we need more, they should be defined to - // be 1 << 1, 1 << 2, etc. (They're masks, and aFlags is a bitfield.) - enum { - SEPARATOR_OPTIONAL = 1 - }; - - nsCCharSeparatedTokenizerTemplate(const nsCSubstring& aSource, - char aSeparatorChar, - uint32_t aFlags = 0) - : mIter(aSource.Data(), aSource.Length()), - mEnd(aSource.Data() + aSource.Length(), aSource.Data(), - aSource.Length()), - mSeparatorChar(aSeparatorChar), - mWhitespaceBeforeFirstToken(false), - mWhitespaceAfterCurrentToken(false), - mSeparatorAfterCurrentToken(false), - mSeparatorOptional(aFlags & SEPARATOR_OPTIONAL) - { - // Skip initial whitespace - while (mIter < mEnd && IsWhitespace(*mIter)) { - mWhitespaceBeforeFirstToken = true; - ++mIter; - } - } - - /** - * Checks if any more tokens are available. - */ - bool hasMoreTokens() const - { - MOZ_ASSERT(mIter == mEnd || !IsWhitespace(*mIter), - "Should be at beginning of token if there is one"); - - return mIter < mEnd; - } - - /* - * Returns true if there is whitespace prior to the first token. - */ - bool whitespaceBeforeFirstToken() const - { - return mWhitespaceBeforeFirstToken; - } - - /* - * Returns true if there is a separator after the current token. - * Useful if you want to check whether the last token has a separator - * after it which may not be valid. - */ - bool separatorAfterCurrentToken() const - { - return mSeparatorAfterCurrentToken; - } - - /* - * Returns true if there is any whitespace after the current token. - */ - bool whitespaceAfterCurrentToken() const - { - return mWhitespaceAfterCurrentToken; - } - - /** - * Returns the next token. - */ - const nsDependentCSubstring nextToken() - { - mozilla::RangedPtr tokenStart = mIter, tokenEnd = mIter; - - MOZ_ASSERT(mIter == mEnd || !IsWhitespace(*mIter), - "Should be at beginning of token if there is one"); - - // Search until we hit separator or end (or whitespace, if a separator - // isn't required -- see clause with 'break' below). - while (mIter < mEnd && *mIter != mSeparatorChar) { - // Skip to end of the current word. - while (mIter < mEnd && - !IsWhitespace(*mIter) && *mIter != mSeparatorChar) { - ++mIter; - } - tokenEnd = mIter; - - // Skip whitespace after the current word. - mWhitespaceAfterCurrentToken = false; - while (mIter < mEnd && IsWhitespace(*mIter)) { - mWhitespaceAfterCurrentToken = true; - ++mIter; - } - if (mSeparatorOptional) { - // We've hit (and skipped) whitespace, and that's sufficient to end - // our token, regardless of whether we've reached a SeparatorChar. - break; - } // (else, we'll keep looping until we hit mEnd or SeparatorChar) - } - - mSeparatorAfterCurrentToken = (mIter != mEnd && - *mIter == mSeparatorChar); - MOZ_ASSERT(mSeparatorOptional || - (mSeparatorAfterCurrentToken == (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 (mSeparatorAfterCurrentToken) { - ++mIter; - - while (mIter < mEnd && IsWhitespace(*mIter)) { - mWhitespaceAfterCurrentToken = true; - ++mIter; - } - } - - return Substring(tokenStart.get(), tokenEnd.get()); - } - -private: - mozilla::RangedPtr mIter; - const mozilla::RangedPtr mEnd; - char mSeparatorChar; - bool mWhitespaceBeforeFirstToken; - bool mWhitespaceAfterCurrentToken; - bool mSeparatorAfterCurrentToken; - bool mSeparatorOptional; + nsCCharSeparatedTokenizerTemplate(const nsCSubstring& aSource, + char aSeparatorChar, + uint32_t aFlags = 0) + : nsTCharSeparatedTokenizer(aSource, aSeparatorChar, aFlags) + { + } }; -class nsCCharSeparatedTokenizer: public nsCCharSeparatedTokenizerTemplate<> +class nsCCharSeparatedTokenizer + : public nsCCharSeparatedTokenizerTemplate<> { public: - nsCCharSeparatedTokenizer(const nsCSubstring& aSource, - char aSeparatorChar, - uint32_t aFlags = 0) - : nsCCharSeparatedTokenizerTemplate<>(aSource, aSeparatorChar, aFlags) - { - } + nsCCharSeparatedTokenizer(const nsCSubstring& aSource, + char aSeparatorChar, + uint32_t aFlags = 0) + : nsCCharSeparatedTokenizerTemplate<>(aSource, aSeparatorChar, aFlags) + { + } }; #endif /* __nsCharSeparatedTokenizer_h */