This source file includes following definitions.
- m_currentTokenType
- currentTokenType
- nextTokenType
- currentElement
- lex
- isIdentifierStartCharacter
- isIdentifierCharacter
- lexIdentifier
- IDBIsValidKeyPath
- IDBParseKeyPath
- m_string
- m_array
- isValid
#include "config.h"
#include "modules/indexeddb/IDBKeyPath.h"
#include "wtf/ASCIICType.h"
#include "wtf/dtoa.h"
#include "wtf/unicode/CharacterNames.h"
#include "wtf/unicode/Unicode.h"
using namespace WTF::Unicode;
namespace WebCore {
class IDBKeyPathLexer {
public:
enum TokenType {
TokenIdentifier,
TokenDot,
TokenEnd,
TokenError
};
explicit IDBKeyPathLexer(const String& s)
: m_string(s)
, m_length(s.length())
, m_index(0)
, m_currentTokenType(TokenError)
{
}
TokenType currentTokenType() const { return m_currentTokenType; }
TokenType nextTokenType()
{
m_currentTokenType = lex(m_currentElement);
return m_currentTokenType;
}
const String& currentElement() { return m_currentElement; }
private:
TokenType lex(String&);
TokenType lexIdentifier(String&);
String m_currentElement;
const String m_string;
const unsigned m_length;
unsigned m_index;
TokenType m_currentTokenType;
};
IDBKeyPathLexer::TokenType IDBKeyPathLexer::lex(String& element)
{
if (m_index >= m_length)
return TokenEnd;
ASSERT(m_index < m_length);
if (m_string[m_index] == '.') {
++m_index;
return TokenDot;
}
return lexIdentifier(element);
}
namespace {
using namespace WTF::Unicode;
const uint32_t unicodeLetter = Letter_Uppercase | Letter_Lowercase | Letter_Titlecase | Letter_Modifier | Letter_Other | Number_Letter;
const uint32_t unicodeCombiningMark = Mark_NonSpacing | Mark_SpacingCombining;
const uint32_t unicodeDigit = Number_DecimalDigit;
const uint32_t unicodeConnectorPunctuation = Punctuation_Connector;
static inline bool isIdentifierStartCharacter(UChar c)
{
return (category(c) & unicodeLetter) || (c == '$') || (c == '_');
}
static inline bool isIdentifierCharacter(UChar c)
{
return (category(c) & (unicodeLetter | unicodeCombiningMark | unicodeDigit | unicodeConnectorPunctuation)) || (c == '$') || (c == '_') || (c == zeroWidthNonJoiner) || (c == zeroWidthJoiner);
}
}
IDBKeyPathLexer::TokenType IDBKeyPathLexer::lexIdentifier(String& element)
{
unsigned start = m_index;
if (m_index < m_length && isIdentifierStartCharacter(m_string[m_index]))
++m_index;
else
return TokenError;
while (m_index < m_length && isIdentifierCharacter(m_string[m_index]))
++m_index;
element = m_string.substring(start, m_index - start);
return TokenIdentifier;
}
bool IDBIsValidKeyPath(const String& keyPath)
{
IDBKeyPathParseError error;
Vector<String> keyPathElements;
IDBParseKeyPath(keyPath, keyPathElements, error);
return error == IDBKeyPathParseErrorNone;
}
void IDBParseKeyPath(const String& keyPath, Vector<String>& elements, IDBKeyPathParseError& error)
{
enum ParserState { Identifier, Dot, End };
IDBKeyPathLexer lexer(keyPath);
IDBKeyPathLexer::TokenType tokenType = lexer.nextTokenType();
ParserState state;
if (tokenType == IDBKeyPathLexer::TokenIdentifier)
state = Identifier;
else if (tokenType == IDBKeyPathLexer::TokenEnd)
state = End;
else {
error = IDBKeyPathParseErrorStart;
return;
}
while (1) {
switch (state) {
case Identifier : {
IDBKeyPathLexer::TokenType tokenType = lexer.currentTokenType();
ASSERT(tokenType == IDBKeyPathLexer::TokenIdentifier);
String element = lexer.currentElement();
elements.append(element);
tokenType = lexer.nextTokenType();
if (tokenType == IDBKeyPathLexer::TokenDot)
state = Dot;
else if (tokenType == IDBKeyPathLexer::TokenEnd)
state = End;
else {
error = IDBKeyPathParseErrorIdentifier;
return;
}
break;
}
case Dot: {
IDBKeyPathLexer::TokenType tokenType = lexer.currentTokenType();
ASSERT(tokenType == IDBKeyPathLexer::TokenDot);
tokenType = lexer.nextTokenType();
if (tokenType == IDBKeyPathLexer::TokenIdentifier)
state = Identifier;
else {
error = IDBKeyPathParseErrorDot;
return;
}
break;
}
case End: {
error = IDBKeyPathParseErrorNone;
return;
}
}
}
}
IDBKeyPath::IDBKeyPath(const String& string)
: m_type(StringType)
, m_string(string)
{
ASSERT(!m_string.isNull());
}
IDBKeyPath::IDBKeyPath(const Vector<String>& array)
: m_type(ArrayType)
, m_array(array)
{
#ifndef NDEBUG
for (size_t i = 0; i < m_array.size(); ++i)
ASSERT(!m_array[i].isNull());
#endif
}
bool IDBKeyPath::isValid() const
{
switch (m_type) {
case NullType:
return false;
case StringType:
return IDBIsValidKeyPath(m_string);
case ArrayType:
if (m_array.isEmpty())
return false;
for (size_t i = 0; i < m_array.size(); ++i) {
if (!IDBIsValidKeyPath(m_array[i]))
return false;
}
return true;
}
ASSERT_NOT_REACHED();
return false;
}
bool IDBKeyPath::operator==(const IDBKeyPath& other) const
{
if (m_type != other.m_type)
return false;
switch (m_type) {
case NullType:
return true;
case StringType:
return m_string == other.m_string;
case ArrayType:
return m_array == other.m_array;
}
ASSERT_NOT_REACHED();
return false;
}
}