blob: 8a7d0d8710b4524872fb2077623b9eddde3c32b3 [file] [log] [blame]
[email protected]7d7b0ac2013-03-28 20:07:401// 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
3// found in the LICENSE file.
4// Copied from strings/stringpiece.h with modifications
5//
6// A string-like object that points to a sized piece of memory.
7//
[email protected]16b200c2014-06-24 02:55:218// You can use StringPiece as a function or method parameter. A StringPiece
9// parameter can receive a double-quoted string literal argument, a "const
10// char*" argument, a string argument, or a StringPiece argument with no data
11// copying. Systematic use of StringPiece for arguments reduces data
12// copies and strlen() calls.
[email protected]7d7b0ac2013-03-28 20:07:4013//
[email protected]16b200c2014-06-24 02:55:2114// Prefer passing StringPieces by value:
15// void MyFunction(StringPiece arg);
16// If circumstances require, you may also pass by const reference:
17// void MyFunction(const StringPiece& arg); // not preferred
18// Both of these have the same lifetime semantics. Passing by value
19// generates slightly smaller code. For more discussion, Googlers can see
20// the thread go/stringpiecebyvalue on c-users.
[email protected]7d7b0ac2013-03-28 20:07:4021
22#ifndef BASE_STRINGS_STRING_PIECE_H_
23#define BASE_STRINGS_STRING_PIECE_H_
24
25#include <stddef.h>
26
27#include <iosfwd>
28#include <string>
29
30#include "base/base_export.h"
olli.raula570930f2015-10-12 22:10:5631#include "base/logging.h"
[email protected]c851cfd2013-06-10 20:11:1432#include "base/strings/string16.h"
alex-accc1bde62017-04-19 08:33:5533#include "base/strings/string_piece_forward.h"
[email protected]7d7b0ac2013-03-28 20:07:4034
35namespace base {
36
[email protected]0ecab7892014-03-11 21:15:4037// internal --------------------------------------------------------------------
38
39// Many of the StringPiece functions use different implementations for the
40// 8-bit and 16-bit versions, and we don't want lots of template expansions in
41// this (very common) header that will slow down compilation.
42//
43// So here we define overloaded functions called by the StringPiece template.
44// For those that share an implementation, the two versions will expand to a
45// template internal to the .cc file.
[email protected]7d7b0ac2013-03-28 20:07:4046namespace internal {
47
[email protected]0ecab7892014-03-11 21:15:4048BASE_EXPORT void CopyToString(const StringPiece& self, std::string* target);
49BASE_EXPORT void CopyToString(const StringPiece16& self, string16* target);
50
51BASE_EXPORT void AppendToString(const StringPiece& self, std::string* target);
52BASE_EXPORT void AppendToString(const StringPiece16& self, string16* target);
53
54BASE_EXPORT size_t copy(const StringPiece& self,
55 char* buf,
56 size_t n,
57 size_t pos);
58BASE_EXPORT size_t copy(const StringPiece16& self,
59 char16* buf,
60 size_t n,
61 size_t pos);
62
63BASE_EXPORT size_t find(const StringPiece& self,
64 const StringPiece& s,
65 size_t pos);
66BASE_EXPORT size_t find(const StringPiece16& self,
67 const StringPiece16& s,
68 size_t pos);
69BASE_EXPORT size_t find(const StringPiece& self,
70 char c,
71 size_t pos);
72BASE_EXPORT size_t find(const StringPiece16& self,
73 char16 c,
74 size_t pos);
75
76BASE_EXPORT size_t rfind(const StringPiece& self,
77 const StringPiece& s,
78 size_t pos);
79BASE_EXPORT size_t rfind(const StringPiece16& self,
80 const StringPiece16& s,
81 size_t pos);
82BASE_EXPORT size_t rfind(const StringPiece& self,
83 char c,
84 size_t pos);
85BASE_EXPORT size_t rfind(const StringPiece16& self,
86 char16 c,
87 size_t pos);
88
89BASE_EXPORT size_t find_first_of(const StringPiece& self,
90 const StringPiece& s,
91 size_t pos);
92BASE_EXPORT size_t find_first_of(const StringPiece16& self,
93 const StringPiece16& s,
94 size_t pos);
95
96BASE_EXPORT size_t find_first_not_of(const StringPiece& self,
97 const StringPiece& s,
98 size_t pos);
99BASE_EXPORT size_t find_first_not_of(const StringPiece16& self,
100 const StringPiece16& s,
101 size_t pos);
102BASE_EXPORT size_t find_first_not_of(const StringPiece& self,
103 char c,
104 size_t pos);
105BASE_EXPORT size_t find_first_not_of(const StringPiece16& self,
106 char16 c,
107 size_t pos);
108
109BASE_EXPORT size_t find_last_of(const StringPiece& self,
110 const StringPiece& s,
111 size_t pos);
112BASE_EXPORT size_t find_last_of(const StringPiece16& self,
113 const StringPiece16& s,
114 size_t pos);
115BASE_EXPORT size_t find_last_of(const StringPiece& self,
116 char c,
117 size_t pos);
118BASE_EXPORT size_t find_last_of(const StringPiece16& self,
119 char16 c,
120 size_t pos);
121
122BASE_EXPORT size_t find_last_not_of(const StringPiece& self,
123 const StringPiece& s,
124 size_t pos);
125BASE_EXPORT size_t find_last_not_of(const StringPiece16& self,
126 const StringPiece16& s,
127 size_t pos);
128BASE_EXPORT size_t find_last_not_of(const StringPiece16& self,
129 char16 c,
130 size_t pos);
131BASE_EXPORT size_t find_last_not_of(const StringPiece& self,
132 char c,
133 size_t pos);
134
135BASE_EXPORT StringPiece substr(const StringPiece& self,
136 size_t pos,
137 size_t n);
138BASE_EXPORT StringPiece16 substr(const StringPiece16& self,
139 size_t pos,
140 size_t n);
141
olli.raula570930f2015-10-12 22:10:56142#if DCHECK_IS_ON()
brettwf22866d2015-07-27 22:22:26143// Asserts that begin <= end to catch some errors with iterator usage.
144BASE_EXPORT void AssertIteratorsInOrder(std::string::const_iterator begin,
145 std::string::const_iterator end);
146BASE_EXPORT void AssertIteratorsInOrder(string16::const_iterator begin,
147 string16::const_iterator end);
148#endif
149
[email protected]0ecab7892014-03-11 21:15:40150} // namespace internal
151
152// BasicStringPiece ------------------------------------------------------------
153
[email protected]7d7b0ac2013-03-28 20:07:40154// Defines the types, methods, operators, and data members common to both
155// StringPiece and StringPiece16. Do not refer to this class directly, but
156// rather to BasicStringPiece, StringPiece, or StringPiece16.
[email protected]0ecab7892014-03-11 21:15:40157//
158// This is templatized by string class type rather than character type, so
159// BasicStringPiece<std::string> or BasicStringPiece<base::string16>.
160template <typename STRING_TYPE> class BasicStringPiece {
[email protected]7d7b0ac2013-03-28 20:07:40161 public:
[email protected]0ecab7892014-03-11 21:15:40162 // Standard STL container boilerplate.
[email protected]7d7b0ac2013-03-28 20:07:40163 typedef size_t size_type;
164 typedef typename STRING_TYPE::value_type value_type;
165 typedef const value_type* pointer;
166 typedef const value_type& reference;
167 typedef const value_type& const_reference;
168 typedef ptrdiff_t difference_type;
169 typedef const value_type* const_iterator;
170 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
171
172 static const size_type npos;
173
174 public:
175 // We provide non-explicit singleton constructors so users can pass
176 // in a "const char*" or a "string" wherever a "StringPiece" is
177 // expected (likewise for char16, string16, StringPiece16).
Jeremy Roman40a4dc12017-10-10 18:04:47178 constexpr BasicStringPiece() : ptr_(NULL), length_(0) {}
[email protected]0ecab7892014-03-11 21:15:40179 BasicStringPiece(const value_type* str)
[email protected]7d7b0ac2013-03-28 20:07:40180 : ptr_(str),
181 length_((str == NULL) ? 0 : STRING_TYPE::traits_type::length(str)) {}
[email protected]0ecab7892014-03-11 21:15:40182 BasicStringPiece(const STRING_TYPE& str)
[email protected]7d7b0ac2013-03-28 20:07:40183 : ptr_(str.data()), length_(str.size()) {}
Jeremy Roman40a4dc12017-10-10 18:04:47184 constexpr BasicStringPiece(const value_type* offset, size_type len)
[email protected]7d7b0ac2013-03-28 20:07:40185 : ptr_(offset), length_(len) {}
[email protected]0ecab7892014-03-11 21:15:40186 BasicStringPiece(const typename STRING_TYPE::const_iterator& begin,
brettwf22866d2015-07-27 22:22:26187 const typename STRING_TYPE::const_iterator& end) {
olli.raula570930f2015-10-12 22:10:56188#if DCHECK_IS_ON()
brettwf22866d2015-07-27 22:22:26189 // This assertion is done out-of-line to avoid bringing in logging.h and
190 // instantiating logging macros for every instantiation.
191 internal::AssertIteratorsInOrder(begin, end);
192#endif
193 length_ = static_cast<size_t>(std::distance(begin, end));
194
195 // The length test before assignment is to avoid dereferencing an iterator
196 // that may point to the end() of a string.
197 ptr_ = length_ > 0 ? &*begin : nullptr;
198 }
[email protected]7d7b0ac2013-03-28 20:07:40199
200 // data() may return a pointer to a buffer with embedded NULs, and the
201 // returned buffer may or may not be null terminated. Therefore it is
202 // typically a mistake to pass data() to a routine that expects a NUL
203 // terminated string.
Jeremy Roman40a4dc12017-10-10 18:04:47204 constexpr const value_type* data() const { return ptr_; }
205 constexpr size_type size() const { return length_; }
206 constexpr size_type length() const { return length_; }
[email protected]7d7b0ac2013-03-28 20:07:40207 bool empty() const { return length_ == 0; }
208
209 void clear() {
210 ptr_ = NULL;
211 length_ = 0;
212 }
213 void set(const value_type* data, size_type len) {
214 ptr_ = data;
215 length_ = len;
216 }
217 void set(const value_type* str) {
218 ptr_ = str;
219 length_ = str ? STRING_TYPE::traits_type::length(str) : 0;
220 }
221
Chris Palmere5a6dfada2018-03-06 21:40:08222 constexpr value_type operator[](size_type i) const {
223 CHECK(i < length_);
224 return ptr_[i];
225 }
226
227 value_type front() const {
228 CHECK_NE(0UL, length_);
229 return ptr_[0];
230 }
231
232 value_type back() const {
233 CHECK_NE(0UL, length_);
234 return ptr_[length_ - 1];
235 }
[email protected]7d7b0ac2013-03-28 20:07:40236
Jeremy Roman40a4dc12017-10-10 18:04:47237 constexpr void remove_prefix(size_type n) {
Chris Palmere5a6dfada2018-03-06 21:40:08238 CHECK(n <= length_);
[email protected]7d7b0ac2013-03-28 20:07:40239 ptr_ += n;
240 length_ -= n;
241 }
242
Chris Palmere5a6dfada2018-03-06 21:40:08243 constexpr void remove_suffix(size_type n) {
244 CHECK(n <= length_);
245 length_ -= n;
246 }
[email protected]7d7b0ac2013-03-28 20:07:40247
248 int compare(const BasicStringPiece<STRING_TYPE>& x) const {
249 int r = wordmemcmp(
250 ptr_, x.ptr_, (length_ < x.length_ ? length_ : x.length_));
251 if (r == 0) {
252 if (length_ < x.length_) r = -1;
253 else if (length_ > x.length_) r = +1;
254 }
255 return r;
256 }
257
bnc222e8f32017-03-21 19:10:49258 // This is the style of conversion preferred by std::string_view in C++17.
259 explicit operator STRING_TYPE() const { return as_string(); }
260
[email protected]7d7b0ac2013-03-28 20:07:40261 STRING_TYPE as_string() const {
262 // std::string doesn't like to take a NULL pointer even with a 0 size.
263 return empty() ? STRING_TYPE() : STRING_TYPE(data(), size());
264 }
265
266 const_iterator begin() const { return ptr_; }
267 const_iterator end() const { return ptr_ + length_; }
268 const_reverse_iterator rbegin() const {
269 return const_reverse_iterator(ptr_ + length_);
270 }
271 const_reverse_iterator rend() const {
272 return const_reverse_iterator(ptr_);
273 }
274
275 size_type max_size() const { return length_; }
276 size_type capacity() const { return length_; }
277
278 static int wordmemcmp(const value_type* p,
279 const value_type* p2,
280 size_type N) {
281 return STRING_TYPE::traits_type::compare(p, p2, N);
282 }
283
[email protected]0ecab7892014-03-11 21:15:40284 // Sets the value of the given string target type to be the current string.
285 // This saves a temporary over doing |a = b.as_string()|
286 void CopyToString(STRING_TYPE* target) const {
287 internal::CopyToString(*this, target);
288 }
289
290 void AppendToString(STRING_TYPE* target) const {
291 internal::AppendToString(*this, target);
292 }
293
294 size_type copy(value_type* buf, size_type n, size_type pos = 0) const {
295 return internal::copy(*this, buf, n, pos);
296 }
297
298 // Does "this" start with "x"
299 bool starts_with(const BasicStringPiece& x) const {
300 return ((this->length_ >= x.length_) &&
301 (wordmemcmp(this->ptr_, x.ptr_, x.length_) == 0));
302 }
303
304 // Does "this" end with "x"
305 bool ends_with(const BasicStringPiece& x) const {
306 return ((this->length_ >= x.length_) &&
307 (wordmemcmp(this->ptr_ + (this->length_-x.length_),
308 x.ptr_, x.length_) == 0));
309 }
310
311 // find: Search for a character or substring at a given offset.
312 size_type find(const BasicStringPiece<STRING_TYPE>& s,
313 size_type pos = 0) const {
314 return internal::find(*this, s, pos);
315 }
316 size_type find(value_type c, size_type pos = 0) const {
317 return internal::find(*this, c, pos);
318 }
319
320 // rfind: Reverse find.
321 size_type rfind(const BasicStringPiece& s,
322 size_type pos = BasicStringPiece::npos) const {
323 return internal::rfind(*this, s, pos);
324 }
325 size_type rfind(value_type c, size_type pos = BasicStringPiece::npos) const {
326 return internal::rfind(*this, c, pos);
327 }
328
329 // find_first_of: Find the first occurence of one of a set of characters.
330 size_type find_first_of(const BasicStringPiece& s,
331 size_type pos = 0) const {
332 return internal::find_first_of(*this, s, pos);
333 }
334 size_type find_first_of(value_type c, size_type pos = 0) const {
335 return find(c, pos);
336 }
337
338 // find_first_not_of: Find the first occurence not of a set of characters.
339 size_type find_first_not_of(const BasicStringPiece& s,
340 size_type pos = 0) const {
341 return internal::find_first_not_of(*this, s, pos);
342 }
343 size_type find_first_not_of(value_type c, size_type pos = 0) const {
344 return internal::find_first_not_of(*this, c, pos);
345 }
346
347 // find_last_of: Find the last occurence of one of a set of characters.
348 size_type find_last_of(const BasicStringPiece& s,
349 size_type pos = BasicStringPiece::npos) const {
350 return internal::find_last_of(*this, s, pos);
351 }
352 size_type find_last_of(value_type c,
353 size_type pos = BasicStringPiece::npos) const {
354 return rfind(c, pos);
355 }
356
357 // find_last_not_of: Find the last occurence not of a set of characters.
358 size_type find_last_not_of(const BasicStringPiece& s,
359 size_type pos = BasicStringPiece::npos) const {
360 return internal::find_last_not_of(*this, s, pos);
361 }
362 size_type find_last_not_of(value_type c,
363 size_type pos = BasicStringPiece::npos) const {
364 return internal::find_last_not_of(*this, c, pos);
365 }
366
367 // substr.
368 BasicStringPiece substr(size_type pos,
369 size_type n = BasicStringPiece::npos) const {
370 return internal::substr(*this, pos, n);
371 }
372
[email protected]7d7b0ac2013-03-28 20:07:40373 protected:
374 const value_type* ptr_;
Chris Palmere5a6dfada2018-03-06 21:40:08375 size_type length_;
[email protected]7d7b0ac2013-03-28 20:07:40376};
377
378template <typename STRING_TYPE>
[email protected]0ecab7892014-03-11 21:15:40379const typename BasicStringPiece<STRING_TYPE>::size_type
380BasicStringPiece<STRING_TYPE>::npos =
381 typename BasicStringPiece<STRING_TYPE>::size_type(-1);
[email protected]7d7b0ac2013-03-28 20:07:40382
383// MSVC doesn't like complex extern templates and DLLs.
384#if !defined(COMPILER_MSVC)
[email protected]0ecab7892014-03-11 21:15:40385extern template class BASE_EXPORT BasicStringPiece<std::string>;
[email protected]50bc27c2014-03-07 01:45:03386extern template class BASE_EXPORT BasicStringPiece<string16>;
387#endif
[email protected]51cb94c2014-03-06 17:54:33388
[email protected]0ecab7892014-03-11 21:15:40389// StingPiece operators --------------------------------------------------------
390
[email protected]7d7b0ac2013-03-28 20:07:40391BASE_EXPORT bool operator==(const StringPiece& x, const StringPiece& y);
392
393inline bool operator!=(const StringPiece& x, const StringPiece& y) {
394 return !(x == y);
395}
396
397inline bool operator<(const StringPiece& x, const StringPiece& y) {
398 const int r = StringPiece::wordmemcmp(
399 x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size()));
400 return ((r < 0) || ((r == 0) && (x.size() < y.size())));
401}
402
403inline bool operator>(const StringPiece& x, const StringPiece& y) {
404 return y < x;
405}
406
407inline bool operator<=(const StringPiece& x, const StringPiece& y) {
408 return !(x > y);
409}
410
411inline bool operator>=(const StringPiece& x, const StringPiece& y) {
412 return !(x < y);
413}
414
[email protected]0ecab7892014-03-11 21:15:40415// StringPiece16 operators -----------------------------------------------------
416
[email protected]7d7b0ac2013-03-28 20:07:40417inline bool operator==(const StringPiece16& x, const StringPiece16& y) {
418 if (x.size() != y.size())
419 return false;
420
421 return StringPiece16::wordmemcmp(x.data(), y.data(), x.size()) == 0;
422}
423
424inline bool operator!=(const StringPiece16& x, const StringPiece16& y) {
425 return !(x == y);
426}
427
428inline bool operator<(const StringPiece16& x, const StringPiece16& y) {
429 const int r = StringPiece16::wordmemcmp(
430 x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size()));
431 return ((r < 0) || ((r == 0) && (x.size() < y.size())));
432}
433
434inline bool operator>(const StringPiece16& x, const StringPiece16& y) {
435 return y < x;
436}
437
438inline bool operator<=(const StringPiece16& x, const StringPiece16& y) {
439 return !(x > y);
440}
441
442inline bool operator>=(const StringPiece16& x, const StringPiece16& y) {
443 return !(x < y);
444}
445
446BASE_EXPORT std::ostream& operator<<(std::ostream& o,
447 const StringPiece& piece);
448
[email protected]0ecab7892014-03-11 21:15:40449// Hashing ---------------------------------------------------------------------
450
[email protected]7d7b0ac2013-03-28 20:07:40451// We provide appropriate hash functions so StringPiece and StringPiece16 can
452// be used as keys in hash sets and maps.
453
davidben411d3f72016-01-22 01:41:41454// This hash function is copied from base/strings/string16.h. We don't use the
455// ones already defined for string and string16 directly because it would
456// require the string constructors to be called, which we don't want.
bnc63c013002016-02-06 03:58:14457#define HASH_STRING_PIECE(StringPieceType, string_piece) \
458 std::size_t result = 0; \
459 for (StringPieceType::const_iterator i = string_piece.begin(); \
460 i != string_piece.end(); ++i) \
461 result = (result * 131) + *i; \
462 return result;
[email protected]7d7b0ac2013-03-28 20:07:40463
bnc63c013002016-02-06 03:58:14464struct StringPieceHash {
465 std::size_t operator()(const StringPiece& sp) const {
466 HASH_STRING_PIECE(StringPiece, sp);
[email protected]7d7b0ac2013-03-28 20:07:40467 }
468};
bnc63c013002016-02-06 03:58:14469struct StringPiece16Hash {
470 std::size_t operator()(const StringPiece16& sp16) const {
471 HASH_STRING_PIECE(StringPiece16, sp16);
[email protected]7d7b0ac2013-03-28 20:07:40472 }
473};
Denis Yaroshevskiy63dbcdf2018-03-28 02:44:10474struct WStringPieceHash {
475 std::size_t operator()(const WStringPiece& wsp) const {
476 HASH_STRING_PIECE(WStringPiece, wsp);
477 }
478};
[email protected]7d7b0ac2013-03-28 20:07:40479
bnc63c013002016-02-06 03:58:14480} // namespace base
[email protected]7d7b0ac2013-03-28 20:07:40481
482#endif // BASE_STRINGS_STRING_PIECE_H_