OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 14 matching lines...) Expand all Loading... | |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #ifndef WTF_StringView_h | 31 #ifndef WTF_StringView_h |
32 #define WTF_StringView_h | 32 #define WTF_StringView_h |
33 | 33 |
34 #include "wtf/Allocator.h" | 34 #include "wtf/Allocator.h" |
35 #include "wtf/text/StringImpl.h" | 35 #include "wtf/text/Unicode.h" |
36 #include <cstring> | |
36 | 37 |
37 namespace WTF { | 38 namespace WTF { |
38 | 39 |
40 class AtomicString; | |
41 class String; | |
42 class StringImpl; | |
43 | |
39 class WTF_EXPORT StringView { | 44 class WTF_EXPORT StringView { |
40 DISALLOW_NEW(); | 45 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); |
41 public: | 46 public: |
42 StringView() | 47 // Null string. |
43 : m_offset(0) | 48 StringView() { clear(); } |
44 , m_length(0) | |
45 { | |
46 } | |
47 | 49 |
48 explicit StringView(PassRefPtr<StringImpl> impl) | 50 // From a StringImpl (implemented in StringImpl.h) |
49 : m_impl(impl) | 51 StringView(StringImpl*); |
50 , m_offset(0) | 52 StringView(StringImpl*, unsigned offset); |
51 , m_length(m_impl->length()) | 53 StringView(StringImpl*, unsigned offset, unsigned length); |
52 { | |
53 } | |
54 | 54 |
55 StringView(PassRefPtr<StringImpl> impl, unsigned offset, unsigned length) | 55 // From a String (implemented in WTFString.h) |
56 : m_impl(impl) | 56 StringView(const String&); |
57 , m_offset(offset) | 57 StringView(const String&, unsigned offset); |
58 , m_length(length) | 58 StringView(const String&, unsigned offset, unsigned length); |
59 { | |
60 ASSERT_WITH_SECURITY_IMPLICATION(offset + length <= m_impl->length()); | |
61 } | |
62 | 59 |
63 void narrow(unsigned offset, unsigned length) | 60 // From an AtomicString (implemented in in AtomicString.h) |
64 { | 61 StringView(const AtomicString&); |
65 ASSERT_WITH_SECURITY_IMPLICATION(offset + length <= m_length); | 62 StringView(const AtomicString&, unsigned offset); |
66 m_offset += offset; | 63 StringView(const AtomicString&, unsigned offset, unsigned length); |
67 m_length = length; | |
68 } | |
69 | 64 |
65 // From a StringView. | |
66 StringView(const StringView&, unsigned offset); | |
67 StringView(const StringView&, unsigned offset, unsigned length); | |
68 | |
69 // From a literal string or LChar buffer. | |
70 StringView(const LChar* chars); | |
71 StringView(const char* chars); | |
72 StringView(const LChar* chars, unsigned length); | |
73 StringView(const char* chars, unsigned length); | |
74 | |
75 // From a UChar buffer. | |
76 StringView(const UChar* chars); | |
77 StringView(const UChar* chars, unsigned length); | |
78 | |
79 bool isNull() const { return !m_data.bytes; } | |
70 bool isEmpty() const { return !m_length; } | 80 bool isEmpty() const { return !m_length; } |
haraken
2016/05/26 22:27:22
I'm not sure if we need to distinguish a null Stri
esprehn
2016/05/26 22:40:21
This has the same semantics as String, which are n
haraken
2016/05/26 22:45:24
Yes, but that's an optimization for WTFString to a
esprehn
2016/05/26 22:48:40
We do need to, otherwise code like ScriptRegexp::m
esprehn
2016/05/26 22:57:41
literally ScriptRegex would be:
ScriptRegexp("^$"
| |
81 | |
71 unsigned length() const { return m_length; } | 82 unsigned length() const { return m_length; } |
72 | 83 |
73 bool is8Bit() const { return m_impl->is8Bit(); } | 84 bool is8Bit() const { return m_is8Bit; } |
85 | |
86 void clear() | |
87 { | |
88 m_length = 0; | |
89 m_is8Bit = true; | |
90 m_data.bytes = nullptr; | |
91 } | |
74 | 92 |
75 const LChar* characters8() const | 93 const LChar* characters8() const |
76 { | 94 { |
77 if (!m_impl) | |
78 return 0; | |
79 ASSERT(is8Bit()); | 95 ASSERT(is8Bit()); |
80 return m_impl->characters8() + m_offset; | 96 return m_data.characters8; |
81 } | 97 } |
82 | 98 |
83 const UChar* characters16() const | 99 const UChar* characters16() const |
84 { | 100 { |
85 if (!m_impl) | |
86 return 0; | |
87 ASSERT(!is8Bit()); | 101 ASSERT(!is8Bit()); |
88 return m_impl->characters16() + m_offset; | 102 return m_data.characters16; |
89 } | 103 } |
90 | 104 |
91 PassRefPtr<StringImpl> toString() const | 105 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 | 106 |
100 private: | 107 private: |
101 RefPtr<StringImpl> m_impl; | 108 // Implemented in StringImpl.h |
102 unsigned m_offset; | 109 void set(StringImpl&, unsigned offset, unsigned length); |
110 | |
111 union DataUnion { | |
112 const LChar* characters8; | |
113 const UChar* characters16; | |
114 const void* bytes; | |
115 } m_data; | |
103 unsigned m_length; | 116 unsigned m_length; |
117 unsigned m_is8Bit : 1; | |
104 }; | 118 }; |
105 | 119 |
120 inline StringView::StringView(const StringView& view, unsigned offset, unsigned length) | |
121 : m_length(length) | |
122 , m_is8Bit(view.is8Bit()) | |
123 { | |
124 if (is8Bit()) | |
125 m_data.characters8 = view.characters8() + offset; | |
126 else | |
127 m_data.characters16 = view.characters16() + offset; | |
128 } | |
129 | |
130 inline StringView::StringView(const StringView& view, unsigned offset) | |
131 : StringView(view, offset, view.m_length) {} | |
132 | |
133 inline StringView::StringView(const LChar* chars, unsigned length) | |
134 : m_length(length) | |
135 , m_is8Bit(true) | |
136 { | |
137 SECURITY_DCHECK(!chars || length <= strlen(reinterpret_cast<const char*>(cha rs))); | |
138 m_data.characters8 = chars; | |
139 } | |
140 | |
141 inline StringView::StringView(const char* chars, unsigned length) | |
142 : StringView(reinterpret_cast<const LChar*>(chars), length) {} | |
143 inline StringView::StringView(const LChar* chars) | |
144 : StringView(chars, chars ? strlen(reinterpret_cast<const char*>(chars)) : 0 ) {} | |
145 inline StringView::StringView(const char* chars) | |
146 : StringView(reinterpret_cast<const LChar*>(chars)) {} | |
147 | |
148 // TODO(esprehn): Can't make this an overload of WTF::equal since that makes | |
149 // calls to equal() that pass literal strings ambigious. Figure out if we can | |
haraken
2016/05/26 22:27:22
ambiguous
| |
150 // replace all the callers with equalStringView and then rename it to equal(). | |
151 bool equalStringView(const StringView&, const StringView&); | |
152 | |
153 inline bool operator==(const StringView& a, const StringView& b) | |
154 { | |
155 return equalStringView(a, b); | |
156 } | |
157 | |
158 inline bool operator!=(const StringView& a, const StringView& b) | |
159 { | |
160 return !(a == b); | |
161 } | |
162 | |
106 } // namespace WTF | 163 } // namespace WTF |
107 | 164 |
108 using WTF::StringView; | 165 using WTF::StringView; |
109 | 166 |
110 #endif | 167 #endif |
OLD | NEW |