1/* 2 * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved. 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Library General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Library General Public License for more details. 13 * 14 * You should have received a copy of the GNU Library General Public License 15 * along with this library; see the file COPYING.LIB. If not, write to 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 * 19 */ 20 21#ifndef AtomicString_h 22#define AtomicString_h 23 24#include "wtf/HashTableDeletedValueType.h" 25#include "wtf/WTFExport.h" 26#include "wtf/text/CString.h" 27#include "wtf/text/WTFString.h" 28 29namespace WTF { 30 31struct AtomicStringHash; 32 33class WTF_EXPORT AtomicString { 34public: 35 static void init(); 36 37 AtomicString() { } 38 AtomicString(const LChar* s) : m_string(add(s)) { } 39 AtomicString(const char* s) : m_string(add(s)) { } 40 AtomicString(const LChar* s, unsigned length) : m_string(add(s, length)) { } 41 AtomicString(const UChar* s, unsigned length) : m_string(add(s, length)) { } 42 AtomicString(const UChar* s, unsigned length, unsigned existingHash) : m_string(add(s, length, existingHash)) { } 43 AtomicString(const UChar* s) : m_string(add(s)) { } 44 45 template<size_t inlineCapacity> 46 explicit AtomicString(const Vector<UChar, inlineCapacity>& characters) 47 : m_string(add(characters.data(), characters.size())) 48 { 49 } 50 51 // Constructing an AtomicString from a String / StringImpl can be expensive if 52 // the StringImpl is not already atomic. 53 explicit AtomicString(StringImpl* impl) : m_string(add(impl)) { } 54 explicit AtomicString(const String& s) : m_string(add(s.impl())) { } 55 56 AtomicString(StringImpl* baseString, unsigned start, unsigned length) : m_string(add(baseString, start, length)) { } 57 58 enum ConstructFromLiteralTag { ConstructFromLiteral }; 59 AtomicString(const char* characters, unsigned length, ConstructFromLiteralTag) 60 : m_string(addFromLiteralData(characters, length)) 61 { 62 } 63 64 template<unsigned charactersCount> 65 ALWAYS_INLINE AtomicString(const char (&characters)[charactersCount], ConstructFromLiteralTag) 66 : m_string(addFromLiteralData(characters, charactersCount - 1)) 67 { 68 COMPILE_ASSERT(charactersCount > 1, AtomicStringFromLiteralNotEmpty); 69 COMPILE_ASSERT((charactersCount - 1 <= ((unsigned(~0) - sizeof(StringImpl)) / sizeof(LChar))), AtomicStringFromLiteralCannotOverflow); 70 } 71 72 // Hash table deleted values, which are only constructed and never copied or destroyed. 73 AtomicString(WTF::HashTableDeletedValueType) : m_string(WTF::HashTableDeletedValue) { } 74 bool isHashTableDeletedValue() const { return m_string.isHashTableDeletedValue(); } 75 76 static StringImpl* find(const StringImpl*); 77 78 operator const String&() const { return m_string; } 79 const String& string() const { return m_string; }; 80 81 StringImpl* impl() const { return m_string.impl(); } 82 83 bool is8Bit() const { return m_string.is8Bit(); } 84 const LChar* characters8() const { return m_string.characters8(); } 85 const UChar* characters16() const { return m_string.characters16(); } 86 unsigned length() const { return m_string.length(); } 87 88 UChar operator[](unsigned i) const { return m_string[i]; } 89 90 bool contains(UChar c) const { return m_string.contains(c); } 91 bool contains(const LChar* s, bool caseSensitive = true) const 92 { return m_string.contains(s, caseSensitive); } 93 bool contains(const String& s, bool caseSensitive = true) const 94 { return m_string.contains(s, caseSensitive); } 95 96 size_t find(UChar c, size_t start = 0) const { return m_string.find(c, start); } 97 size_t find(const LChar* s, size_t start = 0, bool caseSentitive = true) const 98 { return m_string.find(s, start, caseSentitive); } 99 size_t find(const String& s, size_t start = 0, bool caseSentitive = true) const 100 { return m_string.find(s, start, caseSentitive); } 101 102 bool startsWith(const String& s, bool caseSensitive = true) const 103 { return m_string.startsWith(s, caseSensitive); } 104 bool startsWith(UChar character) const 105 { return m_string.startsWith(character); } 106 template<unsigned matchLength> 107 bool startsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const 108 { return m_string.startsWith<matchLength>(prefix, caseSensitive); } 109 110 bool endsWith(const String& s, bool caseSensitive = true) const 111 { return m_string.endsWith(s, caseSensitive); } 112 bool endsWith(UChar character) const 113 { return m_string.endsWith(character); } 114 template<unsigned matchLength> 115 bool endsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const 116 { return m_string.endsWith<matchLength>(prefix, caseSensitive); } 117 118 AtomicString lower() const; 119 AtomicString upper() const { return AtomicString(impl()->upper()); } 120 121 int toInt(bool* ok = 0) const { return m_string.toInt(ok); } 122 double toDouble(bool* ok = 0) const { return m_string.toDouble(ok); } 123 float toFloat(bool* ok = 0) const { return m_string.toFloat(ok); } 124 bool percentage(int& p) const { return m_string.percentage(p); } 125 126 static AtomicString number(int); 127 static AtomicString number(unsigned); 128 static AtomicString number(long); 129 static AtomicString number(unsigned long); 130 static AtomicString number(long long); 131 static AtomicString number(unsigned long long); 132 133 static AtomicString number(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros); 134 135 bool isNull() const { return m_string.isNull(); } 136 bool isEmpty() const { return m_string.isEmpty(); } 137 138 static void remove(StringImpl*); 139 140#if USE(CF) 141 AtomicString(CFStringRef s) : m_string(add(s)) { } 142#endif 143#ifdef __OBJC__ 144 AtomicString(NSString* s) : m_string(add((CFStringRef)s)) { } 145 operator NSString*() const { return m_string; } 146#endif 147 // AtomicString::fromUTF8 will return a null string if 148 // the input data contains invalid UTF-8 sequences. 149 static AtomicString fromUTF8(const char*, size_t); 150 static AtomicString fromUTF8(const char*); 151 152 CString ascii() const { return m_string.ascii(); } 153 CString latin1() const { return m_string.latin1(); } 154 CString utf8(UTF8ConversionMode mode = LenientUTF8Conversion) const { return m_string.utf8(mode); } 155 156#ifndef NDEBUG 157 void show() const; 158#endif 159 160private: 161 String m_string; 162 163 static PassRefPtr<StringImpl> add(const LChar*); 164 ALWAYS_INLINE static PassRefPtr<StringImpl> add(const char* s) { return add(reinterpret_cast<const LChar*>(s)); }; 165 static PassRefPtr<StringImpl> add(const LChar*, unsigned length); 166 static PassRefPtr<StringImpl> add(const UChar*, unsigned length); 167 ALWAYS_INLINE static PassRefPtr<StringImpl> add(const char* s, unsigned length) { return add(reinterpret_cast<const LChar*>(s), length); }; 168 static PassRefPtr<StringImpl> add(const UChar*, unsigned length, unsigned existingHash); 169 static PassRefPtr<StringImpl> add(const UChar*); 170 static PassRefPtr<StringImpl> add(StringImpl*, unsigned offset, unsigned length); 171 ALWAYS_INLINE static PassRefPtr<StringImpl> add(StringImpl* r) 172 { 173 if (!r || r->isAtomic()) 174 return r; 175 return addSlowCase(r); 176 } 177 static PassRefPtr<StringImpl> addFromLiteralData(const char* characters, unsigned length); 178 static PassRefPtr<StringImpl> addSlowCase(StringImpl*); 179#if USE(CF) 180 static PassRefPtr<StringImpl> add(CFStringRef); 181#endif 182 183 static AtomicString fromUTF8Internal(const char*, const char*); 184}; 185 186inline bool operator==(const AtomicString& a, const AtomicString& b) { return a.impl() == b.impl(); } 187WTF_EXPORT bool operator==(const AtomicString&, const LChar*); 188inline bool operator==(const AtomicString& a, const char* b) { return WTF::equal(a.impl(), reinterpret_cast<const LChar*>(b)); } 189inline bool operator==(const AtomicString& a, const Vector<UChar>& b) { return a.impl() && equal(a.impl(), b.data(), b.size()); } 190inline bool operator==(const AtomicString& a, const String& b) { return equal(a.impl(), b.impl()); } 191inline bool operator==(const LChar* a, const AtomicString& b) { return b == a; } 192inline bool operator==(const String& a, const AtomicString& b) { return equal(a.impl(), b.impl()); } 193inline bool operator==(const Vector<UChar>& a, const AtomicString& b) { return b == a; } 194 195inline bool operator!=(const AtomicString& a, const AtomicString& b) { return a.impl() != b.impl(); } 196inline bool operator!=(const AtomicString& a, const LChar* b) { return !(a == b); } 197inline bool operator!=(const AtomicString& a, const char* b) { return !(a == b); } 198inline bool operator!=(const AtomicString& a, const String& b) { return !equal(a.impl(), b.impl()); } 199inline bool operator!=(const AtomicString& a, const Vector<UChar>& b) { return !(a == b); } 200inline bool operator!=(const LChar* a, const AtomicString& b) { return !(b == a); } 201inline bool operator!=(const String& a, const AtomicString& b) { return !equal(a.impl(), b.impl()); } 202inline bool operator!=(const Vector<UChar>& a, const AtomicString& b) { return !(a == b); } 203 204inline bool equalIgnoringCase(const AtomicString& a, const AtomicString& b) { return equalIgnoringCase(a.impl(), b.impl()); } 205inline bool equalIgnoringCase(const AtomicString& a, const LChar* b) { return equalIgnoringCase(a.impl(), b); } 206inline bool equalIgnoringCase(const AtomicString& a, const char* b) { return equalIgnoringCase(a.impl(), reinterpret_cast<const LChar*>(b)); } 207inline bool equalIgnoringCase(const AtomicString& a, const String& b) { return equalIgnoringCase(a.impl(), b.impl()); } 208inline bool equalIgnoringCase(const LChar* a, const AtomicString& b) { return equalIgnoringCase(a, b.impl()); } 209inline bool equalIgnoringCase(const char* a, const AtomicString& b) { return equalIgnoringCase(reinterpret_cast<const LChar*>(a), b.impl()); } 210inline bool equalIgnoringCase(const String& a, const AtomicString& b) { return equalIgnoringCase(a.impl(), b.impl()); } 211 212// Define external global variables for the commonly used atomic strings. 213// These are only usable from the main thread. 214WTF_EXPORT extern const AtomicString& nullAtom; 215WTF_EXPORT extern const AtomicString& emptyAtom; 216WTF_EXPORT extern const AtomicString& starAtom; 217WTF_EXPORT extern const AtomicString& xmlAtom; 218WTF_EXPORT extern const AtomicString& xmlnsAtom; 219WTF_EXPORT extern const AtomicString& xlinkAtom; 220 221inline AtomicString AtomicString::fromUTF8(const char* characters, size_t length) 222{ 223 if (!characters) 224 return nullAtom; 225 if (!length) 226 return emptyAtom; 227 return fromUTF8Internal(characters, characters + length); 228} 229 230inline AtomicString AtomicString::fromUTF8(const char* characters) 231{ 232 if (!characters) 233 return nullAtom; 234 if (!*characters) 235 return emptyAtom; 236 return fromUTF8Internal(characters, 0); 237} 238 239// AtomicStringHash is the default hash for AtomicString 240template<typename T> struct DefaultHash; 241template<> struct DefaultHash<AtomicString> { 242 typedef AtomicStringHash Hash; 243}; 244 245} // namespace WTF 246 247WTF_ALLOW_MOVE_INIT_AND_COMPARE_WITH_MEM_FUNCTIONS(AtomicString); 248 249using WTF::AtomicString; 250using WTF::nullAtom; 251using WTF::emptyAtom; 252using WTF::starAtom; 253using WTF::xmlAtom; 254using WTF::xmlnsAtom; 255using WTF::xlinkAtom; 256 257#include "wtf/text/StringConcatenate.h" 258#endif // AtomicString_h 259