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
29 namespace WTF {
30
31 struct AtomicStringHash;
32
33 class WTF_EXPORT AtomicString {
34 public:
35 static void init();
36
AtomicString()37 AtomicString() { }
AtomicString(const LChar * s)38 AtomicString(const LChar* s) : m_string(add(s)) { }
AtomicString(const char * s)39 AtomicString(const char* s) : m_string(add(s)) { }
AtomicString(const LChar * s,unsigned length)40 AtomicString(const LChar* s, unsigned length) : m_string(add(s, length)) { }
AtomicString(const UChar * s,unsigned length)41 AtomicString(const UChar* s, unsigned length) : m_string(add(s, length)) { }
AtomicString(const UChar * s,unsigned length,unsigned existingHash)42 AtomicString(const UChar* s, unsigned length, unsigned existingHash) : m_string(add(s, length, existingHash)) { }
AtomicString(const UChar * s)43 AtomicString(const UChar* s) : m_string(add(s)) { }
44
45 template<size_t inlineCapacity>
AtomicString(const Vector<UChar,inlineCapacity> & characters)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.
AtomicString(StringImpl * impl)53 explicit AtomicString(StringImpl* impl) : m_string(add(impl)) { }
AtomicString(const String & s)54 explicit AtomicString(const String& s) : m_string(add(s.impl())) { }
55
AtomicString(StringImpl * baseString,unsigned start,unsigned length)56 AtomicString(StringImpl* baseString, unsigned start, unsigned length) : m_string(add(baseString, start, length)) { }
57
58 enum ConstructFromLiteralTag { ConstructFromLiteral };
AtomicString(const char * characters,unsigned length,ConstructFromLiteralTag)59 AtomicString(const char* characters, unsigned length, ConstructFromLiteralTag)
60 : m_string(addFromLiteralData(characters, length))
61 {
62 }
63
64 template<unsigned charactersCount>
AtomicString(const char (& characters)[charactersCount],ConstructFromLiteralTag)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.
AtomicString(WTF::HashTableDeletedValueType)73 AtomicString(WTF::HashTableDeletedValueType) : m_string(WTF::HashTableDeletedValue) { }
isHashTableDeletedValue()74 bool isHashTableDeletedValue() const { return m_string.isHashTableDeletedValue(); }
75
76 static StringImpl* find(const StringImpl*);
77
78 operator const String&() const { return m_string; }
string()79 const String& string() const { return m_string; };
80
impl()81 StringImpl* impl() const { return m_string.impl(); }
82
is8Bit()83 bool is8Bit() const { return m_string.is8Bit(); }
characters8()84 const LChar* characters8() const { return m_string.characters8(); }
characters16()85 const UChar* characters16() const { return m_string.characters16(); }
length()86 unsigned length() const { return m_string.length(); }
87
88 UChar operator[](unsigned i) const { return m_string[i]; }
89
contains(UChar c)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); }
startsWith(UChar character)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); }
endsWith(UChar character)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;
upper()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); }
percentage(int & p)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
isNull()135 bool isNull() const { return m_string.isNull(); }
isEmpty()136 bool isEmpty() const { return m_string.isEmpty(); }
137
138 static void remove(StringImpl*);
139
140 #if USE(CF)
AtomicString(CFStringRef s)141 AtomicString(CFStringRef s) : m_string(add(s)) { }
142 #endif
143 #ifdef __OBJC__
AtomicString(NSString * s)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
ascii()152 CString ascii() const { return m_string.ascii(); }
latin1()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
160 private:
161 String m_string;
162
163 static PassRefPtr<StringImpl> add(const LChar*);
add(const char * s)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);
add(const char * s,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);
add(StringImpl * r)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
186 inline bool operator==(const AtomicString& a, const AtomicString& b) { return a.impl() == b.impl(); }
187 WTF_EXPORT bool operator==(const AtomicString&, const LChar*);
188 inline bool operator==(const AtomicString& a, const char* b) { return WTF::equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
189 inline bool operator==(const AtomicString& a, const Vector<UChar>& b) { return a.impl() && equal(a.impl(), b.data(), b.size()); }
190 inline bool operator==(const AtomicString& a, const String& b) { return equal(a.impl(), b.impl()); }
191 inline bool operator==(const LChar* a, const AtomicString& b) { return b == a; }
192 inline bool operator==(const String& a, const AtomicString& b) { return equal(a.impl(), b.impl()); }
193 inline bool operator==(const Vector<UChar>& a, const AtomicString& b) { return b == a; }
194
195 inline bool operator!=(const AtomicString& a, const AtomicString& b) { return a.impl() != b.impl(); }
196 inline bool operator!=(const AtomicString& a, const LChar* b) { return !(a == b); }
197 inline bool operator!=(const AtomicString& a, const char* b) { return !(a == b); }
198 inline bool operator!=(const AtomicString& a, const String& b) { return !equal(a.impl(), b.impl()); }
199 inline bool operator!=(const AtomicString& a, const Vector<UChar>& b) { return !(a == b); }
200 inline bool operator!=(const LChar* a, const AtomicString& b) { return !(b == a); }
201 inline bool operator!=(const String& a, const AtomicString& b) { return !equal(a.impl(), b.impl()); }
202 inline bool operator!=(const Vector<UChar>& a, const AtomicString& b) { return !(a == b); }
203
equalIgnoringCase(const AtomicString & a,const AtomicString & b)204 inline bool equalIgnoringCase(const AtomicString& a, const AtomicString& b) { return equalIgnoringCase(a.impl(), b.impl()); }
equalIgnoringCase(const AtomicString & a,const LChar * b)205 inline bool equalIgnoringCase(const AtomicString& a, const LChar* b) { return equalIgnoringCase(a.impl(), b); }
equalIgnoringCase(const AtomicString & a,const char * b)206 inline bool equalIgnoringCase(const AtomicString& a, const char* b) { return equalIgnoringCase(a.impl(), reinterpret_cast<const LChar*>(b)); }
equalIgnoringCase(const AtomicString & a,const String & b)207 inline bool equalIgnoringCase(const AtomicString& a, const String& b) { return equalIgnoringCase(a.impl(), b.impl()); }
equalIgnoringCase(const LChar * a,const AtomicString & b)208 inline bool equalIgnoringCase(const LChar* a, const AtomicString& b) { return equalIgnoringCase(a, b.impl()); }
equalIgnoringCase(const char * a,const AtomicString & b)209 inline bool equalIgnoringCase(const char* a, const AtomicString& b) { return equalIgnoringCase(reinterpret_cast<const LChar*>(a), b.impl()); }
equalIgnoringCase(const String & a,const AtomicString & b)210 inline 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.
214 WTF_EXPORT extern const AtomicString& nullAtom;
215 WTF_EXPORT extern const AtomicString& emptyAtom;
216 WTF_EXPORT extern const AtomicString& starAtom;
217 WTF_EXPORT extern const AtomicString& xmlAtom;
218 WTF_EXPORT extern const AtomicString& xmlnsAtom;
219 WTF_EXPORT extern const AtomicString& xlinkAtom;
220
fromUTF8(const char * characters,size_t length)221 inline 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
fromUTF8(const char * characters)230 inline 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
240 template<typename T> struct DefaultHash;
241 template<> struct DefaultHash<AtomicString> {
242 typedef AtomicStringHash Hash;
243 };
244
245 } // namespace WTF
246
247 WTF_ALLOW_MOVE_INIT_AND_COMPARE_WITH_MEM_FUNCTIONS(AtomicString);
248
249 using WTF::AtomicString;
250 using WTF::nullAtom;
251 using WTF::emptyAtom;
252 using WTF::starAtom;
253 using WTF::xmlAtom;
254 using WTF::xmlnsAtom;
255 using WTF::xlinkAtom;
256
257 #include "wtf/text/StringConcatenate.h"
258 #endif // AtomicString_h
259