/* * Copyright (C) 2004, 2005, 2006, 2008 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 * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * */ #ifndef AtomicString_h #define AtomicString_h #include "wtf/HashTableDeletedValueType.h" #include "wtf/WTFExport.h" #include "wtf/text/WTFString.h" // Define 'NO_IMPLICIT_ATOMICSTRING' before including this header, // to disallow (expensive) implicit String-->AtomicString conversions. #ifdef NO_IMPLICIT_ATOMICSTRING #define ATOMICSTRING_CONVERSION explicit #else #define ATOMICSTRING_CONVERSION #endif namespace WTF { struct AtomicStringHash; class WTF_EXPORT AtomicString { public: 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)) { } template explicit AtomicString(const Vector& characters) : m_string(add(characters.data(), characters.size())) { } explicit AtomicString(StringImpl* imp) : m_string(add(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)) { } enum ConstructFromLiteralTag { ConstructFromLiteral }; AtomicString(const char* characters, unsigned length, ConstructFromLiteralTag) : m_string(addFromLiteralData(characters, length)) { } template ALWAYS_INLINE AtomicString(const char (&characters)[charactersCount], ConstructFromLiteralTag) : m_string(addFromLiteralData(characters, charactersCount - 1)) { COMPILE_ASSERT(charactersCount > 1, AtomicStringFromLiteralNotEmpty); COMPILE_ASSERT((charactersCount - 1 <= ((unsigned(~0) - sizeof(StringImpl)) / sizeof(LChar))), AtomicStringFromLiteralCannotOverflow); } #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) // 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. // FIXME: Instead of explicitly casting to String&& here, we should use std::move, but that requires us to // have a standard library that supports move semantics. AtomicString(const AtomicString& other) : m_string(other.m_string) { } AtomicString(AtomicString&& other) : m_string(static_cast(other.m_string)) { } AtomicString& operator=(const AtomicString& other) { m_string = other.m_string; return *this; } AtomicString& operator=(AtomicString&& other) { m_string = static_cast(other.m_string); return *this; } #endif // 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(); } static StringImpl* find(const StringImpl*); operator const String&() const { return m_string; } const String& string() const { return m_string; }; StringImpl* impl() const { return m_string.impl(); } bool is8Bit() const { return m_string.is8Bit(); } 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]; } 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 { return m_string.contains(s, caseSensitive); } size_t find(UChar c, size_t start = 0) const { return m_string.find(c, start); } size_t find(const LChar* s, size_t start = 0, bool caseSentitive = true) const { return m_string.find(s, start, caseSentitive); } size_t find(const String& s, size_t start = 0, bool caseSentitive = true) const { return m_string.find(s, start, caseSentitive); } bool startsWith(const String& s, bool caseSensitive = true) const { return m_string.startsWith(s, caseSensitive); } bool startsWith(UChar character) const { return m_string.startsWith(character); } template bool startsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const { return m_string.startsWith(prefix, caseSensitive); } bool endsWith(const String& s, bool caseSensitive = true) const { return m_string.endsWith(s, caseSensitive); } bool endsWith(UChar character) const { return m_string.endsWith(character); } template bool endsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const { return m_string.endsWith(prefix, caseSensitive); } AtomicString lower() const; AtomicString upper() const { return AtomicString(impl()->upper()); } 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); } bool percentage(int& p) const { return m_string.percentage(p); } static AtomicString number(int); static AtomicString number(unsigned); static AtomicString number(long); static AtomicString number(unsigned long); static AtomicString number(long long); static AtomicString number(unsigned long long); static AtomicString number(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros); 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 #ifdef __OBJC__ AtomicString(NSString* s) : m_string(add((CFStringRef)s)) { } operator NSString*() const { return m_string; } #endif // AtomicString::fromUTF8 will return a null string if // the input data contains invalid UTF-8 sequences. static AtomicString fromUTF8(const char*, size_t); static AtomicString fromUTF8(const char*); #ifndef NDEBUG void show() const; #endif private: String m_string; static PassRefPtr add(const LChar*); ALWAYS_INLINE static PassRefPtr add(const char* s) { return add(reinterpret_cast(s)); }; static PassRefPtr add(const LChar*, unsigned length); static PassRefPtr add(const UChar*, unsigned length); ALWAYS_INLINE static PassRefPtr add(const char* s, unsigned length) { return add(reinterpret_cast(s), length); }; static PassRefPtr add(const UChar*, unsigned length, unsigned existingHash); static PassRefPtr add(const UChar*); static PassRefPtr add(StringImpl*, unsigned offset, unsigned length); ALWAYS_INLINE static PassRefPtr add(StringImpl* r) { if (!r || r->isAtomic()) return r; return addSlowCase(r); } static PassRefPtr addFromLiteralData(const char* characters, unsigned length); static PassRefPtr addSlowCase(StringImpl*); #if USE(CF) static PassRefPtr add(CFStringRef); #endif static AtomicString fromUTF8Internal(const char*, const char*); }; inline bool operator==(const AtomicString& a, const AtomicString& b) { return a.impl() == b.impl(); } WTF_EXPORT bool operator==(const AtomicString&, const LChar*); inline bool operator==(const AtomicString& a, const char* b) { return WTF::equal(a.impl(), reinterpret_cast(b)); } inline bool operator==(const AtomicString& a, const Vector& b) { return a.impl() && equal(a.impl(), b.data(), b.size()); } inline bool operator==(const AtomicString& a, const String& b) { return equal(a.impl(), b.impl()); } 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& a, const AtomicString& b) { return b == a; } inline bool operator!=(const AtomicString& a, const AtomicString& b) { return a.impl() != b.impl(); } inline bool operator!=(const AtomicString& a, const LChar* b) { return !(a == b); } inline bool operator!=(const AtomicString& a, const char* b) { return !(a == b); } inline bool operator!=(const AtomicString& a, const String& b) { return !equal(a.impl(), b.impl()); } inline bool operator!=(const AtomicString& a, const Vector& b) { return !(a == b); } 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& 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(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(a), b.impl()); } inline bool equalIgnoringCase(const String& a, const AtomicString& b) { return equalIgnoringCase(a.impl(), b.impl()); } // Define external global variables for the commonly used atomic strings. // These are only usable from the main thread. #ifndef ATOMICSTRING_HIDE_GLOBALS WTF_EXPORT extern const AtomicString nullAtom; WTF_EXPORT extern const AtomicString emptyAtom; WTF_EXPORT extern const AtomicString starAtom; WTF_EXPORT extern const AtomicString xmlAtom; WTF_EXPORT extern const AtomicString xmlnsAtom; WTF_EXPORT extern const AtomicString xlinkAtom; inline AtomicString AtomicString::fromUTF8(const char* characters, size_t length) { if (!characters) return nullAtom; if (!length) return emptyAtom; return fromUTF8Internal(characters, characters + length); } inline AtomicString AtomicString::fromUTF8(const char* characters) { if (!characters) return nullAtom; if (!*characters) return emptyAtom; return fromUTF8Internal(characters, 0); } #endif // AtomicStringHash is the default hash for AtomicString template struct DefaultHash; template<> struct DefaultHash { typedef AtomicStringHash Hash; }; } // namespace WTF #ifndef ATOMICSTRING_HIDE_GLOBALS using WTF::AtomicString; using WTF::nullAtom; using WTF::emptyAtom; using WTF::starAtom; using WTF::xmlAtom; using WTF::xmlnsAtom; using WTF::xlinkAtom; #endif #include "wtf/text/StringConcatenate.h" #endif // AtomicString_h