2008-10-01 00:50:42 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00: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/. */
|
2008-10-01 00:50:42 +00:00
|
|
|
|
2010-05-04 14:43:48 +00:00
|
|
|
#ifndef __nsCharSeparatedTokenizer_h
|
|
|
|
#define __nsCharSeparatedTokenizer_h
|
2008-10-01 00:50:42 +00:00
|
|
|
|
2013-08-23 21:07:55 +00:00
|
|
|
#include "mozilla/RangedPtr.h"
|
|
|
|
|
2008-10-01 00:50:42 +00:00
|
|
|
#include "nsDependentSubstring.h"
|
2010-07-21 16:07:10 +00:00
|
|
|
#include "nsCRT.h"
|
2008-10-01 00:50:42 +00:00
|
|
|
|
|
|
|
/**
|
2010-05-04 14:43:48 +00:00
|
|
|
* This parses a SeparatorChar-separated string into tokens.
|
|
|
|
* Whitespace surrounding tokens is not treated as part of tokens, however
|
|
|
|
* whitespace inside a token is. If the final token is the empty string, it is
|
|
|
|
* not returned.
|
2008-10-01 00:50:42 +00:00
|
|
|
*
|
2010-05-04 14:43:48 +00:00
|
|
|
* Some examples, with SeparatorChar = ',':
|
2008-10-01 00:50:42 +00:00
|
|
|
*
|
|
|
|
* "foo, bar, baz" -> "foo" "bar" "baz"
|
|
|
|
* "foo,bar,baz" -> "foo" "bar" "baz"
|
|
|
|
* "foo , bar hi , baz" -> "foo" "bar hi" "baz"
|
|
|
|
* "foo, ,bar,baz" -> "foo" "" "bar" "baz"
|
|
|
|
* "foo,,bar,baz" -> "foo" "" "bar" "baz"
|
|
|
|
* "foo,bar,baz," -> "foo" "bar" "baz"
|
2010-07-21 16:07:10 +00:00
|
|
|
*
|
|
|
|
* The function used for whitespace detection is a template argument.
|
2010-07-27 11:45:59 +00:00
|
|
|
* By default, it is NS_IsAsciiWhitespace.
|
2008-10-01 00:50:42 +00:00
|
|
|
*/
|
2014-01-04 15:02:17 +00:00
|
|
|
template<bool IsWhitespace(char16_t) = NS_IsAsciiWhitespace>
|
2010-07-21 16:07:10 +00:00
|
|
|
class nsCharSeparatedTokenizerTemplate
|
2008-10-01 00:50:42 +00:00
|
|
|
{
|
|
|
|
public:
|
2010-05-04 14:43:48 +00:00
|
|
|
// Flags -- only one for now. If we need more, they should be defined to
|
2013-09-17 12:52:39 +00:00
|
|
|
// be 1 << 1, 1 << 2, etc. (They're masks, and aFlags is a bitfield.)
|
2010-05-04 14:43:48 +00:00
|
|
|
enum {
|
|
|
|
SEPARATOR_OPTIONAL = 1
|
|
|
|
};
|
|
|
|
|
2010-07-21 16:07:10 +00:00
|
|
|
nsCharSeparatedTokenizerTemplate(const nsSubstring& aSource,
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t aSeparatorChar,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aFlags = 0)
|
2013-08-23 21:07:55 +00:00
|
|
|
: mIter(aSource.Data(), aSource.Length()),
|
|
|
|
mEnd(aSource.Data() + aSource.Length(), aSource.Data(),
|
|
|
|
aSource.Length()),
|
2010-05-04 14:43:48 +00:00
|
|
|
mSeparatorChar(aSeparatorChar),
|
2013-09-17 12:52:39 +00:00
|
|
|
mWhitespaceBeforeFirstToken(false),
|
|
|
|
mWhitespaceAfterCurrentToken(false),
|
|
|
|
mSeparatorAfterCurrentToken(false),
|
|
|
|
mSeparatorOptional(aFlags & SEPARATOR_OPTIONAL)
|
2008-10-01 00:50:42 +00:00
|
|
|
{
|
2010-05-04 14:43:48 +00:00
|
|
|
// Skip initial whitespace
|
2013-08-23 21:07:55 +00:00
|
|
|
while (mIter < mEnd && IsWhitespace(*mIter)) {
|
2013-09-17 12:52:39 +00:00
|
|
|
mWhitespaceBeforeFirstToken = true;
|
2008-10-01 00:50:42 +00:00
|
|
|
++mIter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if any more tokens are available.
|
|
|
|
*/
|
2013-09-17 12:52:39 +00:00
|
|
|
bool hasMoreTokens() const
|
2008-10-01 00:50:42 +00:00
|
|
|
{
|
2013-09-17 12:52:39 +00:00
|
|
|
MOZ_ASSERT(mIter == mEnd || !IsWhitespace(*mIter),
|
|
|
|
"Should be at beginning of token if there is one");
|
2008-10-01 00:50:42 +00:00
|
|
|
|
2013-08-23 21:07:55 +00:00
|
|
|
return mIter < mEnd;
|
2008-10-01 00:50:42 +00:00
|
|
|
}
|
|
|
|
|
2013-09-17 12:52:39 +00:00
|
|
|
/*
|
|
|
|
* Returns true if there is whitespace prior to the first token.
|
|
|
|
*/
|
|
|
|
bool whitespaceBeforeFirstToken() const
|
2011-07-23 08:41:17 +00:00
|
|
|
{
|
2013-09-17 12:52:39 +00:00
|
|
|
return mWhitespaceBeforeFirstToken;
|
2011-07-23 08:41:17 +00:00
|
|
|
}
|
|
|
|
|
2013-09-17 12:52:39 +00:00
|
|
|
/*
|
|
|
|
* Returns true if there is a separator after the current token.
|
|
|
|
* Useful if you want to check whether the last token has a separator
|
|
|
|
* after it which may not be valid.
|
|
|
|
*/
|
|
|
|
bool separatorAfterCurrentToken() const
|
2008-12-17 02:11:07 +00:00
|
|
|
{
|
2013-09-17 12:52:39 +00:00
|
|
|
return mSeparatorAfterCurrentToken;
|
2008-12-17 02:11:07 +00:00
|
|
|
}
|
|
|
|
|
2013-09-17 12:52:39 +00:00
|
|
|
/*
|
|
|
|
* Returns true if there is any whitespace after the current token.
|
|
|
|
*/
|
|
|
|
bool whitespaceAfterCurrentToken() const
|
2011-07-23 08:41:17 +00:00
|
|
|
{
|
2013-09-17 12:52:39 +00:00
|
|
|
return mWhitespaceAfterCurrentToken;
|
2011-07-23 08:41:17 +00:00
|
|
|
}
|
|
|
|
|
2008-10-01 00:50:42 +00:00
|
|
|
/**
|
|
|
|
* Returns the next token.
|
|
|
|
*/
|
|
|
|
const nsDependentSubstring nextToken()
|
|
|
|
{
|
2014-01-04 15:02:17 +00:00
|
|
|
mozilla::RangedPtr<const char16_t> tokenStart = mIter, tokenEnd = mIter;
|
2008-10-01 00:50:42 +00:00
|
|
|
|
2013-09-17 12:52:39 +00:00
|
|
|
MOZ_ASSERT(mIter == mEnd || !IsWhitespace(*mIter),
|
|
|
|
"Should be at beginning of token if there is one");
|
2008-10-01 00:50:42 +00:00
|
|
|
|
2013-09-17 12:52:39 +00:00
|
|
|
// Search until we hit separator or end (or whitespace, if a separator
|
2010-05-04 14:43:48 +00:00
|
|
|
// isn't required -- see clause with 'break' below).
|
2013-08-23 21:07:55 +00:00
|
|
|
while (mIter < mEnd && *mIter != mSeparatorChar) {
|
2013-09-17 12:52:39 +00:00
|
|
|
// Skip to end of the current word.
|
2013-08-23 21:07:55 +00:00
|
|
|
while (mIter < mEnd &&
|
2010-07-21 16:07:10 +00:00
|
|
|
!IsWhitespace(*mIter) && *mIter != mSeparatorChar) {
|
2008-10-01 00:50:42 +00:00
|
|
|
++mIter;
|
|
|
|
}
|
2013-08-23 21:07:55 +00:00
|
|
|
tokenEnd = mIter;
|
2008-10-01 00:50:42 +00:00
|
|
|
|
2013-09-17 12:52:39 +00:00
|
|
|
// Skip whitespace after the current word.
|
|
|
|
mWhitespaceAfterCurrentToken = false;
|
2013-08-23 21:07:55 +00:00
|
|
|
while (mIter < mEnd && IsWhitespace(*mIter)) {
|
2013-09-17 12:52:39 +00:00
|
|
|
mWhitespaceAfterCurrentToken = true;
|
2008-10-01 00:50:42 +00:00
|
|
|
++mIter;
|
|
|
|
}
|
2013-09-17 12:52:39 +00:00
|
|
|
if (mSeparatorOptional) {
|
2010-05-04 14:43:48 +00:00
|
|
|
// We've hit (and skipped) whitespace, and that's sufficient to end
|
|
|
|
// our token, regardless of whether we've reached a SeparatorChar.
|
|
|
|
break;
|
|
|
|
} // (else, we'll keep looping until we hit mEnd or SeparatorChar)
|
2008-10-01 00:50:42 +00:00
|
|
|
}
|
2008-12-17 02:11:07 +00:00
|
|
|
|
2013-09-17 12:52:39 +00:00
|
|
|
mSeparatorAfterCurrentToken = (mIter != mEnd &&
|
|
|
|
*mIter == mSeparatorChar);
|
|
|
|
MOZ_ASSERT(mSeparatorOptional ||
|
|
|
|
(mSeparatorAfterCurrentToken == (mIter < mEnd)),
|
|
|
|
"If we require a separator and haven't hit the end of "
|
|
|
|
"our string, then we shouldn't have left the loop "
|
|
|
|
"unless we hit a separator");
|
2010-05-04 14:43:48 +00:00
|
|
|
|
|
|
|
// Skip separator (and any whitespace after it), if we're at one.
|
2013-09-17 12:52:39 +00:00
|
|
|
if (mSeparatorAfterCurrentToken) {
|
2008-10-01 00:50:42 +00:00
|
|
|
++mIter;
|
|
|
|
|
2013-08-23 21:07:55 +00:00
|
|
|
while (mIter < mEnd && IsWhitespace(*mIter)) {
|
2013-09-17 12:52:39 +00:00
|
|
|
mWhitespaceAfterCurrentToken = true;
|
2008-10-01 00:50:42 +00:00
|
|
|
++mIter;
|
|
|
|
}
|
|
|
|
}
|
2010-05-04 14:43:48 +00:00
|
|
|
|
2013-08-23 21:07:55 +00:00
|
|
|
return Substring(tokenStart.get(), tokenEnd.get());
|
2008-10-01 00:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-01-04 15:02:17 +00:00
|
|
|
mozilla::RangedPtr<const char16_t> mIter;
|
|
|
|
const mozilla::RangedPtr<const char16_t> mEnd;
|
|
|
|
char16_t mSeparatorChar;
|
2013-09-17 12:52:39 +00:00
|
|
|
bool mWhitespaceBeforeFirstToken;
|
|
|
|
bool mWhitespaceAfterCurrentToken;
|
|
|
|
bool mSeparatorAfterCurrentToken;
|
|
|
|
bool mSeparatorOptional;
|
2010-07-21 16:07:10 +00:00
|
|
|
};
|
2008-10-01 00:50:42 +00:00
|
|
|
|
2010-07-21 16:07:10 +00:00
|
|
|
class nsCharSeparatedTokenizer: public nsCharSeparatedTokenizerTemplate<>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsCharSeparatedTokenizer(const nsSubstring& aSource,
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t aSeparatorChar,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aFlags = 0)
|
2010-07-21 16:07:10 +00:00
|
|
|
: nsCharSeparatedTokenizerTemplate<>(aSource, aSeparatorChar, aFlags)
|
2008-10-01 00:50:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
template<bool IsWhitespace(char16_t) = NS_IsAsciiWhitespace>
|
2013-09-17 12:52:39 +00:00
|
|
|
class nsCCharSeparatedTokenizerTemplate
|
2008-10-01 00:50:42 +00:00
|
|
|
{
|
|
|
|
public:
|
2013-09-17 12:52:39 +00:00
|
|
|
// Flags -- only one for now. If we need more, they should be defined to
|
|
|
|
// be 1 << 1, 1 << 2, etc. (They're masks, and aFlags is a bitfield.)
|
|
|
|
enum {
|
|
|
|
SEPARATOR_OPTIONAL = 1
|
|
|
|
};
|
|
|
|
|
|
|
|
nsCCharSeparatedTokenizerTemplate(const nsCSubstring& aSource,
|
|
|
|
char aSeparatorChar,
|
|
|
|
uint32_t aFlags = 0)
|
2013-08-23 21:07:55 +00:00
|
|
|
: mIter(aSource.Data(), aSource.Length()),
|
|
|
|
mEnd(aSource.Data() + aSource.Length(), aSource.Data(),
|
|
|
|
aSource.Length()),
|
2013-09-17 12:52:39 +00:00
|
|
|
mSeparatorChar(aSeparatorChar),
|
|
|
|
mWhitespaceBeforeFirstToken(false),
|
|
|
|
mWhitespaceAfterCurrentToken(false),
|
|
|
|
mSeparatorAfterCurrentToken(false),
|
|
|
|
mSeparatorOptional(aFlags & SEPARATOR_OPTIONAL)
|
2008-10-01 00:50:42 +00:00
|
|
|
{
|
2013-09-17 12:52:39 +00:00
|
|
|
// Skip initial whitespace
|
|
|
|
while (mIter < mEnd && IsWhitespace(*mIter)) {
|
|
|
|
mWhitespaceBeforeFirstToken = true;
|
2008-10-01 00:50:42 +00:00
|
|
|
++mIter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if any more tokens are available.
|
|
|
|
*/
|
2013-09-17 12:52:39 +00:00
|
|
|
bool hasMoreTokens() const
|
2008-10-01 00:50:42 +00:00
|
|
|
{
|
2013-09-17 12:52:39 +00:00
|
|
|
MOZ_ASSERT(mIter == mEnd || !IsWhitespace(*mIter),
|
|
|
|
"Should be at beginning of token if there is one");
|
|
|
|
|
2013-08-23 21:07:55 +00:00
|
|
|
return mIter < mEnd;
|
2008-10-01 00:50:42 +00:00
|
|
|
}
|
|
|
|
|
2013-09-17 12:52:39 +00:00
|
|
|
/*
|
|
|
|
* Returns true if there is whitespace prior to the first token.
|
|
|
|
*/
|
|
|
|
bool whitespaceBeforeFirstToken() const
|
|
|
|
{
|
|
|
|
return mWhitespaceBeforeFirstToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true if there is a separator after the current token.
|
|
|
|
* Useful if you want to check whether the last token has a separator
|
|
|
|
* after it which may not be valid.
|
|
|
|
*/
|
|
|
|
bool separatorAfterCurrentToken() const
|
|
|
|
{
|
|
|
|
return mSeparatorAfterCurrentToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true if there is any whitespace after the current token.
|
|
|
|
*/
|
|
|
|
bool whitespaceAfterCurrentToken() const
|
|
|
|
{
|
|
|
|
return mWhitespaceAfterCurrentToken;
|
|
|
|
}
|
|
|
|
|
2008-10-01 00:50:42 +00:00
|
|
|
/**
|
|
|
|
* Returns the next token.
|
|
|
|
*/
|
|
|
|
const nsDependentCSubstring nextToken()
|
|
|
|
{
|
2013-08-23 21:07:55 +00:00
|
|
|
mozilla::RangedPtr<const char> tokenStart = mIter, tokenEnd = mIter;
|
2008-10-01 00:50:42 +00:00
|
|
|
|
2013-09-17 12:52:39 +00:00
|
|
|
MOZ_ASSERT(mIter == mEnd || !IsWhitespace(*mIter),
|
|
|
|
"Should be at beginning of token if there is one");
|
|
|
|
|
|
|
|
// Search until we hit separator or end (or whitespace, if a separator
|
|
|
|
// isn't required -- see clause with 'break' below).
|
2013-08-23 21:07:55 +00:00
|
|
|
while (mIter < mEnd && *mIter != mSeparatorChar) {
|
2013-09-17 12:52:39 +00:00
|
|
|
// Skip to end of the current word.
|
2013-08-23 21:07:55 +00:00
|
|
|
while (mIter < mEnd &&
|
2013-09-17 12:52:39 +00:00
|
|
|
!IsWhitespace(*mIter) && *mIter != mSeparatorChar) {
|
2008-10-01 00:50:42 +00:00
|
|
|
++mIter;
|
|
|
|
}
|
2013-08-23 21:07:55 +00:00
|
|
|
tokenEnd = mIter;
|
2008-10-01 00:50:42 +00:00
|
|
|
|
2013-09-17 12:52:39 +00:00
|
|
|
// Skip whitespace after the current word.
|
|
|
|
mWhitespaceAfterCurrentToken = false;
|
|
|
|
while (mIter < mEnd && IsWhitespace(*mIter)) {
|
|
|
|
mWhitespaceAfterCurrentToken = true;
|
2008-10-01 00:50:42 +00:00
|
|
|
++mIter;
|
|
|
|
}
|
2013-09-17 12:52:39 +00:00
|
|
|
if (mSeparatorOptional) {
|
|
|
|
// We've hit (and skipped) whitespace, and that's sufficient to end
|
|
|
|
// our token, regardless of whether we've reached a SeparatorChar.
|
|
|
|
break;
|
|
|
|
} // (else, we'll keep looping until we hit mEnd or SeparatorChar)
|
2008-10-01 00:50:42 +00:00
|
|
|
}
|
2010-05-04 14:43:48 +00:00
|
|
|
|
2013-09-17 12:52:39 +00:00
|
|
|
mSeparatorAfterCurrentToken = (mIter != mEnd &&
|
|
|
|
*mIter == mSeparatorChar);
|
|
|
|
MOZ_ASSERT(mSeparatorOptional ||
|
|
|
|
(mSeparatorAfterCurrentToken == (mIter < mEnd)),
|
|
|
|
"If we require a separator and haven't hit the end of "
|
|
|
|
"our string, then we shouldn't have left the loop "
|
|
|
|
"unless we hit a separator");
|
|
|
|
|
|
|
|
// Skip separator (and any whitespace after it), if we're at one.
|
|
|
|
if (mSeparatorAfterCurrentToken) {
|
2008-10-01 00:50:42 +00:00
|
|
|
++mIter;
|
|
|
|
|
2013-09-17 12:52:39 +00:00
|
|
|
while (mIter < mEnd && IsWhitespace(*mIter)) {
|
|
|
|
mWhitespaceAfterCurrentToken = true;
|
2008-10-01 00:50:42 +00:00
|
|
|
++mIter;
|
|
|
|
}
|
|
|
|
}
|
2010-05-04 14:43:48 +00:00
|
|
|
|
2013-08-23 21:07:55 +00:00
|
|
|
return Substring(tokenStart.get(), tokenEnd.get());
|
2008-10-01 00:50:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-08-23 21:07:55 +00:00
|
|
|
mozilla::RangedPtr<const char> mIter;
|
|
|
|
const mozilla::RangedPtr<const char> mEnd;
|
2010-05-04 14:43:48 +00:00
|
|
|
char mSeparatorChar;
|
2013-09-17 12:52:39 +00:00
|
|
|
bool mWhitespaceBeforeFirstToken;
|
|
|
|
bool mWhitespaceAfterCurrentToken;
|
|
|
|
bool mSeparatorAfterCurrentToken;
|
|
|
|
bool mSeparatorOptional;
|
|
|
|
};
|
2008-10-01 00:50:42 +00:00
|
|
|
|
2013-09-17 12:52:39 +00:00
|
|
|
class nsCCharSeparatedTokenizer: public nsCCharSeparatedTokenizerTemplate<>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsCCharSeparatedTokenizer(const nsCSubstring& aSource,
|
|
|
|
char aSeparatorChar,
|
|
|
|
uint32_t aFlags = 0)
|
|
|
|
: nsCCharSeparatedTokenizerTemplate<>(aSource, aSeparatorChar, aFlags)
|
2008-10-01 00:50:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-05-04 14:43:48 +00:00
|
|
|
#endif /* __nsCharSeparatedTokenizer_h */
|