| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) | 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
| 3 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2013 Apple Inc. All rights | 3 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2013 Apple Inc. All rights |
| 4 * reserved. | 4 * reserved. |
| 5 * Copyright (C) 2009 Google Inc. All rights reserved. | 5 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 6 * | 6 * |
| 7 * This library is free software; you can redistribute it and/or | 7 * This library is free software; you can redistribute it and/or |
| 8 * modify it under the terms of the GNU Library General Public | 8 * modify it under the terms of the GNU Library General Public |
| 9 * License as published by the Free Software Foundation; either | 9 * License as published by the Free Software Foundation; either |
| 10 * version 2 of the License, or (at your option) any later version. | 10 * version 2 of the License, or (at your option) any later version. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 // features. | 62 // features. |
| 63 kTextCaseUnicodeInsensitive | 63 kTextCaseUnicodeInsensitive |
| 64 }; | 64 }; |
| 65 | 65 |
| 66 enum StripBehavior { kStripExtraWhiteSpace, kDoNotStripWhiteSpace }; | 66 enum StripBehavior { kStripExtraWhiteSpace, kDoNotStripWhiteSpace }; |
| 67 | 67 |
| 68 typedef bool (*CharacterMatchFunctionPtr)(UChar); | 68 typedef bool (*CharacterMatchFunctionPtr)(UChar); |
| 69 typedef bool (*IsWhiteSpaceFunctionPtr)(UChar); | 69 typedef bool (*IsWhiteSpaceFunctionPtr)(UChar); |
| 70 typedef HashMap<unsigned, StringImpl*, AlreadyHashed> StaticStringsTable; | 70 typedef HashMap<unsigned, StringImpl*, AlreadyHashed> StaticStringsTable; |
| 71 | 71 |
| 72 // Define STRING_STATS to turn on run time statistics of string sizes and memory | |
| 73 // usage | |
| 74 #undef STRING_STATS | |
| 75 | |
| 76 #ifdef STRING_STATS | |
| 77 struct StringStats { | |
| 78 inline void add8BitString(unsigned length) { | |
| 79 ++m_totalNumberStrings; | |
| 80 ++m_number8BitStrings; | |
| 81 m_total8BitData += length; | |
| 82 } | |
| 83 | |
| 84 inline void add16BitString(unsigned length) { | |
| 85 ++m_totalNumberStrings; | |
| 86 ++m_number16BitStrings; | |
| 87 m_total16BitData += length; | |
| 88 } | |
| 89 | |
| 90 void removeString(StringImpl*); | |
| 91 void printStats(); | |
| 92 | |
| 93 static const unsigned s_printStringStatsFrequency = 5000; | |
| 94 static unsigned s_stringRemovesTillPrintStats; | |
| 95 | |
| 96 unsigned m_totalNumberStrings; | |
| 97 unsigned m_number8BitStrings; | |
| 98 unsigned m_number16BitStrings; | |
| 99 unsigned long long m_total8BitData; | |
| 100 unsigned long long m_total16BitData; | |
| 101 }; | |
| 102 | |
| 103 void addStringForStats(StringImpl*); | |
| 104 void removeStringForStats(StringImpl*); | |
| 105 | |
| 106 #define STRING_STATS_ADD_8BIT_STRING(length) \ | |
| 107 StringImpl::stringStats().add8BitString(length); \ | |
| 108 addStringForStats(this) | |
| 109 #define STRING_STATS_ADD_16BIT_STRING(length) \ | |
| 110 StringImpl::stringStats().add16BitString(length); \ | |
| 111 addStringForStats(this) | |
| 112 #define STRING_STATS_REMOVE_STRING(string) \ | |
| 113 StringImpl::stringStats().removeString(string); \ | |
| 114 removeStringForStats(this) | |
| 115 #else | |
| 116 #define STRING_STATS_ADD_8BIT_STRING(length) ((void)0) | |
| 117 #define STRING_STATS_ADD_16BIT_STRING(length) ((void)0) | |
| 118 #define STRING_STATS_REMOVE_STRING(string) ((void)0) | |
| 119 #endif | |
| 120 | |
| 121 // You can find documentation about this class in this doc: | 72 // You can find documentation about this class in this doc: |
| 122 // https://docs.google.com/document/d/1kOCUlJdh2WJMJGDf-WoEQhmnjKLaOYRbiHz5TiGJl
14/edit?usp=sharing | 73 // https://docs.google.com/document/d/1kOCUlJdh2WJMJGDf-WoEQhmnjKLaOYRbiHz5TiGJl
14/edit?usp=sharing |
| 123 class WTF_EXPORT StringImpl { | 74 class WTF_EXPORT StringImpl { |
| 124 WTF_MAKE_NONCOPYABLE(StringImpl); | 75 WTF_MAKE_NONCOPYABLE(StringImpl); |
| 125 | 76 |
| 126 private: | 77 private: |
| 127 // StringImpls are allocated out of the WTF buffer partition. | 78 // StringImpls are allocated out of the WTF buffer partition. |
| 128 void* operator new(size_t); | 79 void* operator new(size_t); |
| 129 void* operator new(size_t, void* ptr) { return ptr; } | 80 void* operator new(size_t, void* ptr) { return ptr; } |
| 130 void operator delete(void*); | 81 void operator delete(void*); |
| 131 | 82 |
| 132 // Used to construct static strings, which have an special refCount that can | 83 // Used to construct static strings, which have an special refCount that can |
| 133 // never hit zero. This means that the static string will never be | 84 // never hit zero. This means that the static string will never be |
| 134 // destroyed, which is important because static strings will be shared | 85 // destroyed, which is important because static strings will be shared |
| 135 // across threads & ref-counted in a non-threadsafe manner. | 86 // across threads & ref-counted in a non-threadsafe manner. |
| 136 enum ConstructEmptyStringTag { kConstructEmptyString }; | 87 enum ConstructEmptyStringTag { kConstructEmptyString }; |
| 137 explicit StringImpl(ConstructEmptyStringTag) | 88 explicit StringImpl(ConstructEmptyStringTag) |
| 138 : ref_count_(1), | 89 : ref_count_(1), |
| 139 length_(0), | 90 length_(0), |
| 140 hash_(0), | 91 hash_(0), |
| 141 contains_only_ascii_(true), | 92 contains_only_ascii_(true), |
| 142 needs_ascii_check_(false), | 93 needs_ascii_check_(false), |
| 143 is_atomic_(false), | 94 is_atomic_(false), |
| 144 is8_bit_(true), | 95 is8_bit_(true), |
| 145 is_static_(true) { | 96 is_static_(true) { |
| 146 // Ensure that the hash is computed so that AtomicStringHash can call | 97 // Ensure that the hash is computed so that AtomicStringHash can call |
| 147 // existingHash() with impunity. The empty string is special because it | 98 // existingHash() with impunity. The empty string is special because it |
| 148 // is never entered into AtomicString's HashKey, but still needs to | 99 // is never entered into AtomicString's HashKey, but still needs to |
| 149 // compare correctly. | 100 // compare correctly. |
| 150 STRING_STATS_ADD_8BIT_STRING(m_length); | |
| 151 GetHash(); | 101 GetHash(); |
| 152 } | 102 } |
| 153 | 103 |
| 154 enum ConstructEmptyString16BitTag { kConstructEmptyString16Bit }; | 104 enum ConstructEmptyString16BitTag { kConstructEmptyString16Bit }; |
| 155 explicit StringImpl(ConstructEmptyString16BitTag) | 105 explicit StringImpl(ConstructEmptyString16BitTag) |
| 156 : ref_count_(1), | 106 : ref_count_(1), |
| 157 length_(0), | 107 length_(0), |
| 158 hash_(0), | 108 hash_(0), |
| 159 contains_only_ascii_(true), | 109 contains_only_ascii_(true), |
| 160 needs_ascii_check_(false), | 110 needs_ascii_check_(false), |
| 161 is_atomic_(false), | 111 is_atomic_(false), |
| 162 is8_bit_(false), | 112 is8_bit_(false), |
| 163 is_static_(true) { | 113 is_static_(true) { |
| 164 STRING_STATS_ADD_16BIT_STRING(m_length); | |
| 165 GetHash(); | 114 GetHash(); |
| 166 } | 115 } |
| 167 | 116 |
| 168 // FIXME: there has to be a less hacky way to do this. | 117 // FIXME: there has to be a less hacky way to do this. |
| 169 enum Force8Bit { kForce8BitConstructor }; | 118 enum Force8Bit { kForce8BitConstructor }; |
| 170 StringImpl(unsigned length, Force8Bit) | 119 StringImpl(unsigned length, Force8Bit) |
| 171 : ref_count_(1), | 120 : ref_count_(1), |
| 172 length_(length), | 121 length_(length), |
| 173 hash_(0), | 122 hash_(0), |
| 174 contains_only_ascii_(!length), | 123 contains_only_ascii_(!length), |
| 175 needs_ascii_check_(static_cast<bool>(length)), | 124 needs_ascii_check_(static_cast<bool>(length)), |
| 176 is_atomic_(false), | 125 is_atomic_(false), |
| 177 is8_bit_(true), | 126 is8_bit_(true), |
| 178 is_static_(false) { | 127 is_static_(false) { |
| 179 DCHECK(length_); | 128 DCHECK(length_); |
| 180 STRING_STATS_ADD_8BIT_STRING(m_length); | |
| 181 } | 129 } |
| 182 | 130 |
| 183 StringImpl(unsigned length) | 131 StringImpl(unsigned length) |
| 184 : ref_count_(1), | 132 : ref_count_(1), |
| 185 length_(length), | 133 length_(length), |
| 186 hash_(0), | 134 hash_(0), |
| 187 contains_only_ascii_(!length), | 135 contains_only_ascii_(!length), |
| 188 needs_ascii_check_(static_cast<bool>(length)), | 136 needs_ascii_check_(static_cast<bool>(length)), |
| 189 is_atomic_(false), | 137 is_atomic_(false), |
| 190 is8_bit_(false), | 138 is8_bit_(false), |
| 191 is_static_(false) { | 139 is_static_(false) { |
| 192 DCHECK(length_); | 140 DCHECK(length_); |
| 193 STRING_STATS_ADD_16BIT_STRING(m_length); | |
| 194 } | 141 } |
| 195 | 142 |
| 196 enum StaticStringTag { kStaticString }; | 143 enum StaticStringTag { kStaticString }; |
| 197 StringImpl(unsigned length, unsigned hash, StaticStringTag) | 144 StringImpl(unsigned length, unsigned hash, StaticStringTag) |
| 198 : ref_count_(1), | 145 : ref_count_(1), |
| 199 length_(length), | 146 length_(length), |
| 200 hash_(hash), | 147 hash_(hash), |
| 201 contains_only_ascii_(!length), | 148 contains_only_ascii_(!length), |
| 202 needs_ascii_check_(static_cast<bool>(length)), | 149 needs_ascii_check_(static_cast<bool>(length)), |
| 203 is_atomic_(false), | 150 is_atomic_(false), |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 unsigned start = 0, | 420 unsigned start = 0, |
| 474 unsigned length = UINT_MAX) const; | 421 unsigned length = UINT_MAX) const; |
| 475 | 422 |
| 476 #if OS(MACOSX) | 423 #if OS(MACOSX) |
| 477 RetainPtr<CFStringRef> CreateCFString(); | 424 RetainPtr<CFStringRef> CreateCFString(); |
| 478 #endif | 425 #endif |
| 479 #ifdef __OBJC__ | 426 #ifdef __OBJC__ |
| 480 operator NSString*(); | 427 operator NSString*(); |
| 481 #endif | 428 #endif |
| 482 | 429 |
| 483 #ifdef STRING_STATS | |
| 484 ALWAYS_INLINE static StringStats& stringStats() { return m_stringStats; } | |
| 485 #endif | |
| 486 static const UChar kLatin1CaseFoldTable[256]; | 430 static const UChar kLatin1CaseFoldTable[256]; |
| 487 | 431 |
| 488 private: | 432 private: |
| 489 template <typename CharType> | 433 template <typename CharType> |
| 490 static size_t AllocationSize(unsigned length) { | 434 static size_t AllocationSize(unsigned length) { |
| 491 CHECK_LE(length, | 435 CHECK_LE(length, |
| 492 ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / | 436 ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl)) / |
| 493 sizeof(CharType))); | 437 sizeof(CharType))); |
| 494 return sizeof(StringImpl) + length * sizeof(CharType); | 438 return sizeof(StringImpl) + length * sizeof(CharType); |
| 495 } | 439 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 508 StripBehavior); | 452 StripBehavior); |
| 509 NEVER_INLINE unsigned HashSlowCase() const; | 453 NEVER_INLINE unsigned HashSlowCase() const; |
| 510 | 454 |
| 511 void DestroyIfNotStatic() const; | 455 void DestroyIfNotStatic() const; |
| 512 void UpdateContainsOnlyASCII() const; | 456 void UpdateContainsOnlyASCII() const; |
| 513 | 457 |
| 514 #if DCHECK_IS_ON() | 458 #if DCHECK_IS_ON() |
| 515 std::string AsciiForDebugging() const; | 459 std::string AsciiForDebugging() const; |
| 516 #endif | 460 #endif |
| 517 | 461 |
| 518 #ifdef STRING_STATS | |
| 519 static StringStats m_stringStats; | |
| 520 #endif | |
| 521 | |
| 522 static unsigned highest_static_string_length_; | 462 static unsigned highest_static_string_length_; |
| 523 | 463 |
| 524 #if DCHECK_IS_ON() | 464 #if DCHECK_IS_ON() |
| 525 void AssertHashIsCorrect() { | 465 void AssertHashIsCorrect() { |
| 526 DCHECK(HasHash()); | 466 DCHECK(HasHash()); |
| 527 DCHECK_EQ(ExistingHash(), StringHasher::ComputeHashAndMaskTop8Bits( | 467 DCHECK_EQ(ExistingHash(), StringHasher::ComputeHashAndMaskTop8Bits( |
| 528 Characters8(), length())); | 468 Characters8(), length())); |
| 529 } | 469 } |
| 530 #endif | 470 #endif |
| 531 | 471 |
| (...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 898 using WTF::kTextCaseASCIIInsensitive; | 838 using WTF::kTextCaseASCIIInsensitive; |
| 899 using WTF::kTextCaseUnicodeInsensitive; | 839 using WTF::kTextCaseUnicodeInsensitive; |
| 900 using WTF::kTextCaseSensitive; | 840 using WTF::kTextCaseSensitive; |
| 901 using WTF::TextCaseSensitivity; | 841 using WTF::TextCaseSensitivity; |
| 902 using WTF::Equal; | 842 using WTF::Equal; |
| 903 using WTF::EqualNonNull; | 843 using WTF::EqualNonNull; |
| 904 using WTF::LengthOfNullTerminatedString; | 844 using WTF::LengthOfNullTerminatedString; |
| 905 using WTF::ReverseFind; | 845 using WTF::ReverseFind; |
| 906 | 846 |
| 907 #endif | 847 #endif |
| OLD | NEW |