2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 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 <wtf/ASCIICType.h>
28 #include <wtf/Forward.h>
29 #include <wtf/StdLibExtras.h>
30 #include <wtf/StringHasher.h>
31 #include <wtf/Vector.h>
32 #include <wtf/unicode/Unicode.h>
39 typedef const struct __CFString * CFStringRef;
46 // FIXME: This is a temporary layering violation while we move string code to WTF.
47 // Landing the file moves in one patch, will follow on with patches to change the namespaces.
49 struct IdentifierASCIIStringTranslator;
50 namespace LLInt { class Data; }
51 class LLIntOffsetsExtractor;
52 template <typename T> struct IdentifierCharBufferTranslator;
53 struct IdentifierLCharFromUCharTranslator;
58 struct CStringTranslator;
59 template<typename CharacterType> struct HashAndCharactersTranslator;
60 struct HashAndUTF8CharactersTranslator;
61 struct LCharBufferTranslator;
62 struct CharBufferFromLiteralDataTranslator;
63 struct SubstringTranslator;
64 struct UCharBufferTranslator;
66 enum TextCaseSensitivity { TextCaseSensitive, TextCaseInsensitive };
68 typedef bool (*CharacterMatchFunctionPtr)(UChar);
69 typedef bool (*IsWhiteSpaceFunctionPtr)(UChar);
72 WTF_MAKE_NONCOPYABLE(StringImpl); WTF_MAKE_FAST_ALLOCATED;
73 friend struct JSC::IdentifierASCIIStringTranslator;
74 friend struct JSC::IdentifierCharBufferTranslator<LChar>;
75 friend struct JSC::IdentifierCharBufferTranslator<UChar>;
76 friend struct JSC::IdentifierLCharFromUCharTranslator;
77 friend struct WTF::CStringTranslator;
78 template<typename CharacterType> friend struct WTF::HashAndCharactersTranslator;
79 friend struct WTF::HashAndUTF8CharactersTranslator;
80 friend struct WTF::CharBufferFromLiteralDataTranslator;
81 friend struct WTF::LCharBufferTranslator;
82 friend struct WTF::SubstringTranslator;
83 friend struct WTF::UCharBufferTranslator;
84 friend class AtomicStringImpl;
85 friend class JSC::LLInt::Data;
86 friend class JSC::LLIntOffsetsExtractor;
89 enum BufferOwnership {
96 // NOTE: Adding more ownership types needs to extend m_hashAndFlags as we're at capacity
99 // Used to construct static strings, which have an special refCount that can never hit zero.
100 // This means that the static string will never be destroyed, which is important because
101 // static strings will be shared across threads & ref-counted in a non-threadsafe manner.
102 enum ConstructStaticStringTag { ConstructStaticString };
103 StringImpl(const UChar* characters, unsigned length, ConstructStaticStringTag)
104 : m_refCount(s_refCountFlagIsStaticString)
106 , m_data16(characters)
108 , m_hashAndFlags(s_hashFlagIsIdentifier | BufferOwned)
110 // Ensure that the hash is computed so that AtomicStringHash can call existingHash()
111 // with impunity. The empty string is special because it is never entered into
112 // AtomicString's HashKey, but still needs to compare correctly.
116 // Used to construct static strings, which have an special refCount that can never hit zero.
117 // This means that the static string will never be destroyed, which is important because
118 // static strings will be shared across threads & ref-counted in a non-threadsafe manner.
119 StringImpl(const LChar* characters, unsigned length, ConstructStaticStringTag)
120 : m_refCount(s_refCountFlagIsStaticString)
122 , m_data8(characters)
124 , m_hashAndFlags(s_hashFlag8BitBuffer | s_hashFlagIsIdentifier | BufferOwned)
126 // Ensure that the hash is computed so that AtomicStringHash can call existingHash()
127 // with impunity. The empty string is special because it is never entered into
128 // AtomicString's HashKey, but still needs to compare correctly.
132 // FIXME: there has to be a less hacky way to do this.
133 enum Force8Bit { Force8BitConstructor };
134 // Create a normal 8-bit string with internal storage (BufferInternal)
135 StringImpl(unsigned length, Force8Bit)
136 : m_refCount(s_refCountIncrement)
138 , m_data8(reinterpret_cast<const LChar*>(this + 1))
140 , m_hashAndFlags(s_hashFlag8BitBuffer | BufferInternal)
146 // Create a normal 16-bit string with internal storage (BufferInternal)
147 StringImpl(unsigned length)
148 : m_refCount(s_refCountIncrement)
150 , m_data16(reinterpret_cast<const UChar*>(this + 1))
152 , m_hashAndFlags(BufferInternal)
158 // Create a StringImpl adopting ownership of the provided buffer (BufferOwned)
159 StringImpl(const LChar* characters, unsigned length)
160 : m_refCount(s_refCountIncrement)
162 , m_data8(characters)
164 , m_hashAndFlags(s_hashFlag8BitBuffer | BufferOwned)
170 enum ConstructFromLiteralTag { ConstructFromLiteral };
171 StringImpl(const char* characters, unsigned length, ConstructFromLiteralTag)
172 : m_refCount(s_refCountIncrement)
174 , m_data8(reinterpret_cast<const LChar*>(characters))
176 , m_hashAndFlags(s_hashFlag8BitBuffer | BufferInternal | s_hashFlagHasTerminatingNullCharacter)
180 ASSERT(!characters[length]);
183 // Create a StringImpl adopting ownership of the provided buffer (BufferOwned)
184 StringImpl(const UChar* characters, unsigned length)
185 : m_refCount(s_refCountIncrement)
187 , m_data16(characters)
189 , m_hashAndFlags(BufferOwned)
195 // Used to create new strings that are a substring of an existing 8-bit StringImpl (BufferSubstring)
196 StringImpl(const LChar* characters, unsigned length, PassRefPtr<StringImpl> base)
197 : m_refCount(s_refCountIncrement)
199 , m_data8(characters)
200 , m_substringBuffer(base.leakRef())
201 , m_hashAndFlags(s_hashFlag8BitBuffer | BufferSubstring)
206 ASSERT(m_substringBuffer->bufferOwnership() != BufferSubstring);
209 // Used to create new strings that are a substring of an existing 16-bit StringImpl (BufferSubstring)
210 StringImpl(const UChar* characters, unsigned length, PassRefPtr<StringImpl> base)
211 : m_refCount(s_refCountIncrement)
213 , m_data16(characters)
214 , m_substringBuffer(base.leakRef())
215 , m_hashAndFlags(BufferSubstring)
220 ASSERT(m_substringBuffer->bufferOwnership() != BufferSubstring);
223 enum CreateEmptyUnique_T { CreateEmptyUnique };
224 StringImpl(CreateEmptyUnique_T)
225 : m_refCount(s_refCountIncrement)
227 , m_data16(reinterpret_cast<const UChar*>(1))
231 // Set the hash early, so that all empty unique StringImpls have a hash,
232 // and don't use the normal hashing algorithm - the unique nature of these
233 // keys means that we don't need them to match any other string (in fact,
234 // that's exactly the oposite of what we want!), and teh normal hash would
235 // lead to lots of conflicts.
236 unsigned hash = reinterpret_cast<uintptr_t>(this);
237 hash <<= s_flagCount;
239 hash = 1 << s_flagCount;
240 m_hashAndFlags = hash | BufferInternal;
244 // Used to create new strings that adopt an existing QString's data
245 enum ConstructAdoptedQStringTag { ConstructAdoptedQString };
246 StringImpl(QStringData* qStringData, ConstructAdoptedQStringTag)
247 : m_refCount(s_refCountIncrement)
248 , m_length(qStringData->size)
250 , m_qStringData(qStringData)
251 , m_hashAndFlags(BufferAdoptedQString)
255 // We ref the string-data to ensure it will be valid for the lifetime of
256 // this string. We then deref it in the destructor, so that the string
257 // data can eventually be freed.
258 m_qStringData->ref.ref();
260 // Now that we have a ref we can safely reference the string data
261 m_data16 = reinterpret_cast_ptr<const UChar*>(qStringData->data());
267 WTF_EXPORT_STRING_API ~StringImpl();
269 WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> create(const UChar*, unsigned length);
270 static PassRefPtr<StringImpl> create(const LChar*, unsigned length);
271 ALWAYS_INLINE static PassRefPtr<StringImpl> create(const char* s, unsigned length) { return create(reinterpret_cast<const LChar*>(s), length); }
272 WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> create(const LChar*);
273 ALWAYS_INLINE static PassRefPtr<StringImpl> create(const char* s) { return create(reinterpret_cast<const LChar*>(s)); }
275 static ALWAYS_INLINE PassRefPtr<StringImpl> create8(PassRefPtr<StringImpl> rep, unsigned offset, unsigned length)
278 ASSERT(length <= rep->length());
283 ASSERT(rep->is8Bit());
284 StringImpl* ownerRep = (rep->bufferOwnership() == BufferSubstring) ? rep->m_substringBuffer : rep.get();
285 return adoptRef(new StringImpl(rep->m_data8 + offset, length, ownerRep));
288 static ALWAYS_INLINE PassRefPtr<StringImpl> create(PassRefPtr<StringImpl> rep, unsigned offset, unsigned length)
291 ASSERT(length <= rep->length());
296 StringImpl* ownerRep = (rep->bufferOwnership() == BufferSubstring) ? rep->m_substringBuffer : rep.get();
298 return adoptRef(new StringImpl(rep->m_data8 + offset, length, ownerRep));
299 return adoptRef(new StringImpl(rep->m_data16 + offset, length, ownerRep));
302 WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> createFromLiteral(const char* characters, unsigned length);
303 template<unsigned charactersCount>
304 ALWAYS_INLINE static PassRefPtr<StringImpl> createFromLiteral(const char (&characters)[charactersCount])
306 COMPILE_ASSERT(charactersCount > 1, StringImplFromLiteralNotEmpty);
307 COMPILE_ASSERT((charactersCount - 1 <= ((unsigned(~0) - sizeof(StringImpl)) / sizeof(LChar))), StringImplFromLiteralCannotOverflow);
309 return createFromLiteral(characters, charactersCount - 1);
311 WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> createFromLiteral(const char* characters);
313 WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> createUninitialized(unsigned length, LChar*& data);
314 WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> createUninitialized(unsigned length, UChar*& data);
315 template <typename T> static ALWAYS_INLINE PassRefPtr<StringImpl> tryCreateUninitialized(unsigned length, T*& output)
322 if (length > ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / sizeof(T))) {
326 StringImpl* resultImpl;
327 if (!tryFastMalloc(sizeof(T) * length + sizeof(StringImpl)).getValue(resultImpl)) {
331 output = reinterpret_cast<T*>(resultImpl + 1);
333 if (sizeof(T) == sizeof(char))
334 return adoptRef(new (NotNull, resultImpl) StringImpl(length, Force8BitConstructor));
336 return adoptRef(new (NotNull, resultImpl) StringImpl(length));
339 static PassRefPtr<StringImpl> createEmptyUnique()
341 return adoptRef(new StringImpl(CreateEmptyUnique));
344 // Reallocate the StringImpl. The originalString must be only owned by the PassRefPtr,
345 // and the buffer ownership must be BufferInternal. Just like the input pointer of realloc(),
346 // the originalString can't be used after this function.
347 static PassRefPtr<StringImpl> reallocate(PassRefPtr<StringImpl> originalString, unsigned length, LChar*& data);
348 static PassRefPtr<StringImpl> reallocate(PassRefPtr<StringImpl> originalString, unsigned length, UChar*& data);
350 static unsigned flagsOffset() { return OBJECT_OFFSETOF(StringImpl, m_hashAndFlags); }
351 static unsigned flagIs8Bit() { return s_hashFlag8BitBuffer; }
352 static unsigned dataOffset() { return OBJECT_OFFSETOF(StringImpl, m_data8); }
353 static PassRefPtr<StringImpl> createWithTerminatingNullCharacter(const StringImpl&);
355 template<typename CharType, size_t inlineCapacity>
356 static PassRefPtr<StringImpl> adopt(Vector<CharType, inlineCapacity>& vector)
358 if (size_t size = vector.size()) {
359 ASSERT(vector.data());
360 if (size > std::numeric_limits<unsigned>::max())
362 return adoptRef(new StringImpl(vector.releaseBuffer(), size));
367 WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> adopt(StringBuffer<UChar>&);
368 WTF_EXPORT_STRING_API static PassRefPtr<StringImpl> adopt(StringBuffer<LChar>&);
371 static PassRefPtr<StringImpl> adopt(QStringData*);
374 unsigned length() const { return m_length; }
375 bool is8Bit() const { return m_hashAndFlags & s_hashFlag8BitBuffer; }
377 // FIXME: Remove all unnecessary usages of characters()
378 ALWAYS_INLINE const LChar* characters8() const { ASSERT(is8Bit()); return m_data8; }
379 ALWAYS_INLINE const UChar* characters16() const { ASSERT(!is8Bit()); return m_data16; }
380 ALWAYS_INLINE const UChar* characters() const
385 return getData16SlowCase();
388 template <typename CharType>
389 ALWAYS_INLINE const CharType * getCharacters() const;
393 // For substrings, return the cost of the base string.
394 if (bufferOwnership() == BufferSubstring)
395 return m_substringBuffer->cost();
397 if (m_hashAndFlags & s_hashFlagDidReportCost)
400 m_hashAndFlags |= s_hashFlagDidReportCost;
404 WTF_EXPORT_STRING_API size_t sizeInBytes() const;
406 bool has16BitShadow() const { return m_hashAndFlags & s_hashFlagHas16BitShadow; }
407 WTF_EXPORT_STRING_API void upconvertCharacters(unsigned, unsigned) const;
408 bool isIdentifier() const { return m_hashAndFlags & s_hashFlagIsIdentifier; }
409 void setIsIdentifier(bool isIdentifier)
413 m_hashAndFlags |= s_hashFlagIsIdentifier;
415 m_hashAndFlags &= ~s_hashFlagIsIdentifier;
418 bool isEmptyUnique() const
420 return !length() && !isStatic();
423 bool hasTerminatingNullCharacter() const { return m_hashAndFlags & s_hashFlagHasTerminatingNullCharacter; }
425 bool isAtomic() const { return m_hashAndFlags & s_hashFlagIsAtomic; }
426 void setIsAtomic(bool isIdentifier)
430 m_hashAndFlags |= s_hashFlagIsAtomic;
432 m_hashAndFlags &= ~s_hashFlagIsAtomic;
436 QStringData* qStringData() { return bufferOwnership() == BufferAdoptedQString ? m_qStringData : 0; }
440 // The high bits of 'hash' are always empty, but we prefer to store our flags
441 // in the low bits because it makes them slightly more efficient to access.
442 // So, we shift left and right when setting and getting our hash code.
443 void setHash(unsigned hash) const
446 // Multiple clients assume that StringHasher is the canonical string hash function.
447 ASSERT(hash == (is8Bit() ? StringHasher::computeHashAndMaskTop8Bits(m_data8, m_length) : StringHasher::computeHashAndMaskTop8Bits(m_data16, m_length)));
448 ASSERT(!(hash & (s_flagMask << (8 * sizeof(hash) - s_flagCount)))); // Verify that enough high bits are empty.
450 hash <<= s_flagCount;
451 ASSERT(!(hash & m_hashAndFlags)); // Verify that enough low bits are empty after shift.
452 ASSERT(hash); // Verify that 0 is a valid sentinel hash value.
454 m_hashAndFlags |= hash; // Store hash with flags in low bits.
457 unsigned rawHash() const
459 return m_hashAndFlags >> s_flagCount;
465 return rawHash() != 0;
468 unsigned existingHash() const
474 unsigned hash() const
477 return existingHash();
478 return hashSlowCase();
481 inline bool hasOneRef() const
483 return m_refCount == s_refCountIncrement;
488 m_refCount += s_refCountIncrement;
493 if (m_refCount == s_refCountIncrement) {
498 m_refCount -= s_refCountIncrement;
501 WTF_EXPORT_PRIVATE static StringImpl* empty();
503 // FIXME: Does this really belong in StringImpl?
504 template <typename T> static void copyChars(T* destination, const T* source, unsigned numCharacters)
506 if (numCharacters == 1) {
507 *destination = *source;
511 if (numCharacters <= s_copyCharsInlineCutOff) {
513 #if (CPU(X86) || CPU(X86_64))
514 const unsigned charsPerInt = sizeof(uint32_t) / sizeof(T);
516 if (numCharacters > charsPerInt) {
517 unsigned stopCount = numCharacters & ~(charsPerInt - 1);
519 const uint32_t* srcCharacters = reinterpret_cast<const uint32_t*>(source);
520 uint32_t* destCharacters = reinterpret_cast<uint32_t*>(destination);
521 for (unsigned j = 0; i < stopCount; i += charsPerInt, ++j)
522 destCharacters[j] = srcCharacters[j];
525 for (; i < numCharacters; ++i)
526 destination[i] = source[i];
528 memcpy(destination, source, numCharacters * sizeof(T));
531 // Some string features, like refcounting and the atomicity flag, are not
532 // thread-safe. We achieve thread safety by isolation, giving each thread
533 // its own copy of the string.
534 PassRefPtr<StringImpl> isolatedCopy() const;
536 WTF_EXPORT_STRING_API PassRefPtr<StringImpl> substring(unsigned pos, unsigned len = UINT_MAX);
538 UChar operator[](unsigned i) const
540 ASSERT(i < m_length);
545 WTF_EXPORT_STRING_API UChar32 characterStartingAt(unsigned);
547 WTF_EXPORT_STRING_API bool containsOnlyWhitespace();
549 int toIntStrict(bool* ok = 0, int base = 10);
550 unsigned toUIntStrict(bool* ok = 0, int base = 10);
551 int64_t toInt64Strict(bool* ok = 0, int base = 10);
552 uint64_t toUInt64Strict(bool* ok = 0, int base = 10);
553 intptr_t toIntPtrStrict(bool* ok = 0, int base = 10);
555 WTF_EXPORT_STRING_API int toInt(bool* ok = 0); // ignores trailing garbage
556 unsigned toUInt(bool* ok = 0); // ignores trailing garbage
557 int64_t toInt64(bool* ok = 0); // ignores trailing garbage
558 uint64_t toUInt64(bool* ok = 0); // ignores trailing garbage
559 intptr_t toIntPtr(bool* ok = 0); // ignores trailing garbage
561 // FIXME: Like the strict functions above, these give false for "ok" when there is trailing garbage.
562 // Like the non-strict functions above, these return the value when there is trailing garbage.
563 // It would be better if these were more consistent with the above functions instead.
564 double toDouble(bool* ok = 0);
565 float toFloat(bool* ok = 0);
567 WTF_EXPORT_STRING_API PassRefPtr<StringImpl> lower();
568 WTF_EXPORT_STRING_API PassRefPtr<StringImpl> upper();
570 WTF_EXPORT_STRING_API PassRefPtr<StringImpl> fill(UChar);
571 // FIXME: Do we need fill(char) or can we just do the right thing if UChar is ASCII?
572 PassRefPtr<StringImpl> foldCase();
574 PassRefPtr<StringImpl> stripWhiteSpace();
575 PassRefPtr<StringImpl> stripWhiteSpace(IsWhiteSpaceFunctionPtr);
576 WTF_EXPORT_STRING_API PassRefPtr<StringImpl> simplifyWhiteSpace();
577 PassRefPtr<StringImpl> simplifyWhiteSpace(IsWhiteSpaceFunctionPtr);
579 PassRefPtr<StringImpl> removeCharacters(CharacterMatchFunctionPtr);
580 template <typename CharType>
581 ALWAYS_INLINE PassRefPtr<StringImpl> removeCharacters(const CharType* characters, CharacterMatchFunctionPtr);
583 size_t find(LChar character, unsigned start = 0);
584 size_t find(char character, unsigned start = 0);
585 size_t find(UChar character, unsigned start = 0);
586 WTF_EXPORT_STRING_API size_t find(CharacterMatchFunctionPtr, unsigned index = 0);
587 size_t find(const LChar*, unsigned index = 0);
588 ALWAYS_INLINE size_t find(const char* s, unsigned index = 0) { return find(reinterpret_cast<const LChar*>(s), index); };
589 WTF_EXPORT_STRING_API size_t find(StringImpl*);
590 WTF_EXPORT_STRING_API size_t find(StringImpl*, unsigned index);
591 size_t findIgnoringCase(const LChar*, unsigned index = 0);
592 ALWAYS_INLINE size_t findIgnoringCase(const char* s, unsigned index = 0) { return findIgnoringCase(reinterpret_cast<const LChar*>(s), index); };
593 WTF_EXPORT_STRING_API size_t findIgnoringCase(StringImpl*, unsigned index = 0);
595 WTF_EXPORT_STRING_API size_t reverseFind(UChar, unsigned index = UINT_MAX);
596 WTF_EXPORT_STRING_API size_t reverseFind(StringImpl*, unsigned index = UINT_MAX);
597 WTF_EXPORT_STRING_API size_t reverseFindIgnoringCase(StringImpl*, unsigned index = UINT_MAX);
599 bool startsWith(StringImpl* str, bool caseSensitive = true) { return (caseSensitive ? reverseFind(str, 0) : reverseFindIgnoringCase(str, 0)) == 0; }
600 WTF_EXPORT_STRING_API bool startsWith(UChar) const;
601 WTF_EXPORT_STRING_API bool startsWith(const char*, unsigned matchLength, bool caseSensitive) const;
602 template<unsigned matchLength>
603 bool startsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const { return startsWith(prefix, matchLength - 1, caseSensitive); };
605 WTF_EXPORT_STRING_API bool endsWith(StringImpl*, bool caseSensitive = true);
606 WTF_EXPORT_STRING_API bool endsWith(UChar) const;
607 WTF_EXPORT_STRING_API bool endsWith(const char*, unsigned matchLength, bool caseSensitive) const;
608 template<unsigned matchLength>
609 bool endsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const { return endsWith(prefix, matchLength - 1, caseSensitive); }
611 WTF_EXPORT_STRING_API PassRefPtr<StringImpl> replace(UChar, UChar);
612 WTF_EXPORT_STRING_API PassRefPtr<StringImpl> replace(UChar, StringImpl*);
613 WTF_EXPORT_STRING_API PassRefPtr<StringImpl> replace(StringImpl*, StringImpl*);
614 WTF_EXPORT_STRING_API PassRefPtr<StringImpl> replace(unsigned index, unsigned len, StringImpl*);
616 WTF_EXPORT_STRING_API WTF::Unicode::Direction defaultWritingDirection(bool* hasStrongDirectionality = 0);
619 CFStringRef createCFString();
622 operator NSString*();
626 // This number must be at least 2 to avoid sharing empty, null as well as 1 character strings from SmallStrings.
627 static const unsigned s_copyCharsInlineCutOff = 20;
629 BufferOwnership bufferOwnership() const { return static_cast<BufferOwnership>(m_hashAndFlags & s_hashMaskBufferOwnership); }
630 bool isStatic() const { return m_refCount & s_refCountFlagIsStaticString; }
631 template <class UCharPredicate> PassRefPtr<StringImpl> stripMatchedCharacters(UCharPredicate);
632 template <typename CharType, class UCharPredicate> PassRefPtr<StringImpl> simplifyMatchedCharactersToSpace(UCharPredicate);
633 WTF_EXPORT_STRING_API NEVER_INLINE const UChar* getData16SlowCase() const;
634 WTF_EXPORT_PRIVATE NEVER_INLINE unsigned hashSlowCase() const;
636 // The bottom bit in the ref count indicates a static (immortal) string.
637 static const unsigned s_refCountFlagIsStaticString = 0x1;
638 static const unsigned s_refCountIncrement = 0x2; // This allows us to ref / deref without disturbing the static string flag.
640 // The bottom 8 bits in the hash are flags.
641 static const unsigned s_flagCount = 8;
642 static const unsigned s_flagMask = (1u << s_flagCount) - 1;
643 COMPILE_ASSERT(s_flagCount == StringHasher::flagCount, StringHasher_reserves_enough_bits_for_StringImpl_flags);
645 static const unsigned s_hashFlagHas16BitShadow = 1u << 7;
646 static const unsigned s_hashFlag8BitBuffer = 1u << 6;
647 static const unsigned s_hashFlagHasTerminatingNullCharacter = 1u << 5;
648 static const unsigned s_hashFlagIsAtomic = 1u << 4;
649 static const unsigned s_hashFlagDidReportCost = 1u << 3;
650 static const unsigned s_hashFlagIsIdentifier = 1u << 2;
651 static const unsigned s_hashMaskBufferOwnership = 1u | (1u << 1);
656 const LChar* m_data8;
657 const UChar* m_data16;
661 StringImpl* m_substringBuffer;
662 mutable UChar* m_copyData16;
664 QStringData* m_qStringData;
667 mutable unsigned m_hashAndFlags;
671 ALWAYS_INLINE const LChar* StringImpl::getCharacters<LChar>() const { return characters8(); }
674 ALWAYS_INLINE const UChar* StringImpl::getCharacters<UChar>() const { return characters(); }
676 WTF_EXPORT_STRING_API bool equal(const StringImpl*, const StringImpl*);
677 WTF_EXPORT_STRING_API bool equal(const StringImpl*, const LChar*);
678 inline bool equal(const StringImpl* a, const char* b) { return equal(a, reinterpret_cast<const LChar*>(b)); }
679 WTF_EXPORT_STRING_API bool equal(const StringImpl*, const LChar*, unsigned);
680 inline bool equal(const StringImpl* a, const char* b, unsigned length) { return equal(a, reinterpret_cast<const LChar*>(b), length); }
681 inline bool equal(const LChar* a, StringImpl* b) { return equal(b, a); }
682 inline bool equal(const char* a, StringImpl* b) { return equal(b, reinterpret_cast<const LChar*>(a)); }
683 WTF_EXPORT_STRING_API bool equal(const StringImpl*, const UChar*, unsigned);
685 // Do comparisons 8 or 4 bytes-at-a-time on architectures where it's safe.
687 ALWAYS_INLINE bool equal(const LChar* a, const LChar* b, unsigned length)
689 unsigned dwordLength = length >> 3;
692 const uint64_t* aDWordCharacters = reinterpret_cast<const uint64_t*>(a);
693 const uint64_t* bDWordCharacters = reinterpret_cast<const uint64_t*>(b);
695 for (unsigned i = 0; i != dwordLength; ++i) {
696 if (*aDWordCharacters++ != *bDWordCharacters++)
700 a = reinterpret_cast<const LChar*>(aDWordCharacters);
701 b = reinterpret_cast<const LChar*>(bDWordCharacters);
705 if (*reinterpret_cast<const uint32_t*>(a) != *reinterpret_cast<const uint32_t*>(b))
713 if (*reinterpret_cast<const uint16_t*>(a) != *reinterpret_cast<const uint16_t*>(b))
720 if (length & 1 && (*a != *b))
726 ALWAYS_INLINE bool equal(const UChar* a, const UChar* b, unsigned length)
728 unsigned dwordLength = length >> 2;
731 const uint64_t* aDWordCharacters = reinterpret_cast<const uint64_t*>(a);
732 const uint64_t* bDWordCharacters = reinterpret_cast<const uint64_t*>(b);
734 for (unsigned i = 0; i != dwordLength; ++i) {
735 if (*aDWordCharacters++ != *bDWordCharacters++)
739 a = reinterpret_cast<const UChar*>(aDWordCharacters);
740 b = reinterpret_cast<const UChar*>(bDWordCharacters);
744 if (*reinterpret_cast<const uint32_t*>(a) != *reinterpret_cast<const uint32_t*>(b))
751 if (length & 1 && (*a != *b))
757 ALWAYS_INLINE bool equal(const LChar* a, const LChar* b, unsigned length)
759 const uint32_t* aCharacters = reinterpret_cast<const uint32_t*>(a);
760 const uint32_t* bCharacters = reinterpret_cast<const uint32_t*>(b);
762 unsigned wordLength = length >> 2;
763 for (unsigned i = 0; i != wordLength; ++i) {
764 if (*aCharacters++ != *bCharacters++)
771 const LChar* aRemainder = reinterpret_cast<const LChar*>(aCharacters);
772 const LChar* bRemainder = reinterpret_cast<const LChar*>(bCharacters);
774 for (unsigned i = 0; i < length; ++i) {
775 if (aRemainder[i] != bRemainder[i])
783 ALWAYS_INLINE bool equal(const UChar* a, const UChar* b, unsigned length)
785 const uint32_t* aCharacters = reinterpret_cast<const uint32_t*>(a);
786 const uint32_t* bCharacters = reinterpret_cast<const uint32_t*>(b);
788 unsigned wordLength = length >> 1;
789 for (unsigned i = 0; i != wordLength; ++i) {
790 if (*aCharacters++ != *bCharacters++)
794 if (length & 1 && *reinterpret_cast<const UChar*>(aCharacters) != *reinterpret_cast<const UChar*>(bCharacters))
800 ALWAYS_INLINE bool equal(const LChar* a, const LChar* b, unsigned length)
802 for (unsigned i = 0; i != length; ++i) {
810 ALWAYS_INLINE bool equal(const UChar* a, const UChar* b, unsigned length)
812 for (unsigned i = 0; i != length; ++i) {
821 ALWAYS_INLINE bool equal(const LChar* a, const UChar* b, unsigned length)
823 for (unsigned i = 0; i != length; ++i) {
831 ALWAYS_INLINE bool equal(const UChar* a, const LChar* b, unsigned length)
833 for (unsigned i = 0; i != length; ++i) {
841 WTF_EXPORT_STRING_API bool equalIgnoringCase(StringImpl*, StringImpl*);
842 WTF_EXPORT_STRING_API bool equalIgnoringCase(StringImpl*, const LChar*);
843 inline bool equalIgnoringCase(const LChar* a, StringImpl* b) { return equalIgnoringCase(b, a); }
844 WTF_EXPORT_STRING_API bool equalIgnoringCase(const LChar*, const LChar*, unsigned);
845 WTF_EXPORT_STRING_API bool equalIgnoringCase(const UChar*, const LChar*, unsigned);
846 inline bool equalIgnoringCase(const UChar* a, const char* b, unsigned length) { return equalIgnoringCase(a, reinterpret_cast<const LChar*>(b), length); }
847 inline bool equalIgnoringCase(const LChar* a, const UChar* b, unsigned length) { return equalIgnoringCase(b, a, length); }
848 inline bool equalIgnoringCase(const char* a, const UChar* b, unsigned length) { return equalIgnoringCase(b, reinterpret_cast<const LChar*>(a), length); }
850 WTF_EXPORT_STRING_API bool equalIgnoringNullity(StringImpl*, StringImpl*);
852 template<typename CharacterType>
853 inline size_t find(const CharacterType* characters, unsigned length, CharacterType matchCharacter, unsigned index = 0)
855 while (index < length) {
856 if (characters[index] == matchCharacter)
863 ALWAYS_INLINE size_t find(const UChar* characters, unsigned length, LChar matchCharacter, unsigned index = 0)
865 return find(characters, length, static_cast<UChar>(matchCharacter), index);
868 inline size_t find(const LChar* characters, unsigned length, UChar matchCharacter, unsigned index = 0)
870 if (matchCharacter & ~0xFF)
872 return find(characters, length, static_cast<LChar>(matchCharacter), index);
875 inline size_t find(const LChar* characters, unsigned length, CharacterMatchFunctionPtr matchFunction, unsigned index = 0)
877 while (index < length) {
878 if (matchFunction(characters[index]))
885 inline size_t find(const UChar* characters, unsigned length, CharacterMatchFunctionPtr matchFunction, unsigned index = 0)
887 while (index < length) {
888 if (matchFunction(characters[index]))
895 inline size_t reverseFind(const LChar* characters, unsigned length, LChar matchCharacter, unsigned index = UINT_MAX)
901 while (characters[index] != matchCharacter) {
908 inline size_t reverseFind(const UChar* characters, unsigned length, UChar matchCharacter, unsigned index = UINT_MAX)
914 while (characters[index] != matchCharacter) {
921 inline size_t StringImpl::find(LChar character, unsigned start)
924 return WTF::find(characters8(), m_length, character, start);
925 return WTF::find(characters16(), m_length, character, start);
928 ALWAYS_INLINE size_t StringImpl::find(char character, unsigned start)
930 return find(static_cast<LChar>(character), start);
933 inline size_t StringImpl::find(UChar character, unsigned start)
936 return WTF::find(characters8(), m_length, character, start);
937 return WTF::find(characters16(), m_length, character, start);
940 template<size_t inlineCapacity>
941 bool equalIgnoringNullity(const Vector<UChar, inlineCapacity>& a, StringImpl* b)
945 if (a.size() != b->length())
947 return !memcmp(a.data(), b->characters(), b->length() * sizeof(UChar));
950 template<typename CharacterType1, typename CharacterType2>
951 static inline int codePointCompare(unsigned l1, unsigned l2, const CharacterType1* c1, const CharacterType2* c2)
953 const unsigned lmin = l1 < l2 ? l1 : l2;
955 while (pos < lmin && *c1 == *c2) {
962 return (c1[0] > c2[0]) ? 1 : -1;
967 return (l1 > l2) ? 1 : -1;
970 static inline int codePointCompare8(const StringImpl* string1, const StringImpl* string2)
972 return codePointCompare(string1->length(), string2->length(), string1->characters8(), string2->characters8());
975 static inline int codePointCompare16(const StringImpl* string1, const StringImpl* string2)
977 return codePointCompare(string1->length(), string2->length(), string1->characters16(), string2->characters16());
980 static inline int codePointCompare8To16(const StringImpl* string1, const StringImpl* string2)
982 return codePointCompare(string1->length(), string2->length(), string1->characters8(), string2->characters16());
985 static inline int codePointCompare(const StringImpl* string1, const StringImpl* string2)
988 return (string2 && string2->length()) ? -1 : 0;
991 return string1->length() ? 1 : 0;
993 bool string1Is8Bit = string1->is8Bit();
994 bool string2Is8Bit = string2->is8Bit();
997 return codePointCompare8(string1, string2);
998 return codePointCompare8To16(string1, string2);
1001 return -codePointCompare8To16(string2, string1);
1002 return codePointCompare16(string1, string2);
1005 static inline bool isSpaceOrNewline(UChar c)
1007 // Use isASCIISpace() for basic Latin-1.
1008 // This will include newlines, which aren't included in Unicode DirWS.
1009 return c <= 0x7F ? WTF::isASCIISpace(c) : WTF::Unicode::direction(c) == WTF::Unicode::WhiteSpaceNeutral;
1012 inline PassRefPtr<StringImpl> StringImpl::isolatedCopy() const
1015 return create(m_data8, m_length);
1016 return create(m_data16, m_length);
1021 // StringHash is the default hash for StringImpl* and RefPtr<StringImpl>
1022 template<typename T> struct DefaultHash;
1023 template<> struct DefaultHash<StringImpl*> {
1024 typedef StringHash Hash;
1026 template<> struct DefaultHash<RefPtr<StringImpl> > {
1027 typedef StringHash Hash;
1032 using WTF::StringImpl;
1034 using WTF::TextCaseSensitivity;
1035 using WTF::TextCaseSensitive;
1036 using WTF::TextCaseInsensitive;