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