2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2005-2010, 2013-2014 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/uchar.h>
28 #include <unicode/ustring.h>
29 #include <wtf/ASCIICType.h>
30 #include <wtf/Forward.h>
31 #include <wtf/MathExtras.h>
32 #include <wtf/StdLibExtras.h>
33 #include <wtf/StringHasher.h>
34 #include <wtf/Vector.h>
35 #include <wtf/text/ConversionMode.h>
38 typedef const struct __CFString * CFStringRef;
46 namespace LLInt { class Data; }
47 class LLIntOffsetsExtractor;
52 struct CStringTranslator;
53 template<typename CharacterType> struct HashAndCharactersTranslator;
54 struct HashAndUTF8CharactersTranslator;
55 struct LCharBufferTranslator;
56 struct CharBufferFromLiteralDataTranslator;
57 struct SubstringTranslator;
58 struct UCharBufferTranslator;
59 template<typename> class RetainPtr;
61 enum TextCaseSensitivity {
66 typedef bool (*CharacterMatchFunctionPtr)(UChar);
67 typedef bool (*IsWhiteSpaceFunctionPtr)(UChar);
69 // Define STRING_STATS to 1 turn on run time statistics of string sizes and memory usage
70 #define STRING_STATS 0
74 inline void add8BitString(unsigned length, bool isSubString = false)
76 ++m_totalNumberStrings;
77 ++m_number8BitStrings;
79 m_total8BitData += length;
82 inline void add16BitString(unsigned length, bool isSubString = false)
84 ++m_totalNumberStrings;
85 ++m_number16BitStrings;
87 m_total16BitData += length;
90 void removeString(StringImpl&);
93 static const unsigned s_printStringStatsFrequency = 5000;
94 static std::atomic<unsigned> s_stringRemovesTillPrintStats;
96 std::atomic<unsigned> m_refCalls;
97 std::atomic<unsigned> m_derefCalls;
99 std::atomic<unsigned> m_totalNumberStrings;
100 std::atomic<unsigned> m_number8BitStrings;
101 std::atomic<unsigned> m_number16BitStrings;
102 std::atomic<unsigned long long> m_total8BitData;
103 std::atomic<unsigned long long> m_total16BitData;
106 #define STRING_STATS_ADD_8BIT_STRING(length) StringImpl::stringStats().add8BitString(length)
107 #define STRING_STATS_ADD_8BIT_STRING2(length, isSubString) StringImpl::stringStats().add8BitString(length, isSubString)
108 #define STRING_STATS_ADD_16BIT_STRING(length) StringImpl::stringStats().add16BitString(length)
109 #define STRING_STATS_ADD_16BIT_STRING2(length, isSubString) StringImpl::stringStats().add16BitString(length, isSubString)
110 #define STRING_STATS_REMOVE_STRING(string) StringImpl::stringStats().removeString(string)
111 #define STRING_STATS_REF_STRING(string) ++StringImpl::stringStats().m_refCalls;
112 #define STRING_STATS_DEREF_STRING(string) ++StringImpl::stringStats().m_derefCalls;
114 #define STRING_STATS_ADD_8BIT_STRING(length) ((void)0)
115 #define STRING_STATS_ADD_8BIT_STRING2(length, isSubString) ((void)0)
116 #define STRING_STATS_ADD_16BIT_STRING(length) ((void)0)
117 #define STRING_STATS_ADD_16BIT_STRING2(length, isSubString) ((void)0)
118 #define STRING_STATS_ADD_UPCONVERTED_STRING(length) ((void)0)
119 #define STRING_STATS_REMOVE_STRING(string) ((void)0)
120 #define STRING_STATS_REF_STRING(string) ((void)0)
121 #define STRING_STATS_DEREF_STRING(string) ((void)0)
125 WTF_MAKE_NONCOPYABLE(StringImpl); WTF_MAKE_FAST_ALLOCATED;
126 friend struct WTF::CStringTranslator;
127 template<typename CharacterType> friend struct WTF::HashAndCharactersTranslator;
128 friend struct WTF::HashAndUTF8CharactersTranslator;
129 friend struct WTF::CharBufferFromLiteralDataTranslator;
130 friend struct WTF::LCharBufferTranslator;
131 friend struct WTF::SubstringTranslator;
132 friend struct WTF::UCharBufferTranslator;
133 friend class AtomicStringImpl;
134 friend class JSC::LLInt::Data;
135 friend class JSC::LLIntOffsetsExtractor;
138 enum BufferOwnership {
144 // Used to construct static strings, which have an special refCount that can never hit zero.
145 // This means that the static string will never be destroyed, which is important because
146 // static strings will be shared across threads & ref-counted in a non-threadsafe manner.
147 friend class NeverDestroyed<StringImpl>;
148 enum ConstructEmptyStringTag { ConstructEmptyString };
149 StringImpl(ConstructEmptyStringTag)
150 : m_refCount(s_refCountFlagIsStaticString)
152 , m_data8(reinterpret_cast<const LChar*>(&m_length))
153 , m_hashAndFlags(s_hashFlag8BitBuffer | s_hashFlagIsAtomic | BufferOwned)
155 // Ensure that the hash is computed so that AtomicStringHash can call existingHash()
156 // with impunity. The empty string is special because it is never entered into
157 // AtomicString's HashKey, but still needs to compare correctly.
158 STRING_STATS_ADD_8BIT_STRING(m_length);
163 // FIXME: there has to be a less hacky way to do this.
164 enum Force8Bit { Force8BitConstructor };
165 // Create a normal 8-bit string with internal storage (BufferInternal)
166 StringImpl(unsigned length, Force8Bit)
167 : m_refCount(s_refCountIncrement)
169 , m_data8(tailPointer<LChar>())
170 , m_hashAndFlags(s_hashFlag8BitBuffer | BufferInternal)
175 STRING_STATS_ADD_8BIT_STRING(m_length);
178 // Create a normal 16-bit string with internal storage (BufferInternal)
179 StringImpl(unsigned length)
180 : m_refCount(s_refCountIncrement)
182 , m_data16(tailPointer<UChar>())
183 , m_hashAndFlags(BufferInternal)
188 STRING_STATS_ADD_16BIT_STRING(m_length);
191 // Create a StringImpl adopting ownership of the provided buffer (BufferOwned)
192 StringImpl(MallocPtr<LChar> characters, unsigned length)
193 : m_refCount(s_refCountIncrement)
195 , m_data8(characters.leakPtr())
196 , m_hashAndFlags(s_hashFlag8BitBuffer | BufferOwned)
201 STRING_STATS_ADD_8BIT_STRING(m_length);
204 enum ConstructWithoutCopyingTag { ConstructWithoutCopying };
205 StringImpl(const UChar* characters, unsigned length, ConstructWithoutCopyingTag)
206 : m_refCount(s_refCountIncrement)
208 , m_data16(characters)
209 , m_hashAndFlags(BufferInternal)
214 STRING_STATS_ADD_16BIT_STRING(m_length);
217 StringImpl(const LChar* characters, unsigned length, ConstructWithoutCopyingTag)
218 : m_refCount(s_refCountIncrement)
220 , m_data8(characters)
221 , m_hashAndFlags(s_hashFlag8BitBuffer | BufferInternal)
226 STRING_STATS_ADD_8BIT_STRING(m_length);
229 // Create a StringImpl adopting ownership of the provided buffer (BufferOwned)
230 StringImpl(MallocPtr<UChar> characters, unsigned length)
231 : m_refCount(s_refCountIncrement)
233 , m_data16(characters.leakPtr())
234 , m_hashAndFlags(BufferOwned)
239 STRING_STATS_ADD_16BIT_STRING(m_length);
242 // Used to create new strings that are a substring of an existing 8-bit StringImpl (BufferSubstring)
243 StringImpl(const LChar* characters, unsigned length, PassRefPtr<StringImpl> base)
244 : m_refCount(s_refCountIncrement)
246 , m_data8(characters)
247 , m_hashAndFlags(s_hashFlag8BitBuffer | BufferSubstring)
252 ASSERT(base->bufferOwnership() != BufferSubstring);
254 substringBuffer() = base.leakRef();
256 STRING_STATS_ADD_8BIT_STRING2(m_length, true);
259 // Used to create new strings that are a substring of an existing 16-bit StringImpl (BufferSubstring)
260 StringImpl(const UChar* characters, unsigned length, PassRefPtr<StringImpl> base)
261 : m_refCount(s_refCountIncrement)
263 , m_data16(characters)
264 , m_hashAndFlags(BufferSubstring)
269 ASSERT(base->bufferOwnership() != BufferSubstring);
271 substringBuffer() = base.leakRef();
273 STRING_STATS_ADD_16BIT_STRING2(m_length, true);
276 enum CreateEmptyUniqueTag { CreateEmptyUnique };
277 StringImpl(CreateEmptyUniqueTag)
278 : m_refCount(s_refCountIncrement)
280 // We expect m_length to be initialized to 0 as we use it
281 // to represent a null terminated buffer.
282 , m_data8(reinterpret_cast<const LChar*>(&m_length))
283 , m_hashAndFlags(hashAndFlagsForUnique(s_hashFlag8BitBuffer | BufferInternal))
287 STRING_STATS_ADD_8BIT_STRING(m_length);
293 WTF_EXPORT_STRING_API static void destroy(StringImpl*);
295 WTF_EXPORT_STRING_API static Ref<StringImpl> create(const UChar*, unsigned length);
296 WTF_EXPORT_STRING_API static Ref<StringImpl> create(const LChar*, unsigned length);
297 WTF_EXPORT_STRING_API static Ref<StringImpl> create8BitIfPossible(const UChar*, unsigned length);
298 template<size_t inlineCapacity>
299 static Ref<StringImpl> create8BitIfPossible(const Vector<UChar, inlineCapacity>& vector)
301 return create8BitIfPossible(vector.data(), vector.size());
303 WTF_EXPORT_STRING_API static Ref<StringImpl> create8BitIfPossible(const UChar*);
305 ALWAYS_INLINE static Ref<StringImpl> create(const char* s, unsigned length) { return create(reinterpret_cast<const LChar*>(s), length); }
306 WTF_EXPORT_STRING_API static Ref<StringImpl> create(const LChar*);
307 ALWAYS_INLINE static Ref<StringImpl> create(const char* s) { return create(reinterpret_cast<const LChar*>(s)); }
309 static ALWAYS_INLINE Ref<StringImpl> createSubstringSharingImpl8(PassRefPtr<StringImpl> rep, unsigned offset, unsigned length)
312 ASSERT(length <= rep->length());
317 ASSERT(rep->is8Bit());
318 StringImpl* ownerRep = (rep->bufferOwnership() == BufferSubstring) ? rep->substringBuffer() : rep.get();
320 // We allocate a buffer that contains both the StringImpl struct as well as the pointer to the owner string.
321 StringImpl* stringImpl = static_cast<StringImpl*>(fastMalloc(allocationSize<StringImpl*>(1)));
322 return adoptRef(*new (NotNull, stringImpl) StringImpl(rep->m_data8 + offset, length, ownerRep));
325 static ALWAYS_INLINE Ref<StringImpl> createSubstringSharingImpl(PassRefPtr<StringImpl> rep, unsigned offset, unsigned length)
328 ASSERT(length <= rep->length());
333 StringImpl* ownerRep = (rep->bufferOwnership() == BufferSubstring) ? rep->substringBuffer() : rep.get();
335 // We allocate a buffer that contains both the StringImpl struct as well as the pointer to the owner string.
336 StringImpl* stringImpl = static_cast<StringImpl*>(fastMalloc(allocationSize<StringImpl*>(1)));
338 return adoptRef(*new (NotNull, stringImpl) StringImpl(rep->m_data8 + offset, length, ownerRep));
339 return adoptRef(*new (NotNull, stringImpl) StringImpl(rep->m_data16 + offset, length, ownerRep));
342 template<unsigned charactersCount>
343 ALWAYS_INLINE static Ref<StringImpl> createFromLiteral(const char (&characters)[charactersCount])
345 COMPILE_ASSERT(charactersCount > 1, StringImplFromLiteralNotEmpty);
346 COMPILE_ASSERT((charactersCount - 1 <= ((unsigned(~0) - sizeof(StringImpl)) / sizeof(LChar))), StringImplFromLiteralCannotOverflow);
348 return createWithoutCopying(reinterpret_cast<const LChar*>(characters), charactersCount - 1);
351 // FIXME: Transition off of these functions to createWithoutCopying instead.
352 WTF_EXPORT_STRING_API static Ref<StringImpl> createFromLiteral(const char* characters, unsigned length);
353 WTF_EXPORT_STRING_API static Ref<StringImpl> createFromLiteral(const char* characters);
355 WTF_EXPORT_STRING_API static Ref<StringImpl> createWithoutCopying(const UChar* characters, unsigned length);
356 WTF_EXPORT_STRING_API static Ref<StringImpl> createWithoutCopying(const LChar* characters, unsigned length);
358 WTF_EXPORT_STRING_API static Ref<StringImpl> createUninitialized(unsigned length, LChar*& data);
359 WTF_EXPORT_STRING_API static Ref<StringImpl> createUninitialized(unsigned length, UChar*& data);
360 template <typename T> static ALWAYS_INLINE PassRefPtr<StringImpl> tryCreateUninitialized(unsigned length, T*& output)
367 if (length > ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / sizeof(T))) {
371 StringImpl* resultImpl;
372 if (!tryFastMalloc(allocationSize<T>(length)).getValue(resultImpl)) {
376 output = resultImpl->tailPointer<T>();
378 return constructInternal<T>(resultImpl, length);
381 ALWAYS_INLINE static Ref<StringImpl> createUniqueEmpty()
383 return adoptRef(*new StringImpl(CreateEmptyUnique));
386 WTF_EXPORT_STRING_API static Ref<StringImpl> createUnique(PassRefPtr<StringImpl> rep);
388 // Reallocate the StringImpl. The originalString must be only owned by the PassRefPtr,
389 // and the buffer ownership must be BufferInternal. Just like the input pointer of realloc(),
390 // the originalString can't be used after this function.
391 static Ref<StringImpl> reallocate(PassRefPtr<StringImpl> originalString, unsigned length, LChar*& data);
392 static Ref<StringImpl> reallocate(PassRefPtr<StringImpl> originalString, unsigned length, UChar*& data);
394 static unsigned flagsOffset() { return OBJECT_OFFSETOF(StringImpl, m_hashAndFlags); }
395 static unsigned flagIs8Bit() { return s_hashFlag8BitBuffer; }
396 static unsigned flagIsAtomic() { return s_hashFlagIsAtomic; }
397 static unsigned flagIsUnique() { return s_hashFlagIsUnique; }
398 static unsigned dataOffset() { return OBJECT_OFFSETOF(StringImpl, m_data8); }
400 template<typename CharType, size_t inlineCapacity, typename OverflowHandler>
401 static Ref<StringImpl> adopt(Vector<CharType, inlineCapacity, OverflowHandler>& vector)
403 if (size_t size = vector.size()) {
404 ASSERT(vector.data());
405 if (size > std::numeric_limits<unsigned>::max())
407 return adoptRef(*new StringImpl(vector.releaseBuffer(), size));
412 WTF_EXPORT_STRING_API static Ref<StringImpl> adopt(StringBuffer<UChar>&);
413 WTF_EXPORT_STRING_API static Ref<StringImpl> adopt(StringBuffer<LChar>&);
415 unsigned length() const { return m_length; }
416 static ptrdiff_t lengthMemoryOffset() { return OBJECT_OFFSETOF(StringImpl, m_length); }
417 bool is8Bit() const { return m_hashAndFlags & s_hashFlag8BitBuffer; }
419 ALWAYS_INLINE const LChar* characters8() const { ASSERT(is8Bit()); return m_data8; }
420 ALWAYS_INLINE const UChar* characters16() const { ASSERT(!is8Bit()); return m_data16; }
422 template <typename CharType>
423 ALWAYS_INLINE const CharType *characters() const;
427 // For substrings, return the cost of the base string.
428 if (bufferOwnership() == BufferSubstring)
429 return substringBuffer()->cost();
431 if (m_hashAndFlags & s_hashFlagDidReportCost)
434 m_hashAndFlags |= s_hashFlagDidReportCost;
435 size_t result = m_length;
441 size_t costDuringGC()
446 if (bufferOwnership() == BufferSubstring)
447 return divideRoundedUp(substringBuffer()->costDuringGC(), refCount());
449 size_t result = m_length;
452 return divideRoundedUp(result, refCount());
455 WTF_EXPORT_STRING_API size_t sizeInBytes() const;
457 bool isUnique() const { return m_hashAndFlags & s_hashFlagIsUnique; }
459 bool isAtomic() const { return m_hashAndFlags & s_hashFlagIsAtomic; }
460 void setIsAtomic(bool isAtomic)
465 m_hashAndFlags |= s_hashFlagIsAtomic;
467 m_hashAndFlags &= ~s_hashFlagIsAtomic;
471 bool isSubString() const { return bufferOwnership() == BufferSubstring; }
474 static WTF_EXPORT_STRING_API CString utf8ForCharacters(const UChar* characters, unsigned length, ConversionMode = LenientConversion);
475 WTF_EXPORT_STRING_API CString utf8ForRange(unsigned offset, unsigned length, ConversionMode = LenientConversion) const;
476 WTF_EXPORT_STRING_API CString utf8(ConversionMode = LenientConversion) const;
479 static WTF_EXPORT_STRING_API bool utf8Impl(const UChar* characters, unsigned length, char*& buffer, size_t bufferSize, ConversionMode);
481 // The high bits of 'hash' are always empty, but we prefer to store our flags
482 // in the low bits because it makes them slightly more efficient to access.
483 // So, we shift left and right when setting and getting our hash code.
484 void setHash(unsigned hash) const
487 // Multiple clients assume that StringHasher is the canonical string hash function.
488 ASSERT(hash == (is8Bit() ? StringHasher::computeHashAndMaskTop8Bits(m_data8, m_length) : StringHasher::computeHashAndMaskTop8Bits(m_data16, m_length)));
489 ASSERT(!(hash & (s_flagMask << (8 * sizeof(hash) - s_flagCount)))); // Verify that enough high bits are empty.
491 hash <<= s_flagCount;
492 ASSERT(!(hash & m_hashAndFlags)); // Verify that enough low bits are empty after shift.
493 ASSERT(hash); // Verify that 0 is a valid sentinel hash value.
495 m_hashAndFlags |= hash; // Store hash with flags in low bits.
498 unsigned rawHash() const
500 return m_hashAndFlags >> s_flagCount;
506 return rawHash() != 0;
509 unsigned existingHash() const
515 unsigned hash() const
518 return existingHash();
519 return hashSlowCase();
522 bool isStatic() const { return m_refCount & s_refCountFlagIsStaticString; }
524 inline size_t refCount() const
526 return m_refCount / s_refCountIncrement;
529 inline bool hasOneRef() const
531 return m_refCount == s_refCountIncrement;
534 // This method is useful for assertions.
535 inline bool hasAtLeastOneRef() const
542 ASSERT(!isCompilationThread());
544 STRING_STATS_REF_STRING(*this);
546 m_refCount += s_refCountIncrement;
551 ASSERT(!isCompilationThread());
553 STRING_STATS_DEREF_STRING(*this);
555 unsigned tempRefCount = m_refCount - s_refCountIncrement;
557 StringImpl::destroy(this);
560 m_refCount = tempRefCount;
563 WTF_EXPORT_PRIVATE static StringImpl* empty();
565 // FIXME: Does this really belong in StringImpl?
566 template <typename T> static void copyChars(T* destination, const T* source, unsigned numCharacters)
568 if (numCharacters == 1) {
569 *destination = *source;
573 if (numCharacters <= s_copyCharsInlineCutOff) {
575 #if (CPU(X86) || CPU(X86_64))
576 const unsigned charsPerInt = sizeof(uint32_t) / sizeof(T);
578 if (numCharacters > charsPerInt) {
579 unsigned stopCount = numCharacters & ~(charsPerInt - 1);
581 const uint32_t* srcCharacters = reinterpret_cast<const uint32_t*>(source);
582 uint32_t* destCharacters = reinterpret_cast<uint32_t*>(destination);
583 for (unsigned j = 0; i < stopCount; i += charsPerInt, ++j)
584 destCharacters[j] = srcCharacters[j];
587 for (; i < numCharacters; ++i)
588 destination[i] = source[i];
590 memcpy(destination, source, numCharacters * sizeof(T));
593 ALWAYS_INLINE static void copyChars(UChar* destination, const LChar* source, unsigned numCharacters)
595 for (unsigned i = 0; i < numCharacters; ++i)
596 destination[i] = source[i];
599 // Some string features, like refcounting and the atomicity flag, are not
600 // thread-safe. We achieve thread safety by isolation, giving each thread
601 // its own copy of the string.
602 Ref<StringImpl> isolatedCopy() const;
604 WTF_EXPORT_STRING_API Ref<StringImpl> substring(unsigned pos, unsigned len = UINT_MAX);
606 UChar at(unsigned i) const
608 ASSERT_WITH_SECURITY_IMPLICATION(i < m_length);
613 UChar operator[](unsigned i) const { return at(i); }
614 WTF_EXPORT_STRING_API UChar32 characterStartingAt(unsigned);
616 WTF_EXPORT_STRING_API bool containsOnlyWhitespace();
618 int toIntStrict(bool* ok = 0, int base = 10);
619 unsigned toUIntStrict(bool* ok = 0, int base = 10);
620 int64_t toInt64Strict(bool* ok = 0, int base = 10);
621 uint64_t toUInt64Strict(bool* ok = 0, int base = 10);
622 intptr_t toIntPtrStrict(bool* ok = 0, int base = 10);
624 WTF_EXPORT_STRING_API int toInt(bool* ok = 0); // ignores trailing garbage
625 unsigned toUInt(bool* ok = 0); // ignores trailing garbage
626 int64_t toInt64(bool* ok = 0); // ignores trailing garbage
627 uint64_t toUInt64(bool* ok = 0); // ignores trailing garbage
628 intptr_t toIntPtr(bool* ok = 0); // ignores trailing garbage
630 // FIXME: Like the strict functions above, these give false for "ok" when there is trailing garbage.
631 // Like the non-strict functions above, these return the value when there is trailing garbage.
632 // It would be better if these were more consistent with the above functions instead.
633 double toDouble(bool* ok = 0);
634 float toFloat(bool* ok = 0);
636 WTF_EXPORT_STRING_API Ref<StringImpl> convertToASCIILowercase();
637 WTF_EXPORT_STRING_API Ref<StringImpl> lower();
638 WTF_EXPORT_STRING_API Ref<StringImpl> upper();
639 WTF_EXPORT_STRING_API Ref<StringImpl> lower(const AtomicString& localeIdentifier);
640 WTF_EXPORT_STRING_API Ref<StringImpl> upper(const AtomicString& localeIdentifier);
642 Ref<StringImpl> foldCase();
644 Ref<StringImpl> stripWhiteSpace();
645 Ref<StringImpl> stripWhiteSpace(IsWhiteSpaceFunctionPtr);
646 WTF_EXPORT_STRING_API Ref<StringImpl> simplifyWhiteSpace();
647 Ref<StringImpl> simplifyWhiteSpace(IsWhiteSpaceFunctionPtr);
649 Ref<StringImpl> removeCharacters(CharacterMatchFunctionPtr);
650 template <typename CharType>
651 ALWAYS_INLINE Ref<StringImpl> removeCharacters(const CharType* characters, CharacterMatchFunctionPtr);
653 size_t find(LChar character, unsigned start = 0);
654 size_t find(char character, unsigned start = 0);
655 size_t find(UChar character, unsigned start = 0);
656 WTF_EXPORT_STRING_API size_t find(CharacterMatchFunctionPtr, unsigned index = 0);
657 size_t find(const LChar*, unsigned index = 0);
658 ALWAYS_INLINE size_t find(const char* s, unsigned index = 0) { return find(reinterpret_cast<const LChar*>(s), index); }
659 WTF_EXPORT_STRING_API size_t find(StringImpl*);
660 WTF_EXPORT_STRING_API size_t find(StringImpl*, unsigned index);
661 size_t findIgnoringCase(const LChar*, unsigned index = 0);
662 ALWAYS_INLINE size_t findIgnoringCase(const char* s, unsigned index = 0) { return findIgnoringCase(reinterpret_cast<const LChar*>(s), index); }
663 WTF_EXPORT_STRING_API size_t findIgnoringCase(StringImpl*, unsigned index = 0);
665 WTF_EXPORT_STRING_API size_t findNextLineStart(unsigned index = UINT_MAX);
667 WTF_EXPORT_STRING_API size_t reverseFind(UChar, unsigned index = UINT_MAX);
668 WTF_EXPORT_STRING_API size_t reverseFind(StringImpl*, unsigned index = UINT_MAX);
669 WTF_EXPORT_STRING_API size_t reverseFindIgnoringCase(StringImpl*, unsigned index = UINT_MAX);
671 WTF_EXPORT_STRING_API bool startsWith(const StringImpl*) const;
672 bool startsWith(StringImpl* str, bool caseSensitive) { return caseSensitive ? startsWith(str) : (reverseFindIgnoringCase(str, 0) == 0); }
673 WTF_EXPORT_STRING_API bool startsWith(UChar) const;
674 WTF_EXPORT_STRING_API bool startsWith(const char*, unsigned matchLength, bool caseSensitive) const;
675 template<unsigned matchLength>
676 bool startsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const { return startsWith(prefix, matchLength - 1, caseSensitive); }
677 WTF_EXPORT_STRING_API bool startsWith(StringImpl&, unsigned startOffset, bool caseSensitive) const;
679 WTF_EXPORT_STRING_API bool endsWith(StringImpl*, bool caseSensitive = true);
680 WTF_EXPORT_STRING_API bool endsWith(UChar) const;
681 WTF_EXPORT_STRING_API bool endsWith(const char*, unsigned matchLength, bool caseSensitive) const;
682 template<unsigned matchLength>
683 bool endsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const { return endsWith(prefix, matchLength - 1, caseSensitive); }
684 WTF_EXPORT_STRING_API bool endsWith(StringImpl&, unsigned endOffset, bool caseSensitive) const;
686 WTF_EXPORT_STRING_API Ref<StringImpl> replace(UChar, UChar);
687 WTF_EXPORT_STRING_API Ref<StringImpl> replace(UChar, StringImpl*);
688 ALWAYS_INLINE Ref<StringImpl> replace(UChar pattern, const char* replacement, unsigned replacementLength) { return replace(pattern, reinterpret_cast<const LChar*>(replacement), replacementLength); }
689 WTF_EXPORT_STRING_API Ref<StringImpl> replace(UChar, const LChar*, unsigned replacementLength);
690 Ref<StringImpl> replace(UChar, const UChar*, unsigned replacementLength);
691 WTF_EXPORT_STRING_API Ref<StringImpl> replace(StringImpl*, StringImpl*);
692 WTF_EXPORT_STRING_API Ref<StringImpl> replace(unsigned index, unsigned len, StringImpl*);
694 WTF_EXPORT_STRING_API UCharDirection defaultWritingDirection(bool* hasStrongDirectionality = nullptr);
697 RetainPtr<CFStringRef> createCFString();
700 WTF_EXPORT_STRING_API operator NSString*();
704 ALWAYS_INLINE static StringStats& stringStats() { return m_stringStats; }
707 WTF_EXPORT_STRING_API static const UChar latin1CaseFoldTable[256];
710 bool requiresCopy() const
712 if (bufferOwnership() != BufferInternal)
716 return m_data8 == tailPointer<LChar>();
717 return m_data16 == tailPointer<UChar>();
721 static size_t allocationSize(unsigned tailElementCount)
723 return tailOffset<T>() + tailElementCount * sizeof(T);
727 static ptrdiff_t tailOffset()
730 // MSVC doesn't support alignof yet.
731 return roundUpToMultipleOf<sizeof(T)>(sizeof(StringImpl));
733 return roundUpToMultipleOf<alignof(T)>(offsetof(StringImpl, m_hashAndFlags) + sizeof(StringImpl::m_hashAndFlags));
738 const T* tailPointer() const
740 return reinterpret_cast_ptr<const T*>(reinterpret_cast<const uint8_t*>(this) + tailOffset<T>());
746 return reinterpret_cast_ptr<T*>(reinterpret_cast<uint8_t*>(this) + tailOffset<T>());
749 StringImpl* const& substringBuffer() const
751 ASSERT(bufferOwnership() == BufferSubstring);
753 return *tailPointer<StringImpl*>();
756 StringImpl*& substringBuffer()
758 ASSERT(bufferOwnership() == BufferSubstring);
760 return *tailPointer<StringImpl*>();
763 // This number must be at least 2 to avoid sharing empty, null as well as 1 character strings from SmallStrings.
764 static const unsigned s_copyCharsInlineCutOff = 20;
766 BufferOwnership bufferOwnership() const { return static_cast<BufferOwnership>(m_hashAndFlags & s_hashMaskBufferOwnership); }
767 template <class UCharPredicate> Ref<StringImpl> stripMatchedCharacters(UCharPredicate);
768 template <typename CharType, class UCharPredicate> Ref<StringImpl> simplifyMatchedCharactersToSpace(UCharPredicate);
769 template <typename CharType> static Ref<StringImpl> constructInternal(StringImpl*, unsigned);
770 template <typename CharType> static Ref<StringImpl> createUninitializedInternal(unsigned, CharType*&);
771 template <typename CharType> static Ref<StringImpl> createUninitializedInternalNonEmpty(unsigned, CharType*&);
772 template <typename CharType> static Ref<StringImpl> reallocateInternal(PassRefPtr<StringImpl>, unsigned, CharType*&);
773 template <typename CharType> static Ref<StringImpl> createInternal(const CharType*, unsigned);
774 WTF_EXPORT_PRIVATE NEVER_INLINE unsigned hashSlowCase() const;
775 WTF_EXPORT_PRIVATE static unsigned hashAndFlagsForUnique(unsigned flags);
777 // The bottom bit in the ref count indicates a static (immortal) string.
778 static const unsigned s_refCountFlagIsStaticString = 0x1;
779 static const unsigned s_refCountIncrement = 0x2; // This allows us to ref / deref without disturbing the static string flag.
781 // The bottom 7 bits in the hash are flags.
782 static const unsigned s_flagCount = 7;
783 static const unsigned s_flagMask = (1u << s_flagCount) - 1;
784 COMPILE_ASSERT(s_flagCount <= StringHasher::flagCount, StringHasher_reserves_enough_bits_for_StringImpl_flags);
786 static const unsigned s_hashFlagIsUnique = 1u << 6;
787 static const unsigned s_hashFlag8BitBuffer = 1u << 5;
788 static const unsigned s_hashFlagIsAtomic = 1u << 4;
789 static const unsigned s_hashFlagDidReportCost = 1u << 3;
790 static const unsigned s_hashMaskBufferOwnership = 1u | (1u << 1);
793 WTF_EXPORTDATA static StringStats m_stringStats;
797 struct StaticASCIILiteral {
798 // These member variables must match the layout of StringImpl.
801 const LChar* m_data8;
802 unsigned m_hashAndFlags;
804 // These values mimic ConstructFromLiteral.
805 static const unsigned s_initialRefCount = s_refCountIncrement;
806 static const unsigned s_initialFlags = s_hashFlag8BitBuffer | BufferInternal;
807 static const unsigned s_hashShift = s_flagCount;
811 void assertHashIsCorrect()
814 ASSERT(existingHash() == StringHasher::computeHashAndMaskTop8Bits(characters8(), length()));
819 // These member variables must match the layout of StaticASCIILiteral.
823 const LChar* m_data8;
824 const UChar* m_data16;
826 mutable unsigned m_hashAndFlags;
829 static_assert(sizeof(StringImpl) == sizeof(StringImpl::StaticASCIILiteral), "");
832 // StringImpls created from StaticASCIILiteral will ASSERT
833 // in the generic ValueCheck<T>::checkConsistency
834 // as they are not allocated by fastMalloc.
835 // We don't currently have any way to detect that case
836 // so we ignore the consistency check for all StringImpl*.
838 ValueCheck<StringImpl*> {
839 static void checkConsistency(const StringImpl*) { }
844 ALWAYS_INLINE Ref<StringImpl> StringImpl::constructInternal<LChar>(StringImpl* impl, unsigned length) { return adoptRef(*new (NotNull, impl) StringImpl(length, Force8BitConstructor)); }
846 ALWAYS_INLINE Ref<StringImpl> StringImpl::constructInternal<UChar>(StringImpl* impl, unsigned length) { return adoptRef(*new (NotNull, impl) StringImpl(length)); }
849 ALWAYS_INLINE const LChar* StringImpl::characters<LChar>() const { return characters8(); }
852 ALWAYS_INLINE const UChar* StringImpl::characters<UChar>() const { return characters16(); }
854 WTF_EXPORT_STRING_API bool equal(const StringImpl*, const StringImpl*);
855 WTF_EXPORT_STRING_API bool equal(const StringImpl*, const LChar*);
856 inline bool equal(const StringImpl* a, const char* b) { return equal(a, reinterpret_cast<const LChar*>(b)); }
857 WTF_EXPORT_STRING_API bool equal(const StringImpl*, const LChar*, unsigned);
858 WTF_EXPORT_STRING_API bool equal(const StringImpl*, const UChar*, unsigned);
859 inline bool equal(const StringImpl* a, const char* b, unsigned length) { return equal(a, reinterpret_cast<const LChar*>(b), length); }
860 inline bool equal(const LChar* a, StringImpl* b) { return equal(b, a); }
861 inline bool equal(const char* a, StringImpl* b) { return equal(b, reinterpret_cast<const LChar*>(a)); }
862 WTF_EXPORT_STRING_API bool equal(const StringImpl& a, const StringImpl& b);
865 inline T loadUnaligned(const char* s)
869 memcpy(&tmp, s, sizeof(T));
872 // This may result in undefined behavior due to unaligned access.
873 return *reinterpret_cast<const T*>(s);
877 // Do comparisons 8 or 4 bytes-at-a-time on architectures where it's safe.
878 #if CPU(X86_64) || CPU(ARM64)
879 ALWAYS_INLINE bool equal(const LChar* aLChar, const LChar* bLChar, unsigned length)
881 unsigned dwordLength = length >> 3;
883 const char* a = reinterpret_cast<const char*>(aLChar);
884 const char* b = reinterpret_cast<const char*>(bLChar);
887 for (unsigned i = 0; i != dwordLength; ++i) {
888 if (loadUnaligned<uint64_t>(a) != loadUnaligned<uint64_t>(b))
891 a += sizeof(uint64_t);
892 b += sizeof(uint64_t);
897 if (loadUnaligned<uint32_t>(a) != loadUnaligned<uint32_t>(b))
900 a += sizeof(uint32_t);
901 b += sizeof(uint32_t);
905 if (loadUnaligned<uint16_t>(a) != loadUnaligned<uint16_t>(b))
908 a += sizeof(uint16_t);
909 b += sizeof(uint16_t);
912 if (length & 1 && (*reinterpret_cast<const LChar*>(a) != *reinterpret_cast<const LChar*>(b)))
918 ALWAYS_INLINE bool equal(const UChar* aUChar, const UChar* bUChar, unsigned length)
920 unsigned dwordLength = length >> 2;
922 const char* a = reinterpret_cast<const char*>(aUChar);
923 const char* b = reinterpret_cast<const char*>(bUChar);
926 for (unsigned i = 0; i != dwordLength; ++i) {
927 if (loadUnaligned<uint64_t>(a) != loadUnaligned<uint64_t>(b))
930 a += sizeof(uint64_t);
931 b += sizeof(uint64_t);
936 if (loadUnaligned<uint32_t>(a) != loadUnaligned<uint32_t>(b))
939 a += sizeof(uint32_t);
940 b += sizeof(uint32_t);
943 if (length & 1 && (*reinterpret_cast<const UChar*>(a) != *reinterpret_cast<const UChar*>(b)))
949 ALWAYS_INLINE bool equal(const LChar* aLChar, const LChar* bLChar, unsigned length)
951 const char* a = reinterpret_cast<const char*>(aLChar);
952 const char* b = reinterpret_cast<const char*>(bLChar);
954 unsigned wordLength = length >> 2;
955 for (unsigned i = 0; i != wordLength; ++i) {
956 if (loadUnaligned<uint32_t>(a) != loadUnaligned<uint32_t>(b))
958 a += sizeof(uint32_t);
959 b += sizeof(uint32_t);
965 const LChar* aRemainder = reinterpret_cast<const LChar*>(a);
966 const LChar* bRemainder = reinterpret_cast<const LChar*>(b);
968 for (unsigned i = 0; i < length; ++i) {
969 if (aRemainder[i] != bRemainder[i])
977 ALWAYS_INLINE bool equal(const UChar* aUChar, const UChar* bUChar, unsigned length)
979 const char* a = reinterpret_cast<const char*>(aUChar);
980 const char* b = reinterpret_cast<const char*>(bUChar);
982 unsigned wordLength = length >> 1;
983 for (unsigned i = 0; i != wordLength; ++i) {
984 if (loadUnaligned<uint32_t>(a) != loadUnaligned<uint32_t>(b))
986 a += sizeof(uint32_t);
987 b += sizeof(uint32_t);
990 if (length & 1 && *reinterpret_cast<const UChar*>(a) != *reinterpret_cast<const UChar*>(b))
995 #elif PLATFORM(IOS) && WTF_ARM_ARCH_AT_LEAST(7)
996 ALWAYS_INLINE bool equal(const LChar* a, const LChar* b, unsigned length)
998 bool isEqual = false;
1001 asm("subs %[length], #4\n"
1004 "0:\n" // Label 0 = Start of loop over 32 bits.
1005 "ldr %[aValue], [%[a]], #4\n"
1006 "ldr %[bValue], [%[b]], #4\n"
1007 "cmp %[aValue], %[bValue]\n"
1009 "subs %[length], #4\n"
1012 // At this point, length can be:
1013 // -0: 00000000000000000000000000000000 (0 bytes left)
1014 // -1: 11111111111111111111111111111111 (3 bytes left)
1015 // -2: 11111111111111111111111111111110 (2 bytes left)
1016 // -3: 11111111111111111111111111111101 (1 byte left)
1017 // -4: 11111111111111111111111111111100 (length was 0)
1018 // The pointers are at the correct position.
1019 "2:\n" // Label 2 = End of loop over 32 bits, check for pair of characters.
1020 "tst %[length], #2\n"
1022 "ldrh %[aValue], [%[a]], #2\n"
1023 "ldrh %[bValue], [%[b]], #2\n"
1024 "cmp %[aValue], %[bValue]\n"
1027 "1:\n" // Label 1 = Check for a single character left.
1028 "tst %[length], #1\n"
1030 "ldrb %[aValue], [%[a]]\n"
1031 "ldrb %[bValue], [%[b]]\n"
1032 "cmp %[aValue], %[bValue]\n"
1035 "42:\n" // Label 42 = Success.
1036 "mov %[isEqual], #1\n"
1037 "66:\n" // Label 66 = End without changing isEqual to 1.
1038 : [length]"+r"(length), [isEqual]"+r"(isEqual), [a]"+r"(a), [b]"+r"(b), [aValue]"+r"(aValue), [bValue]"+r"(bValue)
1045 ALWAYS_INLINE bool equal(const UChar* a, const UChar* b, unsigned length)
1047 bool isEqual = false;
1050 asm("subs %[length], #2\n"
1053 "0:\n" // Label 0 = Start of loop over 32 bits.
1054 "ldr %[aValue], [%[a]], #4\n"
1055 "ldr %[bValue], [%[b]], #4\n"
1056 "cmp %[aValue], %[bValue]\n"
1058 "subs %[length], #2\n"
1061 // At this point, length can be:
1062 // -0: 00000000000000000000000000000000 (0 bytes left)
1063 // -1: 11111111111111111111111111111111 (1 character left, 2 bytes)
1064 // -2: 11111111111111111111111111111110 (length was zero)
1065 // The pointers are at the correct position.
1066 "1:\n" // Label 1 = Check for a single character left.
1067 "tst %[length], #1\n"
1069 "ldrh %[aValue], [%[a]]\n"
1070 "ldrh %[bValue], [%[b]]\n"
1071 "cmp %[aValue], %[bValue]\n"
1074 "42:\n" // Label 42 = Success.
1075 "mov %[isEqual], #1\n"
1076 "66:\n" // Label 66 = End without changing isEqual to 1.
1077 : [length]"+r"(length), [isEqual]"+r"(isEqual), [a]"+r"(a), [b]"+r"(b), [aValue]"+r"(aValue), [bValue]"+r"(bValue)
1084 ALWAYS_INLINE bool equal(const LChar* a, const LChar* b, unsigned length) { return !memcmp(a, b, length); }
1085 ALWAYS_INLINE bool equal(const UChar* a, const UChar* b, unsigned length) { return !memcmp(a, b, length * sizeof(UChar)); }
1088 ALWAYS_INLINE bool equal(const LChar* a, const UChar* b, unsigned length)
1090 for (unsigned i = 0; i < length; ++i) {
1097 ALWAYS_INLINE bool equal(const UChar* a, const LChar* b, unsigned length) { return equal(b, a, length); }
1099 WTF_EXPORT_STRING_API bool equalIgnoringCase(const StringImpl*, const StringImpl*);
1100 WTF_EXPORT_STRING_API bool equalIgnoringCase(const StringImpl*, const LChar*);
1101 inline bool equalIgnoringCase(const LChar* a, const StringImpl* b) { return equalIgnoringCase(b, a); }
1102 WTF_EXPORT_STRING_API bool equalIgnoringCase(const LChar*, const LChar*, unsigned);
1103 WTF_EXPORT_STRING_API bool equalIgnoringCase(const UChar*, const LChar*, unsigned);
1104 inline bool equalIgnoringCase(const UChar* a, const char* b, unsigned length) { return equalIgnoringCase(a, reinterpret_cast<const LChar*>(b), length); }
1105 inline bool equalIgnoringCase(const LChar* a, const UChar* b, unsigned length) { return equalIgnoringCase(b, a, length); }
1106 inline bool equalIgnoringCase(const char* a, const UChar* b, unsigned length) { return equalIgnoringCase(b, reinterpret_cast<const LChar*>(a), length); }
1107 inline bool equalIgnoringCase(const char* a, const LChar* b, unsigned length) { return equalIgnoringCase(b, reinterpret_cast<const LChar*>(a), length); }
1108 inline bool equalIgnoringCase(const UChar* a, const UChar* b, int length)
1110 ASSERT(length >= 0);
1111 return !u_memcasecmp(a, b, length, U_FOLD_CASE_DEFAULT);
1113 WTF_EXPORT_STRING_API bool equalIgnoringCaseNonNull(const StringImpl*, const StringImpl*);
1115 WTF_EXPORT_STRING_API bool equalIgnoringNullity(StringImpl*, StringImpl*);
1116 WTF_EXPORT_STRING_API bool equalIgnoringNullity(const UChar*, size_t length, StringImpl*);
1118 template<typename CharacterType>
1119 inline size_t find(const CharacterType* characters, unsigned length, CharacterType matchCharacter, unsigned index = 0)
1121 while (index < length) {
1122 if (characters[index] == matchCharacter)
1129 ALWAYS_INLINE size_t find(const UChar* characters, unsigned length, LChar matchCharacter, unsigned index = 0)
1131 return find(characters, length, static_cast<UChar>(matchCharacter), index);
1134 inline size_t find(const LChar* characters, unsigned length, UChar matchCharacter, unsigned index = 0)
1136 if (matchCharacter & ~0xFF)
1138 return find(characters, length, static_cast<LChar>(matchCharacter), index);
1141 inline size_t find(const LChar* characters, unsigned length, CharacterMatchFunctionPtr matchFunction, unsigned index = 0)
1143 while (index < length) {
1144 if (matchFunction(characters[index]))
1151 inline size_t find(const UChar* characters, unsigned length, CharacterMatchFunctionPtr matchFunction, unsigned index = 0)
1153 while (index < length) {
1154 if (matchFunction(characters[index]))
1161 template<typename CharacterType>
1162 inline size_t findNextLineStart(const CharacterType* characters, unsigned length, unsigned index = 0)
1164 while (index < length) {
1165 CharacterType c = characters[index++];
1166 if ((c != '\n') && (c != '\r'))
1169 // There can only be a start of a new line if there are more characters
1170 // beyond the current character.
1171 if (index < length) {
1172 // The 3 common types of line terminators are 1. \r\n (Windows),
1173 // 2. \r (old MacOS) and 3. \n (Unix'es).
1176 return index; // Case 3: just \n.
1178 CharacterType c2 = characters[index];
1180 return index; // Case 2: just \r.
1183 // But, there's only a start of a new line if there are more
1184 // characters beyond the \r\n.
1185 if (++index < length)
1192 template<typename CharacterType>
1193 inline size_t reverseFindLineTerminator(const CharacterType* characters, unsigned length, unsigned index = UINT_MAX)
1197 if (index >= length)
1199 CharacterType c = characters[index];
1200 while ((c != '\n') && (c != '\r')) {
1203 c = characters[index];
1208 template<typename CharacterType>
1209 inline size_t reverseFind(const CharacterType* characters, unsigned length, CharacterType matchCharacter, unsigned index = UINT_MAX)
1213 if (index >= length)
1215 while (characters[index] != matchCharacter) {
1222 ALWAYS_INLINE size_t reverseFind(const UChar* characters, unsigned length, LChar matchCharacter, unsigned index = UINT_MAX)
1224 return reverseFind(characters, length, static_cast<UChar>(matchCharacter), index);
1227 inline size_t reverseFind(const LChar* characters, unsigned length, UChar matchCharacter, unsigned index = UINT_MAX)
1229 if (matchCharacter & ~0xFF)
1231 return reverseFind(characters, length, static_cast<LChar>(matchCharacter), index);
1234 inline size_t StringImpl::find(LChar character, unsigned start)
1237 return WTF::find(characters8(), m_length, character, start);
1238 return WTF::find(characters16(), m_length, character, start);
1241 ALWAYS_INLINE size_t StringImpl::find(char character, unsigned start)
1243 return find(static_cast<LChar>(character), start);
1246 inline size_t StringImpl::find(UChar character, unsigned start)
1249 return WTF::find(characters8(), m_length, character, start);
1250 return WTF::find(characters16(), m_length, character, start);
1253 template<size_t inlineCapacity> inline bool equalIgnoringNullity(const Vector<UChar, inlineCapacity>& a, StringImpl* b)
1255 return equalIgnoringNullity(a.data(), a.size(), b);
1258 template<typename CharacterType1, typename CharacterType2>
1259 inline int codePointCompare(unsigned l1, unsigned l2, const CharacterType1* c1, const CharacterType2* c2)
1261 const unsigned lmin = l1 < l2 ? l1 : l2;
1263 while (pos < lmin && *c1 == *c2) {
1270 return (c1[0] > c2[0]) ? 1 : -1;
1275 return (l1 > l2) ? 1 : -1;
1278 inline int codePointCompare8(const StringImpl* string1, const StringImpl* string2)
1280 return codePointCompare(string1->length(), string2->length(), string1->characters8(), string2->characters8());
1283 inline int codePointCompare16(const StringImpl* string1, const StringImpl* string2)
1285 return codePointCompare(string1->length(), string2->length(), string1->characters16(), string2->characters16());
1288 inline int codePointCompare8To16(const StringImpl* string1, const StringImpl* string2)
1290 return codePointCompare(string1->length(), string2->length(), string1->characters8(), string2->characters16());
1293 inline int codePointCompare(const StringImpl* string1, const StringImpl* string2)
1296 return (string2 && string2->length()) ? -1 : 0;
1299 return string1->length() ? 1 : 0;
1301 bool string1Is8Bit = string1->is8Bit();
1302 bool string2Is8Bit = string2->is8Bit();
1303 if (string1Is8Bit) {
1305 return codePointCompare8(string1, string2);
1306 return codePointCompare8To16(string1, string2);
1309 return -codePointCompare8To16(string2, string1);
1310 return codePointCompare16(string1, string2);
1313 inline bool isSpaceOrNewline(UChar c)
1315 // Use isASCIISpace() for basic Latin-1.
1316 // This will include newlines, which aren't included in Unicode DirWS.
1317 return c <= 0x7F ? isASCIISpace(c) : u_charDirection(c) == U_WHITE_SPACE_NEUTRAL;
1320 template<typename CharacterType>
1321 inline unsigned lengthOfNullTerminatedString(const CharacterType* string)
1325 while (string[length])
1328 RELEASE_ASSERT(length < std::numeric_limits<unsigned>::max());
1329 return static_cast<unsigned>(length);
1332 inline Ref<StringImpl> StringImpl::isolatedCopy() const
1334 if (!requiresCopy()) {
1336 return StringImpl::createWithoutCopying(m_data8, m_length);
1337 return StringImpl::createWithoutCopying(m_data16, m_length);
1341 return create(m_data8, m_length);
1342 return create(m_data16, m_length);
1347 // StringHash is the default hash for StringImpl* and RefPtr<StringImpl>
1348 template<typename T> struct DefaultHash;
1349 template<> struct DefaultHash<StringImpl*> {
1350 typedef StringHash Hash;
1352 template<> struct DefaultHash<RefPtr<StringImpl>> {
1353 typedef StringHash Hash;
1358 using WTF::StringImpl;
1360 using WTF::TextCaseSensitivity;
1361 using WTF::TextCaseSensitive;
1362 using WTF::TextCaseInsensitive;