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 ATOMICSTRING_CONVERSION 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 bool isNull() const { return m_string.isNull(); } 142 bool isEmpty() const { return m_string.isEmpty(); } 143 144 static void remove(StringImpl*); 145 146 #if USE(CF) 147 AtomicString(CFStringRef s) : m_string(add(s)) { } 148 #endif 149 #ifdef __OBJC__ 150 AtomicString(NSString* s) : m_string(add((CFStringRef)s)) { } 151 operator NSString*() const { return m_string; } 152 #endif 153 // AtomicString::fromUTF8 will return a null string if 154 // the input data contains invalid UTF-8 sequences. 155 static AtomicString fromUTF8(const char*, size_t); 156 static AtomicString fromUTF8(const char*); 157 158 #ifndef NDEBUG 159 void show() const; 160 #endif 161 162 private: 163 String m_string; 164 165 static PassRefPtr<StringImpl> add(const LChar*); 166 ALWAYS_INLINE static PassRefPtr<StringImpl> add(const char* s) { return add(reinterpret_cast<const LChar*>(s)); }; 167 static PassRefPtr<StringImpl> add(const LChar*, unsigned length); 168 static PassRefPtr<StringImpl> add(const UChar*, unsigned length); 169 ALWAYS_INLINE static PassRefPtr<StringImpl> add(const char* s, unsigned length) { return add(reinterpret_cast<const LChar*>(s), length); }; 170 static PassRefPtr<StringImpl> add(const UChar*, unsigned length, unsigned existingHash); 171 static PassRefPtr<StringImpl> add(const UChar*); 172 static PassRefPtr<StringImpl> add(StringImpl*, unsigned offset, unsigned length); 173 ALWAYS_INLINE static PassRefPtr<StringImpl> add(StringImpl* r) 174 { 175 if (!r || r->isAtomic()) 176 return r; 177 return addSlowCase(r); 178 } 179 static PassRefPtr<StringImpl> addFromLiteralData(const char* characters, unsigned length); 180 static PassRefPtr<StringImpl> addSlowCase(StringImpl*); 181 #if USE(CF) 182 static PassRefPtr<StringImpl> add(CFStringRef); 183 #endif 184 185 static AtomicString fromUTF8Internal(const char*, const char*); 186 }; 187 188 inline bool operator==(const AtomicString& a, const AtomicString& b) { return a.impl() == b.impl(); } 189 WTF_EXPORT bool operator==(const AtomicString&, const LChar*); 190 inline bool operator==(const AtomicString& a, const char* b) { return WTF::equal(a.impl(), reinterpret_cast<const LChar*>(b)); } 191 inline bool operator==(const AtomicString& a, const Vector<UChar>& b) { return a.impl() && equal(a.impl(), b.data(), b.size()); } 192 inline bool operator==(const AtomicString& a, const String& b) { return equal(a.impl(), b.impl()); } 193 inline bool operator==(const LChar* a, const AtomicString& b) { return b == a; } 194 inline bool operator==(const String& a, const AtomicString& b) { return equal(a.impl(), b.impl()); } 195 inline bool operator==(const Vector<UChar>& a, const AtomicString& b) { return b == a; } 196 197 inline bool operator!=(const AtomicString& a, const AtomicString& b) { return a.impl() != b.impl(); } 198 inline bool operator!=(const AtomicString& a, const LChar* b) { return !(a == b); } 199 inline bool operator!=(const AtomicString& a, const char* b) { return !(a == b); } 200 inline bool operator!=(const AtomicString& a, const String& b) { return !equal(a.impl(), b.impl()); } 201 inline bool operator!=(const AtomicString& a, const Vector<UChar>& b) { return !(a == b); } 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 !(a == b); } 205 206 inline bool equalIgnoringCase(const AtomicString& a, const AtomicString& b) { return equalIgnoringCase(a.impl(), b.impl()); } 207 inline bool equalIgnoringCase(const AtomicString& a, const LChar* b) { return equalIgnoringCase(a.impl(), b); } 208 inline bool equalIgnoringCase(const AtomicString& a, const char* b) { return equalIgnoringCase(a.impl(), reinterpret_cast<const LChar*>(b)); } 209 inline bool equalIgnoringCase(const AtomicString& a, const String& b) { return equalIgnoringCase(a.impl(), b.impl()); } 210 inline bool equalIgnoringCase(const LChar* a, const AtomicString& b) { return equalIgnoringCase(a, b.impl()); } 211 inline bool equalIgnoringCase(const char* a, const AtomicString& b) { return equalIgnoringCase(reinterpret_cast<const LChar*>(a), b.impl()); } 212 inline bool equalIgnoringCase(const String& a, const AtomicString& b) { return equalIgnoringCase(a.impl(), b.impl()); } 213 214 // Define external global variables for the commonly used atomic strings. 215 // These are only usable from the main thread. 216 #ifndef ATOMICSTRING_HIDE_GLOBALS 217 WTF_EXPORT extern const AtomicString nullAtom; 218 WTF_EXPORT extern const AtomicString emptyAtom; 219 WTF_EXPORT extern const AtomicString textAtom; 220 WTF_EXPORT extern const AtomicString commentAtom; 221 WTF_EXPORT extern const AtomicString starAtom; 222 WTF_EXPORT extern const AtomicString xmlAtom; 223 WTF_EXPORT extern const AtomicString xmlnsAtom; 224 WTF_EXPORT extern const AtomicString xlinkAtom; 225 226 inline AtomicString AtomicString::fromUTF8(const char* characters, size_t length) 227 { 228 if (!characters) 229 return nullAtom; 230 if (!length) 231 return emptyAtom; 232 return fromUTF8Internal(characters, characters + length); 233 } 234 235 inline AtomicString AtomicString::fromUTF8(const char* characters) 236 { 237 if (!characters) 238 return nullAtom; 239 if (!*characters) 240 return emptyAtom; 241 return fromUTF8Internal(characters, 0); 242 } 243 #endif 244 245 // AtomicStringHash is the default hash for AtomicString 246 template<typename T> struct DefaultHash; 247 template<> struct DefaultHash<AtomicString> { 248 typedef AtomicStringHash Hash; 249 }; 250 251 } // namespace WTF 252 253 #ifndef ATOMICSTRING_HIDE_GLOBALS 254 using WTF::AtomicString; 255 using WTF::nullAtom; 256 using WTF::emptyAtom; 257 using WTF::textAtom; 258 using WTF::commentAtom; 259 using WTF::starAtom; 260 using WTF::xmlAtom; 261 using WTF::xmlnsAtom; 262 using WTF::xlinkAtom; 263 #endif 264 265 #include "wtf/text/StringConcatenate.h" 266 #endif // AtomicString_h 267