Bug 1929968 - Ensure the search unit tests use add_setup correctly. r=search-reviewer...
[gecko.git] / xpcom / ds / nsWhitespaceTokenizer.h
blob77fea708504bf791f7b9e4f10cd8965adf999c40
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;
19 public:
20 explicit nsTWhitespaceTokenizer(const SubstringType& aSource)
21 : mIter(aSource.Data(), aSource.Length()),
22 mEnd(aSource.Data() + aSource.Length(), aSource.Data(),
23 aSource.Length()),
24 mWhitespaceBeforeFirstToken(false),
25 mWhitespaceAfterCurrentToken(false) {
26 while (mIter < mEnd && IsWhitespace(*mIter)) {
27 mWhitespaceBeforeFirstToken = true;
28 ++mIter;
32 /**
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;
52 /**
53 * Returns the next token.
55 const DependentSubstringType nextToken() {
56 const mozilla::RangedPtr<const CharType> tokenStart = mIter;
57 while (mIter < mEnd && !IsWhitespace(*mIter)) {
58 ++mIter;
60 const mozilla::RangedPtr<const CharType> tokenEnd = mIter;
61 mWhitespaceAfterCurrentToken = false;
62 while (mIter < mEnd && IsWhitespace(*mIter)) {
63 mWhitespaceAfterCurrentToken = true;
64 ++mIter;
66 return Substring(tokenStart.get(), tokenEnd.get());
69 private:
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> {
79 public:
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> {
89 public:
90 explicit nsCWhitespaceTokenizerTemplate(const nsACString& aSource)
91 : nsTWhitespaceTokenizer<nsDependentCSubstring, IsWhitespace>(aSource) {}
94 typedef nsCWhitespaceTokenizerTemplate<> nsCWhitespaceTokenizer;
96 #endif /* __nsWhitespaceTokenizer_h */