Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(339)

Unified Diff: third_party/WebKit/Source/wtf/text/StringView.h

Issue 2007103003: Expand WTF::StringView's API to be more like StringPiece. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add a lot of tests, fix a bunch of bugs. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..b2deb04bbb8d7e5b0ee7302659db97113181123a 100644
--- a/third_party/WebKit/Source/wtf/text/StringView.h
+++ b/third_party/WebKit/Source/wtf/text/StringView.h
@@ -1,108 +1,152 @@
-/*
- * 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"
-#include "wtf/text/StringImpl.h"
+#if DCHECK_IS_ON()
+#include "wtf/RefPtr.h"
+#endif
+#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);
+
+#if DCHECK_IS_ON()
+ ~StringView();
+#endif
- explicit StringView(PassRefPtr<StringImpl> impl)
- : m_impl(impl)
- , m_offset(0)
- , m_length(m_impl->length())
- {
- }
+ bool isNull() const { return !m_data.bytes; }
+ bool isEmpty() 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());
- }
+ unsigned length() const { return m_length; }
- void narrow(unsigned offset, unsigned length)
+ bool is8Bit() const { return m_is8Bit; }
+
+ 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;
+#if DCHECK_IS_ON()
+ m_impl = nullptr;
+#endif
}
- 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:
+ // Implemented in StringImpl.h
+ 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(const StringView& view, unsigned offset)
+ : StringView(view, offset, view.m_length - offset) {}
+
+inline StringView::StringView(const LChar* chars, unsigned length)
+ : m_length(length)
+ , m_is8Bit(true)
+{
+ 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 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;

Powered by Google App Engine
This is Rietveld 408576698