OLD | NEW |
1 /* | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 // Use of this source code is governed by a BSD-style license that can be |
3 * | 3 // found in the LICENSE file. |
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 */ | |
30 | 4 |
31 #ifndef WTF_StringView_h | 5 #ifndef WTF_StringView_h |
32 #define WTF_StringView_h | 6 #define WTF_StringView_h |
33 | 7 |
34 #include "wtf/Allocator.h" | 8 #include "wtf/Allocator.h" |
| 9 #if DCHECK_IS_ON() |
| 10 #include "wtf/RefPtr.h" |
| 11 #endif |
| 12 #include "wtf/text/AtomicString.h" |
35 #include "wtf/text/StringImpl.h" | 13 #include "wtf/text/StringImpl.h" |
| 14 #include "wtf/text/Unicode.h" |
| 15 #include "wtf/text/WTFString.h" |
| 16 #include <cstring> |
36 | 17 |
37 namespace WTF { | 18 namespace WTF { |
38 | 19 |
| 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. |
39 class WTF_EXPORT StringView { | 28 class WTF_EXPORT StringView { |
40 DISALLOW_NEW(); | 29 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); |
41 public: | 30 public: |
42 StringView() | 31 // Null string. |
43 : m_offset(0) | 32 StringView() { clear(); } |
44 , m_length(0) | |
45 { | |
46 } | |
47 | 33 |
48 explicit StringView(PassRefPtr<StringImpl> impl) | 34 // From a StringView: |
49 : m_impl(impl) | 35 StringView(const StringView&, unsigned offset, unsigned length); |
50 , m_offset(0) | 36 StringView(const StringView& view, unsigned offset) |
51 , m_length(m_impl->length()) | 37 : StringView(view, offset, view.m_length - offset) {} |
52 { | |
53 } | |
54 | 38 |
55 StringView(PassRefPtr<StringImpl> impl, unsigned offset, unsigned length) | 39 // From a StringImpl: |
56 : m_impl(impl) | 40 StringView(StringImpl*); |
57 , m_offset(offset) | 41 StringView(StringImpl*, unsigned offset); |
58 , m_length(length) | 42 StringView(StringImpl*, unsigned offset, unsigned length); |
59 { | |
60 ASSERT_WITH_SECURITY_IMPLICATION(offset + length <= m_impl->length()); | |
61 } | |
62 | 43 |
63 void narrow(unsigned offset, unsigned length) | 44 // From a String: |
64 { | 45 StringView(const String& string, unsigned offset, unsigned length) |
65 ASSERT_WITH_SECURITY_IMPLICATION(offset + length <= m_length); | 46 : StringView(string.impl(), offset, length) {} |
66 m_offset += offset; | 47 StringView(const String& string, unsigned offset) |
67 m_length = length; | 48 : StringView(string.impl(), offset) {} |
68 } | 49 StringView(const String& string) |
| 50 : StringView(string, 0, string.length()) {} |
69 | 51 |
| 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; } |
70 bool isEmpty() const { return !m_length; } | 78 bool isEmpty() const { return !m_length; } |
| 79 |
71 unsigned length() const { return m_length; } | 80 unsigned length() const { return m_length; } |
72 | 81 |
73 bool is8Bit() const { return m_impl->is8Bit(); } | 82 bool is8Bit() const { return m_is8Bit; } |
| 83 |
| 84 void clear(); |
74 | 85 |
75 const LChar* characters8() const | 86 const LChar* characters8() const |
76 { | 87 { |
77 if (!m_impl) | |
78 return 0; | |
79 ASSERT(is8Bit()); | 88 ASSERT(is8Bit()); |
80 return m_impl->characters8() + m_offset; | 89 return m_data.characters8; |
81 } | 90 } |
82 | 91 |
83 const UChar* characters16() const | 92 const UChar* characters16() const |
84 { | 93 { |
85 if (!m_impl) | |
86 return 0; | |
87 ASSERT(!is8Bit()); | 94 ASSERT(!is8Bit()); |
88 return m_impl->characters16() + m_offset; | 95 return m_data.characters16; |
89 } | 96 } |
90 | 97 |
91 PassRefPtr<StringImpl> toString() const | 98 String 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() |
101 RefPtr<StringImpl> m_impl; | 104 RefPtr<StringImpl> m_impl; |
102 unsigned m_offset; | 105 #endif |
| 106 union DataUnion { |
| 107 const LChar* characters8; |
| 108 const UChar* characters16; |
| 109 const void* bytes; |
| 110 } m_data; |
103 unsigned m_length; | 111 unsigned m_length; |
| 112 unsigned m_is8Bit : 1; |
104 }; | 113 }; |
105 | 114 |
| 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 |
106 } // namespace WTF | 187 } // namespace WTF |
107 | 188 |
108 using WTF::StringView; | 189 using WTF::StringView; |
109 | 190 |
110 #endif | 191 #endif |
OLD | NEW |