Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / text / WTFString.h
blobf06ed79dbc305cce03387bb3d2eddf138aa33bc5
1 /*
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.
22 #ifndef WTFString_h
23 #define WTFString_h
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/testing/WTFUnitTestHelpersExport.h"
31 #include "wtf/text/ASCIIFastPath.h"
32 #include "wtf/text/StringImpl.h"
33 #include "wtf/text/StringView.h"
34 #include <algorithm>
35 #include <iosfwd>
37 #ifdef __OBJC__
38 #include <objc/objc.h>
39 #endif
41 namespace WTF {
43 class CString;
44 struct StringHash;
46 // Declarations of string operations
48 WTF_EXPORT int charactersToIntStrict(const LChar*, size_t, bool* ok = 0, int base = 10);
49 WTF_EXPORT int charactersToIntStrict(const UChar*, size_t, bool* ok = 0, int base = 10);
50 WTF_EXPORT unsigned charactersToUIntStrict(const LChar*, size_t, bool* ok = 0, int base = 10);
51 WTF_EXPORT unsigned charactersToUIntStrict(const UChar*, size_t, bool* ok = 0, int base = 10);
52 WTF_EXPORT int64_t charactersToInt64Strict(const LChar*, size_t, bool* ok = 0, int base = 10);
53 WTF_EXPORT int64_t charactersToInt64Strict(const UChar*, size_t, bool* ok = 0, int base = 10);
54 WTF_EXPORT uint64_t charactersToUInt64Strict(const LChar*, size_t, bool* ok = 0, int base = 10);
55 WTF_EXPORT uint64_t charactersToUInt64Strict(const UChar*, size_t, bool* ok = 0, int base = 10);
57 WTF_EXPORT int charactersToInt(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
58 WTF_EXPORT int charactersToInt(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
59 WTF_EXPORT unsigned charactersToUInt(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
60 WTF_EXPORT unsigned charactersToUInt(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
61 WTF_EXPORT int64_t charactersToInt64(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
62 WTF_EXPORT int64_t charactersToInt64(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
63 WTF_EXPORT uint64_t charactersToUInt64(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage
64 WTF_EXPORT uint64_t charactersToUInt64(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage
66 // FIXME: Like the strict functions above, these give false for "ok" when there is trailing garbage.
67 // Like the non-strict functions above, these return the value when there is trailing garbage.
68 // It would be better if these were more consistent with the above functions instead.
69 WTF_EXPORT double charactersToDouble(const LChar*, size_t, bool* ok = 0);
70 WTF_EXPORT double charactersToDouble(const UChar*, size_t, bool* ok = 0);
71 WTF_EXPORT float charactersToFloat(const LChar*, size_t, bool* ok = 0);
72 WTF_EXPORT float charactersToFloat(const UChar*, size_t, bool* ok = 0);
73 WTF_EXPORT float charactersToFloat(const LChar*, size_t, size_t& parsedLength);
74 WTF_EXPORT float charactersToFloat(const UChar*, size_t, size_t& parsedLength);
76 enum TrailingZerosTruncatingPolicy {
77 KeepTrailingZeros,
78 TruncateTrailingZeros
81 enum UTF8ConversionMode {
82 LenientUTF8Conversion,
83 StrictUTF8Conversion,
84 StrictUTF8ConversionReplacingUnpairedSurrogatesWithFFFD
87 template<bool isSpecialCharacter(UChar), typename CharacterType>
88 bool isAllSpecialCharacters(const CharacterType*, size_t);
90 // You can find documentation about this class in this doc:
91 // https://docs.google.com/document/d/1kOCUlJdh2WJMJGDf-WoEQhmnjKLaOYRbiHz5TiGJl14/edit?usp=sharing
92 class WTF_EXPORT String {
93 public:
94 // Construct a null string, distinguishable from an empty string.
95 String() { }
97 // Construct a string with UTF-16 data.
98 String(const UChar* characters, unsigned length);
100 // Construct a string by copying the contents of a vector.
101 // This method will never create a null string. Vectors with size() == 0
102 // will return the empty string.
103 // NOTE: This is different from String(vector.data(), vector.size())
104 // which will sometimes return a null string when vector.data() is null
105 // which can only occur for vectors without inline capacity.
106 // See: https://bugs.webkit.org/show_bug.cgi?id=109792
107 template<size_t inlineCapacity>
108 explicit String(const Vector<UChar, inlineCapacity>&);
110 // Construct a string with UTF-16 data, from a null-terminated source.
111 String(const UChar*);
113 // Construct a string with latin1 data.
114 String(const LChar* characters, unsigned length);
115 String(const char* characters, unsigned length);
117 // Construct a string with latin1 data, from a null-terminated source.
118 String(const LChar* characters);
119 String(const char* characters);
121 // Construct a string referencing an existing StringImpl.
122 String(StringImpl* impl) : m_impl(impl) { }
123 String(PassRefPtr<StringImpl> impl) : m_impl(impl) { }
125 void swap(String& o) { m_impl.swap(o.m_impl); }
127 template<typename CharType>
128 static String adopt(StringBuffer<CharType>& buffer)
130 if (!buffer.length())
131 return StringImpl::empty();
132 return String(buffer.release());
135 bool isNull() const { return !m_impl; }
136 bool isEmpty() const { return !m_impl || !m_impl->length(); }
138 StringImpl* impl() const { return m_impl.get(); }
139 PassRefPtr<StringImpl> releaseImpl() { return m_impl.release(); }
141 unsigned length() const
143 if (!m_impl)
144 return 0;
145 return m_impl->length();
148 const LChar* characters8() const
150 if (!m_impl)
151 return 0;
152 ASSERT(m_impl->is8Bit());
153 return m_impl->characters8();
156 const UChar* characters16() const
158 if (!m_impl)
159 return 0;
160 ASSERT(!m_impl->is8Bit());
161 return m_impl->characters16();
164 // Return characters8() or characters16() depending on CharacterType.
165 template <typename CharacterType>
166 inline const CharacterType* getCharacters() const;
168 bool is8Bit() const { return m_impl->is8Bit(); }
170 unsigned sizeInBytes() const
172 if (!m_impl)
173 return 0;
174 return m_impl->length() * (is8Bit() ? sizeof(LChar) : sizeof(UChar));
177 CString ascii() const;
178 CString latin1() const;
179 CString utf8(UTF8ConversionMode = LenientUTF8Conversion) const;
181 UChar operator[](unsigned index) const
183 if (!m_impl || index >= m_impl->length())
184 return 0;
185 return (*m_impl)[index];
188 static String number(int);
189 static String number(unsigned);
190 static String number(long);
191 static String number(unsigned long);
192 static String number(long long);
193 static String number(unsigned long long);
195 static String number(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros);
197 // Number to String conversion following the ECMAScript definition.
198 static String numberToStringECMAScript(double);
199 static String numberToStringFixedWidth(double, unsigned decimalPlaces);
201 // Find a single character or string, also with match function & latin1 forms.
202 size_t find(UChar c, unsigned start = 0) const
203 { return m_impl ? m_impl->find(c, start) : kNotFound; }
204 size_t find(LChar c, unsigned start = 0) const
205 { return m_impl ? m_impl->find(c, start) : kNotFound; }
206 size_t find(char c, unsigned start = 0) const { return find(static_cast<LChar>(c), start); }
208 size_t find(const String& str) const
209 { return m_impl ? m_impl->find(str.impl()) : kNotFound; }
210 size_t find(const String& str, unsigned start) const
211 { return m_impl ? m_impl->find(str.impl(), start) : kNotFound; }
213 size_t find(CharacterMatchFunctionPtr matchFunction, unsigned start = 0) const
214 { return m_impl ? m_impl->find(matchFunction, start) : kNotFound; }
215 size_t find(const LChar* str, unsigned start = 0) const
216 { return m_impl ? m_impl->find(str, start) : kNotFound; }
218 size_t findNextLineStart(unsigned start = 0) const
219 { return m_impl ? m_impl->findNextLineStart(start) : kNotFound; }
221 // Find the last instance of a single character or string.
222 size_t reverseFind(UChar c, unsigned start = UINT_MAX) const
223 { return m_impl ? m_impl->reverseFind(c, start) : kNotFound; }
224 size_t reverseFind(const String& str, unsigned start = UINT_MAX) const
225 { return m_impl ? m_impl->reverseFind(str.impl(), start) : kNotFound; }
227 // Case insensitive string matching.
228 size_t findIgnoringCase(const LChar* str, unsigned start = 0) const
229 { return m_impl ? m_impl->findIgnoringCase(str, start) : kNotFound; }
230 size_t findIgnoringCase(const String& str, unsigned start = 0) const
231 { return m_impl ? m_impl->findIgnoringCase(str.impl(), start) : kNotFound; }
232 size_t reverseFindIgnoringCase(const String& str, unsigned start = UINT_MAX) const
233 { return m_impl ? m_impl->reverseFindIgnoringCase(str.impl(), start) : kNotFound; }
235 // Wrappers for find & reverseFind adding dynamic sensitivity check.
236 size_t find(const LChar* str, unsigned start, TextCaseSensitivity caseSensitivity) const
237 { return (caseSensitivity == TextCaseSensitive) ? find(str, start) : findIgnoringCase(str, start); }
238 size_t find(const String& str, unsigned start, TextCaseSensitivity caseSensitivity) const
239 { return (caseSensitivity == TextCaseSensitive) ? find(str, start) : findIgnoringCase(str, start); }
240 size_t reverseFind(const String& str, unsigned start, TextCaseSensitivity caseSensitivity) const
241 { return (caseSensitivity == TextCaseSensitive) ? reverseFind(str, start) : reverseFindIgnoringCase(str, start); }
243 Vector<UChar> charactersWithNullTermination() const;
244 unsigned copyTo(UChar* buffer, unsigned pos, unsigned maxLength) const;
246 template<size_t inlineCapacity>
247 void appendTo(Vector<UChar, inlineCapacity>&, unsigned pos = 0, unsigned len = UINT_MAX) const;
249 template<typename BufferType>
250 void appendTo(BufferType&, unsigned pos = 0, unsigned len = UINT_MAX) const;
252 template<size_t inlineCapacity>
253 void prependTo(Vector<UChar, inlineCapacity>&, unsigned pos = 0, unsigned len = UINT_MAX) const;
255 UChar32 characterStartingAt(unsigned) const;
256 template<typename CharacterType>
257 bool contains(CharacterType c) const { return find(c) != kNotFound; }
258 bool contains(const LChar* str, TextCaseSensitivity caseSensitivity = TextCaseSensitive) const { return find(str, 0, caseSensitivity) != kNotFound; }
259 bool contains(const String& str, TextCaseSensitivity caseSensitivity = TextCaseSensitive) const { return find(str, 0, caseSensitivity) != kNotFound; }
261 bool startsWith(const String& s, TextCaseSensitivity caseSensitivity = TextCaseSensitive) const
262 { return m_impl ? m_impl->startsWith(s.impl(), caseSensitivity) : s.isEmpty(); }
263 bool startsWith(UChar character) const
264 { return m_impl ? m_impl->startsWith(character) : false; }
265 template<unsigned matchLength>
266 bool startsWith(const char (&prefix)[matchLength], TextCaseSensitivity caseSensitivity = TextCaseSensitive) const
267 { return m_impl ? m_impl->startsWith<matchLength>(prefix, caseSensitivity) : !matchLength; }
269 bool endsWith(const String& s, TextCaseSensitivity caseSensitivity = TextCaseSensitive) const
270 { return m_impl ? m_impl->endsWith(s.impl(), caseSensitivity) : s.isEmpty(); }
271 bool endsWith(UChar character) const
272 { return m_impl ? m_impl->endsWith(character) : false; }
273 template<unsigned matchLength>
274 bool endsWith(const char (&prefix)[matchLength], TextCaseSensitivity caseSensitivity = TextCaseSensitive) const
275 { return m_impl ? m_impl->endsWith<matchLength>(prefix, caseSensitivity) : !matchLength; }
277 void append(const String&);
278 void append(LChar);
279 void append(char c) { append(static_cast<LChar>(c)); }
280 void append(UChar);
281 void append(const LChar*, unsigned length);
282 void append(const char* charactersToAppend, unsigned length) { append(reinterpret_cast<const LChar*>(charactersToAppend), length); }
283 void append(const UChar*, unsigned length);
284 void insert(const String&, unsigned pos);
285 void insert(const LChar*, unsigned length, unsigned pos);
286 void insert(const UChar*, unsigned length, unsigned pos);
288 String& replace(UChar a, UChar b) { if (m_impl) m_impl = m_impl->replace(a, b); return *this; }
289 String& replace(UChar a, const String& b) { if (m_impl) m_impl = m_impl->replace(a, b.impl()); return *this; }
290 String& replace(const String& a, const String& b) { if (m_impl) m_impl = m_impl->replace(a.impl(), b.impl()); return *this; }
291 String& replace(unsigned index, unsigned len, const String& b) { if (m_impl) m_impl = m_impl->replace(index, len, b.impl()); return *this; }
293 template<unsigned charactersCount>
294 ALWAYS_INLINE String& replaceWithLiteral(UChar a, const char (&characters)[charactersCount])
296 if (m_impl)
297 m_impl = m_impl->replace(a, characters, charactersCount - 1);
299 return *this;
302 void fill(UChar c) { if (m_impl) m_impl = m_impl->fill(c); }
304 void ensure16Bit();
306 void truncate(unsigned len);
307 void remove(unsigned pos, int len = 1);
309 String substring(unsigned pos, unsigned len = UINT_MAX) const;
310 String left(unsigned len) const { return substring(0, len); }
311 String right(unsigned len) const { return substring(length() - len, len); }
313 StringView createView() const { return StringView(impl()); }
314 StringView createView(unsigned offset, unsigned length) const { return StringView(impl(), offset, length); }
316 // Returns a lowercase/uppercase version of the string
317 String lower() const;
318 String upper() const;
320 String lower(const AtomicString& localeIdentifier) const;
321 String upper(const AtomicString& localeIdentifier) const;
323 String stripWhiteSpace() const;
324 String stripWhiteSpace(IsWhiteSpaceFunctionPtr) const;
325 String simplifyWhiteSpace(StripBehavior stripBehavior = StripExtraWhiteSpace) const;
326 String simplifyWhiteSpace(IsWhiteSpaceFunctionPtr, StripBehavior stripBehavior = StripExtraWhiteSpace) const;
328 String removeCharacters(CharacterMatchFunctionPtr) const;
329 template<bool isSpecialCharacter(UChar)> bool isAllSpecialCharacters() const;
331 // Return the string with case folded for case insensitive comparison.
332 String foldCase() const;
334 static String format(const char *, ...) WTF_ATTRIBUTE_PRINTF(1, 2);
336 // Returns an uninitialized string. The characters needs to be written
337 // into the buffer returned in data before the returned string is used.
338 // Failure to do this will have unpredictable results.
339 static String createUninitialized(unsigned length, UChar*& data) { return StringImpl::createUninitialized(length, data); }
340 static String createUninitialized(unsigned length, LChar*& data) { return StringImpl::createUninitialized(length, data); }
342 void split(const String& separator, bool allowEmptyEntries, Vector<String>& result) const;
343 void split(const String& separator, Vector<String>& result) const
345 split(separator, false, result);
347 void split(UChar separator, bool allowEmptyEntries, Vector<String>& result) const;
348 void split(UChar separator, Vector<String>& result) const
350 split(separator, false, result);
353 int toIntStrict(bool* ok = 0, int base = 10) const;
354 unsigned toUIntStrict(bool* ok = 0, int base = 10) const;
355 int64_t toInt64Strict(bool* ok = 0, int base = 10) const;
356 uint64_t toUInt64Strict(bool* ok = 0, int base = 10) const;
358 int toInt(bool* ok = 0) const;
359 unsigned toUInt(bool* ok = 0) const;
360 int64_t toInt64(bool* ok = 0) const;
361 uint64_t toUInt64(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 String isolatedCopy() const;
370 bool isSafeToSendToAnotherThread() const;
372 #if USE(CF)
373 String(CFStringRef);
374 RetainPtr<CFStringRef> createCFString() const;
375 #endif
377 #ifdef __OBJC__
378 String(NSString*);
380 // This conversion maps NULL to "", which loses the meaning of NULL, but we
381 // need this mapping because AppKit crashes when passed nil NSStrings.
382 operator NSString*() const { if (!m_impl) return @""; return *m_impl; }
383 #endif
385 static String make8BitFrom16BitSource(const UChar*, size_t);
386 template<size_t inlineCapacity>
387 static String make8BitFrom16BitSource(const Vector<UChar, inlineCapacity>& buffer)
389 return make8BitFrom16BitSource(buffer.data(), buffer.size());
392 static String make16BitFrom8BitSource(const LChar*, size_t);
394 // String::fromUTF8 will return a null string if
395 // the input data contains invalid UTF-8 sequences.
396 static String fromUTF8(const LChar*, size_t);
397 static String fromUTF8(const LChar*);
398 static String fromUTF8(const char* s, size_t length) { return fromUTF8(reinterpret_cast<const LChar*>(s), length); }
399 static String fromUTF8(const char* s) { return fromUTF8(reinterpret_cast<const LChar*>(s)); }
400 static String fromUTF8(const CString&);
402 // 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.
403 static String fromUTF8WithLatin1Fallback(const LChar*, size_t);
404 static String fromUTF8WithLatin1Fallback(const char* s, size_t length) { return fromUTF8WithLatin1Fallback(reinterpret_cast<const LChar*>(s), length); }
406 bool containsOnlyASCII() const;
407 bool containsOnlyLatin1() const;
408 bool containsOnlyWhitespace() const { return !m_impl || m_impl->containsOnlyWhitespace(); }
410 // Hash table deleted values, which are only constructed and never copied or destroyed.
411 String(WTF::HashTableDeletedValueType) : m_impl(WTF::HashTableDeletedValue) { }
412 bool isHashTableDeletedValue() const { return m_impl.isHashTableDeletedValue(); }
414 #ifndef NDEBUG
415 void show() const;
416 #endif
418 // Workaround for a compiler bug. Use operator[] instead.
419 UChar characterAt(unsigned index) const
421 if (!m_impl || index >= m_impl->length())
422 return 0;
423 return (*m_impl)[index];
426 private:
427 typedef struct ImplicitConversionFromWTFStringToBoolDisallowed* (String::*UnspecifiedBoolType);
428 operator UnspecifiedBoolType() const;
430 template <typename CharacterType>
431 void removeInternal(const CharacterType*, unsigned, int);
433 template <typename CharacterType>
434 void appendInternal(CharacterType);
436 RefPtr<StringImpl> m_impl;
439 inline bool operator==(const String& a, const String& b) { return equal(a.impl(), b.impl()); }
440 inline bool operator==(const String& a, const LChar* b) { return equal(a.impl(), b); }
441 inline bool operator==(const String& a, const char* b) { return equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
442 inline bool operator==(const LChar* a, const String& b) { return equal(a, b.impl()); }
443 inline bool operator==(const char* a, const String& b) { return equal(reinterpret_cast<const LChar*>(a), b.impl()); }
444 template<size_t inlineCapacity>
445 inline bool operator==(const Vector<char, inlineCapacity>& a, const String& b) { return equal(b.impl(), a.data(), a.size()); }
446 template<size_t inlineCapacity>
447 inline bool operator==(const String& a, const Vector<char, inlineCapacity>& b) { return b == a; }
450 inline bool operator!=(const String& a, const String& b) { return !equal(a.impl(), b.impl()); }
451 inline bool operator!=(const String& a, const LChar* b) { return !equal(a.impl(), b); }
452 inline bool operator!=(const String& a, const char* b) { return !equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
453 inline bool operator!=(const LChar* a, const String& b) { return !equal(a, b.impl()); }
454 inline bool operator!=(const char* a, const String& b) { return !equal(reinterpret_cast<const LChar*>(a), b.impl()); }
455 template<size_t inlineCapacity>
456 inline bool operator!=(const Vector<char, inlineCapacity>& a, const String& b) { return !(a == b); }
457 template<size_t inlineCapacity>
458 inline bool operator!=(const String& a, const Vector<char, inlineCapacity>& b) { return b != a; }
460 inline bool equalIgnoringCase(const String& a, const String& b) { return equalIgnoringCase(a.impl(), b.impl()); }
461 inline bool equalIgnoringCase(const String& a, const LChar* b) { return equalIgnoringCase(a.impl(), b); }
462 inline bool equalIgnoringCase(const String& a, const char* b) { return equalIgnoringCase(a.impl(), reinterpret_cast<const LChar*>(b)); }
463 inline bool equalIgnoringCase(const LChar* a, const String& b) { return equalIgnoringCase(a, b.impl()); }
464 inline bool equalIgnoringCase(const char* a, const String& b) { return equalIgnoringCase(reinterpret_cast<const LChar*>(a), b.impl()); }
466 inline bool equalPossiblyIgnoringCase(const String& a, const String& b, bool ignoreCase)
468 return ignoreCase ? equalIgnoringCase(a, b) : (a == b);
471 inline bool equalIgnoringNullity(const String& a, const String& b) { return equalIgnoringNullity(a.impl(), b.impl()); }
473 template<size_t inlineCapacity>
474 inline bool equalIgnoringNullity(const Vector<UChar, inlineCapacity>& a, const String& b) { return equalIgnoringNullity(a, b.impl()); }
476 inline bool operator!(const String& str) { return str.isNull(); }
478 inline void swap(String& a, String& b) { a.swap(b); }
480 // Definitions of string operations
482 template<size_t inlineCapacity>
483 String::String(const Vector<UChar, inlineCapacity>& vector)
484 : m_impl(vector.size() ? StringImpl::create(vector.data(), vector.size()) : StringImpl::empty())
488 template<>
489 inline const LChar* String::getCharacters<LChar>() const
491 ASSERT(is8Bit());
492 return characters8();
495 template<>
496 inline const UChar* String::getCharacters<UChar>() const
498 ASSERT(!is8Bit());
499 return characters16();
502 inline bool String::containsOnlyLatin1() const
504 if (isEmpty())
505 return true;
507 if (is8Bit())
508 return true;
510 const UChar* characters = characters16();
511 UChar ored = 0;
512 for (size_t i = 0; i < m_impl->length(); ++i)
513 ored |= characters[i];
514 return !(ored & 0xFF00);
518 #ifdef __OBJC__
519 // This is for situations in WebKit where the long standing behavior has been
520 // "nil if empty", so we try to maintain longstanding behavior for the sake of
521 // entrenched clients
522 inline NSString* nsStringNilIfEmpty(const String& str) { return str.isEmpty() ? nil : (NSString*)str; }
523 #endif
525 inline bool String::containsOnlyASCII() const
527 if (isEmpty())
528 return true;
530 if (is8Bit())
531 return charactersAreAllASCII(characters8(), m_impl->length());
533 return charactersAreAllASCII(characters16(), m_impl->length());
536 WTF_EXPORT int codePointCompare(const String&, const String&);
538 inline bool codePointCompareLessThan(const String& a, const String& b)
540 return codePointCompare(a.impl(), b.impl()) < 0;
543 template<size_t inlineCapacity>
544 inline void append(Vector<UChar, inlineCapacity>& vector, const String& string)
546 unsigned length = string.length();
547 if (!length)
548 return;
549 if (string.is8Bit()) {
550 const LChar* characters8 = string.characters8();
551 vector.reserveCapacity(vector.size() + length);
552 for (size_t i = 0; i < length; ++i)
553 vector.uncheckedAppend(characters8[i]);
554 } else {
555 vector.append(string.characters16(), length);
559 template<bool isSpecialCharacter(UChar), typename CharacterType>
560 inline bool isAllSpecialCharacters(const CharacterType* characters, size_t length)
562 for (size_t i = 0; i < length; ++i) {
563 if (!isSpecialCharacter(characters[i]))
564 return false;
566 return true;
569 template<bool isSpecialCharacter(UChar)>
570 inline bool String::isAllSpecialCharacters() const
572 size_t len = length();
574 if (!len)
575 return true;
577 if (is8Bit())
578 return WTF::isAllSpecialCharacters<isSpecialCharacter, LChar>(characters8(), len);
579 return WTF::isAllSpecialCharacters<isSpecialCharacter, UChar>(characters16(), len);
582 template<size_t inlineCapacity>
583 inline void String::appendTo(Vector<UChar, inlineCapacity>& result, unsigned pos, unsigned len) const
585 unsigned numberOfCharactersToCopy = std::min(len, length() - pos);
586 if (!numberOfCharactersToCopy)
587 return;
588 result.reserveCapacity(result.size() + numberOfCharactersToCopy);
589 if (is8Bit()) {
590 const LChar* characters8 = m_impl->characters8();
591 for (size_t i = 0; i < numberOfCharactersToCopy; ++i)
592 result.uncheckedAppend(characters8[pos + i]);
593 } else {
594 const UChar* characters16 = m_impl->characters16();
595 result.append(characters16 + pos, numberOfCharactersToCopy);
599 template<typename BufferType>
600 inline void String::appendTo(BufferType& result, unsigned pos, unsigned len) const
602 unsigned numberOfCharactersToCopy = std::min(len, length() - pos);
603 if (!numberOfCharactersToCopy)
604 return;
605 if (is8Bit())
606 result.append(m_impl->characters8() + pos, numberOfCharactersToCopy);
607 else
608 result.append(m_impl->characters16() + pos, numberOfCharactersToCopy);
611 template<size_t inlineCapacity>
612 inline void String::prependTo(Vector<UChar, inlineCapacity>& result, unsigned pos, unsigned len) const
614 unsigned numberOfCharactersToCopy = std::min(len, length() - pos);
615 if (!numberOfCharactersToCopy)
616 return;
617 if (is8Bit()) {
618 size_t oldSize = result.size();
619 result.resize(oldSize + numberOfCharactersToCopy);
620 memmove(result.data() + numberOfCharactersToCopy, result.data(), oldSize * sizeof(UChar));
621 StringImpl::copyChars(result.data(), m_impl->characters8() + pos, numberOfCharactersToCopy);
622 } else {
623 result.prepend(m_impl->characters16() + pos, numberOfCharactersToCopy);
627 // StringHash is the default hash for String
628 template<typename T> struct DefaultHash;
629 template<> struct DefaultHash<String> {
630 typedef StringHash Hash;
633 // Shared global empty string.
634 WTF_EXPORT const String& emptyString();
635 WTF_EXPORT const String& emptyString16Bit();
636 WTF_EXPORT extern const String& xmlnsWithColon;
638 // Pretty printer for gtest. Declared here to avoid ODR violations.
639 WTF_UNITTEST_HELPERS_EXPORT std::ostream& operator<<(std::ostream&, const String&);
641 } // namespace WTF
643 WTF_ALLOW_MOVE_AND_INIT_WITH_MEM_FUNCTIONS(String);
645 using WTF::CString;
646 using WTF::KeepTrailingZeros;
647 using WTF::StrictUTF8Conversion;
648 using WTF::StrictUTF8ConversionReplacingUnpairedSurrogatesWithFFFD;
649 using WTF::String;
650 using WTF::emptyString;
651 using WTF::emptyString16Bit;
652 using WTF::append;
653 using WTF::charactersAreAllASCII;
654 using WTF::charactersToIntStrict;
655 using WTF::charactersToUIntStrict;
656 using WTF::charactersToInt64Strict;
657 using WTF::charactersToUInt64Strict;
658 using WTF::charactersToInt;
659 using WTF::charactersToUInt;
660 using WTF::charactersToInt64;
661 using WTF::charactersToUInt64;
662 using WTF::charactersToDouble;
663 using WTF::charactersToFloat;
664 using WTF::equal;
665 using WTF::equalIgnoringCase;
666 using WTF::find;
667 using WTF::isAllSpecialCharacters;
668 using WTF::isSpaceOrNewline;
669 using WTF::reverseFind;
671 #include "wtf/text/AtomicString.h"
672 #endif // WTFString_h