OLD | NEW |
---|---|
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 #if DCHECK_IS_ON() | 9 #if DCHECK_IS_ON() |
10 #include "wtf/RefPtr.h" | 10 #include "wtf/RefPtr.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 : StringView(reinterpret_cast<const LChar*>(chars), length) {} | 63 : StringView(reinterpret_cast<const LChar*>(chars), length) {} |
64 StringView(const LChar* chars) | 64 StringView(const LChar* chars) |
65 : StringView(chars, chars ? strlen(reinterpret_cast<const char*>(chars)) : 0) {} | 65 : StringView(chars, chars ? strlen(reinterpret_cast<const char*>(chars)) : 0) {} |
66 StringView(const char* chars) | 66 StringView(const char* chars) |
67 : StringView(reinterpret_cast<const LChar*>(chars)) {} | 67 : StringView(reinterpret_cast<const LChar*>(chars)) {} |
68 | 68 |
69 // From a wide literal string or UChar buffer. | 69 // From a wide literal string or UChar buffer. |
70 StringView(const UChar* chars); | 70 StringView(const UChar* chars); |
71 StringView(const UChar* chars, unsigned length); | 71 StringView(const UChar* chars, unsigned length); |
72 | 72 |
73 // From a byte pointer. | |
74 StringView(const void* bytes, unsigned length, bool is8Bit) | |
75 : m_length(length) | |
76 , m_is8Bit(is8Bit) | |
77 { | |
78 m_data.bytes = bytes; | |
79 } | |
80 | |
73 #if DCHECK_IS_ON() | 81 #if DCHECK_IS_ON() |
74 ~StringView(); | 82 ~StringView(); |
75 #endif | 83 #endif |
76 | 84 |
77 bool isNull() const { return !m_data.bytes; } | 85 bool isNull() const { return !m_data.bytes; } |
78 bool isEmpty() const { return !m_length; } | 86 bool isEmpty() const { return !m_length; } |
79 | 87 |
80 unsigned length() const { return m_length; } | 88 unsigned length() const { return m_length; } |
81 | 89 |
82 bool is8Bit() const { return m_is8Bit; } | 90 bool is8Bit() const { return m_is8Bit; } |
83 | 91 |
84 void clear(); | 92 void clear(); |
85 | 93 |
94 UChar operator[](unsigned i) const | |
95 { | |
96 SECURITY_DCHECK(i < length()); | |
97 if (is8Bit()) | |
98 return characters8()[i]; | |
99 return characters16()[i]; | |
100 } | |
101 | |
86 const LChar* characters8() const | 102 const LChar* characters8() const |
87 { | 103 { |
88 ASSERT(is8Bit()); | 104 ASSERT(is8Bit()); |
89 return m_data.characters8; | 105 return m_data.characters8; |
90 } | 106 } |
91 | 107 |
92 const UChar* characters16() const | 108 const UChar* characters16() const |
93 { | 109 { |
94 ASSERT(!is8Bit()); | 110 ASSERT(!is8Bit()); |
95 return m_data.characters16; | 111 return m_data.characters16; |
96 } | 112 } |
97 | 113 |
114 const void* bytes() const { return m_data.bytes; } | |
115 | |
98 String toString() const; | 116 String toString() const; |
99 | 117 |
100 private: | 118 private: |
101 void set(StringImpl&, unsigned offset, unsigned length); | 119 void set(StringImpl&, unsigned offset, unsigned length); |
102 | 120 |
103 #if DCHECK_IS_ON() | 121 #if DCHECK_IS_ON() |
104 RefPtr<StringImpl> m_impl; | 122 RefPtr<StringImpl> m_impl; |
105 #endif | 123 #endif |
106 union DataUnion { | 124 union DataUnion { |
107 const LChar* characters8; | 125 const LChar* characters8; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 m_is8Bit = impl.is8Bit(); | 180 m_is8Bit = impl.is8Bit(); |
163 #if DCHECK_IS_ON() | 181 #if DCHECK_IS_ON() |
164 m_impl = &impl; | 182 m_impl = &impl; |
165 #endif | 183 #endif |
166 if (m_is8Bit) | 184 if (m_is8Bit) |
167 m_data.characters8 = impl.characters8() + offset; | 185 m_data.characters8 = impl.characters8() + offset; |
168 else | 186 else |
169 m_data.characters16 = impl.characters16() + offset; | 187 m_data.characters16 = impl.characters16() + offset; |
170 } | 188 } |
171 | 189 |
190 WTF_EXPORT bool equalIgnoringASCIICase(StringView a, StringView b); | |
Timothy Loh
2016/06/01 05:51:32
Maybe const StringView& for consistency with the b
esprehn
2016/06/01 06:19:04
Interestingly base thinks we should pass them by v
| |
191 | |
172 // TODO(esprehn): Can't make this an overload of WTF::equal since that makes | 192 // 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 | 193 // 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(). | 194 // replace all the callers with equalStringView and then rename it to equal(). |
175 WTF_EXPORT bool equalStringView(const StringView&, const StringView&); | 195 WTF_EXPORT bool equalStringView(const StringView&, const StringView&); |
176 | 196 |
177 inline bool operator==(const StringView& a, const StringView& b) | 197 inline bool operator==(const StringView& a, const StringView& b) |
178 { | 198 { |
179 return equalStringView(a, b); | 199 return equalStringView(a, b); |
180 } | 200 } |
181 | 201 |
182 inline bool operator!=(const StringView& a, const StringView& b) | 202 inline bool operator!=(const StringView& a, const StringView& b) |
183 { | 203 { |
184 return !(a == b); | 204 return !(a == b); |
185 } | 205 } |
186 | 206 |
187 } // namespace WTF | 207 } // namespace WTF |
188 | 208 |
189 using WTF::StringView; | 209 using WTF::StringView; |
190 | 210 |
191 #endif | 211 #endif |
OLD | NEW |