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

Side by Side Diff: base/string_piece.cc

Issue 8933012: Revert "Extract common code from StringPiece and StringPiece16 into a templated base class.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years 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 | Annotate | Revision Log
« no previous file with comments | « base/string_piece.h ('k') | base/string_piece_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 // Copied from strings/stringpiece.cc with modifications 4 // Copied from strings/stringpiece.cc with modifications
5 5
6 #include <algorithm> 6 #include <algorithm>
7 #include <ostream> 7 #include <ostream>
8 8
9 #include "base/string_piece.h" 9 #include "base/string_piece.h"
10 10
11 namespace base { 11 namespace base {
12 12
13 // MSVC doesn't like complex extern templates and DLLs.
14 #if !defined(COMPILER_MSVC)
15 namespace internal {
16 template class StringPieceDetail<std::string>;
17 template class StringPieceDetail<string16>;
18 } // namespace internal
19
20 template class BasicStringPiece<std::string>;
21 template class BasicStringPiece<string16>;
22 #endif
23
24 typedef StringPiece::size_type size_type; 13 typedef StringPiece::size_type size_type;
25 14
26 bool operator==(const StringPiece& x, const StringPiece& y) { 15 bool operator==(const StringPiece& x, const StringPiece& y) {
27 if (x.size() != y.size()) 16 if (x.size() != y.size())
28 return false; 17 return false;
29 18
30 return StringPiece::wordmemcmp(x.data(), y.data(), x.size()) == 0; 19 return StringPiece::wordmemcmp(x.data(), y.data(), x.size()) == 0;
31 } 20 }
32 21
33 void BasicStringPiece<std::string>::CopyToString(std::string* target) const { 22 void StringPiece::CopyToString(std::string* target) const {
34 target->assign(!empty() ? data() : "", size()); 23 target->assign(!empty() ? data() : "", size());
35 } 24 }
36 25
37 void BasicStringPiece<std::string>::AppendToString(std::string* target) const { 26 void StringPiece::AppendToString(std::string* target) const {
38 if (!empty()) 27 if (!empty())
39 target->append(data(), size()); 28 target->append(data(), size());
40 } 29 }
41 30
42 size_type BasicStringPiece<std::string>::copy(char* buf, 31 size_type StringPiece::copy(char* buf, size_type n, size_type pos) const {
43 size_type n,
44 size_type pos) const {
45 size_type ret = std::min(length_ - pos, n); 32 size_type ret = std::min(length_ - pos, n);
46 memcpy(buf, ptr_ + pos, ret); 33 memcpy(buf, ptr_ + pos, ret);
47 return ret; 34 return ret;
48 } 35 }
49 36
50 size_type BasicStringPiece<std::string>::find(const BasicStringPiece& s, 37 size_type StringPiece::find(const StringPiece& s, size_type pos) const {
51 size_type pos) const {
52 if (pos > length_) 38 if (pos > length_)
53 return npos; 39 return npos;
54 40
55 const char* result = std::search(ptr_ + pos, ptr_ + length_, 41 const char* result = std::search(ptr_ + pos, ptr_ + length_,
56 s.ptr_, s.ptr_ + s.length_); 42 s.ptr_, s.ptr_ + s.length_);
57 const size_type xpos = result - ptr_; 43 const size_type xpos = result - ptr_;
58 return xpos + s.length_ <= length_ ? xpos : npos; 44 return xpos + s.length_ <= length_ ? xpos : npos;
59 } 45 }
60 46
61 size_type BasicStringPiece<std::string>::find(char c, size_type pos) const { 47 size_type StringPiece::find(char c, size_type pos) const {
62 if (pos >= length_) 48 if (pos >= length_)
63 return npos; 49 return npos;
64 50
65 const char* result = std::find(ptr_ + pos, ptr_ + length_, c); 51 const char* result = std::find(ptr_ + pos, ptr_ + length_, c);
66 return result != ptr_ + length_ ? static_cast<size_t>(result - ptr_) : npos; 52 return result != ptr_ + length_ ? static_cast<size_t>(result - ptr_) : npos;
67 } 53 }
68 54
69 size_type BasicStringPiece<std::string>::rfind(const BasicStringPiece& s, 55 size_type StringPiece::rfind(const StringPiece& s, size_type pos) const {
70 size_type pos) const {
71 if (length_ < s.length_) 56 if (length_ < s.length_)
72 return npos; 57 return npos;
73 58
74 if (s.empty()) 59 if (s.empty())
75 return std::min(length_, pos); 60 return std::min(length_, pos);
76 61
77 const char* last = ptr_ + std::min(length_ - s.length_, pos) + s.length_; 62 const char* last = ptr_ + std::min(length_ - s.length_, pos) + s.length_;
78 const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_); 63 const char* result = std::find_end(ptr_, last, s.ptr_, s.ptr_ + s.length_);
79 return result != last ? static_cast<size_t>(result - ptr_) : npos; 64 return result != last ? static_cast<size_t>(result - ptr_) : npos;
80 } 65 }
81 66
82 size_type BasicStringPiece<std::string>::rfind(char c, size_type pos) const { 67 size_type StringPiece::rfind(char c, size_type pos) const {
83 if (length_ == 0) 68 if (length_ == 0)
84 return npos; 69 return npos;
85 70
86 for (size_type i = std::min(pos, length_ - 1); ; --i) { 71 for (size_type i = std::min(pos, length_ - 1); ; --i) {
87 if (ptr_[i] == c) 72 if (ptr_[i] == c)
88 return i; 73 return i;
89 if (i == 0) 74 if (i == 0)
90 break; 75 break;
91 } 76 }
92 return npos; 77 return npos;
93 } 78 }
94 79
95 // For each character in characters_wanted, sets the index corresponding 80 // For each character in characters_wanted, sets the index corresponding
96 // to the ASCII code of that character to 1 in table. This is used by 81 // to the ASCII code of that character to 1 in table. This is used by
97 // the find_.*_of methods below to tell whether or not a character is in 82 // the find_.*_of methods below to tell whether or not a character is in
98 // the lookup table in constant time. 83 // the lookup table in constant time.
99 // The argument `table' must be an array that is large enough to hold all 84 // The argument `table' must be an array that is large enough to hold all
100 // the possible values of an unsigned char. Thus it should be be declared 85 // the possible values of an unsigned char. Thus it should be be declared
101 // as follows: 86 // as follows:
102 // bool table[UCHAR_MAX + 1] 87 // bool table[UCHAR_MAX + 1]
103 static inline void BuildLookupTable(const StringPiece& characters_wanted, 88 static inline void BuildLookupTable(const StringPiece& characters_wanted,
104 bool* table) { 89 bool* table) {
105 const size_type length = characters_wanted.length(); 90 const size_type length = characters_wanted.length();
106 const char* const data = characters_wanted.data(); 91 const char* const data = characters_wanted.data();
107 for (size_type i = 0; i < length; ++i) { 92 for (size_type i = 0; i < length; ++i) {
108 table[static_cast<unsigned char>(data[i])] = true; 93 table[static_cast<unsigned char>(data[i])] = true;
109 } 94 }
110 } 95 }
111 96
112 size_type BasicStringPiece<std::string>::find_first_of( 97 size_type StringPiece::find_first_of(const StringPiece& s,
113 const BasicStringPiece& s, size_type pos) const { 98 size_type pos) const {
114 if (length_ == 0 || s.length_ == 0) 99 if (length_ == 0 || s.length_ == 0)
115 return npos; 100 return npos;
116 101
117 // Avoid the cost of BuildLookupTable() for a single-character search. 102 // Avoid the cost of BuildLookupTable() for a single-character search.
118 if (s.length_ == 1) 103 if (s.length_ == 1)
119 return find_first_of(s.ptr_[0], pos); 104 return find_first_of(s.ptr_[0], pos);
120 105
121 bool lookup[UCHAR_MAX + 1] = { false }; 106 bool lookup[UCHAR_MAX + 1] = { false };
122 BuildLookupTable(s, lookup); 107 BuildLookupTable(s, lookup);
123 for (size_type i = pos; i < length_; ++i) { 108 for (size_type i = pos; i < length_; ++i) {
124 if (lookup[static_cast<unsigned char>(ptr_[i])]) { 109 if (lookup[static_cast<unsigned char>(ptr_[i])]) {
125 return i; 110 return i;
126 } 111 }
127 } 112 }
128 return npos; 113 return npos;
129 } 114 }
130 115
131 size_type BasicStringPiece<std::string>::find_first_not_of( 116 size_type StringPiece::find_first_not_of(const StringPiece& s,
132 const BasicStringPiece& s, size_type pos) const { 117 size_type pos) const {
133 if (length_ == 0) 118 if (length_ == 0)
134 return npos; 119 return npos;
135 120
136 if (s.length_ == 0) 121 if (s.length_ == 0)
137 return 0; 122 return 0;
138 123
139 // Avoid the cost of BuildLookupTable() for a single-character search. 124 // Avoid the cost of BuildLookupTable() for a single-character search.
140 if (s.length_ == 1) 125 if (s.length_ == 1)
141 return find_first_not_of(s.ptr_[0], pos); 126 return find_first_not_of(s.ptr_[0], pos);
142 127
143 bool lookup[UCHAR_MAX + 1] = { false }; 128 bool lookup[UCHAR_MAX + 1] = { false };
144 BuildLookupTable(s, lookup); 129 BuildLookupTable(s, lookup);
145 for (size_type i = pos; i < length_; ++i) { 130 for (size_type i = pos; i < length_; ++i) {
146 if (!lookup[static_cast<unsigned char>(ptr_[i])]) { 131 if (!lookup[static_cast<unsigned char>(ptr_[i])]) {
147 return i; 132 return i;
148 } 133 }
149 } 134 }
150 return npos; 135 return npos;
151 } 136 }
152 137
153 size_type BasicStringPiece<std::string>::find_first_not_of( 138 size_type StringPiece::find_first_not_of(char c, size_type pos) const {
154 char c, size_type pos) const {
155 if (length_ == 0) 139 if (length_ == 0)
156 return npos; 140 return npos;
157 141
158 for (; pos < length_; ++pos) { 142 for (; pos < length_; ++pos) {
159 if (ptr_[pos] != c) { 143 if (ptr_[pos] != c) {
160 return pos; 144 return pos;
161 } 145 }
162 } 146 }
163 return npos; 147 return npos;
164 } 148 }
165 149
166 size_type BasicStringPiece<std::string>::find_last_of(const StringPiece& s, 150 size_type StringPiece::find_last_of(const StringPiece& s, size_type pos) const {
167 size_type pos) const {
168 if (length_ == 0 || s.length_ == 0) 151 if (length_ == 0 || s.length_ == 0)
169 return npos; 152 return npos;
170 153
171 // Avoid the cost of BuildLookupTable() for a single-character search. 154 // Avoid the cost of BuildLookupTable() for a single-character search.
172 if (s.length_ == 1) 155 if (s.length_ == 1)
173 return find_last_of(s.ptr_[0], pos); 156 return find_last_of(s.ptr_[0], pos);
174 157
175 bool lookup[UCHAR_MAX + 1] = { false }; 158 bool lookup[UCHAR_MAX + 1] = { false };
176 BuildLookupTable(s, lookup); 159 BuildLookupTable(s, lookup);
177 for (size_type i = std::min(pos, length_ - 1); ; --i) { 160 for (size_type i = std::min(pos, length_ - 1); ; --i) {
178 if (lookup[static_cast<unsigned char>(ptr_[i])]) 161 if (lookup[static_cast<unsigned char>(ptr_[i])])
179 return i; 162 return i;
180 if (i == 0) 163 if (i == 0)
181 break; 164 break;
182 } 165 }
183 return npos; 166 return npos;
184 } 167 }
185 168
186 size_type BasicStringPiece<std::string>::find_last_not_of( 169 size_type StringPiece::find_last_not_of(const StringPiece& s,
187 const BasicStringPiece& s, size_type pos) const { 170 size_type pos) const {
188 if (length_ == 0) 171 if (length_ == 0)
189 return npos; 172 return npos;
190 173
191 size_type i = std::min(pos, length_ - 1); 174 size_type i = std::min(pos, length_ - 1);
192 if (s.length_ == 0) 175 if (s.length_ == 0)
193 return i; 176 return i;
194 177
195 // Avoid the cost of BuildLookupTable() for a single-character search. 178 // Avoid the cost of BuildLookupTable() for a single-character search.
196 if (s.length_ == 1) 179 if (s.length_ == 1)
197 return find_last_not_of(s.ptr_[0], pos); 180 return find_last_not_of(s.ptr_[0], pos);
198 181
199 bool lookup[UCHAR_MAX + 1] = { false }; 182 bool lookup[UCHAR_MAX + 1] = { false };
200 BuildLookupTable(s, lookup); 183 BuildLookupTable(s, lookup);
201 for (; ; --i) { 184 for (; ; --i) {
202 if (!lookup[static_cast<unsigned char>(ptr_[i])]) 185 if (!lookup[static_cast<unsigned char>(ptr_[i])])
203 return i; 186 return i;
204 if (i == 0) 187 if (i == 0)
205 break; 188 break;
206 } 189 }
207 return npos; 190 return npos;
208 } 191 }
209 192
210 size_type BasicStringPiece<std::string>::find_last_not_of(char c, 193 size_type StringPiece::find_last_not_of(char c, size_type pos) const {
211 size_type pos) const {
212 if (length_ == 0) 194 if (length_ == 0)
213 return npos; 195 return npos;
214 196
215 for (size_type i = std::min(pos, length_ - 1); ; --i) { 197 for (size_type i = std::min(pos, length_ - 1); ; --i) {
216 if (ptr_[i] != c) 198 if (ptr_[i] != c)
217 return i; 199 return i;
218 if (i == 0) 200 if (i == 0)
219 break; 201 break;
220 } 202 }
221 return npos; 203 return npos;
222 } 204 }
223 205
224 BasicStringPiece<std::string> BasicStringPiece<std::string>::substr( 206 StringPiece StringPiece::substr(size_type pos, size_type n) const {
225 size_type pos, size_type n) const {
226 if (pos > length_) pos = length_; 207 if (pos > length_) pos = length_;
227 if (n > length_ - pos) n = length_ - pos; 208 if (n > length_ - pos) n = length_ - pos;
228 return StringPiece(ptr_ + pos, n); 209 return StringPiece(ptr_ + pos, n);
229 } 210 }
230 211
212 const StringPiece::size_type StringPiece::npos = size_type(-1);
213
231 } // namespace base 214 } // namespace base
OLDNEW
« no previous file with comments | « base/string_piece.h ('k') | base/string_piece_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698