Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(91)

Side by Side Diff: Source/wtf/text/StringImpl.h

Issue 21274008: Remove ASCIILiteral optimization from StringImpl (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/wtf/text/AtomicString.cpp ('k') | Source/wtf/text/StringImpl.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 reserved. 3 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2013 Apple Inc. All rights reserved.
4 * Copyright (C) 2009 Google Inc. All rights reserved. 4 * Copyright (C) 2009 Google Inc. All rights reserved.
5 * 5 *
6 * This library is free software; you can redistribute it and/or 6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public 7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either 8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version. 9 * version 2 of the License, or (at your option) any later version.
10 * 10 *
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 , m_refCount(s_refCountIncrement) 151 , m_refCount(s_refCountIncrement)
152 , m_length(length) 152 , m_length(length)
153 , m_hashAndFlags(0) 153 , m_hashAndFlags(0)
154 { 154 {
155 ASSERT(m_data16); 155 ASSERT(m_data16);
156 ASSERT(m_length); 156 ASSERT(m_length);
157 157
158 STRING_STATS_ADD_16BIT_STRING(m_length); 158 STRING_STATS_ADD_16BIT_STRING(m_length);
159 } 159 }
160 160
161 enum ConstructFromLiteralTag { ConstructFromLiteral };
162 StringImpl(const char* characters, unsigned length, ConstructFromLiteralTag)
163 : m_data8(reinterpret_cast<const LChar*>(characters))
164 , m_refCount(s_refCountIncrement)
165 , m_length(length)
166 , m_hashAndFlags(s_hashFlag8BitBuffer)
167 {
168 ASSERT(m_data8);
169 ASSERT(m_length);
170 ASSERT(!characters[length]);
171
172 STRING_STATS_ADD_8BIT_STRING(0);
173 }
174
175 public: 161 public:
176 ~StringImpl(); 162 ~StringImpl();
177 163
178 static PassRefPtr<StringImpl> create(const UChar*, unsigned length); 164 static PassRefPtr<StringImpl> create(const UChar*, unsigned length);
179 static PassRefPtr<StringImpl> create(const LChar*, unsigned length); 165 static PassRefPtr<StringImpl> create(const LChar*, unsigned length);
180 static PassRefPtr<StringImpl> create8BitIfPossible(const UChar*, unsigned le ngth); 166 static PassRefPtr<StringImpl> create8BitIfPossible(const UChar*, unsigned le ngth);
181 template<size_t inlineCapacity> 167 template<size_t inlineCapacity>
182 static PassRefPtr<StringImpl> create8BitIfPossible(const Vector<UChar, inlin eCapacity>& vector) 168 static PassRefPtr<StringImpl> create8BitIfPossible(const Vector<UChar, inlin eCapacity>& vector)
183 { 169 {
184 return create8BitIfPossible(vector.data(), vector.size()); 170 return create8BitIfPossible(vector.data(), vector.size());
185 } 171 }
186 172
187 ALWAYS_INLINE static PassRefPtr<StringImpl> create(const char* s, unsigned l ength) { return create(reinterpret_cast<const LChar*>(s), length); } 173 ALWAYS_INLINE static PassRefPtr<StringImpl> create(const char* s, unsigned l ength) { return create(reinterpret_cast<const LChar*>(s), length); }
188 static PassRefPtr<StringImpl> create(const LChar*); 174 static PassRefPtr<StringImpl> create(const LChar*);
189 ALWAYS_INLINE static PassRefPtr<StringImpl> create(const char* s) { return c reate(reinterpret_cast<const LChar*>(s)); } 175 ALWAYS_INLINE static PassRefPtr<StringImpl> create(const char* s) { return c reate(reinterpret_cast<const LChar*>(s)); }
190 176
191 static PassRefPtr<StringImpl> createFromLiteral(const char* characters, unsi gned length);
192 template<unsigned charactersCount>
193 ALWAYS_INLINE static PassRefPtr<StringImpl> createFromLiteral(const char (&c haracters)[charactersCount])
194 {
195 COMPILE_ASSERT(charactersCount > 1, StringImplFromLiteralNotEmpty);
196 COMPILE_ASSERT((charactersCount - 1 <= ((unsigned(~0) - sizeof(StringImp l)) / sizeof(LChar))), StringImplFromLiteralCannotOverflow);
197
198 return createFromLiteral(characters, charactersCount - 1);
199 }
200 static PassRefPtr<StringImpl> createFromLiteral(const char* characters);
201
202 static PassRefPtr<StringImpl> createUninitialized(unsigned length, LChar*& d ata); 177 static PassRefPtr<StringImpl> createUninitialized(unsigned length, LChar*& d ata);
203 static PassRefPtr<StringImpl> createUninitialized(unsigned length, UChar*& d ata); 178 static PassRefPtr<StringImpl> createUninitialized(unsigned length, UChar*& d ata);
204 template <typename T> static ALWAYS_INLINE PassRefPtr<StringImpl> tryCreateU ninitialized(unsigned length, T*& output) 179 template <typename T> static ALWAYS_INLINE PassRefPtr<StringImpl> tryCreateU ninitialized(unsigned length, T*& output)
205 { 180 {
206 if (!length) { 181 if (!length) {
207 output = 0; 182 output = 0;
208 return empty(); 183 return empty();
209 } 184 }
210 185
211 if (length > ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl) ) / sizeof(T))) { 186 if (length > ((std::numeric_limits<unsigned>::max() - sizeof(StringImpl) ) / sizeof(T))) {
(...skipping 16 matching lines...) Expand all
228 // Reallocate the StringImpl. The originalString must be only owned by the P assRefPtr. 203 // Reallocate the StringImpl. The originalString must be only owned by the P assRefPtr.
229 // Just like the input pointer of realloc(), the originalString can't be use d after this function. 204 // Just like the input pointer of realloc(), the originalString can't be use d after this function.
230 static PassRefPtr<StringImpl> reallocate(PassRefPtr<StringImpl> originalStri ng, unsigned length, LChar*& data); 205 static PassRefPtr<StringImpl> reallocate(PassRefPtr<StringImpl> originalStri ng, unsigned length, LChar*& data);
231 static PassRefPtr<StringImpl> reallocate(PassRefPtr<StringImpl> originalStri ng, unsigned length, UChar*& data); 206 static PassRefPtr<StringImpl> reallocate(PassRefPtr<StringImpl> originalStri ng, unsigned length, UChar*& data);
232 207
233 // If this StringImpl has only one reference, we can truncate the string by updating 208 // If this StringImpl has only one reference, we can truncate the string by updating
234 // its m_length property without actually re-allocating its buffer. 209 // its m_length property without actually re-allocating its buffer.
235 void truncateAssumingIsolated(unsigned length) 210 void truncateAssumingIsolated(unsigned length)
236 { 211 {
237 ASSERT(hasOneRef()); 212 ASSERT(hasOneRef());
238 ASSERT(!isASCIILiteral());
239 ASSERT(length <= m_length); 213 ASSERT(length <= m_length);
240 m_length = length; 214 m_length = length;
241 } 215 }
242 216
243 unsigned length() const { return m_length; } 217 unsigned length() const { return m_length; }
244 bool is8Bit() const { return m_hashAndFlags & s_hashFlag8BitBuffer; } 218 bool is8Bit() const { return m_hashAndFlags & s_hashFlag8BitBuffer; }
245 219
246 ALWAYS_INLINE const LChar* characters8() const { ASSERT(is8Bit()); return m_ data8; } 220 ALWAYS_INLINE const LChar* characters8() const { ASSERT(is8Bit()); return m_ data8; }
247 ALWAYS_INLINE const UChar* characters16() const { ASSERT(!is8Bit()); return m_data16; } 221 ALWAYS_INLINE const UChar* characters16() const { ASSERT(!is8Bit()); return m_data16; }
248 222
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 #endif 434 #endif
461 #ifdef __OBJC__ 435 #ifdef __OBJC__
462 operator NSString*(); 436 operator NSString*();
463 #endif 437 #endif
464 438
465 #ifdef STRING_STATS 439 #ifdef STRING_STATS
466 ALWAYS_INLINE static StringStats& stringStats() { return m_stringStats; } 440 ALWAYS_INLINE static StringStats& stringStats() { return m_stringStats; }
467 #endif 441 #endif
468 442
469 private: 443 private:
470 bool isASCIILiteral() const
471 {
472 return is8Bit() && length() && reinterpret_cast<const void*>(m_data8) != reinterpret_cast<const void*>(this + 1);
473 }
474
475 // This number must be at least 2 to avoid sharing empty, null as well as 1 character strings from SmallStrings. 444 // This number must be at least 2 to avoid sharing empty, null as well as 1 character strings from SmallStrings.
476 static const unsigned s_copyCharsInlineCutOff = 20; 445 static const unsigned s_copyCharsInlineCutOff = 20;
477 446
478 template <class UCharPredicate> PassRefPtr<StringImpl> stripMatchedCharacter s(UCharPredicate); 447 template <class UCharPredicate> PassRefPtr<StringImpl> stripMatchedCharacter s(UCharPredicate);
479 template <typename CharType, class UCharPredicate> PassRefPtr<StringImpl> si mplifyMatchedCharactersToSpace(UCharPredicate); 448 template <typename CharType, class UCharPredicate> PassRefPtr<StringImpl> si mplifyMatchedCharactersToSpace(UCharPredicate);
480 NEVER_INLINE unsigned hashSlowCase() const; 449 NEVER_INLINE unsigned hashSlowCase() const;
481 450
482 // The bottom bit in the ref count indicates a static (immortal) string. 451 // The bottom bit in the ref count indicates a static (immortal) string.
483 static const unsigned s_refCountFlagIsStaticString = 0x1; 452 static const unsigned s_refCountFlagIsStaticString = 0x1;
484 static const unsigned s_refCountIncrement = 0x2; // This allows us to ref / deref without disturbing the static string flag. 453 static const unsigned s_refCountIncrement = 0x2; // This allows us to ref / deref without disturbing the static string flag.
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
790 759
791 static inline bool isSpaceOrNewline(UChar c) 760 static inline bool isSpaceOrNewline(UChar c)
792 { 761 {
793 // Use isASCIISpace() for basic Latin-1. 762 // Use isASCIISpace() for basic Latin-1.
794 // This will include newlines, which aren't included in Unicode DirWS. 763 // This will include newlines, which aren't included in Unicode DirWS.
795 return c <= 0x7F ? WTF::isASCIISpace(c) : WTF::Unicode::direction(c) == WTF: :Unicode::WhiteSpaceNeutral; 764 return c <= 0x7F ? WTF::isASCIISpace(c) : WTF::Unicode::direction(c) == WTF: :Unicode::WhiteSpaceNeutral;
796 } 765 }
797 766
798 inline PassRefPtr<StringImpl> StringImpl::isolatedCopy() const 767 inline PassRefPtr<StringImpl> StringImpl::isolatedCopy() const
799 { 768 {
800 if (isASCIILiteral())
801 return StringImpl::createFromLiteral(reinterpret_cast<const char*>(m_dat a8), m_length);
802 if (is8Bit()) 769 if (is8Bit())
803 return create(m_data8, m_length); 770 return create(m_data8, m_length);
804 return create(m_data16, m_length); 771 return create(m_data16, m_length);
805 } 772 }
806 773
807 struct StringHash; 774 struct StringHash;
808 775
809 // StringHash is the default hash for StringImpl* and RefPtr<StringImpl> 776 // StringHash is the default hash for StringImpl* and RefPtr<StringImpl>
810 template<typename T> struct DefaultHash; 777 template<typename T> struct DefaultHash;
811 template<> struct DefaultHash<StringImpl*> { 778 template<> struct DefaultHash<StringImpl*> {
812 typedef StringHash Hash; 779 typedef StringHash Hash;
813 }; 780 };
814 template<> struct DefaultHash<RefPtr<StringImpl> > { 781 template<> struct DefaultHash<RefPtr<StringImpl> > {
815 typedef StringHash Hash; 782 typedef StringHash Hash;
816 }; 783 };
817 784
818 } 785 }
819 786
820 using WTF::StringImpl; 787 using WTF::StringImpl;
821 using WTF::equal; 788 using WTF::equal;
822 using WTF::equalNonNull; 789 using WTF::equalNonNull;
823 using WTF::TextCaseSensitivity; 790 using WTF::TextCaseSensitivity;
824 using WTF::TextCaseSensitive; 791 using WTF::TextCaseSensitive;
825 using WTF::TextCaseInsensitive; 792 using WTF::TextCaseInsensitive;
826 793
827 #endif 794 #endif
OLDNEW
« no previous file with comments | « Source/wtf/text/AtomicString.cpp ('k') | Source/wtf/text/StringImpl.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698