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 mozilla_CharTokenizer_h michael@0: #define mozilla_CharTokenizer_h michael@0: michael@0: #include "nsDependentSubstring.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: template michael@0: class CharTokenizer michael@0: { michael@0: public: michael@0: CharTokenizer(const nsSubstring& aSource) michael@0: { michael@0: aSource.BeginReading(mIter); michael@0: aSource.EndReading(mEnd); michael@0: } michael@0: michael@0: /** michael@0: * Checks if any more tokens are available. michael@0: */ michael@0: bool hasMoreTokens() michael@0: { michael@0: return mIter != mEnd; michael@0: } michael@0: michael@0: /** michael@0: * Returns the next token. michael@0: */ michael@0: const nsDependentSubstring nextToken() michael@0: { michael@0: nsSubstring::const_char_iterator begin = mIter; michael@0: while (mIter != mEnd && (*mIter) != delimiter) { michael@0: ++mIter; michael@0: } michael@0: nsSubstring::const_char_iterator end = mIter; michael@0: if (mIter != mEnd) { michael@0: ++mIter; michael@0: } michael@0: michael@0: return Substring(begin, end); michael@0: } michael@0: michael@0: private: michael@0: nsSubstring::const_char_iterator mIter, mEnd; michael@0: }; michael@0: michael@0: template michael@0: class CCharTokenizer michael@0: { michael@0: public: michael@0: CCharTokenizer(const nsCSubstring& aSource) michael@0: { michael@0: aSource.BeginReading(mIter); michael@0: aSource.EndReading(mEnd); michael@0: } michael@0: michael@0: /** michael@0: * Checks if any more tokens are available. michael@0: */ michael@0: bool hasMoreTokens() michael@0: { michael@0: return mIter != mEnd; michael@0: } michael@0: michael@0: /** michael@0: * Returns the next token. michael@0: */ michael@0: const nsDependentCSubstring nextToken() michael@0: { michael@0: nsCSubstring::const_char_iterator begin = mIter; michael@0: while (mIter != mEnd && (*mIter) != delimiter) { michael@0: ++mIter; michael@0: } michael@0: nsCSubstring::const_char_iterator end = mIter; michael@0: if (mIter != mEnd) { michael@0: ++mIter; michael@0: } michael@0: michael@0: return Substring(begin, end); michael@0: } michael@0: michael@0: private: michael@0: nsCSubstring::const_char_iterator mIter, mEnd; michael@0: }; michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif /* mozilla_CharTokenizer_h */