2005-11-02 10:42:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-11-02 10:39:41 +03:00
|
|
|
|
2007-07-27 02:20:29 +04:00
|
|
|
#ifndef __nsWhitespaceTokenizer_h
|
|
|
|
#define __nsWhitespaceTokenizer_h
|
2005-11-02 10:39:41 +03:00
|
|
|
|
2013-08-24 01:07:55 +04:00
|
|
|
#include "mozilla/RangedPtr.h"
|
2005-11-02 10:39:41 +03:00
|
|
|
#include "nsDependentSubstring.h"
|
2013-09-17 16:52:39 +04:00
|
|
|
#include "nsCRT.h"
|
2005-11-02 10:39:41 +03:00
|
|
|
|
2014-07-27 19:52:52 +04:00
|
|
|
template<typename DependentSubstringType, bool IsWhitespace(char16_t)>
|
2014-07-19 01:52:29 +04:00
|
|
|
class nsTWhitespaceTokenizer
|
2005-11-02 10:39:41 +03:00
|
|
|
{
|
2014-07-27 19:52:52 +04:00
|
|
|
typedef typename DependentSubstringType::char_type CharType;
|
|
|
|
typedef typename DependentSubstringType::substring_type SubstringType;
|
2014-07-19 01:52:29 +04:00
|
|
|
|
2005-11-02 10:39:41 +03:00
|
|
|
public:
|
2014-08-25 23:17:28 +04:00
|
|
|
explicit nsTWhitespaceTokenizer(const SubstringType& aSource)
|
|
|
|
: mIter(aSource.Data(), aSource.Length())
|
|
|
|
, mEnd(aSource.Data() + aSource.Length(), aSource.Data(),
|
|
|
|
aSource.Length())
|
|
|
|
, mWhitespaceBeforeFirstToken(false)
|
|
|
|
, mWhitespaceAfterCurrentToken(false)
|
|
|
|
{
|
|
|
|
while (mIter < mEnd && IsWhitespace(*mIter)) {
|
|
|
|
mWhitespaceBeforeFirstToken = true;
|
|
|
|
++mIter;
|
2005-11-02 10:39:41 +03:00
|
|
|
}
|
2014-08-25 23:17:28 +04:00
|
|
|
}
|
2005-11-02 10:39:41 +03:00
|
|
|
|
2014-08-25 23:17:28 +04:00
|
|
|
/**
|
|
|
|
* Checks if any more tokens are available.
|
|
|
|
*/
|
|
|
|
bool hasMoreTokens() const
|
|
|
|
{
|
|
|
|
return mIter < mEnd;
|
|
|
|
}
|
2005-11-02 10:39:41 +03:00
|
|
|
|
2014-08-25 23:17:28 +04:00
|
|
|
/*
|
|
|
|
* Returns true if there is whitespace prior to the first token.
|
|
|
|
*/
|
|
|
|
bool whitespaceBeforeFirstToken() const
|
|
|
|
{
|
|
|
|
return mWhitespaceBeforeFirstToken;
|
|
|
|
}
|
2013-09-17 16:52:39 +04:00
|
|
|
|
2014-08-25 23:17:28 +04:00
|
|
|
/*
|
|
|
|
* Returns true if there is any whitespace after the current token.
|
|
|
|
* This is always true unless we're reading the last token.
|
|
|
|
*/
|
|
|
|
bool whitespaceAfterCurrentToken() const
|
|
|
|
{
|
|
|
|
return mWhitespaceAfterCurrentToken;
|
|
|
|
}
|
2013-09-17 16:52:39 +04:00
|
|
|
|
2014-08-25 23:17:28 +04:00
|
|
|
/**
|
|
|
|
* Returns the next token.
|
|
|
|
*/
|
|
|
|
const DependentSubstringType nextToken()
|
|
|
|
{
|
|
|
|
const mozilla::RangedPtr<const CharType> tokenStart = mIter;
|
|
|
|
while (mIter < mEnd && !IsWhitespace(*mIter)) {
|
|
|
|
++mIter;
|
|
|
|
}
|
|
|
|
const mozilla::RangedPtr<const CharType> tokenEnd = mIter;
|
|
|
|
mWhitespaceAfterCurrentToken = false;
|
|
|
|
while (mIter < mEnd && IsWhitespace(*mIter)) {
|
|
|
|
mWhitespaceAfterCurrentToken = true;
|
|
|
|
++mIter;
|
2005-11-02 10:39:41 +03:00
|
|
|
}
|
2014-08-25 23:17:28 +04:00
|
|
|
return Substring(tokenStart.get(), tokenEnd.get());
|
|
|
|
}
|
2005-11-02 10:39:41 +03:00
|
|
|
|
|
|
|
private:
|
2014-08-25 23:17:28 +04:00
|
|
|
mozilla::RangedPtr<const CharType> mIter;
|
|
|
|
const mozilla::RangedPtr<const CharType> mEnd;
|
|
|
|
bool mWhitespaceBeforeFirstToken;
|
|
|
|
bool mWhitespaceAfterCurrentToken;
|
2013-09-17 16:52:39 +04:00
|
|
|
};
|
2007-07-27 02:20:29 +04:00
|
|
|
|
2014-07-19 01:52:29 +04:00
|
|
|
template<bool IsWhitespace(char16_t) = NS_IsAsciiWhitespace>
|
|
|
|
class nsWhitespaceTokenizerTemplate
|
2014-07-27 19:52:52 +04:00
|
|
|
: public nsTWhitespaceTokenizer<nsDependentSubstring, IsWhitespace>
|
2013-09-17 16:52:39 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-08-05 17:36:32 +04:00
|
|
|
explicit nsWhitespaceTokenizerTemplate(const nsSubstring& aSource)
|
2014-07-27 19:52:52 +04:00
|
|
|
: nsTWhitespaceTokenizer<nsDependentSubstring, IsWhitespace>(aSource)
|
2014-07-19 01:52:29 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-27 19:52:52 +04:00
|
|
|
typedef nsWhitespaceTokenizerTemplate<> nsWhitespaceTokenizer;
|
2005-11-02 10:39:41 +03:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
template<bool IsWhitespace(char16_t) = NS_IsAsciiWhitespace>
|
2013-09-17 16:52:39 +04:00
|
|
|
class nsCWhitespaceTokenizerTemplate
|
2014-07-27 19:52:52 +04:00
|
|
|
: public nsTWhitespaceTokenizer<nsDependentCSubstring, IsWhitespace>
|
2007-07-27 02:20:29 +04:00
|
|
|
{
|
|
|
|
public:
|
2014-08-05 17:36:32 +04:00
|
|
|
explicit nsCWhitespaceTokenizerTemplate(const nsCSubstring& aSource)
|
2014-07-27 19:52:52 +04:00
|
|
|
: nsTWhitespaceTokenizer<nsDependentCSubstring, IsWhitespace>(aSource)
|
2014-07-19 01:52:29 +04:00
|
|
|
{
|
|
|
|
}
|
2013-09-17 16:52:39 +04:00
|
|
|
};
|
2007-07-27 02:20:29 +04:00
|
|
|
|
2014-07-27 19:52:52 +04:00
|
|
|
typedef nsCWhitespaceTokenizerTemplate<> nsCWhitespaceTokenizer;
|
2005-11-02 10:39:41 +03:00
|
|
|
|
2007-07-27 02:20:29 +04:00
|
|
|
#endif /* __nsWhitespaceTokenizer_h */
|