michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef __nsWhitespaceTokenizer_h michael@0: #define __nsWhitespaceTokenizer_h michael@0: michael@0: #include "mozilla/RangedPtr.h" michael@0: #include "nsDependentSubstring.h" michael@0: #include "nsCRT.h" michael@0: michael@0: template michael@0: class nsWhitespaceTokenizerTemplate michael@0: { michael@0: public: michael@0: nsWhitespaceTokenizerTemplate(const nsSubstring& aSource) michael@0: : mIter(aSource.Data(), aSource.Length()), michael@0: mEnd(aSource.Data() + aSource.Length(), aSource.Data(), michael@0: aSource.Length()), michael@0: mWhitespaceBeforeFirstToken(false), michael@0: mWhitespaceAfterCurrentToken(false) michael@0: { michael@0: while (mIter < mEnd && IsWhitespace(*mIter)) { michael@0: mWhitespaceBeforeFirstToken = true; michael@0: ++mIter; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Checks if any more tokens are available. michael@0: */ michael@0: bool hasMoreTokens() const michael@0: { michael@0: return mIter < mEnd; michael@0: } michael@0: michael@0: /* michael@0: * Returns true if there is whitespace prior to the first token. michael@0: */ michael@0: bool whitespaceBeforeFirstToken() const michael@0: { michael@0: return mWhitespaceBeforeFirstToken; michael@0: } michael@0: michael@0: /* michael@0: * Returns true if there is any whitespace after the current token. michael@0: * This is always true unless we're reading the last token. michael@0: */ michael@0: bool whitespaceAfterCurrentToken() const michael@0: { michael@0: return mWhitespaceAfterCurrentToken; michael@0: } michael@0: michael@0: /** michael@0: * Returns the next token. michael@0: */ michael@0: const nsDependentSubstring nextToken() michael@0: { michael@0: const mozilla::RangedPtr tokenStart = mIter; michael@0: while (mIter < mEnd && !IsWhitespace(*mIter)) { michael@0: ++mIter; michael@0: } michael@0: const mozilla::RangedPtr tokenEnd = mIter; michael@0: mWhitespaceAfterCurrentToken = false; michael@0: while (mIter < mEnd && IsWhitespace(*mIter)) { michael@0: mWhitespaceAfterCurrentToken = true; michael@0: ++mIter; michael@0: } michael@0: return Substring(tokenStart.get(), tokenEnd.get()); michael@0: } michael@0: michael@0: private: michael@0: mozilla::RangedPtr mIter; michael@0: const mozilla::RangedPtr mEnd; michael@0: bool mWhitespaceBeforeFirstToken; michael@0: bool mWhitespaceAfterCurrentToken; michael@0: }; michael@0: michael@0: class nsWhitespaceTokenizer: public nsWhitespaceTokenizerTemplate<> michael@0: { michael@0: public: michael@0: nsWhitespaceTokenizer(const nsSubstring& aSource) michael@0: : nsWhitespaceTokenizerTemplate<>(aSource) michael@0: { michael@0: } michael@0: }; michael@0: michael@0: template michael@0: class nsCWhitespaceTokenizerTemplate michael@0: { michael@0: public: michael@0: nsCWhitespaceTokenizerTemplate(const nsCSubstring& aSource) michael@0: : mIter(aSource.Data(), aSource.Length()), michael@0: mEnd(aSource.Data() + aSource.Length(), aSource.Data(), michael@0: aSource.Length()), michael@0: mWhitespaceBeforeFirstToken(false), michael@0: mWhitespaceAfterCurrentToken(false) michael@0: { michael@0: while (mIter < mEnd && IsWhitespace(*mIter)) { michael@0: mWhitespaceBeforeFirstToken = true; michael@0: ++mIter; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Checks if any more tokens are available. michael@0: */ michael@0: bool hasMoreTokens() const michael@0: { michael@0: return mIter < mEnd; michael@0: } michael@0: michael@0: /* michael@0: * Returns true if there is whitespace prior to the first token. michael@0: */ michael@0: bool whitespaceBeforeFirstToken() const michael@0: { michael@0: return mWhitespaceBeforeFirstToken; michael@0: } michael@0: michael@0: /* michael@0: * Returns true if there is any whitespace after the current token. michael@0: * This is always true unless we're reading the last token. michael@0: */ michael@0: bool whitespaceAfterCurrentToken() const michael@0: { michael@0: return mWhitespaceAfterCurrentToken; michael@0: } michael@0: michael@0: /** michael@0: * Returns the next token. michael@0: */ michael@0: const nsDependentCSubstring nextToken() michael@0: { michael@0: const mozilla::RangedPtr tokenStart = mIter; michael@0: while (mIter < mEnd && !IsWhitespace(*mIter)) { michael@0: ++mIter; michael@0: } michael@0: const mozilla::RangedPtr tokenEnd = mIter; michael@0: mWhitespaceAfterCurrentToken = false; michael@0: while (mIter < mEnd && IsWhitespace(*mIter)) { michael@0: mWhitespaceAfterCurrentToken = true; michael@0: ++mIter; michael@0: } michael@0: return Substring(tokenStart.get(), tokenEnd.get()); michael@0: } michael@0: michael@0: private: michael@0: mozilla::RangedPtr mIter; michael@0: const mozilla::RangedPtr mEnd; michael@0: bool mWhitespaceBeforeFirstToken; michael@0: bool mWhitespaceAfterCurrentToken; michael@0: }; michael@0: michael@0: class nsCWhitespaceTokenizer: public nsCWhitespaceTokenizerTemplate<> michael@0: { michael@0: public: michael@0: nsCWhitespaceTokenizer(const nsCSubstring& aSource) michael@0: : nsCWhitespaceTokenizerTemplate<>(aSource) michael@0: { michael@0: } michael@0: }; michael@0: michael@0: #endif /* __nsWhitespaceTokenizer_h */