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

Side by Side 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: Add a lot of tests, fix a bunch of bugs. Created 4 years, 6 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 unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "wtf/text/StringView.h"
6
7 #include "wtf/text/StringImpl.h"
8 #include "wtf/text/WTFString.h"
9
10 namespace WTF {
11
12 StringView::StringView(const UChar* chars, unsigned length)
13 : m_length(length)
14 , m_is8Bit(false)
15 {
16 SECURITY_DCHECK(!chars || length <= lengthOfNullTerminatedString(chars));
17 m_data.characters16 = chars;
18 }
19
20 StringView::StringView(const UChar* chars)
21 : StringView(chars, chars ? lengthOfNullTerminatedString(chars) : 0) {}
22
23 #if DCHECK_IS_ON()
24 StringView::~StringView()
25 {
26 // StringView does not own the StringImpl, we must not be the last ref.
27 DCHECK(!m_impl || !m_impl->hasOneRef());
28 }
29 #endif
30
31 String StringView::toString() const
32 {
33 if (isNull())
34 return String();
35 if (isEmpty())
36 return emptyString();
37 if (is8Bit())
38 return String(m_data.characters8, m_length);
39 return String(m_data.characters16, m_length);
40 }
41
42 bool equalStringView(const StringView& a, const StringView& b)
43 {
44 if (a.length() != b.length())
45 return false;
46 if (a.isEmpty() || b.isEmpty())
47 return a.isEmpty() == b.isEmpty();
48 if (a.is8Bit()) {
49 if (b.is8Bit())
50 return WTF::equal(a.characters8(), b.characters8(), a.length());
Yuta Kitamura 2016/05/27 04:47:41 nit: Omit "WTF::".
51 return WTF::equal(a.characters8(), b.characters16(), a.length());
52 }
53 if (b.is8Bit())
54 return WTF::equal(a.characters16(), b.characters8(), a.length());
55 return WTF::equal(a.characters16(), b.characters16(), a.length());
56 }
57
58 } // namespace WTF
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698