Skip to content

Commit

Permalink
Bug 1035884 - Template-ify nsWhitespaceTokenizer to remove duplicate …
Browse files Browse the repository at this point in the history
…code. r=froydnj
  • Loading branch information
poiru committed Jul 18, 2014
1 parent f8536cf commit 0c6b467
Showing 1 changed file with 44 additions and 79 deletions.
123 changes: 44 additions & 79 deletions xpcom/ds/nsWhitespaceTokenizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
#include "nsDependentSubstring.h"
#include "nsCRT.h"

template<bool IsWhitespace(char16_t) = NS_IsAsciiWhitespace>
class nsWhitespaceTokenizerTemplate
template<typename SubstringType,
typename DependentSubstringType,
bool IsWhitespace(char16_t)>
class nsTWhitespaceTokenizer
{
typedef typename SubstringType::char_type CharType;

public:
nsWhitespaceTokenizerTemplate(const nsSubstring& aSource)
nsTWhitespaceTokenizer(const SubstringType& aSource)
: mIter(aSource.Data(), aSource.Length()),
mEnd(aSource.Data() + aSource.Length(), aSource.Data(),
aSource.Length()),
Expand Down Expand Up @@ -55,13 +59,13 @@ class nsWhitespaceTokenizerTemplate
/**
* Returns the next token.
*/
const nsDependentSubstring nextToken()
const DependentSubstringType nextToken()
{
const mozilla::RangedPtr<const char16_t> tokenStart = mIter;
const mozilla::RangedPtr<const CharType> tokenStart = mIter;
while (mIter < mEnd && !IsWhitespace(*mIter)) {
++mIter;
}
const mozilla::RangedPtr<const char16_t> tokenEnd = mIter;
const mozilla::RangedPtr<const CharType> tokenEnd = mIter;
mWhitespaceAfterCurrentToken = false;
while (mIter < mEnd && IsWhitespace(*mIter)) {
mWhitespaceAfterCurrentToken = true;
Expand All @@ -71,95 +75,56 @@ class nsWhitespaceTokenizerTemplate
}

private:
mozilla::RangedPtr<const char16_t> mIter;
const mozilla::RangedPtr<const char16_t> mEnd;
mozilla::RangedPtr<const CharType> mIter;
const mozilla::RangedPtr<const CharType> mEnd;
bool mWhitespaceBeforeFirstToken;
bool mWhitespaceAfterCurrentToken;
};

class nsWhitespaceTokenizer: public nsWhitespaceTokenizerTemplate<>
template<bool IsWhitespace(char16_t) = NS_IsAsciiWhitespace>
class nsWhitespaceTokenizerTemplate
: public nsTWhitespaceTokenizer<nsSubstring, nsDependentSubstring,
IsWhitespace>
{
public:
nsWhitespaceTokenizer(const nsSubstring& aSource)
: nsWhitespaceTokenizerTemplate<>(aSource)
{
}
nsWhitespaceTokenizerTemplate(const nsSubstring& aSource)
: nsTWhitespaceTokenizer<nsSubstring, nsDependentSubstring,
IsWhitespace>(aSource)
{
}
};

class nsWhitespaceTokenizer
: public nsWhitespaceTokenizerTemplate<>
{
public:
nsWhitespaceTokenizer(const nsSubstring& aSource)
: nsWhitespaceTokenizerTemplate<>(aSource)
{
}
};

template<bool IsWhitespace(char16_t) = NS_IsAsciiWhitespace>
class nsCWhitespaceTokenizerTemplate
: public nsTWhitespaceTokenizer<nsCSubstring, nsDependentCSubstring,
IsWhitespace>
{
public:
nsCWhitespaceTokenizerTemplate(const nsCSubstring& 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;
}
}

/**
* Checks if any more tokens are available.
*/
bool hasMoreTokens() const
{
return mIter < mEnd;
}

/*
* Returns true if there is whitespace prior to the first token.
*/
bool whitespaceBeforeFirstToken() const
{
return mWhitespaceBeforeFirstToken;
}

/*
* 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;
}

/**
* Returns the next token.
*/
const nsDependentCSubstring nextToken()
{
const mozilla::RangedPtr<const char> tokenStart = mIter;
while (mIter < mEnd && !IsWhitespace(*mIter)) {
++mIter;
}
const mozilla::RangedPtr<const char> tokenEnd = mIter;
mWhitespaceAfterCurrentToken = false;
while (mIter < mEnd && IsWhitespace(*mIter)) {
mWhitespaceAfterCurrentToken = true;
++mIter;
}
return Substring(tokenStart.get(), tokenEnd.get());
}

private:
mozilla::RangedPtr<const char> mIter;
const mozilla::RangedPtr<const char> mEnd;
bool mWhitespaceBeforeFirstToken;
bool mWhitespaceAfterCurrentToken;
nsCWhitespaceTokenizerTemplate(const nsCSubstring& aSource)
: nsTWhitespaceTokenizer<nsCSubstring, nsDependentCSubstring,
IsWhitespace>(aSource)
{
}
};

class nsCWhitespaceTokenizer: public nsCWhitespaceTokenizerTemplate<>
class nsCWhitespaceTokenizer
: public nsCWhitespaceTokenizerTemplate<>
{
public:
nsCWhitespaceTokenizer(const nsCSubstring& aSource)
: nsCWhitespaceTokenizerTemplate<>(aSource)
{
}
nsCWhitespaceTokenizer(const nsCSubstring& aSource)
: nsCWhitespaceTokenizerTemplate<>(aSource)
{
}
};

#endif /* __nsWhitespaceTokenizer_h */

0 comments on commit 0c6b467

Please sign in to comment.