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: Remove bad assert. 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 namespace WTF {
8
9 StringView::StringView(const UChar* chars, unsigned length)
10 : m_length(length)
11 , m_is8Bit(false)
12 {
13 m_data.characters16 = chars;
14 }
15
16 StringView::StringView(const UChar* chars)
17 : StringView(chars, chars ? lengthOfNullTerminatedString(chars) : 0) {}
18
19 #if DCHECK_IS_ON()
20 StringView::~StringView()
21 {
22 // StringView does not own the StringImpl, we must not be the last ref.
23 DCHECK(!m_impl || !m_impl->hasOneRef());
24 }
25 #endif
26
27 String StringView::toString() const
28 {
29 if (isNull())
30 return String();
31 if (isEmpty())
32 return emptyString();
33 if (is8Bit())
34 return String(m_data.characters8, m_length);
35 return String(m_data.characters16, m_length);
36 }
37
38 bool equalStringView(const StringView& a, const StringView& b)
39 {
40 if (a.length() != b.length())
41 return false;
42 if (a.isEmpty() || b.isEmpty())
43 return a.isEmpty() == b.isEmpty();
44 if (a.is8Bit()) {
45 if (b.is8Bit())
46 return equal(a.characters8(), b.characters8(), a.length());
47 return equal(a.characters8(), b.characters16(), a.length());
48 }
49 if (b.is8Bit())
50 return equal(a.characters16(), b.characters8(), a.length());
51 return equal(a.characters16(), b.characters16(), a.length());
52 }
53
54 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/text/StringView.h ('k') | third_party/WebKit/Source/wtf/text/StringViewTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698