ParsingUtilities.h [plain text]
#pragma once
#include <wtf/text/StringCommon.h>
#include <wtf/text/StringParsingBuffer.h>
namespace WebCore {
template<typename CharacterType> inline bool isNotASCIISpace(CharacterType c)
{
return !isASCIISpace(c);
}
template<typename CharacterType, typename DelimiterType> bool skipExactly(const CharacterType*& position, const CharacterType* end, DelimiterType delimiter)
{
if (position < end && *position == delimiter) {
++position;
return true;
}
return false;
}
template<typename CharacterType, typename DelimiterType> bool skipExactly(StringParsingBuffer<CharacterType>& buffer, DelimiterType delimiter)
{
if (buffer.hasCharactersRemaining() && *buffer == delimiter) {
++buffer;
return true;
}
return false;
}
template<bool characterPredicate(LChar)> bool skipExactly(const LChar*& position, const LChar* end)
{
if (position < end && characterPredicate(*position)) {
++position;
return true;
}
return false;
}
template<bool characterPredicate(UChar)> bool skipExactly(const UChar*& position, const UChar* end)
{
if (position < end && characterPredicate(*position)) {
++position;
return true;
}
return false;
}
template<bool characterPredicate(LChar)> bool skipExactly(StringParsingBuffer<LChar>& buffer)
{
if (buffer.hasCharactersRemaining() && characterPredicate(*buffer)) {
++buffer;
return true;
}
return false;
}
template<bool characterPredicate(UChar)> bool skipExactly(StringParsingBuffer<UChar>& buffer)
{
if (buffer.hasCharactersRemaining() && characterPredicate(*buffer)) {
++buffer;
return true;
}
return false;
}
template<typename CharacterType, typename DelimiterType> void skipUntil(const CharacterType*& position, const CharacterType* end, DelimiterType delimiter)
{
while (position < end && *position != delimiter)
++position;
}
template<typename CharacterType, typename DelimiterType> void skipUntil(StringParsingBuffer<CharacterType>& buffer, DelimiterType delimiter)
{
while (buffer.hasCharactersRemaining() && *buffer != delimiter)
++buffer;
}
template<bool characterPredicate(LChar)> void skipUntil(const LChar*& position, const LChar* end)
{
while (position < end && !characterPredicate(*position))
++position;
}
template<bool characterPredicate(UChar)> void skipUntil(const UChar*& position, const UChar* end)
{
while (position < end && !characterPredicate(*position))
++position;
}
template<bool characterPredicate(LChar)> void skipUntil(StringParsingBuffer<LChar>& buffer)
{
while (buffer.hasCharactersRemaining() && !characterPredicate(*buffer))
++buffer;
}
template<bool characterPredicate(UChar)> void skipUntil(StringParsingBuffer<UChar>& buffer)
{
while (buffer.hasCharactersRemaining() && !characterPredicate(*buffer))
++buffer;
}
template<bool characterPredicate(LChar)> void skipWhile(const LChar*& position, const LChar* end)
{
while (position < end && characterPredicate(*position))
++position;
}
template<bool characterPredicate(UChar)> void skipWhile(const UChar*& position, const UChar* end)
{
while (position < end && characterPredicate(*position))
++position;
}
template<bool characterPredicate(LChar)> void skipWhile(StringParsingBuffer<LChar>& buffer)
{
while (buffer.hasCharactersRemaining() && characterPredicate(*buffer))
++buffer;
}
template<bool characterPredicate(UChar)> void skipWhile(StringParsingBuffer<UChar>& buffer)
{
while (buffer.hasCharactersRemaining() && characterPredicate(*buffer))
++buffer;
}
template<bool characterPredicate(LChar)> void reverseSkipWhile(const LChar*& position, const LChar* start)
{
while (position >= start && characterPredicate(*position))
--position;
}
template<bool characterPredicate(UChar)> void reverseSkipWhile(const UChar*& position, const UChar* start)
{
while (position >= start && characterPredicate(*position))
--position;
}
template<typename CharacterType, unsigned lowercaseLettersArraySize> bool skipExactlyIgnoringASCIICase(const CharacterType*& position, const CharacterType* end, const char (&lowercaseLetters)[lowercaseLettersArraySize])
{
constexpr auto lowercaseLettersLength = lowercaseLettersArraySize - 1;
if (position + lowercaseLettersLength > end)
return false;
if (!WTF::equalLettersIgnoringASCIICase(position, lowercaseLettersLength, lowercaseLetters))
return false;
position += lowercaseLettersLength;
return true;
}
template<typename CharacterType, unsigned lowercaseLettersArraySize> bool skipExactlyIgnoringASCIICase(StringParsingBuffer<CharacterType>& buffer, const char (&lowercaseLetters)[lowercaseLettersArraySize])
{
constexpr auto lowercaseLettersLength = lowercaseLettersArraySize - 1;
if (buffer.lengthRemaining() < lowercaseLettersLength)
return false;
if (!WTF::equalLettersIgnoringASCIICase(buffer.position(), lowercaseLettersLength, lowercaseLetters))
return false;
buffer += lowercaseLettersLength;
return true;
}
template<typename CharacterType, unsigned characterCount> constexpr bool skipCharactersExactly(const CharacterType*& ptr, const CharacterType* end, const CharacterType(&str)[characterCount])
{
if (end - ptr < characterCount)
return false;
if (memcmp(str, ptr, sizeof(CharacterType) * characterCount))
return false;
ptr += characterCount;
return true;
}
template<typename CharacterType, unsigned characterCount> constexpr bool skipCharactersExactly(StringParsingBuffer<CharacterType>& buffer, const CharacterType(&str)[characterCount])
{
if (buffer.lengthRemaining() < characterCount)
return false;
if (memcmp(str, buffer.position(), sizeof(CharacterType) * characterCount))
return false;
buffer += characterCount;
return true;
}
}