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/WTFString.h" 27 28 // Define 'NO_IMPLICIT_ATOMICSTRING' before including this header, 29 // to disallow (expensive) implicit String-->AtomicString conversions. 30 #ifdef NO_IMPLICIT_ATOMICSTRING 31 #define ATOMICSTRING_CONVERSION explicit 32 #else 33 #define ATOMICSTRING_CONVERSION 34 #endif 35 36 namespace WTF { 37 38 struct AtomicStringHash; 39 40 class WTF_EXPORT AtomicString { 41 public: 42 static void init(); 43 44 AtomicString() { } 45 AtomicString(const LChar* s) : m_string(add(s)) { } 46 AtomicString(const char* s) : m_string(add(s)) { } 47 AtomicString(const LChar* s, unsigned length) : m_string(add(s, length)) { } 48 AtomicString(const UChar* s, unsigned length) : m_string(add(s, length)) { } 49 AtomicString(const UChar* s, unsigned length, unsigned existingHash) : m_string(add(s, length, existingHash)) { } 50 AtomicString(const UChar* s) : m_string(add(s)) { } 51 52 template<size_t inlineCapacity> 53 explicit AtomicString(const Vector<UChar, inlineCapacity>& characters) 54 : m_string(add(characters.data(), characters.size())) 55 { 56 } 57 58 explicit AtomicString(StringImpl* imp) : m_string(add(imp)) { } 59 ATOMICSTRING_CONVERSION AtomicString(const String& s) : m_string(add(s.impl())) { } 60 AtomicString(StringImpl* baseString, unsigned start, unsigned length) : m_string(add(baseString, start, length)) { } 61 62 enum ConstructFromLiteralTag { ConstructFromLiteral }; 63 AtomicString(const char* characters, unsigned length, ConstructFromLiteralTag) 64 : m_string(addFromLiteralData(characters, length)) 65 { 66 } 67 68 template<unsigned charactersCount> 69 ALWAYS_INLINE AtomicString(const char (&characters)[charactersCount], ConstructFromLiteralTag) 70 : m_string(addFromLiteralData(characters, charactersCount - 1)) 71 { 72 COMPILE_ASSERT(charactersCount > 1, AtomicStringFromLiteralNotEmpty); 73 COMPILE_ASSERT((charactersCount - 1 <= ((unsigned(~0) - sizeof(StringImpl)) / sizeof(LChar))), AtomicStringFromLiteralCannotOverflow); 74 } 75 76 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) 77 // We have to declare the copy constructor and copy assignment operator as well, otherwise 78 // they'll be implicitly deleted by adding the move constructor and move assignment operator. 79 // FIXME: Instead of explicitly casting to String&& here, we should use std::move, but that requires us to 80 // have a standard library that supports move semantics. 81 AtomicString(const AtomicString& other) : m_string(other.m_string) { } 82 AtomicString(AtomicString&& other) : m_string(static_cast<String&&>(other.m_string)) { } 83 AtomicString& operator=(const AtomicString& other) { m_string = other.m_string; return *this; } 84 AtomicString& operator=(AtomicString&& other) { m_string = static_cast<String&&>(other.m_string); return *this; } 85 #endif 86 87 // Hash table deleted values, which are only constructed and never copied or destroyed. 88 AtomicString(WTF::HashTableDeletedValueType) : m_string(WTF::HashTableDeletedValue) { } 89 bool isHashTableDeletedValue() const { return m_string.isHashTableDeletedValue(); } 90 91 static StringImpl* find(const StringImpl*); 92 93 operator const String&() const { return m_string; } 94 const String& string() const { return m_string; }; 95 96 StringImpl* impl() const { return m_string.impl(); } 97 98 bool is8Bit() const { return m_string.is8Bit(); } 99 const LChar* characters8() const { return m_string.characters8(); } 100 const UChar* characters16() const { return m_string.characters16(); } 101 unsigned length() const { return m_string.length(); } 102 103 UChar operator[](unsigned int i) const { return m_string[i]; } 104 105 bool contains(UChar c) const { return m_string.contains(c); } 106 bool contains(const LChar* s, bool caseSensitive = true) const 107 { return m_string.contains(s, caseSensitive); } 108 bool contains(const String& s, bool caseSensitive = true) const 109 { return m_string.contains(s, caseSensitive); } 110 111 size_t find(UChar c, size_t start = 0) const { return m_string.find(c, start); } 112 size_t find(const LChar* s, size_t start = 0, bool caseSentitive = true) const 113 { return m_string.find(s, start, caseSentitive); } 114 size_t find(const String& s, size_t start = 0, bool caseSentitive = true) const 115 { return m_string.find(s, start, caseSentitive); } 116 117 bool startsWith(const String& s, bool caseSensitive = true) const 118 { return m_string.startsWith(s, caseSensitive); } 119 bool startsWith(UChar character) const 120 { return m_string.startsWith(character); } 121 template<unsigned matchLength> 122 bool startsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const 123 { return m_string.startsWith<matchLength>(prefix, caseSensitive); } 124 125 bool endsWith(const String& s, bool caseSensitive = true) const 126 { return m_string.endsWith(s, caseSensitive); } 127 bool endsWith(UChar character) const 128 { return m_string.endsWith(character); } 129 template<unsigned matchLength> 130 bool endsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const 131 { return m_string.endsWith<matchLength>(prefix, caseSensitive); } 132 133 AtomicString lower() const; 134 AtomicString upper() const { return AtomicString(impl()->upper()); } 135 136 int toInt(bool* ok = 0) const { return m_string.toInt(ok); } 137 double toDouble(bool* ok = 0) const { return m_string.toDouble(ok); } 138 float toFloat(bool* ok = 0) const { return m_string.toFloat(ok); } 139 bool percentage(int& p) const { return m_string.percentage(p); } 140 141 static AtomicString number(int); 142 static AtomicString number(unsigned); 143 static AtomicString number(long); 144 static AtomicString number(unsigned long); 145 static AtomicString number(long long); 146 static AtomicString number(unsigned long long); 147 148 static AtomicString number(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros); 149 150 bool isNull() const { return m_string.isNull(); } 151 bool isEmpty() const { return m_string.isEmpty(); } 152 153 static void remove(StringImpl*); 154 155 #if USE(CF) 156 AtomicString(CFStringRef s) : m_string(add(s)) { } 157 #endif 158 #ifdef __OBJC__ 159 AtomicString(NSString* s) : m_string(add((CFStringRef)s)) { } 160 operator NSString*() const { return m_string; } 161 #endif 162 // AtomicString::fromUTF8 will return a null string if 163 // the input data contains invalid UTF-8 sequences. 164 static AtomicString fromUTF8(const char*, size_t); 165 static AtomicString fromUTF8(const char*); 166 167 #ifndef NDEBUG 168 void show() const; 169 #endif 170 171 private: 172 String m_string; 173 174 static PassRefPtr<StringImpl> add(const LChar*); 175 ALWAYS_INLINE static PassRefPtr<StringImpl> add(const char* s) { return add(reinterpret_cast<const LChar*>(s)); }; 176 static PassRefPtr<StringImpl> add(const LChar*, unsigned length); 177 static PassRefPtr<StringImpl> add(const UChar*, unsigned length); 178 ALWAYS_INLINE static PassRefPtr<StringImpl> add(const char* s, unsigned length) { return add(reinterpret_cast<const LChar*>(s), length); }; 179 static PassRefPtr<StringImpl> add(const UChar*, unsigned length, unsigned existingHash); 180 static PassRefPtr<StringImpl> add(const UChar*); 181 static PassRefPtr<StringImpl> add(StringImpl*, unsigned offset, unsigned length); 182 ALWAYS_INLINE static PassRefPtr<StringImpl> add(StringImpl* r) 183 { 184 if (!r || r->isAtomic()) 185 return r; 186 return addSlowCase(r); 187 } 188 static PassRefPtr<StringImpl> addFromLiteralData(const char* characters, unsigned length); 189 static PassRefPtr<StringImpl> addSlowCase(StringImpl*); 190 #if USE(CF) 191 static PassRefPtr<StringImpl> add(CFStringRef); 192 #endif 193 194 static AtomicString fromUTF8Internal(const char*, const char*); 195 }; 196 197 inline bool operator==(const AtomicString& a, const AtomicString& b) { return a.impl() == b.impl(); } 198 WTF_EXPORT bool operator==(const AtomicString&, const LChar*); 199 inline bool operator==(const AtomicString& a, const char* b) { return WTF::equal(a.impl(), reinterpret_cast<const LChar*>(b)); } 200 inline bool operator==(const AtomicString& a, const Vector<UChar>& b) { return a.impl() && equal(a.impl(), b.data(), b.size()); } 201 inline bool operator==(const AtomicString& a, const String& b) { return equal(a.impl(), b.impl()); } 202 inline bool operator==(const LChar* a, const AtomicString& b) { return b == a; } 203 inline bool operator==(const String& a, const AtomicString& b) { return equal(a.impl(), b.impl()); } 204 inline bool operator==(const Vector<UChar>& a, const AtomicString& b) { return b == a; } 205 206 inline bool operator!=(const AtomicString& a, const AtomicString& b) { return a.impl() != b.impl(); } 207 inline bool operator!=(const AtomicString& a, const LChar* b) { return !(a == b); } 208 inline bool operator!=(const AtomicString& a, const char* b) { return !(a == b); } 209 inline bool operator!=(const AtomicString& a, const String& b) { return !equal(a.impl(), b.impl()); } 210 inline bool operator!=(const AtomicString& a, const Vector<UChar>& b) { return !(a == b); } 211 inline bool operator!=(const LChar* a, const AtomicString& b) { return !(b == a); } 212 inline bool operator!=(const String& a, const AtomicString& b) { return !equal(a.impl(), b.impl()); } 213 inline bool operator!=(const Vector<UChar>& a, const AtomicString& b) { return !(a == b); } 214 215 inline bool equalIgnoringCase(const AtomicString& a, const AtomicString& b) { return equalIgnoringCase(a.impl(), b.impl()); } 216 inline bool equalIgnoringCase(const AtomicString& a, const LChar* b) { return equalIgnoringCase(a.impl(), b); } 217 inline bool equalIgnoringCase(const AtomicString& a, const char* b) { return equalIgnoringCase(a.impl(), reinterpret_cast<const LChar*>(b)); } 218 inline bool equalIgnoringCase(const AtomicString& a, const String& b) { return equalIgnoringCase(a.impl(), b.impl()); } 219 inline bool equalIgnoringCase(const LChar* a, const AtomicString& b) { return equalIgnoringCase(a, b.impl()); } 220 inline bool equalIgnoringCase(const char* a, const AtomicString& b) { return equalIgnoringCase(reinterpret_cast<const LChar*>(a), b.impl()); } 221 inline bool equalIgnoringCase(const String& a, const AtomicString& b) { return equalIgnoringCase(a.impl(), b.impl()); } 222 223 // Define external global variables for the commonly used atomic strings. 224 // These are only usable from the main thread. 225 #ifndef ATOMICSTRING_HIDE_GLOBALS 226 WTF_EXPORT extern const AtomicString nullAtom; 227 WTF_EXPORT extern const AtomicString emptyAtom; 228 WTF_EXPORT extern const AtomicString starAtom; 229 WTF_EXPORT extern const AtomicString xmlAtom; 230 WTF_EXPORT extern const AtomicString xmlnsAtom; 231 WTF_EXPORT extern const AtomicString xlinkAtom; 232 233 inline AtomicString AtomicString::fromUTF8(const char* characters, size_t length) 234 { 235 if (!characters) 236 return nullAtom; 237 if (!length) 238 return emptyAtom; 239 return fromUTF8Internal(characters, characters + length); 240 } 241 242 inline AtomicString AtomicString::fromUTF8(const char* characters) 243 { 244 if (!characters) 245 return nullAtom; 246 if (!*characters) 247 return emptyAtom; 248 return fromUTF8Internal(characters, 0); 249 } 250 #endif 251 252 // AtomicStringHash is the default hash for AtomicString 253 template<typename T> struct DefaultHash; 254 template<> struct DefaultHash<AtomicString> { 255 typedef AtomicStringHash Hash; 256 }; 257 258 } // namespace WTF 259 260 #ifndef ATOMICSTRING_HIDE_GLOBALS 261 using WTF::AtomicString; 262 using WTF::nullAtom; 263 using WTF::emptyAtom; 264 using WTF::starAtom; 265 using WTF::xmlAtom; 266 using WTF::xmlnsAtom; 267 using WTF::xlinkAtom; 268 #endif 269 270 #include "wtf/text/StringConcatenate.h" 271 #endif // AtomicString_h 272