1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef __nsWhitespaceTokenizer_h
8 #define __nsWhitespaceTokenizer_h
10 #include "mozilla/RangedPtr.h"
11 #include "nsDependentSubstring.h"
12 #include "nsCRTGlue.h"
14 template <typename DependentSubstringType
, bool IsWhitespace(char16_t
)>
15 class nsTWhitespaceTokenizer
{
16 typedef typename
DependentSubstringType::char_type CharType
;
17 typedef typename
DependentSubstringType::substring_type SubstringType
;
20 explicit nsTWhitespaceTokenizer(const SubstringType
& aSource
)
21 : mIter(aSource
.Data(), aSource
.Length()),
22 mEnd(aSource
.Data() + aSource
.Length(), aSource
.Data(),
24 mWhitespaceBeforeFirstToken(false),
25 mWhitespaceAfterCurrentToken(false) {
26 while (mIter
< mEnd
&& IsWhitespace(*mIter
)) {
27 mWhitespaceBeforeFirstToken
= true;
33 * Checks if any more tokens are available.
35 bool hasMoreTokens() const { return mIter
< mEnd
; }
38 * Returns true if there is whitespace prior to the first token.
40 bool whitespaceBeforeFirstToken() const {
41 return mWhitespaceBeforeFirstToken
;
45 * Returns true if there is any whitespace after the current token.
46 * This is always true unless we're reading the last token.
48 bool whitespaceAfterCurrentToken() const {
49 return mWhitespaceAfterCurrentToken
;
53 * Returns the next token.
55 const DependentSubstringType
nextToken() {
56 const mozilla::RangedPtr
<const CharType
> tokenStart
= mIter
;
57 while (mIter
< mEnd
&& !IsWhitespace(*mIter
)) {
60 const mozilla::RangedPtr
<const CharType
> tokenEnd
= mIter
;
61 mWhitespaceAfterCurrentToken
= false;
62 while (mIter
< mEnd
&& IsWhitespace(*mIter
)) {
63 mWhitespaceAfterCurrentToken
= true;
66 return Substring(tokenStart
.get(), tokenEnd
.get());
70 mozilla::RangedPtr
<const CharType
> mIter
;
71 const mozilla::RangedPtr
<const CharType
> mEnd
;
72 bool mWhitespaceBeforeFirstToken
;
73 bool mWhitespaceAfterCurrentToken
;
76 template <bool IsWhitespace(char16_t
) = NS_IsAsciiWhitespace
>
77 class nsWhitespaceTokenizerTemplate
78 : public nsTWhitespaceTokenizer
<nsDependentSubstring
, IsWhitespace
> {
80 explicit nsWhitespaceTokenizerTemplate(const nsAString
& aSource
)
81 : nsTWhitespaceTokenizer
<nsDependentSubstring
, IsWhitespace
>(aSource
) {}
84 typedef nsWhitespaceTokenizerTemplate
<> nsWhitespaceTokenizer
;
86 template <bool IsWhitespace(char16_t
) = NS_IsAsciiWhitespace
>
87 class nsCWhitespaceTokenizerTemplate
88 : public nsTWhitespaceTokenizer
<nsDependentCSubstring
, IsWhitespace
> {
90 explicit nsCWhitespaceTokenizerTemplate(const nsACString
& aSource
)
91 : nsTWhitespaceTokenizer
<nsDependentCSubstring
, IsWhitespace
>(aSource
) {}
94 typedef nsCWhitespaceTokenizerTemplate
<> nsCWhitespaceTokenizer
;
96 #endif /* __nsWhitespaceTokenizer_h */