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 {
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 // FIXME: What guarantees this isn't a SymbolImpl rather than an AtomStringImpl?
94 AtomStringImpl* impl() const { return static_cast<AtomStringImpl*>(m_string.impl()); }
95
96 bool is8Bit() const { return m_string.is8Bit(); }
97 const LChar* characters8() const { return m_string.characters8(); }
98 const UChar* characters16() const { return m_string.characters16(); }
99 unsigned length() const { return m_string.length(); }
100
101 UChar operator[](unsigned int i) const { return m_string[i]; }
102
103 WTF_EXPORT_PRIVATE static AtomString number(int);
104 WTF_EXPORT_PRIVATE static AtomString number(unsigned);
105 WTF_EXPORT_PRIVATE static AtomString number(unsigned long);
106 WTF_EXPORT_PRIVATE static AtomString number(unsigned long long);
107 WTF_EXPORT_PRIVATE static AtomString number(float);
108 WTF_EXPORT_PRIVATE static AtomString number(double);
109 // 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.
110
111 bool contains(UChar character) const { return m_string.contains(character); }
112 bool contains(const LChar* string) const { return m_string.contains(string); }
113 bool contains(const String& string) const { return m_string.contains(string); }
114 bool containsIgnoringASCIICase(const String& string) const { return m_string.containsIgnoringASCIICase(string); }
115
116 size_t find(UChar character, unsigned start = 0) const { return m_string.find(character, start); }
117 size_t find(const LChar* string, unsigned start = 0) const { return m_string.find(string, start); }
118 size_t find(const String& string, unsigned start = 0) const { return m_string.find(string, start); }
119 size_t findIgnoringASCIICase(const String& string) const { return m_string.findIgnoringASCIICase(string); }
120 size_t findIgnoringASCIICase(const String& string, unsigned startOffset) const { return m_string.findIgnoringASCIICase(string, startOffset); }
121 size_t find(CodeUnitMatchFunction matchFunction, unsigned start = 0) const { return m_string.find(matchFunction, start); }
122
123 bool startsWith(const String& string) const { return m_string.startsWith(string); }
124 bool startsWithIgnoringASCIICase(const String& string) const { return m_string.startsWithIgnoringASCIICase(string); }
125 bool startsWith(UChar character) const { return m_string.startsWith(character); }
126 template<unsigned matchLength> bool startsWith(const char (&prefix)[matchLength]) const { return m_string.startsWith<matchLength>(prefix); }
127
128 bool endsWith(const String& string) const { return m_string.endsWith(string); }
129 bool endsWithIgnoringASCIICase(const String& string) const { return m_string.endsWithIgnoringASCIICase(string); }
130 bool endsWith(UChar character) const { return m_string.endsWith(character); }
131 template<unsigned matchLength> bool endsWith(const char (&prefix)[matchLength]) const { return m_string.endsWith<matchLength>(prefix); }
132
133 WTF_EXPORT_PRIVATE AtomString convertToASCIILowercase() const;
134 WTF_EXPORT_PRIVATE AtomString convertToASCIIUppercase() const;
135
136 int toInt(bool* ok = nullptr) const { return m_string.toInt(ok); }
137 double toDouble(bool* ok = nullptr) const { return m_string.toDouble(ok); }
138 float toFloat(bool* ok = nullptr) 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#if USE(CF)
145 AtomString(CFStringRef);
146#endif
147
148#ifdef __OBJC__
149 AtomString(NSString *);
150 operator NSString *() const { return m_string; }
151#endif
152
153#if OS(WINDOWS) && U_ICU_VERSION_MAJOR_NUM >= 59
154 AtomString(const wchar_t* characters, unsigned length)
155 : AtomString(ucharFrom(characters), length) { }
156
157 AtomString(const wchar_t* characters)
158 : AtomString(ucharFrom(characters)) { }
159#endif
160
161 // AtomString::fromUTF8 will return a null string if the input data contains invalid UTF-8 sequences.
162 static AtomString fromUTF8(const char*, size_t);
163 static AtomString fromUTF8(const char*);
164
165#ifndef NDEBUG
166 void show() const;
167#endif
168
169private:
170 // The explicit constructors with AtomString::ConstructFromLiteral must be used for literals.
171 AtomString(ASCIILiteral);
172
173 enum class CaseConvertType { Upper, Lower };
174 template<CaseConvertType> AtomString convertASCIICase() const;
175
176 WTF_EXPORT_PRIVATE static AtomString fromUTF8Internal(const char*, const char*);
177
178 String m_string;
179};
180
181static_assert(sizeof(AtomString) == sizeof(String), "AtomString and String must be the same size!");
182
183inline bool operator==(const AtomString& a, const AtomString& b) { return a.impl() == b.impl(); }
184bool operator==(const AtomString&, const LChar*);
185inline bool operator==(const AtomString& a, const char* b) { return WTF::equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
186inline bool operator==(const AtomString& a, const Vector<UChar>& b) { return a.impl() && equal(a.impl(), b.data(), b.size()); }
187inline bool operator==(const AtomString& a, const String& b) { return equal(a.impl(), b.impl()); }
188inline bool operator==(const LChar* a, const AtomString& b) { return b == a; }
189inline bool operator==(const String& a, const AtomString& b) { return equal(a.impl(), b.impl()); }
190inline bool operator==(const Vector<UChar>& a, const AtomString& b) { return b == a; }
191
192inline bool operator!=(const AtomString& a, const AtomString& b) { return a.impl() != b.impl(); }
193inline bool operator!=(const AtomString& a, const LChar* b) { return !(a == b); }
194inline bool operator!=(const AtomString& a, const char* b) { return !(a == b); }
195inline bool operator!=(const AtomString& a, const String& b) { return !equal(a.impl(), b.impl()); }
196inline bool operator!=(const AtomString& a, const Vector<UChar>& b) { return !(a == b); }
197inline bool operator!=(const LChar* a, const AtomString& b) { return !(b == a); }
198inline bool operator!=(const String& a, const AtomString& b) { return !equal(a.impl(), b.impl()); }
199inline bool operator!=(const Vector<UChar>& a, const AtomString& b) { return !(a == b); }
200
201bool equalIgnoringASCIICase(const AtomString&, const AtomString&);
202bool equalIgnoringASCIICase(const AtomString&, const String&);
203bool equalIgnoringASCIICase(const String&, const AtomString&);
204bool equalIgnoringASCIICase(const AtomString&, const char*);
205
206template<unsigned length> bool equalLettersIgnoringASCIICase(const AtomString&, const char (&lowercaseLetters)[length]);
207
208inline AtomString::AtomString()
209{
210}
211
212inline AtomString::AtomString(const LChar* string)
213 : m_string(AtomStringImpl::add(string))
214{
215}
216
217inline AtomString::AtomString(const char* string)
218 : m_string(AtomStringImpl::add(string))
219{
220}
221
222inline AtomString::AtomString(const LChar* string, unsigned length)
223 : m_string(AtomStringImpl::add(string, length))
224{
225}
226
227inline AtomString::AtomString(const UChar* string, unsigned length)
228 : m_string(AtomStringImpl::add(string, length))
229{
230}
231
232inline AtomString::AtomString(const UChar* string)
233 : m_string(AtomStringImpl::add(string))
234{
235}
236
237inline AtomString::AtomString(AtomStringImpl* string)
238 : m_string(string)
239{
240}
241
242inline AtomString::AtomString(RefPtr<AtomStringImpl>&& string)
243 : m_string(WTFMove(string))
244{
245}
246
247inline AtomString::AtomString(StringImpl* string)
248 : m_string(AtomStringImpl::add(string))
249{
250}
251
252inline AtomString::AtomString(const StaticStringImpl* string)
253 : m_string(AtomStringImpl::add(string))
254{
255}
256
257inline AtomString::AtomString(const String& string)
258 : m_string(AtomStringImpl::add(string.impl()))
259{
260}
261
262inline AtomString::AtomString(StringImpl* baseString, unsigned start, unsigned length)
263 : m_string(AtomStringImpl::add(baseString, start, length))
264{
265}
266
267inline AtomString::AtomString(UniquedStringImpl* uid)
268 : m_string(uid)
269{
270}
271
272#if USE(CF)
273
274inline AtomString::AtomString(CFStringRef string)
275 : m_string(AtomStringImpl::add(string))
276{
277}
278
279#endif
280
281#ifdef __OBJC__
282
283inline AtomString::AtomString(NSString *string)
284 : m_string(AtomStringImpl::add((__bridge CFStringRef)string))
285{
286}
287
288#endif
289
290// Define external global variables for the commonly used atom strings.
291// These are only usable from the main thread.
292extern WTF_EXPORT_PRIVATE LazyNeverDestroyed<AtomString> nullAtomData;
293extern WTF_EXPORT_PRIVATE LazyNeverDestroyed<AtomString> emptyAtomData;
294extern WTF_EXPORT_PRIVATE LazyNeverDestroyed<AtomString> starAtomData;
295extern WTF_EXPORT_PRIVATE LazyNeverDestroyed<AtomString> xmlAtomData;
296extern WTF_EXPORT_PRIVATE LazyNeverDestroyed<AtomString> xmlnsAtomData;
297
298inline const AtomString& nullAtom() { return nullAtomData.get(); }
299inline const AtomString& emptyAtom() { return emptyAtomData.get(); }
300inline const AtomString& starAtom() { return starAtomData.get(); }
301inline const AtomString& xmlAtom() { return xmlAtomData.get(); }
302inline const AtomString& xmlnsAtom() { return xmlnsAtomData.get(); }
303
304inline AtomString AtomString::fromUTF8(const char* characters, size_t length)
305{
306 if (!characters)
307 return nullAtom();
308 if (!length)
309 return emptyAtom();
310 return fromUTF8Internal(characters, characters + length);
311}
312
313inline AtomString AtomString::fromUTF8(const char* characters)
314{
315 if (!characters)
316 return nullAtom();
317 if (!*characters)
318 return emptyAtom();
319 return fromUTF8Internal(characters, nullptr);
320}
321
322// AtomStringHash is the default hash for AtomString
323template<typename T> struct DefaultHash;
324template<> struct DefaultHash<AtomString> {
325 typedef AtomStringHash Hash;
326};
327
328template<unsigned length> inline bool equalLettersIgnoringASCIICase(const AtomString& string, const char (&lowercaseLetters)[length])
329{
330 return equalLettersIgnoringASCIICase(string.string(), lowercaseLetters);
331}
332
333inline bool equalIgnoringASCIICase(const AtomString& a, const AtomString& b)
334{
335 return equalIgnoringASCIICase(a.string(), b.string());
336}
337
338inline bool equalIgnoringASCIICase(const AtomString& a, const String& b)
339{
340 return equalIgnoringASCIICase(a.string(), b);
341}
342
343inline bool equalIgnoringASCIICase(const String& a, const AtomString& b)
344{
345 return equalIgnoringASCIICase(a, b.string());
346}
347
348inline bool equalIgnoringASCIICase(const AtomString& a, const char* b)
349{
350 return equalIgnoringASCIICase(a.string(), b);
351}
352
353template<> struct IntegerToStringConversionTrait<AtomString> {
354 using ReturnType = AtomString;
355 using AdditionalArgumentType = void;
356 static AtomString flush(LChar* characters, unsigned length, void*) { return { characters, length }; }
357};
358
359} // namespace WTF
360
361using WTF::AtomString;
362using WTF::nullAtom;
363using WTF::emptyAtom;
364using WTF::starAtom;
365using WTF::xmlAtom;
366using WTF::xmlnsAtom;
367
368#include <wtf/text/StringConcatenate.h>
369