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

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

Issue 2764243002: Move files in wtf/ to platform/wtf/ (Part 9). (Closed)
Patch Set: Rebase. Created 3 years, 9 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 2017 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 #include "platform/wtf/text/StringView.h"
6 #define WTF_StringView_h
7 6
8 #include "wtf/Allocator.h" 7 // The contents of this header was moved to platform/wtf as part of
9 #include "wtf/GetPtr.h" 8 // WTF migration project. See the following post for details:
10 #if DCHECK_IS_ON() 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY CAAJ
11 #include "wtf/RefPtr.h"
12 #endif
13 #include "wtf/text/StringImpl.h"
14 #include "wtf/text/Unicode.h"
15 #include <cstring>
16
17 namespace WTF {
18
19 class AtomicString;
20 class String;
21
22 // A string like object that wraps either an 8bit or 16bit byte sequence
23 // and keeps track of the length and the type, it does NOT own the bytes.
24 //
25 // Since StringView does not own the bytes creating a StringView from a String,
26 // then calling clear() on the String will result in a use-after-free. Asserts
27 // in ~StringView attempt to enforce this for most common cases.
28 //
29 // See base/strings/string_piece.h for more details.
30 class WTF_EXPORT StringView {
31 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
32
33 public:
34 // Null string.
35 StringView() { clear(); }
36
37 // From a StringView:
38 StringView(const StringView&, unsigned offset, unsigned length);
39 StringView(const StringView& view, unsigned offset)
40 : StringView(view, offset, view.m_length - offset) {}
41
42 // From a StringImpl:
43 StringView(const StringImpl*);
44 StringView(const StringImpl*, unsigned offset);
45 StringView(const StringImpl*, unsigned offset, unsigned length);
46
47 // From a non-null StringImpl.
48 StringView(const StringImpl& impl)
49 : m_impl(const_cast<StringImpl*>(&impl)),
50 m_bytes(impl.bytes()),
51 m_length(impl.length()) {}
52
53 // From a non-null StringImpl, avoids the null check.
54 StringView(StringImpl& impl)
55 : m_impl(&impl), m_bytes(impl.bytes()), m_length(impl.length()) {}
56 StringView(StringImpl&, unsigned offset);
57 StringView(StringImpl&, unsigned offset, unsigned length);
58
59 // From an String, implemented in String.h
60 inline StringView(const String&, unsigned offset, unsigned length);
61 inline StringView(const String&, unsigned offset);
62 inline StringView(const String&);
63
64 // From an AtomicString, implemented in AtomicString.h
65 inline StringView(const AtomicString&, unsigned offset, unsigned length);
66 inline StringView(const AtomicString&, unsigned offset);
67 inline StringView(const AtomicString&);
68
69 // From a literal string or LChar buffer:
70 StringView(const LChar* chars, unsigned length)
71 : m_impl(StringImpl::empty), m_characters8(chars), m_length(length) {}
72 StringView(const char* chars, unsigned length)
73 : StringView(reinterpret_cast<const LChar*>(chars), length) {}
74 StringView(const LChar* chars)
75 : StringView(chars,
76 chars ? strlen(reinterpret_cast<const char*>(chars)) : 0) {}
77 StringView(const char* chars)
78 : StringView(reinterpret_cast<const LChar*>(chars)) {}
79
80 // From a wide literal string or UChar buffer.
81 StringView(const UChar* chars, unsigned length)
82 : m_impl(StringImpl::empty16Bit),
83 m_characters16(chars),
84 m_length(length) {}
85 StringView(const UChar* chars);
86 StringView(const char16_t* chars)
87 : StringView(reinterpret_cast<const UChar*>(chars)) {}
88
89 #if DCHECK_IS_ON()
90 ~StringView();
91 #endif
92
93 bool isNull() const { return !m_bytes; }
94 bool isEmpty() const { return !m_length; }
95
96 unsigned length() const { return m_length; }
97
98 bool is8Bit() const {
99 DCHECK(m_impl);
100 return m_impl->is8Bit();
101 }
102
103 void clear();
104
105 UChar operator[](unsigned i) const {
106 SECURITY_DCHECK(i < length());
107 if (is8Bit())
108 return characters8()[i];
109 return characters16()[i];
110 }
111
112 const LChar* characters8() const {
113 DCHECK(is8Bit());
114 return m_characters8;
115 }
116
117 const UChar* characters16() const {
118 DCHECK(!is8Bit());
119 return m_characters16;
120 }
121
122 const void* bytes() const { return m_bytes; }
123
124 // This is not named impl() like String because it has different semantics.
125 // String::impl() is never null if String::isNull() is false. For StringView
126 // sharedImpl() can be null if the StringView was created with a non-zero
127 // offset, or a length that made it shorter than the underlying impl.
128 StringImpl* sharedImpl() const {
129 // If this StringView is backed by a StringImpl, and was constructed
130 // with a zero offset and the same length we can just access the impl
131 // directly since this == StringView(m_impl).
132 if (m_impl->bytes() == bytes() && m_length == m_impl->length())
133 return getPtr(m_impl);
134 return nullptr;
135 }
136
137 String toString() const;
138 AtomicString toAtomicString() const;
139
140 template <bool isSpecialCharacter(UChar)>
141 bool isAllSpecialCharacters() const;
142
143 private:
144 void set(const StringImpl&, unsigned offset, unsigned length);
145
146 // We use the StringImpl to mark for 8bit or 16bit, even for strings where
147 // we were constructed from a char pointer. So m_impl->bytes() might have
148 // nothing to do with this view's bytes().
149 #if DCHECK_IS_ON()
150 RefPtr<StringImpl> m_impl;
151 #else
152 StringImpl* m_impl;
153 #endif
154 union {
155 const LChar* m_characters8;
156 const UChar* m_characters16;
157 const void* m_bytes;
158 };
159 unsigned m_length;
160 };
161
162 inline StringView::StringView(const StringView& view,
163 unsigned offset,
164 unsigned length)
165 : m_impl(view.m_impl), m_length(length) {
166 SECURITY_DCHECK(offset + length <= view.length());
167 if (is8Bit())
168 m_characters8 = view.characters8() + offset;
169 else
170 m_characters16 = view.characters16() + offset;
171 }
172
173 inline StringView::StringView(const StringImpl* impl) {
174 if (!impl) {
175 clear();
176 return;
177 }
178 m_impl = const_cast<StringImpl*>(impl);
179 m_length = impl->length();
180 m_bytes = impl->bytes();
181 }
182
183 inline StringView::StringView(const StringImpl* impl, unsigned offset) {
184 impl ? set(*impl, offset, impl->length() - offset) : clear();
185 }
186
187 inline StringView::StringView(const StringImpl* impl,
188 unsigned offset,
189 unsigned length) {
190 impl ? set(*impl, offset, length) : clear();
191 }
192
193 inline StringView::StringView(StringImpl& impl, unsigned offset) {
194 set(impl, offset, impl.length() - offset);
195 }
196
197 inline StringView::StringView(StringImpl& impl,
198 unsigned offset,
199 unsigned length) {
200 set(impl, offset, length);
201 }
202
203 inline void StringView::clear() {
204 m_length = 0;
205 m_bytes = nullptr;
206 m_impl = StringImpl::empty; // mark as 8 bit.
207 }
208
209 inline void StringView::set(const StringImpl& impl,
210 unsigned offset,
211 unsigned length) {
212 SECURITY_DCHECK(offset + length <= impl.length());
213 m_length = length;
214 m_impl = const_cast<StringImpl*>(&impl);
215 if (impl.is8Bit())
216 m_characters8 = impl.characters8() + offset;
217 else
218 m_characters16 = impl.characters16() + offset;
219 }
220
221 // Unicode aware case insensitive string matching. Non-ASCII characters might
222 // match to ASCII characters. These functions are rarely used to implement web
223 // platform features.
224 WTF_EXPORT bool equalIgnoringCase(const StringView&, const StringView&);
225 WTF_EXPORT bool equalIgnoringCaseAndNullity(const StringView&,
226 const StringView&);
227
228 WTF_EXPORT bool equalIgnoringASCIICase(const StringView&, const StringView&);
229
230 // TODO(esprehn): Can't make this an overload of WTF::equal since that makes
231 // calls to equal() that pass literal strings ambiguous. Figure out if we can
232 // replace all the callers with equalStringView and then rename it to equal().
233 WTF_EXPORT bool equalStringView(const StringView&, const StringView&);
234
235 inline bool operator==(const StringView& a, const StringView& b) {
236 return equalStringView(a, b);
237 }
238
239 inline bool operator!=(const StringView& a, const StringView& b) {
240 return !(a == b);
241 }
242
243 template <bool isSpecialCharacter(UChar), typename CharacterType>
244 inline bool isAllSpecialCharacters(const CharacterType* characters,
245 size_t length) {
246 for (size_t i = 0; i < length; ++i) {
247 if (!isSpecialCharacter(characters[i]))
248 return false;
249 }
250 return true;
251 }
252
253 template <bool isSpecialCharacter(UChar)>
254 inline bool StringView::isAllSpecialCharacters() const {
255 size_t len = length();
256 if (!len)
257 return true;
258
259 return is8Bit() ? WTF::isAllSpecialCharacters<isSpecialCharacter, LChar>(
260 characters8(), len)
261 : WTF::isAllSpecialCharacters<isSpecialCharacter, UChar>(
262 characters16(), len);
263 }
264
265 } // namespace WTF
266
267 using WTF::StringView;
268 using WTF::equalIgnoringASCIICase;
269 using WTF::equalIgnoringCase;
270 using WTF::isAllSpecialCharacters;
271
272 #endif
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/text/StringToNumber.cpp ('k') | third_party/WebKit/Source/wtf/text/StringView.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698