Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved. | 2 * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * This library is free software; you can redistribute it and/or | 4 * This library is free software; you can redistribute it and/or |
| 5 * modify it under the terms of the GNU Library General Public | 5 * modify it under the terms of the GNU Library General Public |
| 6 * License as published by the Free Software Foundation; either | 6 * License as published by the Free Software Foundation; either |
| 7 * version 2 of the License, or (at your option) any later version. | 7 * version 2 of the License, or (at your option) any later version. |
| 8 * | 8 * |
| 9 * This library is distributed in the hope that it will be useful, | 9 * This library is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 * Library General Public License for more details. | 12 * Library General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU Library General Public License | 14 * You should have received a copy of the GNU Library General Public License |
| 15 * along with this library; see the file COPYING.LIB. If not, write to | 15 * along with this library; see the file COPYING.LIB. If not, write to |
| 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 * Boston, MA 02110-1301, USA. | 17 * Boston, MA 02110-1301, USA. |
| 18 * | 18 * |
| 19 */ | 19 */ |
| 20 | 20 |
| 21 #ifndef AtomicString_h | 21 #ifndef AtomicString_h |
| 22 #define AtomicString_h | 22 #define AtomicString_h |
| 23 | 23 |
| 24 #include "wtf/Allocator.h" | 24 #include "wtf/Allocator.h" |
| 25 #include "wtf/HashTableDeletedValueType.h" | 25 #include "wtf/HashTableDeletedValueType.h" |
| 26 #include "wtf/WTFExport.h" | 26 #include "wtf/WTFExport.h" |
| 27 #include "wtf/text/CString.h" | 27 #include "wtf/text/CString.h" |
| 28 #include "wtf/text/WTFString.h" | 28 #include "wtf/text/WTFString.h" |
| 29 #include <cstring> | |
| 29 #include <iosfwd> | 30 #include <iosfwd> |
| 30 | 31 |
| 31 namespace WTF { | 32 namespace WTF { |
| 32 | 33 |
| 33 struct AtomicStringHash; | 34 struct AtomicStringHash; |
| 34 | 35 |
| 35 class WTF_EXPORT AtomicString { | 36 class WTF_EXPORT AtomicString { |
| 36 USING_FAST_MALLOC(AtomicString); | 37 USING_FAST_MALLOC(AtomicString); |
| 37 public: | 38 public: |
| 38 static void init(); | 39 static void init(); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 52 { | 53 { |
| 53 } | 54 } |
| 54 | 55 |
| 55 // Constructing an AtomicString from a String / StringImpl can be expensive if | 56 // Constructing an AtomicString from a String / StringImpl can be expensive if |
| 56 // the StringImpl is not already atomic. | 57 // the StringImpl is not already atomic. |
| 57 explicit AtomicString(StringImpl* impl) : m_string(add(impl)) { } | 58 explicit AtomicString(StringImpl* impl) : m_string(add(impl)) { } |
| 58 explicit AtomicString(const String& s) : m_string(add(s.impl())) { } | 59 explicit AtomicString(const String& s) : m_string(add(s.impl())) { } |
| 59 | 60 |
| 60 AtomicString(StringImpl* baseString, unsigned start, unsigned length) : m_st ring(add(baseString, start, length)) { } | 61 AtomicString(StringImpl* baseString, unsigned start, unsigned length) : m_st ring(add(baseString, start, length)) { } |
| 61 | 62 |
| 62 enum ConstructFromLiteralTag { ConstructFromLiteral }; | |
| 63 AtomicString(const char* characters, unsigned length, ConstructFromLiteralTa g) | |
| 64 : m_string(addFromLiteralData(characters, length)) | |
| 65 { | |
| 66 } | |
| 67 | |
| 68 template<unsigned charactersCount> | |
| 69 ALWAYS_INLINE AtomicString(const char (&characters)[charactersCount], Constr uctFromLiteralTag) | |
| 70 : m_string(addFromLiteralData(characters, charactersCount - 1)) | |
| 71 { | |
| 72 static_assert(charactersCount > 1, "AtomicString FromLiteralData should not be empty"); | |
| 73 static_assert((charactersCount - 1 <= ((unsigned(~0) - sizeof(StringImpl )) / sizeof(LChar))), "AtomicString FromLiteralData cannot overflow"); | |
| 74 } | |
| 75 | |
| 76 // Hash table deleted values, which are only constructed and never copied or destroyed. | 63 // Hash table deleted values, which are only constructed and never copied or destroyed. |
| 77 AtomicString(WTF::HashTableDeletedValueType) : m_string(WTF::HashTableDelete dValue) { } | 64 AtomicString(WTF::HashTableDeletedValueType) : m_string(WTF::HashTableDelete dValue) { } |
| 78 bool isHashTableDeletedValue() const { return m_string.isHashTableDeletedVal ue(); } | 65 bool isHashTableDeletedValue() const { return m_string.isHashTableDeletedVal ue(); } |
| 79 | 66 |
| 80 static StringImpl* find(const StringImpl*); | 67 static StringImpl* find(const StringImpl*); |
| 81 | 68 |
| 82 operator const String&() const { return m_string; } | 69 operator const String&() const { return m_string; } |
| 83 const String& getString() const { return m_string; } | 70 const String& getString() const { return m_string; } |
| 84 | 71 |
| 85 StringImpl* impl() const { return m_string.impl(); } | 72 StringImpl* impl() const { return m_string.impl(); } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 154 CString latin1() const { return m_string.latin1(); } | 141 CString latin1() const { return m_string.latin1(); } |
| 155 CString utf8(UTF8ConversionMode mode = LenientUTF8Conversion) const { return m_string.utf8(mode); } | 142 CString utf8(UTF8ConversionMode mode = LenientUTF8Conversion) const { return m_string.utf8(mode); } |
| 156 | 143 |
| 157 #ifndef NDEBUG | 144 #ifndef NDEBUG |
| 158 void show() const; | 145 void show() const; |
| 159 #endif | 146 #endif |
| 160 | 147 |
| 161 private: | 148 private: |
| 162 String m_string; | 149 String m_string; |
| 163 | 150 |
| 164 static PassRefPtr<StringImpl> add(const LChar*); | 151 ALWAYS_INLINE static PassRefPtr<StringImpl> add(const LChar* characters) |
|
Yuta Kitamura
2016/03/31 06:12:06
String literals have a type of |const char[N]|, so
esprehn
2016/03/31 18:04:12
Yeah you can do that, but that won't work for
con
| |
| 152 { | |
| 153 if (!characters) | |
|
dcheng
2016/03/31 05:54:36
null is a valid input to add()?! That sounds insan
esprehn
2016/03/31 18:04:12
I'm just doing what the old code did... I agree it
| |
| 154 return nullptr; | |
| 155 return add(characters, strlen(reinterpret_cast<const char*>(characters)) ); | |
| 156 } | |
| 165 ALWAYS_INLINE static PassRefPtr<StringImpl> add(const char* s) { return add( reinterpret_cast<const LChar*>(s)); } | 157 ALWAYS_INLINE static PassRefPtr<StringImpl> add(const char* s) { return add( reinterpret_cast<const LChar*>(s)); } |
| 166 static PassRefPtr<StringImpl> add(const LChar*, unsigned length); | 158 static PassRefPtr<StringImpl> add(const LChar*, unsigned length); |
| 167 static PassRefPtr<StringImpl> add(const UChar*, unsigned length); | 159 static PassRefPtr<StringImpl> add(const UChar*, unsigned length); |
| 168 ALWAYS_INLINE static PassRefPtr<StringImpl> add(const char* s, unsigned leng th) { return add(reinterpret_cast<const LChar*>(s), length); } | 160 ALWAYS_INLINE static PassRefPtr<StringImpl> add(const char* s, unsigned leng th) { return add(reinterpret_cast<const LChar*>(s), length); } |
| 169 static PassRefPtr<StringImpl> add(const UChar*, unsigned length, unsigned ex istingHash); | 161 static PassRefPtr<StringImpl> add(const UChar*, unsigned length, unsigned ex istingHash); |
| 170 static PassRefPtr<StringImpl> add(const UChar*); | 162 static PassRefPtr<StringImpl> add(const UChar*); |
| 171 static PassRefPtr<StringImpl> add(StringImpl*, unsigned offset, unsigned len gth); | 163 static PassRefPtr<StringImpl> add(StringImpl*, unsigned offset, unsigned len gth); |
| 172 ALWAYS_INLINE static PassRefPtr<StringImpl> add(StringImpl* r) | 164 ALWAYS_INLINE static PassRefPtr<StringImpl> add(StringImpl* r) |
| 173 { | 165 { |
| 174 if (!r || r->isAtomic()) | 166 if (!r || r->isAtomic()) |
| 175 return r; | 167 return r; |
| 176 return addSlowCase(r); | 168 return addSlowCase(r); |
| 177 } | 169 } |
| 178 static PassRefPtr<StringImpl> addFromLiteralData(const char* characters, uns igned length); | |
| 179 static PassRefPtr<StringImpl> addSlowCase(StringImpl*); | 170 static PassRefPtr<StringImpl> addSlowCase(StringImpl*); |
| 180 #if OS(MACOSX) | 171 #if OS(MACOSX) |
| 181 static PassRefPtr<StringImpl> add(CFStringRef); | 172 static PassRefPtr<StringImpl> add(CFStringRef); |
| 182 #endif | 173 #endif |
| 183 | 174 |
| 184 static AtomicString fromUTF8Internal(const char*, const char*); | 175 static AtomicString fromUTF8Internal(const char*, const char*); |
| 185 }; | 176 }; |
| 186 | 177 |
| 187 inline bool operator==(const AtomicString& a, const AtomicString& b) { return a. impl() == b.impl(); } | 178 inline bool operator==(const AtomicString& a, const AtomicString& b) { return a. impl() == b.impl(); } |
| 188 WTF_EXPORT bool operator==(const AtomicString&, const LChar*); | 179 WTF_EXPORT bool operator==(const AtomicString&, const LChar*); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 258 using WTF::AtomicString; | 249 using WTF::AtomicString; |
| 259 using WTF::nullAtom; | 250 using WTF::nullAtom; |
| 260 using WTF::emptyAtom; | 251 using WTF::emptyAtom; |
| 261 using WTF::starAtom; | 252 using WTF::starAtom; |
| 262 using WTF::xmlAtom; | 253 using WTF::xmlAtom; |
| 263 using WTF::xmlnsAtom; | 254 using WTF::xmlnsAtom; |
| 264 using WTF::xlinkAtom; | 255 using WTF::xlinkAtom; |
| 265 | 256 |
| 266 #include "wtf/text/StringConcatenate.h" | 257 #include "wtf/text/StringConcatenate.h" |
| 267 #endif // AtomicString_h | 258 #endif // AtomicString_h |
| OLD | NEW |