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..b595bc980131b5f6f751edbe1a0b01fd9d0d8d93 100644 |
--- a/third_party/WebKit/Source/wtf/text/StringView.h |
+++ b/third_party/WebKit/Source/wtf/text/StringView.h |
@@ -1,108 +1,189 @@ |
-/* |
- * Copyright (C) 2013 Google Inc. All rights reserved. |
- * |
- * Redistribution and use in source and binary forms, with or without |
- * modification, are permitted provided that the following conditions are |
- * met: |
- * |
- * * Redistributions of source code must retain the above copyright |
- * notice, this list of conditions and the following disclaimer. |
- * * Redistributions in binary form must reproduce the above |
- * copyright notice, this list of conditions and the following disclaimer |
- * in the documentation and/or other materials provided with the |
- * distribution. |
- * * Neither the name of Google Inc. nor the names of its |
- * contributors may be used to endorse or promote products derived from |
- * this software without specific prior written permission. |
- * |
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
- */ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
#ifndef WTF_StringView_h |
#define WTF_StringView_h |
#include "wtf/Allocator.h" |
+#if DCHECK_IS_ON() |
+#include "wtf/RefPtr.h" |
+#endif |
+#include "wtf/text/AtomicString.h" |
#include "wtf/text/StringImpl.h" |
+#include "wtf/text/Unicode.h" |
+#include "wtf/text/WTFString.h" |
+#include <cstring> |
namespace WTF { |
+// A string like object that wraps either an 8bit or 16bit byte sequence |
+// and keeps track of the length and the type, it does NOT own the bytes. |
+// |
+// Since StringView does not own the bytes creating a StringView from a String, |
+// then calling clear() on the string will result in a use-after-free. Asserts |
+// in ~StringView attempt to enforce this for most common cases. |
+// |
+// See base/strings/string_piece.h for more details. |
class WTF_EXPORT StringView { |
- DISALLOW_NEW(); |
+ DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); |
public: |
- StringView() |
- : m_offset(0) |
- , m_length(0) |
- { |
- } |
- |
- explicit StringView(PassRefPtr<StringImpl> impl) |
- : m_impl(impl) |
- , m_offset(0) |
- , m_length(m_impl->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()); |
- } |
- |
- void narrow(unsigned offset, unsigned length) |
- { |
- ASSERT_WITH_SECURITY_IMPLICATION(offset + length <= m_length); |
- m_offset += offset; |
- m_length = length; |
- } |
+ // Null string. |
+ StringView() { clear(); } |
+ |
+ // From a StringView: |
+ StringView(const StringView&, unsigned offset, unsigned length); |
+ StringView(const StringView& view, unsigned offset) |
+ : StringView(view, offset, view.m_length - offset) {} |
+ |
+ // From a StringImpl: |
+ StringView(StringImpl*); |
+ StringView(StringImpl*, unsigned offset); |
+ StringView(StringImpl*, unsigned offset, unsigned length); |
+ |
+ // From a String: |
+ StringView(const String& string, unsigned offset, unsigned length) |
+ : StringView(string.impl(), offset, length) {} |
+ StringView(const String& string, unsigned offset) |
+ : StringView(string.impl(), offset) {} |
+ StringView(const String& string) |
+ : StringView(string, 0, string.length()) {} |
+ |
+ // From an AtomicString: |
+ StringView(const AtomicString& string, unsigned offset, unsigned length) |
+ : StringView(string.impl(), offset, length) {} |
+ StringView(const AtomicString& string, unsigned offset) |
+ : StringView(string.impl(), offset) {} |
+ StringView(const AtomicString& string) |
+ : StringView(string, 0, string.length()) {} |
+ |
+ // From a literal string or LChar buffer: |
+ StringView(const LChar* chars, unsigned length); |
+ StringView(const char* chars, unsigned length) |
+ : StringView(reinterpret_cast<const LChar*>(chars), length) {} |
+ StringView(const LChar* chars) |
+ : StringView(chars, chars ? strlen(reinterpret_cast<const char*>(chars)) : 0) {} |
+ StringView(const char* chars) |
+ : StringView(reinterpret_cast<const LChar*>(chars)) {} |
+ |
+ // From a wide literal string or UChar buffer. |
+ StringView(const UChar* chars); |
+ StringView(const UChar* chars, unsigned length); |
+ |
+#if DCHECK_IS_ON() |
+ ~StringView(); |
+#endif |
+ bool isNull() const { return !m_data.bytes; } |
bool isEmpty() const { return !m_length; } |
+ |
unsigned length() const { return m_length; } |
- bool is8Bit() const { return m_impl->is8Bit(); } |
+ bool is8Bit() const { return m_is8Bit; } |
+ |
+ void clear(); |
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: |
+ void set(StringImpl&, unsigned offset, unsigned length); |
+ |
+#if DCHECK_IS_ON() |
RefPtr<StringImpl> m_impl; |
- unsigned m_offset; |
+#endif |
+ 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()) |
+{ |
+ SECURITY_DCHECK(offset + length <= view.length()); |
+ if (is8Bit()) |
+ m_data.characters8 = view.characters8() + offset; |
+ else |
+ m_data.characters16 = view.characters16() + offset; |
+} |
+ |
+inline StringView::StringView(StringImpl* impl) |
+{ |
+ impl ? set(*impl, 0, impl->length()) : clear(); |
+} |
+ |
+inline StringView::StringView(StringImpl* impl, unsigned offset) |
+{ |
+ impl ? set(*impl, offset, impl->length() - offset) : clear(); |
+} |
+ |
+inline StringView::StringView(StringImpl* impl, unsigned offset, unsigned length) |
+{ |
+ impl ? set(*impl, offset, length) : clear(); |
+} |
+ |
+inline StringView::StringView(const LChar* chars, unsigned length) |
+ : m_length(length) |
+ , m_is8Bit(true) |
+{ |
+ m_data.characters8 = chars; |
+} |
+ |
+inline void StringView::clear() |
+{ |
+ m_length = 0; |
+ m_is8Bit = true; |
+ m_data.bytes = nullptr; |
+#if DCHECK_IS_ON() |
+ m_impl = nullptr; |
+#endif |
+} |
+ |
+inline void StringView::set(StringImpl& impl, unsigned offset, unsigned length) |
+{ |
+ SECURITY_DCHECK(offset + length <= impl.length()); |
+ m_length = length; |
+ m_is8Bit = impl.is8Bit(); |
+#if DCHECK_IS_ON() |
+ m_impl = &impl; |
+#endif |
+ if (m_is8Bit) |
+ m_data.characters8 = impl.characters8() + offset; |
+ else |
+ m_data.characters16 = impl.characters16() + offset; |
+} |
+ |
+// TODO(esprehn): Can't make this an overload of WTF::equal since that makes |
+// calls to equal() that pass literal strings ambiguous. Figure out if we can |
+// replace all the callers with equalStringView and then rename it to equal(). |
+WTF_EXPORT 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; |