1/*
2 * (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004-2016 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#ifndef WTFString_h
23#define WTFString_h
24
25// This file would be called String.h, but that conflicts with <string.h>
26// on systems without case-sensitive file systems.
27
28#include <wtf/text/ASCIIFastPath.h>
29#include <wtf/text/StringImpl.h>
30
31#ifdef __OBJC__
32#include <objc/objc.h>
33#endif
34
35#if PLATFORM(QT)
36QT_BEGIN_NAMESPACE
37class QString;
38QT_END_NAMESPACE
39#endif
40
41namespace WTF {
42
43// Declarations of string operations
44
45WTF_EXPORT_STRING_API int charactersToIntStrict(const LChar*, size_t, bool* ok = 0, int base = 10);
46WTF_EXPORT_STRING_API int charactersToIntStrict(const UChar*, size_t, bool* ok = 0, int base = 10);
47WTF_EXPORT_STRING_API unsigned charactersToUIntStrict(const LChar*, size_t, bool* ok = 0, int base = 10);
48WTF_EXPORT_STRING_API unsigned charactersToUIntStrict(const UChar*, size_t, bool* ok = 0, int base = 10);
49int64_t charactersToInt64Strict(const LChar*, size_t, bool* ok = 0, int base = 10);
50int64_t charactersToInt64Strict(const UChar*, size_t, bool* ok = 0, int base = 10);
51uint64_t charactersToUInt64Strict(const LChar*, size_t, bool* ok = 0, int base = 10);
52uint64_t charactersToUInt64Strict(const UChar*, size_t, bool* ok = 0, int base = 10);
53intptr_t charactersToIntPtrStrict(const LChar*, size_t, bool* ok = 0, int base = 10);
54intptr_t charactersToIntPtrStrict(const UChar*, size_t, bool* ok = 0, int base = 10);
55
56WTF_EXPORT_STRING_API int charactersToInt(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
57WTF_EXPORT_STRING_API int charactersToInt(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
58unsigned charactersToUInt(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
59unsigned charactersToUInt(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
60int64_t charactersToInt64(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
61int64_t charactersToInt64(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
62uint64_t charactersToUInt64(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
63uint64_t charactersToUInt64(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
64intptr_t charactersToIntPtr(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
65intptr_t charactersToIntPtr(const UChar*, size_t, bool* ok = 0); // 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_STRING_API double charactersToDouble(const LChar*, size_t, bool* ok = 0);
71WTF_EXPORT_STRING_API double charactersToDouble(const UChar*, size_t, bool* ok = 0);
72WTF_EXPORT_STRING_API float charactersToFloat(const LChar*, size_t, bool* ok = 0);
73WTF_EXPORT_STRING_API float charactersToFloat(const UChar*, size_t, bool* ok = 0);
74WTF_EXPORT_STRING_API float charactersToFloat(const LChar*, size_t, size_t& parsedLength);
75WTF_EXPORT_STRING_API float charactersToFloat(const UChar*, size_t, size_t& parsedLength);
76
77class ASCIILiteral;
78
79enum TrailingZerosTruncatingPolicy {
80 KeepTrailingZeros,
81 TruncateTrailingZeros
82};
83
84template<bool isSpecialCharacter(UChar), typename CharacterType>
85bool isAllSpecialCharacters(const CharacterType*, size_t);
86
87class String {
88public:
89 // Construct a null string, distinguishable from an empty string.
90 String() { }
91
92 // Construct a string with UTF-16 data.
93 WTF_EXPORT_STRING_API String(const UChar* characters, unsigned length);
94
95 // Construct a string by copying the contents of a vector. To avoid
96 // copying, consider using String::adopt instead.
97 // This method will never create a null string. Vectors with size() == 0
98 // will return the empty string.
99 // NOTE: This is different from String(vector.data(), vector.size())
100 // which will sometimes return a null string when vector.data() is null
101 // which can only occur for vectors without inline capacity.
102 // See: https://bugs.webkit.org/show_bug.cgi?id=109792
103 template<size_t inlineCapacity, typename OverflowHandler>
104 explicit String(const Vector<UChar, inlineCapacity, OverflowHandler>&);
105
106 // Construct a string with UTF-16 data, from a null-terminated source.
107 WTF_EXPORT_STRING_API String(const UChar*);
108
109 // Construct a string with latin1 data.
110 WTF_EXPORT_STRING_API String(const LChar* characters, unsigned length);
111 WTF_EXPORT_STRING_API String(const char* characters, unsigned length);
112
113 // Construct a string with latin1 data, from a null-terminated source.
114 WTF_EXPORT_STRING_API String(const LChar* characters);
115 WTF_EXPORT_STRING_API String(const char* characters);
116
117 // Construct a string referencing an existing StringImpl.
118 String(StringImpl&);
119 String(StringImpl*);
120 String(PassRefPtr<StringImpl>);
121 String(Ref<StringImpl>&&);
122 String(RefPtr<StringImpl>&&);
123
124 String(Ref<AtomicStringImpl>&&);
125 String(RefPtr<AtomicStringImpl>&&);
126
127 // Construct a string from a constant string literal.
128 WTF_EXPORT_STRING_API String(ASCIILiteral characters);
129
130 // Construct a string from a constant string literal.
131 // This constructor is the "big" version, as it put the length in the function call and generate bigger code.
132 enum ConstructFromLiteralTag { ConstructFromLiteral };
133 template<unsigned charactersCount>
134 String(const char (&characters)[charactersCount], ConstructFromLiteralTag) : m_impl(StringImpl::createFromLiteral<charactersCount>(characters)) { }
135
136 // We have to declare the copy constructor and copy assignment operator as well, otherwise
137 // they'll be implicitly deleted by adding the move constructor and move assignment operator.
138 String(const String& other) : m_impl(other.m_impl) { }
139 String(String&& other) : m_impl(other.m_impl.release()) { }
140 String& operator=(const String& other) { m_impl = other.m_impl; return *this; }
141 String& operator=(String&& other) { m_impl = other.m_impl.release(); return *this; }
142
143 // Inline the destructor.
144 ALWAYS_INLINE ~String() { }
145
146 void swap(String& o) { m_impl.swap(o.m_impl); }
147
148 static String adopt(StringBuffer<LChar>& buffer) { return StringImpl::adopt(buffer); }
149 static String adopt(StringBuffer<UChar>& buffer) { return StringImpl::adopt(buffer); }
150 template<typename CharacterType, size_t inlineCapacity, typename OverflowHandler>
151 static String adopt(Vector<CharacterType, inlineCapacity, OverflowHandler>& vector) { return StringImpl::adopt(vector); }
152
153 bool isNull() const { return !m_impl; }
154 bool isEmpty() const { return !m_impl || !m_impl->length(); }
155
156 StringImpl* impl() const { return m_impl.get(); }
157 PassRefPtr<StringImpl> releaseImpl() { return m_impl.release(); }
158
159 unsigned length() const
160 {
161 if (!m_impl)
162 return 0;
163 return m_impl->length();
164 }
165
166 const LChar* characters8() const
167 {
168 if (!m_impl)
169 return 0;
170 ASSERT(m_impl->is8Bit());
171 return m_impl->characters8();
172 }
173
174 const UChar* characters16() const
175 {
176 if (!m_impl)
177 return 0;
178 ASSERT(!m_impl->is8Bit());
179 return m_impl->characters16();
180 }
181
182 // Return characters8() or characters16() depending on CharacterType.
183 template <typename CharacterType>
184 inline const CharacterType* characters() const;
185
186 bool is8Bit() const { return m_impl->is8Bit(); }
187
188 unsigned sizeInBytes() const
189 {
190 if (!m_impl)
191 return 0;
192 return m_impl->length() * (is8Bit() ? sizeof(LChar) : sizeof(UChar));
193 }
194
195 WTF_EXPORT_STRING_API CString ascii() const;
196 WTF_EXPORT_STRING_API CString latin1() const;
197
198 WTF_EXPORT_STRING_API CString utf8(ConversionMode) const;
199 WTF_EXPORT_STRING_API CString utf8() const;
200
201 UChar at(unsigned index) const
202 {
203 if (!m_impl || index >= m_impl->length())
204 return 0;
205 return (*m_impl)[index];
206 }
207 UChar operator[](unsigned index) const { return at(index); }
208
209 WTF_EXPORT_STRING_API static String number(int);
210 WTF_EXPORT_STRING_API static String number(unsigned int);
211 WTF_EXPORT_STRING_API static String number(long);
212 WTF_EXPORT_STRING_API static String number(unsigned long);
213 WTF_EXPORT_STRING_API static String number(long long);
214 WTF_EXPORT_STRING_API static String number(unsigned long long);
215
216 WTF_EXPORT_STRING_API static String number(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros);
217
218 // Number to String conversion following the ECMAScript definition.
219 WTF_EXPORT_STRING_API static String numberToStringECMAScript(double);
220 WTF_EXPORT_STRING_API static String numberToStringFixedWidth(double, unsigned decimalPlaces);
221
222 // Find a single character or string, also with match function & latin1 forms.
223 size_t find(UChar c, unsigned start = 0) const
224 { return m_impl ? m_impl->find(c, start) : notFound; }
225
226 size_t find(const String& str) const
227 { return m_impl ? m_impl->find(str.impl()) : notFound; }
228 size_t find(const String& str, unsigned start) const
229 { return m_impl ? m_impl->find(str.impl(), start) : notFound; }
230 size_t findIgnoringASCIICase(const String& str) const
231 { return m_impl ? m_impl->findIgnoringASCIICase(str.impl()) : notFound; }
232 size_t findIgnoringASCIICase(const String& str, unsigned startOffset) const
233 { return m_impl ? m_impl->findIgnoringASCIICase(str.impl(), startOffset) : notFound; }
234
235 size_t find(CharacterMatchFunctionPtr matchFunction, unsigned start = 0) const
236 { return m_impl ? m_impl->find(matchFunction, start) : notFound; }
237 size_t find(const LChar* str, unsigned start = 0) const
238 { return m_impl ? m_impl->find(str, start) : notFound; }
239
240 size_t findNextLineStart(unsigned start = 0) const
241 { return m_impl ? m_impl->findNextLineStart(start) : notFound; }
242
243 // Find the last instance of a single character or string.
244 size_t reverseFind(UChar c, unsigned start = UINT_MAX) const
245 { return m_impl ? m_impl->reverseFind(c, start) : notFound; }
246 size_t reverseFind(const String& str, unsigned start = UINT_MAX) const
247 { return m_impl ? m_impl->reverseFind(str.impl(), start) : notFound; }
248
249 // Case insensitive string matching.
250 size_t findIgnoringCase(const LChar* str, unsigned start = 0) const
251 { return m_impl ? m_impl->findIgnoringCase(str, start) : notFound; }
252 size_t findIgnoringCase(const String& str, unsigned start = 0) const
253 { return m_impl ? m_impl->findIgnoringCase(str.impl(), start) : notFound; }
254 size_t reverseFindIgnoringCase(const String& str, unsigned start = UINT_MAX) const
255 { return m_impl ? m_impl->reverseFindIgnoringCase(str.impl(), start) : notFound; }
256
257 // Wrappers for find & reverseFind adding dynamic sensitivity check.
258 size_t find(const LChar* str, unsigned start, bool caseSensitive) const
259 { return caseSensitive ? find(str, start) : findIgnoringCase(str, start); }
260 size_t find(const String& str, unsigned start, bool caseSensitive) const
261 { return caseSensitive ? find(str, start) : findIgnoringCase(str, start); }
262 size_t reverseFind(const String& str, unsigned start, bool caseSensitive) const
263 { return caseSensitive ? reverseFind(str, start) : reverseFindIgnoringCase(str, start); }
264
265 WTF_EXPORT_STRING_API Vector<UChar> charactersWithNullTermination() const;
266
267 WTF_EXPORT_STRING_API UChar32 characterStartingAt(unsigned) const; // Ditto.
268
269 bool contains(UChar c) const { return find(c) != notFound; }
270 bool contains(const LChar* str, bool caseSensitive = true, unsigned startOffset = 0) const
271 { return find(str, startOffset, caseSensitive) != notFound; }
272 bool contains(const String& str) const
273 { return find(str) != notFound; }
274 bool contains(const String& str, bool caseSensitive, unsigned startOffset = 0) const
275 { return find(str, startOffset, caseSensitive) != notFound; }
276 bool containsIgnoringASCIICase(const String& str) const
277 { return findIgnoringASCIICase(str) != notFound; }
278 bool containsIgnoringASCIICase(const String& str, unsigned startOffset) const
279 { return findIgnoringASCIICase(str, startOffset) != notFound; }
280
281 bool startsWith(const String& s) const
282 { return m_impl ? m_impl->startsWith(s.impl()) : s.isEmpty(); }
283 bool startsWithIgnoringASCIICase(const String& s) const
284 { return m_impl ? m_impl->startsWithIgnoringASCIICase(s.impl()) : s.isEmpty(); }
285 bool startsWith(const String& s, bool caseSensitive) const
286 { return m_impl ? m_impl->startsWith(s.impl(), caseSensitive) : s.isEmpty(); }
287 bool startsWith(UChar character) const
288 { return m_impl ? m_impl->startsWith(character) : false; }
289 template<unsigned matchLength>
290 bool startsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const
291 { return m_impl ? m_impl->startsWith<matchLength>(prefix, caseSensitive) : !matchLength; }
292 bool hasInfixStartingAt(const String& prefix, unsigned startOffset) const
293 { return m_impl && prefix.impl() ? m_impl->hasInfixStartingAt(*prefix.impl(), startOffset) : false; }
294
295 bool endsWith(const String& s) const
296 { return m_impl ? m_impl->endsWith(s.impl()) : s.isEmpty(); }
297 bool endsWithIgnoringASCIICase(const String& s) const
298 { return m_impl ? m_impl->endsWithIgnoringASCIICase(s.impl()) : s.isEmpty(); }
299 bool endsWith(const String& s, bool caseSensitive) const
300 { return m_impl ? m_impl->endsWith(s.impl(), caseSensitive) : s.isEmpty(); }
301 bool endsWith(UChar character) const
302 { return m_impl ? m_impl->endsWith(character) : false; }
303 bool endsWith(char character) const { return endsWith(static_cast<UChar>(character)); }
304 template<unsigned matchLength>
305 bool endsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const
306 { return m_impl ? m_impl->endsWith<matchLength>(prefix, caseSensitive) : !matchLength; }
307 bool hasInfixEndingAt(const String& suffix, unsigned endOffset) const
308 { return m_impl && suffix.impl() ? m_impl->hasInfixEndingAt(*suffix.impl(), endOffset) : false; }
309
310 WTF_EXPORT_STRING_API void append(const String&);
311 WTF_EXPORT_STRING_API void append(LChar);
312 void append(char c) { append(static_cast<LChar>(c)); };
313 WTF_EXPORT_STRING_API void append(UChar);
314 WTF_EXPORT_STRING_API void append(const LChar*, unsigned length);
315 WTF_EXPORT_STRING_API void append(const UChar*, unsigned length);
316 WTF_EXPORT_STRING_API void insert(const String&, unsigned pos);
317
318 String& replace(UChar a, UChar b) { if (m_impl) m_impl = m_impl->replace(a, b); return *this; }
319 String& replace(UChar a, const String& b) { if (m_impl) m_impl = m_impl->replace(a, b.impl()); return *this; }
320 String& replace(const String& a, const String& b) { if (m_impl) m_impl = m_impl->replace(a.impl(), b.impl()); return *this; }
321 String& replace(unsigned index, unsigned len, const String& b) { if (m_impl) m_impl = m_impl->replace(index, len, b.impl()); return *this; }
322
323 template<unsigned charactersCount>
324 ALWAYS_INLINE String& replaceWithLiteral(UChar a, const char (&characters)[charactersCount])
325 {
326 if (m_impl)
327 m_impl = m_impl->replace(a, characters, charactersCount - 1);
328
329 return *this;
330 }
331
332 WTF_EXPORT_STRING_API void truncate(unsigned len);
333 WTF_EXPORT_STRING_API void remove(unsigned pos, int len = 1);
334
335 WTF_EXPORT_STRING_API String substring(unsigned pos, unsigned len = UINT_MAX) const;
336 WTF_EXPORT_STRING_API String substringSharingImpl(unsigned pos, unsigned len = UINT_MAX) const;
337 String left(unsigned len) const { return substring(0, len); }
338 String right(unsigned len) const { return substring(length() - len, len); }
339
340 WTF_EXPORT_STRING_API String convertToASCIILowercase() const;
341 WTF_EXPORT_STRING_API String convertToASCIIUppercase() const;
342 WTF_EXPORT_STRING_API String convertToLowercaseWithoutLocale() const;
343 WTF_EXPORT_STRING_API String convertToUppercaseWithoutLocale() const;
344 WTF_EXPORT_STRING_API String convertToLowercaseWithLocale(const AtomicString& localeIdentifier) const;
345 WTF_EXPORT_STRING_API String convertToUppercaseWithLocale(const AtomicString& localeIdentifier) const;
346
347 WTF_EXPORT_STRING_API String stripWhiteSpace() const;
348 WTF_EXPORT_STRING_API String stripWhiteSpace(IsWhiteSpaceFunctionPtr) const;
349 WTF_EXPORT_STRING_API String simplifyWhiteSpace() const;
350 WTF_EXPORT_STRING_API String simplifyWhiteSpace(IsWhiteSpaceFunctionPtr) const;
351
352 WTF_EXPORT_STRING_API String removeCharacters(CharacterMatchFunctionPtr) const;
353 template<bool isSpecialCharacter(UChar)> bool isAllSpecialCharacters() const;
354
355 // Returns the string with case folded for case insensitive comparison.
356 // Use convertToASCIILowercase instead if ASCII case insensitive comparison is desired.
357 WTF_EXPORT_STRING_API String foldCase() const;
358
359 WTF_EXPORT_STRING_API static String format(const char *, ...) WTF_ATTRIBUTE_PRINTF(1, 2);
360
361 // Returns an uninitialized string. The characters needs to be written
362 // into the buffer returned in data before the returned string is used.
363 // Failure to do this will have unpredictable results.
364 static String createUninitialized(unsigned length, UChar*& data) { return StringImpl::createUninitialized(length, data); }
365 static String createUninitialized(unsigned length, LChar*& data) { return StringImpl::createUninitialized(length, data); }
366
367 WTF_EXPORT_STRING_API void split(const String& separator, bool allowEmptyEntries, Vector<String>& result) const;
368 void split(const String& separator, Vector<String>& result) const
369 {
370 split(separator, false, result);
371 }
372 WTF_EXPORT_STRING_API void split(UChar separator, bool allowEmptyEntries, Vector<String>& result) const;
373 void split(UChar separator, Vector<String>& result) const
374 {
375 split(separator, false, result);
376 }
377
378 WTF_EXPORT_STRING_API int toIntStrict(bool* ok = 0, int base = 10) const;
379 WTF_EXPORT_STRING_API unsigned toUIntStrict(bool* ok = 0, int base = 10) const;
380 WTF_EXPORT_STRING_API int64_t toInt64Strict(bool* ok = 0, int base = 10) const;
381 WTF_EXPORT_STRING_API uint64_t toUInt64Strict(bool* ok = 0, int base = 10) const;
382 WTF_EXPORT_STRING_API intptr_t toIntPtrStrict(bool* ok = 0, int base = 10) const;
383
384 WTF_EXPORT_STRING_API int toInt(bool* ok = 0) const;
385 WTF_EXPORT_STRING_API unsigned toUInt(bool* ok = 0) const;
386 WTF_EXPORT_STRING_API int64_t toInt64(bool* ok = 0) const;
387 WTF_EXPORT_STRING_API uint64_t toUInt64(bool* ok = 0) const;
388 WTF_EXPORT_STRING_API intptr_t toIntPtr(bool* ok = 0) const;
389
390 // FIXME: Like the strict functions above, these give false for "ok" when there is trailing garbage.
391 // Like the non-strict functions above, these return the value when there is trailing garbage.
392 // It would be better if these were more consistent with the above functions instead.
393 WTF_EXPORT_STRING_API double toDouble(bool* ok = 0) const;
394 WTF_EXPORT_STRING_API float toFloat(bool* ok = 0) const;
395
396 bool percentage(int& percentage) const;
397
398#if COMPILER_SUPPORTS(CXX_REFERENCE_QUALIFIED_FUNCTIONS)
399 WTF_EXPORT_STRING_API String isolatedCopy() const &;
400 WTF_EXPORT_STRING_API String isolatedCopy() &&;
401#else
402 WTF_EXPORT_STRING_API String isolatedCopy() const;
403#endif
404
405 WTF_EXPORT_STRING_API bool isSafeToSendToAnotherThread() const;
406
407 // Prevent Strings from being implicitly convertable to bool as it will be ambiguous on any platform that
408 // allows implicit conversion to another pointer type (e.g., Mac allows implicit conversion to NSString *).
409 typedef struct ImplicitConversionFromWTFStringToBoolDisallowedA* (String::*UnspecifiedBoolTypeA);
410 typedef struct ImplicitConversionFromWTFStringToBoolDisallowedB* (String::*UnspecifiedBoolTypeB);
411 operator UnspecifiedBoolTypeA() const;
412 operator UnspecifiedBoolTypeB() const;
413
414#if USE(CF)
415 WTF_EXPORT_STRING_API String(CFStringRef);
416 WTF_EXPORT_STRING_API RetainPtr<CFStringRef> createCFString() const;
417#endif
418
419#ifdef __OBJC__
420 WTF_EXPORT_STRING_API String(NSString *);
421
422 // This conversion converts the null string to an empty NSString rather than to nil.
423 // Given Cocoa idioms, this is a more useful default. Clients that need to preserve the
424 // null string can check isNull explicitly.
425 operator NSString *() const;
426#endif
427
428#if PLATFORM(QT)
429 WTF_EXPORT_STRING_API String(const QString&);
430 WTF_EXPORT_STRING_API String(const QStringRef&);
431 WTF_EXPORT_STRING_API operator QString() const;
432#endif
433
434 WTF_EXPORT_STRING_API static String make8BitFrom16BitSource(const UChar*, size_t);
435 template<size_t inlineCapacity>
436 static String make8BitFrom16BitSource(const Vector<UChar, inlineCapacity>& buffer)
437 {
438 return make8BitFrom16BitSource(buffer.data(), buffer.size());
439 }
440
441 WTF_EXPORT_STRING_API static String make16BitFrom8BitSource(const LChar*, size_t);
442
443 // String::fromUTF8 will return a null string if
444 // the input data contains invalid UTF-8 sequences.
445 WTF_EXPORT_STRING_API static String fromUTF8(const LChar*, size_t);
446 WTF_EXPORT_STRING_API static String fromUTF8(const LChar*);
447 static String fromUTF8(const char* s, size_t length) { return fromUTF8(reinterpret_cast<const LChar*>(s), length); };
448 static String fromUTF8(const char* s) { return fromUTF8(reinterpret_cast<const LChar*>(s)); };
449 WTF_EXPORT_STRING_API static String fromUTF8(const CString&);
450
451 // 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.
452 WTF_EXPORT_STRING_API static String fromUTF8WithLatin1Fallback(const LChar*, size_t);
453 static String fromUTF8WithLatin1Fallback(const char* s, size_t length) { return fromUTF8WithLatin1Fallback(reinterpret_cast<const LChar*>(s), length); };
454
455 // Determines the writing direction using the Unicode Bidi Algorithm rules P2 and P3.
456 UCharDirection defaultWritingDirection(bool* hasStrongDirectionality = nullptr) const
457 {
458 if (m_impl)
459 return m_impl->defaultWritingDirection(hasStrongDirectionality);
460 if (hasStrongDirectionality)
461 *hasStrongDirectionality = false;
462 return U_LEFT_TO_RIGHT;
463 }
464
465 bool containsOnlyASCII() const;
466 bool containsOnlyLatin1() const;
467 bool containsOnlyWhitespace() const { return !m_impl || m_impl->containsOnlyWhitespace(); }
468
469 // Hash table deleted values, which are only constructed and never copied or destroyed.
470 String(WTF::HashTableDeletedValueType) : m_impl(WTF::HashTableDeletedValue) { }
471 bool isHashTableDeletedValue() const { return m_impl.isHashTableDeletedValue(); }
472
473 unsigned existingHash() const { return isNull() ? 0 : impl()->existingHash(); }
474
475#ifndef NDEBUG
476 WTF_EXPORT_STRING_API void show() const;
477#endif
478
479 // Workaround for a compiler bug. Use operator[] instead.
480 UChar characterAt(unsigned index) const
481 {
482 if (!m_impl || index >= m_impl->length())
483 return 0;
484 return (*m_impl)[index];
485 }
486
487private:
488 template <typename CharacterType>
489 void removeInternal(const CharacterType*, unsigned, int);
490
491 template <typename CharacterType>
492 void appendInternal(CharacterType);
493
494 RefPtr<StringImpl> m_impl;
495};
496
497inline bool operator==(const String& a, const String& b) { return equal(a.impl(), b.impl()); }
498inline bool operator==(const String& a, const LChar* b) { return equal(a.impl(), b); }
499inline bool operator==(const String& a, const char* b) { return equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
500inline bool operator==(const LChar* a, const String& b) { return equal(a, b.impl()); }
501inline bool operator==(const char* a, const String& b) { return equal(reinterpret_cast<const LChar*>(a), b.impl()); }
502template<size_t inlineCapacity> inline bool operator==(const Vector<char, inlineCapacity>& a, const String& b) { return equal(b.impl(), a.data(), a.size()); }
503template<size_t inlineCapacity> inline bool operator==(const String& a, const Vector<char, inlineCapacity>& b) { return b == a; }
504
505
506inline bool operator!=(const String& a, const String& b) { return !equal(a.impl(), b.impl()); }
507inline bool operator!=(const String& a, const LChar* b) { return !equal(a.impl(), b); }
508inline bool operator!=(const String& a, const char* b) { return !equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
509inline bool operator!=(const LChar* a, const String& b) { return !equal(a, b.impl()); }
510inline bool operator!=(const char* a, const String& b) { return !equal(reinterpret_cast<const LChar*>(a), b.impl()); }
511template<size_t inlineCapacity> inline bool operator!=(const Vector<char, inlineCapacity>& a, const String& b) { return !(a == b); }
512template<size_t inlineCapacity> inline bool operator!=(const String& a, const Vector<char, inlineCapacity>& b) { return b != a; }
513
514bool equalIgnoringASCIICase(const String&, const String&);
515bool equalIgnoringASCIICase(const String&, const char*);
516
517template<unsigned length> bool equalLettersIgnoringASCIICase(const String&, const char (&lowercaseLetters)[length]);
518
519inline bool equalIgnoringNullity(const String& a, const String& b) { return equalIgnoringNullity(a.impl(), b.impl()); }
520template<size_t inlineCapacity> inline bool equalIgnoringNullity(const Vector<UChar, inlineCapacity>& a, const String& b) { return equalIgnoringNullity(a, b.impl()); }
521
522inline bool operator!(const String& str) { return str.isNull(); }
523
524inline void swap(String& a, String& b) { a.swap(b); }
525
526#ifdef __OBJC__
527
528// Used in a small number of places where the long standing behavior has been "nil if empty".
529NSString * nsStringNilIfEmpty(const String&);
530
531#endif
532
533// Definitions of string operations
534
535inline String::String(StringImpl& impl)
536 : m_impl(&impl)
537{
538}
539
540inline String::String(StringImpl* impl)
541 : m_impl(impl)
542{
543}
544
545inline String::String(PassRefPtr<StringImpl> impl)
546 : m_impl(impl)
547{
548}
549
550inline String::String(Ref<StringImpl>&& impl)
551 : m_impl(WTFMove(impl))
552{
553}
554
555inline String::String(RefPtr<StringImpl>&& impl)
556 : m_impl(WTFMove(impl))
557{
558}
559
560inline String::String(Ref<AtomicStringImpl>&& impl)
561 : m_impl(WTFMove(impl))
562{
563}
564
565inline String::String(RefPtr<AtomicStringImpl>&& impl)
566 : m_impl(WTFMove(impl))
567{
568}
569
570template<size_t inlineCapacity, typename OverflowHandler>
571String::String(const Vector<UChar, inlineCapacity, OverflowHandler>& vector)
572 : m_impl(vector.size() ? StringImpl::create(vector.data(), vector.size()) : Ref<StringImpl>(*StringImpl::empty()))
573{
574}
575
576template<>
577inline const LChar* String::characters<LChar>() const
578{
579 ASSERT(is8Bit());
580 return characters8();
581}
582
583template<>
584inline const UChar* String::characters<UChar>() const
585{
586 ASSERT(!is8Bit());
587 return characters16();
588}
589
590inline bool String::containsOnlyLatin1() const
591{
592 if (isEmpty())
593 return true;
594
595 if (is8Bit())
596 return true;
597
598 const UChar* characters = characters16();
599 UChar ored = 0;
600 for (size_t i = 0; i < m_impl->length(); ++i)
601 ored |= characters[i];
602 return !(ored & 0xFF00);
603}
604
605#ifdef __OBJC__
606
607inline String::operator NSString *() const
608{
609 if (!m_impl)
610 return @"";
611 return *m_impl;
612}
613
614inline NSString * nsStringNilIfEmpty(const String& string)
615{
616 if (string.isEmpty())
617 return nil;
618 return *string.impl();
619}
620
621#endif
622
623inline bool String::containsOnlyASCII() const
624{
625 if (isEmpty())
626 return true;
627
628 if (is8Bit())
629 return charactersAreAllASCII(characters8(), m_impl->length());
630
631 return charactersAreAllASCII(characters16(), m_impl->length());
632}
633
634WTF_EXPORT_STRING_API int codePointCompare(const String&, const String&);
635
636inline bool codePointCompareLessThan(const String& a, const String& b)
637{
638 return codePointCompare(a.impl(), b.impl()) < 0;
639}
640
641template<typename CharacterType>
642inline void appendNumber(Vector<CharacterType>& vector, unsigned char number)
643{
644 int numberLength = number > 99 ? 3 : (number > 9 ? 2 : 1);
645 size_t vectorSize = vector.size();
646 vector.grow(vectorSize + numberLength);
647
648 switch (numberLength) {
649 case 3:
650 vector[vectorSize + 2] = number % 10 + '0';
651 number /= 10;
652 FALLTHROUGH;
653
654 case 2:
655 vector[vectorSize + 1] = number % 10 + '0';
656 number /= 10;
657 FALLTHROUGH;
658
659 case 1:
660 vector[vectorSize] = number % 10 + '0';
661 }
662}
663
664template<bool isSpecialCharacter(UChar), typename CharacterType>
665inline bool isAllSpecialCharacters(const CharacterType* characters, size_t length)
666{
667 for (size_t i = 0; i < length; ++i) {
668 if (!isSpecialCharacter(characters[i]))
669 return false;
670 }
671 return true;
672}
673
674template<bool isSpecialCharacter(UChar)>
675inline bool String::isAllSpecialCharacters() const
676{
677 size_t len = length();
678
679 if (!len)
680 return true;
681
682 if (is8Bit())
683 return WTF::isAllSpecialCharacters<isSpecialCharacter, LChar>(characters8(), len);
684 return WTF::isAllSpecialCharacters<isSpecialCharacter, UChar>(characters16(), len);
685}
686
687// StringHash is the default hash for String
688template<typename T> struct DefaultHash;
689template<> struct DefaultHash<String> {
690 typedef StringHash Hash;
691};
692
693template <> struct VectorTraits<String> : SimpleClassVectorTraits { };
694
695class ASCIILiteral {
696public:
697 explicit ASCIILiteral(const char* characters) : m_characters(characters) { }
698 operator const char*() { return m_characters; }
699
700private:
701 const char* m_characters;
702};
703
704// For thread-safe lambda capture:
705// StringCapture stringCapture(string);
706// auto lambdaThatRunsInOtherThread = [stringCapture] { String string = stringCapture.string(); ... }
707// FIXME: Remove when we can use C++14 initialized lambda capture: [string = string.isolatedCopy()].
708class StringCapture {
709public:
710 StringCapture() { }
711 StringCapture(const String& string) : m_string(string) { }
712 explicit StringCapture(String&& string) : m_string(string) { }
713 StringCapture(const StringCapture& other) : m_string(other.m_string.isolatedCopy()) { }
714 const String& string() const { return m_string; }
715 String releaseString() { return WTFMove(m_string); }
716
717 void operator=(const StringCapture& other) { m_string = other.m_string.isolatedCopy(); }
718
719private:
720 String m_string;
721};
722
723// Shared global empty string.
724WTF_EXPORT_STRING_API const String& emptyString();
725
726template<unsigned length> inline bool equalLettersIgnoringASCIICase(const String& string, const char (&lowercaseLetters)[length])
727{
728 return equalLettersIgnoringASCIICase(string.impl(), lowercaseLetters);
729}
730
731inline bool equalIgnoringASCIICase(const String& a, const String& b)
732{
733 return equalIgnoringASCIICase(a.impl(), b.impl());
734}
735
736inline bool equalIgnoringASCIICase(const String& a, const char* b)
737{
738 return equalIgnoringASCIICase(a.impl(), b);
739}
740
741}
742
743using WTF::CString;
744using WTF::KeepTrailingZeros;
745using WTF::String;
746using WTF::emptyString;
747using WTF::appendNumber;
748using WTF::charactersAreAllASCII;
749using WTF::charactersToIntStrict;
750using WTF::charactersToUIntStrict;
751using WTF::charactersToInt64Strict;
752using WTF::charactersToUInt64Strict;
753using WTF::charactersToIntPtrStrict;
754using WTF::charactersToInt;
755using WTF::charactersToUInt;
756using WTF::charactersToInt64;
757using WTF::charactersToUInt64;
758using WTF::charactersToIntPtr;
759using WTF::charactersToDouble;
760using WTF::charactersToFloat;
761using WTF::equal;
762using WTF::find;
763using WTF::isAllSpecialCharacters;
764using WTF::isSpaceOrNewline;
765using WTF::reverseFind;
766using WTF::ASCIILiteral;
767using WTF::StringCapture;
768
769#include <wtf/text/AtomicString.h>
770
771#endif
772