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

Side by Side Diff: trunk/src/base/strings/string_piece.cc

Issue 189793002: Revert 255397 "Fill out the rest of the StringPiece functions fo..." (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: Created 6 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « trunk/src/base/strings/string_piece.h ('k') | trunk/src/base/strings/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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/strings/string_piece.h" 6 #include "base/strings/string_piece.h"
7 7
8 #include <algorithm> 8 #include <algorithm>
9 #include <ostream> 9 #include <ostream>
10 10
11 namespace base { 11 namespace base {
12 namespace {
13
14 // For each character in characters_wanted, sets the index corresponding
15 // to the ASCII code of that character to 1 in table. This is used by
16 // the find_.*_of methods below to tell whether or not a character is in
17 // the lookup table in constant time.
18 // The argument `table' must be an array that is large enough to hold all
19 // the possible values of an unsigned char. Thus it should be be declared
20 // as follows:
21 // bool table[UCHAR_MAX + 1]
22 inline void BuildLookupTable(const StringPiece& characters_wanted,
23 bool* table) {
24 const size_t length = characters_wanted.length();
25 const char* const data = characters_wanted.data();
26 for (size_t i = 0; i < length; ++i) {
27 table[static_cast<unsigned char>(data[i])] = true;
28 }
29 }
30
31 } // namespace
32 12
33 // MSVC doesn't like complex extern templates and DLLs. 13 // MSVC doesn't like complex extern templates and DLLs.
34 #if !defined(COMPILER_MSVC) 14 #if !defined(COMPILER_MSVC)
35 template class BasicStringPiece<std::string>; 15 namespace internal {
16 template class StringPieceDetail<std::string>;
17 template class StringPieceDetail<string16>;
18 } // namespace internal
19
36 template class BasicStringPiece<string16>; 20 template class BasicStringPiece<string16>;
37 #endif 21 #endif
38 22
39 bool operator==(const StringPiece& x, const StringPiece& y) { 23 bool operator==(const StringPiece& x, const StringPiece& y) {
40 if (x.size() != y.size()) 24 if (x.size() != y.size())
41 return false; 25 return false;
42 26
43 return StringPiece::wordmemcmp(x.data(), y.data(), x.size()) == 0; 27 return StringPiece::wordmemcmp(x.data(), y.data(), x.size()) == 0;
44 } 28 }
45 29
46 std::ostream& operator<<(std::ostream& o, const StringPiece& piece) { 30 std::ostream& operator<<(std::ostream& o, const StringPiece& piece) {
47 o.write(piece.data(), static_cast<std::streamsize>(piece.size())); 31 o.write(piece.data(), static_cast<std::streamsize>(piece.size()));
48 return o; 32 return o;
49 } 33 }
50 34
51 namespace internal { 35 namespace internal {
52 36 void CopyToString(const StringPiece& self, std::string* target) {
53 template<typename STR> 37 target->assign(!self.empty() ? self.data() : "", self.size());
54 void CopyToStringT(const BasicStringPiece<STR>& self, STR* target) {
55 if (self.empty())
56 target->clear();
57 else
58 target->assign(self.data(), self.size());
59 } 38 }
60 39
61 void CopyToString(const StringPiece& self, std::string* target) { 40 void AppendToString(const StringPiece& self, std::string* target) {
62 CopyToStringT(self, target);
63 }
64
65 void CopyToString(const StringPiece16& self, string16* target) {
66 CopyToStringT(self, target);
67 }
68
69 template<typename STR>
70 void AppendToStringT(const BasicStringPiece<STR>& self, STR* target) {
71 if (!self.empty()) 41 if (!self.empty())
72 target->append(self.data(), self.size()); 42 target->append(self.data(), self.size());
73 } 43 }
74 44
75 void AppendToString(const StringPiece& self, std::string* target) { 45 StringPiece::size_type copy(const StringPiece& self,
76 AppendToStringT(self, target); 46 char* buf,
77 } 47 StringPiece::size_type n,
78 48 StringPiece::size_type pos) {
79 void AppendToString(const StringPiece16& self, string16* target) { 49 StringPiece::size_type ret = std::min(self.size() - pos, n);
80 AppendToStringT(self, target); 50 memcpy(buf, self.data() + pos, ret);
81 }
82
83 template<typename STR>
84 size_t copyT(const BasicStringPiece<STR>& self,
85 typename STR::value_type* buf,
86 size_t n,
87 size_t pos) {
88 size_t ret = std::min(self.size() - pos, n);
89 memcpy(buf, self.data() + pos, ret * sizeof(typename STR::value_type));
90 return ret; 51 return ret;
91 } 52 }
92 53
93 size_t copy(const StringPiece& self, char* buf, size_t n, size_t pos) { 54 StringPiece::size_type find(const StringPiece& self,
94 return copyT(self, buf, n, pos); 55 const StringPiece& s,
56 StringPiece::size_type pos) {
57 if (pos > self.size())
58 return StringPiece::npos;
59
60 StringPiece::const_iterator result =
61 std::search(self.begin() + pos, self.end(), s.begin(), s.end());
62 const StringPiece::size_type xpos =
63 static_cast<size_t>(result - self.begin());
64 return xpos + s.size() <= self.size() ? xpos : StringPiece::npos;
95 } 65 }
96 66
97 size_t copy(const StringPiece16& self, char16* buf, size_t n, size_t pos) { 67 StringPiece::size_type find(const StringPiece& self,
98 return copyT(self, buf, n, pos); 68 char c,
69 StringPiece::size_type pos) {
70 if (pos >= self.size())
71 return StringPiece::npos;
72
73 StringPiece::const_iterator result =
74 std::find(self.begin() + pos, self.end(), c);
75 return result != self.end() ?
76 static_cast<size_t>(result - self.begin()) : StringPiece::npos;
99 } 77 }
100 78
101 template<typename STR> 79 StringPiece::size_type rfind(const StringPiece& self,
102 size_t findT(const BasicStringPiece<STR>& self, 80 const StringPiece& s,
103 const BasicStringPiece<STR>& s, 81 StringPiece::size_type pos) {
104 size_t pos) {
105 if (pos > self.size())
106 return BasicStringPiece<STR>::npos;
107
108 typename BasicStringPiece<STR>::const_iterator result =
109 std::search(self.begin() + pos, self.end(), s.begin(), s.end());
110 const size_t xpos =
111 static_cast<size_t>(result - self.begin());
112 return xpos + s.size() <= self.size() ? xpos : BasicStringPiece<STR>::npos;
113 }
114
115 size_t find(const StringPiece& self, const StringPiece& s, size_t pos) {
116 return findT(self, s, pos);
117 }
118
119 size_t find(const StringPiece16& self, const StringPiece16& s, size_t pos) {
120 return findT(self, s, pos);
121 }
122
123 template<typename STR>
124 size_t findT(const BasicStringPiece<STR>& self,
125 typename STR::value_type c,
126 size_t pos) {
127 if (pos >= self.size())
128 return BasicStringPiece<STR>::npos;
129
130 typename BasicStringPiece<STR>::const_iterator result =
131 std::find(self.begin() + pos, self.end(), c);
132 return result != self.end() ?
133 static_cast<size_t>(result - self.begin()) : BasicStringPiece<STR>::npos;
134 }
135
136 size_t find(const StringPiece& self, char c, size_t pos) {
137 return findT(self, c, pos);
138 }
139
140 size_t find(const StringPiece16& self, char16 c, size_t pos) {
141 return findT(self, c, pos);
142 }
143
144 template<typename STR>
145 size_t rfindT(const BasicStringPiece<STR>& self,
146 const BasicStringPiece<STR>& s,
147 size_t pos) {
148 if (self.size() < s.size()) 82 if (self.size() < s.size())
149 return BasicStringPiece<STR>::npos; 83 return StringPiece::npos;
150 84
151 if (s.empty()) 85 if (s.empty())
152 return std::min(self.size(), pos); 86 return std::min(self.size(), pos);
153 87
154 typename BasicStringPiece<STR>::const_iterator last = 88 StringPiece::const_iterator last =
155 self.begin() + std::min(self.size() - s.size(), pos) + s.size(); 89 self.begin() + std::min(self.size() - s.size(), pos) + s.size();
156 typename BasicStringPiece<STR>::const_iterator result = 90 StringPiece::const_iterator result =
157 std::find_end(self.begin(), last, s.begin(), s.end()); 91 std::find_end(self.begin(), last, s.begin(), s.end());
158 return result != last ? 92 return result != last ?
159 static_cast<size_t>(result - self.begin()) : BasicStringPiece<STR>::npos; 93 static_cast<size_t>(result - self.begin()) : StringPiece::npos;
160 } 94 }
161 95
162 size_t rfind(const StringPiece& self, const StringPiece& s, size_t pos) { 96 StringPiece::size_type rfind(const StringPiece& self,
163 return rfindT(self, s, pos); 97 char c,
164 } 98 StringPiece::size_type pos) {
99 if (self.size() == 0)
100 return StringPiece::npos;
165 101
166 size_t rfind(const StringPiece16& self, const StringPiece16& s, size_t pos) { 102 for (StringPiece::size_type i = std::min(pos, self.size() - 1); ; --i) {
167 return rfindT(self, s, pos);
168 }
169
170 template<typename STR>
171 size_t rfindT(const BasicStringPiece<STR>& self,
172 typename STR::value_type c,
173 size_t pos) {
174 if (self.size() == 0)
175 return BasicStringPiece<STR>::npos;
176
177 for (size_t i = std::min(pos, self.size() - 1); ;
178 --i) {
179 if (self.data()[i] == c) 103 if (self.data()[i] == c)
180 return i; 104 return i;
181 if (i == 0) 105 if (i == 0)
182 break; 106 break;
183 } 107 }
184 return BasicStringPiece<STR>::npos; 108 return StringPiece::npos;
185 } 109 }
186 110
187 size_t rfind(const StringPiece& self, char c, size_t pos) { 111 // For each character in characters_wanted, sets the index corresponding
188 return rfindT(self, c, pos); 112 // to the ASCII code of that character to 1 in table. This is used by
113 // the find_.*_of methods below to tell whether or not a character is in
114 // the lookup table in constant time.
115 // The argument `table' must be an array that is large enough to hold all
116 // the possible values of an unsigned char. Thus it should be be declared
117 // as follows:
118 // bool table[UCHAR_MAX + 1]
119 static inline void BuildLookupTable(const StringPiece& characters_wanted,
120 bool* table) {
121 const StringPiece::size_type length = characters_wanted.length();
122 const char* const data = characters_wanted.data();
123 for (StringPiece::size_type i = 0; i < length; ++i) {
124 table[static_cast<unsigned char>(data[i])] = true;
125 }
189 } 126 }
190 127
191 size_t rfind(const StringPiece16& self, char16 c, size_t pos) { 128 StringPiece::size_type find_first_of(const StringPiece& self,
192 return rfindT(self, c, pos); 129 const StringPiece& s,
193 } 130 StringPiece::size_type pos) {
194
195 // 8-bit version using lookup table.
196 size_t find_first_of(const StringPiece& self,
197 const StringPiece& s,
198 size_t pos) {
199 if (self.size() == 0 || s.size() == 0) 131 if (self.size() == 0 || s.size() == 0)
200 return StringPiece::npos; 132 return StringPiece::npos;
201 133
202 // Avoid the cost of BuildLookupTable() for a single-character search. 134 // Avoid the cost of BuildLookupTable() for a single-character search.
203 if (s.size() == 1) 135 if (s.size() == 1)
204 return find(self, s.data()[0], pos); 136 return find(self, s.data()[0], pos);
205 137
206 bool lookup[UCHAR_MAX + 1] = { false }; 138 bool lookup[UCHAR_MAX + 1] = { false };
207 BuildLookupTable(s, lookup); 139 BuildLookupTable(s, lookup);
208 for (size_t i = pos; i < self.size(); ++i) { 140 for (StringPiece::size_type i = pos; i < self.size(); ++i) {
209 if (lookup[static_cast<unsigned char>(self.data()[i])]) { 141 if (lookup[static_cast<unsigned char>(self.data()[i])]) {
210 return i; 142 return i;
211 } 143 }
212 } 144 }
213 return StringPiece::npos; 145 return StringPiece::npos;
214 } 146 }
215 147
216 // 16-bit brute force version. 148 StringPiece::size_type find_first_not_of(const StringPiece& self,
217 size_t find_first_of(const StringPiece16& self, 149 const StringPiece& s,
218 const StringPiece16& s, 150 StringPiece::size_type pos) {
219 size_t pos) {
220 StringPiece16::const_iterator found =
221 std::find_first_of(self.begin() + pos, self.end(), s.begin(), s.end());
222 if (found == self.end())
223 return StringPiece16::npos;
224 return found - self.begin();
225 }
226
227 // 8-bit version using lookup table.
228 size_t find_first_not_of(const StringPiece& self,
229 const StringPiece& s,
230 size_t pos) {
231 if (self.size() == 0) 151 if (self.size() == 0)
232 return StringPiece::npos; 152 return StringPiece::npos;
233 153
234 if (s.size() == 0) 154 if (s.size() == 0)
235 return 0; 155 return 0;
236 156
237 // Avoid the cost of BuildLookupTable() for a single-character search. 157 // Avoid the cost of BuildLookupTable() for a single-character search.
238 if (s.size() == 1) 158 if (s.size() == 1)
239 return find_first_not_of(self, s.data()[0], pos); 159 return find_first_not_of(self, s.data()[0], pos);
240 160
241 bool lookup[UCHAR_MAX + 1] = { false }; 161 bool lookup[UCHAR_MAX + 1] = { false };
242 BuildLookupTable(s, lookup); 162 BuildLookupTable(s, lookup);
243 for (size_t i = pos; i < self.size(); ++i) { 163 for (StringPiece::size_type i = pos; i < self.size(); ++i) {
244 if (!lookup[static_cast<unsigned char>(self.data()[i])]) { 164 if (!lookup[static_cast<unsigned char>(self.data()[i])]) {
245 return i; 165 return i;
246 } 166 }
247 } 167 }
248 return StringPiece::npos; 168 return StringPiece::npos;
249 } 169 }
250 170
251 // 16-bit brute-force version. 171 StringPiece::size_type find_first_not_of(const StringPiece& self,
252 BASE_EXPORT size_t find_first_not_of(const StringPiece16& self, 172 char c,
253 const StringPiece16& s, 173 StringPiece::size_type pos) {
254 size_t pos) {
255 if (self.size() == 0) 174 if (self.size() == 0)
256 return StringPiece16::npos; 175 return StringPiece::npos;
257
258 for (size_t self_i = pos; self_i < self.size(); ++self_i) {
259 bool found = false;
260 for (size_t s_i = 0; s_i < s.size(); ++s_i) {
261 if (self[self_i] == s[s_i]) {
262 found = true;
263 break;
264 }
265 }
266 if (!found)
267 return self_i;
268 }
269 return StringPiece16::npos;
270 }
271
272 template<typename STR>
273 size_t find_first_not_ofT(const BasicStringPiece<STR>& self,
274 typename STR::value_type c,
275 size_t pos) {
276 if (self.size() == 0)
277 return BasicStringPiece<STR>::npos;
278 176
279 for (; pos < self.size(); ++pos) { 177 for (; pos < self.size(); ++pos) {
280 if (self.data()[pos] != c) { 178 if (self.data()[pos] != c) {
281 return pos; 179 return pos;
282 } 180 }
283 } 181 }
284 return BasicStringPiece<STR>::npos; 182 return StringPiece::npos;
285 } 183 }
286 184
287 size_t find_first_not_of(const StringPiece& self, 185 StringPiece::size_type find_last_of(const StringPiece& self,
288 char c, 186 const StringPiece& s,
289 size_t pos) { 187 StringPiece::size_type pos) {
290 return find_first_not_ofT(self, c, pos);
291 }
292
293 size_t find_first_not_of(const StringPiece16& self,
294 char16 c,
295 size_t pos) {
296 return find_first_not_ofT(self, c, pos);
297 }
298
299 // 8-bit version using lookup table.
300 size_t find_last_of(const StringPiece& self, const StringPiece& s, size_t pos) {
301 if (self.size() == 0 || s.size() == 0) 188 if (self.size() == 0 || s.size() == 0)
302 return StringPiece::npos; 189 return StringPiece::npos;
303 190
304 // Avoid the cost of BuildLookupTable() for a single-character search. 191 // Avoid the cost of BuildLookupTable() for a single-character search.
305 if (s.size() == 1) 192 if (s.size() == 1)
306 return rfind(self, s.data()[0], pos); 193 return rfind(self, s.data()[0], pos);
307 194
308 bool lookup[UCHAR_MAX + 1] = { false }; 195 bool lookup[UCHAR_MAX + 1] = { false };
309 BuildLookupTable(s, lookup); 196 BuildLookupTable(s, lookup);
310 for (size_t i = std::min(pos, self.size() - 1); ; --i) { 197 for (StringPiece::size_type i = std::min(pos, self.size() - 1); ; --i) {
311 if (lookup[static_cast<unsigned char>(self.data()[i])]) 198 if (lookup[static_cast<unsigned char>(self.data()[i])])
312 return i; 199 return i;
313 if (i == 0) 200 if (i == 0)
314 break; 201 break;
315 } 202 }
316 return StringPiece::npos; 203 return StringPiece::npos;
317 } 204 }
318 205
319 // 16-bit brute-force version. 206 StringPiece::size_type find_last_not_of(const StringPiece& self,
320 size_t find_last_of(const StringPiece16& self, 207 const StringPiece& s,
321 const StringPiece16& s, 208 StringPiece::size_type pos) {
322 size_t pos) {
323 if (self.size() == 0)
324 return StringPiece16::npos;
325
326 for (size_t self_i = std::min(pos, self.size() - 1); ;
327 --self_i) {
328 for (size_t s_i = 0; s_i < s.size(); s_i++) {
329 if (self.data()[self_i] == s[s_i])
330 return self_i;
331 }
332 if (self_i == 0)
333 break;
334 }
335 return StringPiece16::npos;
336 }
337
338 // 8-bit version using lookup table.
339 size_t find_last_not_of(const StringPiece& self,
340 const StringPiece& s,
341 size_t pos) {
342 if (self.size() == 0) 209 if (self.size() == 0)
343 return StringPiece::npos; 210 return StringPiece::npos;
344 211
345 size_t i = std::min(pos, self.size() - 1); 212 StringPiece::size_type i = std::min(pos, self.size() - 1);
346 if (s.size() == 0) 213 if (s.size() == 0)
347 return i; 214 return i;
348 215
349 // Avoid the cost of BuildLookupTable() for a single-character search. 216 // Avoid the cost of BuildLookupTable() for a single-character search.
350 if (s.size() == 1) 217 if (s.size() == 1)
351 return find_last_not_of(self, s.data()[0], pos); 218 return find_last_not_of(self, s.data()[0], pos);
352 219
353 bool lookup[UCHAR_MAX + 1] = { false }; 220 bool lookup[UCHAR_MAX + 1] = { false };
354 BuildLookupTable(s, lookup); 221 BuildLookupTable(s, lookup);
355 for (; ; --i) { 222 for (; ; --i) {
356 if (!lookup[static_cast<unsigned char>(self.data()[i])]) 223 if (!lookup[static_cast<unsigned char>(self.data()[i])])
357 return i; 224 return i;
358 if (i == 0) 225 if (i == 0)
359 break; 226 break;
360 } 227 }
361 return StringPiece::npos; 228 return StringPiece::npos;
362 } 229 }
363 230
364 // 16-bit brute-force version. 231 StringPiece::size_type find_last_not_of(const StringPiece& self,
365 size_t find_last_not_of(const StringPiece16& self, 232 char c,
366 const StringPiece16& s, 233 StringPiece::size_type pos) {
367 size_t pos) {
368 if (self.size() == 0) 234 if (self.size() == 0)
369 return StringPiece::npos; 235 return StringPiece::npos;
370 236
371 for (size_t self_i = std::min(pos, self.size() - 1); ; --self_i) { 237 for (StringPiece::size_type i = std::min(pos, self.size() - 1); ; --i) {
372 bool found = false;
373 for (size_t s_i = 0; s_i < s.size(); s_i++) {
374 if (self.data()[self_i] == s[s_i]) {
375 found = true;
376 break;
377 }
378 }
379 if (!found)
380 return self_i;
381 }
382 return StringPiece16::npos;
383 }
384
385 template<typename STR>
386 size_t find_last_not_ofT(const BasicStringPiece<STR>& self,
387 typename STR::value_type c,
388 size_t pos) {
389 if (self.size() == 0)
390 return BasicStringPiece<STR>::npos;
391
392 for (size_t i = std::min(pos, self.size() - 1); ; --i) {
393 if (self.data()[i] != c) 238 if (self.data()[i] != c)
394 return i; 239 return i;
395 if (i == 0) 240 if (i == 0)
396 break; 241 break;
397 } 242 }
398 return BasicStringPiece<STR>::npos; 243 return StringPiece::npos;
399 }
400
401 size_t find_last_not_of(const StringPiece& self,
402 char c,
403 size_t pos) {
404 return find_last_not_ofT(self, c, pos);
405 }
406
407 size_t find_last_not_of(const StringPiece16& self,
408 char16 c,
409 size_t pos) {
410 return find_last_not_ofT(self, c, pos);
411 }
412
413 template<typename STR>
414 BasicStringPiece<STR> substrT(const BasicStringPiece<STR>& self,
415 size_t pos,
416 size_t n) {
417 if (pos > self.size()) pos = self.size();
418 if (n > self.size() - pos) n = self.size() - pos;
419 return BasicStringPiece<STR>(self.data() + pos, n);
420 } 244 }
421 245
422 StringPiece substr(const StringPiece& self, 246 StringPiece substr(const StringPiece& self,
423 size_t pos, 247 StringPiece::size_type pos,
424 size_t n) { 248 StringPiece::size_type n) {
425 return substrT(self, pos, n); 249 if (pos > self.size()) pos = self.size();
426 } 250 if (n > self.size() - pos) n = self.size() - pos;
427 251 return StringPiece(self.data() + pos, n);
428 StringPiece16 substr(const StringPiece16& self,
429 size_t pos,
430 size_t n) {
431 return substrT(self, pos, n);
432 } 252 }
433 253
434 } // namespace internal 254 } // namespace internal
435 } // namespace base 255 } // namespace base
OLDNEW
« no previous file with comments | « trunk/src/base/strings/string_piece.h ('k') | trunk/src/base/strings/string_piece_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698