| 1 | /* |
| 2 | * (C) 1999 Lars Knoll (knoll@kde.org) |
| 3 | * Copyright (C) 2004-2018 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 | #pragma once |
| 23 | |
| 24 | // This file would be called String.h, but that conflicts with <string.h> |
| 25 | // on systems without case-sensitive file systems. |
| 26 | |
| 27 | #include <stdarg.h> |
| 28 | #include <wtf/Function.h> |
| 29 | #include <wtf/text/ASCIILiteral.h> |
| 30 | #include <wtf/text/IntegerToStringConversion.h> |
| 31 | #include <wtf/text/StringImpl.h> |
| 32 | |
| 33 | #ifdef __OBJC__ |
| 34 | #include <objc/objc.h> |
| 35 | #endif |
| 36 | |
| 37 | #if OS(WINDOWS) |
| 38 | #include <wtf/text/win/WCharStringExtras.h> |
| 39 | #endif |
| 40 | |
| 41 | namespace WTF { |
| 42 | |
| 43 | // Declarations of string operations |
| 44 | |
| 45 | WTF_EXPORT_PRIVATE int charactersToIntStrict(const LChar*, size_t, bool* ok = nullptr, int base = 10); |
| 46 | WTF_EXPORT_PRIVATE int charactersToIntStrict(const UChar*, size_t, bool* ok = nullptr, int base = 10); |
| 47 | WTF_EXPORT_PRIVATE unsigned charactersToUIntStrict(const LChar*, size_t, bool* ok = nullptr, int base = 10); |
| 48 | WTF_EXPORT_PRIVATE unsigned charactersToUIntStrict(const UChar*, size_t, bool* ok = nullptr, int base = 10); |
| 49 | int64_t charactersToInt64Strict(const LChar*, size_t, bool* ok = nullptr, int base = 10); |
| 50 | int64_t charactersToInt64Strict(const UChar*, size_t, bool* ok = nullptr, int base = 10); |
| 51 | WTF_EXPORT_PRIVATE uint64_t charactersToUInt64Strict(const LChar*, size_t, bool* ok = nullptr, int base = 10); |
| 52 | WTF_EXPORT_PRIVATE uint64_t charactersToUInt64Strict(const UChar*, size_t, bool* ok = nullptr, int base = 10); |
| 53 | intptr_t charactersToIntPtrStrict(const LChar*, size_t, bool* ok = nullptr, int base = 10); |
| 54 | intptr_t charactersToIntPtrStrict(const UChar*, size_t, bool* ok = nullptr, int base = 10); |
| 55 | |
| 56 | WTF_EXPORT_PRIVATE int charactersToInt(const LChar*, size_t, bool* ok = nullptr); // ignores trailing garbage |
| 57 | WTF_EXPORT_PRIVATE int charactersToInt(const UChar*, size_t, bool* ok = nullptr); // ignores trailing garbage |
| 58 | unsigned charactersToUInt(const LChar*, size_t, bool* ok = nullptr); // ignores trailing garbage |
| 59 | unsigned charactersToUInt(const UChar*, size_t, bool* ok = nullptr); // ignores trailing garbage |
| 60 | int64_t charactersToInt64(const LChar*, size_t, bool* ok = nullptr); // ignores trailing garbage |
| 61 | int64_t charactersToInt64(const UChar*, size_t, bool* ok = nullptr); // ignores trailing garbage |
| 62 | uint64_t charactersToUInt64(const LChar*, size_t, bool* ok = nullptr); // ignores trailing garbage |
| 63 | WTF_EXPORT_PRIVATE uint64_t charactersToUInt64(const UChar*, size_t, bool* ok = nullptr); // ignores trailing garbage |
| 64 | intptr_t charactersToIntPtr(const LChar*, size_t, bool* ok = nullptr); // ignores trailing garbage |
| 65 | intptr_t charactersToIntPtr(const UChar*, size_t, bool* ok = nullptr); // 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_PRIVATE double charactersToDouble(const LChar*, size_t, bool* ok = nullptr); |
| 71 | WTF_EXPORT_PRIVATE double charactersToDouble(const UChar*, size_t, bool* ok = nullptr); |
| 72 | WTF_EXPORT_PRIVATE float charactersToFloat(const LChar*, size_t, bool* ok = nullptr); |
| 73 | WTF_EXPORT_PRIVATE float charactersToFloat(const UChar*, size_t, bool* ok = nullptr); |
| 74 | WTF_EXPORT_PRIVATE float charactersToFloat(const LChar*, size_t, size_t& parsedLength); |
| 75 | WTF_EXPORT_PRIVATE float charactersToFloat(const UChar*, size_t, size_t& parsedLength); |
| 76 | |
| 77 | template<bool isSpecialCharacter(UChar), typename CharacterType> bool isAllSpecialCharacters(const CharacterType*, size_t); |
| 78 | |
| 79 | enum TrailingZerosTruncatingPolicy { KeepTrailingZeros, TruncateTrailingZeros }; |
| 80 | |
| 81 | class String { |
| 82 | public: |
| 83 | // Construct a null string, distinguishable from an empty string. |
| 84 | String() = default; |
| 85 | |
| 86 | // Construct a string with UTF-16 data. |
| 87 | WTF_EXPORT_PRIVATE String(const UChar* characters, unsigned length); |
| 88 | |
| 89 | // Construct a string by copying the contents of a vector. To avoid |
| 90 | // copying, consider using String::adopt instead. |
| 91 | // This method will never create a null string. Vectors with size() == 0 |
| 92 | // will return the empty string. |
| 93 | // NOTE: This is different from String(vector.data(), vector.size()) |
| 94 | // which will sometimes return a null string when vector.data() is null |
| 95 | // which can only occur for vectors without inline capacity. |
| 96 | // See: https://bugs.webkit.org/show_bug.cgi?id=109792 |
| 97 | template<size_t inlineCapacity, typename OverflowHandler> |
| 98 | explicit String(const Vector<UChar, inlineCapacity, OverflowHandler>&); |
| 99 | |
| 100 | // Construct a string with UTF-16 data, from a null-terminated source. |
| 101 | WTF_EXPORT_PRIVATE String(const UChar*); |
| 102 | |
| 103 | // Construct a string with latin1 data. |
| 104 | WTF_EXPORT_PRIVATE String(const LChar* characters, unsigned length); |
| 105 | WTF_EXPORT_PRIVATE String(const char* characters, unsigned length); |
| 106 | |
| 107 | // Construct a string with latin1 data, from a null-terminated source. |
| 108 | WTF_EXPORT_PRIVATE String(const LChar* characters); |
| 109 | WTF_EXPORT_PRIVATE String(const char* characters); |
| 110 | |
| 111 | // Construct a string referencing an existing StringImpl. |
| 112 | String(StringImpl&); |
| 113 | String(StringImpl*); |
| 114 | String(Ref<StringImpl>&&); |
| 115 | String(RefPtr<StringImpl>&&); |
| 116 | |
| 117 | String(Ref<AtomicStringImpl>&&); |
| 118 | String(RefPtr<AtomicStringImpl>&&); |
| 119 | |
| 120 | String(StaticStringImpl&); |
| 121 | String(StaticStringImpl*); |
| 122 | |
| 123 | // Construct a string from a constant string literal. |
| 124 | WTF_EXPORT_PRIVATE String(ASCIILiteral); |
| 125 | |
| 126 | // Construct a string from a constant string literal. |
| 127 | // This constructor is the "big" version, as it put the length in the function call and generate bigger code. |
| 128 | enum ConstructFromLiteralTag { ConstructFromLiteral }; |
| 129 | template<unsigned characterCount> String(const char (&characters)[characterCount], ConstructFromLiteralTag) : m_impl(StringImpl::createFromLiteral<characterCount>(characters)) { } |
| 130 | |
| 131 | // FIXME: Why do we have to define these explicitly given that we just want the default versions? |
| 132 | // We have verified empirically that we do. |
| 133 | String(const String&) = default; |
| 134 | String(String&&) = default; |
| 135 | String& operator=(const String&) = default; |
| 136 | String& operator=(String&&) = default; |
| 137 | |
| 138 | ALWAYS_INLINE ~String() = default; |
| 139 | |
| 140 | void swap(String& o) { m_impl.swap(o.m_impl); } |
| 141 | |
| 142 | static String adopt(StringBuffer<LChar>&& buffer) { return StringImpl::adopt(WTFMove(buffer)); } |
| 143 | static String adopt(StringBuffer<UChar>&& buffer) { return StringImpl::adopt(WTFMove(buffer)); } |
| 144 | template<typename CharacterType, size_t inlineCapacity, typename OverflowHandler, size_t minCapacity> |
| 145 | static String adopt(Vector<CharacterType, inlineCapacity, OverflowHandler, minCapacity>&& vector) { return StringImpl::adopt(WTFMove(vector)); } |
| 146 | |
| 147 | bool isNull() const { return !m_impl; } |
| 148 | bool isEmpty() const { return !m_impl || m_impl->isEmpty(); } |
| 149 | |
| 150 | StringImpl* impl() const { return m_impl.get(); } |
| 151 | RefPtr<StringImpl> releaseImpl() { return WTFMove(m_impl); } |
| 152 | |
| 153 | unsigned length() const { return m_impl ? m_impl->length() : 0; } |
| 154 | const LChar* characters8() const { return m_impl ? m_impl->characters8() : nullptr; } |
| 155 | const UChar* characters16() const { return m_impl ? m_impl->characters16() : nullptr; } |
| 156 | |
| 157 | // Return characters8() or characters16() depending on CharacterType. |
| 158 | template<typename CharacterType> const CharacterType* characters() const; |
| 159 | |
| 160 | bool is8Bit() const { return !m_impl || m_impl->is8Bit(); } |
| 161 | |
| 162 | unsigned sizeInBytes() const { return m_impl ? m_impl->length() * (is8Bit() ? sizeof(LChar) : sizeof(UChar)) : 0; } |
| 163 | |
| 164 | WTF_EXPORT_PRIVATE CString ascii() const; |
| 165 | WTF_EXPORT_PRIVATE CString latin1() const; |
| 166 | |
| 167 | WTF_EXPORT_PRIVATE CString utf8(ConversionMode) const; |
| 168 | WTF_EXPORT_PRIVATE CString utf8() const; |
| 169 | |
| 170 | WTF_EXPORT_PRIVATE Expected<CString, UTF8ConversionError> tryGetUtf8(ConversionMode) const; |
| 171 | WTF_EXPORT_PRIVATE Expected<CString, UTF8ConversionError> tryGetUtf8() const; |
| 172 | |
| 173 | UChar characterAt(unsigned index) const; |
| 174 | UChar operator[](unsigned index) const { return characterAt(index); } |
| 175 | |
| 176 | WTF_EXPORT_PRIVATE static String number(int); |
| 177 | WTF_EXPORT_PRIVATE static String number(unsigned); |
| 178 | WTF_EXPORT_PRIVATE static String number(long); |
| 179 | WTF_EXPORT_PRIVATE static String number(unsigned long); |
| 180 | WTF_EXPORT_PRIVATE static String number(long long); |
| 181 | WTF_EXPORT_PRIVATE static String number(unsigned long long); |
| 182 | // FIXME: Change to call numberToStringShortest. |
| 183 | static String number(float) = delete; |
| 184 | static String number(double) = delete; |
| 185 | |
| 186 | WTF_EXPORT_PRIVATE static String numberToStringShortest(float); |
| 187 | WTF_EXPORT_PRIVATE static String numberToStringShortest(double); |
| 188 | WTF_EXPORT_PRIVATE static String numberToStringFixedPrecision(float, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros); |
| 189 | WTF_EXPORT_PRIVATE static String numberToStringFixedPrecision(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros); |
| 190 | WTF_EXPORT_PRIVATE static String numberToStringFixedWidth(float, unsigned decimalPlaces); |
| 191 | WTF_EXPORT_PRIVATE static String numberToStringFixedWidth(double, unsigned decimalPlaces); |
| 192 | |
| 193 | // FIXME: Delete in favor of the name numberToStringShortest or just number. |
| 194 | static String numberToStringECMAScript(float) = delete; |
| 195 | static String numberToStringECMAScript(double); |
| 196 | |
| 197 | // Find a single character or string, also with match function & latin1 forms. |
| 198 | size_t find(UChar character, unsigned start = 0) const { return m_impl ? m_impl->find(character, start) : notFound; } |
| 199 | |
| 200 | size_t find(const String& string) const { return m_impl ? m_impl->find(string.impl()) : notFound; } |
| 201 | size_t find(const String& string, unsigned start) const { return m_impl ? m_impl->find(string.impl(), start) : notFound; } |
| 202 | size_t findIgnoringASCIICase(const String& string) const { return m_impl ? m_impl->findIgnoringASCIICase(string.impl()) : notFound; } |
| 203 | size_t findIgnoringASCIICase(const String& string, unsigned startOffset) const { return m_impl ? m_impl->findIgnoringASCIICase(string.impl(), startOffset) : notFound; } |
| 204 | |
| 205 | size_t find(CodeUnitMatchFunction matchFunction, unsigned start = 0) const { return m_impl ? m_impl->find(matchFunction, start) : notFound; } |
| 206 | size_t find(const LChar* string, unsigned start = 0) const { return m_impl ? m_impl->find(string, start) : notFound; } |
| 207 | |
| 208 | // Find the last instance of a single character or string. |
| 209 | size_t reverseFind(UChar character, unsigned start = MaxLength) const { return m_impl ? m_impl->reverseFind(character, start) : notFound; } |
| 210 | size_t reverseFind(const String& string, unsigned start = MaxLength) const { return m_impl ? m_impl->reverseFind(string.impl(), start) : notFound; } |
| 211 | |
| 212 | WTF_EXPORT_PRIVATE Vector<UChar> charactersWithNullTermination() const; |
| 213 | |
| 214 | WTF_EXPORT_PRIVATE UChar32 characterStartingAt(unsigned) const; |
| 215 | |
| 216 | bool contains(UChar character) const { return find(character) != notFound; } |
| 217 | bool contains(const LChar* string) const { return find(string) != notFound; } |
| 218 | bool contains(const String& string) const { return find(string) != notFound; } |
| 219 | bool containsIgnoringASCIICase(const String& string) const { return findIgnoringASCIICase(string) != notFound; } |
| 220 | bool containsIgnoringASCIICase(const String& string, unsigned startOffset) const { return findIgnoringASCIICase(string, startOffset) != notFound; } |
| 221 | |
| 222 | bool startsWith(const String& string) const { return m_impl ? m_impl->startsWith(string.impl()) : string.isEmpty(); } |
| 223 | bool startsWithIgnoringASCIICase(const String& string) const { return m_impl ? m_impl->startsWithIgnoringASCIICase(string.impl()) : string.isEmpty(); } |
| 224 | bool startsWith(UChar character) const { return m_impl && m_impl->startsWith(character); } |
| 225 | template<unsigned matchLength> bool startsWith(const char (&prefix)[matchLength]) const { return m_impl ? m_impl->startsWith<matchLength>(prefix) : !matchLength; } |
| 226 | bool hasInfixStartingAt(const String& prefix, unsigned startOffset) const { return m_impl && prefix.impl() && m_impl->hasInfixStartingAt(*prefix.impl(), startOffset); } |
| 227 | |
| 228 | bool endsWith(const String& string) const { return m_impl ? m_impl->endsWith(string.impl()) : string.isEmpty(); } |
| 229 | bool endsWithIgnoringASCIICase(const String& string) const { return m_impl ? m_impl->endsWithIgnoringASCIICase(string.impl()) : string.isEmpty(); } |
| 230 | bool endsWith(UChar character) const { return m_impl && m_impl->endsWith(character); } |
| 231 | bool endsWith(char character) const { return endsWith(static_cast<UChar>(character)); } |
| 232 | template<unsigned matchLength> bool endsWith(const char (&prefix)[matchLength]) const { return m_impl ? m_impl->endsWith<matchLength>(prefix) : !matchLength; } |
| 233 | bool hasInfixEndingAt(const String& suffix, unsigned endOffset) const { return m_impl && suffix.impl() && m_impl->hasInfixEndingAt(*suffix.impl(), endOffset); } |
| 234 | |
| 235 | WTF_EXPORT_PRIVATE void append(const String&); |
| 236 | WTF_EXPORT_PRIVATE void append(LChar); |
| 237 | void append(char character) { append(static_cast<LChar>(character)); }; |
| 238 | WTF_EXPORT_PRIVATE void append(UChar); |
| 239 | WTF_EXPORT_PRIVATE void append(const LChar*, unsigned length); |
| 240 | WTF_EXPORT_PRIVATE void append(const UChar*, unsigned length); |
| 241 | WTF_EXPORT_PRIVATE void insert(const String&, unsigned position); |
| 242 | |
| 243 | String& replace(UChar target, UChar replacement); |
| 244 | String& replace(UChar target, const String& replacement); |
| 245 | String& replace(const String& target, const String& replacement); |
| 246 | String& replace(unsigned start, unsigned length, const String& replacement); |
| 247 | template<unsigned characterCount> String& replaceWithLiteral(UChar target, const char (&replacement)[characterCount]); |
| 248 | |
| 249 | WTF_EXPORT_PRIVATE void truncate(unsigned length); |
| 250 | WTF_EXPORT_PRIVATE void remove(unsigned position, unsigned length = 1); |
| 251 | |
| 252 | WTF_EXPORT_PRIVATE String substring(unsigned position, unsigned length = MaxLength) const; |
| 253 | WTF_EXPORT_PRIVATE String substringSharingImpl(unsigned position, unsigned length = MaxLength) const; |
| 254 | String left(unsigned length) const { return substring(0, length); } |
| 255 | String right(unsigned length) const { return substring(this->length() - length, length); } |
| 256 | |
| 257 | WTF_EXPORT_PRIVATE String convertToASCIILowercase() const; |
| 258 | WTF_EXPORT_PRIVATE String convertToASCIIUppercase() const; |
| 259 | WTF_EXPORT_PRIVATE String convertToLowercaseWithoutLocale() const; |
| 260 | WTF_EXPORT_PRIVATE String convertToLowercaseWithoutLocaleStartingAtFailingIndex8Bit(unsigned) const; |
| 261 | WTF_EXPORT_PRIVATE String convertToUppercaseWithoutLocale() const; |
| 262 | WTF_EXPORT_PRIVATE String convertToLowercaseWithLocale(const AtomicString& localeIdentifier) const; |
| 263 | WTF_EXPORT_PRIVATE String convertToUppercaseWithLocale(const AtomicString& localeIdentifier) const; |
| 264 | |
| 265 | WTF_EXPORT_PRIVATE String stripWhiteSpace() const; |
| 266 | WTF_EXPORT_PRIVATE String simplifyWhiteSpace() const; |
| 267 | WTF_EXPORT_PRIVATE String simplifyWhiteSpace(CodeUnitMatchFunction) const; |
| 268 | |
| 269 | WTF_EXPORT_PRIVATE String stripLeadingAndTrailingCharacters(CodeUnitMatchFunction) const; |
| 270 | WTF_EXPORT_PRIVATE String removeCharacters(CodeUnitMatchFunction) const; |
| 271 | |
| 272 | // Returns the string with case folded for case insensitive comparison. |
| 273 | // Use convertToASCIILowercase instead if ASCII case insensitive comparison is desired. |
| 274 | WTF_EXPORT_PRIVATE String foldCase() const; |
| 275 | |
| 276 | // Returns an uninitialized string. The characters needs to be written |
| 277 | // into the buffer returned in data before the returned string is used. |
| 278 | static String createUninitialized(unsigned length, UChar*& data) { return StringImpl::createUninitialized(length, data); } |
| 279 | static String createUninitialized(unsigned length, LChar*& data) { return StringImpl::createUninitialized(length, data); } |
| 280 | |
| 281 | using SplitFunctor = WTF::Function<void(const StringView&)>; |
| 282 | |
| 283 | WTF_EXPORT_PRIVATE void split(UChar separator, const SplitFunctor&) const; |
| 284 | WTF_EXPORT_PRIVATE Vector<String> split(UChar separator) const; |
| 285 | WTF_EXPORT_PRIVATE Vector<String> split(const String& separator) const; |
| 286 | |
| 287 | WTF_EXPORT_PRIVATE void splitAllowingEmptyEntries(UChar separator, const SplitFunctor&) const; |
| 288 | WTF_EXPORT_PRIVATE Vector<String> splitAllowingEmptyEntries(UChar separator) const; |
| 289 | WTF_EXPORT_PRIVATE Vector<String> splitAllowingEmptyEntries(const String& separator) const; |
| 290 | |
| 291 | WTF_EXPORT_PRIVATE int toIntStrict(bool* ok = nullptr, int base = 10) const; |
| 292 | WTF_EXPORT_PRIVATE unsigned toUIntStrict(bool* ok = nullptr, int base = 10) const; |
| 293 | WTF_EXPORT_PRIVATE int64_t toInt64Strict(bool* ok = nullptr, int base = 10) const; |
| 294 | WTF_EXPORT_PRIVATE uint64_t toUInt64Strict(bool* ok = nullptr, int base = 10) const; |
| 295 | WTF_EXPORT_PRIVATE intptr_t toIntPtrStrict(bool* ok = nullptr, int base = 10) const; |
| 296 | |
| 297 | WTF_EXPORT_PRIVATE int toInt(bool* ok = nullptr) const; |
| 298 | WTF_EXPORT_PRIVATE unsigned toUInt(bool* ok = nullptr) const; |
| 299 | WTF_EXPORT_PRIVATE int64_t toInt64(bool* ok = nullptr) const; |
| 300 | WTF_EXPORT_PRIVATE uint64_t toUInt64(bool* ok = nullptr) const; |
| 301 | WTF_EXPORT_PRIVATE intptr_t toIntPtr(bool* ok = nullptr) const; |
| 302 | |
| 303 | // FIXME: Like the strict functions above, these give false for "ok" when there is trailing garbage. |
| 304 | // Like the non-strict functions above, these return the value when there is trailing garbage. |
| 305 | // It would be better if these were more consistent with the above functions instead. |
| 306 | WTF_EXPORT_PRIVATE double toDouble(bool* ok = nullptr) const; |
| 307 | WTF_EXPORT_PRIVATE float toFloat(bool* ok = nullptr) const; |
| 308 | |
| 309 | bool percentage(int& percentage) const; |
| 310 | |
| 311 | WTF_EXPORT_PRIVATE String isolatedCopy() const &; |
| 312 | WTF_EXPORT_PRIVATE String isolatedCopy() &&; |
| 313 | |
| 314 | WTF_EXPORT_PRIVATE bool isSafeToSendToAnotherThread() const; |
| 315 | |
| 316 | // Prevent Strings from being implicitly convertable to bool as it will be ambiguous on any platform that |
| 317 | // allows implicit conversion to another pointer type (e.g., Mac allows implicit conversion to NSString *). |
| 318 | typedef struct ImplicitConversionFromWTFStringToBoolDisallowedA* (String::*UnspecifiedBoolTypeA); |
| 319 | typedef struct ImplicitConversionFromWTFStringToBoolDisallowedB* (String::*UnspecifiedBoolTypeB); |
| 320 | operator UnspecifiedBoolTypeA() const; |
| 321 | operator UnspecifiedBoolTypeB() const; |
| 322 | |
| 323 | #if USE(CF) |
| 324 | WTF_EXPORT_PRIVATE String(CFStringRef); |
| 325 | WTF_EXPORT_PRIVATE RetainPtr<CFStringRef> createCFString() const; |
| 326 | #endif |
| 327 | |
| 328 | #ifdef __OBJC__ |
| 329 | WTF_EXPORT_PRIVATE String(NSString *); |
| 330 | |
| 331 | // This conversion converts the null string to an empty NSString rather than to nil. |
| 332 | // Given Cocoa idioms, this is a more useful default. Clients that need to preserve the |
| 333 | // null string can check isNull explicitly. |
| 334 | operator NSString *() const; |
| 335 | #endif |
| 336 | |
| 337 | #if OS(WINDOWS) |
| 338 | #if U_ICU_VERSION_MAJOR_NUM >= 59 |
| 339 | String(const wchar_t* characters, unsigned length) |
| 340 | : String(ucharFrom(characters), length) { } |
| 341 | |
| 342 | String(const wchar_t* characters) |
| 343 | : String(ucharFrom(characters)) { } |
| 344 | #endif |
| 345 | |
| 346 | WTF_EXPORT_PRIVATE Vector<wchar_t> wideCharacters() const; |
| 347 | #endif |
| 348 | |
| 349 | WTF_EXPORT_PRIVATE static String make8BitFrom16BitSource(const UChar*, size_t); |
| 350 | template<size_t inlineCapacity> static String make8BitFrom16BitSource(const Vector<UChar, inlineCapacity>&); |
| 351 | |
| 352 | WTF_EXPORT_PRIVATE static String make16BitFrom8BitSource(const LChar*, size_t); |
| 353 | |
| 354 | // String::fromUTF8 will return a null string if |
| 355 | // the input data contains invalid UTF-8 sequences. |
| 356 | WTF_EXPORT_PRIVATE static String fromUTF8(const LChar*, size_t); |
| 357 | WTF_EXPORT_PRIVATE static String fromUTF8(const LChar*); |
| 358 | static String fromUTF8(const char* characters, size_t length) { return fromUTF8(reinterpret_cast<const LChar*>(characters), length); }; |
| 359 | static String fromUTF8(const char* string) { return fromUTF8(reinterpret_cast<const LChar*>(string)); }; |
| 360 | WTF_EXPORT_PRIVATE static String fromUTF8(const CString&); |
| 361 | static String fromUTF8(const Vector<LChar>& characters); |
| 362 | |
| 363 | // 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. |
| 364 | WTF_EXPORT_PRIVATE static String fromUTF8WithLatin1Fallback(const LChar*, size_t); |
| 365 | static String fromUTF8WithLatin1Fallback(const char* characters, size_t length) { return fromUTF8WithLatin1Fallback(reinterpret_cast<const LChar*>(characters), length); }; |
| 366 | |
| 367 | // Determines the writing direction using the Unicode Bidi Algorithm rules P2 and P3. |
| 368 | UCharDirection defaultWritingDirection(bool* hasStrongDirectionality = nullptr) const; |
| 369 | |
| 370 | bool isAllASCII() const { return !m_impl || m_impl->isAllASCII(); } |
| 371 | bool isAllLatin1() const { return !m_impl || m_impl->isAllLatin1(); } |
| 372 | template<bool isSpecialCharacter(UChar)> bool isAllSpecialCharacters() const { return !m_impl || m_impl->isAllSpecialCharacters<isSpecialCharacter>(); } |
| 373 | |
| 374 | // Hash table deleted values, which are only constructed and never copied or destroyed. |
| 375 | String(WTF::HashTableDeletedValueType) : m_impl(WTF::HashTableDeletedValue) { } |
| 376 | bool isHashTableDeletedValue() const { return m_impl.isHashTableDeletedValue(); } |
| 377 | |
| 378 | unsigned hash() const { return isNull() ? 0 : impl()->hash(); } |
| 379 | unsigned existingHash() const { return isNull() ? 0 : impl()->existingHash(); } |
| 380 | |
| 381 | #ifndef NDEBUG |
| 382 | WTF_EXPORT_PRIVATE void show() const; |
| 383 | #endif |
| 384 | |
| 385 | // Turns this String empty if the StringImpl is not referenced by anyone else. |
| 386 | // This is useful for clearing String-based caches. |
| 387 | void clearImplIfNotShared(); |
| 388 | |
| 389 | static constexpr unsigned MaxLength = StringImpl::MaxLength; |
| 390 | |
| 391 | private: |
| 392 | template<typename CharacterType> void removeInternal(const CharacterType*, unsigned, unsigned); |
| 393 | |
| 394 | template<bool allowEmptyEntries> void splitInternal(UChar separator, const SplitFunctor&) const; |
| 395 | template<bool allowEmptyEntries> Vector<String> splitInternal(UChar separator) const; |
| 396 | template<bool allowEmptyEntries> Vector<String> splitInternal(const String& separator) const; |
| 397 | |
| 398 | RefPtr<StringImpl> m_impl; |
| 399 | }; |
| 400 | |
| 401 | static_assert(sizeof(String) == sizeof(void*), "String should effectively be a pointer to a StringImpl, and efficient to pass by value" ); |
| 402 | |
| 403 | inline bool operator==(const String& a, const String& b) { return equal(a.impl(), b.impl()); } |
| 404 | inline bool operator==(const String& a, const LChar* b) { return equal(a.impl(), b); } |
| 405 | inline bool operator==(const String& a, const char* b) { return equal(a.impl(), reinterpret_cast<const LChar*>(b)); } |
| 406 | inline bool operator==(const String& a, ASCIILiteral b) { return equal(a.impl(), reinterpret_cast<const LChar*>(b.characters())); } |
| 407 | inline bool operator==(const LChar* a, const String& b) { return equal(a, b.impl()); } |
| 408 | inline bool operator==(const char* a, const String& b) { return equal(reinterpret_cast<const LChar*>(a), b.impl()); } |
| 409 | inline bool operator==(ASCIILiteral a, const String& b) { return equal(reinterpret_cast<const LChar*>(a.characters()), b.impl()); } |
| 410 | template<size_t inlineCapacity> inline bool operator==(const Vector<char, inlineCapacity>& a, const String& b) { return equal(b.impl(), a.data(), a.size()); } |
| 411 | template<size_t inlineCapacity> inline bool operator==(const String& a, const Vector<char, inlineCapacity>& b) { return b == a; } |
| 412 | |
| 413 | inline bool operator!=(const String& a, const String& b) { return !equal(a.impl(), b.impl()); } |
| 414 | inline bool operator!=(const String& a, const LChar* b) { return !equal(a.impl(), b); } |
| 415 | inline bool operator!=(const String& a, const char* b) { return !equal(a.impl(), reinterpret_cast<const LChar*>(b)); } |
| 416 | inline bool operator!=(const String& a, ASCIILiteral b) { return !equal(a.impl(), reinterpret_cast<const LChar*>(b.characters())); } |
| 417 | inline bool operator!=(const LChar* a, const String& b) { return !equal(a, b.impl()); } |
| 418 | inline bool operator!=(const char* a, const String& b) { return !equal(reinterpret_cast<const LChar*>(a), b.impl()); } |
| 419 | inline bool operator!=(ASCIILiteral a, const String& b) { return !equal(reinterpret_cast<const LChar*>(a.characters()), b.impl()); } |
| 420 | template<size_t inlineCapacity> inline bool operator!=(const Vector<char, inlineCapacity>& a, const String& b) { return !(a == b); } |
| 421 | template<size_t inlineCapacity> inline bool operator!=(const String& a, const Vector<char, inlineCapacity>& b) { return b != a; } |
| 422 | |
| 423 | bool equalIgnoringASCIICase(const String&, const String&); |
| 424 | bool equalIgnoringASCIICase(const String&, const char*); |
| 425 | |
| 426 | template<unsigned length> bool equalLettersIgnoringASCIICase(const String&, const char (&lowercaseLetters)[length]); |
| 427 | template<unsigned length> bool startsWithLettersIgnoringASCIICase(const String&, const char (&lowercaseLetters)[length]); |
| 428 | |
| 429 | inline bool equalIgnoringNullity(const String& a, const String& b) { return equalIgnoringNullity(a.impl(), b.impl()); } |
| 430 | template<size_t inlineCapacity> inline bool equalIgnoringNullity(const Vector<UChar, inlineCapacity>& a, const String& b) { return equalIgnoringNullity(a, b.impl()); } |
| 431 | |
| 432 | inline bool operator!(const String& string) { return string.isNull(); } |
| 433 | |
| 434 | inline void swap(String& a, String& b) { a.swap(b); } |
| 435 | |
| 436 | #ifdef __OBJC__ |
| 437 | |
| 438 | // Used in a small number of places where the long standing behavior has been "nil if empty". |
| 439 | NSString * nsStringNilIfEmpty(const String&); |
| 440 | |
| 441 | #endif |
| 442 | |
| 443 | WTF_EXPORT_PRIVATE int codePointCompare(const String&, const String&); |
| 444 | bool codePointCompareLessThan(const String&, const String&); |
| 445 | |
| 446 | template<typename CharacterType> void appendNumber(Vector<CharacterType>&, unsigned char number); |
| 447 | |
| 448 | // Shared global empty and null string. |
| 449 | WTF_EXPORT_PRIVATE const String& emptyString(); |
| 450 | WTF_EXPORT_PRIVATE const String& nullString(); |
| 451 | |
| 452 | template<typename> struct DefaultHash; |
| 453 | template<> struct DefaultHash<String> { using Hash = StringHash; }; |
| 454 | template<> struct VectorTraits<String> : VectorTraitsBase<false, void> { |
| 455 | static const bool canInitializeWithMemset = true; |
| 456 | static const bool canMoveWithMemcpy = true; |
| 457 | }; |
| 458 | |
| 459 | template<> struct IntegerToStringConversionTrait<String> { |
| 460 | using ReturnType = String; |
| 461 | using AdditionalArgumentType = void; |
| 462 | static String flush(LChar* characters, unsigned length, void*) { return { characters, length }; } |
| 463 | }; |
| 464 | |
| 465 | // Definitions of string operations |
| 466 | |
| 467 | inline String::String(StringImpl& string) |
| 468 | : m_impl(&string) |
| 469 | { |
| 470 | } |
| 471 | |
| 472 | inline String::String(StringImpl* string) |
| 473 | : m_impl(string) |
| 474 | { |
| 475 | } |
| 476 | |
| 477 | inline String::String(Ref<StringImpl>&& string) |
| 478 | : m_impl(WTFMove(string)) |
| 479 | { |
| 480 | } |
| 481 | |
| 482 | inline String::String(RefPtr<StringImpl>&& string) |
| 483 | : m_impl(WTFMove(string)) |
| 484 | { |
| 485 | } |
| 486 | |
| 487 | inline String::String(Ref<AtomicStringImpl>&& string) |
| 488 | : m_impl(WTFMove(string)) |
| 489 | { |
| 490 | } |
| 491 | |
| 492 | inline String::String(RefPtr<AtomicStringImpl>&& string) |
| 493 | : m_impl(WTFMove(string)) |
| 494 | { |
| 495 | } |
| 496 | |
| 497 | inline String::String(StaticStringImpl& string) |
| 498 | : m_impl(reinterpret_cast<StringImpl*>(&string)) |
| 499 | { |
| 500 | } |
| 501 | |
| 502 | inline String::String(StaticStringImpl* string) |
| 503 | : m_impl(reinterpret_cast<StringImpl*>(string)) |
| 504 | { |
| 505 | } |
| 506 | |
| 507 | template<size_t inlineCapacity, typename OverflowHandler> String::String(const Vector<UChar, inlineCapacity, OverflowHandler>& vector) |
| 508 | : m_impl(vector.size() ? StringImpl::create(vector.data(), vector.size()) : Ref<StringImpl> { *StringImpl::empty() }) |
| 509 | { |
| 510 | } |
| 511 | |
| 512 | template<> inline const LChar* String::characters<LChar>() const |
| 513 | { |
| 514 | return characters8(); |
| 515 | } |
| 516 | |
| 517 | template<> inline const UChar* String::characters<UChar>() const |
| 518 | { |
| 519 | return characters16(); |
| 520 | } |
| 521 | |
| 522 | inline UChar String::characterAt(unsigned index) const |
| 523 | { |
| 524 | if (!m_impl || index >= m_impl->length()) |
| 525 | return 0; |
| 526 | return (*m_impl)[index]; |
| 527 | } |
| 528 | |
| 529 | inline String& String::replace(UChar target, UChar replacement) |
| 530 | { |
| 531 | if (m_impl) |
| 532 | m_impl = m_impl->replace(target, replacement); |
| 533 | return *this; |
| 534 | } |
| 535 | |
| 536 | inline String& String::replace(UChar target, const String& replacement) |
| 537 | { |
| 538 | if (m_impl) |
| 539 | m_impl = m_impl->replace(target, replacement.impl()); |
| 540 | return *this; |
| 541 | } |
| 542 | |
| 543 | inline String& String::replace(const String& target, const String& replacement) |
| 544 | { |
| 545 | if (m_impl) |
| 546 | m_impl = m_impl->replace(target.impl(), replacement.impl()); |
| 547 | return *this; |
| 548 | } |
| 549 | |
| 550 | inline String& String::replace(unsigned start, unsigned length, const String& replacement) |
| 551 | { |
| 552 | if (m_impl) |
| 553 | m_impl = m_impl->replace(start, length, replacement.impl()); |
| 554 | return *this; |
| 555 | } |
| 556 | |
| 557 | template<unsigned characterCount> ALWAYS_INLINE String& String::replaceWithLiteral(UChar target, const char (&characters)[characterCount]) |
| 558 | { |
| 559 | if (m_impl) |
| 560 | m_impl = m_impl->replace(target, characters, characterCount - 1); |
| 561 | return *this; |
| 562 | } |
| 563 | |
| 564 | template<size_t inlineCapacity> inline String String::make8BitFrom16BitSource(const Vector<UChar, inlineCapacity>& buffer) |
| 565 | { |
| 566 | return make8BitFrom16BitSource(buffer.data(), buffer.size()); |
| 567 | } |
| 568 | |
| 569 | inline UCharDirection String::defaultWritingDirection(bool* hasStrongDirectionality) const |
| 570 | { |
| 571 | if (m_impl) |
| 572 | return m_impl->defaultWritingDirection(hasStrongDirectionality); |
| 573 | if (hasStrongDirectionality) |
| 574 | *hasStrongDirectionality = false; |
| 575 | return U_LEFT_TO_RIGHT; |
| 576 | } |
| 577 | |
| 578 | inline void String::clearImplIfNotShared() |
| 579 | { |
| 580 | if (m_impl && m_impl->hasOneRef()) |
| 581 | m_impl = nullptr; |
| 582 | } |
| 583 | |
| 584 | #ifdef __OBJC__ |
| 585 | |
| 586 | inline String::operator NSString *() const |
| 587 | { |
| 588 | if (!m_impl) |
| 589 | return @"" ; |
| 590 | return *m_impl; |
| 591 | } |
| 592 | |
| 593 | inline NSString * nsStringNilIfEmpty(const String& string) |
| 594 | { |
| 595 | if (string.isEmpty()) |
| 596 | return nil; |
| 597 | return *string.impl(); |
| 598 | } |
| 599 | |
| 600 | #endif |
| 601 | |
| 602 | inline bool codePointCompareLessThan(const String& a, const String& b) |
| 603 | { |
| 604 | return codePointCompare(a.impl(), b.impl()) < 0; |
| 605 | } |
| 606 | |
| 607 | template<typename CharacterType> |
| 608 | inline void appendNumber(Vector<CharacterType>& vector, unsigned char number) |
| 609 | { |
| 610 | int numberLength = number > 99 ? 3 : (number > 9 ? 2 : 1); |
| 611 | size_t vectorSize = vector.size(); |
| 612 | vector.grow(vectorSize + numberLength); |
| 613 | |
| 614 | switch (numberLength) { |
| 615 | case 3: |
| 616 | vector[vectorSize + 2] = number % 10 + '0'; |
| 617 | number /= 10; |
| 618 | FALLTHROUGH; |
| 619 | |
| 620 | case 2: |
| 621 | vector[vectorSize + 1] = number % 10 + '0'; |
| 622 | number /= 10; |
| 623 | FALLTHROUGH; |
| 624 | |
| 625 | case 1: |
| 626 | vector[vectorSize] = number % 10 + '0'; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | inline String String::fromUTF8(const Vector<LChar>& characters) |
| 631 | { |
| 632 | if (characters.isEmpty()) |
| 633 | return emptyString(); |
| 634 | return fromUTF8(characters.data(), characters.size()); |
| 635 | } |
| 636 | |
| 637 | template<unsigned length> inline bool equalLettersIgnoringASCIICase(const String& string, const char (&lowercaseLetters)[length]) |
| 638 | { |
| 639 | return equalLettersIgnoringASCIICase(string.impl(), lowercaseLetters); |
| 640 | } |
| 641 | |
| 642 | inline bool equalIgnoringASCIICase(const String& a, const String& b) |
| 643 | { |
| 644 | return equalIgnoringASCIICase(a.impl(), b.impl()); |
| 645 | } |
| 646 | |
| 647 | inline bool equalIgnoringASCIICase(const String& a, const char* b) |
| 648 | { |
| 649 | return equalIgnoringASCIICase(a.impl(), b); |
| 650 | } |
| 651 | |
| 652 | template<unsigned length> inline bool startsWithLettersIgnoringASCIICase(const String& string, const char (&lowercaseLetters)[length]) |
| 653 | { |
| 654 | return startsWithLettersIgnoringASCIICase(string.impl(), lowercaseLetters); |
| 655 | } |
| 656 | |
| 657 | inline String String::numberToStringECMAScript(double number) |
| 658 | { |
| 659 | return numberToStringShortest(number); |
| 660 | } |
| 661 | |
| 662 | inline namespace StringLiterals { |
| 663 | |
| 664 | inline String operator"" _str(const char* characters, size_t) |
| 665 | { |
| 666 | return ASCIILiteral::fromLiteralUnsafe(characters); |
| 667 | } |
| 668 | |
| 669 | } // inline StringLiterals |
| 670 | |
| 671 | } // namespace WTF |
| 672 | |
| 673 | using WTF::KeepTrailingZeros; |
| 674 | using WTF::String; |
| 675 | using WTF::appendNumber; |
| 676 | using WTF::charactersToDouble; |
| 677 | using WTF::charactersToFloat; |
| 678 | using WTF::charactersToInt64; |
| 679 | using WTF::charactersToInt64Strict; |
| 680 | using WTF::charactersToInt; |
| 681 | using WTF::charactersToIntPtr; |
| 682 | using WTF::charactersToIntPtrStrict; |
| 683 | using WTF::charactersToIntStrict; |
| 684 | using WTF::charactersToUInt64; |
| 685 | using WTF::charactersToUInt64Strict; |
| 686 | using WTF::charactersToUInt; |
| 687 | using WTF::charactersToUIntStrict; |
| 688 | using WTF::emptyString; |
| 689 | using WTF::nullString; |
| 690 | using WTF::equal; |
| 691 | using WTF::find; |
| 692 | using WTF::isAllSpecialCharacters; |
| 693 | using WTF::isSpaceOrNewline; |
| 694 | using WTF::reverseFind; |
| 695 | |
| 696 | #include <wtf/text/AtomicString.h> |
| 697 | |