Chromium Code Reviews| 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 #include "wtf/GetPtr.h" | 9 #include "wtf/GetPtr.h" |
| 10 #if DCHECK_IS_ON() | 10 #if DCHECK_IS_ON() |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 public: | 32 public: |
| 33 // Null string. | 33 // Null string. |
| 34 StringView() { clear(); } | 34 StringView() { clear(); } |
| 35 | 35 |
| 36 // From a StringView: | 36 // From a StringView: |
| 37 StringView(const StringView&, unsigned offset, unsigned length); | 37 StringView(const StringView&, unsigned offset, unsigned length); |
| 38 StringView(const StringView& view, unsigned offset) | 38 StringView(const StringView& view, unsigned offset) |
| 39 : StringView(view, offset, view.m_length - offset) {} | 39 : StringView(view, offset, view.m_length - offset) {} |
| 40 | 40 |
| 41 // From a StringImpl: | 41 // From a StringImpl: |
| 42 StringView(StringImpl*); | 42 StringView(const StringImpl*); |
|
haraken
2016/08/04 09:05:28
Add explicit.
esprehn
2016/08/04 18:30:08
It's implicit on purpose, otherwise doing
StringI
| |
| 43 StringView(StringImpl*, unsigned offset); | 43 StringView(const StringImpl*, unsigned offset); |
| 44 StringView(StringImpl*, unsigned offset, unsigned length); | 44 StringView(const StringImpl*, unsigned offset, unsigned length); |
| 45 | |
| 46 // From a non-null StringImpl. | |
| 47 StringView(const StringImpl& impl) | |
| 48 : m_impl(const_cast<StringImpl*>(&impl)) | |
| 49 , m_bytes(impl.bytes()) | |
| 50 , m_length(impl.length()) {} | |
| 45 | 51 |
| 46 // From a non-null StringImpl, avoids the null check. | 52 // From a non-null StringImpl, avoids the null check. |
| 47 StringView(StringImpl& impl) | 53 StringView(StringImpl& impl) |
| 48 : m_impl(&impl) | 54 : m_impl(&impl) |
| 49 , m_bytes(impl.bytes()) | 55 , m_bytes(impl.bytes()) |
| 50 , m_length(impl.length()) {} | 56 , m_length(impl.length()) {} |
| 51 StringView(StringImpl&, unsigned offset); | 57 StringView(StringImpl&, unsigned offset); |
| 52 StringView(StringImpl&, unsigned offset, unsigned length); | 58 StringView(StringImpl&, unsigned offset, unsigned length); |
| 53 | 59 |
| 54 // From an String, implemented in String.h | 60 // From an String, implemented in String.h |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 126 // directly since this == StringView(m_impl). | 132 // directly since this == StringView(m_impl). |
| 127 if (m_impl->bytes() == bytes() && m_length == m_impl->length()) | 133 if (m_impl->bytes() == bytes() && m_length == m_impl->length()) |
| 128 return getPtr(m_impl); | 134 return getPtr(m_impl); |
| 129 return nullptr; | 135 return nullptr; |
| 130 } | 136 } |
| 131 | 137 |
| 132 String toString() const; | 138 String toString() const; |
| 133 AtomicString toAtomicString() const; | 139 AtomicString toAtomicString() const; |
| 134 | 140 |
| 135 private: | 141 private: |
| 136 void set(StringImpl&, unsigned offset, unsigned length); | 142 void set(const StringImpl&, unsigned offset, unsigned length); |
| 137 | 143 |
| 138 // We use the StringImpl to mark for 8bit or 16bit, even for strings where | 144 // We use the StringImpl to mark for 8bit or 16bit, even for strings where |
| 139 // we were constructed from a char pointer. So m_impl->bytes() might have | 145 // we were constructed from a char pointer. So m_impl->bytes() might have |
| 140 // nothing to do with this view's bytes(). | 146 // nothing to do with this view's bytes(). |
| 141 #if DCHECK_IS_ON() | 147 #if DCHECK_IS_ON() |
| 142 RefPtr<StringImpl> m_impl; | 148 RefPtr<StringImpl> m_impl; |
| 143 #else | 149 #else |
| 144 StringImpl* m_impl; | 150 StringImpl* m_impl; |
| 145 #endif | 151 #endif |
| 146 union { | 152 union { |
| 147 const LChar* m_characters8; | 153 const LChar* m_characters8; |
| 148 const UChar* m_characters16; | 154 const UChar* m_characters16; |
| 149 const void* m_bytes; | 155 const void* m_bytes; |
| 150 }; | 156 }; |
| 151 unsigned m_length; | 157 unsigned m_length; |
| 152 }; | 158 }; |
| 153 | 159 |
| 154 inline StringView::StringView(const StringView& view, unsigned offset, unsigned length) | 160 inline StringView::StringView(const StringView& view, unsigned offset, unsigned length) |
| 155 : m_impl(view.m_impl) | 161 : m_impl(view.m_impl) |
| 156 , m_length(length) | 162 , m_length(length) |
| 157 { | 163 { |
| 158 SECURITY_DCHECK(offset + length <= view.length()); | 164 SECURITY_DCHECK(offset + length <= view.length()); |
| 159 if (is8Bit()) | 165 if (is8Bit()) |
| 160 m_characters8 = view.characters8() + offset; | 166 m_characters8 = view.characters8() + offset; |
| 161 else | 167 else |
| 162 m_characters16 = view.characters16() + offset; | 168 m_characters16 = view.characters16() + offset; |
| 163 } | 169 } |
| 164 | 170 |
| 165 inline StringView::StringView(StringImpl* impl) | 171 inline StringView::StringView(const StringImpl* impl) |
| 166 { | 172 { |
| 167 if (!impl) { | 173 if (!impl) { |
| 168 clear(); | 174 clear(); |
| 169 return; | 175 return; |
| 170 } | 176 } |
| 171 m_impl = impl; | 177 m_impl = const_cast<StringImpl*>(impl); |
| 172 m_length = impl->length(); | 178 m_length = impl->length(); |
| 173 m_bytes = impl->bytes(); | 179 m_bytes = impl->bytes(); |
| 174 } | 180 } |
| 175 | 181 |
| 176 inline StringView::StringView(StringImpl* impl, unsigned offset) | 182 inline StringView::StringView(const StringImpl* impl, unsigned offset) |
| 177 { | 183 { |
| 178 impl ? set(*impl, offset, impl->length() - offset) : clear(); | 184 impl ? set(*impl, offset, impl->length() - offset) : clear(); |
| 179 } | 185 } |
| 180 | 186 |
| 181 inline StringView::StringView(StringImpl* impl, unsigned offset, unsigned length ) | 187 inline StringView::StringView(const StringImpl* impl, unsigned offset, unsigned length) |
| 182 { | 188 { |
| 183 impl ? set(*impl, offset, length) : clear(); | 189 impl ? set(*impl, offset, length) : clear(); |
| 184 } | 190 } |
| 185 | 191 |
| 186 inline StringView::StringView(StringImpl& impl, unsigned offset) | 192 inline StringView::StringView(StringImpl& impl, unsigned offset) |
| 187 { | 193 { |
| 188 set(impl, offset, impl.length() - offset); | 194 set(impl, offset, impl.length() - offset); |
| 189 } | 195 } |
| 190 | 196 |
| 191 inline StringView::StringView(StringImpl& impl, unsigned offset, unsigned length ) | 197 inline StringView::StringView(StringImpl& impl, unsigned offset, unsigned length ) |
| 192 { | 198 { |
| 193 set(impl, offset, length); | 199 set(impl, offset, length); |
| 194 } | 200 } |
| 195 | 201 |
| 196 inline void StringView::clear() | 202 inline void StringView::clear() |
| 197 { | 203 { |
| 198 m_length = 0; | 204 m_length = 0; |
| 199 m_bytes = nullptr; | 205 m_bytes = nullptr; |
| 200 m_impl = StringImpl::empty(); // mark as 8 bit. | 206 m_impl = StringImpl::empty(); // mark as 8 bit. |
| 201 } | 207 } |
| 202 | 208 |
| 203 inline void StringView::set(StringImpl& impl, unsigned offset, unsigned length) | 209 inline void StringView::set(const StringImpl& impl, unsigned offset, unsigned le ngth) |
| 204 { | 210 { |
| 205 SECURITY_DCHECK(offset + length <= impl.length()); | 211 SECURITY_DCHECK(offset + length <= impl.length()); |
| 206 m_length = length; | 212 m_length = length; |
| 207 m_impl = &impl; | 213 m_impl = const_cast<StringImpl*>(&impl); |
| 208 if (impl.is8Bit()) | 214 if (impl.is8Bit()) |
| 209 m_characters8 = impl.characters8() + offset; | 215 m_characters8 = impl.characters8() + offset; |
| 210 else | 216 else |
| 211 m_characters16 = impl.characters16() + offset; | 217 m_characters16 = impl.characters16() + offset; |
| 212 } | 218 } |
| 213 | 219 |
| 214 WTF_EXPORT bool equalIgnoringASCIICase(const StringView& a, const StringView& b) ; | 220 WTF_EXPORT bool equalIgnoringCase(const StringView&, const StringView&); |
| 221 WTF_EXPORT bool equalIgnoringASCIICase(const StringView&, const StringView&); | |
| 222 | |
| 223 WTF_EXPORT bool equalIgnoringCaseAndNullity(const StringView&, const StringView& ); | |
| 215 | 224 |
| 216 // TODO(esprehn): Can't make this an overload of WTF::equal since that makes | 225 // TODO(esprehn): Can't make this an overload of WTF::equal since that makes |
| 217 // calls to equal() that pass literal strings ambiguous. Figure out if we can | 226 // calls to equal() that pass literal strings ambiguous. Figure out if we can |
| 218 // replace all the callers with equalStringView and then rename it to equal(). | 227 // replace all the callers with equalStringView and then rename it to equal(). |
| 219 WTF_EXPORT bool equalStringView(const StringView&, const StringView&); | 228 WTF_EXPORT bool equalStringView(const StringView&, const StringView&); |
| 220 | 229 |
| 221 inline bool operator==(const StringView& a, const StringView& b) | 230 inline bool operator==(const StringView& a, const StringView& b) |
| 222 { | 231 { |
| 223 return equalStringView(a, b); | 232 return equalStringView(a, b); |
| 224 } | 233 } |
| 225 | 234 |
| 226 inline bool operator!=(const StringView& a, const StringView& b) | 235 inline bool operator!=(const StringView& a, const StringView& b) |
| 227 { | 236 { |
| 228 return !(a == b); | 237 return !(a == b); |
| 229 } | 238 } |
| 230 | 239 |
| 231 } // namespace WTF | 240 } // namespace WTF |
| 232 | 241 |
| 233 using WTF::StringView; | 242 using WTF::StringView; |
| 243 using WTF::equalIgnoringASCIICase; | |
| 244 using WTF::equalIgnoringCase; | |
| 245 | |
| 234 | 246 |
| 235 #endif | 247 #endif |
| OLD | NEW |