2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2005-2017 Apple Inc. All rights reserved.
4 * Copyright (C) 2009 Google Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
27 #include <unicode/ustring.h>
28 #include <wtf/ASCIICType.h>
29 #include <wtf/CheckedArithmetic.h>
30 #include <wtf/Forward.h>
31 #include <wtf/Hasher.h>
32 #include <wtf/MathExtras.h>
33 #include <wtf/StdLibExtras.h>
34 #include <wtf/Vector.h>
35 #include <wtf/text/ASCIIFastPath.h>
36 #include <wtf/text/ConversionMode.h>
37 #include <wtf/text/StringCommon.h>
38 #include <wtf/text/StringMalloc.h>
39 #include <wtf/text/StringVector.h>
42 typedef const struct __CFString * CFStringRef;
50 namespace LLInt { class Data; }
51 class LLIntOffsetsExtractor;
59 struct CStringTranslator;
60 struct HashAndUTF8CharactersTranslator;
61 struct LCharBufferTranslator;
63 struct SubstringTranslator;
64 struct UCharBufferTranslator;
66 template<typename> class RetainPtr;
68 template<typename> struct BufferFromStaticDataTranslator;
69 template<typename> struct HashAndCharactersTranslator;
71 // Define STRING_STATS to 1 turn on runtime statistics of string sizes and memory usage.
72 #define STRING_STATS 0
74 template<bool isSpecialCharacter(UChar), typename CharacterType> bool isAllSpecialCharacters(const CharacterType*, size_t length);
79 void add8BitString(unsigned length, bool isSubString = false)
81 ++m_totalNumberStrings;
82 ++m_number8BitStrings;
84 m_total8BitData += length;
87 void add16BitString(unsigned length, bool isSubString = false)
89 ++m_totalNumberStrings;
90 ++m_number16BitStrings;
92 m_total16BitData += length;
95 void removeString(StringImpl&);
98 static const unsigned s_printStringStatsFrequency = 5000;
99 static std::atomic<unsigned> s_stringRemovesTillPrintStats;
101 std::atomic<unsigned> m_refCalls;
102 std::atomic<unsigned> m_derefCalls;
104 std::atomic<unsigned> m_totalNumberStrings;
105 std::atomic<unsigned> m_number8BitStrings;
106 std::atomic<unsigned> m_number16BitStrings;
107 std::atomic<unsigned long long> m_total8BitData;
108 std::atomic<unsigned long long> m_total16BitData;
111 #define STRING_STATS_ADD_8BIT_STRING(length) StringImpl::stringStats().add8BitString(length)
112 #define STRING_STATS_ADD_8BIT_STRING2(length, isSubString) StringImpl::stringStats().add8BitString(length, isSubString)
113 #define STRING_STATS_ADD_16BIT_STRING(length) StringImpl::stringStats().add16BitString(length)
114 #define STRING_STATS_ADD_16BIT_STRING2(length, isSubString) StringImpl::stringStats().add16BitString(length, isSubString)
115 #define STRING_STATS_REMOVE_STRING(string) StringImpl::stringStats().removeString(string)
116 #define STRING_STATS_REF_STRING(string) ++StringImpl::stringStats().m_refCalls;
117 #define STRING_STATS_DEREF_STRING(string) ++StringImpl::stringStats().m_derefCalls;
121 #define STRING_STATS_ADD_8BIT_STRING(length) ((void)0)
122 #define STRING_STATS_ADD_8BIT_STRING2(length, isSubString) ((void)0)
123 #define STRING_STATS_ADD_16BIT_STRING(length) ((void)0)
124 #define STRING_STATS_ADD_16BIT_STRING2(length, isSubString) ((void)0)
125 #define STRING_STATS_ADD_UPCONVERTED_STRING(length) ((void)0)
126 #define STRING_STATS_REMOVE_STRING(string) ((void)0)
127 #define STRING_STATS_REF_STRING(string) ((void)0)
128 #define STRING_STATS_DEREF_STRING(string) ((void)0)
132 class StringImplShape {
133 WTF_MAKE_NONCOPYABLE(StringImplShape);
135 StringImplShape(unsigned refCount, unsigned length, const LChar*, unsigned hashAndFlags);
136 StringImplShape(unsigned refCount, unsigned length, const UChar*, unsigned hashAndFlags);
138 enum ConstructWithConstExprTag { ConstructWithConstExpr };
139 template<unsigned characterCount> constexpr StringImplShape(unsigned refCount, unsigned length, const char (&characters)[characterCount], unsigned hashAndFlags, ConstructWithConstExprTag);
140 template<unsigned characterCount> constexpr StringImplShape(unsigned refCount, unsigned length, const char16_t (&characters)[characterCount], unsigned hashAndFlags, ConstructWithConstExprTag);
145 const LChar* m_data8;
146 const UChar* m_data16;
147 // It seems that reinterpret_cast prevents constexpr's compile time initialization in VC++.
148 // These are needed to avoid reinterpret_cast.
149 const char* m_data8Char;
150 const char16_t* m_data16Char;
152 mutable unsigned m_hashAndFlags;
155 // FIXME: Use of StringImpl and const is rather confused.
156 // The actual string inside a StringImpl is immutable, so you can't modify a string using a StringImpl&.
157 // We could mark every member function const and always use "const StringImpl&" and "const StringImpl*".
158 // Or we could say that "const" doesn't make sense at all and use "StringImpl&" and "StringImpl*" everywhere.
159 // Right now we use a mix of both, which makes code more confusing and has no benefit.
161 class StringImpl : private StringImplShape {
162 WTF_MAKE_NONCOPYABLE(StringImpl); WTF_MAKE_STRING_ALLOCATED;
164 friend class AtomicStringImpl;
165 friend class JSC::LLInt::Data;
166 friend class JSC::LLIntOffsetsExtractor;
167 friend class PrivateSymbolImpl;
168 friend class RegisteredSymbolImpl;
169 friend class SymbolImpl;
171 friend struct WTF::CStringTranslator;
172 friend struct WTF::HashAndUTF8CharactersTranslator;
173 friend struct WTF::LCharBufferTranslator;
174 friend struct WTF::SubstringTranslator;
175 friend struct WTF::UCharBufferTranslator;
177 template<typename> friend struct WTF::BufferFromStaticDataTranslator;
178 template<typename> friend struct WTF::HashAndCharactersTranslator;
181 enum BufferOwnership { BufferInternal, BufferOwned, BufferSubstring };
183 // The bottom 6 bits in the hash are flags.
184 static constexpr const unsigned s_flagCount = 6;
186 static constexpr const unsigned s_flagMask = (1u << s_flagCount) - 1;
187 static_assert(s_flagCount <= StringHasher::flagCount, "StringHasher reserves enough bits for StringImpl flags");
188 static constexpr const unsigned s_flagStringKindCount = 4;
190 static constexpr const unsigned s_hashFlagStringKindIsAtomic = 1u << (s_flagStringKindCount);
191 static constexpr const unsigned s_hashFlagStringKindIsSymbol = 1u << (s_flagStringKindCount + 1);
192 static constexpr const unsigned s_hashMaskStringKind = s_hashFlagStringKindIsAtomic | s_hashFlagStringKindIsSymbol;
193 static constexpr const unsigned s_hashFlag8BitBuffer = 1u << 3;
194 static constexpr const unsigned s_hashFlagDidReportCost = 1u << 2;
195 static constexpr const unsigned s_hashMaskBufferOwnership = (1u << 0) | (1u << 1);
198 StringNormal = 0u, // non-symbol, non-atomic
199 StringAtomic = s_hashFlagStringKindIsAtomic, // non-symbol, atomic
200 StringSymbol = s_hashFlagStringKindIsSymbol, // symbol, non-atomic
203 // Create a normal 8-bit string with internal storage (BufferInternal).
204 enum Force8Bit { Force8BitConstructor };
205 StringImpl(unsigned length, Force8Bit);
207 // Create a normal 16-bit string with internal storage (BufferInternal).
208 explicit StringImpl(unsigned length);
210 // Create a StringImpl adopting ownership of the provided buffer (BufferOwned).
211 StringImpl(MallocPtr<LChar>, unsigned length);
212 StringImpl(MallocPtr<UChar>, unsigned length);
213 enum ConstructWithoutCopyingTag { ConstructWithoutCopying };
214 StringImpl(const UChar*, unsigned length, ConstructWithoutCopyingTag);
215 StringImpl(const LChar*, unsigned length, ConstructWithoutCopyingTag);
217 // Used to create new strings that are a substring of an existing StringImpl (BufferSubstring).
218 StringImpl(const LChar*, unsigned length, Ref<StringImpl>&&);
219 StringImpl(const UChar*, unsigned length, Ref<StringImpl>&&);
222 WTF_EXPORT_STRING_API static void destroy(StringImpl*);
224 WTF_EXPORT_STRING_API static Ref<StringImpl> create(const UChar*, unsigned length);
225 WTF_EXPORT_STRING_API static Ref<StringImpl> create(const LChar*, unsigned length);
226 WTF_EXPORT_STRING_API static Ref<StringImpl> create8BitIfPossible(const UChar*, unsigned length);
227 template<size_t inlineCapacity> static Ref<StringImpl> create8BitIfPossible(const Vector<UChar, inlineCapacity>&);
228 WTF_EXPORT_STRING_API static Ref<StringImpl> create8BitIfPossible(const UChar*);
230 ALWAYS_INLINE static Ref<StringImpl> create(const char* characters, unsigned length) { return create(reinterpret_cast<const LChar*>(characters), length); }
231 WTF_EXPORT_STRING_API static Ref<StringImpl> create(const LChar*);
232 ALWAYS_INLINE static Ref<StringImpl> create(const char* string) { return create(reinterpret_cast<const LChar*>(string)); }
234 static Ref<StringImpl> createSubstringSharingImpl(StringImpl&, unsigned offset, unsigned length);
236 template<unsigned characterCount> static Ref<StringImpl> createFromLiteral(const char (&)[characterCount]);
238 // FIXME: Replace calls to these overloads of createFromLiteral to createWithoutCopying instead.
239 WTF_EXPORT_STRING_API static Ref<StringImpl> createFromLiteral(const char*, unsigned length);
240 WTF_EXPORT_STRING_API static Ref<StringImpl> createFromLiteral(const char*);
242 WTF_EXPORT_STRING_API static Ref<StringImpl> createWithoutCopying(const UChar*, unsigned length);
243 WTF_EXPORT_STRING_API static Ref<StringImpl> createWithoutCopying(const LChar*, unsigned length);
244 WTF_EXPORT_STRING_API static Ref<StringImpl> createUninitialized(unsigned length, LChar*&);
245 WTF_EXPORT_STRING_API static Ref<StringImpl> createUninitialized(unsigned length, UChar*&);
246 template<typename CharacterType> static RefPtr<StringImpl> tryCreateUninitialized(unsigned length, CharacterType*&);
248 // Reallocate the StringImpl. The originalString must be only owned by the Ref,
249 // and the buffer ownership must be BufferInternal. Just like the input pointer of realloc(),
250 // the originalString can't be used after this function.
251 static Ref<StringImpl> reallocate(Ref<StringImpl>&& originalString, unsigned length, LChar*& data);
252 static Ref<StringImpl> reallocate(Ref<StringImpl>&& originalString, unsigned length, UChar*& data);
254 static unsigned flagsOffset() { return OBJECT_OFFSETOF(StringImpl, m_hashAndFlags); }
255 static unsigned flagIs8Bit() { return s_hashFlag8BitBuffer; }
256 static unsigned flagIsAtomic() { return s_hashFlagStringKindIsAtomic; }
257 static unsigned flagIsSymbol() { return s_hashFlagStringKindIsSymbol; }
258 static unsigned maskStringKind() { return s_hashMaskStringKind; }
259 static unsigned dataOffset() { return OBJECT_OFFSETOF(StringImpl, m_data8); }
261 template<typename CharacterType, size_t inlineCapacity, typename OverflowHandler, size_t minCapacity>
262 static Ref<StringImpl> adopt(StringVector<CharacterType, inlineCapacity, OverflowHandler, minCapacity>&&);
264 WTF_EXPORT_STRING_API static Ref<StringImpl> adopt(StringBuffer<UChar>&&);
265 WTF_EXPORT_STRING_API static Ref<StringImpl> adopt(StringBuffer<LChar>&&);
267 unsigned length() const { return m_length; }
268 static ptrdiff_t lengthMemoryOffset() { return OBJECT_OFFSETOF(StringImpl, m_length); }
269 bool isEmpty() const { return !m_length; }
271 bool is8Bit() const { return m_hashAndFlags & s_hashFlag8BitBuffer; }
272 ALWAYS_INLINE const LChar* characters8() const { ASSERT(is8Bit()); return m_data8; }
273 ALWAYS_INLINE const UChar* characters16() const { ASSERT(!is8Bit()); return m_data16; }
275 template<typename CharacterType> const CharacterType* characters() const;
278 size_t costDuringGC();
280 WTF_EXPORT_STRING_API size_t sizeInBytes() const;
282 bool isSymbol() const { return m_hashAndFlags & s_hashFlagStringKindIsSymbol; }
283 bool isAtomic() const { return m_hashAndFlags & s_hashFlagStringKindIsAtomic; }
284 void setIsAtomic(bool);
287 bool isSubString() const { return bufferOwnership() == BufferSubstring; }
290 static WTF_EXPORT_STRING_API CString utf8ForCharacters(const LChar* characters, unsigned length);
291 static WTF_EXPORT_STRING_API CString utf8ForCharacters(const UChar* characters, unsigned length, ConversionMode = LenientConversion);
292 WTF_EXPORT_STRING_API CString utf8ForRange(unsigned offset, unsigned length, ConversionMode = LenientConversion) const;
293 WTF_EXPORT_STRING_API CString utf8(ConversionMode = LenientConversion) const;
296 static WTF_EXPORT_STRING_API bool utf8Impl(const UChar* characters, unsigned length, char*& buffer, size_t bufferSize, ConversionMode);
298 // The high bits of 'hash' are always empty, but we prefer to store our flags
299 // in the low bits because it makes them slightly more efficient to access.
300 // So, we shift left and right when setting and getting our hash code.
301 void setHash(unsigned) const;
303 unsigned rawHash() const { return m_hashAndFlags >> s_flagCount; }
306 bool hasHash() const { return !!rawHash(); }
308 unsigned existingHash() const { ASSERT(hasHash()); return rawHash(); }
309 unsigned hash() const { return hasHash() ? rawHash() : hashSlowCase(); }
311 WTF_EXPORT_PRIVATE unsigned concurrentHash() const;
313 unsigned symbolAwareHash() const;
314 unsigned existingSymbolAwareHash() const;
316 bool isStatic() const { return m_refCount & s_refCountFlagIsStaticString; }
318 size_t refCount() const { return m_refCount / s_refCountIncrement; }
319 bool hasOneRef() const { return m_refCount == s_refCountIncrement; }
320 bool hasAtLeastOneRef() const { return m_refCount; } // For assertions.
325 class StaticStringImpl : private StringImplShape {
326 WTF_MAKE_NONCOPYABLE(StaticStringImpl);
328 // Used to construct static strings, which have an special refCount that can never hit zero.
329 // This means that the static string will never be destroyed, which is important because
330 // static strings will be shared across threads & ref-counted in a non-threadsafe manner.
332 // In order to make StaticStringImpl thread safe, we also need to ensure that the rest of
333 // the fields are never mutated by threads. We have this guarantee because:
335 // 1. m_length is only set on construction and never mutated thereafter.
337 // 2. m_data8 and m_data16 are only set on construction and never mutated thereafter.
338 // We also know that a StringImpl never changes from 8 bit to 16 bit because there
339 // is no way to set/clear the s_hashFlag8BitBuffer flag other than at construction.
341 // 3. m_hashAndFlags will not be mutated by different threads because:
343 // a. StaticStringImpl's constructor sets the s_hashFlagDidReportCost flag to ensure
344 // that StringImpl::cost() returns early.
345 // This means StaticStringImpl costs are not counted. But since there should only
346 // be a finite set of StaticStringImpls, their cost can be aggregated into a single
347 // system cost if needed.
348 // b. setIsAtomic() is never called on a StaticStringImpl.
349 // setIsAtomic() asserts !isStatic().
350 // c. setHash() is never called on a StaticStringImpl.
351 // StaticStringImpl's constructor sets the hash on construction.
352 // StringImpl::hash() only sets a new hash iff !hasHash().
353 // Additionally, StringImpl::setHash() asserts hasHash() and !isStatic().
355 template<unsigned characterCount> constexpr StaticStringImpl(const char (&characters)[characterCount], StringKind = StringNormal);
356 template<unsigned characterCount> constexpr StaticStringImpl(const char16_t (&characters)[characterCount], StringKind = StringNormal);
357 operator StringImpl&();
360 WTF_EXPORTDATA static StaticStringImpl s_atomicEmptyString;
361 ALWAYS_INLINE static StringImpl* empty() { return reinterpret_cast<StringImpl*>(&s_atomicEmptyString); }
363 // FIXME: Does this really belong in StringImpl?
364 template<typename CharacterType> static void copyCharacters(CharacterType* destination, const CharacterType* source, unsigned numCharacters);
365 static void copyCharacters(UChar* destination, const LChar* source, unsigned numCharacters);
367 // Some string features, like reference counting and the atomicity flag, are not
368 // thread-safe. We achieve thread safety by isolation, giving each thread
369 // its own copy of the string.
370 Ref<StringImpl> isolatedCopy() const;
372 WTF_EXPORT_STRING_API Ref<StringImpl> substring(unsigned position, unsigned length = std::numeric_limits<unsigned>::max());
374 UChar at(unsigned) const;
375 UChar operator[](unsigned i) const { return at(i); }
376 WTF_EXPORT_STRING_API UChar32 characterStartingAt(unsigned);
378 int toIntStrict(bool* ok = 0, int base = 10);
379 unsigned toUIntStrict(bool* ok = 0, int base = 10);
380 int64_t toInt64Strict(bool* ok = 0, int base = 10);
381 uint64_t toUInt64Strict(bool* ok = 0, int base = 10);
382 intptr_t toIntPtrStrict(bool* ok = 0, int base = 10);
384 WTF_EXPORT_STRING_API int toInt(bool* ok = 0); // ignores trailing garbage
385 unsigned toUInt(bool* ok = 0); // ignores trailing garbage
386 int64_t toInt64(bool* ok = 0); // ignores trailing garbage
387 uint64_t toUInt64(bool* ok = 0); // ignores trailing garbage
388 intptr_t toIntPtr(bool* ok = 0); // ignores trailing garbage
390 // FIXME: Like the strict functions above, these give false for "ok" when there is trailing garbage.
391 // Like the non-strict functions above, these return the value when there is trailing garbage.
392 // It would be better if these were more consistent with the above functions instead.
393 double toDouble(bool* ok = 0);
394 float toFloat(bool* ok = 0);
396 WTF_EXPORT_STRING_API Ref<StringImpl> convertToASCIILowercase();
397 WTF_EXPORT_STRING_API Ref<StringImpl> convertToASCIIUppercase();
398 WTF_EXPORT_STRING_API Ref<StringImpl> convertToLowercaseWithoutLocale();
399 WTF_EXPORT_STRING_API Ref<StringImpl> convertToLowercaseWithoutLocaleStartingAtFailingIndex8Bit(unsigned);
400 WTF_EXPORT_STRING_API Ref<StringImpl> convertToUppercaseWithoutLocale();
401 WTF_EXPORT_STRING_API Ref<StringImpl> convertToLowercaseWithLocale(const AtomicString& localeIdentifier);
402 WTF_EXPORT_STRING_API Ref<StringImpl> convertToUppercaseWithLocale(const AtomicString& localeIdentifier);
404 Ref<StringImpl> foldCase();
406 Ref<StringImpl> stripWhiteSpace();
407 WTF_EXPORT_STRING_API Ref<StringImpl> simplifyWhiteSpace();
408 Ref<StringImpl> simplifyWhiteSpace(CodeUnitMatchFunction);
410 Ref<StringImpl> stripLeadingAndTrailingCharacters(CodeUnitMatchFunction);
411 Ref<StringImpl> removeCharacters(CodeUnitMatchFunction);
413 bool isAllASCII() const;
414 bool isAllLatin1() const;
415 template<bool isSpecialCharacter(UChar)> bool isAllSpecialCharacters() const;
417 size_t find(LChar character, unsigned start = 0);
418 size_t find(char character, unsigned start = 0);
419 size_t find(UChar character, unsigned start = 0);
420 WTF_EXPORT_STRING_API size_t find(CodeUnitMatchFunction, unsigned index = 0);
421 size_t find(const LChar*, unsigned index = 0);
422 ALWAYS_INLINE size_t find(const char* string, unsigned index = 0) { return find(reinterpret_cast<const LChar*>(string), index); }
423 WTF_EXPORT_STRING_API size_t find(StringImpl*);
424 WTF_EXPORT_STRING_API size_t find(StringImpl*, unsigned index);
425 WTF_EXPORT_STRING_API size_t findIgnoringASCIICase(const StringImpl&) const;
426 WTF_EXPORT_STRING_API size_t findIgnoringASCIICase(const StringImpl&, unsigned startOffset) const;
427 WTF_EXPORT_STRING_API size_t findIgnoringASCIICase(const StringImpl*) const;
428 WTF_EXPORT_STRING_API size_t findIgnoringASCIICase(const StringImpl*, unsigned startOffset) const;
430 WTF_EXPORT_STRING_API size_t reverseFind(UChar, unsigned index = std::numeric_limits<unsigned>::max());
431 WTF_EXPORT_STRING_API size_t reverseFind(StringImpl*, unsigned index = std::numeric_limits<unsigned>::max());
433 WTF_EXPORT_STRING_API bool startsWith(const StringImpl*) const;
434 WTF_EXPORT_STRING_API bool startsWith(const StringImpl&) const;
435 WTF_EXPORT_STRING_API bool startsWithIgnoringASCIICase(const StringImpl*) const;
436 WTF_EXPORT_STRING_API bool startsWithIgnoringASCIICase(const StringImpl&) const;
437 WTF_EXPORT_STRING_API bool startsWith(UChar) const;
438 WTF_EXPORT_STRING_API bool startsWith(const char*, unsigned matchLength) const;
439 template<unsigned matchLength> bool startsWith(const char (&prefix)[matchLength]) const { return startsWith(prefix, matchLength - 1); }
440 WTF_EXPORT_STRING_API bool hasInfixStartingAt(const StringImpl&, unsigned startOffset) const;
442 WTF_EXPORT_STRING_API bool endsWith(StringImpl*);
443 WTF_EXPORT_STRING_API bool endsWith(StringImpl&);
444 WTF_EXPORT_STRING_API bool endsWithIgnoringASCIICase(const StringImpl*) const;
445 WTF_EXPORT_STRING_API bool endsWithIgnoringASCIICase(const StringImpl&) const;
446 WTF_EXPORT_STRING_API bool endsWith(UChar) const;
447 WTF_EXPORT_STRING_API bool endsWith(const char*, unsigned matchLength) const;
448 template<unsigned matchLength> bool endsWith(const char (&prefix)[matchLength]) const { return endsWith(prefix, matchLength - 1); }
449 WTF_EXPORT_STRING_API bool hasInfixEndingAt(const StringImpl&, unsigned endOffset) const;
451 WTF_EXPORT_STRING_API Ref<StringImpl> replace(UChar, UChar);
452 WTF_EXPORT_STRING_API Ref<StringImpl> replace(UChar, StringImpl*);
453 ALWAYS_INLINE Ref<StringImpl> replace(UChar pattern, const char* replacement, unsigned replacementLength) { return replace(pattern, reinterpret_cast<const LChar*>(replacement), replacementLength); }
454 WTF_EXPORT_STRING_API Ref<StringImpl> replace(UChar, const LChar*, unsigned replacementLength);
455 Ref<StringImpl> replace(UChar, const UChar*, unsigned replacementLength);
456 WTF_EXPORT_STRING_API Ref<StringImpl> replace(StringImpl*, StringImpl*);
457 WTF_EXPORT_STRING_API Ref<StringImpl> replace(unsigned index, unsigned length, StringImpl*);
459 WTF_EXPORT_STRING_API UCharDirection defaultWritingDirection(bool* hasStrongDirectionality = nullptr);
462 RetainPtr<CFStringRef> createCFString();
466 WTF_EXPORT_STRING_API operator NSString *();
470 ALWAYS_INLINE static StringStats& stringStats() { return m_stringStats; }
473 BufferOwnership bufferOwnership() const { return static_cast<BufferOwnership>(m_hashAndFlags & s_hashMaskBufferOwnership); }
475 void assertCaged() const;
476 WTF_EXPORT_PRIVATE void releaseAssertCaged() const;
481 // Used to create new symbol string that holds an existing [[Description]] string as a substring buffer (BufferSubstring).
482 enum CreateSymbolTag { CreateSymbol };
483 StringImpl(CreateSymbolTag, const LChar*, unsigned length);
484 StringImpl(CreateSymbolTag, const UChar*, unsigned length);
487 explicit StringImpl(CreateSymbolTag);
490 template<typename> static size_t allocationSize(Checked<size_t> tailElementCount);
491 template<typename> static size_t tailOffset();
493 bool requiresCopy() const;
494 template<typename T> const T* tailPointer() const;
495 template<typename T> T* tailPointer();
496 StringImpl* const& substringBuffer() const;
497 StringImpl*& substringBuffer();
499 enum class CaseConvertType { Upper, Lower };
500 template<CaseConvertType, typename CharacterType> static Ref<StringImpl> convertASCIICase(StringImpl&, const CharacterType*, unsigned);
502 template<class CodeUnitPredicate> Ref<StringImpl> stripMatchedCharacters(CodeUnitPredicate);
503 template<typename CharacterType> ALWAYS_INLINE Ref<StringImpl> removeCharacters(const CharacterType* characters, CodeUnitMatchFunction);
504 template<typename CharacterType, class CodeUnitPredicate> Ref<StringImpl> simplifyMatchedCharactersToSpace(CodeUnitPredicate);
505 template<typename CharacterType> static Ref<StringImpl> constructInternal(StringImpl&, unsigned);
506 template<typename CharacterType> static Ref<StringImpl> createUninitializedInternal(unsigned, CharacterType*&);
507 template<typename CharacterType> static Ref<StringImpl> createUninitializedInternalNonEmpty(unsigned, CharacterType*&);
508 template<typename CharacterType> static Ref<StringImpl> reallocateInternal(Ref<StringImpl>&&, unsigned, CharacterType*&);
509 template<typename CharacterType> static Ref<StringImpl> createInternal(const CharacterType*, unsigned);
510 WTF_EXPORT_PRIVATE NEVER_INLINE unsigned hashSlowCase() const;
512 // The bottom bit in the ref count indicates a static (immortal) string.
513 static const unsigned s_refCountFlagIsStaticString = 0x1;
514 static const unsigned s_refCountIncrement = 0x2; // This allows us to ref / deref without disturbing the static string flag.
517 WTF_EXPORTDATA static StringStats m_stringStats;
521 void assertHashIsCorrect() const;
524 using StaticStringImpl = StringImpl::StaticStringImpl;
526 static_assert(sizeof(StringImpl) == sizeof(StaticStringImpl), "");
530 // StringImpls created from StaticStringImpl will ASSERT in the generic ValueCheck<T>::checkConsistency
531 // as they are not allocated by stringMalloc. We don't currently have any way to detect that case
532 // so we ignore the consistency check for all StringImpl*.
533 template<> struct ValueCheck<StringImpl*> {
534 static void checkConsistency(const StringImpl*) { }
539 WTF_EXPORT_STRING_API bool equal(const StringImpl*, const StringImpl*);
540 WTF_EXPORT_STRING_API bool equal(const StringImpl*, const LChar*);
541 inline bool equal(const StringImpl* a, const char* b) { return equal(a, reinterpret_cast<const LChar*>(b)); }
542 WTF_EXPORT_STRING_API bool equal(const StringImpl*, const LChar*, unsigned);
543 WTF_EXPORT_STRING_API bool equal(const StringImpl*, const UChar*, unsigned);
544 inline bool equal(const StringImpl* a, const char* b, unsigned length) { return equal(a, reinterpret_cast<const LChar*>(b), length); }
545 inline bool equal(const LChar* a, StringImpl* b) { return equal(b, a); }
546 inline bool equal(const char* a, StringImpl* b) { return equal(b, reinterpret_cast<const LChar*>(a)); }
547 WTF_EXPORT_STRING_API bool equal(const StringImpl& a, const StringImpl& b);
549 WTF_EXPORT_STRING_API bool equalIgnoringNullity(StringImpl*, StringImpl*);
550 WTF_EXPORT_STRING_API bool equalIgnoringNullity(const UChar*, size_t length, StringImpl*);
552 bool equalIgnoringASCIICase(const StringImpl&, const StringImpl&);
553 WTF_EXPORT_STRING_API bool equalIgnoringASCIICase(const StringImpl*, const StringImpl*);
554 bool equalIgnoringASCIICase(const StringImpl&, const char*);
555 bool equalIgnoringASCIICase(const StringImpl*, const char*);
557 WTF_EXPORT_STRING_API bool equalIgnoringASCIICaseNonNull(const StringImpl*, const StringImpl*);
559 template<unsigned length> bool equalLettersIgnoringASCIICase(const StringImpl&, const char (&lowercaseLetters)[length]);
560 template<unsigned length> bool equalLettersIgnoringASCIICase(const StringImpl*, const char (&lowercaseLetters)[length]);
562 size_t find(const LChar*, unsigned length, CodeUnitMatchFunction, unsigned index = 0);
563 size_t find(const UChar*, unsigned length, CodeUnitMatchFunction, unsigned index = 0);
565 template<typename CharacterType> size_t reverseFindLineTerminator(const CharacterType*, unsigned length, unsigned index = std::numeric_limits<unsigned>::max());
566 template<typename CharacterType> size_t reverseFind(const CharacterType*, unsigned length, CharacterType matchCharacter, unsigned index = std::numeric_limits<unsigned>::max());
567 size_t reverseFind(const UChar*, unsigned length, LChar matchCharacter, unsigned index = std::numeric_limits<unsigned>::max());
568 size_t reverseFind(const LChar*, unsigned length, UChar matchCharacter, unsigned index = std::numeric_limits<unsigned>::max());
570 template<size_t inlineCapacity> bool equalIgnoringNullity(const Vector<UChar, inlineCapacity>&, StringImpl*);
572 template<typename CharacterType1, typename CharacterType2> int codePointCompare(const CharacterType1*, unsigned length1, const CharacterType2*, unsigned length2);
573 int codePointCompare(const StringImpl*, const StringImpl*);
575 // FIXME: Should rename this to make clear it uses the Unicode definition of whitespace.
576 // Most WebKit callers don't want that would use isASCIISpace or isHTMLSpace instead.
577 bool isSpaceOrNewline(UChar);
579 template<typename CharacterType> unsigned lengthOfNullTerminatedString(const CharacterType*);
581 // StringHash is the default hash for StringImpl* and RefPtr<StringImpl>
582 template<typename T> struct DefaultHash;
583 template<> struct DefaultHash<StringImpl*> {
584 typedef StringHash Hash;
586 template<> struct DefaultHash<RefPtr<StringImpl>> {
587 typedef StringHash Hash;
590 #define MAKE_STATIC_STRING_IMPL(characters) ([] { \
591 static StaticStringImpl impl(characters); \
595 template<> ALWAYS_INLINE Ref<StringImpl> StringImpl::constructInternal<LChar>(StringImpl& string, unsigned length)
597 return adoptRef(*new (NotNull, &string) StringImpl { length, Force8BitConstructor });
600 template<> ALWAYS_INLINE Ref<StringImpl> StringImpl::constructInternal<UChar>(StringImpl& string, unsigned length)
602 return adoptRef(*new (NotNull, &string) StringImpl { length });
605 template<> ALWAYS_INLINE const LChar* StringImpl::characters<LChar>() const
607 return characters8();
610 template<> ALWAYS_INLINE const UChar* StringImpl::characters<UChar>() const
612 return characters16();
615 inline size_t find(const LChar* characters, unsigned length, CodeUnitMatchFunction matchFunction, unsigned index)
617 while (index < length) {
618 if (matchFunction(characters[index]))
625 inline size_t find(const UChar* characters, unsigned length, CodeUnitMatchFunction matchFunction, unsigned index)
627 while (index < length) {
628 if (matchFunction(characters[index]))
635 template<typename CharacterType> inline size_t reverseFindLineTerminator(const CharacterType* characters, unsigned length, unsigned index)
641 auto character = characters[index];
642 while (character != '\n' && character != '\r') {
645 character = characters[index];
650 template<typename CharacterType> inline size_t reverseFind(const CharacterType* characters, unsigned length, CharacterType matchCharacter, unsigned index)
656 while (characters[index] != matchCharacter) {
663 ALWAYS_INLINE size_t reverseFind(const UChar* characters, unsigned length, LChar matchCharacter, unsigned index)
665 return reverseFind(characters, length, static_cast<UChar>(matchCharacter), index);
668 inline size_t reverseFind(const LChar* characters, unsigned length, UChar matchCharacter, unsigned index)
670 if (matchCharacter & ~0xFF)
672 return reverseFind(characters, length, static_cast<LChar>(matchCharacter), index);
675 inline size_t StringImpl::find(LChar character, unsigned start)
678 return WTF::find(characters8(), m_length, character, start);
679 return WTF::find(characters16(), m_length, character, start);
682 ALWAYS_INLINE size_t StringImpl::find(char character, unsigned start)
684 return find(static_cast<LChar>(character), start);
687 inline size_t StringImpl::find(UChar character, unsigned start)
690 return WTF::find(characters8(), m_length, character, start);
691 return WTF::find(characters16(), m_length, character, start);
694 template<size_t inlineCapacity> inline bool equalIgnoringNullity(const Vector<UChar, inlineCapacity>& a, StringImpl* b)
696 return equalIgnoringNullity(a.data(), a.size(), b);
699 template<typename CharacterType1, typename CharacterType2> inline int codePointCompare(const CharacterType1* characters1, unsigned length1, const CharacterType2* characters2, unsigned length2)
701 unsigned commonLength = std::min(length1, length2);
703 unsigned position = 0;
704 while (position < commonLength && *characters1 == *characters2) {
710 if (position < commonLength)
711 return (characters1[0] > characters2[0]) ? 1 : -1;
713 if (length1 == length2)
715 return (length1 > length2) ? 1 : -1;
718 inline int codePointCompare(const StringImpl* string1, const StringImpl* string2)
720 // FIXME: Should null strings compare as less than empty strings rather than equal to them?
722 return (string2 && string2->length()) ? -1 : 0;
724 return string1->length() ? 1 : 0;
726 bool string1Is8Bit = string1->is8Bit();
727 bool string2Is8Bit = string2->is8Bit();
730 return codePointCompare(string1->characters8(), string1->length(), string2->characters8(), string2->length());
731 return codePointCompare(string1->characters8(), string1->length(), string2->characters16(), string2->length());
734 return codePointCompare(string1->characters16(), string1->length(), string2->characters8(), string2->length());
735 return codePointCompare(string1->characters16(), string1->length(), string2->characters16(), string2->length());
738 inline bool isSpaceOrNewline(UChar character)
740 // Use isASCIISpace() for all Latin-1 characters. This will include newlines, which aren't included in Unicode DirWS.
741 return character <= 0xFF ? isASCIISpace(character) : u_charDirection(character) == U_WHITE_SPACE_NEUTRAL;
744 template<typename CharacterType> inline unsigned lengthOfNullTerminatedString(const CharacterType* string)
748 while (string[length])
751 RELEASE_ASSERT(length < std::numeric_limits<unsigned>::max());
752 return static_cast<unsigned>(length);
755 inline StringImplShape::StringImplShape(unsigned refCount, unsigned length, const LChar* data8, unsigned hashAndFlags)
756 : m_refCount(refCount)
759 , m_hashAndFlags(hashAndFlags)
763 inline StringImplShape::StringImplShape(unsigned refCount, unsigned length, const UChar* data16, unsigned hashAndFlags)
764 : m_refCount(refCount)
767 , m_hashAndFlags(hashAndFlags)
771 template<unsigned characterCount> inline constexpr StringImplShape::StringImplShape(unsigned refCount, unsigned length, const char (&characters)[characterCount], unsigned hashAndFlags, ConstructWithConstExprTag)
772 : m_refCount(refCount)
774 , m_data8Char(characters)
775 , m_hashAndFlags(hashAndFlags)
779 template<unsigned characterCount> inline constexpr StringImplShape::StringImplShape(unsigned refCount, unsigned length, const char16_t (&characters)[characterCount], unsigned hashAndFlags, ConstructWithConstExprTag)
780 : m_refCount(refCount)
782 , m_data16Char(characters)
783 , m_hashAndFlags(hashAndFlags)
787 inline Ref<StringImpl> StringImpl::isolatedCopy() const
789 if (!requiresCopy()) {
791 return StringImpl::createWithoutCopying(m_data8, m_length);
792 return StringImpl::createWithoutCopying(m_data16, m_length);
796 return create(m_data8, m_length);
797 return create(m_data16, m_length);
800 inline bool StringImpl::isAllASCII() const
803 return charactersAreAllASCII(characters8(), length());
804 return charactersAreAllASCII(characters16(), length());
807 inline bool StringImpl::isAllLatin1() const
811 auto* characters = characters16();
813 for (size_t i = 0; i < length(); ++i)
814 ored |= characters[i];
815 return !(ored & 0xFF00);
818 template<bool isSpecialCharacter(UChar), typename CharacterType> inline bool isAllSpecialCharacters(const CharacterType* characters, size_t length)
820 for (size_t i = 0; i < length; ++i) {
821 if (!isSpecialCharacter(characters[i]))
827 template<bool isSpecialCharacter(UChar)> inline bool StringImpl::isAllSpecialCharacters() const
830 return WTF::isAllSpecialCharacters<isSpecialCharacter>(characters8(), length());
831 return WTF::isAllSpecialCharacters<isSpecialCharacter>(characters16(), length());
834 inline StringImpl::StringImpl(unsigned length, Force8Bit)
835 : StringImplShape(s_refCountIncrement, length, tailPointer<LChar>(), s_hashFlag8BitBuffer | StringNormal | BufferInternal)
840 STRING_STATS_ADD_8BIT_STRING(m_length);
843 inline StringImpl::StringImpl(unsigned length)
844 : StringImplShape(s_refCountIncrement, length, tailPointer<UChar>(), StringNormal | BufferInternal)
849 STRING_STATS_ADD_16BIT_STRING(m_length);
852 inline StringImpl::StringImpl(MallocPtr<LChar> characters, unsigned length)
853 : StringImplShape(s_refCountIncrement, length, characters.leakPtr(), s_hashFlag8BitBuffer | StringNormal | BufferOwned)
858 STRING_STATS_ADD_8BIT_STRING(m_length);
861 inline StringImpl::StringImpl(const UChar* characters, unsigned length, ConstructWithoutCopyingTag)
862 : StringImplShape(s_refCountIncrement, length, characters, StringNormal | BufferInternal)
867 STRING_STATS_ADD_16BIT_STRING(m_length);
870 inline StringImpl::StringImpl(const LChar* characters, unsigned length, ConstructWithoutCopyingTag)
871 : StringImplShape(s_refCountIncrement, length, characters, s_hashFlag8BitBuffer | StringNormal | BufferInternal)
876 STRING_STATS_ADD_8BIT_STRING(m_length);
879 inline StringImpl::StringImpl(MallocPtr<UChar> characters, unsigned length)
880 : StringImplShape(s_refCountIncrement, length, characters.leakPtr(), StringNormal | BufferOwned)
885 STRING_STATS_ADD_16BIT_STRING(m_length);
888 inline StringImpl::StringImpl(const LChar* characters, unsigned length, Ref<StringImpl>&& base)
889 : StringImplShape(s_refCountIncrement, length, characters, s_hashFlag8BitBuffer | StringNormal | BufferSubstring)
894 ASSERT(base->bufferOwnership() != BufferSubstring);
896 substringBuffer() = &base.leakRef();
898 STRING_STATS_ADD_8BIT_STRING2(m_length, true);
901 inline StringImpl::StringImpl(const UChar* characters, unsigned length, Ref<StringImpl>&& base)
902 : StringImplShape(s_refCountIncrement, length, characters, StringNormal | BufferSubstring)
907 ASSERT(base->bufferOwnership() != BufferSubstring);
909 substringBuffer() = &base.leakRef();
911 STRING_STATS_ADD_16BIT_STRING2(m_length, true);
914 template<size_t inlineCapacity> inline Ref<StringImpl> StringImpl::create8BitIfPossible(const Vector<UChar, inlineCapacity>& vector)
916 return create8BitIfPossible(vector.data(), vector.size());
919 ALWAYS_INLINE Ref<StringImpl> StringImpl::createSubstringSharingImpl(StringImpl& rep, unsigned offset, unsigned length)
921 ASSERT(length <= rep.length());
926 auto* ownerRep = ((rep.bufferOwnership() == BufferSubstring) ? rep.substringBuffer() : &rep);
928 // We allocate a buffer that contains both the StringImpl struct as well as the pointer to the owner string.
929 auto* stringImpl = static_cast<StringImpl*>(stringMalloc(allocationSize<StringImpl*>(1)));
931 return adoptRef(*new (NotNull, stringImpl) StringImpl(rep.m_data8 + offset, length, *ownerRep));
932 return adoptRef(*new (NotNull, stringImpl) StringImpl(rep.m_data16 + offset, length, *ownerRep));
935 template<unsigned characterCount> ALWAYS_INLINE Ref<StringImpl> StringImpl::createFromLiteral(const char (&characters)[characterCount])
937 COMPILE_ASSERT(characterCount > 1, StringImplFromLiteralNotEmpty);
938 COMPILE_ASSERT((characterCount - 1 <= ((unsigned(~0) - sizeof(StringImpl)) / sizeof(LChar))), StringImplFromLiteralCannotOverflow);
940 return createWithoutCopying(reinterpret_cast<const LChar*>(characters), characterCount - 1);
943 template<typename CharacterType> ALWAYS_INLINE RefPtr<StringImpl> StringImpl::tryCreateUninitialized(unsigned length, CharacterType*& output)
950 if (length > ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / sizeof(CharacterType))) {
954 auto* result = static_cast<StringImpl*>(tryStringMalloc(allocationSize<CharacterType>(length)));
959 output = result->tailPointer<CharacterType>();
961 return constructInternal<CharacterType>(*result, length);
964 template<typename CharacterType, size_t inlineCapacity, typename OverflowHandler, size_t minCapacity>
965 inline Ref<StringImpl> StringImpl::adopt(StringVector<CharacterType, inlineCapacity, OverflowHandler, minCapacity>&& vector)
967 if (size_t size = vector.size()) {
968 ASSERT(vector.data());
969 if (size > std::numeric_limits<unsigned>::max())
971 return adoptRef(*new StringImpl(vector.releaseBuffer(), size));
976 inline size_t StringImpl::cost() const
978 // For substrings, return the cost of the base string.
979 if (bufferOwnership() == BufferSubstring)
980 return substringBuffer()->cost();
982 // Note: we must not alter the m_hashAndFlags field in instances of StaticStringImpl.
983 // We ensure this by pre-setting the s_hashFlagDidReportCost bit in all instances of
984 // StaticStringImpl. As a result, StaticStringImpl instances will always return a cost of
985 // 0 here and avoid modifying m_hashAndFlags.
986 if (m_hashAndFlags & s_hashFlagDidReportCost)
989 m_hashAndFlags |= s_hashFlagDidReportCost;
990 size_t result = m_length;
996 inline size_t StringImpl::costDuringGC()
1001 if (bufferOwnership() == BufferSubstring)
1002 return divideRoundedUp(substringBuffer()->costDuringGC(), refCount());
1004 size_t result = m_length;
1007 return divideRoundedUp(result, refCount());
1010 inline void StringImpl::setIsAtomic(bool isAtomic)
1012 ASSERT(!isStatic());
1013 ASSERT(!isSymbol());
1015 m_hashAndFlags |= s_hashFlagStringKindIsAtomic;
1017 m_hashAndFlags &= ~s_hashFlagStringKindIsAtomic;
1020 inline void StringImpl::setHash(unsigned hash) const
1022 // The high bits of 'hash' are always empty, but we prefer to store our flags
1023 // in the low bits because it makes them slightly more efficient to access.
1024 // So, we shift left and right when setting and getting our hash code.
1027 ASSERT(!isStatic());
1028 // Multiple clients assume that StringHasher is the canonical string hash function.
1029 ASSERT(hash == (is8Bit() ? StringHasher::computeHashAndMaskTop8Bits(m_data8, m_length) : StringHasher::computeHashAndMaskTop8Bits(m_data16, m_length)));
1030 ASSERT(!(hash & (s_flagMask << (8 * sizeof(hash) - s_flagCount)))); // Verify that enough high bits are empty.
1032 hash <<= s_flagCount;
1033 ASSERT(!(hash & m_hashAndFlags)); // Verify that enough low bits are empty after shift.
1034 ASSERT(hash); // Verify that 0 is a valid sentinel hash value.
1036 m_hashAndFlags |= hash; // Store hash with flags in low bits.
1039 inline void StringImpl::ref()
1041 STRING_STATS_REF_STRING(*this);
1043 m_refCount += s_refCountIncrement;
1046 inline void StringImpl::deref()
1048 STRING_STATS_DEREF_STRING(*this);
1050 unsigned tempRefCount = m_refCount - s_refCountIncrement;
1051 if (!tempRefCount) {
1052 StringImpl::destroy(this);
1055 m_refCount = tempRefCount;
1058 template<typename CharacterType> inline void StringImpl::copyCharacters(CharacterType* destination, const CharacterType* source, unsigned numCharacters)
1060 if (numCharacters == 1) {
1061 *destination = *source;
1064 memcpy(destination, source, numCharacters * sizeof(CharacterType));
1067 ALWAYS_INLINE void StringImpl::copyCharacters(UChar* destination, const LChar* source, unsigned numCharacters)
1069 for (unsigned i = 0; i < numCharacters; ++i)
1070 destination[i] = source[i];
1073 inline UChar StringImpl::at(unsigned i) const
1075 ASSERT_WITH_SECURITY_IMPLICATION(i < m_length);
1076 return is8Bit() ? m_data8[i] : m_data16[i];
1079 inline void StringImpl::assertCaged() const
1081 if (!ASSERT_DISABLED)
1082 releaseAssertCaged();
1085 inline StringImpl::StringImpl(CreateSymbolTag, const LChar* characters, unsigned length)
1086 : StringImplShape(s_refCountIncrement, length, characters, s_hashFlag8BitBuffer | StringSymbol | BufferSubstring)
1090 STRING_STATS_ADD_8BIT_STRING2(m_length, true);
1093 inline StringImpl::StringImpl(CreateSymbolTag, const UChar* characters, unsigned length)
1094 : StringImplShape(s_refCountIncrement, length, characters, StringSymbol | BufferSubstring)
1098 STRING_STATS_ADD_16BIT_STRING2(m_length, true);
1101 inline StringImpl::StringImpl(CreateSymbolTag)
1102 : StringImplShape(s_refCountIncrement, 0, empty()->characters8(), s_hashFlag8BitBuffer | StringSymbol | BufferSubstring)
1106 STRING_STATS_ADD_8BIT_STRING2(m_length, true);
1109 template<typename T> inline size_t StringImpl::allocationSize(Checked<size_t> tailElementCount)
1111 return (tailOffset<T>() + tailElementCount * sizeof(T)).unsafeGet();
1114 template<typename T> inline size_t StringImpl::tailOffset()
1117 // MSVC doesn't support alignof yet.
1118 return roundUpToMultipleOf<sizeof(T)>(sizeof(StringImpl));
1120 return roundUpToMultipleOf<alignof(T)>(offsetof(StringImpl, m_hashAndFlags) + sizeof(StringImpl::m_hashAndFlags));
1124 inline bool StringImpl::requiresCopy() const
1126 if (bufferOwnership() != BufferInternal)
1130 return m_data8 == tailPointer<LChar>();
1131 return m_data16 == tailPointer<UChar>();
1134 template<typename T> inline const T* StringImpl::tailPointer() const
1136 return reinterpret_cast_ptr<const T*>(reinterpret_cast<const uint8_t*>(this) + tailOffset<T>());
1139 template<typename T> inline T* StringImpl::tailPointer()
1141 return reinterpret_cast_ptr<T*>(reinterpret_cast<uint8_t*>(this) + tailOffset<T>());
1144 inline StringImpl* const& StringImpl::substringBuffer() const
1146 ASSERT(bufferOwnership() == BufferSubstring);
1148 return *tailPointer<StringImpl*>();
1151 inline StringImpl*& StringImpl::substringBuffer()
1153 ASSERT(bufferOwnership() == BufferSubstring);
1155 return *tailPointer<StringImpl*>();
1158 inline void StringImpl::assertHashIsCorrect() const
1160 ASSERT(existingHash() == StringHasher::computeHashAndMaskTop8Bits(characters8(), length()));
1163 template<unsigned characterCount> inline constexpr StringImpl::StaticStringImpl::StaticStringImpl(const char (&characters)[characterCount], StringKind stringKind)
1164 : StringImplShape(s_refCountFlagIsStaticString, characterCount - 1, characters,
1165 s_hashFlag8BitBuffer | s_hashFlagDidReportCost | stringKind | BufferInternal | (StringHasher::computeLiteralHashAndMaskTop8Bits(characters) << s_flagCount), ConstructWithConstExpr)
1169 template<unsigned characterCount> inline constexpr StringImpl::StaticStringImpl::StaticStringImpl(const char16_t (&characters)[characterCount], StringKind stringKind)
1170 : StringImplShape(s_refCountFlagIsStaticString, characterCount - 1, characters,
1171 s_hashFlagDidReportCost | stringKind | BufferInternal | (StringHasher::computeLiteralHashAndMaskTop8Bits(characters) << s_flagCount), ConstructWithConstExpr)
1175 inline StringImpl::StaticStringImpl::operator StringImpl&()
1177 return *reinterpret_cast<StringImpl*>(this);
1180 inline bool equalIgnoringASCIICase(const StringImpl& a, const StringImpl& b)
1182 return equalIgnoringASCIICaseCommon(a, b);
1185 inline bool equalIgnoringASCIICase(const StringImpl& a, const char* b)
1187 return equalIgnoringASCIICaseCommon(a, b);
1190 inline bool equalIgnoringASCIICase(const StringImpl* a, const char* b)
1192 return a && equalIgnoringASCIICase(*a, b);
1195 template<unsigned length> inline bool startsWithLettersIgnoringASCIICase(const StringImpl& string, const char (&lowercaseLetters)[length])
1197 return startsWithLettersIgnoringASCIICaseCommon(string, lowercaseLetters);
1200 template<unsigned length> inline bool startsWithLettersIgnoringASCIICase(const StringImpl* string, const char (&lowercaseLetters)[length])
1202 return string && startsWithLettersIgnoringASCIICase(*string, lowercaseLetters);
1205 template<unsigned length> inline bool equalLettersIgnoringASCIICase(const StringImpl& string, const char (&lowercaseLetters)[length])
1207 return equalLettersIgnoringASCIICaseCommon(string, lowercaseLetters);
1210 template<unsigned length> inline bool equalLettersIgnoringASCIICase(const StringImpl* string, const char (&lowercaseLetters)[length])
1212 return string && equalLettersIgnoringASCIICase(*string, lowercaseLetters);
1217 using WTF::StringImpl;
1218 using WTF::StaticStringImpl;