1/*
2 * (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004-2019 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#pragma once
23
24// This file would be called String.h, but that conflicts with <string.h>
25// on systems without case-sensitive file systems.
26
27#include <stdarg.h>
28#include <wtf/Function.h>
29#include <wtf/text/ASCIILiteral.h>
30#include <wtf/text/IntegerToStringConversion.h>
31#include <wtf/text/StringImpl.h>
32
33#ifdef __OBJC__
34#include <objc/objc.h>
35#endif
36
37#if OS(WINDOWS)
38#include <wtf/text/win/WCharStringExtras.h>
39#endif
40
41namespace WTF {
42
43// Declarations of string operations
44
45WTF_EXPORT_PRIVATE int charactersToIntStrict(const LChar*, size_t, bool* ok = nullptr, int base = 10);
46WTF_EXPORT_PRIVATE int charactersToIntStrict(const UChar*, size_t, bool* ok = nullptr, int base = 10);
47WTF_EXPORT_PRIVATE unsigned charactersToUIntStrict(const LChar*, size_t, bool* ok = nullptr, int base = 10);
48WTF_EXPORT_PRIVATE unsigned charactersToUIntStrict(const UChar*, size_t, bool* ok = nullptr, int base = 10);
49int64_t charactersToInt64Strict(const LChar*, size_t, bool* ok = nullptr, int base = 10);
50int64_t charactersToInt64Strict(const UChar*, size_t, bool* ok = nullptr, int base = 10);
51WTF_EXPORT_PRIVATE uint64_t charactersToUInt64Strict(const LChar*, size_t, bool* ok = nullptr, int base = 10);
52WTF_EXPORT_PRIVATE uint64_t charactersToUInt64Strict(const UChar*, size_t, bool* ok = nullptr, int base = 10);
53intptr_t charactersToIntPtrStrict(const LChar*, size_t, bool* ok = nullptr, int base = 10);
54intptr_t charactersToIntPtrStrict(const UChar*, size_t, bool* ok = nullptr, int base = 10);
55
56WTF_EXPORT_PRIVATE int charactersToInt(const LChar*, size_t, bool* ok = nullptr); // ignores trailing garbage
57WTF_EXPORT_PRIVATE int charactersToInt(const UChar*, size_t, bool* ok = nullptr); // ignores trailing garbage
58unsigned charactersToUInt(const LChar*, size_t, bool* ok = nullptr); // ignores trailing garbage
59unsigned charactersToUInt(const UChar*, size_t, bool* ok = nullptr); // ignores trailing garbage
60int64_t charactersToInt64(const LChar*, size_t, bool* ok = nullptr); // ignores trailing garbage
61int64_t charactersToInt64(const UChar*, size_t, bool* ok = nullptr); // ignores trailing garbage
62uint64_t charactersToUInt64(const LChar*, size_t, bool* ok = nullptr); // ignores trailing garbage
63WTF_EXPORT_PRIVATE uint64_t charactersToUInt64(const UChar*, size_t, bool* ok = nullptr); // ignores trailing garbage
64intptr_t charactersToIntPtr(const LChar*, size_t, bool* ok = nullptr); // ignores trailing garbage
65intptr_t charactersToIntPtr(const UChar*, size_t, bool* ok = nullptr); // ignores trailing garbage
66
67// FIXME: Like the strict functions above, these give false for "ok" when there is trailing garbage.
68// Like the non-strict functions above, these return the value when there is trailing garbage.
69// It would be better if these were more consistent with the above functions instead.
70WTF_EXPORT_PRIVATE double charactersToDouble(const LChar*, size_t, bool* ok = nullptr);
71WTF_EXPORT_PRIVATE double charactersToDouble(const UChar*, size_t, bool* ok = nullptr);
72WTF_EXPORT_PRIVATE float charactersToFloat(const LChar*, size_t, bool* ok = nullptr);
73WTF_EXPORT_PRIVATE float charactersToFloat(const UChar*, size_t, bool* ok = nullptr);
74WTF_EXPORT_PRIVATE float charactersToFloat(const LChar*, size_t, size_t& parsedLength);
75WTF_EXPORT_PRIVATE float charactersToFloat(const UChar*, size_t, size_t& parsedLength);
76
77template<bool isSpecialCharacter(UChar), typename CharacterType> bool isAllSpecialCharacters(const CharacterType*, size_t);
78
79enum TrailingZerosTruncatingPolicy { KeepTrailingZeros, TruncateTrailingZeros };
80
81class String final {
82 WTF_MAKE_FAST_ALLOCATED;
83public:
84 // Construct a null string, distinguishable from an empty string.
85 String() = default;
86
87 // Construct a string with UTF-16 data.
88 WTF_EXPORT_PRIVATE String(const UChar* characters, unsigned length);
89
90 // Construct a string by copying the contents of a vector. To avoid
91 // copying, consider using String::adopt instead.
92 // This method will never create a null string. Vectors with size() == 0
93 // will return the empty string.
94 // NOTE: This is different from String(vector.data(), vector.size())
95 // which will sometimes return a null string when vector.data() is null
96 // which can only occur for vectors without inline capacity.
97 // See: https://bugs.webkit.org/show_bug.cgi?id=109792
98 template<size_t inlineCapacity, typename OverflowHandler>
99 explicit String(const Vector<UChar, inlineCapacity, OverflowHandler>&);
100
101 // Construct a string with UTF-16 data, from a null-terminated source.
102 WTF_EXPORT_PRIVATE String(const UChar*);
103
104 // Construct a string with latin1 data.
105 WTF_EXPORT_PRIVATE String(const LChar* characters, unsigned length);
106 WTF_EXPORT_PRIVATE String(const char* characters, unsigned length);
107
108 // Construct a string with latin1 data, from a null-terminated source.
109 WTF_EXPORT_PRIVATE String(const LChar* characters);
110 WTF_EXPORT_PRIVATE String(const char* characters);
111
112 // Construct a string referencing an existing StringImpl.
113 String(StringImpl&);
114 String(StringImpl*);
115 String(Ref<StringImpl>&&);
116 String(RefPtr<StringImpl>&&);
117
118 String(Ref<AtomStringImpl>&&);
119 String(RefPtr<AtomStringImpl>&&);
120
121 String(StaticStringImpl&);
122 String(StaticStringImpl*);
123
124 // Construct a string from a constant string literal.
125 WTF_EXPORT_PRIVATE String(ASCIILiteral);
126
127 // Construct a string from a constant string literal.
128 // This is the "big" version: puts the length in the function call and generates bigger code.
129 enum ConstructFromLiteralTag { ConstructFromLiteral };
130 template<unsigned characterCount> String(const char (&characters)[characterCount], ConstructFromLiteralTag) : m_impl(StringImpl::createFromLiteral<characterCount>(characters)) { }
131
132 String(const String&) = default;
133 String(String&&) = default;
134 String& operator=(const String&) = default;
135 String& operator=(String&&) = default;
136
137 ALWAYS_INLINE ~String() = default;
138
139 void swap(String& o) { m_impl.swap(o.m_impl); }
140
141 static String adopt(StringBuffer<LChar>&& buffer) { return StringImpl::adopt(WTFMove(buffer)); }
142 static String adopt(StringBuffer<UChar>&& buffer) { return StringImpl::adopt(WTFMove(buffer)); }
143 template<typename CharacterType, size_t inlineCapacity, typename OverflowHandler, size_t minCapacity>
144 static String adopt(Vector<CharacterType, inlineCapacity, OverflowHandler, minCapacity>&& vector) { return StringImpl::adopt(WTFMove(vector)); }
145
146 bool isNull() const { return !m_impl; }
147 bool isEmpty() const { return !m_impl || m_impl->isEmpty(); }
148
149 StringImpl* impl() const { return m_impl.get(); }
150 RefPtr<StringImpl> releaseImpl() { return WTFMove(m_impl); }
151
152 unsigned length() const { return m_impl ? m_impl->length() : 0; }
153 const LChar* characters8() const { return m_impl ? m_impl->characters8() : nullptr; }
154 const UChar* characters16() const { return m_impl ? m_impl->characters16() : nullptr; }
155
156 // Return characters8() or characters16() depending on CharacterType.
157 template<typename CharacterType> const CharacterType* characters() const;
158
159 bool is8Bit() const { return !m_impl || m_impl->is8Bit(); }
160
161 unsigned sizeInBytes() const { return m_impl ? m_impl->length() * (is8Bit() ? sizeof(LChar) : sizeof(UChar)) : 0; }
162
163 WTF_EXPORT_PRIVATE CString ascii() const;
164 WTF_EXPORT_PRIVATE CString latin1() const;
165
166 WTF_EXPORT_PRIVATE CString utf8(ConversionMode) const;
167 WTF_EXPORT_PRIVATE CString utf8() const;
168
169 WTF_EXPORT_PRIVATE Expected<CString, UTF8ConversionError> tryGetUtf8(ConversionMode) const;
170 WTF_EXPORT_PRIVATE Expected<CString, UTF8ConversionError> tryGetUtf8() const;
171
172 UChar characterAt(unsigned index) const;
173 UChar operator[](unsigned index) const { return characterAt(index); }
174
175 WTF_EXPORT_PRIVATE static String number(int);
176 WTF_EXPORT_PRIVATE static String number(unsigned);
177 WTF_EXPORT_PRIVATE static String number(long);
178 WTF_EXPORT_PRIVATE static String number(unsigned long);
179 WTF_EXPORT_PRIVATE static String number(long long);
180 WTF_EXPORT_PRIVATE static String number(unsigned long long);
181 WTF_EXPORT_PRIVATE static String number(float);
182 WTF_EXPORT_PRIVATE static String number(double);
183
184 WTF_EXPORT_PRIVATE static String numberToStringFixedPrecision(float, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros);
185 WTF_EXPORT_PRIVATE static String numberToStringFixedPrecision(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros);
186 WTF_EXPORT_PRIVATE static String numberToStringFixedWidth(float, unsigned decimalPlaces);
187 WTF_EXPORT_PRIVATE static String numberToStringFixedWidth(double, unsigned decimalPlaces);
188
189 // Find a single character or string, also with match function & latin1 forms.
190 size_t find(UChar character, unsigned start = 0) const { return m_impl ? m_impl->find(character, start) : notFound; }
191
192 size_t find(const String& string) const { return m_impl ? m_impl->find(string.impl()) : notFound; }
193 size_t find(const String& string, unsigned start) const { return m_impl ? m_impl->find(string.impl(), start) : notFound; }
194 size_t findIgnoringASCIICase(const String& string) const { return m_impl ? m_impl->findIgnoringASCIICase(string.impl()) : notFound; }
195 size_t findIgnoringASCIICase(const String& string, unsigned startOffset) const { return m_impl ? m_impl->findIgnoringASCIICase(string.impl(), startOffset) : notFound; }
196
197 size_t find(CodeUnitMatchFunction matchFunction, unsigned start = 0) const { return m_impl ? m_impl->find(matchFunction, start) : notFound; }
198 size_t find(const LChar* string, unsigned start = 0) const { return m_impl ? m_impl->find(string, start) : notFound; }
199
200 // Find the last instance of a single character or string.
201 size_t reverseFind(UChar character, unsigned start = MaxLength) const { return m_impl ? m_impl->reverseFind(character, start) : notFound; }
202 size_t reverseFind(const String& string, unsigned start = MaxLength) const { return m_impl ? m_impl->reverseFind(string.impl(), start) : notFound; }
203
204 WTF_EXPORT_PRIVATE Vector<UChar> charactersWithNullTermination() const;
205
206 WTF_EXPORT_PRIVATE UChar32 characterStartingAt(unsigned) const;
207
208 bool contains(UChar character) const { return find(character) != notFound; }
209 bool contains(const LChar* string) const { return find(string) != notFound; }
210 bool contains(const String& string) const { return find(string) != notFound; }
211 bool containsIgnoringASCIICase(const String& string) const { return findIgnoringASCIICase(string) != notFound; }
212 bool containsIgnoringASCIICase(const String& string, unsigned startOffset) const { return findIgnoringASCIICase(string, startOffset) != notFound; }
213
214 bool startsWith(const String& string) const { return m_impl ? m_impl->startsWith(string.impl()) : string.isEmpty(); }
215 bool startsWithIgnoringASCIICase(const String& string) const { return m_impl ? m_impl->startsWithIgnoringASCIICase(string.impl()) : string.isEmpty(); }
216 bool startsWith(UChar character) const { return m_impl && m_impl->startsWith(character); }
217 template<unsigned matchLength> bool startsWith(const char (&prefix)[matchLength]) const { return m_impl ? m_impl->startsWith<matchLength>(prefix) : !matchLength; }
218 bool hasInfixStartingAt(const String& prefix, unsigned startOffset) const { return m_impl && prefix.impl() && m_impl->hasInfixStartingAt(*prefix.impl(), startOffset); }
219
220 bool endsWith(const String& string) const { return m_impl ? m_impl->endsWith(string.impl()) : string.isEmpty(); }
221 bool endsWithIgnoringASCIICase(const String& string) const { return m_impl ? m_impl->endsWithIgnoringASCIICase(string.impl()) : string.isEmpty(); }
222 bool endsWith(UChar character) const { return m_impl && m_impl->endsWith(character); }
223 bool endsWith(char character) const { return endsWith(static_cast<UChar>(character)); }
224 template<unsigned matchLength> bool endsWith(const char (&prefix)[matchLength]) const { return m_impl ? m_impl->endsWith<matchLength>(prefix) : !matchLength; }
225 bool hasInfixEndingAt(const String& suffix, unsigned endOffset) const { return m_impl && suffix.impl() && m_impl->hasInfixEndingAt(*suffix.impl(), endOffset); }
226
227 WTF_EXPORT_PRIVATE void append(const String&);
228 WTF_EXPORT_PRIVATE void append(LChar);
229 void append(char character) { append(static_cast<LChar>(character)); };
230 WTF_EXPORT_PRIVATE void append(UChar);
231 WTF_EXPORT_PRIVATE void append(const LChar*, unsigned length);
232 WTF_EXPORT_PRIVATE void append(const UChar*, unsigned length);
233 WTF_EXPORT_PRIVATE void insert(const String&, unsigned position);
234
235 String& replace(UChar target, UChar replacement);
236 String& replace(UChar target, const String& replacement);
237 String& replace(const String& target, const String& replacement);
238 String& replace(unsigned start, unsigned length, const String& replacement);
239 template<unsigned characterCount> String& replaceWithLiteral(UChar target, const char (&replacement)[characterCount]);
240
241 WTF_EXPORT_PRIVATE void truncate(unsigned length);
242 WTF_EXPORT_PRIVATE void remove(unsigned position, unsigned length = 1);
243
244 WTF_EXPORT_PRIVATE String substring(unsigned position, unsigned length = MaxLength) const;
245 WTF_EXPORT_PRIVATE String substringSharingImpl(unsigned position, unsigned length = MaxLength) const;
246 String left(unsigned length) const { return substring(0, length); }
247 String right(unsigned length) const { return substring(this->length() - length, length); }
248
249 WTF_EXPORT_PRIVATE String convertToASCIILowercase() const;
250 WTF_EXPORT_PRIVATE String convertToASCIIUppercase() const;
251 WTF_EXPORT_PRIVATE String convertToLowercaseWithoutLocale() const;
252 WTF_EXPORT_PRIVATE String convertToLowercaseWithoutLocaleStartingAtFailingIndex8Bit(unsigned) const;
253 WTF_EXPORT_PRIVATE String convertToUppercaseWithoutLocale() const;
254 WTF_EXPORT_PRIVATE String convertToLowercaseWithLocale(const AtomString& localeIdentifier) const;
255 WTF_EXPORT_PRIVATE String convertToUppercaseWithLocale(const AtomString& localeIdentifier) const;
256
257 WTF_EXPORT_PRIVATE String stripWhiteSpace() const;
258 WTF_EXPORT_PRIVATE String simplifyWhiteSpace() const;
259 WTF_EXPORT_PRIVATE String simplifyWhiteSpace(CodeUnitMatchFunction) const;
260
261 WTF_EXPORT_PRIVATE String stripLeadingAndTrailingCharacters(CodeUnitMatchFunction) const;
262 WTF_EXPORT_PRIVATE String removeCharacters(CodeUnitMatchFunction) const;
263
264 // Returns the string with case folded for case insensitive comparison.
265 // Use convertToASCIILowercase instead if ASCII case insensitive comparison is desired.
266 WTF_EXPORT_PRIVATE String foldCase() const;
267
268 // Returns an uninitialized string. The characters needs to be written
269 // into the buffer returned in data before the returned string is used.
270 static String createUninitialized(unsigned length, UChar*& data) { return StringImpl::createUninitialized(length, data); }
271 static String createUninitialized(unsigned length, LChar*& data) { return StringImpl::createUninitialized(length, data); }
272
273 using SplitFunctor = WTF::Function<void(const StringView&)>;
274
275 WTF_EXPORT_PRIVATE void split(UChar separator, const SplitFunctor&) const;
276 WTF_EXPORT_PRIVATE Vector<String> split(UChar separator) const;
277 WTF_EXPORT_PRIVATE Vector<String> split(const String& separator) const;
278
279 WTF_EXPORT_PRIVATE void splitAllowingEmptyEntries(UChar separator, const SplitFunctor&) const;
280 WTF_EXPORT_PRIVATE Vector<String> splitAllowingEmptyEntries(UChar separator) const;
281 WTF_EXPORT_PRIVATE Vector<String> splitAllowingEmptyEntries(const String& separator) const;
282
283 WTF_EXPORT_PRIVATE int toIntStrict(bool* ok = nullptr, int base = 10) const;
284 WTF_EXPORT_PRIVATE unsigned toUIntStrict(bool* ok = nullptr, int base = 10) const;
285 WTF_EXPORT_PRIVATE int64_t toInt64Strict(bool* ok = nullptr, int base = 10) const;
286 WTF_EXPORT_PRIVATE uint64_t toUInt64Strict(bool* ok = nullptr, int base = 10) const;
287 WTF_EXPORT_PRIVATE intptr_t toIntPtrStrict(bool* ok = nullptr, int base = 10) const;
288
289 WTF_EXPORT_PRIVATE int toInt(bool* ok = nullptr) const;
290 WTF_EXPORT_PRIVATE unsigned toUInt(bool* ok = nullptr) const;
291 WTF_EXPORT_PRIVATE int64_t toInt64(bool* ok = nullptr) const;
292 WTF_EXPORT_PRIVATE uint64_t toUInt64(bool* ok = nullptr) const;
293 WTF_EXPORT_PRIVATE intptr_t toIntPtr(bool* ok = nullptr) const;
294
295 // FIXME: Like the strict functions above, these give false for "ok" when there is trailing garbage.
296 // Like the non-strict functions above, these return the value when there is trailing garbage.
297 // It would be better if these were more consistent with the above functions instead.
298 WTF_EXPORT_PRIVATE double toDouble(bool* ok = nullptr) const;
299 WTF_EXPORT_PRIVATE float toFloat(bool* ok = nullptr) const;
300
301 bool percentage(int& percentage) const;
302
303 WTF_EXPORT_PRIVATE String isolatedCopy() const &;
304 WTF_EXPORT_PRIVATE String isolatedCopy() &&;
305
306 WTF_EXPORT_PRIVATE bool isSafeToSendToAnotherThread() const;
307
308 // Prevent Strings from being implicitly convertable to bool as it will be ambiguous on any platform that
309 // allows implicit conversion to another pointer type (e.g., Mac allows implicit conversion to NSString *).
310 typedef struct ImplicitConversionFromWTFStringToBoolDisallowedA* (String::*UnspecifiedBoolTypeA);
311 typedef struct ImplicitConversionFromWTFStringToBoolDisallowedB* (String::*UnspecifiedBoolTypeB);
312 operator UnspecifiedBoolTypeA() const;
313 operator UnspecifiedBoolTypeB() const;
314
315#if USE(CF)
316 WTF_EXPORT_PRIVATE String(CFStringRef);
317 WTF_EXPORT_PRIVATE RetainPtr<CFStringRef> createCFString() const;
318#endif
319
320#ifdef __OBJC__
321 WTF_EXPORT_PRIVATE String(NSString *);
322
323 // This conversion converts the null string to an empty NSString rather than to nil.
324 // Given Cocoa idioms, this is a more useful default. Clients that need to preserve the
325 // null string can check isNull explicitly.
326 operator NSString *() const;
327#endif
328
329#if OS(WINDOWS)
330#if U_ICU_VERSION_MAJOR_NUM >= 59
331 String(const wchar_t* characters, unsigned length)
332 : String(ucharFrom(characters), length) { }
333
334 String(const wchar_t* characters)
335 : String(ucharFrom(characters)) { }
336#endif
337
338 WTF_EXPORT_PRIVATE Vector<wchar_t> wideCharacters() const;
339#endif
340
341 WTF_EXPORT_PRIVATE static String make8BitFrom16BitSource(const UChar*, size_t);
342 template<size_t inlineCapacity> static String make8BitFrom16BitSource(const Vector<UChar, inlineCapacity>&);
343
344 WTF_EXPORT_PRIVATE static String make16BitFrom8BitSource(const LChar*, size_t);
345
346 // String::fromUTF8 will return a null string if
347 // the input data contains invalid UTF-8 sequences.
348 WTF_EXPORT_PRIVATE static String fromUTF8(const LChar*, size_t);
349 WTF_EXPORT_PRIVATE static String fromUTF8(const LChar*);
350 static String fromUTF8(const char* characters, size_t length) { return fromUTF8(reinterpret_cast<const LChar*>(characters), length); };
351 static String fromUTF8(const char* string) { return fromUTF8(reinterpret_cast<const LChar*>(string)); };
352 WTF_EXPORT_PRIVATE static String fromUTF8(const CString&);
353 static String fromUTF8(const Vector<LChar>& characters);
354
355 // Tries to convert the passed in string to UTF-8, but will fall back to Latin-1 if the string is not valid UTF-8.
356 WTF_EXPORT_PRIVATE static String fromUTF8WithLatin1Fallback(const LChar*, size_t);
357 static String fromUTF8WithLatin1Fallback(const char* characters, size_t length) { return fromUTF8WithLatin1Fallback(reinterpret_cast<const LChar*>(characters), length); };
358
359 // Determines the writing direction using the Unicode Bidi Algorithm rules P2 and P3.
360 UCharDirection defaultWritingDirection(bool* hasStrongDirectionality = nullptr) const;
361
362 bool isAllASCII() const { return !m_impl || m_impl->isAllASCII(); }
363 bool isAllLatin1() const { return !m_impl || m_impl->isAllLatin1(); }
364 template<bool isSpecialCharacter(UChar)> bool isAllSpecialCharacters() const { return !m_impl || m_impl->isAllSpecialCharacters<isSpecialCharacter>(); }
365
366 // Hash table deleted values, which are only constructed and never copied or destroyed.
367 String(WTF::HashTableDeletedValueType) : m_impl(WTF::HashTableDeletedValue) { }
368 bool isHashTableDeletedValue() const { return m_impl.isHashTableDeletedValue(); }
369
370 unsigned hash() const { return isNull() ? 0 : impl()->hash(); }
371 unsigned existingHash() const { return isNull() ? 0 : impl()->existingHash(); }
372
373#ifndef NDEBUG
374 WTF_EXPORT_PRIVATE void show() const;
375#endif
376
377 // Turns this String empty if the StringImpl is not referenced by anyone else.
378 // This is useful for clearing String-based caches.
379 void clearImplIfNotShared();
380
381 static constexpr unsigned MaxLength = StringImpl::MaxLength;
382
383private:
384 template<typename CharacterType> void removeInternal(const CharacterType*, unsigned, unsigned);
385
386 template<bool allowEmptyEntries> void splitInternal(UChar separator, const SplitFunctor&) const;
387 template<bool allowEmptyEntries> Vector<String> splitInternal(UChar separator) const;
388 template<bool allowEmptyEntries> Vector<String> splitInternal(const String& separator) const;
389
390 RefPtr<StringImpl> m_impl;
391};
392
393static_assert(sizeof(String) == sizeof(void*), "String should effectively be a pointer to a StringImpl, and efficient to pass by value");
394
395inline bool operator==(const String& a, const String& b) { return equal(a.impl(), b.impl()); }
396inline bool operator==(const String& a, const LChar* b) { return equal(a.impl(), b); }
397inline bool operator==(const String& a, const char* b) { return equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
398inline bool operator==(const String& a, ASCIILiteral b) { return equal(a.impl(), reinterpret_cast<const LChar*>(b.characters())); }
399inline bool operator==(const LChar* a, const String& b) { return equal(a, b.impl()); }
400inline bool operator==(const char* a, const String& b) { return equal(reinterpret_cast<const LChar*>(a), b.impl()); }
401inline bool operator==(ASCIILiteral a, const String& b) { return equal(reinterpret_cast<const LChar*>(a.characters()), b.impl()); }
402template<size_t inlineCapacity> inline bool operator==(const Vector<char, inlineCapacity>& a, const String& b) { return equal(b.impl(), a.data(), a.size()); }
403template<size_t inlineCapacity> inline bool operator==(const String& a, const Vector<char, inlineCapacity>& b) { return b == a; }
404
405inline bool operator!=(const String& a, const String& b) { return !equal(a.impl(), b.impl()); }
406inline bool operator!=(const String& a, const LChar* b) { return !equal(a.impl(), b); }
407inline bool operator!=(const String& a, const char* b) { return !equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
408inline bool operator!=(const String& a, ASCIILiteral b) { return !equal(a.impl(), reinterpret_cast<const LChar*>(b.characters())); }
409inline bool operator!=(const LChar* a, const String& b) { return !equal(a, b.impl()); }
410inline bool operator!=(const char* a, const String& b) { return !equal(reinterpret_cast<const LChar*>(a), b.impl()); }
411inline bool operator!=(ASCIILiteral a, const String& b) { return !equal(reinterpret_cast<const LChar*>(a.characters()), b.impl()); }
412template<size_t inlineCapacity> inline bool operator!=(const Vector<char, inlineCapacity>& a, const String& b) { return !(a == b); }
413template<size_t inlineCapacity> inline bool operator!=(const String& a, const Vector<char, inlineCapacity>& b) { return b != a; }
414
415bool equalIgnoringASCIICase(const String&, const String&);
416bool equalIgnoringASCIICase(const String&, const char*);
417
418template<unsigned length> bool equalLettersIgnoringASCIICase(const String&, const char (&lowercaseLetters)[length]);
419template<unsigned length> bool startsWithLettersIgnoringASCIICase(const String&, const char (&lowercaseLetters)[length]);
420
421inline bool equalIgnoringNullity(const String& a, const String& b) { return equalIgnoringNullity(a.impl(), b.impl()); }
422template<size_t inlineCapacity> inline bool equalIgnoringNullity(const Vector<UChar, inlineCapacity>& a, const String& b) { return equalIgnoringNullity(a, b.impl()); }
423
424inline bool operator!(const String& string) { return string.isNull(); }
425
426inline void swap(String& a, String& b) { a.swap(b); }
427
428#ifdef __OBJC__
429
430// Used in a small number of places where the long standing behavior has been "nil if empty".
431NSString * nsStringNilIfEmpty(const String&);
432
433#endif
434
435WTF_EXPORT_PRIVATE int codePointCompare(const String&, const String&);
436bool codePointCompareLessThan(const String&, const String&);
437
438template<typename CharacterType> void appendNumber(Vector<CharacterType>&, unsigned char number);
439
440// Shared global empty and null string.
441WTF_EXPORT_PRIVATE const String& emptyString();
442WTF_EXPORT_PRIVATE const String& nullString();
443
444template<typename> struct DefaultHash;
445template<> struct DefaultHash<String> { using Hash = StringHash; };
446template<> struct VectorTraits<String> : VectorTraitsBase<false, void> {
447 static constexpr bool canInitializeWithMemset = true;
448 static constexpr bool canMoveWithMemcpy = true;
449};
450
451template<> struct IntegerToStringConversionTrait<String> {
452 using ReturnType = String;
453 using AdditionalArgumentType = void;
454 static String flush(LChar* characters, unsigned length, void*) { return { characters, length }; }
455};
456
457// Definitions of string operations
458
459inline String::String(StringImpl& string)
460 : m_impl(&string)
461{
462}
463
464inline String::String(StringImpl* string)
465 : m_impl(string)
466{
467}
468
469inline String::String(Ref<StringImpl>&& string)
470 : m_impl(WTFMove(string))
471{
472}
473
474inline String::String(RefPtr<StringImpl>&& string)
475 : m_impl(WTFMove(string))
476{
477}
478
479inline String::String(Ref<AtomStringImpl>&& string)
480 : m_impl(WTFMove(string))
481{
482}
483
484inline String::String(RefPtr<AtomStringImpl>&& string)
485 : m_impl(WTFMove(string))
486{
487}
488
489inline String::String(StaticStringImpl& string)
490 : m_impl(reinterpret_cast<StringImpl*>(&string))
491{
492}
493
494inline String::String(StaticStringImpl* string)
495 : m_impl(reinterpret_cast<StringImpl*>(string))
496{
497}
498
499template<size_t inlineCapacity, typename OverflowHandler> String::String(const Vector<UChar, inlineCapacity, OverflowHandler>& vector)
500 : m_impl(vector.size() ? StringImpl::create(vector.data(), vector.size()) : Ref<StringImpl> { *StringImpl::empty() })
501{
502}
503
504template<> inline const LChar* String::characters<LChar>() const
505{
506 return characters8();
507}
508
509template<> inline const UChar* String::characters<UChar>() const
510{
511 return characters16();
512}
513
514inline UChar String::characterAt(unsigned index) const
515{
516 if (!m_impl || index >= m_impl->length())
517 return 0;
518 return (*m_impl)[index];
519}
520
521inline String& String::replace(UChar target, UChar replacement)
522{
523 if (m_impl)
524 m_impl = m_impl->replace(target, replacement);
525 return *this;
526}
527
528inline String& String::replace(UChar target, const String& replacement)
529{
530 if (m_impl)
531 m_impl = m_impl->replace(target, replacement.impl());
532 return *this;
533}
534
535inline String& String::replace(const String& target, const String& replacement)
536{
537 if (m_impl)
538 m_impl = m_impl->replace(target.impl(), replacement.impl());
539 return *this;
540}
541
542inline String& String::replace(unsigned start, unsigned length, const String& replacement)
543{
544 if (m_impl)
545 m_impl = m_impl->replace(start, length, replacement.impl());
546 return *this;
547}
548
549template<unsigned characterCount> ALWAYS_INLINE String& String::replaceWithLiteral(UChar target, const char (&characters)[characterCount])
550{
551 if (m_impl)
552 m_impl = m_impl->replace(target, characters, characterCount - 1);
553 return *this;
554}
555
556template<size_t inlineCapacity> inline String String::make8BitFrom16BitSource(const Vector<UChar, inlineCapacity>& buffer)
557{
558 return make8BitFrom16BitSource(buffer.data(), buffer.size());
559}
560
561inline UCharDirection String::defaultWritingDirection(bool* hasStrongDirectionality) const
562{
563 if (m_impl)
564 return m_impl->defaultWritingDirection(hasStrongDirectionality);
565 if (hasStrongDirectionality)
566 *hasStrongDirectionality = false;
567 return U_LEFT_TO_RIGHT;
568}
569
570inline void String::clearImplIfNotShared()
571{
572 if (m_impl && m_impl->hasOneRef())
573 m_impl = nullptr;
574}
575
576#ifdef __OBJC__
577
578inline String::operator NSString *() const
579{
580 if (!m_impl)
581 return @"";
582 return *m_impl;
583}
584
585inline NSString * nsStringNilIfEmpty(const String& string)
586{
587 if (string.isEmpty())
588 return nil;
589 return *string.impl();
590}
591
592#endif
593
594inline bool codePointCompareLessThan(const String& a, const String& b)
595{
596 return codePointCompare(a.impl(), b.impl()) < 0;
597}
598
599template<typename CharacterType>
600inline void appendNumber(Vector<CharacterType>& vector, unsigned char number)
601{
602 int numberLength = number > 99 ? 3 : (number > 9 ? 2 : 1);
603 size_t vectorSize = vector.size();
604 vector.grow(vectorSize + numberLength);
605
606 switch (numberLength) {
607 case 3:
608 vector[vectorSize + 2] = number % 10 + '0';
609 number /= 10;
610 FALLTHROUGH;
611
612 case 2:
613 vector[vectorSize + 1] = number % 10 + '0';
614 number /= 10;
615 FALLTHROUGH;
616
617 case 1:
618 vector[vectorSize] = number % 10 + '0';
619 }
620}
621
622inline String String::fromUTF8(const Vector<LChar>& characters)
623{
624 if (characters.isEmpty())
625 return emptyString();
626 return fromUTF8(characters.data(), characters.size());
627}
628
629template<unsigned length> inline bool equalLettersIgnoringASCIICase(const String& string, const char (&lowercaseLetters)[length])
630{
631 return equalLettersIgnoringASCIICase(string.impl(), lowercaseLetters);
632}
633
634inline bool equalIgnoringASCIICase(const String& a, const String& b)
635{
636 return equalIgnoringASCIICase(a.impl(), b.impl());
637}
638
639inline bool equalIgnoringASCIICase(const String& a, const char* b)
640{
641 return equalIgnoringASCIICase(a.impl(), b);
642}
643
644template<unsigned length> inline bool startsWithLettersIgnoringASCIICase(const String& string, const char (&lowercaseLetters)[length])
645{
646 return startsWithLettersIgnoringASCIICase(string.impl(), lowercaseLetters);
647}
648
649inline namespace StringLiterals {
650
651inline String operator"" _str(const char* characters, size_t)
652{
653 return ASCIILiteral::fromLiteralUnsafe(characters);
654}
655
656} // inline StringLiterals
657
658} // namespace WTF
659
660using WTF::KeepTrailingZeros;
661using WTF::String;
662using WTF::appendNumber;
663using WTF::charactersToDouble;
664using WTF::charactersToFloat;
665using WTF::charactersToInt64;
666using WTF::charactersToInt64Strict;
667using WTF::charactersToInt;
668using WTF::charactersToIntPtr;
669using WTF::charactersToIntPtrStrict;
670using WTF::charactersToIntStrict;
671using WTF::charactersToUInt64;
672using WTF::charactersToUInt64Strict;
673using WTF::charactersToUInt;
674using WTF::charactersToUIntStrict;
675using WTF::emptyString;
676using WTF::nullString;
677using WTF::equal;
678using WTF::find;
679using WTF::isAllSpecialCharacters;
680using WTF::isSpaceOrNewline;
681using WTF::reverseFind;
682
683#include <wtf/text/AtomString.h>
684