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

Side by Side Diff: third_party/WebKit/Source/wtf/text/StringView.h

Issue 2033293002: Implement StringImpl sharing for StringView::toString(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix preload scanner bug. Created 4 years, 6 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef WTF_StringView_h 5 #ifndef WTF_StringView_h
6 #define WTF_StringView_h 6 #define WTF_StringView_h
7 7
8 #include "wtf/Allocator.h" 8 #include "wtf/Allocator.h"
9 #include "wtf/GetPtr.h"
9 #if DCHECK_IS_ON() 10 #if DCHECK_IS_ON()
10 #include "wtf/RefPtr.h" 11 #include "wtf/RefPtr.h"
11 #endif 12 #endif
12 #include "wtf/text/AtomicString.h" 13 #include "wtf/text/AtomicString.h"
13 #include "wtf/text/StringImpl.h" 14 #include "wtf/text/StringImpl.h"
14 #include "wtf/text/Unicode.h" 15 #include "wtf/text/Unicode.h"
15 #include "wtf/text/WTFString.h" 16 #include "wtf/text/WTFString.h"
16 #include <cstring> 17 #include <cstring>
17 18
18 namespace WTF { 19 namespace WTF {
19 20
20 // A string like object that wraps either an 8bit or 16bit byte sequence 21 // A string like object that wraps either an 8bit or 16bit byte sequence
21 // and keeps track of the length and the type, it does NOT own the bytes. 22 // and keeps track of the length and the type, it does NOT own the bytes.
22 // 23 //
23 // Since StringView does not own the bytes creating a StringView from a String, 24 // Since StringView does not own the bytes creating a StringView from a String,
24 // then calling clear() on the string will result in a use-after-free. Asserts 25 // then calling clear() on the String will result in a use-after-free. Asserts
25 // in ~StringView attempt to enforce this for most common cases. 26 // in ~StringView attempt to enforce this for most common cases.
26 // 27 //
27 // See base/strings/string_piece.h for more details. 28 // See base/strings/string_piece.h for more details.
28 class WTF_EXPORT StringView { 29 class WTF_EXPORT StringView {
29 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); 30 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
30 public: 31 public:
31 // Null string. 32 // Null string.
32 StringView() { clear(); } 33 StringView() { clear(); }
33 34
34 // From a StringView: 35 // From a StringView:
35 StringView(const StringView&, unsigned offset, unsigned length); 36 StringView(const StringView&, unsigned offset, unsigned length);
36 StringView(const StringView& view, unsigned offset) 37 StringView(const StringView& view, unsigned offset)
37 : StringView(view, offset, view.m_length - offset) {} 38 : StringView(view, offset, view.m_length - offset) {}
38 39
39 // From a StringImpl: 40 // From a StringImpl:
40 StringView(StringImpl*); 41 StringView(StringImpl*);
41 StringView(StringImpl*, unsigned offset); 42 StringView(StringImpl*, unsigned offset);
42 StringView(StringImpl*, unsigned offset, unsigned length); 43 StringView(StringImpl*, unsigned offset, unsigned length);
43 44
44 // From a String: 45 // From a String:
45 StringView(const String& string, unsigned offset, unsigned length) 46 StringView(const String& string, unsigned offset, unsigned length)
46 : StringView(string.impl(), offset, length) {} 47 : StringView(string.impl(), offset, length) {}
47 StringView(const String& string, unsigned offset) 48 StringView(const String& string, unsigned offset)
48 : StringView(string.impl(), offset) {} 49 : StringView(string.impl(), offset) {}
49 StringView(const String& string) 50 StringView(const String& string)
50 : StringView(string, 0, string.length()) {} 51 : StringView(string.impl()) {}
51 52
52 // From an AtomicString: 53 // From an AtomicString:
53 StringView(const AtomicString& string, unsigned offset, unsigned length) 54 StringView(const AtomicString& string, unsigned offset, unsigned length)
54 : StringView(string.impl(), offset, length) {} 55 : StringView(string.impl(), offset, length) {}
55 StringView(const AtomicString& string, unsigned offset) 56 StringView(const AtomicString& string, unsigned offset)
56 : StringView(string.impl(), offset) {} 57 : StringView(string.impl(), offset) {}
57 StringView(const AtomicString& string) 58 StringView(const AtomicString& string)
58 : StringView(string, 0, string.length()) {} 59 : StringView(string.impl()) {}
59 60
60 // From a literal string or LChar buffer: 61 // From a literal string or LChar buffer:
61 StringView(const LChar* chars, unsigned length); 62 StringView(const LChar* chars, unsigned length)
63 : StringView(reinterpret_cast<const void*>(chars), length, true) {}
62 StringView(const char* chars, unsigned length) 64 StringView(const char* chars, unsigned length)
63 : StringView(reinterpret_cast<const LChar*>(chars), length) {} 65 : StringView(reinterpret_cast<const LChar*>(chars), length) {}
64 StringView(const LChar* chars) 66 StringView(const LChar* chars)
65 : StringView(chars, chars ? strlen(reinterpret_cast<const char*>(chars)) : 0) {} 67 : StringView(chars, chars ? strlen(reinterpret_cast<const char*>(chars)) : 0) {}
66 StringView(const char* chars) 68 StringView(const char* chars)
67 : StringView(reinterpret_cast<const LChar*>(chars)) {} 69 : StringView(reinterpret_cast<const LChar*>(chars)) {}
68 70
69 // From a wide literal string or UChar buffer. 71 // From a wide literal string or UChar buffer.
72 StringView(const UChar* chars, unsigned length)
73 : StringView(reinterpret_cast<const void*>(chars), length, false) {}
70 StringView(const UChar* chars); 74 StringView(const UChar* chars);
71 StringView(const UChar* chars, unsigned length);
72 75
73 // From a byte pointer. 76 // From a byte pointer.
74 StringView(const void* bytes, unsigned length, bool is8Bit) 77 StringView(const void* bytes, unsigned length, bool is8Bit)
75 : m_length(length) 78 : m_impl(is8Bit ? StringImpl::empty() : StringImpl::empty16Bit())
76 , m_is8Bit(is8Bit) 79 , m_bytes(bytes)
77 { 80 , m_length(length) {}
78 m_data.bytes = bytes;
79 }
80 81
81 #if DCHECK_IS_ON() 82 #if DCHECK_IS_ON()
82 ~StringView(); 83 ~StringView();
83 #endif 84 #endif
84 85
85 bool isNull() const { return !m_data.bytes; } 86 bool isNull() const { return !m_bytes; }
86 bool isEmpty() const { return !m_length; } 87 bool isEmpty() const { return !m_length; }
87 88
88 unsigned length() const { return m_length; } 89 unsigned length() const { return m_length; }
89 90
90 bool is8Bit() const { return m_is8Bit; } 91 bool is8Bit() const { DCHECK(m_impl); return m_impl->is8Bit(); }
91 92
92 void clear(); 93 void clear();
93 94
94 UChar operator[](unsigned i) const 95 UChar operator[](unsigned i) const
95 { 96 {
96 SECURITY_DCHECK(i < length()); 97 SECURITY_DCHECK(i < length());
97 if (is8Bit()) 98 if (is8Bit())
98 return characters8()[i]; 99 return characters8()[i];
99 return characters16()[i]; 100 return characters16()[i];
100 } 101 }
101 102
102 const LChar* characters8() const 103 const LChar* characters8() const
103 { 104 {
104 ASSERT(is8Bit()); 105 DCHECK(is8Bit());
105 return m_data.characters8; 106 return m_characters8;
106 } 107 }
107 108
108 const UChar* characters16() const 109 const UChar* characters16() const
109 { 110 {
110 ASSERT(!is8Bit()); 111 DCHECK(!is8Bit());
111 return m_data.characters16; 112 return m_characters16;
112 } 113 }
113 114
114 const void* bytes() const { return m_data.bytes; } 115 const void* bytes() const { return m_bytes; }
116
117 // This is not named impl() like String because it has different semantics.
118 // String::impl() is never null if String::isNull() is false. For StringView
119 // sharedImpl() can be null if the StringView was created with a non-zero
120 // offset, or a length that made it shorter than the underlying impl.
121 StringImpl* sharedImpl() const
122 {
123 // If this StringView is backed by a StringImpl, and was constructed
124 // with a zero offset and the same length we can just access the impl
125 // directly since this == StringView(m_impl).
126 if (m_impl->bytes() == bytes() && m_length == m_impl->length())
haraken 2016/06/06 02:28:52 Would you help me understand why do you need to ch
esprehn 2016/06/06 04:42:21 A StringView can be a shorter length than the unde
127 return getPtr(m_impl);
128 return nullptr;
129 }
115 130
116 String toString() const; 131 String toString() const;
117 AtomicString toAtomicString() const; 132 AtomicString toAtomicString() const;
118 133
119 private: 134 private:
120 void set(StringImpl&, unsigned offset, unsigned length); 135 void set(StringImpl&, unsigned offset, unsigned length);
121 136
137 // We use the StringImpl to mark for 8bit or 16bit, even for strings where
138 // we were constructed from a char pointer. So m_impl->bytes() might have
139 // nothing to do with this view's bytes().
122 #if DCHECK_IS_ON() 140 #if DCHECK_IS_ON()
123 RefPtr<StringImpl> m_impl; 141 RefPtr<StringImpl> m_impl;
142 #else
143 StringImpl* m_impl;
haraken 2016/06/06 02:28:52 Who guarantees that someone else keeps a reference
esprehn 2016/06/06 04:42:21 The caller does, this is the same as base::StringP
haraken 2016/06/06 05:02:35 Thanks for the details. Makes sense. The assertio
124 #endif 144 #endif
125 union DataUnion { 145 union {
126 const LChar* characters8; 146 const LChar* m_characters8;
127 const UChar* characters16; 147 const UChar* m_characters16;
128 const void* bytes; 148 const void* m_bytes;
haraken 2016/06/06 02:28:52 Worth adding a comment that how each of the fields
esprehn 2016/06/06 04:42:21 This is the same as String's semantics. In the nex
haraken 2016/06/06 05:02:35 That sounds great.
129 } m_data; 149 };
130 unsigned m_length; 150 unsigned m_length;
131 unsigned m_is8Bit : 1;
132 }; 151 };
133 152
134 inline StringView::StringView(const StringView& view, unsigned offset, unsigned length) 153 inline StringView::StringView(const StringView& view, unsigned offset, unsigned length)
135 : m_length(length) 154 : m_impl(view.m_impl)
136 , m_is8Bit(view.is8Bit()) 155 , m_length(length)
137 { 156 {
138 SECURITY_DCHECK(offset + length <= view.length()); 157 SECURITY_DCHECK(offset + length <= view.length());
139 if (is8Bit()) 158 if (is8Bit())
140 m_data.characters8 = view.characters8() + offset; 159 m_characters8 = view.characters8() + offset;
141 else 160 else
142 m_data.characters16 = view.characters16() + offset; 161 m_characters16 = view.characters16() + offset;
143 } 162 }
144 163
145 inline StringView::StringView(StringImpl* impl) 164 inline StringView::StringView(StringImpl* impl)
146 { 165 {
147 impl ? set(*impl, 0, impl->length()) : clear(); 166 if (!impl) {
167 clear();
168 return;
169 }
170 m_impl = impl;
171 m_length = impl->length();
172 m_bytes = impl->bytes();
148 } 173 }
149 174
150 inline StringView::StringView(StringImpl* impl, unsigned offset) 175 inline StringView::StringView(StringImpl* impl, unsigned offset)
151 { 176 {
152 impl ? set(*impl, offset, impl->length() - offset) : clear(); 177 impl ? set(*impl, offset, impl->length() - offset) : clear();
153 } 178 }
154 179
155 inline StringView::StringView(StringImpl* impl, unsigned offset, unsigned length ) 180 inline StringView::StringView(StringImpl* impl, unsigned offset, unsigned length )
156 { 181 {
157 impl ? set(*impl, offset, length) : clear(); 182 impl ? set(*impl, offset, length) : clear();
158 } 183 }
159 184
160 inline StringView::StringView(const LChar* chars, unsigned length)
161 : m_length(length)
162 , m_is8Bit(true)
163 {
164 m_data.characters8 = chars;
165 }
166
167 inline void StringView::clear() 185 inline void StringView::clear()
168 { 186 {
169 m_length = 0; 187 m_length = 0;
170 m_is8Bit = true; 188 m_bytes = nullptr;
171 m_data.bytes = nullptr; 189 m_impl = StringImpl::empty(); // mark as 8 bit.
172 #if DCHECK_IS_ON()
173 m_impl = nullptr;
174 #endif
175 } 190 }
176 191
177 inline void StringView::set(StringImpl& impl, unsigned offset, unsigned length) 192 inline void StringView::set(StringImpl& impl, unsigned offset, unsigned length)
178 { 193 {
179 SECURITY_DCHECK(offset + length <= impl.length()); 194 SECURITY_DCHECK(offset + length <= impl.length());
180 m_length = length; 195 m_length = length;
181 m_is8Bit = impl.is8Bit();
182 #if DCHECK_IS_ON()
183 m_impl = &impl; 196 m_impl = &impl;
184 #endif 197 if (impl.is8Bit())
185 if (m_is8Bit) 198 m_characters8 = impl.characters8() + offset;
186 m_data.characters8 = impl.characters8() + offset;
187 else 199 else
188 m_data.characters16 = impl.characters16() + offset; 200 m_characters16 = impl.characters16() + offset;
189 } 201 }
190 202
191 WTF_EXPORT bool equalIgnoringASCIICase(const StringView& a, const StringView& b) ; 203 WTF_EXPORT bool equalIgnoringASCIICase(const StringView& a, const StringView& b) ;
192 204
193 // TODO(esprehn): Can't make this an overload of WTF::equal since that makes 205 // TODO(esprehn): Can't make this an overload of WTF::equal since that makes
194 // calls to equal() that pass literal strings ambiguous. Figure out if we can 206 // calls to equal() that pass literal strings ambiguous. Figure out if we can
195 // replace all the callers with equalStringView and then rename it to equal(). 207 // replace all the callers with equalStringView and then rename it to equal().
196 WTF_EXPORT bool equalStringView(const StringView&, const StringView&); 208 WTF_EXPORT bool equalStringView(const StringView&, const StringView&);
197 209
198 inline bool operator==(const StringView& a, const StringView& b) 210 inline bool operator==(const StringView& a, const StringView& b)
199 { 211 {
200 return equalStringView(a, b); 212 return equalStringView(a, b);
201 } 213 }
202 214
203 inline bool operator!=(const StringView& a, const StringView& b) 215 inline bool operator!=(const StringView& a, const StringView& b)
204 { 216 {
205 return !(a == b); 217 return !(a == b);
206 } 218 }
207 219
208 } // namespace WTF 220 } // namespace WTF
209 221
210 using WTF::StringView; 222 using WTF::StringView;
211 223
212 #endif 224 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698