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

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

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: fix typo for length access. 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.cpp
diff --git a/third_party/WebKit/Source/wtf/text/StringView.cpp b/third_party/WebKit/Source/wtf/text/StringView.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..ae69b3178d03a2ef5800f1d68426df6b9c449495
--- /dev/null
+++ b/third_party/WebKit/Source/wtf/text/StringView.cpp
@@ -0,0 +1,48 @@
+// 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.
+
+#include "wtf/text/StringView.h"
+
+#include "wtf/text/StringImpl.h"
+#include "wtf/text/WTFString.h"
+
+namespace WTF {
+
+StringView::StringView(const UChar* chars, unsigned length)
+ : m_length(length)
+ , m_is8Bit(false)
+{
+ SECURITY_DCHECK(!chars || length <= lengthOfNullTerminatedString(chars));
+ m_data.characters16 = chars;
+}
+
+StringView::StringView(const UChar* chars)
+ : StringView(chars, chars ? lengthOfNullTerminatedString(chars) : 0) {}
+
+String StringView::toString() const
+{
+ if (!m_data.bytes)
haraken 2016/05/26 22:27:22 isNull()
esprehn 2016/05/26 22:40:21 done, also added a case for empty strings.
+ return String();
+ if (is8Bit())
+ return String(m_data.characters8, m_length);
+ return String(m_data.characters16, m_length);
+}
+
+bool equalStringView(const StringView& a, const StringView& b)
+{
+ if (a.length() != b.length())
+ return false;
+ if (a.isNull() || b.isNull())
+ return a.isNull() == b.isNull();
haraken 2016/05/26 22:27:22 A better check would be: if (a.length() == 0 ||
esprehn 2016/05/26 22:40:21 Yeah, checking isEmpty() works here.
+ if (a.is8Bit()) {
+ if (b.is8Bit())
+ return WTF::equal(a.characters8(), b.characters8(), a.length());
+ return WTF::equal(a.characters8(), b.characters16(), a.length());
+ }
+ if (b.is8Bit())
+ return WTF::equal(a.characters16(), b.characters8(), a.length());
+ return WTF::equal(a.characters16(), b.characters16(), a.length());
+}
+
+} // namespace WTF

Powered by Google App Engine
This is Rietveld 408576698