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>
34 #if PLATFORM(QT) && HAVE(QT5)
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 IdentifierCStringTranslator;
50 namespace LLInt { class Data; }
51 class LLIntOffsetsExtractor;
52 template <typename T> struct IdentifierCharBufferTranslator;
53 struct IdentifierLCharFromUCharTranslator;
58 struct CStringTranslator;
59 struct HashAndCharactersTranslator;
60 struct HashAndUTF8CharactersTranslator;
61 struct LCharBufferFromLiteralDataTranslator;
62 struct SubstringTranslator;
63 struct UCharBufferTranslator;
65 enum TextCaseSensitivity { TextCaseSensitive, TextCaseInsensitive };
67 typedef bool (*CharacterMatchFunctionPtr)(UChar);
68 typedef bool (*IsWhiteSpaceFunctionPtr)(UChar);
71 WTF_MAKE_NONCOPYABLE(StringImpl); WTF_MAKE_FAST_ALLOCATED;
72 friend struct JSC::IdentifierCStringTranslator;
73 friend struct JSC::IdentifierCharBufferTranslator<LChar>;
74 friend struct JSC::IdentifierCharBufferTranslator<UChar>;
75 friend struct JSC::IdentifierLCharFromUCharTranslator;
76 friend struct WTF::CStringTranslator;
77 friend struct WTF::HashAndCharactersTranslator;
78 friend struct WTF::HashAndUTF8CharactersTranslator;
79 friend struct WTF::LCharBufferFromLiteralDataTranslator;
80 friend struct WTF::SubstringTranslator;
81 friend struct WTF::UCharBufferTranslator;
82 friend class AtomicStringImpl;
83 friend class JSC::LLInt::Data;
84 friend class JSC::LLIntOffsetsExtractor;
87 enum BufferOwnership {
91 #if PLATFORM(QT) && HAVE(QT5)
94 // NOTE: Adding more ownership types needs to extend m_hashAndFlags as we're at capacity
97 // Used to construct static strings, which have an special refCount that can never hit zero.
98 // This means that the static string will never be destroyed, which is important because
99 // static strings will be shared across threads & ref-counted in a non-threadsafe manner.
100 enum ConstructStaticStringTag { ConstructStaticString };
101 StringImpl(const UChar* characters, unsigned length, ConstructStaticStringTag)
102 : m_refCount(s_refCountFlagIsStaticString)
104 , m_data16(characters)
106 , m_hashAndFlags(s_hashFlagIsIdentifier | BufferOwned)
108 // Ensure that the hash is computed so that AtomicStringHash can call existingHash()
109 // with impunity. The empty string is special because it is never entered into
110 // AtomicString's HashKey, but still needs to compare correctly.
114 // Used to construct static strings, which have an special refCount that can never hit zero.
115 // This means that the static string will never be destroyed, which is important because
116 // static strings will be shared across threads & ref-counted in a non-threadsafe manner.
117 StringImpl(const LChar* characters, unsigned length, ConstructStaticStringTag)
118 : m_refCount(s_refCountFlagIsStaticString)
120 , m_data8(characters)
122 , m_hashAndFlags(s_hashFlag8BitBuffer | s_hashFlagIsIdentifier | BufferOwned)
124 // Ensure that the hash is computed so that AtomicStringHash can call existingHash()
125 // with impunity. The empty string is special because it is never entered into
126 // AtomicString's HashKey, but still needs to compare correctly.
130 // FIXME: there has to be a less hacky way to do this.
131 enum Force8Bit { Force8BitConstructor };
132 // Create a normal 8-bit string with internal storage (BufferInternal)
133 StringImpl(unsigned length, Force8Bit)
134 : m_refCount(s_refCountIncrement)
136 , m_data8(reinterpret_cast<const LChar*>(this + 1))
138 , m_hashAndFlags(s_hashFlag8BitBuffer | BufferInternal)
144 // Create a normal 16-bit string with internal storage (BufferInternal)
145 StringImpl(unsigned length)
146 : m_refCount(s_refCountIncrement)
148 , m_data16(reinterpret_cast<const UChar*>(this + 1))
150 , m_hashAndFlags(BufferInternal)
156 // Create a StringImpl adopting ownership of the provided buffer (BufferOwned)
157 StringImpl(const LChar* characters, unsigned length)
158 : m_refCount(s_refCountIncrement)
160 , m_data8(characters)
162 , m_hashAndFlags(s_hashFlag8BitBuffer | BufferOwned)
168 // Create a StringImpl adopting ownership of the provided buffer (BufferOwned)
169 StringImpl(const UChar* characters, unsigned length)
170 : m_refCount(s_refCountIncrement)
172 , m_data16(characters)
174 , m_hashAndFlags(BufferOwned)
180 // Used to create new strings that are a substring of an existing 8-bit StringImpl (BufferSubstring)
181 StringImpl(const LChar* characters, unsigned length, PassRefPtr<StringImpl> base)
182 : m_refCount(s_refCountIncrement)
184 , m_data8(characters)
185 , m_substringBuffer(base.leakRef())
186 , m_hashAndFlags(s_hashFlag8BitBuffer | BufferSubstring)
191 ASSERT(m_substringBuffer->bufferOwnership() != BufferSubstring);
194 // Used to create new strings that are a substring of an existing 16-bit StringImpl (BufferSubstring)
195 StringImpl(const UChar* characters, unsigned length, PassRefPtr<StringImpl> base)
196 : m_refCount(s_refCountIncrement)
198 , m_data16(characters)
199 , m_substringBuffer(base.leakRef())
200 , m_hashAndFlags(BufferSubstring)
205 ASSERT(m_substringBuffer->bufferOwnership() != BufferSubstring);
208 enum CreateEmptyUnique_T { CreateEmptyUnique };
209 StringImpl(CreateEmptyUnique_T)
210 : m_refCount(s_refCountIncrement)
212 , m_data16(reinterpret_cast<const UChar*>(1))
216 // Set the hash early, so that all empty unique StringImpls have a hash,
217 // and don't use the normal hashing algorithm - the unique nature of these
218 // keys means that we don't need them to match any other string (in fact,
219 // that's exactly the oposite of what we want!), and teh normal hash would
220 // lead to lots of conflicts.
221 unsigned hash = reinterpret_cast<uintptr_t>(this);
222 hash <<= s_flagCount;
224 hash = 1 << s_flagCount;
225 m_hashAndFlags = hash | BufferInternal;
228 #if PLATFORM(QT) && HAVE(QT5)
229 // Used to create new strings that adopt an existing QString's data
230 enum ConstructAdoptedQStringTag { ConstructAdoptedQString };
231 StringImpl(QStringData* qStringData, ConstructAdoptedQStringTag)
232 : m_refCount(s_refCountIncrement)
233 , m_length(qStringData->size)
235 , m_qStringData(qStringData)
236 , m_hashAndFlags(BufferAdoptedQString)
240 // We ref the string-data to ensure it will be valid for the lifetime of
241 // this string. We then deref it in the destructor, so that the string
242 // data can eventually be freed.
243 m_qStringData->ref.ref();
245 // Now that we have a ref we can safely reference the string data
246 m_data16 = reinterpret_cast_ptr<const UChar*>(qStringData->data());
252 WTF_EXPORT_PRIVATE ~StringImpl();
254 WTF_EXPORT_PRIVATE static PassRefPtr<StringImpl> create(const UChar*, unsigned length);
255 static PassRefPtr<StringImpl> create(const LChar*, unsigned length);
256 ALWAYS_INLINE static PassRefPtr<StringImpl> create(const char* s, unsigned length) { return create(reinterpret_cast<const LChar*>(s), length); }
257 WTF_EXPORT_PRIVATE static PassRefPtr<StringImpl> create(const LChar*);
258 ALWAYS_INLINE static PassRefPtr<StringImpl> create(const char* s) { return create(reinterpret_cast<const LChar*>(s)); }
260 static ALWAYS_INLINE PassRefPtr<StringImpl> create8(PassRefPtr<StringImpl> rep, unsigned offset, unsigned length)
263 ASSERT(length <= rep->length());
268 ASSERT(rep->is8Bit());
269 StringImpl* ownerRep = (rep->bufferOwnership() == BufferSubstring) ? rep->m_substringBuffer : rep.get();
270 return adoptRef(new StringImpl(rep->m_data8 + offset, length, ownerRep));
273 static ALWAYS_INLINE PassRefPtr<StringImpl> create(PassRefPtr<StringImpl> rep, unsigned offset, unsigned length)
276 ASSERT(length <= rep->length());
281 StringImpl* ownerRep = (rep->bufferOwnership() == BufferSubstring) ? rep->m_substringBuffer : rep.get();
283 return adoptRef(new StringImpl(rep->m_data8 + offset, length, ownerRep));
284 return adoptRef(new StringImpl(rep->m_data16 + offset, length, ownerRep));
287 WTF_EXPORT_PRIVATE static PassRefPtr<StringImpl> createFromLiteral(const LChar* characters, unsigned length);
288 template<unsigned charactersCount>
289 ALWAYS_INLINE static PassRefPtr<StringImpl> createFromLiteral(const char (&characters)[charactersCount])
291 COMPILE_ASSERT(charactersCount > 1, StringImplFromLiteralNotEmpty);
292 COMPILE_ASSERT((charactersCount - 1 <= ((unsigned(~0) - sizeof(StringImpl)) / sizeof(LChar))), StringImplFromLiteralCannotOverflow);
294 return createFromLiteral(reinterpret_cast<const LChar*>(characters), charactersCount - 1);
297 WTF_EXPORT_PRIVATE static PassRefPtr<StringImpl> createUninitialized(unsigned length, LChar*& data);
298 WTF_EXPORT_PRIVATE static PassRefPtr<StringImpl> createUninitialized(unsigned length, UChar*& data);
299 template <typename T> static ALWAYS_INLINE PassRefPtr<StringImpl> tryCreateUninitialized(unsigned length, T*& output)
306 if (length > ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / sizeof(T))) {
310 StringImpl* resultImpl;
311 if (!tryFastMalloc(sizeof(T) * length + sizeof(StringImpl)).getValue(resultImpl)) {
315 output = reinterpret_cast<T*>(resultImpl + 1);
317 if (sizeof(T) == sizeof(char))
318 return adoptRef(new (NotNull, resultImpl) StringImpl(length, Force8BitConstructor));
320 return adoptRef(new (NotNull, resultImpl) StringImpl(length));
323 static PassRefPtr<StringImpl> createEmptyUnique()
325 return adoptRef(new StringImpl(CreateEmptyUnique));
328 // Reallocate the StringImpl. The originalString must be only owned by the PassRefPtr,
329 // and the buffer ownership must be BufferInternal. Just like the input pointer of realloc(),
330 // the originalString can't be used after this function.
331 static PassRefPtr<StringImpl> reallocate(PassRefPtr<StringImpl> originalString, unsigned length, LChar*& data);
332 static PassRefPtr<StringImpl> reallocate(PassRefPtr<StringImpl> originalString, unsigned length, UChar*& data);
334 static unsigned flagsOffset() { return OBJECT_OFFSETOF(StringImpl, m_hashAndFlags); }
335 static unsigned flagIs8Bit() { return s_hashFlag8BitBuffer; }
336 static unsigned dataOffset() { return OBJECT_OFFSETOF(StringImpl, m_data8); }
337 static PassRefPtr<StringImpl> createWithTerminatingNullCharacter(const StringImpl&);
339 template<typename CharType, size_t inlineCapacity>
340 static PassRefPtr<StringImpl> adopt(Vector<CharType, inlineCapacity>& vector)
342 if (size_t size = vector.size()) {
343 ASSERT(vector.data());
344 if (size > std::numeric_limits<unsigned>::max())
346 return adoptRef(new StringImpl(vector.releaseBuffer(), size));
351 WTF_EXPORT_PRIVATE static PassRefPtr<StringImpl> adopt(StringBuffer<UChar>&);
352 WTF_EXPORT_PRIVATE static PassRefPtr<StringImpl> adopt(StringBuffer<LChar>&);
354 #if PLATFORM(QT) && HAVE(QT5)
355 static PassRefPtr<StringImpl> adopt(QStringData*);
358 unsigned length() const { return m_length; }
359 bool is8Bit() const { return m_hashAndFlags & s_hashFlag8BitBuffer; }
361 // FIXME: Remove all unnecessary usages of characters()
362 ALWAYS_INLINE const LChar* characters8() const { ASSERT(is8Bit()); return m_data8; }
363 ALWAYS_INLINE const UChar* characters16() const { ASSERT(!is8Bit()); return m_data16; }
364 ALWAYS_INLINE const UChar* characters() const
369 return getData16SlowCase();
372 template <typename CharType>
373 ALWAYS_INLINE const CharType * getCharacters() const;
377 // For substrings, return the cost of the base string.
378 if (bufferOwnership() == BufferSubstring)
379 return m_substringBuffer->cost();
381 if (m_hashAndFlags & s_hashFlagDidReportCost)
384 m_hashAndFlags |= s_hashFlagDidReportCost;
388 bool has16BitShadow() const { return m_hashAndFlags & s_hashFlagHas16BitShadow; }
389 WTF_EXPORT_PRIVATE void upconvertCharacters(unsigned, unsigned) const;
390 bool isIdentifier() const { return m_hashAndFlags & s_hashFlagIsIdentifier; }
391 void setIsIdentifier(bool isIdentifier)
395 m_hashAndFlags |= s_hashFlagIsIdentifier;
397 m_hashAndFlags &= ~s_hashFlagIsIdentifier;
400 bool isEmptyUnique() const
402 return !length() && !isStatic();
405 bool hasTerminatingNullCharacter() const { return m_hashAndFlags & s_hashFlagHasTerminatingNullCharacter; }
407 bool isAtomic() const { return m_hashAndFlags & s_hashFlagIsAtomic; }
408 void setIsAtomic(bool isIdentifier)
412 m_hashAndFlags |= s_hashFlagIsAtomic;
414 m_hashAndFlags &= ~s_hashFlagIsAtomic;
417 #if PLATFORM(QT) && HAVE(QT5)
418 QStringData* qStringData() { return bufferOwnership() == BufferAdoptedQString ? m_qStringData : 0; }
422 // The high bits of 'hash' are always empty, but we prefer to store our flags
423 // in the low bits because it makes them slightly more efficient to access.
424 // So, we shift left and right when setting and getting our hash code.
425 void setHash(unsigned hash) const
428 // Multiple clients assume that StringHasher is the canonical string hash function.
429 ASSERT(hash == (is8Bit() ? StringHasher::computeHash(m_data8, m_length) : StringHasher::computeHash(m_data16, m_length)));
430 ASSERT(!(hash & (s_flagMask << (8 * sizeof(hash) - s_flagCount)))); // Verify that enough high bits are empty.
432 hash <<= s_flagCount;
433 ASSERT(!(hash & m_hashAndFlags)); // Verify that enough low bits are empty after shift.
434 ASSERT(hash); // Verify that 0 is a valid sentinel hash value.
436 m_hashAndFlags |= hash; // Store hash with flags in low bits.
439 unsigned rawHash() const
441 return m_hashAndFlags >> s_flagCount;
447 return rawHash() != 0;
450 unsigned existingHash() const
456 unsigned hash() const
459 return existingHash();
460 return hashSlowCase();
463 inline bool hasOneRef() const
465 return m_refCount == s_refCountIncrement;
470 m_refCount += s_refCountIncrement;
475 if (m_refCount == s_refCountIncrement) {
480 m_refCount -= s_refCountIncrement;
483 WTF_EXPORT_PRIVATE static StringImpl* empty();
485 // FIXME: Does this really belong in StringImpl?
486 template <typename T> static void copyChars(T* destination, const T* source, unsigned numCharacters)
488 if (numCharacters == 1) {
489 *destination = *source;
493 if (numCharacters <= s_copyCharsInlineCutOff) {
495 #if (CPU(X86) || CPU(X86_64))
496 const unsigned charsPerInt = sizeof(uint32_t) / sizeof(T);
498 if (numCharacters > charsPerInt) {
499 unsigned stopCount = numCharacters & ~(charsPerInt - 1);
501 const uint32_t* srcCharacters = reinterpret_cast<const uint32_t*>(source);
502 uint32_t* destCharacters = reinterpret_cast<uint32_t*>(destination);
503 for (unsigned j = 0; i < stopCount; i += charsPerInt, ++j)
504 destCharacters[j] = srcCharacters[j];
507 for (; i < numCharacters; ++i)
508 destination[i] = source[i];
510 memcpy(destination, source, numCharacters * sizeof(T));
513 // Some string features, like refcounting and the atomicity flag, are not
514 // thread-safe. We achieve thread safety by isolation, giving each thread
515 // its own copy of the string.
516 PassRefPtr<StringImpl> isolatedCopy() const;
518 WTF_EXPORT_PRIVATE PassRefPtr<StringImpl> substring(unsigned pos, unsigned len = UINT_MAX);
520 UChar operator[](unsigned i) const
522 ASSERT(i < m_length);
527 WTF_EXPORT_PRIVATE UChar32 characterStartingAt(unsigned);
529 WTF_EXPORT_PRIVATE bool containsOnlyWhitespace();
531 int toIntStrict(bool* ok = 0, int base = 10);
532 unsigned toUIntStrict(bool* ok = 0, int base = 10);
533 int64_t toInt64Strict(bool* ok = 0, int base = 10);
534 uint64_t toUInt64Strict(bool* ok = 0, int base = 10);
535 intptr_t toIntPtrStrict(bool* ok = 0, int base = 10);
537 WTF_EXPORT_PRIVATE int toInt(bool* ok = 0); // ignores trailing garbage
538 unsigned toUInt(bool* ok = 0); // ignores trailing garbage
539 int64_t toInt64(bool* ok = 0); // ignores trailing garbage
540 uint64_t toUInt64(bool* ok = 0); // ignores trailing garbage
541 intptr_t toIntPtr(bool* ok = 0); // ignores trailing garbage
543 // FIXME: Like the strict functions above, these give false for "ok" when there is trailing garbage.
544 // Like the non-strict functions above, these return the value when there is trailing garbage.
545 // It would be better if these were more consistent with the above functions instead.
546 double toDouble(bool* ok = 0);
547 float toFloat(bool* ok = 0);
549 WTF_EXPORT_PRIVATE PassRefPtr<StringImpl> lower();
550 WTF_EXPORT_PRIVATE PassRefPtr<StringImpl> upper();
552 WTF_EXPORT_PRIVATE PassRefPtr<StringImpl> fill(UChar);
553 // FIXME: Do we need fill(char) or can we just do the right thing if UChar is ASCII?
554 PassRefPtr<StringImpl> foldCase();
556 PassRefPtr<StringImpl> stripWhiteSpace();
557 PassRefPtr<StringImpl> stripWhiteSpace(IsWhiteSpaceFunctionPtr);
558 WTF_EXPORT_PRIVATE PassRefPtr<StringImpl> simplifyWhiteSpace();
559 PassRefPtr<StringImpl> simplifyWhiteSpace(IsWhiteSpaceFunctionPtr);
561 PassRefPtr<StringImpl> removeCharacters(CharacterMatchFunctionPtr);
562 template <typename CharType>
563 ALWAYS_INLINE PassRefPtr<StringImpl> removeCharacters(const CharType* characters, CharacterMatchFunctionPtr);
565 size_t find(LChar character, unsigned start = 0);
566 size_t find(char character, unsigned start = 0);
567 size_t find(UChar character, unsigned start = 0);
568 WTF_EXPORT_PRIVATE size_t find(CharacterMatchFunctionPtr, unsigned index = 0);
569 size_t find(const LChar*, unsigned index = 0);
570 ALWAYS_INLINE size_t find(const char* s, unsigned index = 0) { return find(reinterpret_cast<const LChar*>(s), index); };
571 WTF_EXPORT_PRIVATE size_t find(StringImpl*);
572 WTF_EXPORT_PRIVATE size_t find(StringImpl*, unsigned index);
573 size_t findIgnoringCase(const LChar*, unsigned index = 0);
574 ALWAYS_INLINE size_t findIgnoringCase(const char* s, unsigned index = 0) { return findIgnoringCase(reinterpret_cast<const LChar*>(s), index); };
575 WTF_EXPORT_PRIVATE size_t findIgnoringCase(StringImpl*, unsigned index = 0);
577 WTF_EXPORT_PRIVATE size_t reverseFind(UChar, unsigned index = UINT_MAX);
578 WTF_EXPORT_PRIVATE size_t reverseFind(StringImpl*, unsigned index = UINT_MAX);
579 WTF_EXPORT_PRIVATE size_t reverseFindIgnoringCase(StringImpl*, unsigned index = UINT_MAX);
581 bool startsWith(StringImpl* str, bool caseSensitive = true) { return (caseSensitive ? reverseFind(str, 0) : reverseFindIgnoringCase(str, 0)) == 0; }
582 WTF_EXPORT_PRIVATE bool startsWith(UChar) const;
583 WTF_EXPORT_PRIVATE bool startsWith(const char*, unsigned matchLength, bool caseSensitive) const;
584 template<unsigned matchLength>
585 bool startsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const { return startsWith(prefix, matchLength - 1, caseSensitive); };
587 WTF_EXPORT_PRIVATE bool endsWith(StringImpl*, bool caseSensitive = true);
588 WTF_EXPORT_PRIVATE bool endsWith(UChar) const;
589 WTF_EXPORT_PRIVATE bool endsWith(const char*, unsigned matchLength, bool caseSensitive) const;
590 template<unsigned matchLength>
591 bool endsWith(const char (&prefix)[matchLength], bool caseSensitive = true) const { return endsWith(prefix, matchLength - 1, caseSensitive); }
593 WTF_EXPORT_PRIVATE PassRefPtr<StringImpl> replace(UChar, UChar);
594 WTF_EXPORT_PRIVATE PassRefPtr<StringImpl> replace(UChar, StringImpl*);
595 WTF_EXPORT_PRIVATE PassRefPtr<StringImpl> replace(StringImpl*, StringImpl*);
596 ALWAYS_INLINE PassRefPtr<StringImpl> replace(UChar pattern, const char* replacement, unsigned replacementLength) { return replace(pattern, reinterpret_cast<const LChar*>(replacement), replacementLength); }
597 WTF_EXPORT_PRIVATE PassRefPtr<StringImpl> replace(UChar, const LChar*, unsigned replacementLength);
598 PassRefPtr<StringImpl> replace(UChar, const UChar*, unsigned replacementLength);
599 WTF_EXPORT_PRIVATE PassRefPtr<StringImpl> replace(unsigned index, unsigned len, StringImpl*);
601 WTF_EXPORT_PRIVATE WTF::Unicode::Direction defaultWritingDirection(bool* hasStrongDirectionality = 0);
604 CFStringRef createCFString();
607 operator NSString*();
611 // This number must be at least 2 to avoid sharing empty, null as well as 1 character strings from SmallStrings.
612 static const unsigned s_copyCharsInlineCutOff = 20;
614 BufferOwnership bufferOwnership() const { return static_cast<BufferOwnership>(m_hashAndFlags & s_hashMaskBufferOwnership); }
615 bool isStatic() const { return m_refCount & s_refCountFlagIsStaticString; }
616 template <class UCharPredicate> PassRefPtr<StringImpl> stripMatchedCharacters(UCharPredicate);
617 template <typename CharType, class UCharPredicate> PassRefPtr<StringImpl> simplifyMatchedCharactersToSpace(UCharPredicate);
618 WTF_EXPORT_PRIVATE NEVER_INLINE const UChar* getData16SlowCase() const;
619 WTF_EXPORT_PRIVATE NEVER_INLINE unsigned hashSlowCase() const;
621 // The bottom bit in the ref count indicates a static (immortal) string.
622 static const unsigned s_refCountFlagIsStaticString = 0x1;
623 static const unsigned s_refCountIncrement = 0x2; // This allows us to ref / deref without disturbing the static string flag.
625 // The bottom 8 bits in the hash are flags.
626 static const unsigned s_flagCount = 8;
627 static const unsigned s_flagMask = (1u << s_flagCount) - 1;
628 COMPILE_ASSERT(s_flagCount == StringHasher::flagCount, StringHasher_reserves_enough_bits_for_StringImpl_flags);
630 static const unsigned s_hashFlagHas16BitShadow = 1u << 7;
631 static const unsigned s_hashFlag8BitBuffer = 1u << 6;
632 static const unsigned s_hashFlagHasTerminatingNullCharacter = 1u << 5;
633 static const unsigned s_hashFlagIsAtomic = 1u << 4;
634 static const unsigned s_hashFlagDidReportCost = 1u << 3;
635 static const unsigned s_hashFlagIsIdentifier = 1u << 2;
636 static const unsigned s_hashMaskBufferOwnership = 1u | (1u << 1);
641 const LChar* m_data8;
642 const UChar* m_data16;
646 StringImpl* m_substringBuffer;
647 mutable UChar* m_copyData16;
648 #if PLATFORM(QT) && HAVE(QT5)
649 QStringData* m_qStringData;
652 mutable unsigned m_hashAndFlags;
656 ALWAYS_INLINE const LChar* StringImpl::getCharacters<LChar>() const { return characters8(); }
659 ALWAYS_INLINE const UChar* StringImpl::getCharacters<UChar>() const { return characters(); }
661 WTF_EXPORT_PRIVATE bool equal(const StringImpl*, const StringImpl*);
662 WTF_EXPORT_PRIVATE bool equal(const StringImpl*, const LChar*);
663 inline bool equal(const StringImpl* a, const char* b) { return equal(a, reinterpret_cast<const LChar*>(b)); }
664 WTF_EXPORT_PRIVATE bool equal(const StringImpl*, const LChar*, unsigned);
665 inline bool equal(const StringImpl* a, const char* b, unsigned length) { return equal(a, reinterpret_cast<const LChar*>(b), length); }
666 inline bool equal(const LChar* a, StringImpl* b) { return equal(b, a); }
667 inline bool equal(const char* a, StringImpl* b) { return equal(b, reinterpret_cast<const LChar*>(a)); }
668 WTF_EXPORT_PRIVATE bool equal(const StringImpl*, const UChar*, unsigned);
670 // Do comparisons 8 or 4 bytes-at-a-time on architectures where it's safe.
672 ALWAYS_INLINE bool equal(const LChar* a, const LChar* b, unsigned length)
674 unsigned dwordLength = length >> 3;
677 const uint64_t* aDWordCharacters = reinterpret_cast<const uint64_t*>(a);
678 const uint64_t* bDWordCharacters = reinterpret_cast<const uint64_t*>(b);
680 for (unsigned i = 0; i != dwordLength; ++i) {
681 if (*aDWordCharacters++ != *bDWordCharacters++)
685 a = reinterpret_cast<const LChar*>(aDWordCharacters);
686 b = reinterpret_cast<const LChar*>(bDWordCharacters);
690 if (*reinterpret_cast<const uint32_t*>(a) != *reinterpret_cast<const uint32_t*>(b))
698 if (*reinterpret_cast<const uint16_t*>(a) != *reinterpret_cast<const uint16_t*>(b))
705 if (length & 1 && (*a != *b))
711 ALWAYS_INLINE bool equal(const UChar* a, const UChar* b, unsigned length)
713 unsigned dwordLength = length >> 2;
716 const uint64_t* aDWordCharacters = reinterpret_cast<const uint64_t*>(a);
717 const uint64_t* bDWordCharacters = reinterpret_cast<const uint64_t*>(b);
719 for (unsigned i = 0; i != dwordLength; ++i) {
720 if (*aDWordCharacters++ != *bDWordCharacters++)
724 a = reinterpret_cast<const UChar*>(aDWordCharacters);
725 b = reinterpret_cast<const UChar*>(bDWordCharacters);
729 if (*reinterpret_cast<const uint32_t*>(a) != *reinterpret_cast<const uint32_t*>(b))
736 if (length & 1 && (*a != *b))
742 ALWAYS_INLINE bool equal(const LChar* a, const LChar* b, unsigned length)
744 const uint32_t* aCharacters = reinterpret_cast<const uint32_t*>(a);
745 const uint32_t* bCharacters = reinterpret_cast<const uint32_t*>(b);
747 unsigned wordLength = length >> 2;
748 for (unsigned i = 0; i != wordLength; ++i) {
749 if (*aCharacters++ != *bCharacters++)
756 const LChar* aRemainder = reinterpret_cast<const LChar*>(aCharacters);
757 const LChar* bRemainder = reinterpret_cast<const LChar*>(bCharacters);
759 for (unsigned i = 0; i < length; ++i) {
760 if (aRemainder[i] != bRemainder[i])
768 ALWAYS_INLINE bool equal(const UChar* a, const UChar* b, unsigned length)
770 const uint32_t* aCharacters = reinterpret_cast<const uint32_t*>(a);
771 const uint32_t* bCharacters = reinterpret_cast<const uint32_t*>(b);
773 unsigned wordLength = length >> 1;
774 for (unsigned i = 0; i != wordLength; ++i) {
775 if (*aCharacters++ != *bCharacters++)
779 if (length & 1 && *reinterpret_cast<const UChar*>(aCharacters) != *reinterpret_cast<const UChar*>(bCharacters))
785 ALWAYS_INLINE bool equal(const LChar* a, const LChar* b, unsigned length)
787 for (unsigned i = 0; i != length; ++i) {
795 ALWAYS_INLINE bool equal(const UChar* a, const UChar* b, unsigned length)
797 for (unsigned i = 0; i != length; ++i) {
806 ALWAYS_INLINE bool equal(const LChar* a, const UChar* b, unsigned length)
808 for (unsigned i = 0; i != length; ++i) {
816 ALWAYS_INLINE bool equal(const UChar* a, const LChar* b, unsigned length)
818 for (unsigned i = 0; i != length; ++i) {
826 WTF_EXPORT_PRIVATE bool equalIgnoringCase(StringImpl*, StringImpl*);
827 WTF_EXPORT_PRIVATE bool equalIgnoringCase(StringImpl*, const LChar*);
828 inline bool equalIgnoringCase(const LChar* a, StringImpl* b) { return equalIgnoringCase(b, a); }
829 WTF_EXPORT_PRIVATE bool equalIgnoringCase(const LChar*, const LChar*, unsigned);
830 WTF_EXPORT_PRIVATE bool equalIgnoringCase(const UChar*, const LChar*, unsigned);
831 inline bool equalIgnoringCase(const UChar* a, const char* b, unsigned length) { return equalIgnoringCase(a, reinterpret_cast<const LChar*>(b), length); }
832 inline bool equalIgnoringCase(const LChar* a, const UChar* b, unsigned length) { return equalIgnoringCase(b, a, length); }
833 inline bool equalIgnoringCase(const char* a, const UChar* b, unsigned length) { return equalIgnoringCase(b, reinterpret_cast<const LChar*>(a), length); }
835 WTF_EXPORT_PRIVATE bool equalIgnoringNullity(StringImpl*, StringImpl*);
837 template<typename CharacterType>
838 inline size_t find(const CharacterType* characters, unsigned length, CharacterType matchCharacter, unsigned index = 0)
840 while (index < length) {
841 if (characters[index] == matchCharacter)
848 ALWAYS_INLINE size_t find(const UChar* characters, unsigned length, LChar matchCharacter, unsigned index = 0)
850 return find(characters, length, static_cast<UChar>(matchCharacter), index);
853 inline size_t find(const LChar* characters, unsigned length, UChar matchCharacter, unsigned index = 0)
855 if (matchCharacter & ~0xFF)
857 return find(characters, length, static_cast<LChar>(matchCharacter), index);
860 inline size_t find(const LChar* characters, unsigned length, CharacterMatchFunctionPtr matchFunction, unsigned index = 0)
862 while (index < length) {
863 if (matchFunction(characters[index]))
870 inline size_t find(const UChar* characters, unsigned length, CharacterMatchFunctionPtr matchFunction, unsigned index = 0)
872 while (index < length) {
873 if (matchFunction(characters[index]))
880 inline size_t reverseFind(const LChar* characters, unsigned length, LChar matchCharacter, unsigned index = UINT_MAX)
886 while (characters[index] != matchCharacter) {
893 inline size_t reverseFind(const UChar* characters, unsigned length, UChar matchCharacter, unsigned index = UINT_MAX)
899 while (characters[index] != matchCharacter) {
906 inline size_t StringImpl::find(LChar character, unsigned start)
909 return WTF::find(characters8(), m_length, character, start);
910 return WTF::find(characters16(), m_length, character, start);
913 ALWAYS_INLINE size_t StringImpl::find(char character, unsigned start)
915 return find(static_cast<LChar>(character), start);
918 inline size_t StringImpl::find(UChar character, unsigned start)
921 return WTF::find(characters8(), m_length, character, start);
922 return WTF::find(characters16(), m_length, character, start);
925 template<size_t inlineCapacity>
926 bool equalIgnoringNullity(const Vector<UChar, inlineCapacity>& a, StringImpl* b)
930 if (a.size() != b->length())
932 return !memcmp(a.data(), b->characters(), b->length() * sizeof(UChar));
935 template<typename CharacterType1, typename CharacterType2>
936 static inline int codePointCompare(unsigned l1, unsigned l2, const CharacterType1* c1, const CharacterType2* c2)
938 const unsigned lmin = l1 < l2 ? l1 : l2;
940 while (pos < lmin && *c1 == *c2) {
947 return (c1[0] > c2[0]) ? 1 : -1;
952 return (l1 > l2) ? 1 : -1;
955 static inline int codePointCompare8(const StringImpl* string1, const StringImpl* string2)
957 return codePointCompare(string1->length(), string2->length(), string1->characters8(), string2->characters8());
960 static inline int codePointCompare16(const StringImpl* string1, const StringImpl* string2)
962 return codePointCompare(string1->length(), string2->length(), string1->characters16(), string2->characters16());
965 static inline int codePointCompare8To16(const StringImpl* string1, const StringImpl* string2)
967 return codePointCompare(string1->length(), string2->length(), string1->characters8(), string2->characters16());
970 static inline int codePointCompare(const StringImpl* string1, const StringImpl* string2)
973 return (string2 && string2->length()) ? -1 : 0;
976 return string1->length() ? 1 : 0;
978 bool string1Is8Bit = string1->is8Bit();
979 bool string2Is8Bit = string2->is8Bit();
982 return codePointCompare8(string1, string2);
983 return codePointCompare8To16(string1, string2);
986 return -codePointCompare8To16(string2, string1);
987 return codePointCompare16(string1, string2);
990 static inline bool isSpaceOrNewline(UChar c)
992 // Use isASCIISpace() for basic Latin-1.
993 // This will include newlines, which aren't included in Unicode DirWS.
994 return c <= 0x7F ? WTF::isASCIISpace(c) : WTF::Unicode::direction(c) == WTF::Unicode::WhiteSpaceNeutral;
997 inline PassRefPtr<StringImpl> StringImpl::isolatedCopy() const
1000 return create(m_data8, m_length);
1001 return create(m_data16, m_length);
1006 // StringHash is the default hash for StringImpl* and RefPtr<StringImpl>
1007 template<typename T> struct DefaultHash;
1008 template<> struct DefaultHash<StringImpl*> {
1009 typedef StringHash Hash;
1011 template<> struct DefaultHash<RefPtr<StringImpl> > {
1012 typedef StringHash Hash;
1017 using WTF::StringImpl;
1019 using WTF::TextCaseSensitivity;
1020 using WTF::TextCaseSensitive;
1021 using WTF::TextCaseInsensitive;