diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
commit | 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c (patch) | |
tree | 46dcd36c86e7fbc6e5df36deb463b33e9967a6f7 /Source/WebCore/Modules/indexeddb/IDBKeyPath.cpp | |
parent | 32761a6cee1d0dee366b885b7b9c777e67885688 (diff) | |
download | WebKitGtk-tarball-master.tar.gz |
webkitgtk-2.16.5HEADwebkitgtk-2.16.5master
Diffstat (limited to 'Source/WebCore/Modules/indexeddb/IDBKeyPath.cpp')
-rw-r--r-- | Source/WebCore/Modules/indexeddb/IDBKeyPath.cpp | 183 |
1 files changed, 63 insertions, 120 deletions
diff --git a/Source/WebCore/Modules/indexeddb/IDBKeyPath.cpp b/Source/WebCore/Modules/indexeddb/IDBKeyPath.cpp index 2b46ff81d..e76d4f9ce 100644 --- a/Source/WebCore/Modules/indexeddb/IDBKeyPath.cpp +++ b/Source/WebCore/Modules/indexeddb/IDBKeyPath.cpp @@ -1,5 +1,6 @@ /* * Copyright (C) 2010 Google Inc. All rights reserved. + * Copyright (C) 2016-2017 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -24,14 +25,13 @@ */ #include "config.h" -#include "IDBKeyPath.h" #if ENABLE(INDEXED_DATABASE) +#include "IDBKeyPath.h" -#include "KeyedCoding.h" #include <wtf/ASCIICType.h> #include <wtf/dtoa.h> -#include <wtf/unicode/Unicode.h> +#include <wtf/text/StringBuilder.h> namespace WebCore { @@ -46,8 +46,7 @@ public: explicit IDBKeyPathLexer(const String& s) : m_string(s) - , m_ptr(s.deprecatedCharacters()) - , m_end(s.deprecatedCharacters() + s.length()) + , m_remainingText(s) , m_currentTokenType(TokenError) { } @@ -65,23 +64,23 @@ public: private: TokenType lex(String&); TokenType lexIdentifier(String&); + String m_currentElement; - String m_string; - const UChar* m_ptr; - const UChar* m_end; + const String m_string; + StringView m_remainingText; TokenType m_currentTokenType; }; IDBKeyPathLexer::TokenType IDBKeyPathLexer::lex(String& element) { - if (m_ptr >= m_end) + if (m_remainingText.isEmpty()) return TokenEnd; - ASSERT_WITH_SECURITY_IMPLICATION(m_ptr < m_end); - if (*m_ptr == '.') { - ++m_ptr; + if (m_remainingText[0] == '.') { + m_remainingText = m_remainingText.substring(1); return TokenDot; } + return lexIdentifier(element); } @@ -109,16 +108,16 @@ static inline bool isIdentifierCharacter(UChar c) IDBKeyPathLexer::TokenType IDBKeyPathLexer::lexIdentifier(String& element) { - const UChar* start = m_ptr; - if (m_ptr < m_end && isIdentifierStartCharacter(*m_ptr)) - ++m_ptr; + StringView start = m_remainingText; + if (!m_remainingText.isEmpty() && isIdentifierStartCharacter(m_remainingText[0])) + m_remainingText = m_remainingText.substring(1); else return TokenError; - while (m_ptr < m_end && isIdentifierCharacter(*m_ptr)) - ++m_ptr; + while (!m_remainingText.isEmpty() && isIdentifierCharacter(m_remainingText[0])) + m_remainingText = m_remainingText.substring(1); - element = String(start, m_ptr - start); + element = start.substring(0, start.length() - m_remainingText.length()).toString(); return TokenIdentifier; } @@ -127,7 +126,7 @@ static bool IDBIsValidKeyPath(const String& keyPath) IDBKeyPathParseError error; Vector<String> keyPathElements; IDBParseKeyPath(keyPath, keyPathElements, error); - return error == IDBKeyPathParseErrorNone; + return error == IDBKeyPathParseError::None; } void IDBParseKeyPath(const String& keyPath, Vector<String>& elements, IDBKeyPathParseError& error) @@ -148,7 +147,7 @@ void IDBParseKeyPath(const String& keyPath, Vector<String>& elements, IDBKeyPath else if (tokenType == IDBKeyPathLexer::TokenEnd) state = End; else { - error = IDBKeyPathParseErrorStart; + error = IDBKeyPathParseError::Start; return; } @@ -167,7 +166,7 @@ void IDBParseKeyPath(const String& keyPath, Vector<String>& elements, IDBKeyPath else if (tokenType == IDBKeyPathLexer::TokenEnd) state = End; else { - error = IDBKeyPathParseErrorIdentifier; + error = IDBKeyPathParseError::Identifier; return; } break; @@ -180,130 +179,74 @@ void IDBParseKeyPath(const String& keyPath, Vector<String>& elements, IDBKeyPath if (tokenType == IDBKeyPathLexer::TokenIdentifier) state = Identifier; else { - error = IDBKeyPathParseErrorDot; + error = IDBKeyPathParseError::Dot; return; } break; } case End: { - error = IDBKeyPathParseErrorNone; + error = IDBKeyPathParseError::None; 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) +bool isIDBKeyPathValid(const IDBKeyPath& keyPath) { -#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()) + auto visitor = WTF::makeVisitor([](const String& string) { + return IDBIsValidKeyPath(string); + }, [](const Vector<String>& vector) { + if (vector.isEmpty()) return false; - for (size_t i = 0; i < m_array.size(); ++i) { - if (!IDBIsValidKeyPath(m_array[i])) + for (auto& key : vector) { + if (!IDBIsValidKeyPath(key)) return false; } return true; - } - ASSERT_NOT_REACHED(); - return false; + }); + return WTF::visit(visitor, keyPath); } -bool IDBKeyPath::operator==(const IDBKeyPath& other) const +IDBKeyPath isolatedCopy(const IDBKeyPath& keyPath) { - 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; + auto visitor = WTF::makeVisitor([](const String& string) -> IDBKeyPath { + return string.isolatedCopy(); + }, [](const Vector<String>& vector) -> IDBKeyPath { + Vector<String> vectorCopy; + vectorCopy.reserveInitialCapacity(vector.size()); + for (auto& string : vector) + vectorCopy.uncheckedAppend(string.isolatedCopy()); + return vectorCopy; + }); + + return WTF::visit(visitor, keyPath); } -IDBKeyPath IDBKeyPath::isolatedCopy() const -{ - IDBKeyPath result; - result.m_type = m_type; - result.m_string = m_string.isolatedCopy(); - - result.m_array.reserveInitialCapacity(m_array.size()); - for (size_t i = 0; i < m_array.size(); ++i) - result.m_array.uncheckedAppend(m_array[i].isolatedCopy()); - - return result; -} - -void IDBKeyPath::encode(KeyedEncoder& encoder) const -{ - encoder.encodeEnum("type", m_type); - switch (m_type) { - case IDBKeyPath::NullType: - break; - case IDBKeyPath::StringType: - encoder.encodeString("string", m_string); - break; - case IDBKeyPath::ArrayType: - encoder.encodeObjects("array", m_array.begin(), m_array.end(), [](WebCore::KeyedEncoder& encoder, const String& string) { - encoder.encodeString("string", string); - }); - break; - default: - ASSERT_NOT_REACHED(); - }; -} - -bool IDBKeyPath::decode(KeyedDecoder& decoder, IDBKeyPath& result) +#ifndef NDEBUG +String loggingString(const IDBKeyPath& path) { - auto enumFunction = [](int64_t value) { - return value == NullType || value == StringType || value == ArrayType; - }; - - if (!decoder.decodeVerifiedEnum("type", result.m_type, enumFunction)) - return false; - - if (result.m_type == NullType) - return true; - - if (result.m_type == StringType) - return decoder.decodeString("string", result.m_string); - - ASSERT(result.m_type == ArrayType); + auto visitor = WTF::makeVisitor([](const String& string) { + return makeString("< ", string, " >"); + }, [](const Vector<String>& strings) { + if (strings.isEmpty()) + return String("< >"); + + StringBuilder builder; + builder.append("< "); + for (size_t i = 0; i < strings.size() - 1; ++i) { + builder.append(strings[i]); + builder.append(", "); + } + builder.append(strings.last()); + builder.append(" >"); - auto arrayFunction = [](KeyedDecoder& decoder, String& result) { - return decoder.decodeString("string", result); - }; + return builder.toString(); + }); - return decoder.decodeObjects("array", result.m_array, arrayFunction); + return WTF::visit(visitor, path); } +#endif } // namespace WebCore |