2 * (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012, 2013 Apple Inc. All rights reserved.
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.
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.
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.
25 // This file would be called String.h, but that conflicts with <string.h>
26 // on systems without case-sensitive file systems.
28 #include <wtf/text/ASCIIFastPath.h>
29 #include <wtf/text/StringImpl.h>
32 #include <objc/objc.h>
40 // Declarations of string operations
42 WTF_EXPORT_STRING_API int charactersToIntStrict(const LChar*, size_t, bool* ok = 0, int base = 10);
43 WTF_EXPORT_STRING_API int charactersToIntStrict(const UChar*, size_t, bool* ok = 0, int base = 10);
44 WTF_EXPORT_STRING_API unsigned charactersToUIntStrict(const LChar*, size_t, bool* ok = 0, int base = 10);
45 WTF_EXPORT_STRING_API unsigned charactersToUIntStrict(const UChar*, size_t, bool* ok = 0, int base = 10);
46 int64_t charactersToInt64Strict(const LChar*, size_t, bool* ok = 0, int base = 10);
47 int64_t charactersToInt64Strict(const UChar*, size_t, bool* ok = 0, int base = 10);
48 uint64_t charactersToUInt64Strict(const LChar*, size_t, bool* ok = 0, int base = 10);
49 uint64_t charactersToUInt64Strict(const UChar*, size_t, bool* ok = 0, int base = 10);
50 intptr_t charactersToIntPtrStrict(const LChar*, size_t, bool* ok = 0, int base = 10);
51 intptr_t charactersToIntPtrStrict(const UChar*, size_t, bool* ok = 0, int base = 10);
53 WTF_EXPORT_STRING_API int charactersToInt(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
54 WTF_EXPORT_STRING_API int charactersToInt(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
55 unsigned charactersToUInt(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
56 unsigned charactersToUInt(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
57 int64_t charactersToInt64(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
58 int64_t charactersToInt64(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
59 uint64_t charactersToUInt64(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
60 uint64_t charactersToUInt64(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
61 intptr_t charactersToIntPtr(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
62 intptr_t charactersToIntPtr(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
64 // FIXME: Like the strict functions above, these give false for "ok" when there is trailing garbage.
65 // Like the non-strict functions above, these return the value when there is trailing garbage.
66 // It would be better if these were more consistent with the above functions instead.
67 WTF_EXPORT_STRING_API double charactersToDouble(const LChar*, size_t, bool* ok = 0);
68 WTF_EXPORT_STRING_API double charactersToDouble(const UChar*, size_t, bool* ok = 0);
69 WTF_EXPORT_STRING_API float charactersToFloat(const LChar*, size_t, bool* ok = 0);
70 WTF_EXPORT_STRING_API float charactersToFloat(const UChar*, size_t, bool* ok = 0);
71 WTF_EXPORT_STRING_API float charactersToFloat(const LChar*, size_t, size_t& parsedLength);
72 WTF_EXPORT_STRING_API float charactersToFloat(const UChar*, size_t, size_t& parsedLength);
76 enum TrailingZerosTruncatingPolicy {
81 template<bool isSpecialCharacter(UChar), typename CharacterType>
82 bool isAllSpecialCharacters(const CharacterType*, size_t);
86 // Construct a null string, distinguishable from an empty string.
89 // Construct a string with UTF-16 data.
90 WTF_EXPORT_STRING_API String(const UChar* characters, unsigned length);
92 // Construct a string by copying the contents of a vector. To avoid
93 // copying, consider using String::adopt instead.
94 // This method will never create a null string. Vectors with size() == 0
95 // will return the empty string.
96 // NOTE: This is different from String(vector.data(), vector.size())
97 // which will sometimes return a null string when vector.data() is null
98 // which can only occur for vectors without inline capacity.
99 // See: https://bugs.webkit.org/show_bug.cgi?id=109792
100 template<size_t inlineCapacity, typename OverflowHandler>
101 explicit String(const Vector<UChar, inlineCapacity, OverflowHandler>&);
103 // Construct a string with UTF-16 data, from a null-terminated source.
104 WTF_EXPORT_STRING_API String(const UChar*);
106 // Construct a string with latin1 data.
107 WTF_EXPORT_STRING_API String(const LChar* characters, unsigned length);
108 WTF_EXPORT_STRING_API String(const char* characters, unsigned length);
110 // Construct a string with latin1 data, from a null-terminated source.
111 WTF_EXPORT_STRING_API String(const LChar* characters);
112 WTF_EXPORT_STRING_API String(const char* characters);
114 // Construct a string referencing an existing StringImpl.
117 String(PassRefPtr<StringImpl>);
118 String(Ref<StringImpl>&&);
119 String(RefPtr<StringImpl>&&);
121 String(Ref<AtomicStringImpl>&&);
122 String(RefPtr<AtomicStringImpl>&&);
124 // Construct a string from a constant string literal.
125 WTF_EXPORT_STRING_API String(ASCIILiteral characters);
127 // Construct a string from a constant string literal.
128 // This constructor is the "big" version, as it put the length in the function call and generate bigger code.
129 enum ConstructFromLiteralTag { ConstructFromLiteral };
130 template<unsigned charactersCount>
131 String(const char (&characters)[charactersCount], ConstructFromLiteralTag) : m_impl(StringImpl::createFromLiteral<charactersCount>(characters)) { }
133 // We have to declare the copy constructor and copy assignment operator as well, otherwise
134 // they'll be implicitly deleted by adding the move constructor and move assignment operator.
135 String(const String& other) : m_impl(other.m_impl) { }
136 String(String&& other) : m_impl(other.m_impl.release()) { }
137 String& operator=(const String& other) { m_impl = other.m_impl; return *this; }
138 String& operator=(String&& other) { m_impl = other.m_impl.release(); return *this; }
140 // Inline the destructor.
141 ALWAYS_INLINE ~String() { }
143 void swap(String& o) { m_impl.swap(o.m_impl); }
145 static String adopt(StringBuffer<LChar>& buffer) { return StringImpl::adopt(buffer); }
146 static String adopt(StringBuffer<UChar>& buffer) { return StringImpl::adopt(buffer); }
147 template<typename CharacterType, size_t inlineCapacity, typename OverflowHandler>
148 static String adopt(Vector<CharacterType, inlineCapacity, OverflowHandler>& vector) { return StringImpl::adopt(vector); }
150 bool isNull() const { return !m_impl; }
151 bool isEmpty() const { return !m_impl || !m_impl->length(); }
153 StringImpl* impl() const { return m_impl.get(); }
154 PassRefPtr<StringImpl> releaseImpl() { return m_impl.release(); }
156 unsigned length() const
160 return m_impl->length();
163 const LChar* characters8() const
167 ASSERT(m_impl->is8Bit());
168 return m_impl->characters8();
171 const UChar* characters16() const
175 ASSERT(!m_impl->is8Bit());
176 return m_impl->characters16();
179 // Return characters8() or characters16() depending on CharacterType.
180 template <typename CharacterType>
181 inline const CharacterType* characters() const;
183 bool is8Bit() const { return m_impl->is8Bit(); }
185 unsigned sizeInBytes() const
189 return m_impl->length() * (is8Bit() ? sizeof(LChar) : sizeof(UChar));
192 WTF_EXPORT_STRING_API CString ascii() const;
193 WTF_EXPORT_STRING_API CString latin1() const;
195 WTF_EXPORT_STRING_API CString utf8(ConversionMode) const;
196 WTF_EXPORT_STRING_API CString utf8() const;
198 UChar at(unsigned index) const
200 if (!m_impl || index >= m_impl->length())
202 return (*m_impl)[index];
204 UChar operator[](unsigned index) const { return at(index); }
206 WTF_EXPORT_STRING_API static String number(int);
207 WTF_EXPORT_STRING_API static String number(unsigned int);
208 WTF_EXPORT_STRING_API static String number(long);
209 WTF_EXPORT_STRING_API static String number(unsigned long);
210 WTF_EXPORT_STRING_API static String number(long long);
211 WTF_EXPORT_STRING_API static String number(unsigned long long);
213 WTF_EXPORT_STRING_API static String number(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros);
215 // Number to String conversion following the ECMAScript definition.
216 WTF_EXPORT_STRING_API static String numberToStringECMAScript(double);
217 WTF_EXPORT_STRING_API static String numberToStringFixedWidth(double, unsigned decimalPlaces);
219 // Find a single character or string, also with match function & latin1 forms.
220 size_t find(UChar c, unsigned start = 0) const
221 { return m_impl ? m_impl->find(c, start) : notFound; }
223 size_t find(const String& str) const
224 { return m_impl ? m_impl->find(str.impl()) : notFound; }
225 size_t find(const String& str, unsigned start) const
226 { return m_impl ? m_impl->find(str.impl(), start) : notFound; }
227 size_t findIgnoringASCIICase(const String& str) const
228 { return m_impl ? m_impl->findIgnoringASCIICase(str.impl()) : notFound; }
229 size_t findIgnoringASCIICase(const String& str, unsigned startOffset) const
230 { return m_impl ? m_impl->findIgnoringASCIICase(str.impl(), startOffset) : notFound; }
232 size_t find(CharacterMatchFunctionPtr matchFunction, unsigned start = 0) const
233 { return m_impl ? m_impl->find(matchFunction, start) : notFound; }
234 size_t find(const LChar* str, unsigned start = 0) const
235 { return m_impl ? m_impl->find(str, start) : notFound; }
237 size_t findNextLineStart(unsigned start = 0) const
238 { return m_impl ? m_impl->findNextLineStart(start) : notFound; }
240 // Find the last instance of a single character or string.
241 size_t reverseFind(UChar c, unsigned start = UINT_MAX) const
242 { return m_impl ? m_impl->reverseFind(c, start) : notFound; }
243 size_t reverseFind(const String& str, unsigned start = UINT_MAX) const
244 { return m_impl ? m_impl->reverseFind(str.impl(), start) : notFound; }
246 // Case insensitive string matching.
247 size_t findIgnoringCase(const LChar* str, unsigned start = 0) const
248 { return m_impl ? m_impl->findIgnoringCase(str, start) : notFound; }
249 size_t findIgnoringCase(const String& str, unsigned start = 0) const
250 { return m_impl ? m_impl->findIgnoringCase(str.impl(), start) : notFound; }
251 size_t reverseFindIgnoringCase(const String& str, unsigned start = UINT_MAX) const
252 { return m_impl ? m_impl->reverseFindIgnoringCase(str.impl(), start) : notFound; }
254 // Wrappers for find & reverseFind adding dynamic sensitivity check.
255 size_t find(const LChar* str, unsigned start, bool caseSensitive) const
256 { return caseSensitive ? find(str, start) : findIgnoringCase(str, start); }
257 size_t find(const String& str, unsigned start, bool caseSensitive) const
258 { return caseSensitive ? find(str, start) : findIgnoringCase(str, start); }
259 size_t reverseFind(const String& str, unsigned start, bool caseSensitive) const
260 { return caseSensitive ? reverseFind(str, start) : reverseFindIgnoringCase(str, start); }
262 WTF_EXPORT_STRING_API Vector<UChar> charactersWithNullTermination() const;
264 WTF_EXPORT_STRING_API UChar32 characterStartingAt(unsigned) const; // Ditto.
266 bool contains(UChar c) const { return find(c) != notFound; }
267 bool contains(const LChar* str, bool caseSensitive = true, unsigned startOffset = 0) const
268 { return find(str, startOffset, caseSensitive) != notFound; }
269 bool contains(const String& str) const
270 { return find(str) != notFound; }
271 bool contains(const String& str, bool caseSensitive, unsigned startOffset = 0) const
272 { return find(str, startOffset, caseSensitive) != notFound; }
273 bool containsIgnoringASCIICase(const String& str) const
274 { return findIgnoringASCIICase(str) != notFound; }
275 bool containsIgnoringASCIICase(const String& str, unsigned startOffset) const
276 { return findIgnoringASCIICase(str, startOffset) != notFound; }
278 bool startsWith(const String& s) const
279 { return m_impl ? m_impl->startsWith(s.impl()) : s.isEmpty(); }
280 bool startsWithIgnoringASCIICase(const String& s) const
281 { return m_impl ? m_impl->startsWithIgnoringASCIICase(s.impl()) : s.isEmpty(); }
282 bool startsWith(const String& s, bool caseSensitive) const
283 { return m_impl ? m_impl->startsWith(s.impl(), caseSensitive) : s.isEmpty(); }
284 bool startsWith(UChar character) const
285 { return m_impl ? m_impl->startsWith(character) : false; }
286 template<unsigned matchLength>
287 bool startsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const
288 { return m_impl ? m_impl->startsWith<matchLength>(prefix, caseSensitive) : !matchLength; }
289 bool hasInfixStartingAt(const String& prefix, unsigned startOffset) const
290 { return m_impl && prefix.impl() ? m_impl->hasInfixStartingAt(*prefix.impl(), startOffset) : false; }
292 bool endsWith(const String& s) const
293 { return m_impl ? m_impl->endsWith(s.impl()) : s.isEmpty(); }
294 bool endsWithIgnoringASCIICase(const String& s) const
295 { return m_impl ? m_impl->endsWithIgnoringASCIICase(s.impl()) : s.isEmpty(); }
296 bool endsWith(const String& s, bool caseSensitive) const
297 { return m_impl ? m_impl->endsWith(s.impl(), caseSensitive) : s.isEmpty(); }
298 bool endsWith(UChar character) const
299 { return m_impl ? m_impl->endsWith(character) : false; }
300 bool endsWith(char character) const { return endsWith(static_cast<UChar>(character)); }
301 template<unsigned matchLength>
302 bool endsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const
303 { return m_impl ? m_impl->endsWith<matchLength>(prefix, caseSensitive) : !matchLength; }
304 bool hasInfixEndingAt(const String& suffix, unsigned endOffset) const
305 { return m_impl && suffix.impl() ? m_impl->hasInfixEndingAt(*suffix.impl(), endOffset) : false; }
307 WTF_EXPORT_STRING_API void append(const String&);
308 WTF_EXPORT_STRING_API void append(LChar);
309 void append(char c) { append(static_cast<LChar>(c)); };
310 WTF_EXPORT_STRING_API void append(UChar);
311 WTF_EXPORT_STRING_API void append(const LChar*, unsigned length);
312 WTF_EXPORT_STRING_API void append(const UChar*, unsigned length);
313 WTF_EXPORT_STRING_API void insert(const String&, unsigned pos);
315 String& replace(UChar a, UChar b) { if (m_impl) m_impl = m_impl->replace(a, b); return *this; }
316 String& replace(UChar a, const String& b) { if (m_impl) m_impl = m_impl->replace(a, b.impl()); return *this; }
317 String& replace(const String& a, const String& b) { if (m_impl) m_impl = m_impl->replace(a.impl(), b.impl()); return *this; }
318 String& replace(unsigned index, unsigned len, const String& b) { if (m_impl) m_impl = m_impl->replace(index, len, b.impl()); return *this; }
320 template<unsigned charactersCount>
321 ALWAYS_INLINE String& replaceWithLiteral(UChar a, const char (&characters)[charactersCount])
324 m_impl = m_impl->replace(a, characters, charactersCount - 1);
329 WTF_EXPORT_STRING_API void truncate(unsigned len);
330 WTF_EXPORT_STRING_API void remove(unsigned pos, int len = 1);
332 WTF_EXPORT_STRING_API String substring(unsigned pos, unsigned len = UINT_MAX) const;
333 WTF_EXPORT_STRING_API String substringSharingImpl(unsigned pos, unsigned len = UINT_MAX) const;
334 String left(unsigned len) const { return substring(0, len); }
335 String right(unsigned len) const { return substring(length() - len, len); }
337 // Returns a lowercase/uppercase version of the string.
338 // The convertToASCIILowercase is useful in many contexts such as HTML where we don't
339 // want to do any conversion for non-ASCII letters.
340 WTF_EXPORT_STRING_API String convertToASCIILowercase() const;
341 WTF_EXPORT_STRING_API String lower() const;
342 WTF_EXPORT_STRING_API String convertToASCIIUppercase() const;
343 WTF_EXPORT_STRING_API String upper() const;
345 WTF_EXPORT_STRING_API String lower(const AtomicString& localeIdentifier) const;
346 WTF_EXPORT_STRING_API String upper(const AtomicString& localeIdentifier) const;
348 WTF_EXPORT_STRING_API String stripWhiteSpace() const;
349 WTF_EXPORT_STRING_API String stripWhiteSpace(IsWhiteSpaceFunctionPtr) const;
350 WTF_EXPORT_STRING_API String simplifyWhiteSpace() const;
351 WTF_EXPORT_STRING_API String simplifyWhiteSpace(IsWhiteSpaceFunctionPtr) const;
353 WTF_EXPORT_STRING_API String removeCharacters(CharacterMatchFunctionPtr) const;
354 template<bool isSpecialCharacter(UChar)> bool isAllSpecialCharacters() const;
356 // Returns the string with case folded for case insensitive comparison.
357 // Use convertToASCIILowercase instead if ASCII case insensitive comparison is desired.
358 WTF_EXPORT_STRING_API String foldCase() const;
360 WTF_EXPORT_STRING_API static String format(const char *, ...) WTF_ATTRIBUTE_PRINTF(1, 2);
362 // Returns an uninitialized string. The characters needs to be written
363 // into the buffer returned in data before the returned string is used.
364 // Failure to do this will have unpredictable results.
365 static String createUninitialized(unsigned length, UChar*& data) { return StringImpl::createUninitialized(length, data); }
366 static String createUninitialized(unsigned length, LChar*& data) { return StringImpl::createUninitialized(length, data); }
368 WTF_EXPORT_STRING_API void split(const String& separator, bool allowEmptyEntries, Vector<String>& result) const;
369 void split(const String& separator, Vector<String>& result) const
371 split(separator, false, result);
373 WTF_EXPORT_STRING_API void split(UChar separator, bool allowEmptyEntries, Vector<String>& result) const;
374 void split(UChar separator, Vector<String>& result) const
376 split(separator, false, result);
379 WTF_EXPORT_STRING_API int toIntStrict(bool* ok = 0, int base = 10) const;
380 WTF_EXPORT_STRING_API unsigned toUIntStrict(bool* ok = 0, int base = 10) const;
381 WTF_EXPORT_STRING_API int64_t toInt64Strict(bool* ok = 0, int base = 10) const;
382 WTF_EXPORT_STRING_API uint64_t toUInt64Strict(bool* ok = 0, int base = 10) const;
383 WTF_EXPORT_STRING_API intptr_t toIntPtrStrict(bool* ok = 0, int base = 10) const;
385 WTF_EXPORT_STRING_API int toInt(bool* ok = 0) const;
386 WTF_EXPORT_STRING_API unsigned toUInt(bool* ok = 0) const;
387 WTF_EXPORT_STRING_API int64_t toInt64(bool* ok = 0) const;
388 WTF_EXPORT_STRING_API uint64_t toUInt64(bool* ok = 0) const;
389 WTF_EXPORT_STRING_API intptr_t toIntPtr(bool* ok = 0) const;
391 // FIXME: Like the strict functions above, these give false for "ok" when there is trailing garbage.
392 // Like the non-strict functions above, these return the value when there is trailing garbage.
393 // It would be better if these were more consistent with the above functions instead.
394 WTF_EXPORT_STRING_API double toDouble(bool* ok = 0) const;
395 WTF_EXPORT_STRING_API float toFloat(bool* ok = 0) const;
397 bool percentage(int& percentage) const;
399 #if COMPILER_SUPPORTS(CXX_REFERENCE_QUALIFIED_FUNCTIONS)
400 WTF_EXPORT_STRING_API String isolatedCopy() const &;
401 WTF_EXPORT_STRING_API String isolatedCopy() &&;
403 WTF_EXPORT_STRING_API String isolatedCopy() const;
406 WTF_EXPORT_STRING_API bool isSafeToSendToAnotherThread() const;
408 // Prevent Strings from being implicitly convertable to bool as it will be ambiguous on any platform that
409 // allows implicit conversion to another pointer type (e.g., Mac allows implicit conversion to NSString*).
410 typedef struct ImplicitConversionFromWTFStringToBoolDisallowedA* (String::*UnspecifiedBoolTypeA);
411 typedef struct ImplicitConversionFromWTFStringToBoolDisallowedB* (String::*UnspecifiedBoolTypeB);
412 operator UnspecifiedBoolTypeA() const;
413 operator UnspecifiedBoolTypeB() const;
416 WTF_EXPORT_STRING_API String(CFStringRef);
417 WTF_EXPORT_STRING_API RetainPtr<CFStringRef> createCFString() const;
421 WTF_EXPORT_STRING_API String(NSString*);
423 // This conversion maps NULL to "", which loses the meaning of NULL, but we
424 // need this mapping because AppKit crashes when passed nil NSStrings.
425 operator NSString*() const { if (!m_impl) return @""; return *m_impl; }
428 WTF_EXPORT_STRING_API static String make8BitFrom16BitSource(const UChar*, size_t);
429 template<size_t inlineCapacity>
430 static String make8BitFrom16BitSource(const Vector<UChar, inlineCapacity>& buffer)
432 return make8BitFrom16BitSource(buffer.data(), buffer.size());
435 WTF_EXPORT_STRING_API static String make16BitFrom8BitSource(const LChar*, size_t);
437 // String::fromUTF8 will return a null string if
438 // the input data contains invalid UTF-8 sequences.
439 WTF_EXPORT_STRING_API static String fromUTF8(const LChar*, size_t);
440 WTF_EXPORT_STRING_API static String fromUTF8(const LChar*);
441 static String fromUTF8(const char* s, size_t length) { return fromUTF8(reinterpret_cast<const LChar*>(s), length); };
442 static String fromUTF8(const char* s) { return fromUTF8(reinterpret_cast<const LChar*>(s)); };
443 WTF_EXPORT_STRING_API static String fromUTF8(const CString&);
445 // 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.
446 WTF_EXPORT_STRING_API static String fromUTF8WithLatin1Fallback(const LChar*, size_t);
447 static String fromUTF8WithLatin1Fallback(const char* s, size_t length) { return fromUTF8WithLatin1Fallback(reinterpret_cast<const LChar*>(s), length); };
449 // Determines the writing direction using the Unicode Bidi Algorithm rules P2 and P3.
450 UCharDirection defaultWritingDirection(bool* hasStrongDirectionality = nullptr) const
453 return m_impl->defaultWritingDirection(hasStrongDirectionality);
454 if (hasStrongDirectionality)
455 *hasStrongDirectionality = false;
456 return U_LEFT_TO_RIGHT;
459 bool containsOnlyASCII() const;
460 bool containsOnlyLatin1() const;
461 bool containsOnlyWhitespace() const { return !m_impl || m_impl->containsOnlyWhitespace(); }
463 // Hash table deleted values, which are only constructed and never copied or destroyed.
464 String(WTF::HashTableDeletedValueType) : m_impl(WTF::HashTableDeletedValue) { }
465 bool isHashTableDeletedValue() const { return m_impl.isHashTableDeletedValue(); }
467 unsigned existingHash() const { return isNull() ? 0 : impl()->existingHash(); }
470 WTF_EXPORT_STRING_API void show() const;
473 // Workaround for a compiler bug. Use operator[] instead.
474 UChar characterAt(unsigned index) const
476 if (!m_impl || index >= m_impl->length())
478 return (*m_impl)[index];
482 template <typename CharacterType>
483 void removeInternal(const CharacterType*, unsigned, int);
485 template <typename CharacterType>
486 void appendInternal(CharacterType);
488 RefPtr<StringImpl> m_impl;
491 inline bool operator==(const String& a, const String& b) { return equal(a.impl(), b.impl()); }
492 inline bool operator==(const String& a, const LChar* b) { return equal(a.impl(), b); }
493 inline bool operator==(const String& a, const char* b) { return equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
494 inline bool operator==(const LChar* a, const String& b) { return equal(a, b.impl()); }
495 inline bool operator==(const char* a, const String& b) { return equal(reinterpret_cast<const LChar*>(a), b.impl()); }
496 template<size_t inlineCapacity> inline bool operator==(const Vector<char, inlineCapacity>& a, const String& b) { return equal(b.impl(), a.data(), a.size()); }
497 template<size_t inlineCapacity> inline bool operator==(const String& a, const Vector<char, inlineCapacity>& b) { return b == a; }
500 inline bool operator!=(const String& a, const String& b) { return !equal(a.impl(), b.impl()); }
501 inline bool operator!=(const String& a, const LChar* b) { return !equal(a.impl(), b); }
502 inline bool operator!=(const String& a, const char* b) { return !equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
503 inline bool operator!=(const LChar* a, const String& b) { return !equal(a, b.impl()); }
504 inline bool operator!=(const char* a, const String& b) { return !equal(reinterpret_cast<const LChar*>(a), b.impl()); }
505 template<size_t inlineCapacity> inline bool operator!=(const Vector<char, inlineCapacity>& a, const String& b) { return !(a == b); }
506 template<size_t inlineCapacity> inline bool operator!=(const String& a, const Vector<char, inlineCapacity>& b) { return b != a; }
508 inline bool equalIgnoringCase(const String& a, const String& b) { return equalIgnoringCase(a.impl(), b.impl()); }
509 inline bool equalIgnoringCase(const String& a, const LChar* b) { return equalIgnoringCase(a.impl(), b); }
510 inline bool equalIgnoringCase(const String& a, const char* b) { return equalIgnoringCase(a.impl(), reinterpret_cast<const LChar*>(b)); }
511 inline bool equalIgnoringCase(const LChar* a, const String& b) { return equalIgnoringCase(a, b.impl()); }
512 inline bool equalIgnoringCase(const char* a, const String& b) { return equalIgnoringCase(reinterpret_cast<const LChar*>(a), b.impl()); }
514 bool equalPossiblyIgnoringCase(const String&, const String&, bool ignoreCase);
516 inline bool equalIgnoringASCIICase(const String& a, const String& b) { return equalIgnoringASCIICase(a.impl(), b.impl()); }
517 template<unsigned charactersCount> inline bool equalIgnoringASCIICase(const String& a, const char (&b)[charactersCount]) { return equalIgnoringASCIICase<charactersCount>(a.impl(), b); }
519 template<unsigned length> bool equalLettersIgnoringASCIICase(const String&, const char (&lowercaseLetters)[length]);
521 inline bool equalIgnoringNullity(const String& a, const String& b) { return equalIgnoringNullity(a.impl(), b.impl()); }
522 template<size_t inlineCapacity> inline bool equalIgnoringNullity(const Vector<UChar, inlineCapacity>& a, const String& b) { return equalIgnoringNullity(a, b.impl()); }
524 inline bool operator!(const String& str) { return str.isNull(); }
526 inline void swap(String& a, String& b) { a.swap(b); }
528 // Definitions of string operations
530 inline String::String(StringImpl& impl)
535 inline String::String(StringImpl* impl)
540 inline String::String(PassRefPtr<StringImpl> impl)
545 inline String::String(Ref<StringImpl>&& impl)
546 : m_impl(WTFMove(impl))
550 inline String::String(RefPtr<StringImpl>&& impl)
551 : m_impl(WTFMove(impl))
555 inline String::String(Ref<AtomicStringImpl>&& impl)
556 : m_impl(WTFMove(impl))
560 inline String::String(RefPtr<AtomicStringImpl>&& impl)
561 : m_impl(WTFMove(impl))
565 template<size_t inlineCapacity, typename OverflowHandler>
566 String::String(const Vector<UChar, inlineCapacity, OverflowHandler>& vector)
567 : m_impl(vector.size() ? StringImpl::create(vector.data(), vector.size()) : Ref<StringImpl>(*StringImpl::empty()))
572 inline const LChar* String::characters<LChar>() const
575 return characters8();
579 inline const UChar* String::characters<UChar>() const
582 return characters16();
585 inline bool String::containsOnlyLatin1() const
593 const UChar* characters = characters16();
595 for (size_t i = 0; i < m_impl->length(); ++i)
596 ored |= characters[i];
597 return !(ored & 0xFF00);
602 // This is for situations in WebKit where the long standing behavior has been
603 // "nil if empty", so we try to maintain longstanding behavior for the sake of
604 // entrenched clients
605 inline NSString* nsStringNilIfEmpty(const String& str) { return str.isEmpty() ? nil : (NSString*)str; }
608 inline bool String::containsOnlyASCII() const
614 return charactersAreAllASCII(characters8(), m_impl->length());
616 return charactersAreAllASCII(characters16(), m_impl->length());
619 WTF_EXPORT_STRING_API int codePointCompare(const String&, const String&);
621 inline bool codePointCompareLessThan(const String& a, const String& b)
623 return codePointCompare(a.impl(), b.impl()) < 0;
626 template<typename CharacterType>
627 inline void appendNumber(Vector<CharacterType>& vector, unsigned char number)
629 int numberLength = number > 99 ? 3 : (number > 9 ? 2 : 1);
630 size_t vectorSize = vector.size();
631 vector.grow(vectorSize + numberLength);
633 switch (numberLength) {
635 vector[vectorSize + 2] = number % 10 + '0';
640 vector[vectorSize + 1] = number % 10 + '0';
645 vector[vectorSize] = number % 10 + '0';
649 template<bool isSpecialCharacter(UChar), typename CharacterType>
650 inline bool isAllSpecialCharacters(const CharacterType* characters, size_t length)
652 for (size_t i = 0; i < length; ++i) {
653 if (!isSpecialCharacter(characters[i]))
659 template<bool isSpecialCharacter(UChar)>
660 inline bool String::isAllSpecialCharacters() const
662 size_t len = length();
668 return WTF::isAllSpecialCharacters<isSpecialCharacter, LChar>(characters8(), len);
669 return WTF::isAllSpecialCharacters<isSpecialCharacter, UChar>(characters16(), len);
672 inline bool equalPossiblyIgnoringCase(const String& a, const String& b, bool ignoreCase)
674 return ignoreCase ? equalIgnoringCase(a, b) : (a == b);
677 // StringHash is the default hash for String
678 template<typename T> struct DefaultHash;
679 template<> struct DefaultHash<String> {
680 typedef StringHash Hash;
683 template <> struct VectorTraits<String> : SimpleClassVectorTraits { };
687 explicit ASCIILiteral(const char* characters) : m_characters(characters) { }
688 operator const char*() { return m_characters; }
691 const char* m_characters;
694 // For thread-safe lambda capture:
695 // StringCapture stringCapture(string);
696 // auto lambdaThatRunsInOtherThread = [stringCapture] { String string = stringCapture.string(); ... }
697 // FIXME: Remove when we can use C++14 initialized lambda capture: [string = string.isolatedCopy()].
698 class StringCapture {
701 StringCapture(const String& string) : m_string(string) { }
702 explicit StringCapture(String&& string) : m_string(string) { }
703 StringCapture(const StringCapture& other) : m_string(other.m_string.isolatedCopy()) { }
704 const String& string() const { return m_string; }
705 String releaseString() { return WTFMove(m_string); }
707 void operator=(const StringCapture& other) { m_string = other.m_string.isolatedCopy(); }
713 // Shared global empty string.
714 WTF_EXPORT_STRING_API const String& emptyString();
716 template<unsigned length> inline bool equalLettersIgnoringASCIICase(const String& string, const char (&lowercaseLetters)[length])
718 return equalLettersIgnoringASCIICase(string.impl(), lowercaseLetters);
724 using WTF::KeepTrailingZeros;
726 using WTF::emptyString;
727 using WTF::appendNumber;
728 using WTF::charactersAreAllASCII;
729 using WTF::charactersToIntStrict;
730 using WTF::charactersToUIntStrict;
731 using WTF::charactersToInt64Strict;
732 using WTF::charactersToUInt64Strict;
733 using WTF::charactersToIntPtrStrict;
734 using WTF::charactersToInt;
735 using WTF::charactersToUInt;
736 using WTF::charactersToInt64;
737 using WTF::charactersToUInt64;
738 using WTF::charactersToIntPtr;
739 using WTF::charactersToDouble;
740 using WTF::charactersToFloat;
742 using WTF::equalIgnoringCase;
743 using WTF::equalLettersIgnoringASCIICase;
745 using WTF::isAllSpecialCharacters;
746 using WTF::isSpaceOrNewline;
747 using WTF::reverseFind;
748 using WTF::ASCIILiteral;
749 using WTF::StringCapture;
751 #include <wtf/text/AtomicString.h>