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/WTF/wtf/text/AtomicString.h | |
parent | 32761a6cee1d0dee366b885b7b9c777e67885688 (diff) | |
download | WebKitGtk-tarball-master.tar.gz |
webkitgtk-2.16.5HEADwebkitgtk-2.16.5master
Diffstat (limited to 'Source/WTF/wtf/text/AtomicString.h')
-rw-r--r-- | Source/WTF/wtf/text/AtomicString.h | 250 |
1 files changed, 177 insertions, 73 deletions
diff --git a/Source/WTF/wtf/text/AtomicString.h b/Source/WTF/wtf/text/AtomicString.h index 4142de142..91bb20a8b 100644 --- a/Source/WTF/wtf/text/AtomicString.h +++ b/Source/WTF/wtf/text/AtomicString.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved. + * Copyright (C) 2004-2006, 2008, 2014-2016 Apple Inc. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public @@ -23,6 +23,7 @@ #include <utility> #include <wtf/text/AtomicStringImpl.h> +#include <wtf/text/IntegerToStringConversion.h> #include <wtf/text/WTFString.h> // Define 'NO_IMPLICIT_ATOMICSTRING' before including this header, @@ -41,34 +42,38 @@ class AtomicString { public: WTF_EXPORT_PRIVATE static void init(); - AtomicString() { } - AtomicString(const LChar* s) : m_string(add(s)) { } - AtomicString(const char* s) : m_string(add(s)) { } - AtomicString(const LChar* s, unsigned length) : m_string(add(s, length)) { } - AtomicString(const UChar* s, unsigned length) : m_string(add(s, length)) { } - AtomicString(const UChar* s, unsigned length, unsigned existingHash) : m_string(add(s, length, existingHash)) { } - AtomicString(const UChar* s) : m_string(add(s)) { } + AtomicString(); + AtomicString(const LChar*); + AtomicString(const char*); + AtomicString(const LChar*, unsigned length); + AtomicString(const UChar*, unsigned length); + AtomicString(const UChar*, unsigned length, unsigned existingHash); + AtomicString(const UChar*); template<size_t inlineCapacity> explicit AtomicString(const Vector<UChar, inlineCapacity>& characters) - : m_string(add(characters.data(), characters.size())) + : m_string(AtomicStringImpl::add(characters.data(), characters.size())) { } - ATOMICSTRING_CONVERSION AtomicString(StringImpl* imp) : m_string(add(imp)) { } - AtomicString(AtomicStringImpl* imp) : m_string(imp) { } - ATOMICSTRING_CONVERSION AtomicString(const String& s) : m_string(add(s.impl())) { } - AtomicString(StringImpl* baseString, unsigned start, unsigned length) : m_string(add(baseString, start, length)) { } + AtomicString(AtomicStringImpl*); + AtomicString(RefPtr<AtomicStringImpl>&&); + ATOMICSTRING_CONVERSION AtomicString(StringImpl*); + ATOMICSTRING_CONVERSION AtomicString(const String&); + AtomicString(StringImpl* baseString, unsigned start, unsigned length); + + // FIXME: AtomicString doesn’t always have AtomicStringImpl, so one of those two names needs to change.. + AtomicString(UniquedStringImpl* uid); enum ConstructFromLiteralTag { ConstructFromLiteral }; AtomicString(const char* characters, unsigned length, ConstructFromLiteralTag) - : m_string(addFromLiteralData(characters, length)) + : m_string(AtomicStringImpl::addLiteral(characters, length)) { } template<unsigned charactersCount> ALWAYS_INLINE AtomicString(const char (&characters)[charactersCount], ConstructFromLiteralTag) - : m_string(addFromLiteralData(characters, charactersCount - 1)) + : m_string(AtomicStringImpl::addLiteral(characters, charactersCount - 1)) { COMPILE_ASSERT(charactersCount > 1, AtomicStringFromLiteralNotEmpty); COMPILE_ASSERT((charactersCount - 1 <= ((unsigned(~0) - sizeof(StringImpl)) / sizeof(LChar))), AtomicStringFromLiteralCannotOverflow); @@ -77,15 +82,15 @@ public: // We have to declare the copy constructor and copy assignment operator as well, otherwise // they'll be implicitly deleted by adding the move constructor and move assignment operator. AtomicString(const AtomicString& other) : m_string(other.m_string) { } - AtomicString(AtomicString&& other) : m_string(std::move(other.m_string)) { } + AtomicString(AtomicString&& other) : m_string(WTFMove(other.m_string)) { } AtomicString& operator=(const AtomicString& other) { m_string = other.m_string; return *this; } - AtomicString& operator=(AtomicString&& other) { m_string = std::move(other.m_string); return *this; } + AtomicString& operator=(AtomicString&& other) { m_string = WTFMove(other.m_string); return *this; } // Hash table deleted values, which are only constructed and never copied or destroyed. AtomicString(WTF::HashTableDeletedValueType) : m_string(WTF::HashTableDeletedValue) { } bool isHashTableDeletedValue() const { return m_string.isHashTableDeletedValue(); } - WTF_EXPORT_STRING_API static AtomicStringImpl* find(const StringImpl*); + unsigned existingHash() const { return isNull() ? 0 : impl()->existingHash(); } operator const String&() const { return m_string; } const String& string() const { return m_string; }; @@ -93,31 +98,46 @@ public: AtomicStringImpl* impl() const { return static_cast<AtomicStringImpl *>(m_string.impl()); } bool is8Bit() const { return m_string.is8Bit(); } - const UChar* characters() const { return m_string.deprecatedCharacters(); } // FIXME: Delete this. const LChar* characters8() const { return m_string.characters8(); } const UChar* characters16() const { return m_string.characters16(); } unsigned length() const { return m_string.length(); } - + UChar operator[](unsigned int i) const { return m_string[i]; } WTF_EXPORT_STRING_API static AtomicString number(int); WTF_EXPORT_STRING_API static AtomicString number(unsigned); + WTF_EXPORT_STRING_API static AtomicString number(unsigned long); + WTF_EXPORT_STRING_API static AtomicString number(unsigned long long); WTF_EXPORT_STRING_API static AtomicString number(double); // If we need more overloads of the number function, we can add all the others that String has, but these seem to do for now. bool contains(UChar c) const { return m_string.contains(c); } bool contains(const LChar* s, bool caseSensitive = true) const { return m_string.contains(s, caseSensitive); } - bool contains(const String& s, bool caseSensitive = true) const + bool contains(const String& s) const + { return m_string.contains(s); } + bool contains(const String& s, bool caseSensitive) const { return m_string.contains(s, caseSensitive); } + bool containsIgnoringASCIICase(const String& s) const + { return m_string.containsIgnoringASCIICase(s); } size_t find(UChar c, unsigned start = 0) const { return m_string.find(c, start); } size_t find(const LChar* s, unsigned start = 0, bool caseSentitive = true) const { return m_string.find(s, start, caseSentitive); } size_t find(const String& s, unsigned start = 0, bool caseSentitive = true) const { return m_string.find(s, start, caseSentitive); } - - bool startsWith(const String& s, bool caseSensitive = true) const + size_t findIgnoringASCIICase(const String& s) const + { return m_string.findIgnoringASCIICase(s); } + size_t findIgnoringASCIICase(const String& s, unsigned startOffset) const + { return m_string.findIgnoringASCIICase(s, startOffset); } + size_t find(CharacterMatchFunctionPtr matchFunction, unsigned start = 0) const + { return m_string.find(matchFunction, start); } + + bool startsWith(const String& s) const + { return m_string.startsWith(s); } + bool startsWithIgnoringASCIICase(const String& s) const + { return m_string.startsWithIgnoringASCIICase(s); } + bool startsWith(const String& s, bool caseSensitive) const { return m_string.startsWith(s, caseSensitive); } bool startsWith(UChar character) const { return m_string.startsWith(character); } @@ -125,17 +145,21 @@ public: bool startsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const { return m_string.startsWith<matchLength>(prefix, caseSensitive); } - bool endsWith(const String& s, bool caseSensitive = true) const + bool endsWith(const String& s) const + { return m_string.endsWith(s); } + bool endsWithIgnoringASCIICase(const String& s) const + { return m_string.endsWithIgnoringASCIICase(s); } + bool endsWith(const String& s, bool caseSensitive) const { return m_string.endsWith(s, caseSensitive); } bool endsWith(UChar character) const { return m_string.endsWith(character); } template<unsigned matchLength> bool endsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const { return m_string.endsWith<matchLength>(prefix, caseSensitive); } - - WTF_EXPORT_STRING_API AtomicString lower() const; - AtomicString upper() const { return AtomicString(impl()->upper()); } - + + WTF_EXPORT_STRING_API AtomicString convertToASCIILowercase() const; + WTF_EXPORT_STRING_API AtomicString convertToASCIIUppercase() const; + int toInt(bool* ok = 0) const { return m_string.toInt(ok); } double toDouble(bool* ok = 0) const { return m_string.toDouble(ok); } float toFloat(bool* ok = 0) const { return m_string.toFloat(ok); } @@ -144,13 +168,11 @@ public: bool isNull() const { return m_string.isNull(); } bool isEmpty() const { return m_string.isEmpty(); } - static void remove(StringImpl*); - #if USE(CF) - AtomicString(CFStringRef s) : m_string(add(s)) { } -#endif + AtomicString(CFStringRef); +#endif #ifdef __OBJC__ - AtomicString(NSString* s) : m_string(add((CFStringRef)s)) { } + AtomicString(NSString*); operator NSString*() const { return m_string; } #endif @@ -167,37 +189,16 @@ private: // The explicit constructors with AtomicString::ConstructFromLiteral must be used for literals. AtomicString(ASCIILiteral); - String m_string; - - WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> add(const LChar*); - ALWAYS_INLINE static PassRefPtr<StringImpl> add(const char* s) { return add(reinterpret_cast<const LChar*>(s)); }; - WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> add(const LChar*, unsigned length); - WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> add(const UChar*, unsigned length); - ALWAYS_INLINE static PassRefPtr<StringImpl> add(const char* s, unsigned length) { return add(reinterpret_cast<const LChar*>(s), length); }; - WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> add(const UChar*, unsigned length, unsigned existingHash); - WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> add(const UChar*); - WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> add(StringImpl*, unsigned offset, unsigned length); - ALWAYS_INLINE static PassRefPtr<StringImpl> add(StringImpl* string) - { - if (!string || string->isAtomic()) { - ASSERT_WITH_MESSAGE(!string || isInAtomicStringTable(string), "The atomic string comes from an other thread!"); - return string; - } - return addSlowCase(string); - } - WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> addFromLiteralData(const char* characters, unsigned length); - WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> addSlowCase(StringImpl*); -#if USE(CF) - WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> add(CFStringRef); -#endif + enum class CaseConvertType { Upper, Lower }; + template<CaseConvertType> AtomicString convertASCIICase() const; WTF_EXPORT_STRING_API static AtomicString fromUTF8Internal(const char*, const char*); -#if !ASSERT_DISABLED - WTF_EXPORT_STRING_API static bool isInAtomicStringTable(StringImpl*); -#endif + String m_string; }; +static_assert(sizeof(AtomicString) == sizeof(String), "AtomicString and String must be same size!"); + inline bool operator==(const AtomicString& a, const AtomicString& b) { return a.impl() == b.impl(); } bool operator==(const AtomicString&, const LChar*); inline bool operator==(const AtomicString& a, const char* b) { return WTF::equal(a.impl(), reinterpret_cast<const LChar*>(b)); } @@ -216,25 +217,99 @@ inline bool operator!=(const LChar* a, const AtomicString& b) { return !(b == a) inline bool operator!=(const String& a, const AtomicString& b) { return !equal(a.impl(), b.impl()); } inline bool operator!=(const Vector<UChar>& a, const AtomicString& b) { return !(a == b); } -inline bool equalIgnoringCase(const AtomicString& a, const AtomicString& b) { return equalIgnoringCase(a.impl(), b.impl()); } -inline bool equalIgnoringCase(const AtomicString& a, const LChar* b) { return equalIgnoringCase(a.impl(), b); } -inline bool equalIgnoringCase(const AtomicString& a, const char* b) { return equalIgnoringCase(a.impl(), reinterpret_cast<const LChar*>(b)); } -inline bool equalIgnoringCase(const AtomicString& a, const String& b) { return equalIgnoringCase(a.impl(), b.impl()); } -inline bool equalIgnoringCase(const LChar* a, const AtomicString& b) { return equalIgnoringCase(a, b.impl()); } -inline bool equalIgnoringCase(const char* a, const AtomicString& b) { return equalIgnoringCase(reinterpret_cast<const LChar*>(a), b.impl()); } -inline bool equalIgnoringCase(const String& a, const AtomicString& b) { return equalIgnoringCase(a.impl(), b.impl()); } +bool equalIgnoringASCIICase(const AtomicString&, const AtomicString&); +bool equalIgnoringASCIICase(const AtomicString&, const String&); +bool equalIgnoringASCIICase(const String&, const AtomicString&); +bool equalIgnoringASCIICase(const AtomicString&, const char*); + +template<unsigned length> bool equalLettersIgnoringASCIICase(const AtomicString&, const char (&lowercaseLetters)[length]); + +inline AtomicString::AtomicString() +{ +} + +inline AtomicString::AtomicString(const LChar* s) + : m_string(AtomicStringImpl::add(s)) +{ +} + +inline AtomicString::AtomicString(const char* s) + : m_string(AtomicStringImpl::add(s)) +{ +} + +inline AtomicString::AtomicString(const LChar* s, unsigned length) + : m_string(AtomicStringImpl::add(s, length)) +{ +} + +inline AtomicString::AtomicString(const UChar* s, unsigned length) + : m_string(AtomicStringImpl::add(s, length)) +{ +} + +inline AtomicString::AtomicString(const UChar* s, unsigned length, unsigned existingHash) + : m_string(AtomicStringImpl::add(s, length, existingHash)) +{ +} + +inline AtomicString::AtomicString(const UChar* s) + : m_string(AtomicStringImpl::add(s)) +{ +} + +inline AtomicString::AtomicString(AtomicStringImpl* imp) + : m_string(imp) +{ +} + +inline AtomicString::AtomicString(RefPtr<AtomicStringImpl>&& imp) + : m_string(WTFMove(imp)) +{ +} + +inline AtomicString::AtomicString(StringImpl* imp) + : m_string(AtomicStringImpl::add(imp)) +{ +} + +inline AtomicString::AtomicString(const String& s) + : m_string(AtomicStringImpl::add(s.impl())) +{ +} + +inline AtomicString::AtomicString(StringImpl* baseString, unsigned start, unsigned length) + : m_string(AtomicStringImpl::add(baseString, start, length)) +{ +} + +inline AtomicString::AtomicString(UniquedStringImpl* uid) + : m_string(uid) +{ +} + +#if USE(CF) +inline AtomicString::AtomicString(CFStringRef s) + : m_string(AtomicStringImpl::add(s)) +{ +} +#endif + +#ifdef __OBJC__ +inline AtomicString::AtomicString(NSString* s) + : m_string(AtomicStringImpl::add((__bridge CFStringRef)s)) +{ +} +#endif // Define external global variables for the commonly used atomic strings. // These are only usable from the main thread. #ifndef ATOMICSTRING_HIDE_GLOBALS extern const WTF_EXPORTDATA AtomicString nullAtom; extern const WTF_EXPORTDATA AtomicString emptyAtom; -extern const WTF_EXPORTDATA AtomicString textAtom; -extern const WTF_EXPORTDATA AtomicString commentAtom; extern const WTF_EXPORTDATA AtomicString starAtom; extern const WTF_EXPORTDATA AtomicString xmlAtom; extern const WTF_EXPORTDATA AtomicString xmlnsAtom; -extern const WTF_EXPORTDATA AtomicString xlinkAtom; inline AtomicString AtomicString::fromUTF8(const char* characters, size_t length) { @@ -251,7 +326,7 @@ inline AtomicString AtomicString::fromUTF8(const char* characters) return nullAtom; if (!*characters) return emptyAtom; - return fromUTF8Internal(characters, 0); + return fromUTF8Internal(characters, nullptr); } #endif @@ -261,19 +336,48 @@ template<> struct DefaultHash<AtomicString> { typedef AtomicStringHash Hash; }; +template<unsigned length> inline bool equalLettersIgnoringASCIICase(const AtomicString& string, const char (&lowercaseLetters)[length]) +{ + return equalLettersIgnoringASCIICase(string.string(), lowercaseLetters); +} + +inline bool equalIgnoringASCIICase(const AtomicString& a, const AtomicString& b) +{ + return equalIgnoringASCIICase(a.string(), b.string()); +} + +inline bool equalIgnoringASCIICase(const AtomicString& a, const String& b) +{ + return equalIgnoringASCIICase(a.string(), b); +} + +inline bool equalIgnoringASCIICase(const String& a, const AtomicString& b) +{ + return equalIgnoringASCIICase(a, b.string()); +} + +inline bool equalIgnoringASCIICase(const AtomicString& a, const char* b) +{ + return equalIgnoringASCIICase(a.string(), b); +} + +template<> struct IntegerToStringConversionTrait<AtomicString> { + using ReturnType = AtomicString; + using AdditionalArgumentType = void; + static AtomicString flush(LChar* characters, unsigned length, void*) { return { characters, length }; } +}; + } // namespace WTF #ifndef ATOMICSTRING_HIDE_GLOBALS using WTF::AtomicString; using WTF::nullAtom; using WTF::emptyAtom; -using WTF::textAtom; -using WTF::commentAtom; using WTF::starAtom; using WTF::xmlAtom; using WTF::xmlnsAtom; -using WTF::xlinkAtom; #endif #include <wtf/text/StringConcatenate.h> + #endif // AtomicString_h |