2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-26 15:20:29 -07:00
|
|
|
#ifndef __nsWhitespaceTokenizer_h
|
|
|
|
#define __nsWhitespaceTokenizer_h
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-08-16 06:18:13 -07:00
|
|
|
#include "mozilla/RangedPtr.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsDependentSubstring.h"
|
|
|
|
|
2007-07-26 15:20:29 -07:00
|
|
|
class nsWhitespaceTokenizer
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2007-07-26 15:20:29 -07:00
|
|
|
nsWhitespaceTokenizer(const nsSubstring& aSource)
|
2013-08-16 06:18:13 -07:00
|
|
|
: mIter(aSource.Data(), aSource.Length()),
|
|
|
|
mEnd(aSource.Data() + aSource.Length(), aSource.Data(),
|
|
|
|
aSource.Length())
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-08-16 06:18:13 -07:00
|
|
|
while (mIter < mEnd && isWhitespace(*mIter)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
++mIter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if any more tokens are available.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasMoreTokens()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-08-16 06:18:13 -07:00
|
|
|
return mIter < mEnd;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the next token.
|
|
|
|
*/
|
|
|
|
const nsDependentSubstring nextToken()
|
|
|
|
{
|
2013-08-16 06:18:13 -07:00
|
|
|
const mozilla::RangedPtr<const PRUnichar> tokenStart = mIter;
|
|
|
|
while (mIter < mEnd && !isWhitespace(*mIter)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
++mIter;
|
|
|
|
}
|
2013-08-16 06:18:13 -07:00
|
|
|
const mozilla::RangedPtr<const PRUnichar> tokenEnd = mIter;
|
|
|
|
while (mIter < mEnd && isWhitespace(*mIter)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
++mIter;
|
|
|
|
}
|
2013-08-16 06:18:13 -07:00
|
|
|
return Substring(tokenStart.get(), tokenEnd.get());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-08-16 06:18:13 -07:00
|
|
|
mozilla::RangedPtr<const PRUnichar> mIter;
|
|
|
|
const mozilla::RangedPtr<const PRUnichar> mEnd;
|
2007-07-26 15:20:29 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isWhitespace(PRUnichar aChar)
|
2007-07-26 15:20:29 -07:00
|
|
|
{
|
|
|
|
return aChar <= ' ' &&
|
|
|
|
(aChar == ' ' || aChar == '\n' ||
|
|
|
|
aChar == '\r'|| aChar == '\t');
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2007-07-26 15:20:29 -07:00
|
|
|
class nsCWhitespaceTokenizer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsCWhitespaceTokenizer(const nsCSubstring& aSource)
|
2013-08-16 06:18:13 -07:00
|
|
|
: mIter(aSource.Data(), aSource.Length()),
|
|
|
|
mEnd(aSource.Data() + aSource.Length(), aSource.Data(),
|
|
|
|
aSource.Length())
|
2007-07-26 15:20:29 -07:00
|
|
|
{
|
2013-08-16 06:18:13 -07:00
|
|
|
while (mIter < mEnd && isWhitespace(*mIter)) {
|
2007-07-26 15:20:29 -07:00
|
|
|
++mIter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if any more tokens are available.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasMoreTokens()
|
2007-07-26 15:20:29 -07:00
|
|
|
{
|
2013-08-16 06:18:13 -07:00
|
|
|
return mIter < mEnd;
|
2007-07-26 15:20:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the next token.
|
|
|
|
*/
|
|
|
|
const nsDependentCSubstring nextToken()
|
|
|
|
{
|
2013-08-16 06:18:13 -07:00
|
|
|
const mozilla::RangedPtr<const char> tokenStart = mIter;
|
|
|
|
while (mIter < mEnd && !isWhitespace(*mIter)) {
|
2007-07-26 15:20:29 -07:00
|
|
|
++mIter;
|
|
|
|
}
|
2013-08-16 06:18:13 -07:00
|
|
|
const mozilla::RangedPtr<const char> tokenEnd = mIter;
|
|
|
|
while (mIter < mEnd && isWhitespace(*mIter)) {
|
2007-07-26 15:20:29 -07:00
|
|
|
++mIter;
|
|
|
|
}
|
2013-08-16 06:18:13 -07:00
|
|
|
return Substring(tokenStart.get(), tokenEnd.get());
|
2007-07-26 15:20:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-08-16 06:18:13 -07:00
|
|
|
mozilla::RangedPtr<const char> mIter;
|
|
|
|
const mozilla::RangedPtr<const char> mEnd;
|
2007-07-26 15:20:29 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isWhitespace(char aChar)
|
2007-07-26 15:20:29 -07:00
|
|
|
{
|
|
|
|
return aChar <= ' ' &&
|
|
|
|
(aChar == ' ' || aChar == '\n' ||
|
|
|
|
aChar == '\r'|| aChar == '\t');
|
|
|
|
}
|
|
|
|
};
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-26 15:20:29 -07:00
|
|
|
#endif /* __nsWhitespaceTokenizer_h */
|