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

Side by Side Diff: third_party/WebKit/Source/wtf/text/StringView.cpp

Issue 2033293002: Implement StringImpl sharing for StringView::toString(). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix preload scanner bug. 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "wtf/text/StringView.h" 5 #include "wtf/text/StringView.h"
6 6
7 namespace WTF { 7 namespace WTF {
8 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) 9 StringView::StringView(const UChar* chars)
17 : StringView(chars, chars ? lengthOfNullTerminatedString(chars) : 0) {} 10 : StringView(chars, chars ? lengthOfNullTerminatedString(chars) : 0) {}
18 11
19 #if DCHECK_IS_ON() 12 #if DCHECK_IS_ON()
20 StringView::~StringView() 13 StringView::~StringView()
21 { 14 {
22 // StringView does not own the StringImpl, we must not be the last ref. 15 DCHECK(m_impl);
23 DCHECK(!m_impl || !m_impl->hasOneRef()); 16 DCHECK(!m_impl->hasOneRef())
17 << "StringView does not own the StringImpl, it must not have the last re f.";
24 } 18 }
25 #endif 19 #endif
26 20
27 String StringView::toString() const 21 String StringView::toString() const
28 { 22 {
29 if (isNull()) 23 if (isNull())
30 return String(); 24 return String();
31 if (isEmpty()) 25 if (isEmpty())
32 return emptyString(); 26 return emptyString();
27 if (StringImpl* impl = sharedImpl())
28 return impl;
33 if (is8Bit()) 29 if (is8Bit())
34 return String(m_data.characters8, m_length); 30 return String(characters8(), m_length);
35 return StringImpl::create8BitIfPossible(m_data.characters16, m_length); 31 return StringImpl::create8BitIfPossible(characters16(), m_length);
36 } 32 }
37 33
38 AtomicString StringView::toAtomicString() const 34 AtomicString StringView::toAtomicString() const
39 { 35 {
40 if (isNull()) 36 if (isNull())
41 return nullAtom; 37 return nullAtom;
42 if (isEmpty()) 38 if (isEmpty())
43 return emptyAtom; 39 return emptyAtom;
40 if (StringImpl* impl = sharedImpl())
41 return AtomicString(impl);
44 if (is8Bit()) 42 if (is8Bit())
45 return AtomicString(m_data.characters8, m_length); 43 return AtomicString(characters8(), m_length);
46 return AtomicString(m_data.characters16, m_length); 44 return AtomicString(characters16(), m_length);
47 } 45 }
48 46
49 bool equalStringView(const StringView& a, const StringView& b) 47 bool equalStringView(const StringView& a, const StringView& b)
50 { 48 {
51 if (a.isNull() || b.isNull()) 49 if (a.isNull() || b.isNull())
52 return a.isNull() == b.isNull(); 50 return a.isNull() == b.isNull();
53 if (a.length() != b.length()) 51 if (a.length() != b.length())
54 return false; 52 return false;
55 if (a.is8Bit()) { 53 if (a.is8Bit()) {
56 if (b.is8Bit()) 54 if (b.is8Bit())
(...skipping 15 matching lines...) Expand all
72 if (b.is8Bit()) 70 if (b.is8Bit())
73 return equalIgnoringASCIICase(a.characters8(), b.characters8(), a.le ngth()); 71 return equalIgnoringASCIICase(a.characters8(), b.characters8(), a.le ngth());
74 return equalIgnoringASCIICase(a.characters8(), b.characters16(), a.lengt h()); 72 return equalIgnoringASCIICase(a.characters8(), b.characters16(), a.lengt h());
75 } 73 }
76 if (b.is8Bit()) 74 if (b.is8Bit())
77 return equalIgnoringASCIICase(a.characters16(), b.characters8(), a.lengt h()); 75 return equalIgnoringASCIICase(a.characters16(), b.characters8(), a.lengt h());
78 return equalIgnoringASCIICase(a.characters16(), b.characters16(), a.length() ); 76 return equalIgnoringASCIICase(a.characters16(), b.characters16(), a.length() );
79 } 77 }
80 78
81 } // namespace WTF 79 } // namespace WTF
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698