OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 /* |
2 // Use of this source code is governed by a BSD-style license that can be | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 // found in the LICENSE file. | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
4 | 30 |
5 #ifndef WTF_StringView_h | 31 #ifndef WTF_StringView_h |
6 #define WTF_StringView_h | 32 #define WTF_StringView_h |
7 | 33 |
8 #include "wtf/Allocator.h" | 34 #include "wtf/Allocator.h" |
9 #if DCHECK_IS_ON() | |
10 #include "wtf/RefPtr.h" | |
11 #endif | |
12 #include "wtf/text/AtomicString.h" | |
13 #include "wtf/text/StringImpl.h" | 35 #include "wtf/text/StringImpl.h" |
14 #include "wtf/text/Unicode.h" | |
15 #include "wtf/text/WTFString.h" | |
16 #include <cstring> | |
17 | 36 |
18 namespace WTF { | 37 namespace WTF { |
19 | 38 |
20 // 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 // | |
23 // 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 // in ~StringView attempt to enforce this for most common cases. | |
26 // | |
27 // See base/strings/string_piece.h for more details. | |
28 class WTF_EXPORT StringView { | 39 class WTF_EXPORT StringView { |
29 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); | 40 DISALLOW_NEW(); |
30 public: | 41 public: |
31 // Null string. | 42 StringView() |
32 StringView() { clear(); } | 43 : m_offset(0) |
| 44 , m_length(0) |
| 45 { |
| 46 } |
33 | 47 |
34 // From a StringView: | 48 explicit StringView(PassRefPtr<StringImpl> impl) |
35 StringView(const StringView&, unsigned offset, unsigned length); | 49 : m_impl(impl) |
36 StringView(const StringView& view, unsigned offset) | 50 , m_offset(0) |
37 : StringView(view, offset, view.m_length - offset) {} | 51 , m_length(m_impl->length()) |
| 52 { |
| 53 } |
38 | 54 |
39 // From a StringImpl: | 55 StringView(PassRefPtr<StringImpl> impl, unsigned offset, unsigned length) |
40 StringView(StringImpl*); | 56 : m_impl(impl) |
41 StringView(StringImpl*, unsigned offset); | 57 , m_offset(offset) |
42 StringView(StringImpl*, unsigned offset, unsigned length); | 58 , m_length(length) |
| 59 { |
| 60 ASSERT_WITH_SECURITY_IMPLICATION(offset + length <= m_impl->length()); |
| 61 } |
43 | 62 |
44 // From a String: | 63 void narrow(unsigned offset, unsigned length) |
45 StringView(const String& string, unsigned offset, unsigned length) | 64 { |
46 : StringView(string.impl(), offset, length) {} | 65 ASSERT_WITH_SECURITY_IMPLICATION(offset + length <= m_length); |
47 StringView(const String& string, unsigned offset) | 66 m_offset += offset; |
48 : StringView(string.impl(), offset) {} | 67 m_length = length; |
49 StringView(const String& string) | 68 } |
50 : StringView(string, 0, string.length()) {} | |
51 | 69 |
52 // From an AtomicString: | |
53 StringView(const AtomicString& string, unsigned offset, unsigned length) | |
54 : StringView(string.impl(), offset, length) {} | |
55 StringView(const AtomicString& string, unsigned offset) | |
56 : StringView(string.impl(), offset) {} | |
57 StringView(const AtomicString& string) | |
58 : StringView(string, 0, string.length()) {} | |
59 | |
60 // From a literal string or LChar buffer: | |
61 StringView(const LChar* chars, unsigned length); | |
62 StringView(const char* chars, unsigned length) | |
63 : StringView(reinterpret_cast<const LChar*>(chars), length) {} | |
64 StringView(const LChar* chars) | |
65 : StringView(chars, chars ? strlen(reinterpret_cast<const char*>(chars))
: 0) {} | |
66 StringView(const char* chars) | |
67 : StringView(reinterpret_cast<const LChar*>(chars)) {} | |
68 | |
69 // From a wide literal string or UChar buffer. | |
70 StringView(const UChar* chars); | |
71 StringView(const UChar* chars, unsigned length); | |
72 | |
73 #if DCHECK_IS_ON() | |
74 ~StringView(); | |
75 #endif | |
76 | |
77 bool isNull() const { return !m_data.bytes; } | |
78 bool isEmpty() const { return !m_length; } | 70 bool isEmpty() const { return !m_length; } |
79 | |
80 unsigned length() const { return m_length; } | 71 unsigned length() const { return m_length; } |
81 | 72 |
82 bool is8Bit() const { return m_is8Bit; } | 73 bool is8Bit() const { return m_impl->is8Bit(); } |
83 | |
84 void clear(); | |
85 | 74 |
86 const LChar* characters8() const | 75 const LChar* characters8() const |
87 { | 76 { |
| 77 if (!m_impl) |
| 78 return 0; |
88 ASSERT(is8Bit()); | 79 ASSERT(is8Bit()); |
89 return m_data.characters8; | 80 return m_impl->characters8() + m_offset; |
90 } | 81 } |
91 | 82 |
92 const UChar* characters16() const | 83 const UChar* characters16() const |
93 { | 84 { |
| 85 if (!m_impl) |
| 86 return 0; |
94 ASSERT(!is8Bit()); | 87 ASSERT(!is8Bit()); |
95 return m_data.characters16; | 88 return m_impl->characters16() + m_offset; |
96 } | 89 } |
97 | 90 |
98 String toString() const; | 91 PassRefPtr<StringImpl> toString() const |
| 92 { |
| 93 if (!m_impl) |
| 94 return m_impl; |
| 95 if (m_impl->is8Bit()) |
| 96 return StringImpl::create(characters8(), m_length); |
| 97 return StringImpl::create(characters16(), m_length); |
| 98 } |
99 | 99 |
100 private: | 100 private: |
101 void set(StringImpl&, unsigned offset, unsigned length); | |
102 | |
103 #if DCHECK_IS_ON() | |
104 RefPtr<StringImpl> m_impl; | 101 RefPtr<StringImpl> m_impl; |
105 #endif | 102 unsigned m_offset; |
106 union DataUnion { | |
107 const LChar* characters8; | |
108 const UChar* characters16; | |
109 const void* bytes; | |
110 } m_data; | |
111 unsigned m_length; | 103 unsigned m_length; |
112 unsigned m_is8Bit : 1; | |
113 }; | 104 }; |
114 | 105 |
115 inline StringView::StringView(const StringView& view, unsigned offset, unsigned
length) | |
116 : m_length(length) | |
117 , m_is8Bit(view.is8Bit()) | |
118 { | |
119 SECURITY_DCHECK(offset + length <= view.length()); | |
120 if (is8Bit()) | |
121 m_data.characters8 = view.characters8() + offset; | |
122 else | |
123 m_data.characters16 = view.characters16() + offset; | |
124 } | |
125 | |
126 inline StringView::StringView(StringImpl* impl) | |
127 { | |
128 impl ? set(*impl, 0, impl->length()) : clear(); | |
129 } | |
130 | |
131 inline StringView::StringView(StringImpl* impl, unsigned offset) | |
132 { | |
133 impl ? set(*impl, offset, impl->length() - offset) : clear(); | |
134 } | |
135 | |
136 inline StringView::StringView(StringImpl* impl, unsigned offset, unsigned length
) | |
137 { | |
138 impl ? set(*impl, offset, length) : clear(); | |
139 } | |
140 | |
141 inline StringView::StringView(const LChar* chars, unsigned length) | |
142 : m_length(length) | |
143 , m_is8Bit(true) | |
144 { | |
145 m_data.characters8 = chars; | |
146 } | |
147 | |
148 inline void StringView::clear() | |
149 { | |
150 m_length = 0; | |
151 m_is8Bit = true; | |
152 m_data.bytes = nullptr; | |
153 #if DCHECK_IS_ON() | |
154 m_impl = nullptr; | |
155 #endif | |
156 } | |
157 | |
158 inline void StringView::set(StringImpl& impl, unsigned offset, unsigned length) | |
159 { | |
160 SECURITY_DCHECK(offset + length <= impl.length()); | |
161 m_length = length; | |
162 m_is8Bit = impl.is8Bit(); | |
163 #if DCHECK_IS_ON() | |
164 m_impl = &impl; | |
165 #endif | |
166 if (m_is8Bit) | |
167 m_data.characters8 = impl.characters8() + offset; | |
168 else | |
169 m_data.characters16 = impl.characters16() + offset; | |
170 } | |
171 | |
172 // TODO(esprehn): Can't make this an overload of WTF::equal since that makes | |
173 // calls to equal() that pass literal strings ambiguous. Figure out if we can | |
174 // replace all the callers with equalStringView and then rename it to equal(). | |
175 WTF_EXPORT bool equalStringView(const StringView&, const StringView&); | |
176 | |
177 inline bool operator==(const StringView& a, const StringView& b) | |
178 { | |
179 return equalStringView(a, b); | |
180 } | |
181 | |
182 inline bool operator!=(const StringView& a, const StringView& b) | |
183 { | |
184 return !(a == b); | |
185 } | |
186 | |
187 } // namespace WTF | 106 } // namespace WTF |
188 | 107 |
189 using WTF::StringView; | 108 using WTF::StringView; |
190 | 109 |
191 #endif | 110 #endif |
OLD | NEW |