1/*
2 * Copyright (C) 2004-2019 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 final {
38 WTF_MAKE_FAST_ALLOCATED;
39public:
40 WTF_EXPORT_PRIVATE static void init();
41
42 AtomString();
43 AtomString(const LChar*);
44 AtomString(const char*);
45 AtomString(const LChar*, unsigned length);
46 AtomString(const UChar*, unsigned length);
47 AtomString(const UChar*);
48
49 template<size_t inlineCapacity>
50 explicit AtomString(const Vector<UChar, inlineCapacity>& characters)
51 : m_string(AtomStringImpl::add(characters.data(), characters.size()))
52 {
53 }
54
55 AtomString(AtomStringImpl*);
56 AtomString(RefPtr<AtomStringImpl>&&);
57 AtomString(const StaticStringImpl*);
58 AtomString(StringImpl*);
59 AtomString(const String&);
60 AtomString(StringImpl* baseString, unsigned start, unsigned length);
61
62 // FIXME: AtomString doesn’t always have AtomStringImpl, so one of those two names needs to change.
63 AtomString(UniquedStringImpl* uid);
64
65 enum ConstructFromLiteralTag { ConstructFromLiteral };
66 AtomString(const char* characters, unsigned length, ConstructFromLiteralTag)
67 : m_string(AtomStringImpl::addLiteral(characters, length))
68 {
69 }
70
71 template<unsigned characterCount> ALWAYS_INLINE AtomString(const char (&characters)[characterCount], ConstructFromLiteralTag)
72 : m_string(AtomStringImpl::addLiteral(characters, characterCount - 1))
73 {
74 COMPILE_ASSERT(characterCount > 1, AtomStringFromLiteralNotEmpty);
75 COMPILE_ASSERT((characterCount - 1 <= ((unsigned(~0) - sizeof(StringImpl)) / sizeof(LChar))), AtomStringFromLiteralCannotOverflow);
76 }
77
78 // We have to declare the copy constructor and copy assignment operator as well, otherwise
79 // they'll be implicitly deleted by adding the move constructor and move assignment operator.
80 AtomString(const AtomString& other) : m_string(other.m_string) { }
81 AtomString(AtomString&& other) : m_string(WTFMove(other.m_string)) { }
82 AtomString& operator=(const AtomString& other) { m_string = other.m_string; return *this; }
83 AtomString& operator=(AtomString&& other) { m_string = WTFMove(other.m_string); return *this; }
84
85 // Hash table deleted values, which are only constructed and never copied or destroyed.
86 AtomString(WTF::HashTableDeletedValueType) : m_string(WTF::HashTableDeletedValue) { }
87 bool isHashTableDeletedValue() const { return m_string.isHashTableDeletedValue(); }
88
89 unsigned existingHash() const { return isNull() ? 0 : impl()->existingHash(); }
90
91 operator const String&() const { return m_string; }
92 const String& string() const { return m_string; };
93
94 // FIXME: What guarantees this isn't a SymbolImpl rather than an AtomStringImpl?
95 AtomStringImpl* impl() const { return static_cast<AtomStringImpl*>(m_string.impl()); }
96
97 bool is8Bit() const { return m_string.is8Bit(); }
98 const LChar* characters8() const { return m_string.characters8(); }
99 const UChar* characters16() const { return m_string.characters16(); }
100 unsigned length() const { return m_string.length(); }
101
102 UChar operator[](unsigned int i) const { return m_string[i]; }
103
104 WTF_EXPORT_PRIVATE static AtomString number(int);
105 WTF_EXPORT_PRIVATE static AtomString number(unsigned);
106 WTF_EXPORT_PRIVATE static AtomString number(unsigned long);
107 WTF_EXPORT_PRIVATE static AtomString number(unsigned long long);
108 WTF_EXPORT_PRIVATE static AtomString number(float);
109 WTF_EXPORT_PRIVATE static AtomString number(double);
110 // 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.
111
112 bool contains(UChar character) const { return m_string.contains(character); }
113 bool contains(const LChar* string) const { return m_string.contains(string); }
114 bool contains(const String& string) const { return m_string.contains(string); }
115 bool containsIgnoringASCIICase(const String& string) const { return m_string.containsIgnoringASCIICase(string); }
116
117 size_t find(UChar character, unsigned start = 0) const { return m_string.find(character, start); }
118 size_t find(const LChar* string, unsigned start = 0) const { return m_string.find(string, start); }
119 size_t find(const String& string, unsigned start = 0) const { return m_string.find(string, start); }
120 size_t findIgnoringASCIICase(const String& string) const { return m_string.findIgnoringASCIICase(string); }
121 size_t findIgnoringASCIICase(const String& string, unsigned startOffset) const { return m_string.findIgnoringASCIICase(string, startOffset); }
122 size_t find(CodeUnitMatchFunction matchFunction, unsigned start = 0) const { return m_string.find(matchFunction, start); }
123
124 bool startsWith(const String& string) const { return m_string.startsWith(string); }
125 bool startsWithIgnoringASCIICase(const String& string) const { return m_string.startsWithIgnoringASCIICase(string); }
126 bool startsWith(UChar character) const { return m_string.startsWith(character); }
127 template<unsigned matchLength> bool startsWith(const char (&prefix)[matchLength]) const { return m_string.startsWith<matchLength>(prefix); }
128
129 bool endsWith(const String& string) const { return m_string.endsWith(string); }
130 bool endsWithIgnoringASCIICase(const String& string) const { return m_string.endsWithIgnoringASCIICase(string); }
131 bool endsWith(UChar character) const { return m_string.endsWith(character); }
132 template<unsigned matchLength> bool endsWith(const char (&prefix)[matchLength]) const { return m_string.endsWith<matchLength>(prefix); }
133
134 WTF_EXPORT_PRIVATE AtomString convertToASCIILowercase() const;
135 WTF_EXPORT_PRIVATE AtomString convertToASCIIUppercase() const;
136
137 int toInt(bool* ok = nullptr) const { return m_string.toInt(ok); }
138 double toDouble(bool* ok = nullptr) const { return m_string.toDouble(ok); }
139 float toFloat(bool* ok = nullptr) const { return m_string.toFloat(ok); }
140 bool percentage(int& p) const { return m_string.percentage(p); }
141
142 bool isNull() const { return m_string.isNull(); }
143 bool isEmpty() const { return m_string.isEmpty(); }
144
145#if USE(CF)
146 AtomString(CFStringRef);
147#endif
148
149#ifdef __OBJC__
150 AtomString(NSString *);
151 operator NSString *() const { return m_string; }
152#endif
153
154#if OS(WINDOWS) && U_ICU_VERSION_MAJOR_NUM >= 59
155 AtomString(const wchar_t* characters, unsigned length)
156 : AtomString(ucharFrom(characters), length) { }
157
158 AtomString(const wchar_t* characters)
159 : AtomString(ucharFrom(characters)) { }
160#endif
161
162 // AtomString::fromUTF8 will return a null string if the input data contains invalid UTF-8 sequences.
163 static AtomString fromUTF8(const char*, size_t);
164 static AtomString fromUTF8(const char*);
165
166#ifndef NDEBUG
167 void show() const;
168#endif
169
170private:
171 // The explicit constructors with AtomString::ConstructFromLiteral must be used for literals.
172 AtomString(ASCIILiteral);
173
174 enum class CaseConvertType { Upper, Lower };
175 template<CaseConvertType> AtomString convertASCIICase() const;
176
177 WTF_EXPORT_PRIVATE static AtomString fromUTF8Internal(const char*, const char*);
178
179 String m_string;
180};
181
182static_assert(sizeof(AtomString) == sizeof(String), "AtomString and String must be the 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 atom 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::nullAtom;
364using WTF::emptyAtom;
365using WTF::starAtom;
366using WTF::xmlAtom;
367using WTF::xmlnsAtom;
368
369#include <wtf/text/StringConcatenate.h>
370