1/*
2 * Copyright (C) 2004-2017 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#pragma once
22
23#include <utility>
24#include <wtf/NeverDestroyed.h>
25#include <wtf/text/AtomStringImpl.h>
26#include <wtf/text/IntegerToStringConversion.h>
27#include <wtf/text/WTFString.h>
28
29#if OS(WINDOWS)
30#include <wtf/text/win/WCharStringExtras.h>
31#endif
32
33namespace WTF {
34
35struct AtomStringHash;
36
37class AtomString {
38public:
39 WTF_EXPORT_PRIVATE static void init();
40
41 AtomString();
42 AtomString(const LChar*);
43 AtomString(const char*);
44 AtomString(const LChar*, unsigned length);
45 AtomString(const UChar*, unsigned length);
46 AtomString(const UChar*);
47
48 template<size_t inlineCapacity>
49 explicit AtomString(const Vector<UChar, inlineCapacity>& characters)
50 : m_string(AtomStringImpl::add(characters.data(), characters.size()))
51 {
52 }
53
54 AtomString(AtomStringImpl*);
55 AtomString(RefPtr<AtomStringImpl>&&);
56 AtomString(const StaticStringImpl*);
57 AtomString(StringImpl*);
58 AtomString(const String&);
59 AtomString(StringImpl* baseString, unsigned start, unsigned length);
60
61 // FIXME: AtomString doesn’t always have AtomStringImpl, so one of those two names needs to change.
62 AtomString(UniquedStringImpl* uid);
63
64 enum ConstructFromLiteralTag { ConstructFromLiteral };
65 AtomString(const char* characters, unsigned length, ConstructFromLiteralTag)
66 : m_string(AtomStringImpl::addLiteral(characters, length))
67 {
68 }
69
70 template<unsigned characterCount> ALWAYS_INLINE AtomString(const char (&characters)[characterCount], ConstructFromLiteralTag)
71 : m_string(AtomStringImpl::addLiteral(characters, characterCount - 1))
72 {
73 COMPILE_ASSERT(characterCount > 1, AtomStringFromLiteralNotEmpty);
74 COMPILE_ASSERT((characterCount - 1 <= ((unsigned(~0) - sizeof(StringImpl)) / sizeof(LChar))), AtomStringFromLiteralCannotOverflow);
75 }
76
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 AtomString(const AtomString& other) : m_string(other.m_string) { }
80 AtomString(AtomString&& other) : m_string(WTFMove(other.m_string)) { }
81 AtomString& operator=(const AtomString& other) { m_string = other.m_string; return *this; }
82 AtomString& operator=(AtomString&& other) { m_string = WTFMove(other.m_string); return *this; }
83
84 // Hash table deleted values, which are only constructed and never copied or destroyed.
85 AtomString(WTF::HashTableDeletedValueType) : m_string(WTF::HashTableDeletedValue) { }
86 bool isHashTableDeletedValue() const { return m_string.isHashTableDeletedValue(); }
87
88 unsigned existingHash() const { return isNull() ? 0 : impl()->existingHash(); }
89
90 operator const String&() const { return m_string; }
91 const String& string() const { return m_string; };
92
93 AtomStringImpl* impl() const { return static_cast<AtomStringImpl *>(m_string.impl()); }
94
95 bool is8Bit() const { return m_string.is8Bit(); }
96 const LChar* characters8() const { return m_string.characters8(); }
97 const UChar* characters16() const { return m_string.characters16(); }
98 unsigned length() const { return m_string.length(); }
99
100 UChar operator[](unsigned int i) const { return m_string[i]; }
101
102 WTF_EXPORT_PRIVATE static AtomString number(int);
103 WTF_EXPORT_PRIVATE static AtomString number(unsigned);
104 WTF_EXPORT_PRIVATE static AtomString number(unsigned long);
105 WTF_EXPORT_PRIVATE static AtomString number(unsigned long long);
106 WTF_EXPORT_PRIVATE static AtomString number(float);
107 WTF_EXPORT_PRIVATE static AtomString number(double);
108 // If we need more overloads of the number function, we can add all the others that String has, but these seem to do for now.
109
110 bool contains(UChar character) const { return m_string.contains(character); }
111 bool contains(const LChar* string) const { return m_string.contains(string); }
112 bool contains(const String& string) const { return m_string.contains(string); }
113 bool containsIgnoringASCIICase(const String& string) const { return m_string.containsIgnoringASCIICase(string); }
114
115 size_t find(UChar character, unsigned start = 0) const { return m_string.find(character, start); }
116 size_t find(const LChar* string, unsigned start = 0) const { return m_string.find(string, start); }
117 size_t find(const String& string, unsigned start = 0) const { return m_string.find(string, start); }
118 size_t findIgnoringASCIICase(const String& string) const { return m_string.findIgnoringASCIICase(string); }
119 size_t findIgnoringASCIICase(const String& string, unsigned startOffset) const { return m_string.findIgnoringASCIICase(string, startOffset); }
120 size_t find(CodeUnitMatchFunction matchFunction, unsigned start = 0) const { return m_string.find(matchFunction, start); }
121
122 bool startsWith(const String& string) const { return m_string.startsWith(string); }
123 bool startsWithIgnoringASCIICase(const String& string) const { return m_string.startsWithIgnoringASCIICase(string); }
124 bool startsWith(UChar character) const { return m_string.startsWith(character); }
125 template<unsigned matchLength> bool startsWith(const char (&prefix)[matchLength]) const { return m_string.startsWith<matchLength>(prefix); }
126
127 bool endsWith(const String& string) const { return m_string.endsWith(string); }
128 bool endsWithIgnoringASCIICase(const String& string) const { return m_string.endsWithIgnoringASCIICase(string); }
129 bool endsWith(UChar character) const { return m_string.endsWith(character); }
130 template<unsigned matchLength> bool endsWith(const char (&prefix)[matchLength]) const { return m_string.endsWith<matchLength>(prefix); }
131
132 WTF_EXPORT_PRIVATE AtomString convertToASCIILowercase() const;
133 WTF_EXPORT_PRIVATE AtomString convertToASCIIUppercase() const;
134
135 int toInt(bool* ok = 0) const { return m_string.toInt(ok); }
136 double toDouble(bool* ok = 0) const { return m_string.toDouble(ok); }
137 float toFloat(bool* ok = 0) const { return m_string.toFloat(ok); }
138 bool percentage(int& p) const { return m_string.percentage(p); }
139
140 bool isNull() const { return m_string.isNull(); }
141 bool isEmpty() const { return m_string.isEmpty(); }
142
143#if USE(CF)
144 AtomString(CFStringRef);
145#endif
146
147#ifdef __OBJC__
148 AtomString(NSString*);
149 operator NSString*() const { return m_string; }
150#endif
151
152#if OS(WINDOWS) && U_ICU_VERSION_MAJOR_NUM >= 59
153 AtomString(const wchar_t* characters, unsigned length)
154 : AtomString(ucharFrom(characters), length) { }
155
156 AtomString(const wchar_t* characters)
157 : AtomString(ucharFrom(characters)) { }
158#endif
159
160 // AtomString::fromUTF8 will return a null string if the input data contains invalid UTF-8 sequences.
161 static AtomString fromUTF8(const char*, size_t);
162 static AtomString fromUTF8(const char*);
163
164#ifndef NDEBUG
165 void show() const;
166#endif
167
168private:
169 // The explicit constructors with AtomString::ConstructFromLiteral must be used for literals.
170 AtomString(ASCIILiteral);
171
172 enum class CaseConvertType { Upper, Lower };
173 template<CaseConvertType> AtomString convertASCIICase() const;
174
175 WTF_EXPORT_PRIVATE static AtomString fromUTF8Internal(const char*, const char*);
176
177 String m_string;
178};
179
180using AtomicString = AtomString;
181
182static_assert(sizeof(AtomString) == sizeof(String), "AtomString and String must be same size!");
183
184inline bool operator==(const AtomString& a, const AtomString& b) { return a.impl() == b.impl(); }
185bool operator==(const AtomString&, const LChar*);
186inline bool operator==(const AtomString& a, const char* b) { return WTF::equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
187inline bool operator==(const AtomString& a, const Vector<UChar>& b) { return a.impl() && equal(a.impl(), b.data(), b.size()); }
188inline bool operator==(const AtomString& a, const String& b) { return equal(a.impl(), b.impl()); }
189inline bool operator==(const LChar* a, const AtomString& b) { return b == a; }
190inline bool operator==(const String& a, const AtomString& b) { return equal(a.impl(), b.impl()); }
191inline bool operator==(const Vector<UChar>& a, const AtomString& b) { return b == a; }
192
193inline bool operator!=(const AtomString& a, const AtomString& b) { return a.impl() != b.impl(); }
194inline bool operator!=(const AtomString& a, const LChar* b) { return !(a == b); }
195inline bool operator!=(const AtomString& a, const char* b) { return !(a == b); }
196inline bool operator!=(const AtomString& a, const String& b) { return !equal(a.impl(), b.impl()); }
197inline bool operator!=(const AtomString& a, const Vector<UChar>& b) { return !(a == b); }
198inline bool operator!=(const LChar* a, const AtomString& b) { return !(b == a); }
199inline bool operator!=(const String& a, const AtomString& b) { return !equal(a.impl(), b.impl()); }
200inline bool operator!=(const Vector<UChar>& a, const AtomString& b) { return !(a == b); }
201
202bool equalIgnoringASCIICase(const AtomString&, const AtomString&);
203bool equalIgnoringASCIICase(const AtomString&, const String&);
204bool equalIgnoringASCIICase(const String&, const AtomString&);
205bool equalIgnoringASCIICase(const AtomString&, const char*);
206
207template<unsigned length> bool equalLettersIgnoringASCIICase(const AtomString&, const char (&lowercaseLetters)[length]);
208
209inline AtomString::AtomString()
210{
211}
212
213inline AtomString::AtomString(const LChar* string)
214 : m_string(AtomStringImpl::add(string))
215{
216}
217
218inline AtomString::AtomString(const char* string)
219 : m_string(AtomStringImpl::add(string))
220{
221}
222
223inline AtomString::AtomString(const LChar* string, unsigned length)
224 : m_string(AtomStringImpl::add(string, length))
225{
226}
227
228inline AtomString::AtomString(const UChar* string, unsigned length)
229 : m_string(AtomStringImpl::add(string, length))
230{
231}
232
233inline AtomString::AtomString(const UChar* string)
234 : m_string(AtomStringImpl::add(string))
235{
236}
237
238inline AtomString::AtomString(AtomStringImpl* string)
239 : m_string(string)
240{
241}
242
243inline AtomString::AtomString(RefPtr<AtomStringImpl>&& string)
244 : m_string(WTFMove(string))
245{
246}
247
248inline AtomString::AtomString(StringImpl* string)
249 : m_string(AtomStringImpl::add(string))
250{
251}
252
253inline AtomString::AtomString(const StaticStringImpl* string)
254 : m_string(AtomStringImpl::add(string))
255{
256}
257
258inline AtomString::AtomString(const String& string)
259 : m_string(AtomStringImpl::add(string.impl()))
260{
261}
262
263inline AtomString::AtomString(StringImpl* baseString, unsigned start, unsigned length)
264 : m_string(AtomStringImpl::add(baseString, start, length))
265{
266}
267
268inline AtomString::AtomString(UniquedStringImpl* uid)
269 : m_string(uid)
270{
271}
272
273#if USE(CF)
274
275inline AtomString::AtomString(CFStringRef string)
276 : m_string(AtomStringImpl::add(string))
277{
278}
279
280#endif
281
282#ifdef __OBJC__
283
284inline AtomString::AtomString(NSString* string)
285 : m_string(AtomStringImpl::add((__bridge CFStringRef)string))
286{
287}
288
289#endif
290
291// Define external global variables for the commonly used atomic strings.
292// These are only usable from the main thread.
293extern WTF_EXPORT_PRIVATE LazyNeverDestroyed<AtomString> nullAtomData;
294extern WTF_EXPORT_PRIVATE LazyNeverDestroyed<AtomString> emptyAtomData;
295extern WTF_EXPORT_PRIVATE LazyNeverDestroyed<AtomString> starAtomData;
296extern WTF_EXPORT_PRIVATE LazyNeverDestroyed<AtomString> xmlAtomData;
297extern WTF_EXPORT_PRIVATE LazyNeverDestroyed<AtomString> xmlnsAtomData;
298
299inline const AtomString& nullAtom() { return nullAtomData.get(); }
300inline const AtomString& emptyAtom() { return emptyAtomData.get(); }
301inline const AtomString& starAtom() { return starAtomData.get(); }
302inline const AtomString& xmlAtom() { return xmlAtomData.get(); }
303inline const AtomString& xmlnsAtom() { return xmlnsAtomData.get(); }
304
305inline AtomString AtomString::fromUTF8(const char* characters, size_t length)
306{
307 if (!characters)
308 return nullAtom();
309 if (!length)
310 return emptyAtom();
311 return fromUTF8Internal(characters, characters + length);
312}
313
314inline AtomString AtomString::fromUTF8(const char* characters)
315{
316 if (!characters)
317 return nullAtom();
318 if (!*characters)
319 return emptyAtom();
320 return fromUTF8Internal(characters, nullptr);
321}
322
323// AtomStringHash is the default hash for AtomString
324template<typename T> struct DefaultHash;
325template<> struct DefaultHash<AtomString> {
326 typedef AtomStringHash Hash;
327};
328
329template<unsigned length> inline bool equalLettersIgnoringASCIICase(const AtomString& string, const char (&lowercaseLetters)[length])
330{
331 return equalLettersIgnoringASCIICase(string.string(), lowercaseLetters);
332}
333
334inline bool equalIgnoringASCIICase(const AtomString& a, const AtomString& b)
335{
336 return equalIgnoringASCIICase(a.string(), b.string());
337}
338
339inline bool equalIgnoringASCIICase(const AtomString& a, const String& b)
340{
341 return equalIgnoringASCIICase(a.string(), b);
342}
343
344inline bool equalIgnoringASCIICase(const String& a, const AtomString& b)
345{
346 return equalIgnoringASCIICase(a, b.string());
347}
348
349inline bool equalIgnoringASCIICase(const AtomString& a, const char* b)
350{
351 return equalIgnoringASCIICase(a.string(), b);
352}
353
354template<> struct IntegerToStringConversionTrait<AtomString> {
355 using ReturnType = AtomString;
356 using AdditionalArgumentType = void;
357 static AtomString flush(LChar* characters, unsigned length, void*) { return { characters, length }; }
358};
359
360} // namespace WTF
361
362using WTF::AtomString;
363using WTF::AtomicString;
364using WTF::nullAtom;
365using WTF::emptyAtom;
366using WTF::starAtom;
367using WTF::xmlAtom;
368using WTF::xmlnsAtom;
369
370#include <wtf/text/StringConcatenate.h>
371