| 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() |
| 11 #include "wtf/RefPtr.h" | 11 #include "wtf/RefPtr.h" |
| 12 #endif | 12 #endif |
| 13 #include "wtf/text/AtomicString.h" | |
| 14 #include "wtf/text/StringImpl.h" | 13 #include "wtf/text/StringImpl.h" |
| 15 #include "wtf/text/Unicode.h" | 14 #include "wtf/text/Unicode.h" |
| 16 #include "wtf/text/WTFString.h" | |
| 17 #include <cstring> | 15 #include <cstring> |
| 18 | 16 |
| 19 namespace WTF { | 17 namespace WTF { |
| 20 | 18 |
| 19 class AtomicString; |
| 20 class String; |
| 21 |
| 21 // A string like object that wraps either an 8bit or 16bit byte sequence | 22 // A string like object that wraps either an 8bit or 16bit byte sequence |
| 22 // and keeps track of the length and the type, it does NOT own the bytes. | 23 // and keeps track of the length and the type, it does NOT own the bytes. |
| 23 // | 24 // |
| 24 // Since StringView does not own the bytes creating a StringView from a String, | 25 // Since StringView does not own the bytes creating a StringView from a String, |
| 25 // then calling clear() on the String will result in a use-after-free. Asserts | 26 // then calling clear() on the String will result in a use-after-free. Asserts |
| 26 // in ~StringView attempt to enforce this for most common cases. | 27 // in ~StringView attempt to enforce this for most common cases. |
| 27 // | 28 // |
| 28 // See base/strings/string_piece.h for more details. | 29 // See base/strings/string_piece.h for more details. |
| 29 class WTF_EXPORT StringView { | 30 class WTF_EXPORT StringView { |
| 30 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); | 31 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); |
| 31 public: | 32 public: |
| 32 // Null string. | 33 // Null string. |
| 33 StringView() { clear(); } | 34 StringView() { clear(); } |
| 34 | 35 |
| 35 // From a StringView: | 36 // From a StringView: |
| 36 StringView(const StringView&, unsigned offset, unsigned length); | 37 StringView(const StringView&, unsigned offset, unsigned length); |
| 37 StringView(const StringView& view, unsigned offset) | 38 StringView(const StringView& view, unsigned offset) |
| 38 : StringView(view, offset, view.m_length - offset) {} | 39 : StringView(view, offset, view.m_length - offset) {} |
| 39 | 40 |
| 40 // From a StringImpl: | 41 // From a StringImpl: |
| 41 StringView(StringImpl*); | 42 StringView(StringImpl*); |
| 42 StringView(StringImpl*, unsigned offset); | 43 StringView(StringImpl*, unsigned offset); |
| 43 StringView(StringImpl*, unsigned offset, unsigned length); | 44 StringView(StringImpl*, unsigned offset, unsigned length); |
| 44 | 45 |
| 45 // From a String: | 46 // From an String, implemented in String.h |
| 46 StringView(const String& string, unsigned offset, unsigned length) | 47 inline StringView(const String&, unsigned offset, unsigned length); |
| 47 : StringView(string.impl(), offset, length) {} | 48 inline StringView(const String&, unsigned offset); |
| 48 StringView(const String& string, unsigned offset) | 49 inline StringView(const String&); |
| 49 : StringView(string.impl(), offset) {} | |
| 50 StringView(const String& string) | |
| 51 : StringView(string.impl()) {} | |
| 52 | 50 |
| 53 // From an AtomicString: | 51 // From an AtomicString, implemented in AtomicString.h |
| 54 StringView(const AtomicString& string, unsigned offset, unsigned length) | 52 inline StringView(const AtomicString&, unsigned offset, unsigned length); |
| 55 : StringView(string.impl(), offset, length) {} | 53 inline StringView(const AtomicString&, unsigned offset); |
| 56 StringView(const AtomicString& string, unsigned offset) | 54 inline StringView(const AtomicString&); |
| 57 : StringView(string.impl(), offset) {} | |
| 58 StringView(const AtomicString& string) | |
| 59 : StringView(string.impl()) {} | |
| 60 | 55 |
| 61 // From a literal string or LChar buffer: | 56 // From a literal string or LChar buffer: |
| 62 StringView(const LChar* chars, unsigned length) | 57 StringView(const LChar* chars, unsigned length) |
| 63 : StringView(reinterpret_cast<const void*>(chars), length, true) {} | 58 : m_impl(StringImpl::empty()) |
| 59 , m_characters8(chars) |
| 60 , m_length(length) {} |
| 64 StringView(const char* chars, unsigned length) | 61 StringView(const char* chars, unsigned length) |
| 65 : StringView(reinterpret_cast<const LChar*>(chars), length) {} | 62 : StringView(reinterpret_cast<const LChar*>(chars), length) {} |
| 66 StringView(const LChar* chars) | 63 StringView(const LChar* chars) |
| 67 : StringView(chars, chars ? strlen(reinterpret_cast<const char*>(chars))
: 0) {} | 64 : StringView(chars, chars ? strlen(reinterpret_cast<const char*>(chars))
: 0) {} |
| 68 StringView(const char* chars) | 65 StringView(const char* chars) |
| 69 : StringView(reinterpret_cast<const LChar*>(chars)) {} | 66 : StringView(reinterpret_cast<const LChar*>(chars)) {} |
| 70 | 67 |
| 71 // From a wide literal string or UChar buffer. | 68 // From a wide literal string or UChar buffer. |
| 72 StringView(const UChar* chars, unsigned length) | 69 StringView(const UChar* chars, unsigned length) |
| 73 : StringView(reinterpret_cast<const void*>(chars), length, false) {} | 70 : m_impl(StringImpl::empty16Bit()) |
| 71 , m_characters16(chars) |
| 72 , m_length(length) {} |
| 74 StringView(const UChar* chars); | 73 StringView(const UChar* chars); |
| 75 | 74 |
| 76 // From a byte pointer. | |
| 77 StringView(const void* bytes, unsigned length, bool is8Bit) | |
| 78 : m_impl(is8Bit ? StringImpl::empty() : StringImpl::empty16Bit()) | |
| 79 , m_bytes(bytes) | |
| 80 , m_length(length) {} | |
| 81 | |
| 82 #if DCHECK_IS_ON() | 75 #if DCHECK_IS_ON() |
| 83 ~StringView(); | 76 ~StringView(); |
| 84 #endif | 77 #endif |
| 85 | 78 |
| 86 bool isNull() const { return !m_bytes; } | 79 bool isNull() const { return !m_bytes; } |
| 87 bool isEmpty() const { return !m_length; } | 80 bool isEmpty() const { return !m_length; } |
| 88 | 81 |
| 89 unsigned length() const { return m_length; } | 82 unsigned length() const { return m_length; } |
| 90 | 83 |
| 91 bool is8Bit() const { DCHECK(m_impl); return m_impl->is8Bit(); } | 84 bool is8Bit() const { DCHECK(m_impl); return m_impl->is8Bit(); } |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 inline bool operator!=(const StringView& a, const StringView& b) | 208 inline bool operator!=(const StringView& a, const StringView& b) |
| 216 { | 209 { |
| 217 return !(a == b); | 210 return !(a == b); |
| 218 } | 211 } |
| 219 | 212 |
| 220 } // namespace WTF | 213 } // namespace WTF |
| 221 | 214 |
| 222 using WTF::StringView; | 215 using WTF::StringView; |
| 223 | 216 |
| 224 #endif | 217 #endif |
| OLD | NEW |