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

Side by Side 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, 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 /* 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be
3 * 3 // found in the LICENSE file.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 4
31 #ifndef WTF_StringView_h 5 #ifndef WTF_StringView_h
32 #define WTF_StringView_h 6 #define WTF_StringView_h
33 7
34 #include "wtf/Allocator.h" 8 #include "wtf/Allocator.h"
35 #include "wtf/text/StringImpl.h" 9 #if DCHECK_IS_ON()
10 #include "wtf/RefPtr.h"
11 #endif
12 #include "wtf/text/Unicode.h"
13 #include <cstring>
36 14
37 namespace WTF { 15 namespace WTF {
38 16
17 class AtomicString;
18 class String;
19 class StringImpl;
20
39 class WTF_EXPORT StringView { 21 class WTF_EXPORT StringView {
40 DISALLOW_NEW(); 22 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
41 public: 23 public:
42 StringView() 24 // Null string.
43 : m_offset(0) 25 StringView() { clear(); }
44 , m_length(0)
45 {
46 }
47 26
48 explicit StringView(PassRefPtr<StringImpl> impl) 27 // From a StringImpl (implemented in StringImpl.h)
49 : m_impl(impl) 28 StringView(StringImpl*);
50 , m_offset(0) 29 StringView(StringImpl*, unsigned offset);
51 , m_length(m_impl->length()) 30 StringView(StringImpl*, unsigned offset, unsigned length);
52 {
53 }
54 31
55 StringView(PassRefPtr<StringImpl> impl, unsigned offset, unsigned length) 32 // From a String (implemented in WTFString.h)
56 : m_impl(impl) 33 StringView(const String&);
57 , m_offset(offset) 34 StringView(const String&, unsigned offset);
58 , m_length(length) 35 StringView(const String&, unsigned offset, unsigned length);
59 {
60 ASSERT_WITH_SECURITY_IMPLICATION(offset + length <= m_impl->length());
61 }
62 36
63 void narrow(unsigned offset, unsigned length) 37 // From an AtomicString (implemented in in AtomicString.h)
64 { 38 StringView(const AtomicString&);
65 ASSERT_WITH_SECURITY_IMPLICATION(offset + length <= m_length); 39 StringView(const AtomicString&, unsigned offset);
66 m_offset += offset; 40 StringView(const AtomicString&, unsigned offset, unsigned length);
67 m_length = length;
68 }
69 41
42 // From a StringView.
43 StringView(const StringView&, unsigned offset);
44 StringView(const StringView&, unsigned offset, unsigned length);
45
46 // From a literal string or LChar buffer.
47 StringView(const LChar* chars);
48 StringView(const char* chars);
49 StringView(const LChar* chars, unsigned length);
50 StringView(const char* chars, unsigned length);
51
52 // From a UChar buffer.
53 StringView(const UChar* chars);
54 StringView(const UChar* chars, unsigned length);
55
56 #if DCHECK_IS_ON()
57 ~StringView();
58 #endif
59
60 bool isNull() const { return !m_data.bytes; }
70 bool isEmpty() const { return !m_length; } 61 bool isEmpty() const { return !m_length; }
62
71 unsigned length() const { return m_length; } 63 unsigned length() const { return m_length; }
72 64
73 bool is8Bit() const { return m_impl->is8Bit(); } 65 bool is8Bit() const { return m_is8Bit; }
66
67 void clear()
68 {
69 m_length = 0;
70 m_is8Bit = true;
71 m_data.bytes = nullptr;
72 #if DCHECK_IS_ON()
73 m_impl = nullptr;
74 #endif
75 }
74 76
75 const LChar* characters8() const 77 const LChar* characters8() const
76 { 78 {
77 if (!m_impl)
78 return 0;
79 ASSERT(is8Bit()); 79 ASSERT(is8Bit());
80 return m_impl->characters8() + m_offset; 80 return m_data.characters8;
81 } 81 }
82 82
83 const UChar* characters16() const 83 const UChar* characters16() const
84 { 84 {
85 if (!m_impl)
86 return 0;
87 ASSERT(!is8Bit()); 85 ASSERT(!is8Bit());
88 return m_impl->characters16() + m_offset; 86 return m_data.characters16;
89 } 87 }
90 88
91 PassRefPtr<StringImpl> toString() const 89 String toString() const;
92 {
93 if (!m_impl)
94 return m_impl;
95 if (m_impl->is8Bit())
96 return StringImpl::create(characters8(), m_length);
97 return StringImpl::create(characters16(), m_length);
98 }
99 90
100 private: 91 private:
92 // Implemented in StringImpl.h
93 void set(StringImpl&, unsigned offset, unsigned length);
94
95 #if DCHECK_IS_ON()
101 RefPtr<StringImpl> m_impl; 96 RefPtr<StringImpl> m_impl;
102 unsigned m_offset; 97 #endif
98 union DataUnion {
99 const LChar* characters8;
100 const UChar* characters16;
101 const void* bytes;
102 } m_data;
103 unsigned m_length; 103 unsigned m_length;
104 unsigned m_is8Bit : 1;
104 }; 105 };
105 106
107 inline StringView::StringView(const StringView& view, unsigned offset, unsigned length)
108 : m_length(length)
109 , m_is8Bit(view.is8Bit())
110 {
111 SECURITY_DCHECK(offset + length <= view.length());
112 if (is8Bit())
113 m_data.characters8 = view.characters8() + offset;
114 else
115 m_data.characters16 = view.characters16() + offset;
116 }
117
118 inline StringView::StringView(const StringView& view, unsigned offset)
119 : StringView(view, offset, view.m_length - offset) {}
120
121 inline StringView::StringView(const LChar* chars, unsigned length)
122 : m_length(length)
123 , m_is8Bit(true)
124 {
125 m_data.characters8 = chars;
126 }
127
128 inline StringView::StringView(const char* chars, unsigned length)
129 : StringView(reinterpret_cast<const LChar*>(chars), length) {}
130 inline StringView::StringView(const LChar* chars)
131 : StringView(chars, chars ? strlen(reinterpret_cast<const char*>(chars)) : 0 ) {}
132 inline StringView::StringView(const char* chars)
133 : StringView(reinterpret_cast<const LChar*>(chars)) {}
134
135 // TODO(esprehn): Can't make this an overload of WTF::equal since that makes
136 // calls to equal() that pass literal strings ambiguous. Figure out if we can
137 // replace all the callers with equalStringView and then rename it to equal().
138 WTF_EXPORT bool equalStringView(const StringView&, const StringView&);
139
140 inline bool operator==(const StringView& a, const StringView& b)
141 {
142 return equalStringView(a, b);
143 }
144
145 inline bool operator!=(const StringView& a, const StringView& b)
146 {
147 return !(a == b);
148 }
149
106 } // namespace WTF 150 } // namespace WTF
107 151
108 using WTF::StringView; 152 using WTF::StringView;
109 153
110 #endif 154 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698