Chromium Code Reviews| Index: third_party/WebKit/Source/wtf/text/StringView.h |
| diff --git a/third_party/WebKit/Source/wtf/text/StringView.h b/third_party/WebKit/Source/wtf/text/StringView.h |
| index b0cc71fb41613a3cad235f721fb3b5db1b3fa6a9..01baa98eb8087c73f78a6582b266ccd0f50e987d 100644 |
| --- a/third_party/WebKit/Source/wtf/text/StringView.h |
| +++ b/third_party/WebKit/Source/wtf/text/StringView.h |
| @@ -32,77 +32,134 @@ |
| #define WTF_StringView_h |
| #include "wtf/Allocator.h" |
| -#include "wtf/text/StringImpl.h" |
| +#include "wtf/text/Unicode.h" |
| +#include <cstring> |
| namespace WTF { |
| +class AtomicString; |
| +class String; |
| +class StringImpl; |
| + |
| class WTF_EXPORT StringView { |
| - DISALLOW_NEW(); |
| + DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); |
| public: |
| - StringView() |
| - : m_offset(0) |
| - , m_length(0) |
| - { |
| - } |
| + // Null string. |
| + StringView() { clear(); } |
| + |
| + // From a StringImpl (implemented in StringImpl.h) |
| + StringView(StringImpl*); |
| + StringView(StringImpl*, unsigned offset); |
| + StringView(StringImpl*, unsigned offset, unsigned length); |
| + |
| + // From a String (implemented in WTFString.h) |
| + StringView(const String&); |
| + StringView(const String&, unsigned offset); |
| + StringView(const String&, unsigned offset, unsigned length); |
| + |
| + // From an AtomicString (implemented in in AtomicString.h) |
| + StringView(const AtomicString&); |
| + StringView(const AtomicString&, unsigned offset); |
| + StringView(const AtomicString&, unsigned offset, unsigned length); |
| + |
| + // From a StringView. |
| + StringView(const StringView&, unsigned offset); |
| + StringView(const StringView&, unsigned offset, unsigned length); |
| + |
| + // From a literal string or LChar buffer. |
| + StringView(const LChar* chars); |
| + StringView(const char* chars); |
| + StringView(const LChar* chars, unsigned length); |
| + StringView(const char* chars, unsigned length); |
| + |
| + // From a UChar buffer. |
| + StringView(const UChar* chars); |
| + StringView(const UChar* chars, unsigned length); |
| + |
| + bool isNull() const { return !m_data.bytes; } |
| + 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("^$"
|
| - explicit StringView(PassRefPtr<StringImpl> impl) |
| - : m_impl(impl) |
| - , m_offset(0) |
| - , m_length(m_impl->length()) |
| - { |
| - } |
| + unsigned length() const { return m_length; } |
| - StringView(PassRefPtr<StringImpl> impl, unsigned offset, unsigned length) |
| - : m_impl(impl) |
| - , m_offset(offset) |
| - , m_length(length) |
| - { |
| - ASSERT_WITH_SECURITY_IMPLICATION(offset + length <= m_impl->length()); |
| - } |
| + bool is8Bit() const { return m_is8Bit; } |
| - void narrow(unsigned offset, unsigned length) |
| + void clear() |
| { |
| - ASSERT_WITH_SECURITY_IMPLICATION(offset + length <= m_length); |
| - m_offset += offset; |
| - m_length = length; |
| + m_length = 0; |
| + m_is8Bit = true; |
| + m_data.bytes = nullptr; |
| } |
| - bool isEmpty() const { return !m_length; } |
| - unsigned length() const { return m_length; } |
| - |
| - bool is8Bit() const { return m_impl->is8Bit(); } |
| - |
| const LChar* characters8() const |
| { |
| - if (!m_impl) |
| - return 0; |
| ASSERT(is8Bit()); |
| - return m_impl->characters8() + m_offset; |
| + return m_data.characters8; |
| } |
| const UChar* characters16() const |
| { |
| - if (!m_impl) |
| - return 0; |
| ASSERT(!is8Bit()); |
| - return m_impl->characters16() + m_offset; |
| + return m_data.characters16; |
| } |
| - PassRefPtr<StringImpl> toString() const |
| - { |
| - if (!m_impl) |
| - return m_impl; |
| - if (m_impl->is8Bit()) |
| - return StringImpl::create(characters8(), m_length); |
| - return StringImpl::create(characters16(), m_length); |
| - } |
| + String toString() const; |
| private: |
| - RefPtr<StringImpl> m_impl; |
| - unsigned m_offset; |
| + // Implemented in StringImpl.h |
| + void set(StringImpl&, unsigned offset, unsigned length); |
| + |
| + union DataUnion { |
| + const LChar* characters8; |
| + const UChar* characters16; |
| + const void* bytes; |
| + } m_data; |
| unsigned m_length; |
| + unsigned m_is8Bit : 1; |
| }; |
| +inline StringView::StringView(const StringView& view, unsigned offset, unsigned length) |
| + : m_length(length) |
| + , m_is8Bit(view.is8Bit()) |
| +{ |
| + if (is8Bit()) |
| + m_data.characters8 = view.characters8() + offset; |
| + else |
| + m_data.characters16 = view.characters16() + offset; |
| +} |
| + |
| +inline StringView::StringView(const StringView& view, unsigned offset) |
| + : StringView(view, offset, view.m_length) {} |
| + |
| +inline StringView::StringView(const LChar* chars, unsigned length) |
| + : m_length(length) |
| + , m_is8Bit(true) |
| +{ |
| + SECURITY_DCHECK(!chars || length <= strlen(reinterpret_cast<const char*>(chars))); |
| + m_data.characters8 = chars; |
| +} |
| + |
| +inline StringView::StringView(const char* chars, unsigned length) |
| + : StringView(reinterpret_cast<const LChar*>(chars), length) {} |
| +inline StringView::StringView(const LChar* chars) |
| + : StringView(chars, chars ? strlen(reinterpret_cast<const char*>(chars)) : 0) {} |
| +inline StringView::StringView(const char* chars) |
| + : StringView(reinterpret_cast<const LChar*>(chars)) {} |
| + |
| +// TODO(esprehn): Can't make this an overload of WTF::equal since that makes |
| +// calls to equal() that pass literal strings ambigious. Figure out if we can |
|
haraken
2016/05/26 22:27:22
ambiguous
|
| +// replace all the callers with equalStringView and then rename it to equal(). |
| +bool equalStringView(const StringView&, const StringView&); |
| + |
| +inline bool operator==(const StringView& a, const StringView& b) |
| +{ |
| + return equalStringView(a, b); |
| +} |
| + |
| +inline bool operator!=(const StringView& a, const StringView& b) |
| +{ |
| + return !(a == b); |
| +} |
| + |
| } // namespace WTF |
| using WTF::StringView; |