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/HashTableDeletedValueType.h"
29 #include "wtf/WTFExport.h"
30 #include "wtf/text/ASCIIFastPath.h"
31 #include "wtf/text/StringImpl.h"
32 #include "wtf/text/StringView.h"
35 #include <objc/objc.h>
43 // Declarations of string operations
45 WTF_EXPORT int charactersToIntStrict(const LChar*, size_t, bool* ok = 0, int base = 10);
46 WTF_EXPORT int charactersToIntStrict(const UChar*, size_t, bool* ok = 0, int base = 10);
47 WTF_EXPORT unsigned charactersToUIntStrict(const LChar*, size_t, bool* ok = 0, int base = 10);
48 WTF_EXPORT unsigned charactersToUIntStrict(const UChar*, size_t, bool* ok = 0, int base = 10);
49 WTF_EXPORT int64_t charactersToInt64Strict(const LChar*, size_t, bool* ok = 0, int base = 10);
50 WTF_EXPORT int64_t charactersToInt64Strict(const UChar*, size_t, bool* ok = 0, int base = 10);
51 WTF_EXPORT uint64_t charactersToUInt64Strict(const LChar*, size_t, bool* ok = 0, int base = 10);
52 WTF_EXPORT uint64_t charactersToUInt64Strict(const UChar*, size_t, bool* ok = 0, int base = 10);
53 WTF_EXPORT intptr_t charactersToIntPtrStrict(const LChar*, size_t, bool* ok = 0, int base = 10);
54 WTF_EXPORT intptr_t charactersToIntPtrStrict(const UChar*, size_t, bool* ok = 0, int base = 10);
56 WTF_EXPORT int charactersToInt(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
57 WTF_EXPORT int charactersToInt(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
58 WTF_EXPORT unsigned charactersToUInt(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
59 WTF_EXPORT unsigned charactersToUInt(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
60 WTF_EXPORT int64_t charactersToInt64(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
61 WTF_EXPORT int64_t charactersToInt64(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
62 WTF_EXPORT uint64_t charactersToUInt64(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
63 WTF_EXPORT uint64_t charactersToUInt64(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
64 WTF_EXPORT intptr_t charactersToIntPtr(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
65 WTF_EXPORT intptr_t charactersToIntPtr(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
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 double charactersToDouble(const LChar*, size_t, bool* ok = 0);
71 WTF_EXPORT double charactersToDouble(const UChar*, size_t, bool* ok = 0);
72 WTF_EXPORT float charactersToFloat(const LChar*, size_t, bool* ok = 0);
73 WTF_EXPORT float charactersToFloat(const UChar*, size_t, bool* ok = 0);
74 WTF_EXPORT float charactersToFloat(const LChar*, size_t, size_t& parsedLength);
75 WTF_EXPORT float charactersToFloat(const UChar*, size_t, size_t& parsedLength);
77 enum TrailingZerosTruncatingPolicy {
82 enum UTF8ConversionMode {
83 LenientUTF8Conversion,
85 StrictUTF8ConversionReplacingUnpairedSurrogatesWithFFFD
88 template<bool isSpecialCharacter(UChar), typename CharacterType>
89 bool isAllSpecialCharacters(const CharacterType*, size_t);
91 // You can find documentation about this class in this doc:
92 // https://docs.google.com/document/d/1kOCUlJdh2WJMJGDf-WoEQhmnjKLaOYRbiHz5TiGJl14/edit?usp=sharing
93 class WTF_EXPORT String {
95 // Construct a null string, distinguishable from an empty string.
98 // Construct a string with UTF-16 data.
99 String(const UChar* characters, unsigned length);
101 // Construct a string by copying the contents of a vector.
102 // This method will never create a null string. Vectors with size() == 0
103 // will return the empty string.
104 // NOTE: This is different from String(vector.data(), vector.size())
105 // which will sometimes return a null string when vector.data() is null
106 // which can only occur for vectors without inline capacity.
107 // See: https://bugs.webkit.org/show_bug.cgi?id=109792
108 template<size_t inlineCapacity>
109 explicit String(const Vector<UChar, inlineCapacity>&);
111 // Construct a string with UTF-16 data, from a null-terminated source.
112 String(const UChar*);
114 // Construct a string with latin1 data.
115 String(const LChar* characters, unsigned length);
116 String(const char* characters, unsigned length);
118 // Construct a string with latin1 data, from a null-terminated source.
119 String(const LChar* characters);
120 String(const char* characters);
122 // Construct a string referencing an existing StringImpl.
123 String(StringImpl* impl) : m_impl(impl) { }
124 String(PassRefPtr<StringImpl> impl) : m_impl(impl) { }
126 void swap(String& o) { m_impl.swap(o.m_impl); }
128 template<typename CharType>
129 static String adopt(StringBuffer<CharType>& buffer)
131 if (!buffer.length())
132 return StringImpl::empty();
133 return String(buffer.release());
136 bool isNull() const { return !m_impl; }
137 bool isEmpty() const { return !m_impl || !m_impl->length(); }
139 StringImpl* impl() const { return m_impl.get(); }
140 PassRefPtr<StringImpl> releaseImpl() { return m_impl.release(); }
142 unsigned length() const
146 return m_impl->length();
149 const LChar* characters8() const
153 ASSERT(m_impl->is8Bit());
154 return m_impl->characters8();
157 const UChar* characters16() const
161 ASSERT(!m_impl->is8Bit());
162 return m_impl->characters16();
165 // Return characters8() or characters16() depending on CharacterType.
166 template <typename CharacterType>
167 inline const CharacterType* getCharacters() const;
169 bool is8Bit() const { return m_impl->is8Bit(); }
171 unsigned sizeInBytes() const
175 return m_impl->length() * (is8Bit() ? sizeof(LChar) : sizeof(UChar));
178 CString ascii() const;
179 CString latin1() const;
180 CString utf8(UTF8ConversionMode = LenientUTF8Conversion) const;
182 UChar operator[](unsigned index) const
184 if (!m_impl || index >= m_impl->length())
186 return (*m_impl)[index];
189 static String number(int);
190 static String number(unsigned);
191 static String number(long);
192 static String number(unsigned long);
193 static String number(long long);
194 static String number(unsigned long long);
196 static String number(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros);
198 // Number to String conversion following the ECMAScript definition.
199 static String numberToStringECMAScript(double);
200 static String numberToStringFixedWidth(double, unsigned decimalPlaces);
202 // Find a single character or string, also with match function & latin1 forms.
203 size_t find(UChar c, unsigned start = 0) const
204 { return m_impl ? m_impl->find(c, start) : kNotFound; }
206 size_t find(const String& str) const
207 { return m_impl ? m_impl->find(str.impl()) : kNotFound; }
208 size_t find(const String& str, unsigned start) const
209 { return m_impl ? m_impl->find(str.impl(), start) : kNotFound; }
211 size_t find(CharacterMatchFunctionPtr matchFunction, unsigned start = 0) const
212 { return m_impl ? m_impl->find(matchFunction, start) : kNotFound; }
213 size_t find(const LChar* str, unsigned start = 0) const
214 { return m_impl ? m_impl->find(str, start) : kNotFound; }
216 size_t findNextLineStart(unsigned start = 0) const
217 { return m_impl ? m_impl->findNextLineStart(start) : kNotFound; }
219 // Find the last instance of a single character or string.
220 size_t reverseFind(UChar c, unsigned start = UINT_MAX) const
221 { return m_impl ? m_impl->reverseFind(c, start) : kNotFound; }
222 size_t reverseFind(const String& str, unsigned start = UINT_MAX) const
223 { return m_impl ? m_impl->reverseFind(str.impl(), start) : kNotFound; }
225 // Case insensitive string matching.
226 size_t findIgnoringCase(const LChar* str, unsigned start = 0) const
227 { return m_impl ? m_impl->findIgnoringCase(str, start) : kNotFound; }
228 size_t findIgnoringCase(const String& str, unsigned start = 0) const
229 { return m_impl ? m_impl->findIgnoringCase(str.impl(), start) : kNotFound; }
230 size_t reverseFindIgnoringCase(const String& str, unsigned start = UINT_MAX) const
231 { return m_impl ? m_impl->reverseFindIgnoringCase(str.impl(), start) : kNotFound; }
233 // Wrappers for find & reverseFind adding dynamic sensitivity check.
234 size_t find(const LChar* str, unsigned start, bool caseSensitive) const
235 { return caseSensitive ? find(str, start) : findIgnoringCase(str, start); }
236 size_t find(const String& str, unsigned start, bool caseSensitive) const
237 { return caseSensitive ? find(str, start) : findIgnoringCase(str, start); }
238 size_t reverseFind(const String& str, unsigned start, bool caseSensitive) const
239 { return caseSensitive ? reverseFind(str, start) : reverseFindIgnoringCase(str, start); }
241 Vector<UChar> charactersWithNullTermination() const;
242 unsigned copyTo(UChar* buffer, unsigned pos, unsigned maxLength) const;
244 template<size_t inlineCapacity>
245 void appendTo(Vector<UChar, inlineCapacity>&, unsigned pos = 0, unsigned len = UINT_MAX) const;
247 template<typename BufferType>
248 void appendTo(BufferType&, unsigned pos = 0, unsigned len = UINT_MAX) const;
250 template<size_t inlineCapacity>
251 void prependTo(Vector<UChar, inlineCapacity>&, unsigned pos = 0, unsigned len = UINT_MAX) const;
253 UChar32 characterStartingAt(unsigned) const;
255 bool contains(UChar c) const { return find(c) != kNotFound; }
256 bool contains(const LChar* str, bool caseSensitive = true) const { return find(str, 0, caseSensitive) != kNotFound; }
257 bool contains(const String& str, bool caseSensitive = true) const { return find(str, 0, caseSensitive) != kNotFound; }
259 bool startsWith(const String& s, bool caseSensitive = true) const
260 { return m_impl ? m_impl->startsWith(s.impl(), caseSensitive) : s.isEmpty(); }
261 bool startsWith(UChar character) const
262 { return m_impl ? m_impl->startsWith(character) : false; }
263 template<unsigned matchLength>
264 bool startsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const
265 { return m_impl ? m_impl->startsWith<matchLength>(prefix, caseSensitive) : !matchLength; }
267 bool endsWith(const String& s, bool caseSensitive = true) const
268 { return m_impl ? m_impl->endsWith(s.impl(), caseSensitive) : s.isEmpty(); }
269 bool endsWith(UChar character) const
270 { return m_impl ? m_impl->endsWith(character) : false; }
271 template<unsigned matchLength>
272 bool endsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const
273 { return m_impl ? m_impl->endsWith<matchLength>(prefix, caseSensitive) : !matchLength; }
275 void append(const String&);
277 void append(char c) { append(static_cast<LChar>(c)); }
279 void append(const LChar*, unsigned length);
280 void append(const char* charactersToAppend, unsigned length) { append(reinterpret_cast<const LChar*>(charactersToAppend), length); }
281 void append(const UChar*, unsigned length);
282 void insert(const String&, unsigned pos);
283 void insert(const LChar*, unsigned length, unsigned pos);
284 void insert(const UChar*, unsigned length, unsigned pos);
286 String& replace(UChar a, UChar b) { if (m_impl) m_impl = m_impl->replace(a, b); return *this; }
287 String& replace(UChar a, const String& b) { if (m_impl) m_impl = m_impl->replace(a, b.impl()); return *this; }
288 String& replace(const String& a, const String& b) { if (m_impl) m_impl = m_impl->replace(a.impl(), b.impl()); return *this; }
289 String& replace(unsigned index, unsigned len, const String& b) { if (m_impl) m_impl = m_impl->replace(index, len, b.impl()); return *this; }
291 template<unsigned charactersCount>
292 ALWAYS_INLINE String& replaceWithLiteral(UChar a, const char (&characters)[charactersCount])
295 m_impl = m_impl->replace(a, characters, charactersCount - 1);
300 void fill(UChar c) { if (m_impl) m_impl = m_impl->fill(c); }
304 void truncate(unsigned len);
305 void remove(unsigned pos, int len = 1);
307 String substring(unsigned pos, unsigned len = UINT_MAX) const;
308 String left(unsigned len) const { return substring(0, len); }
309 String right(unsigned len) const { return substring(length() - len, len); }
311 StringView createView() const { return StringView(impl()); }
312 StringView createView(unsigned offset, unsigned length) const { return StringView(impl(), offset, length); }
314 // Returns a lowercase/uppercase version of the string
315 String lower() const;
316 String upper() const;
318 String lower(const AtomicString& localeIdentifier) const;
319 String upper(const AtomicString& localeIdentifier) const;
321 String stripWhiteSpace() const;
322 String stripWhiteSpace(IsWhiteSpaceFunctionPtr) const;
323 String simplifyWhiteSpace(StripBehavior stripBehavior = StripExtraWhiteSpace) const;
324 String simplifyWhiteSpace(IsWhiteSpaceFunctionPtr, StripBehavior stripBehavior = StripExtraWhiteSpace) const;
326 String removeCharacters(CharacterMatchFunctionPtr) const;
327 template<bool isSpecialCharacter(UChar)> bool isAllSpecialCharacters() const;
329 // Return the string with case folded for case insensitive comparison.
330 String foldCase() const;
332 static String format(const char *, ...) WTF_ATTRIBUTE_PRINTF(1, 2);
334 // Returns an uninitialized string. The characters needs to be written
335 // into the buffer returned in data before the returned string is used.
336 // Failure to do this will have unpredictable results.
337 static String createUninitialized(unsigned length, UChar*& data) { return StringImpl::createUninitialized(length, data); }
338 static String createUninitialized(unsigned length, LChar*& data) { return StringImpl::createUninitialized(length, data); }
340 void split(const String& separator, bool allowEmptyEntries, Vector<String>& result) const;
341 void split(const String& separator, Vector<String>& result) const
343 split(separator, false, result);
345 void split(UChar separator, bool allowEmptyEntries, Vector<String>& result) const;
346 void split(UChar separator, Vector<String>& result) const
348 split(separator, false, result);
351 int toIntStrict(bool* ok = 0, int base = 10) const;
352 unsigned toUIntStrict(bool* ok = 0, int base = 10) const;
353 int64_t toInt64Strict(bool* ok = 0, int base = 10) const;
354 uint64_t toUInt64Strict(bool* ok = 0, int base = 10) const;
355 intptr_t toIntPtrStrict(bool* ok = 0, int base = 10) const;
357 int toInt(bool* ok = 0) const;
358 unsigned toUInt(bool* ok = 0) const;
359 int64_t toInt64(bool* ok = 0) const;
360 uint64_t toUInt64(bool* ok = 0) const;
361 intptr_t toIntPtr(bool* ok = 0) const;
363 // FIXME: Like the strict functions above, these give false for "ok" when there is trailing garbage.
364 // Like the non-strict functions above, these return the value when there is trailing garbage.
365 // It would be better if these were more consistent with the above functions instead.
366 double toDouble(bool* ok = 0) const;
367 float toFloat(bool* ok = 0) const;
369 bool percentage(int& percentage) const;
371 String isolatedCopy() const;
372 bool isSafeToSendToAnotherThread() const;
376 RetainPtr<CFStringRef> createCFString() const;
382 // This conversion maps NULL to "", which loses the meaning of NULL, but we
383 // need this mapping because AppKit crashes when passed nil NSStrings.
384 operator NSString*() const { if (!m_impl) return @""; return *m_impl; }
387 static String make8BitFrom16BitSource(const UChar*, size_t);
388 template<size_t inlineCapacity>
389 static String make8BitFrom16BitSource(const Vector<UChar, inlineCapacity>& buffer)
391 return make8BitFrom16BitSource(buffer.data(), buffer.size());
394 static String make16BitFrom8BitSource(const LChar*, size_t);
396 // String::fromUTF8 will return a null string if
397 // the input data contains invalid UTF-8 sequences.
398 static String fromUTF8(const LChar*, size_t);
399 static String fromUTF8(const LChar*);
400 static String fromUTF8(const char* s, size_t length) { return fromUTF8(reinterpret_cast<const LChar*>(s), length); };
401 static String fromUTF8(const char* s) { return fromUTF8(reinterpret_cast<const LChar*>(s)); };
402 static String fromUTF8(const CString&);
404 // 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.
405 static String fromUTF8WithLatin1Fallback(const LChar*, size_t);
406 static String fromUTF8WithLatin1Fallback(const char* s, size_t length) { return fromUTF8WithLatin1Fallback(reinterpret_cast<const LChar*>(s), length); };
408 bool containsOnlyASCII() const;
409 bool containsOnlyLatin1() const;
410 bool containsOnlyWhitespace() const { return !m_impl || m_impl->containsOnlyWhitespace(); }
412 // Hash table deleted values, which are only constructed and never copied or destroyed.
413 String(WTF::HashTableDeletedValueType) : m_impl(WTF::HashTableDeletedValue) { }
414 bool isHashTableDeletedValue() const { return m_impl.isHashTableDeletedValue(); }
420 // Workaround for a compiler bug. Use operator[] instead.
421 UChar characterAt(unsigned index) const
423 if (!m_impl || index >= m_impl->length())
425 return (*m_impl)[index];
429 typedef struct ImplicitConversionFromWTFStringToBoolDisallowed* (String::*UnspecifiedBoolType);
430 operator UnspecifiedBoolType() const;
432 template <typename CharacterType>
433 void removeInternal(const CharacterType*, unsigned, int);
435 template <typename CharacterType>
436 void appendInternal(CharacterType);
438 RefPtr<StringImpl> m_impl;
441 inline bool operator==(const String& a, const String& b) { return equal(a.impl(), b.impl()); }
442 inline bool operator==(const String& a, const LChar* b) { return equal(a.impl(), b); }
443 inline bool operator==(const String& a, const char* b) { return equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
444 inline bool operator==(const LChar* a, const String& b) { return equal(a, b.impl()); }
445 inline bool operator==(const char* a, const String& b) { return equal(reinterpret_cast<const LChar*>(a), b.impl()); }
446 template<size_t inlineCapacity>
447 inline bool operator==(const Vector<char, inlineCapacity>& a, const String& b) { return equal(b.impl(), a.data(), a.size()); }
448 template<size_t inlineCapacity>
449 inline bool operator==(const String& a, const Vector<char, inlineCapacity>& b) { return b == a; }
452 inline bool operator!=(const String& a, const String& b) { return !equal(a.impl(), b.impl()); }
453 inline bool operator!=(const String& a, const LChar* b) { return !equal(a.impl(), b); }
454 inline bool operator!=(const String& a, const char* b) { return !equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
455 inline bool operator!=(const LChar* a, const String& b) { return !equal(a, b.impl()); }
456 inline bool operator!=(const char* a, const String& b) { return !equal(reinterpret_cast<const LChar*>(a), b.impl()); }
457 template<size_t inlineCapacity>
458 inline bool operator!=(const Vector<char, inlineCapacity>& a, const String& b) { return !(a == b); }
459 template<size_t inlineCapacity>
460 inline bool operator!=(const String& a, const Vector<char, inlineCapacity>& b) { return b != a; }
462 inline bool equalIgnoringCase(const String& a, const String& b) { return equalIgnoringCase(a.impl(), b.impl()); }
463 inline bool equalIgnoringCase(const String& a, const LChar* b) { return equalIgnoringCase(a.impl(), b); }
464 inline bool equalIgnoringCase(const String& a, const char* b) { return equalIgnoringCase(a.impl(), reinterpret_cast<const LChar*>(b)); }
465 inline bool equalIgnoringCase(const LChar* a, const String& b) { return equalIgnoringCase(a, b.impl()); }
466 inline bool equalIgnoringCase(const char* a, const String& b) { return equalIgnoringCase(reinterpret_cast<const LChar*>(a), b.impl()); }
468 inline bool equalPossiblyIgnoringCase(const String& a, const String& b, bool ignoreCase)
470 return ignoreCase ? equalIgnoringCase(a, b) : (a == b);
473 inline bool equalIgnoringNullity(const String& a, const String& b) { return equalIgnoringNullity(a.impl(), b.impl()); }
475 template<size_t inlineCapacity>
476 inline bool equalIgnoringNullity(const Vector<UChar, inlineCapacity>& a, const String& b) { return equalIgnoringNullity(a, b.impl()); }
478 inline bool operator!(const String& str) { return str.isNull(); }
480 inline void swap(String& a, String& b) { a.swap(b); }
482 // Definitions of string operations
484 template<size_t inlineCapacity>
485 String::String(const Vector<UChar, inlineCapacity>& vector)
486 : m_impl(vector.size() ? StringImpl::create(vector.data(), vector.size()) : StringImpl::empty())
491 inline const LChar* String::getCharacters<LChar>() const
494 return characters8();
498 inline const UChar* String::getCharacters<UChar>() const
501 return characters16();
504 inline bool String::containsOnlyLatin1() const
512 const UChar* characters = characters16();
514 for (size_t i = 0; i < m_impl->length(); ++i)
515 ored |= characters[i];
516 return !(ored & 0xFF00);
521 // This is for situations in WebKit where the long standing behavior has been
522 // "nil if empty", so we try to maintain longstanding behavior for the sake of
523 // entrenched clients
524 inline NSString* nsStringNilIfEmpty(const String& str) { return str.isEmpty() ? nil : (NSString*)str; }
527 inline bool String::containsOnlyASCII() const
533 return charactersAreAllASCII(characters8(), m_impl->length());
535 return charactersAreAllASCII(characters16(), m_impl->length());
538 WTF_EXPORT int codePointCompare(const String&, const String&);
540 inline bool codePointCompareLessThan(const String& a, const String& b)
542 return codePointCompare(a.impl(), b.impl()) < 0;
545 template<size_t inlineCapacity>
546 inline void append(Vector<UChar, inlineCapacity>& vector, const String& string)
548 unsigned length = string.length();
551 if (string.is8Bit()) {
552 const LChar* characters8 = string.characters8();
553 vector.reserveCapacity(vector.size() + length);
554 for (size_t i = 0; i < length; ++i)
555 vector.uncheckedAppend(characters8[i]);
557 vector.append(string.characters16(), length);
561 template<typename CharacterType>
562 inline void appendNumber(Vector<CharacterType>& vector, unsigned char number)
564 int numberLength = number > 99 ? 3 : (number > 9 ? 2 : 1);
565 size_t vectorSize = vector.size();
566 vector.grow(vectorSize + numberLength);
568 switch (numberLength) {
570 vector[vectorSize + 2] = number % 10 + '0';
574 vector[vectorSize + 1] = number % 10 + '0';
578 vector[vectorSize] = number % 10 + '0';
582 template<bool isSpecialCharacter(UChar), typename CharacterType>
583 inline bool isAllSpecialCharacters(const CharacterType* characters, size_t length)
585 for (size_t i = 0; i < length; ++i) {
586 if (!isSpecialCharacter(characters[i]))
592 template<bool isSpecialCharacter(UChar)>
593 inline bool String::isAllSpecialCharacters() const
595 size_t len = length();
601 return WTF::isAllSpecialCharacters<isSpecialCharacter, LChar>(characters8(), len);
602 return WTF::isAllSpecialCharacters<isSpecialCharacter, UChar>(characters16(), len);
605 template<size_t inlineCapacity>
606 inline void String::appendTo(Vector<UChar, inlineCapacity>& result, unsigned pos, unsigned len) const
608 unsigned numberOfCharactersToCopy = std::min(len, length() - pos);
609 if (!numberOfCharactersToCopy)
611 result.reserveCapacity(result.size() + numberOfCharactersToCopy);
613 const LChar* characters8 = m_impl->characters8();
614 for (size_t i = 0; i < numberOfCharactersToCopy; ++i)
615 result.uncheckedAppend(characters8[pos + i]);
617 const UChar* characters16 = m_impl->characters16();
618 result.append(characters16 + pos, numberOfCharactersToCopy);
622 template<typename BufferType>
623 inline void String::appendTo(BufferType& result, unsigned pos, unsigned len) const
625 unsigned numberOfCharactersToCopy = std::min(len, length() - pos);
626 if (!numberOfCharactersToCopy)
629 result.append(m_impl->characters8() + pos, numberOfCharactersToCopy);
631 result.append(m_impl->characters16() + pos, numberOfCharactersToCopy);
634 template<size_t inlineCapacity>
635 inline void String::prependTo(Vector<UChar, inlineCapacity>& result, unsigned pos, unsigned len) const
637 unsigned numberOfCharactersToCopy = std::min(len, length() - pos);
638 if (!numberOfCharactersToCopy)
641 size_t oldSize = result.size();
642 result.resize(oldSize + numberOfCharactersToCopy);
643 memmove(result.data() + numberOfCharactersToCopy, result.data(), oldSize * sizeof(UChar));
644 StringImpl::copyChars(result.data(), m_impl->characters8() + pos, numberOfCharactersToCopy);
646 result.prepend(m_impl->characters16() + pos, numberOfCharactersToCopy);
650 // StringHash is the default hash for String
651 template<typename T> struct DefaultHash;
652 template<> struct DefaultHash<String> {
653 typedef StringHash Hash;
656 // Shared global empty string.
657 WTF_EXPORT const String& emptyString();
658 WTF_EXPORT const String& emptyString16Bit();
659 WTF_EXPORT extern const String& xmlnsWithColon;
663 WTF_ALLOW_MOVE_AND_INIT_WITH_MEM_FUNCTIONS(String);
666 using WTF::KeepTrailingZeros;
667 using WTF::StrictUTF8Conversion;
668 using WTF::StrictUTF8ConversionReplacingUnpairedSurrogatesWithFFFD;
670 using WTF::emptyString;
671 using WTF::emptyString16Bit;
673 using WTF::appendNumber;
674 using WTF::charactersAreAllASCII;
675 using WTF::charactersToIntStrict;
676 using WTF::charactersToUIntStrict;
677 using WTF::charactersToInt64Strict;
678 using WTF::charactersToUInt64Strict;
679 using WTF::charactersToIntPtrStrict;
680 using WTF::charactersToInt;
681 using WTF::charactersToUInt;
682 using WTF::charactersToInt64;
683 using WTF::charactersToUInt64;
684 using WTF::charactersToIntPtr;
685 using WTF::charactersToDouble;
686 using WTF::charactersToFloat;
688 using WTF::equalIgnoringCase;
690 using WTF::isAllSpecialCharacters;
691 using WTF::isSpaceOrNewline;
692 using WTF::reverseFind;
694 #include "wtf/text/AtomicString.h"
695 #endif // WTFString_h