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*); |
|
esprehn
2016/08/04 08:43:06
const StringImpl* is kind of silly since they're i
| |
| 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 an String, implemented in String.h | 52 // From an String, implemented in String.h |
| 47 inline StringView(const String&, unsigned offset, unsigned length); | 53 inline StringView(const String&, unsigned offset, unsigned length); |
| 48 inline StringView(const String&, unsigned offset); | 54 inline StringView(const String&, unsigned offset); |
| 49 inline StringView(const String&); | 55 inline StringView(const String&); |
| 50 | 56 |
| 51 // From an AtomicString, implemented in AtomicString.h | 57 // From an AtomicString, implemented in AtomicString.h |
| 52 inline StringView(const AtomicString&, unsigned offset, unsigned length); | 58 inline StringView(const AtomicString&, unsigned offset, unsigned length); |
| 53 inline StringView(const AtomicString&, unsigned offset); | 59 inline StringView(const AtomicString&, unsigned offset); |
| 54 inline StringView(const AtomicString&); | 60 inline StringView(const AtomicString&); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 // directly since this == StringView(m_impl). | 124 // directly since this == StringView(m_impl). |
| 119 if (m_impl->bytes() == bytes() && m_length == m_impl->length()) | 125 if (m_impl->bytes() == bytes() && m_length == m_impl->length()) |
| 120 return getPtr(m_impl); | 126 return getPtr(m_impl); |
| 121 return nullptr; | 127 return nullptr; |
| 122 } | 128 } |
| 123 | 129 |
| 124 String toString() const; | 130 String toString() const; |
| 125 AtomicString toAtomicString() const; | 131 AtomicString toAtomicString() const; |
| 126 | 132 |
| 127 private: | 133 private: |
| 128 void set(StringImpl&, unsigned offset, unsigned length); | 134 void set(const StringImpl&, unsigned offset, unsigned length); |
| 129 | 135 |
| 130 // We use the StringImpl to mark for 8bit or 16bit, even for strings where | 136 // We use the StringImpl to mark for 8bit or 16bit, even for strings where |
| 131 // we were constructed from a char pointer. So m_impl->bytes() might have | 137 // we were constructed from a char pointer. So m_impl->bytes() might have |
| 132 // nothing to do with this view's bytes(). | 138 // nothing to do with this view's bytes(). |
| 133 #if DCHECK_IS_ON() | 139 #if DCHECK_IS_ON() |
| 134 RefPtr<StringImpl> m_impl; | 140 RefPtr<StringImpl> m_impl; |
| 135 #else | 141 #else |
| 136 StringImpl* m_impl; | 142 StringImpl* m_impl; |
| 137 #endif | 143 #endif |
| 138 union { | 144 union { |
| 139 const LChar* m_characters8; | 145 const LChar* m_characters8; |
| 140 const UChar* m_characters16; | 146 const UChar* m_characters16; |
| 141 const void* m_bytes; | 147 const void* m_bytes; |
| 142 }; | 148 }; |
| 143 unsigned m_length; | 149 unsigned m_length; |
| 144 }; | 150 }; |
| 145 | 151 |
| 146 inline StringView::StringView(const StringView& view, unsigned offset, unsigned length) | 152 inline StringView::StringView(const StringView& view, unsigned offset, unsigned length) |
| 147 : m_impl(view.m_impl) | 153 : m_impl(view.m_impl) |
| 148 , m_length(length) | 154 , m_length(length) |
| 149 { | 155 { |
| 150 SECURITY_DCHECK(offset + length <= view.length()); | 156 SECURITY_DCHECK(offset + length <= view.length()); |
| 151 if (is8Bit()) | 157 if (is8Bit()) |
| 152 m_characters8 = view.characters8() + offset; | 158 m_characters8 = view.characters8() + offset; |
| 153 else | 159 else |
| 154 m_characters16 = view.characters16() + offset; | 160 m_characters16 = view.characters16() + offset; |
| 155 } | 161 } |
| 156 | 162 |
| 157 inline StringView::StringView(StringImpl* impl) | 163 inline StringView::StringView(const StringImpl* impl) |
| 158 { | 164 { |
| 159 if (!impl) { | 165 if (!impl) { |
| 160 clear(); | 166 clear(); |
| 161 return; | 167 return; |
| 162 } | 168 } |
| 163 m_impl = impl; | 169 m_impl = const_cast<StringImpl*>(impl); |
| 164 m_length = impl->length(); | 170 m_length = impl->length(); |
| 165 m_bytes = impl->bytes(); | 171 m_bytes = impl->bytes(); |
| 166 } | 172 } |
| 167 | 173 |
| 168 inline StringView::StringView(StringImpl* impl, unsigned offset) | 174 inline StringView::StringView(const StringImpl* impl, unsigned offset) |
| 169 { | 175 { |
| 170 impl ? set(*impl, offset, impl->length() - offset) : clear(); | 176 impl ? set(*impl, offset, impl->length() - offset) : clear(); |
| 171 } | 177 } |
| 172 | 178 |
| 173 inline StringView::StringView(StringImpl* impl, unsigned offset, unsigned length ) | 179 inline StringView::StringView(const StringImpl* impl, unsigned offset, unsigned length) |
| 174 { | 180 { |
| 175 impl ? set(*impl, offset, length) : clear(); | 181 impl ? set(*impl, offset, length) : clear(); |
| 176 } | 182 } |
| 177 | 183 |
| 178 inline void StringView::clear() | 184 inline void StringView::clear() |
| 179 { | 185 { |
| 180 m_length = 0; | 186 m_length = 0; |
| 181 m_bytes = nullptr; | 187 m_bytes = nullptr; |
| 182 m_impl = StringImpl::empty(); // mark as 8 bit. | 188 m_impl = StringImpl::empty(); // mark as 8 bit. |
| 183 } | 189 } |
| 184 | 190 |
| 185 inline void StringView::set(StringImpl& impl, unsigned offset, unsigned length) | 191 inline void StringView::set(const StringImpl& impl, unsigned offset, unsigned le ngth) |
| 186 { | 192 { |
| 187 SECURITY_DCHECK(offset + length <= impl.length()); | 193 SECURITY_DCHECK(offset + length <= impl.length()); |
| 188 m_length = length; | 194 m_length = length; |
| 189 m_impl = &impl; | 195 m_impl = const_cast<StringImpl*>(&impl); |
| 190 if (impl.is8Bit()) | 196 if (impl.is8Bit()) |
| 191 m_characters8 = impl.characters8() + offset; | 197 m_characters8 = impl.characters8() + offset; |
| 192 else | 198 else |
| 193 m_characters16 = impl.characters16() + offset; | 199 m_characters16 = impl.characters16() + offset; |
| 194 } | 200 } |
| 195 | 201 |
| 196 WTF_EXPORT bool equalIgnoringASCIICase(const StringView& a, const StringView& b) ; | 202 WTF_EXPORT bool equalIgnoringCase(const StringView&, const StringView&); |
| 203 WTF_EXPORT bool equalIgnoringASCIICase(const StringView&, const StringView&); | |
| 204 | |
| 205 WTF_EXPORT bool equalIgnoringCaseAndNullity(const StringView&, const StringView& ); | |
| 197 | 206 |
| 198 // TODO(esprehn): Can't make this an overload of WTF::equal since that makes | 207 // TODO(esprehn): Can't make this an overload of WTF::equal since that makes |
| 199 // calls to equal() that pass literal strings ambiguous. Figure out if we can | 208 // calls to equal() that pass literal strings ambiguous. Figure out if we can |
| 200 // replace all the callers with equalStringView and then rename it to equal(). | 209 // replace all the callers with equalStringView and then rename it to equal(). |
| 201 WTF_EXPORT bool equalStringView(const StringView&, const StringView&); | 210 WTF_EXPORT bool equalStringView(const StringView&, const StringView&); |
| 202 | 211 |
| 203 inline bool operator==(const StringView& a, const StringView& b) | 212 inline bool operator==(const StringView& a, const StringView& b) |
| 204 { | 213 { |
| 205 return equalStringView(a, b); | 214 return equalStringView(a, b); |
| 206 } | 215 } |
| 207 | 216 |
| 208 inline bool operator!=(const StringView& a, const StringView& b) | 217 inline bool operator!=(const StringView& a, const StringView& b) |
| 209 { | 218 { |
| 210 return !(a == b); | 219 return !(a == b); |
| 211 } | 220 } |
| 212 | 221 |
| 213 } // namespace WTF | 222 } // namespace WTF |
| 214 | 223 |
| 215 using WTF::StringView; | 224 using WTF::StringView; |
| 225 using WTF::equalIgnoringASCIICase; | |
| 226 using WTF::equalIgnoringCase; | |
| 227 | |
| 216 | 228 |
| 217 #endif | 229 #endif |
| OLD | NEW |