Home | History | Annotate | Download | only in text
      1 /*
      2  * (C) 1999 Lars Knoll (knoll (at) kde.org)
      3  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012, 2013 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/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"
     33 
     34 #ifdef __OBJC__
     35 #include <objc/objc.h>
     36 #endif
     37 
     38 namespace WTF {
     39 
     40 class CString;
     41 struct StringHash;
     42 
     43 // Declarations of string operations
     44 
     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);
     55 
     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
     66 
     67 // FIXME: Like the strict functions above, these give false for "ok" when there is trailing garbage.
     68 // Like the non-strict functions above, these return the value when there is trailing garbage.
     69 // It would be better if these were more consistent with the above functions instead.
     70 WTF_EXPORT 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);
     76 
     77 enum TrailingZerosTruncatingPolicy {
     78     KeepTrailingZeros,
     79     TruncateTrailingZeros
     80 };
     81 
     82 template<bool isSpecialCharacter(UChar), typename CharacterType>
     83 bool isAllSpecialCharacters(const CharacterType*, size_t);
     84 
     85 // You can find documentation about this class in this doc:
     86 // https://docs.google.com/document/d/1kOCUlJdh2WJMJGDf-WoEQhmnjKLaOYRbiHz5TiGJl14/edit?usp=sharing
     87 class WTF_EXPORT String {
     88 public:
     89     // Construct a null string, distinguishable from an empty string.
     90     String() { }
     91 
     92     // Construct a string with UTF-16 data.
     93     String(const UChar* characters, unsigned length);
     94 
     95     // Construct a string by copying the contents of a vector.
     96     // This method will never create a null string. Vectors with size() == 0
     97     // will return the empty string.
     98     // NOTE: This is different from String(vector.data(), vector.size())
     99     // which will sometimes return a null string when vector.data() is null
    100     // which can only occur for vectors without inline capacity.
    101     // See: https://bugs.webkit.org/show_bug.cgi?id=109792
    102     template<size_t inlineCapacity>
    103     explicit String(const Vector<UChar, inlineCapacity>&);
    104 
    105     // Construct a string with UTF-16 data, from a null-terminated source.
    106     String(const UChar*);
    107 
    108     // Construct a string with latin1 data.
    109     String(const LChar* characters, unsigned length);
    110     String(const char* characters, unsigned length);
    111 
    112     // Construct a string with latin1 data, from a null-terminated source.
    113     String(const LChar* characters);
    114     String(const char* characters);
    115 
    116     // Construct a string referencing an existing StringImpl.
    117     String(StringImpl* impl) : m_impl(impl) { }
    118     String(PassRefPtr<StringImpl> impl) : m_impl(impl) { }
    119 
    120 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES)
    121     // We have to declare the copy constructor and copy assignment operator as well, otherwise
    122     // they'll be implicitly deleted by adding the move constructor and move assignment operator.
    123     String(const String& other) : m_impl(other.m_impl) { }
    124     String(String&& other) : m_impl(other.m_impl.release()) { }
    125     String& operator=(const String& other) { m_impl = other.m_impl; return *this; }
    126     String& operator=(String&& other) { m_impl = other.m_impl.release(); return *this; }
    127 #endif
    128 
    129     // Inline the destructor.
    130     ALWAYS_INLINE ~String() { }
    131 
    132     void swap(String& o) { m_impl.swap(o.m_impl); }
    133 
    134     template<typename CharType>
    135     static String adopt(StringBuffer<CharType>& buffer)
    136     {
    137         if (!buffer.length())
    138             return StringImpl::empty();
    139         return String(buffer.release());
    140     }
    141 
    142     bool isNull() const { return !m_impl; }
    143     bool isEmpty() const { return !m_impl || !m_impl->length(); }
    144 
    145     StringImpl* impl() const { return m_impl.get(); }
    146     PassRefPtr<StringImpl> releaseImpl() { return m_impl.release(); }
    147 
    148     unsigned length() const
    149     {
    150         if (!m_impl)
    151             return 0;
    152         return m_impl->length();
    153     }
    154 
    155     const LChar* characters8() const
    156     {
    157         if (!m_impl)
    158             return 0;
    159         ASSERT(m_impl->is8Bit());
    160         return m_impl->characters8();
    161     }
    162 
    163     const UChar* characters16() const
    164     {
    165         if (!m_impl)
    166             return 0;
    167         ASSERT(!m_impl->is8Bit());
    168         return m_impl->characters16();
    169     }
    170 
    171     // Return characters8() or characters16() depending on CharacterType.
    172     template <typename CharacterType>
    173     inline const CharacterType* getCharacters() const;
    174 
    175     bool is8Bit() const { return m_impl->is8Bit(); }
    176 
    177     unsigned sizeInBytes() const
    178     {
    179         if (!m_impl)
    180             return 0;
    181         return m_impl->length() * (is8Bit() ? sizeof(LChar) : sizeof(UChar));
    182     }
    183 
    184     CString ascii() const;
    185     CString latin1() const;
    186 
    187     typedef enum {
    188         LenientConversion,
    189         StrictConversion,
    190         StrictConversionReplacingUnpairedSurrogatesWithFFFD,
    191     } ConversionMode;
    192 
    193     CString utf8(ConversionMode = LenientConversion) const;
    194 
    195     UChar operator[](unsigned index) const
    196     {
    197         if (!m_impl || index >= m_impl->length())
    198             return 0;
    199         return (*m_impl)[index];
    200     }
    201 
    202     static String number(int);
    203     static String number(unsigned);
    204     static String number(long);
    205     static String number(unsigned long);
    206     static String number(long long);
    207     static String number(unsigned long long);
    208 
    209     static String number(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros);
    210 
    211     // Number to String conversion following the ECMAScript definition.
    212     static String numberToStringECMAScript(double);
    213     static String numberToStringFixedWidth(double, unsigned decimalPlaces);
    214 
    215     // Find a single character or string, also with match function & latin1 forms.
    216     size_t find(UChar c, unsigned start = 0) const
    217         { return m_impl ? m_impl->find(c, start) : kNotFound; }
    218 
    219     size_t find(const String& str) const
    220         { return m_impl ? m_impl->find(str.impl()) : kNotFound; }
    221     size_t find(const String& str, unsigned start) const
    222         { return m_impl ? m_impl->find(str.impl(), start) : kNotFound; }
    223 
    224     size_t find(CharacterMatchFunctionPtr matchFunction, unsigned start = 0) const
    225         { return m_impl ? m_impl->find(matchFunction, start) : kNotFound; }
    226     size_t find(const LChar* str, unsigned start = 0) const
    227         { return m_impl ? m_impl->find(str, start) : kNotFound; }
    228 
    229     size_t findNextLineStart(unsigned start = 0) const
    230         { return m_impl ? m_impl->findNextLineStart(start) : kNotFound; }
    231 
    232     // Find the last instance of a single character or string.
    233     size_t reverseFind(UChar c, unsigned start = UINT_MAX) const
    234         { return m_impl ? m_impl->reverseFind(c, start) : kNotFound; }
    235     size_t reverseFind(const String& str, unsigned start = UINT_MAX) const
    236         { return m_impl ? m_impl->reverseFind(str.impl(), start) : kNotFound; }
    237 
    238     // Case insensitive string matching.
    239     size_t findIgnoringCase(const LChar* str, unsigned start = 0) const
    240         { return m_impl ? m_impl->findIgnoringCase(str, start) : kNotFound; }
    241     size_t findIgnoringCase(const String& str, unsigned start = 0) const
    242         { return m_impl ? m_impl->findIgnoringCase(str.impl(), start) : kNotFound; }
    243     size_t reverseFindIgnoringCase(const String& str, unsigned start = UINT_MAX) const
    244         { return m_impl ? m_impl->reverseFindIgnoringCase(str.impl(), start) : kNotFound; }
    245 
    246     // Wrappers for find & reverseFind adding dynamic sensitivity check.
    247     size_t find(const LChar* str, unsigned start, bool caseSensitive) const
    248         { return caseSensitive ? find(str, start) : findIgnoringCase(str, start); }
    249     size_t find(const String& str, unsigned start, bool caseSensitive) const
    250         { return caseSensitive ? find(str, start) : findIgnoringCase(str, start); }
    251     size_t reverseFind(const String& str, unsigned start, bool caseSensitive) const
    252         { return caseSensitive ? reverseFind(str, start) : reverseFindIgnoringCase(str, start); }
    253 
    254     Vector<UChar> charactersWithNullTermination() const;
    255     unsigned copyTo(UChar* buffer, unsigned pos, unsigned maxLength) const;
    256 
    257     template<size_t inlineCapacity>
    258     void appendTo(Vector<UChar, inlineCapacity>&, unsigned pos = 0, unsigned len = UINT_MAX) const;
    259 
    260     template<typename BufferType>
    261     void appendTo(BufferType&, unsigned pos = 0, unsigned len = UINT_MAX) const;
    262 
    263     template<size_t inlineCapacity>
    264     void prependTo(Vector<UChar, inlineCapacity>&, unsigned pos = 0, unsigned len = UINT_MAX) const;
    265 
    266     UChar32 characterStartingAt(unsigned) const;
    267 
    268     bool contains(UChar c) const { return find(c) != kNotFound; }
    269     bool contains(const LChar* str, bool caseSensitive = true) const { return find(str, 0, caseSensitive) != kNotFound; }
    270     bool contains(const String& str, bool caseSensitive = true) const { return find(str, 0, caseSensitive) != kNotFound; }
    271 
    272     bool startsWith(const String& s, bool caseSensitive = true) const
    273         { return m_impl ? m_impl->startsWith(s.impl(), caseSensitive) : s.isEmpty(); }
    274     bool startsWith(UChar character) const
    275         { return m_impl ? m_impl->startsWith(character) : false; }
    276     template<unsigned matchLength>
    277     bool startsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const
    278         { return m_impl ? m_impl->startsWith<matchLength>(prefix, caseSensitive) : !matchLength; }
    279 
    280     bool endsWith(const String& s, bool caseSensitive = true) const
    281         { return m_impl ? m_impl->endsWith(s.impl(), caseSensitive) : s.isEmpty(); }
    282     bool endsWith(UChar character) const
    283         { return m_impl ? m_impl->endsWith(character) : false; }
    284     template<unsigned matchLength>
    285     bool endsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const
    286         { return m_impl ? m_impl->endsWith<matchLength>(prefix, caseSensitive) : !matchLength; }
    287 
    288     void append(const String&);
    289     void append(LChar);
    290     void append(char c) { append(static_cast<LChar>(c)); };
    291     void append(UChar);
    292     void append(const LChar*, unsigned length);
    293     void append(const UChar*, unsigned length);
    294     void insert(const String&, unsigned pos);
    295     void insert(const LChar*, unsigned length, unsigned pos);
    296     void insert(const UChar*, unsigned length, unsigned pos);
    297 
    298     String& replace(UChar a, UChar b) { if (m_impl) m_impl = m_impl->replace(a, b); return *this; }
    299     String& replace(UChar a, const String& b) { if (m_impl) m_impl = m_impl->replace(a, b.impl()); return *this; }
    300     String& replace(const String& a, const String& b) { if (m_impl) m_impl = m_impl->replace(a.impl(), b.impl()); return *this; }
    301     String& replace(unsigned index, unsigned len, const String& b) { if (m_impl) m_impl = m_impl->replace(index, len, b.impl()); return *this; }
    302 
    303     template<unsigned charactersCount>
    304     ALWAYS_INLINE String& replaceWithLiteral(UChar a, const char (&characters)[charactersCount])
    305     {
    306         if (m_impl)
    307             m_impl = m_impl->replace(a, characters, charactersCount - 1);
    308 
    309         return *this;
    310     }
    311 
    312     void fill(UChar c) { if (m_impl) m_impl = m_impl->fill(c); }
    313 
    314     void ensure16Bit();
    315 
    316     void truncate(unsigned len);
    317     void remove(unsigned pos, int len = 1);
    318 
    319     String substring(unsigned pos, unsigned len = UINT_MAX) const;
    320     String left(unsigned len) const { return substring(0, len); }
    321     String right(unsigned len) const { return substring(length() - len, len); }
    322 
    323     StringView createView() const { return StringView(impl()); }
    324     StringView createView(unsigned offset, unsigned length) const { return StringView(impl(), offset, length); }
    325 
    326     // Returns a lowercase/uppercase version of the string
    327     String lower() const;
    328     String upper() const;
    329 
    330     String lower(const AtomicString& localeIdentifier) const;
    331     String upper(const AtomicString& localeIdentifier) const;
    332 
    333     String stripWhiteSpace() const;
    334     String stripWhiteSpace(IsWhiteSpaceFunctionPtr) const;
    335     String simplifyWhiteSpace(StripBehavior stripBehavior = StripExtraWhiteSpace) const;
    336     String simplifyWhiteSpace(IsWhiteSpaceFunctionPtr, StripBehavior stripBehavior = StripExtraWhiteSpace) const;
    337 
    338     String removeCharacters(CharacterMatchFunctionPtr) const;
    339     template<bool isSpecialCharacter(UChar)> bool isAllSpecialCharacters() const;
    340 
    341     // Return the string with case folded for case insensitive comparison.
    342     String foldCase() const;
    343 
    344     static String format(const char *, ...) WTF_ATTRIBUTE_PRINTF(1, 2);
    345 
    346     // Returns an uninitialized string. The characters needs to be written
    347     // into the buffer returned in data before the returned string is used.
    348     // Failure to do this will have unpredictable results.
    349     static String createUninitialized(unsigned length, UChar*& data) { return StringImpl::createUninitialized(length, data); }
    350     static String createUninitialized(unsigned length, LChar*& data) { return StringImpl::createUninitialized(length, data); }
    351 
    352     void split(const String& separator, bool allowEmptyEntries, Vector<String>& result) const;
    353     void split(const String& separator, Vector<String>& result) const
    354     {
    355         split(separator, false, result);
    356     }
    357     void split(UChar separator, bool allowEmptyEntries, Vector<String>& result) const;
    358     void split(UChar separator, Vector<String>& result) const
    359     {
    360         split(separator, false, result);
    361     }
    362 
    363     int toIntStrict(bool* ok = 0, int base = 10) const;
    364     unsigned toUIntStrict(bool* ok = 0, int base = 10) const;
    365     int64_t toInt64Strict(bool* ok = 0, int base = 10) const;
    366     uint64_t toUInt64Strict(bool* ok = 0, int base = 10) const;
    367     intptr_t toIntPtrStrict(bool* ok = 0, int base = 10) const;
    368 
    369     int toInt(bool* ok = 0) const;
    370     unsigned toUInt(bool* ok = 0) const;
    371     int64_t toInt64(bool* ok = 0) const;
    372     uint64_t toUInt64(bool* ok = 0) const;
    373     intptr_t toIntPtr(bool* ok = 0) const;
    374 
    375     // FIXME: Like the strict functions above, these give false for "ok" when there is trailing garbage.
    376     // Like the non-strict functions above, these return the value when there is trailing garbage.
    377     // It would be better if these were more consistent with the above functions instead.
    378     double toDouble(bool* ok = 0) const;
    379     float toFloat(bool* ok = 0) const;
    380 
    381     bool percentage(int& percentage) const;
    382 
    383     String isolatedCopy() const;
    384     bool isSafeToSendToAnotherThread() const;
    385 
    386     // Prevent Strings from being implicitly convertable to bool as it will be ambiguous on any platform that
    387     // allows implicit conversion to another pointer type (e.g., Mac allows implicit conversion to NSString*).
    388     typedef struct ImplicitConversionFromWTFStringToBoolDisallowedA* (String::*UnspecifiedBoolTypeA);
    389     typedef struct ImplicitConversionFromWTFStringToBoolDisallowedB* (String::*UnspecifiedBoolTypeB);
    390     operator UnspecifiedBoolTypeA() const;
    391     operator UnspecifiedBoolTypeB() const;
    392 
    393 #if USE(CF)
    394     String(CFStringRef);
    395     RetainPtr<CFStringRef> createCFString() const;
    396 #endif
    397 
    398 #ifdef __OBJC__
    399     String(NSString*);
    400 
    401     // This conversion maps NULL to "", which loses the meaning of NULL, but we
    402     // need this mapping because AppKit crashes when passed nil NSStrings.
    403     operator NSString*() const { if (!m_impl) return @""; return *m_impl; }
    404 #endif
    405 
    406     static String make8BitFrom16BitSource(const UChar*, size_t);
    407     template<size_t inlineCapacity>
    408     static String make8BitFrom16BitSource(const Vector<UChar, inlineCapacity>& buffer)
    409     {
    410         return make8BitFrom16BitSource(buffer.data(), buffer.size());
    411     }
    412 
    413     static String make16BitFrom8BitSource(const LChar*, size_t);
    414 
    415     // String::fromUTF8 will return a null string if
    416     // the input data contains invalid UTF-8 sequences.
    417     static String fromUTF8(const LChar*, size_t);
    418     static String fromUTF8(const LChar*);
    419     static String fromUTF8(const char* s, size_t length) { return fromUTF8(reinterpret_cast<const LChar*>(s), length); };
    420     static String fromUTF8(const char* s) { return fromUTF8(reinterpret_cast<const LChar*>(s)); };
    421     static String fromUTF8(const CString&);
    422 
    423     // 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.
    424     static String fromUTF8WithLatin1Fallback(const LChar*, size_t);
    425     static String fromUTF8WithLatin1Fallback(const char* s, size_t length) { return fromUTF8WithLatin1Fallback(reinterpret_cast<const LChar*>(s), length); };
    426 
    427     bool containsOnlyASCII() const;
    428     bool containsOnlyLatin1() const;
    429     bool containsOnlyWhitespace() const { return !m_impl || m_impl->containsOnlyWhitespace(); }
    430 
    431     // Hash table deleted values, which are only constructed and never copied or destroyed.
    432     String(WTF::HashTableDeletedValueType) : m_impl(WTF::HashTableDeletedValue) { }
    433     bool isHashTableDeletedValue() const { return m_impl.isHashTableDeletedValue(); }
    434 
    435 #ifndef NDEBUG
    436     void show() const;
    437 #endif
    438 
    439     // Workaround for a compiler bug. Use operator[] instead.
    440     UChar characterAt(unsigned index) const
    441     {
    442         if (!m_impl || index >= m_impl->length())
    443             return 0;
    444         return (*m_impl)[index];
    445     }
    446 
    447 private:
    448     template <typename CharacterType>
    449     void removeInternal(const CharacterType*, unsigned, int);
    450 
    451     template <typename CharacterType>
    452     void appendInternal(CharacterType);
    453 
    454     RefPtr<StringImpl> m_impl;
    455 };
    456 
    457 inline bool operator==(const String& a, const String& b) { return equal(a.impl(), b.impl()); }
    458 inline bool operator==(const String& a, const LChar* b) { return equal(a.impl(), b); }
    459 inline bool operator==(const String& a, const char* b) { return equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
    460 inline bool operator==(const LChar* a, const String& b) { return equal(a, b.impl()); }
    461 inline bool operator==(const char* a, const String& b) { return equal(reinterpret_cast<const LChar*>(a), b.impl()); }
    462 template<size_t inlineCapacity>
    463 inline bool operator==(const Vector<char, inlineCapacity>& a, const String& b) { return equal(b.impl(), a.data(), a.size()); }
    464 template<size_t inlineCapacity>
    465 inline bool operator==(const String& a, const Vector<char, inlineCapacity>& b) { return b == a; }
    466 
    467 
    468 inline bool operator!=(const String& a, const String& b) { return !equal(a.impl(), b.impl()); }
    469 inline bool operator!=(const String& a, const LChar* b) { return !equal(a.impl(), b); }
    470 inline bool operator!=(const String& a, const char* b) { return !equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
    471 inline bool operator!=(const LChar* a, const String& b) { return !equal(a, b.impl()); }
    472 inline bool operator!=(const char* a, const String& b) { return !equal(reinterpret_cast<const LChar*>(a), b.impl()); }
    473 template<size_t inlineCapacity>
    474 inline bool operator!=(const Vector<char, inlineCapacity>& a, const String& b) { return !(a == b); }
    475 template<size_t inlineCapacity>
    476 inline bool operator!=(const String& a, const Vector<char, inlineCapacity>& b) { return b != a; }
    477 
    478 inline bool equalIgnoringCase(const String& a, const String& b) { return equalIgnoringCase(a.impl(), b.impl()); }
    479 inline bool equalIgnoringCase(const String& a, const LChar* b) { return equalIgnoringCase(a.impl(), b); }
    480 inline bool equalIgnoringCase(const String& a, const char* b) { return equalIgnoringCase(a.impl(), reinterpret_cast<const LChar*>(b)); }
    481 inline bool equalIgnoringCase(const LChar* a, const String& b) { return equalIgnoringCase(a, b.impl()); }
    482 inline bool equalIgnoringCase(const char* a, const String& b) { return equalIgnoringCase(reinterpret_cast<const LChar*>(a), b.impl()); }
    483 
    484 inline bool equalPossiblyIgnoringCase(const String& a, const String& b, bool ignoreCase)
    485 {
    486     return ignoreCase ? equalIgnoringCase(a, b) : (a == b);
    487 }
    488 
    489 inline bool equalIgnoringNullity(const String& a, const String& b) { return equalIgnoringNullity(a.impl(), b.impl()); }
    490 
    491 template<size_t inlineCapacity>
    492 inline bool equalIgnoringNullity(const Vector<UChar, inlineCapacity>& a, const String& b) { return equalIgnoringNullity(a, b.impl()); }
    493 
    494 inline bool operator!(const String& str) { return str.isNull(); }
    495 
    496 inline void swap(String& a, String& b) { a.swap(b); }
    497 
    498 // Definitions of string operations
    499 
    500 template<size_t inlineCapacity>
    501 String::String(const Vector<UChar, inlineCapacity>& vector)
    502     : m_impl(vector.size() ? StringImpl::create(vector.data(), vector.size()) : StringImpl::empty())
    503 {
    504 }
    505 
    506 template<>
    507 inline const LChar* String::getCharacters<LChar>() const
    508 {
    509     ASSERT(is8Bit());
    510     return characters8();
    511 }
    512 
    513 template<>
    514 inline const UChar* String::getCharacters<UChar>() const
    515 {
    516     ASSERT(!is8Bit());
    517     return characters16();
    518 }
    519 
    520 inline bool String::containsOnlyLatin1() const
    521 {
    522     if (isEmpty())
    523         return true;
    524 
    525     if (is8Bit())
    526         return true;
    527 
    528     const UChar* characters = characters16();
    529     UChar ored = 0;
    530     for (size_t i = 0; i < m_impl->length(); ++i)
    531         ored |= characters[i];
    532     return !(ored & 0xFF00);
    533 }
    534 
    535 
    536 #ifdef __OBJC__
    537 // This is for situations in WebKit where the long standing behavior has been
    538 // "nil if empty", so we try to maintain longstanding behavior for the sake of
    539 // entrenched clients
    540 inline NSString* nsStringNilIfEmpty(const String& str) {  return str.isEmpty() ? nil : (NSString*)str; }
    541 #endif
    542 
    543 inline bool String::containsOnlyASCII() const
    544 {
    545     if (isEmpty())
    546         return true;
    547 
    548     if (is8Bit())
    549         return charactersAreAllASCII(characters8(), m_impl->length());
    550 
    551     return charactersAreAllASCII(characters16(), m_impl->length());
    552 }
    553 
    554 WTF_EXPORT int codePointCompare(const String&, const String&);
    555 
    556 inline bool codePointCompareLessThan(const String& a, const String& b)
    557 {
    558     return codePointCompare(a.impl(), b.impl()) < 0;
    559 }
    560 
    561 template<size_t inlineCapacity>
    562 inline void append(Vector<UChar, inlineCapacity>& vector, const String& string)
    563 {
    564     unsigned length = string.length();
    565     if (!length)
    566         return;
    567     if (string.is8Bit()) {
    568         const LChar* characters8 = string.characters8();
    569         vector.reserveCapacity(vector.size() + length);
    570         for (size_t i = 0; i < length; ++i)
    571             vector.uncheckedAppend(characters8[i]);
    572     } else {
    573         vector.append(string.characters16(), length);
    574     }
    575 }
    576 
    577 template<typename CharacterType>
    578 inline void appendNumber(Vector<CharacterType>& vector, unsigned char number)
    579 {
    580     int numberLength = number > 99 ? 3 : (number > 9 ? 2 : 1);
    581     size_t vectorSize = vector.size();
    582     vector.grow(vectorSize + numberLength);
    583 
    584     switch (numberLength) {
    585     case 3:
    586         vector[vectorSize + 2] = number % 10 + '0';
    587         number /= 10;
    588 
    589     case 2:
    590         vector[vectorSize + 1] = number % 10 + '0';
    591         number /= 10;
    592 
    593     case 1:
    594         vector[vectorSize] = number % 10 + '0';
    595     }
    596 }
    597 
    598 template<bool isSpecialCharacter(UChar), typename CharacterType>
    599 inline bool isAllSpecialCharacters(const CharacterType* characters, size_t length)
    600 {
    601     for (size_t i = 0; i < length; ++i) {
    602         if (!isSpecialCharacter(characters[i]))
    603             return false;
    604     }
    605     return true;
    606 }
    607 
    608 template<bool isSpecialCharacter(UChar)>
    609 inline bool String::isAllSpecialCharacters() const
    610 {
    611     size_t len = length();
    612 
    613     if (!len)
    614         return true;
    615 
    616     if (is8Bit())
    617         return WTF::isAllSpecialCharacters<isSpecialCharacter, LChar>(characters8(), len);
    618     return WTF::isAllSpecialCharacters<isSpecialCharacter, UChar>(characters16(), len);
    619 }
    620 
    621 template<size_t inlineCapacity>
    622 inline void String::appendTo(Vector<UChar, inlineCapacity>& result, unsigned pos, unsigned len) const
    623 {
    624     unsigned numberOfCharactersToCopy = std::min(len, length() - pos);
    625     if (numberOfCharactersToCopy <= 0)
    626         return;
    627     result.reserveCapacity(result.size() + numberOfCharactersToCopy);
    628     if (is8Bit()) {
    629         const LChar* characters8 = m_impl->characters8();
    630         for (size_t i = 0; i < numberOfCharactersToCopy; ++i)
    631             result.uncheckedAppend(characters8[pos + i]);
    632     } else {
    633         const UChar* characters16 = m_impl->characters16();
    634         result.append(characters16 + pos, numberOfCharactersToCopy);
    635     }
    636 }
    637 
    638 template<typename BufferType>
    639 inline void String::appendTo(BufferType& result, unsigned pos, unsigned len) const
    640 {
    641     unsigned numberOfCharactersToCopy = std::min(len, length() - pos);
    642     if (numberOfCharactersToCopy <= 0)
    643         return;
    644     if (is8Bit())
    645         result.append(m_impl->characters8() + pos, numberOfCharactersToCopy);
    646     else
    647         result.append(m_impl->characters16() + pos, numberOfCharactersToCopy);
    648 }
    649 
    650 template<size_t inlineCapacity>
    651 inline void String::prependTo(Vector<UChar, inlineCapacity>& result, unsigned pos, unsigned len) const
    652 {
    653     unsigned numberOfCharactersToCopy = std::min(len, length() - pos);
    654     if (numberOfCharactersToCopy <= 0)
    655         return;
    656     if (is8Bit()) {
    657         size_t oldSize = result.size();
    658         result.resize(oldSize + numberOfCharactersToCopy);
    659         memmove(result.data() + numberOfCharactersToCopy, result.data(), oldSize * sizeof(UChar));
    660         StringImpl::copyChars(result.data(), m_impl->characters8() + pos, numberOfCharactersToCopy);
    661     } else {
    662         result.prepend(m_impl->characters16() + pos, numberOfCharactersToCopy);
    663     }
    664 }
    665 
    666 // StringHash is the default hash for String
    667 template<typename T> struct DefaultHash;
    668 template<> struct DefaultHash<String> {
    669     typedef StringHash Hash;
    670 };
    671 
    672 template <> struct VectorTraits<String> : SimpleClassVectorTraits {
    673     static const bool canCompareWithMemcmp = false;
    674 };
    675 
    676 // Shared global empty string.
    677 WTF_EXPORT const String& emptyString();
    678 
    679 }
    680 
    681 using WTF::CString;
    682 using WTF::KeepTrailingZeros;
    683 using WTF::String;
    684 using WTF::emptyString;
    685 using WTF::append;
    686 using WTF::appendNumber;
    687 using WTF::charactersAreAllASCII;
    688 using WTF::charactersToIntStrict;
    689 using WTF::charactersToUIntStrict;
    690 using WTF::charactersToInt64Strict;
    691 using WTF::charactersToUInt64Strict;
    692 using WTF::charactersToIntPtrStrict;
    693 using WTF::charactersToInt;
    694 using WTF::charactersToUInt;
    695 using WTF::charactersToInt64;
    696 using WTF::charactersToUInt64;
    697 using WTF::charactersToIntPtr;
    698 using WTF::charactersToDouble;
    699 using WTF::charactersToFloat;
    700 using WTF::equal;
    701 using WTF::equalIgnoringCase;
    702 using WTF::find;
    703 using WTF::isAllSpecialCharacters;
    704 using WTF::isSpaceOrNewline;
    705 using WTF::reverseFind;
    706 
    707 #include "wtf/text/AtomicString.h"
    708 #endif
    709