15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/* 25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (C) 1999 Lars Knoll (knoll@kde.org) 3926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012, 2013 Apple Inc. All rights reserved. 45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * This library is free software; you can redistribute it and/or 65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modify it under the terms of the GNU Library General Public 75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * License as published by the Free Software Foundation; either 85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * version 2 of the License, or (at your option) any later version. 95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * This library is distributed in the hope that it will be useful, 115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * but WITHOUT ANY WARRANTY; without even the implied warranty of 125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Library General Public License for more details. 145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * You should have received a copy of the GNU Library General Public License 165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * along with this library; see the file COPYING.LIB. If not, write to 175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Boston, MA 02110-1301, USA. 195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */ 215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#ifndef WTFString_h 235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define WTFString_h 245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// This file would be called String.h, but that conflicts with <string.h> 265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// on systems without case-sensitive file systems. 275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 280019e4eead4d990e4304c54a9028aca9122fb256Ben Murdoch#include "wtf/HashTableDeletedValueType.h" 2993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#include "wtf/WTFExport.h" 3093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#include "wtf/text/ASCIIFastPath.h" 3193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#include "wtf/text/StringImpl.h" 327757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch#include "wtf/text/StringView.h" 335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#ifdef __OBJC__ 355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include <objc/objc.h> 365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif 375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)namespace WTF { 395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)class CString; 415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)struct StringHash; 425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// Declarations of string operations 445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 4593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT int charactersToIntStrict(const LChar*, size_t, bool* ok = 0, int base = 10); 4693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT int charactersToIntStrict(const UChar*, size_t, bool* ok = 0, int base = 10); 4793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT unsigned charactersToUIntStrict(const LChar*, size_t, bool* ok = 0, int base = 10); 4893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT unsigned charactersToUIntStrict(const UChar*, size_t, bool* ok = 0, int base = 10); 4993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT int64_t charactersToInt64Strict(const LChar*, size_t, bool* ok = 0, int base = 10); 5093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT int64_t charactersToInt64Strict(const UChar*, size_t, bool* ok = 0, int base = 10); 5193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT uint64_t charactersToUInt64Strict(const LChar*, size_t, bool* ok = 0, int base = 10); 5293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT uint64_t charactersToUInt64Strict(const UChar*, size_t, bool* ok = 0, int base = 10); 5393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT intptr_t charactersToIntPtrStrict(const LChar*, size_t, bool* ok = 0, int base = 10); 5493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT intptr_t charactersToIntPtrStrict(const UChar*, size_t, bool* ok = 0, int base = 10); 5593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) 5693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT int charactersToInt(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage 5793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT int charactersToInt(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage 5893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT unsigned charactersToUInt(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage 5993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT unsigned charactersToUInt(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage 6093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT int64_t charactersToInt64(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage 6193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT int64_t charactersToInt64(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage 6293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT uint64_t charactersToUInt64(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage 6393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT uint64_t charactersToUInt64(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage 6493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT intptr_t charactersToIntPtr(const LChar*, size_t, bool* ok = 0); // ignores trailing garbage 6593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT intptr_t charactersToIntPtr(const UChar*, size_t, bool* ok = 0); // ignores trailing garbage 665c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// FIXME: Like the strict functions above, these give false for "ok" when there is trailing garbage. 685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// Like the non-strict functions above, these return the value when there is trailing garbage. 695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// It would be better if these were more consistent with the above functions instead. 7093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT double charactersToDouble(const LChar*, size_t, bool* ok = 0); 7193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT double charactersToDouble(const UChar*, size_t, bool* ok = 0); 7293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT float charactersToFloat(const LChar*, size_t, bool* ok = 0); 7393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT float charactersToFloat(const UChar*, size_t, bool* ok = 0); 7493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT float charactersToFloat(const LChar*, size_t, size_t& parsedLength); 7593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT float charactersToFloat(const UChar*, size_t, size_t& parsedLength); 765c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 775c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)enum TrailingZerosTruncatingPolicy { 785c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) KeepTrailingZeros, 795c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) TruncateTrailingZeros 805c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}; 815c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 8209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)enum UTF8ConversionMode { 8309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) LenientUTF8Conversion, 8409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) StrictUTF8Conversion, 8509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) StrictUTF8ConversionReplacingUnpairedSurrogatesWithFFFD 8609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}; 8709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 885c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)template<bool isSpecialCharacter(UChar), typename CharacterType> 895c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)bool isAllSpecialCharacters(const CharacterType*, size_t); 905c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 913c9e4aeaee9f9b0a9a814da07bcb33319c7ea363Ben Murdoch// You can find documentation about this class in this doc: 923c9e4aeaee9f9b0a9a814da07bcb33319c7ea363Ben Murdoch// https://docs.google.com/document/d/1kOCUlJdh2WJMJGDf-WoEQhmnjKLaOYRbiHz5TiGJl14/edit?usp=sharing 9393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)class WTF_EXPORT String { 945c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)public: 955c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Construct a null string, distinguishable from an empty string. 965c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) String() { } 975c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 985c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Construct a string with UTF-16 data. 9953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) String(const UChar* characters, unsigned length); 1005c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 101f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) // Construct a string by copying the contents of a vector. 102926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) // This method will never create a null string. Vectors with size() == 0 103926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) // will return the empty string. 104926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) // NOTE: This is different from String(vector.data(), vector.size()) 105926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) // which will sometimes return a null string when vector.data() is null 106926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) // which can only occur for vectors without inline capacity. 107926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) // See: https://bugs.webkit.org/show_bug.cgi?id=109792 1085c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) template<size_t inlineCapacity> 1095c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) explicit String(const Vector<UChar, inlineCapacity>&); 1105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Construct a string with UTF-16 data, from a null-terminated source. 11253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) String(const UChar*); 1135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Construct a string with latin1 data. 11553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) String(const LChar* characters, unsigned length); 11653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) String(const char* characters, unsigned length); 1175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Construct a string with latin1 data, from a null-terminated source. 11953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) String(const LChar* characters); 12053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) String(const char* characters); 1215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Construct a string referencing an existing StringImpl. 1235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) String(StringImpl* impl) : m_impl(impl) { } 1245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) String(PassRefPtr<StringImpl> impl) : m_impl(impl) { } 1255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) void swap(String& o) { m_impl.swap(o.m_impl); } 1275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 12883750176c3ee2cea66c8a9751271026a5901be3aBen Murdoch template<typename CharType> 12983750176c3ee2cea66c8a9751271026a5901be3aBen Murdoch static String adopt(StringBuffer<CharType>& buffer) 13083750176c3ee2cea66c8a9751271026a5901be3aBen Murdoch { 13183750176c3ee2cea66c8a9751271026a5901be3aBen Murdoch if (!buffer.length()) 13283750176c3ee2cea66c8a9751271026a5901be3aBen Murdoch return StringImpl::empty(); 13383750176c3ee2cea66c8a9751271026a5901be3aBen Murdoch return String(buffer.release()); 13483750176c3ee2cea66c8a9751271026a5901be3aBen Murdoch } 13583750176c3ee2cea66c8a9751271026a5901be3aBen Murdoch 1365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) bool isNull() const { return !m_impl; } 1375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) bool isEmpty() const { return !m_impl || !m_impl->length(); } 1385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) StringImpl* impl() const { return m_impl.get(); } 140926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) PassRefPtr<StringImpl> releaseImpl() { return m_impl.release(); } 1415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) unsigned length() const 1435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) { 1445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) if (!m_impl) 1455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return 0; 1465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return m_impl->length(); 1475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) } 1485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) const LChar* characters8() const 1505c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) { 1515c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) if (!m_impl) 1525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return 0; 1535c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) ASSERT(m_impl->is8Bit()); 1545c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return m_impl->characters8(); 1555c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) } 1565c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) const UChar* characters16() const 1585c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) { 1595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) if (!m_impl) 1605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return 0; 1615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) ASSERT(!m_impl->is8Bit()); 1625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return m_impl->characters16(); 1635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) } 1645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Return characters8() or characters16() depending on CharacterType. 1665c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) template <typename CharacterType> 1675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) inline const CharacterType* getCharacters() const; 1685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) bool is8Bit() const { return m_impl->is8Bit(); } 1705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1715c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) unsigned sizeInBytes() const 1725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) { 1735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) if (!m_impl) 1745c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return 0; 1755c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return m_impl->length() * (is8Bit() ? sizeof(LChar) : sizeof(UChar)); 1765c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) } 1775c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 17853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) CString ascii() const; 17953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) CString latin1() const; 18009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) CString utf8(UTF8ConversionMode = LenientUTF8Conversion) const; 1815c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1825c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) UChar operator[](unsigned index) const 1835c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) { 1845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) if (!m_impl || index >= m_impl->length()) 1855c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return 0; 1865c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return (*m_impl)[index]; 1875c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) } 1885c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 18953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) static String number(int); 19053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) static String number(unsigned); 19153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) static String number(long); 19253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) static String number(unsigned long); 19353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) static String number(long long); 19453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) static String number(unsigned long long); 1955c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 19653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) static String number(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros); 1975c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 1985c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Number to String conversion following the ECMAScript definition. 19953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) static String numberToStringECMAScript(double); 20053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) static String numberToStringFixedWidth(double, unsigned decimalPlaces); 2015c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 2025c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Find a single character or string, also with match function & latin1 forms. 2035c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) size_t find(UChar c, unsigned start = 0) const 20406f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles) { return m_impl ? m_impl->find(c, start) : kNotFound; } 2055c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 2065c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) size_t find(const String& str) const 20706f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles) { return m_impl ? m_impl->find(str.impl()) : kNotFound; } 2085c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) size_t find(const String& str, unsigned start) const 20906f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles) { return m_impl ? m_impl->find(str.impl(), start) : kNotFound; } 2105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 2115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) size_t find(CharacterMatchFunctionPtr matchFunction, unsigned start = 0) const 21206f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles) { return m_impl ? m_impl->find(matchFunction, start) : kNotFound; } 2135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) size_t find(const LChar* str, unsigned start = 0) const 21406f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles) { return m_impl ? m_impl->find(str, start) : kNotFound; } 2155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 216926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) size_t findNextLineStart(unsigned start = 0) const 21706f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles) { return m_impl ? m_impl->findNextLineStart(start) : kNotFound; } 218926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 2195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Find the last instance of a single character or string. 2201e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) size_t reverseFind(UChar c, unsigned start = UINT_MAX) const 2211e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) { return m_impl ? m_impl->reverseFind(c, start) : kNotFound; } 2221e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) size_t reverseFind(const String& str, unsigned start = UINT_MAX) const 2231e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) { return m_impl ? m_impl->reverseFind(str.impl(), start) : kNotFound; } 2245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 2255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Case insensitive string matching. 2265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) size_t findIgnoringCase(const LChar* str, unsigned start = 0) const 22706f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles) { return m_impl ? m_impl->findIgnoringCase(str, start) : kNotFound; } 2285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) size_t findIgnoringCase(const String& str, unsigned start = 0) const 22906f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles) { return m_impl ? m_impl->findIgnoringCase(str.impl(), start) : kNotFound; } 2305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) size_t reverseFindIgnoringCase(const String& str, unsigned start = UINT_MAX) const 23106f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles) { return m_impl ? m_impl->reverseFindIgnoringCase(str.impl(), start) : kNotFound; } 2325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 2335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Wrappers for find & reverseFind adding dynamic sensitivity check. 2345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) size_t find(const LChar* str, unsigned start, bool caseSensitive) const 2355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) { return caseSensitive ? find(str, start) : findIgnoringCase(str, start); } 2365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) size_t find(const String& str, unsigned start, bool caseSensitive) const 2375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) { return caseSensitive ? find(str, start) : findIgnoringCase(str, start); } 2385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) size_t reverseFind(const String& str, unsigned start, bool caseSensitive) const 2395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) { return caseSensitive ? reverseFind(str, start) : reverseFindIgnoringCase(str, start); } 2405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 241591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch Vector<UChar> charactersWithNullTermination() const; 242e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch unsigned copyTo(UChar* buffer, unsigned pos, unsigned maxLength) const; 243591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch 244e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch template<size_t inlineCapacity> 245e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch void appendTo(Vector<UChar, inlineCapacity>&, unsigned pos = 0, unsigned len = UINT_MAX) const; 246e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch 2477757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch template<typename BufferType> 2487757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch void appendTo(BufferType&, unsigned pos = 0, unsigned len = UINT_MAX) const; 2497757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch 2507757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch template<size_t inlineCapacity> 2517757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch void prependTo(Vector<UChar, inlineCapacity>&, unsigned pos = 0, unsigned len = UINT_MAX) const; 2527757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch 253e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch UChar32 characterStartingAt(unsigned) const; 25402772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch 25506f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles) bool contains(UChar c) const { return find(c) != kNotFound; } 25606f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles) bool contains(const LChar* str, bool caseSensitive = true) const { return find(str, 0, caseSensitive) != kNotFound; } 25706f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles) bool contains(const String& str, bool caseSensitive = true) const { return find(str, 0, caseSensitive) != kNotFound; } 2585c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 2595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) bool startsWith(const String& s, bool caseSensitive = true) const 2605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) { return m_impl ? m_impl->startsWith(s.impl(), caseSensitive) : s.isEmpty(); } 2615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) bool startsWith(UChar character) const 2625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) { return m_impl ? m_impl->startsWith(character) : false; } 2635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) template<unsigned matchLength> 2645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) bool startsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const 2655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) { return m_impl ? m_impl->startsWith<matchLength>(prefix, caseSensitive) : !matchLength; } 2665c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 2675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) bool endsWith(const String& s, bool caseSensitive = true) const 2685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) { return m_impl ? m_impl->endsWith(s.impl(), caseSensitive) : s.isEmpty(); } 2695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) bool endsWith(UChar character) const 2705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) { return m_impl ? m_impl->endsWith(character) : false; } 2715c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) template<unsigned matchLength> 2725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) bool endsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const 2735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) { return m_impl ? m_impl->endsWith<matchLength>(prefix, caseSensitive) : !matchLength; } 2745c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 27553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) void append(const String&); 27653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) void append(LChar); 277d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles) void append(char c) { append(static_cast<LChar>(c)); } 27853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) void append(UChar); 27953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) void append(const LChar*, unsigned length); 280d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles) void append(const char* charactersToAppend, unsigned length) { append(reinterpret_cast<const LChar*>(charactersToAppend), length); } 28153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) void append(const UChar*, unsigned length); 28253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) void insert(const String&, unsigned pos); 2837757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch void insert(const LChar*, unsigned length, unsigned pos); 2845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) void insert(const UChar*, unsigned length, unsigned pos); 2855c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 2865c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) String& replace(UChar a, UChar b) { if (m_impl) m_impl = m_impl->replace(a, b); return *this; } 2875c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) String& replace(UChar a, const String& b) { if (m_impl) m_impl = m_impl->replace(a, b.impl()); return *this; } 2885c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) String& replace(const String& a, const String& b) { if (m_impl) m_impl = m_impl->replace(a.impl(), b.impl()); return *this; } 2895c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) String& replace(unsigned index, unsigned len, const String& b) { if (m_impl) m_impl = m_impl->replace(index, len, b.impl()); return *this; } 2905c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 2915c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) template<unsigned charactersCount> 2925c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) ALWAYS_INLINE String& replaceWithLiteral(UChar a, const char (&characters)[charactersCount]) 2935c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) { 2945c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) if (m_impl) 2955c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) m_impl = m_impl->replace(a, characters, charactersCount - 1); 2965c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 2975c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return *this; 2985c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) } 2995c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 3005c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) void fill(UChar c) { if (m_impl) m_impl = m_impl->fill(c); } 3015c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 3027757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch void ensure16Bit(); 3037757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch 30453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) void truncate(unsigned len); 30553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) void remove(unsigned pos, int len = 1); 3065c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 30753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) String substring(unsigned pos, unsigned len = UINT_MAX) const; 3085c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) String left(unsigned len) const { return substring(0, len); } 3095c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) String right(unsigned len) const { return substring(length() - len, len); } 3105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 3117757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch StringView createView() const { return StringView(impl()); } 3127757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch StringView createView(unsigned offset, unsigned length) const { return StringView(impl(), offset, length); } 3137757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch 3145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Returns a lowercase/uppercase version of the string 31553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) String lower() const; 31653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) String upper() const; 3175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 3181e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) String lower(const AtomicString& localeIdentifier) const; 3191e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) String upper(const AtomicString& localeIdentifier) const; 3201e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles) 32153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) String stripWhiteSpace() const; 32253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) String stripWhiteSpace(IsWhiteSpaceFunctionPtr) const; 32319cde67944066db31e633d9e386f2aa9bf9fadb3Torne (Richard Coles) String simplifyWhiteSpace(StripBehavior stripBehavior = StripExtraWhiteSpace) const; 32419cde67944066db31e633d9e386f2aa9bf9fadb3Torne (Richard Coles) String simplifyWhiteSpace(IsWhiteSpaceFunctionPtr, StripBehavior stripBehavior = StripExtraWhiteSpace) const; 3255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 32653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) String removeCharacters(CharacterMatchFunctionPtr) const; 3275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) template<bool isSpecialCharacter(UChar)> bool isAllSpecialCharacters() const; 3285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 3295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Return the string with case folded for case insensitive comparison. 33053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) String foldCase() const; 3315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 33253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) static String format(const char *, ...) WTF_ATTRIBUTE_PRINTF(1, 2); 3335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 3345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Returns an uninitialized string. The characters needs to be written 3355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // into the buffer returned in data before the returned string is used. 3365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Failure to do this will have unpredictable results. 3375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) static String createUninitialized(unsigned length, UChar*& data) { return StringImpl::createUninitialized(length, data); } 3385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) static String createUninitialized(unsigned length, LChar*& data) { return StringImpl::createUninitialized(length, data); } 3395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 34053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) void split(const String& separator, bool allowEmptyEntries, Vector<String>& result) const; 3415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) void split(const String& separator, Vector<String>& result) const 3425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) { 3435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) split(separator, false, result); 3445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) } 34553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) void split(UChar separator, bool allowEmptyEntries, Vector<String>& result) const; 3465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) void split(UChar separator, Vector<String>& result) const 3475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) { 3485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) split(separator, false, result); 3495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) } 3505c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 35153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) int toIntStrict(bool* ok = 0, int base = 10) const; 35253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) unsigned toUIntStrict(bool* ok = 0, int base = 10) const; 35353e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) int64_t toInt64Strict(bool* ok = 0, int base = 10) const; 3545c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) uint64_t toUInt64Strict(bool* ok = 0, int base = 10) const; 3555c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) intptr_t toIntPtrStrict(bool* ok = 0, int base = 10) const; 3565c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 35753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) int toInt(bool* ok = 0) const; 35853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) unsigned toUInt(bool* ok = 0) const; 3595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) int64_t toInt64(bool* ok = 0) const; 36053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) uint64_t toUInt64(bool* ok = 0) const; 36153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) intptr_t toIntPtr(bool* ok = 0) const; 3625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 3635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // FIXME: Like the strict functions above, these give false for "ok" when there is trailing garbage. 3645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Like the non-strict functions above, these return the value when there is trailing garbage. 3655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // It would be better if these were more consistent with the above functions instead. 36653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) double toDouble(bool* ok = 0) const; 36753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) float toFloat(bool* ok = 0) const; 3685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 3695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) bool percentage(int& percentage) const; 3705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 37153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) String isolatedCopy() const; 37253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) bool isSafeToSendToAnotherThread() const; 3735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 3745c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#if USE(CF) 3755c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) String(CFStringRef); 3765c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) RetainPtr<CFStringRef> createCFString() const; 3775c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif 3785c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 3795c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#ifdef __OBJC__ 3805c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) String(NSString*); 38102772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch 38202772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch // This conversion maps NULL to "", which loses the meaning of NULL, but we 3835c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // need this mapping because AppKit crashes when passed nil NSStrings. 3845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) operator NSString*() const { if (!m_impl) return @""; return *m_impl; } 3855c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif 3865c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 38753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) static String make8BitFrom16BitSource(const UChar*, size_t); 388926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) template<size_t inlineCapacity> 389926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) static String make8BitFrom16BitSource(const Vector<UChar, inlineCapacity>& buffer) 390926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) { 391926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) return make8BitFrom16BitSource(buffer.data(), buffer.size()); 392926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) } 393926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 39453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) static String make16BitFrom8BitSource(const LChar*, size_t); 3955c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 3965c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // String::fromUTF8 will return a null string if 3975c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // the input data contains invalid UTF-8 sequences. 39853e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) static String fromUTF8(const LChar*, size_t); 39953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) static String fromUTF8(const LChar*); 4005c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) static String fromUTF8(const char* s, size_t length) { return fromUTF8(reinterpret_cast<const LChar*>(s), length); }; 4015c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) static String fromUTF8(const char* s) { return fromUTF8(reinterpret_cast<const LChar*>(s)); }; 402926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) static String fromUTF8(const CString&); 4035c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 4045c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // 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. 40553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) static String fromUTF8WithLatin1Fallback(const LChar*, size_t); 4065c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) static String fromUTF8WithLatin1Fallback(const char* s, size_t length) { return fromUTF8WithLatin1Fallback(reinterpret_cast<const LChar*>(s), length); }; 40702772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch 4085c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) bool containsOnlyASCII() const; 4095c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) bool containsOnlyLatin1() const; 4105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) bool containsOnlyWhitespace() const { return !m_impl || m_impl->containsOnlyWhitespace(); } 4115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 4125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Hash table deleted values, which are only constructed and never copied or destroyed. 4135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) String(WTF::HashTableDeletedValueType) : m_impl(WTF::HashTableDeletedValue) { } 4145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) bool isHashTableDeletedValue() const { return m_impl.isHashTableDeletedValue(); } 4155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 4165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#ifndef NDEBUG 41753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles) void show() const; 4185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif 4195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 4205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Workaround for a compiler bug. Use operator[] instead. 4215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) UChar characterAt(unsigned index) const 4225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) { 4235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) if (!m_impl || index >= m_impl->length()) 4245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return 0; 4255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return (*m_impl)[index]; 4265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) } 4275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 4285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)private: 42909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) typedef struct ImplicitConversionFromWTFStringToBoolDisallowed* (String::*UnspecifiedBoolType); 43009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) operator UnspecifiedBoolType() const; 43109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) 4325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) template <typename CharacterType> 4335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) void removeInternal(const CharacterType*, unsigned, int); 4345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 435591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch template <typename CharacterType> 436591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch void appendInternal(CharacterType); 437591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch 4385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) RefPtr<StringImpl> m_impl; 4395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}; 4405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 4415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool operator==(const String& a, const String& b) { return equal(a.impl(), b.impl()); } 4425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool operator==(const String& a, const LChar* b) { return equal(a.impl(), b); } 4435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool operator==(const String& a, const char* b) { return equal(a.impl(), reinterpret_cast<const LChar*>(b)); } 4445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool operator==(const LChar* a, const String& b) { return equal(a, b.impl()); } 4455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool operator==(const char* a, const String& b) { return equal(reinterpret_cast<const LChar*>(a), b.impl()); } 4465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)template<size_t inlineCapacity> 4475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool operator==(const Vector<char, inlineCapacity>& a, const String& b) { return equal(b.impl(), a.data(), a.size()); } 4485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)template<size_t inlineCapacity> 4495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool operator==(const String& a, const Vector<char, inlineCapacity>& b) { return b == a; } 4505c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 4515c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 4525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool operator!=(const String& a, const String& b) { return !equal(a.impl(), b.impl()); } 4535c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool operator!=(const String& a, const LChar* b) { return !equal(a.impl(), b); } 4545c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool operator!=(const String& a, const char* b) { return !equal(a.impl(), reinterpret_cast<const LChar*>(b)); } 4555c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool operator!=(const LChar* a, const String& b) { return !equal(a, b.impl()); } 4565c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool operator!=(const char* a, const String& b) { return !equal(reinterpret_cast<const LChar*>(a), b.impl()); } 4575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)template<size_t inlineCapacity> 4585c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool operator!=(const Vector<char, inlineCapacity>& a, const String& b) { return !(a == b); } 4595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)template<size_t inlineCapacity> 4605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool operator!=(const String& a, const Vector<char, inlineCapacity>& b) { return b != a; } 4615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 4625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool equalIgnoringCase(const String& a, const String& b) { return equalIgnoringCase(a.impl(), b.impl()); } 4635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool equalIgnoringCase(const String& a, const LChar* b) { return equalIgnoringCase(a.impl(), b); } 4645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool equalIgnoringCase(const String& a, const char* b) { return equalIgnoringCase(a.impl(), reinterpret_cast<const LChar*>(b)); } 4655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool equalIgnoringCase(const LChar* a, const String& b) { return equalIgnoringCase(a, b.impl()); } 4665c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool equalIgnoringCase(const char* a, const String& b) { return equalIgnoringCase(reinterpret_cast<const LChar*>(a), b.impl()); } 4675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 46802772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdochinline bool equalPossiblyIgnoringCase(const String& a, const String& b, bool ignoreCase) 4695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 4705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return ignoreCase ? equalIgnoringCase(a, b) : (a == b); 4715c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 4725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 4735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool equalIgnoringNullity(const String& a, const String& b) { return equalIgnoringNullity(a.impl(), b.impl()); } 4745c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 4755c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)template<size_t inlineCapacity> 4765c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool equalIgnoringNullity(const Vector<UChar, inlineCapacity>& a, const String& b) { return equalIgnoringNullity(a, b.impl()); } 4775c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 4785c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool operator!(const String& str) { return str.isNull(); } 4795c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 4805c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline void swap(String& a, String& b) { a.swap(b); } 4815c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 4825c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// Definitions of string operations 4835c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 4845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)template<size_t inlineCapacity> 4855c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)String::String(const Vector<UChar, inlineCapacity>& vector) 486926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) : m_impl(vector.size() ? StringImpl::create(vector.data(), vector.size()) : StringImpl::empty()) 4875c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 4885c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 4895c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 4905c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)template<> 4915c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline const LChar* String::getCharacters<LChar>() const 4925c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 4935c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) ASSERT(is8Bit()); 4945c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return characters8(); 4955c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 4965c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 4975c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)template<> 4985c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline const UChar* String::getCharacters<UChar>() const 4995c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 5005c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) ASSERT(!is8Bit()); 5015c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return characters16(); 5025c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 5035c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 5045c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool String::containsOnlyLatin1() const 5055c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 5065c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) if (isEmpty()) 5075c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return true; 5085c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 5095c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) if (is8Bit()) 5105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return true; 5115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 5125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) const UChar* characters = characters16(); 5135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) UChar ored = 0; 5145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) for (size_t i = 0; i < m_impl->length(); ++i) 5155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) ored |= characters[i]; 5165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return !(ored & 0xFF00); 5175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 5185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 5195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 5205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#ifdef __OBJC__ 5215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// This is for situations in WebKit where the long standing behavior has been 5225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// "nil if empty", so we try to maintain longstanding behavior for the sake of 5235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// entrenched clients 5245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline NSString* nsStringNilIfEmpty(const String& str) { return str.isEmpty() ? nil : (NSString*)str; } 5255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif 5265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 5275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool String::containsOnlyASCII() const 5285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 5295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) if (isEmpty()) 5305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return true; 5315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 5325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) if (is8Bit()) 5335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return charactersAreAllASCII(characters8(), m_impl->length()); 5345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 5355c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return charactersAreAllASCII(characters16(), m_impl->length()); 5365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 5375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 53893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT int codePointCompare(const String&, const String&); 5395c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 5405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool codePointCompareLessThan(const String& a, const String& b) 5415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 5425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return codePointCompare(a.impl(), b.impl()) < 0; 5435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 5445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 545926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)template<size_t inlineCapacity> 546926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)inline void append(Vector<UChar, inlineCapacity>& vector, const String& string) 5475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 548591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch unsigned length = string.length(); 549591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch if (!length) 550591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch return; 551591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch if (string.is8Bit()) { 552591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch const LChar* characters8 = string.characters8(); 553591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch vector.reserveCapacity(vector.size() + length); 554591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch for (size_t i = 0; i < length; ++i) 555591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch vector.uncheckedAppend(characters8[i]); 556591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch } else { 557591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch vector.append(string.characters16(), length); 558591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch } 5595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 5605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 5615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)template<typename CharacterType> 5625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline void appendNumber(Vector<CharacterType>& vector, unsigned char number) 5635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 5645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) int numberLength = number > 99 ? 3 : (number > 9 ? 2 : 1); 5655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) size_t vectorSize = vector.size(); 5665c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) vector.grow(vectorSize + numberLength); 5675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 5685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) switch (numberLength) { 5695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) case 3: 5705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) vector[vectorSize + 2] = number % 10 + '0'; 5715c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) number /= 10; 5725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 5735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) case 2: 5745c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) vector[vectorSize + 1] = number % 10 + '0'; 5755c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) number /= 10; 5765c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 5775c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) case 1: 5785c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) vector[vectorSize] = number % 10 + '0'; 5795c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) } 5805c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 5815c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 5825c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)template<bool isSpecialCharacter(UChar), typename CharacterType> 5835c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool isAllSpecialCharacters(const CharacterType* characters, size_t length) 5845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 5855c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) for (size_t i = 0; i < length; ++i) { 5865c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) if (!isSpecialCharacter(characters[i])) 5875c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return false; 5885c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) } 5895c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return true; 5905c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 5915c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 5925c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)template<bool isSpecialCharacter(UChar)> 5935c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline bool String::isAllSpecialCharacters() const 5945c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 5955c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) size_t len = length(); 5965c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 5975c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) if (!len) 5985c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return true; 5995c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 6005c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) if (is8Bit()) 6015c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) return WTF::isAllSpecialCharacters<isSpecialCharacter, LChar>(characters8(), len); 6027757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch return WTF::isAllSpecialCharacters<isSpecialCharacter, UChar>(characters16(), len); 6035c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 6045c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 605e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdochtemplate<size_t inlineCapacity> 606e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdochinline void String::appendTo(Vector<UChar, inlineCapacity>& result, unsigned pos, unsigned len) const 607e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch{ 608e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch unsigned numberOfCharactersToCopy = std::min(len, length() - pos); 60909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) if (!numberOfCharactersToCopy) 610e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch return; 611e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch result.reserveCapacity(result.size() + numberOfCharactersToCopy); 612e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch if (is8Bit()) { 613e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch const LChar* characters8 = m_impl->characters8(); 614e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch for (size_t i = 0; i < numberOfCharactersToCopy; ++i) 615e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch result.uncheckedAppend(characters8[pos + i]); 616e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch } else { 617e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch const UChar* characters16 = m_impl->characters16(); 618e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch result.append(characters16 + pos, numberOfCharactersToCopy); 619e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch } 620e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch} 621e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch 6227757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdochtemplate<typename BufferType> 6237757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdochinline void String::appendTo(BufferType& result, unsigned pos, unsigned len) const 6247757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch{ 6257757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch unsigned numberOfCharactersToCopy = std::min(len, length() - pos); 62609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) if (!numberOfCharactersToCopy) 6277757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch return; 6287757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch if (is8Bit()) 6297757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch result.append(m_impl->characters8() + pos, numberOfCharactersToCopy); 6307757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch else 6317757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch result.append(m_impl->characters16() + pos, numberOfCharactersToCopy); 6327757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch} 6337757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch 6347757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdochtemplate<size_t inlineCapacity> 6357757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdochinline void String::prependTo(Vector<UChar, inlineCapacity>& result, unsigned pos, unsigned len) const 6367757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch{ 6377757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch unsigned numberOfCharactersToCopy = std::min(len, length() - pos); 63809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles) if (!numberOfCharactersToCopy) 6397757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch return; 6407757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch if (is8Bit()) { 6417757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch size_t oldSize = result.size(); 6427757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch result.resize(oldSize + numberOfCharactersToCopy); 6437757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch memmove(result.data() + numberOfCharactersToCopy, result.data(), oldSize * sizeof(UChar)); 6447757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch StringImpl::copyChars(result.data(), m_impl->characters8() + pos, numberOfCharactersToCopy); 6457757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch } else { 6467757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch result.prepend(m_impl->characters16() + pos, numberOfCharactersToCopy); 6477757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch } 6487757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch} 6497757ec2eadfa2dd8ac2aeed0a4399e9b07ec38cbBen Murdoch 6505c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// StringHash is the default hash for String 6515c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)template<typename T> struct DefaultHash; 6525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)template<> struct DefaultHash<String> { 6535c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) typedef StringHash Hash; 6545c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}; 6555c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 6565c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)// Shared global empty string. 65793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)WTF_EXPORT const String& emptyString(); 6587242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano TucciWTF_EXPORT const String& emptyString16Bit(); 659c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)WTF_EXPORT extern const String& xmlnsWithColon; 6606f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch 6616f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch} // namespace WTF 6625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 663f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo LiuWTF_ALLOW_MOVE_AND_INIT_WITH_MEM_FUNCTIONS(String); 664f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu 6655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::CString; 6665c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::KeepTrailingZeros; 66709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)using WTF::StrictUTF8Conversion; 66809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)using WTF::StrictUTF8ConversionReplacingUnpairedSurrogatesWithFFFD; 6695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::String; 6705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::emptyString; 6717242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucciusing WTF::emptyString16Bit; 6725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::append; 6735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::appendNumber; 6745c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::charactersAreAllASCII; 6755c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::charactersToIntStrict; 6765c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::charactersToUIntStrict; 6775c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::charactersToInt64Strict; 6785c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::charactersToUInt64Strict; 6795c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::charactersToIntPtrStrict; 6805c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::charactersToInt; 6815c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::charactersToUInt; 6825c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::charactersToInt64; 6835c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::charactersToUInt64; 6845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::charactersToIntPtr; 6855c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::charactersToDouble; 6865c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::charactersToFloat; 6875c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::equal; 6885c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::equalIgnoringCase; 6895c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::find; 6905c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::isAllSpecialCharacters; 6915c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::isSpaceOrNewline; 6925c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)using WTF::reverseFind; 6935c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 694591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#include "wtf/text/AtomicString.h" 6956f543c786fc42989f552b4daa774ca5ff32fa697Ben Murdoch#endif // WTFString_h 696