[go: nahoru, domu]

blob: d6236d13a071e4e717802abc48c956b1026dbade [file] [log] [blame]
brettw@chromium.org7d7b0ac2013-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//
rch@chromium.org16b200c2014-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.
brettw@chromium.org7d7b0ac2013-03-28 20:07:4013//
rch@chromium.org16b200c2014-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.
brettw@chromium.org7d7b0ac2013-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"
avi@chromium.orgc851cfd2013-06-10 20:11:1432#include "base/strings/string16.h"
alex-accc1bde62017-04-19 08:33:5533#include "base/strings/string_piece_forward.h"
brettw@chromium.org7d7b0ac2013-03-28 20:07:4034
35namespace base {
36
brettw@chromium.org0ecab7892014-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.
brettw@chromium.org7d7b0ac2013-03-28 20:07:4046namespace internal {
47
brettw@chromium.org0ecab7892014-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
brettw@chromium.org0ecab7892014-03-11 21:15:40150} // namespace internal
151
152// BasicStringPiece ------------------------------------------------------------
153
brettw@chromium.org7d7b0ac2013-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.
brettw@chromium.org0ecab7892014-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 {
brettw@chromium.org7d7b0ac2013-03-28 20:07:40161 public:
brettw@chromium.org0ecab7892014-03-11 21:15:40162 // Standard STL container boilerplate.
brettw@chromium.org7d7b0ac2013-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) {}
brettw@chromium.org0ecab7892014-03-11 21:15:40179 BasicStringPiece(const value_type* str)
brettw@chromium.org7d7b0ac2013-03-28 20:07:40180 : ptr_(str),
181 length_((str == NULL) ? 0 : STRING_TYPE::traits_type::length(str)) {}
brettw@chromium.org0ecab7892014-03-11 21:15:40182 BasicStringPiece(const STRING_TYPE& str)
brettw@chromium.org7d7b0ac2013-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)
brettw@chromium.org7d7b0ac2013-03-28 20:07:40185 : ptr_(offset), length_(len) {}
brettw@chromium.org0ecab7892014-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 }
brettw@chromium.org7d7b0ac2013-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_; }
brettw@chromium.org7d7b0ac2013-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
Jeremy Roman40a4dc12017-10-10 18:04:47222 constexpr value_type operator[](size_type i) const { return ptr_[i]; }
pkasting9022cb42016-02-05 00:08:56223 value_type front() const { return ptr_[0]; }
224 value_type back() const { return ptr_[length_ - 1]; }
brettw@chromium.org7d7b0ac2013-03-28 20:07:40225
Jeremy Roman40a4dc12017-10-10 18:04:47226 constexpr void remove_prefix(size_type n) {
brettw@chromium.org7d7b0ac2013-03-28 20:07:40227 ptr_ += n;
228 length_ -= n;
229 }
230
Jeremy Roman40a4dc12017-10-10 18:04:47231 constexpr void remove_suffix(size_type n) { length_ -= n; }
brettw@chromium.org7d7b0ac2013-03-28 20:07:40232
233 int compare(const BasicStringPiece<STRING_TYPE>& x) const {
234 int r = wordmemcmp(
235 ptr_, x.ptr_, (length_ < x.length_ ? length_ : x.length_));
236 if (r == 0) {
237 if (length_ < x.length_) r = -1;
238 else if (length_ > x.length_) r = +1;
239 }
240 return r;
241 }
242
bnc222e8f32017-03-21 19:10:49243 // This is the style of conversion preferred by std::string_view in C++17.
244 explicit operator STRING_TYPE() const { return as_string(); }
245
brettw@chromium.org7d7b0ac2013-03-28 20:07:40246 STRING_TYPE as_string() const {
247 // std::string doesn't like to take a NULL pointer even with a 0 size.
248 return empty() ? STRING_TYPE() : STRING_TYPE(data(), size());
249 }
250
251 const_iterator begin() const { return ptr_; }
252 const_iterator end() const { return ptr_ + length_; }
253 const_reverse_iterator rbegin() const {
254 return const_reverse_iterator(ptr_ + length_);
255 }
256 const_reverse_iterator rend() const {
257 return const_reverse_iterator(ptr_);
258 }
259
260 size_type max_size() const { return length_; }
261 size_type capacity() const { return length_; }
262
263 static int wordmemcmp(const value_type* p,
264 const value_type* p2,
265 size_type N) {
266 return STRING_TYPE::traits_type::compare(p, p2, N);
267 }
268
brettw@chromium.org0ecab7892014-03-11 21:15:40269 // Sets the value of the given string target type to be the current string.
270 // This saves a temporary over doing |a = b.as_string()|
271 void CopyToString(STRING_TYPE* target) const {
272 internal::CopyToString(*this, target);
273 }
274
275 void AppendToString(STRING_TYPE* target) const {
276 internal::AppendToString(*this, target);
277 }
278
279 size_type copy(value_type* buf, size_type n, size_type pos = 0) const {
280 return internal::copy(*this, buf, n, pos);
281 }
282
283 // Does "this" start with "x"
284 bool starts_with(const BasicStringPiece& x) const {
285 return ((this->length_ >= x.length_) &&
286 (wordmemcmp(this->ptr_, x.ptr_, x.length_) == 0));
287 }
288
289 // Does "this" end with "x"
290 bool ends_with(const BasicStringPiece& x) const {
291 return ((this->length_ >= x.length_) &&
292 (wordmemcmp(this->ptr_ + (this->length_-x.length_),
293 x.ptr_, x.length_) == 0));
294 }
295
296 // find: Search for a character or substring at a given offset.
297 size_type find(const BasicStringPiece<STRING_TYPE>& s,
298 size_type pos = 0) const {
299 return internal::find(*this, s, pos);
300 }
301 size_type find(value_type c, size_type pos = 0) const {
302 return internal::find(*this, c, pos);
303 }
304
305 // rfind: Reverse find.
306 size_type rfind(const BasicStringPiece& s,
307 size_type pos = BasicStringPiece::npos) const {
308 return internal::rfind(*this, s, pos);
309 }
310 size_type rfind(value_type c, size_type pos = BasicStringPiece::npos) const {
311 return internal::rfind(*this, c, pos);
312 }
313
314 // find_first_of: Find the first occurence of one of a set of characters.
315 size_type find_first_of(const BasicStringPiece& s,
316 size_type pos = 0) const {
317 return internal::find_first_of(*this, s, pos);
318 }
319 size_type find_first_of(value_type c, size_type pos = 0) const {
320 return find(c, pos);
321 }
322
323 // find_first_not_of: Find the first occurence not of a set of characters.
324 size_type find_first_not_of(const BasicStringPiece& s,
325 size_type pos = 0) const {
326 return internal::find_first_not_of(*this, s, pos);
327 }
328 size_type find_first_not_of(value_type c, size_type pos = 0) const {
329 return internal::find_first_not_of(*this, c, pos);
330 }
331
332 // find_last_of: Find the last occurence of one of a set of characters.
333 size_type find_last_of(const BasicStringPiece& s,
334 size_type pos = BasicStringPiece::npos) const {
335 return internal::find_last_of(*this, s, pos);
336 }
337 size_type find_last_of(value_type c,
338 size_type pos = BasicStringPiece::npos) const {
339 return rfind(c, pos);
340 }
341
342 // find_last_not_of: Find the last occurence not of a set of characters.
343 size_type find_last_not_of(const BasicStringPiece& s,
344 size_type pos = BasicStringPiece::npos) const {
345 return internal::find_last_not_of(*this, s, pos);
346 }
347 size_type find_last_not_of(value_type c,
348 size_type pos = BasicStringPiece::npos) const {
349 return internal::find_last_not_of(*this, c, pos);
350 }
351
352 // substr.
353 BasicStringPiece substr(size_type pos,
354 size_type n = BasicStringPiece::npos) const {
355 return internal::substr(*this, pos, n);
356 }
357
brettw@chromium.org7d7b0ac2013-03-28 20:07:40358 protected:
359 const value_type* ptr_;
360 size_type length_;
361};
362
363template <typename STRING_TYPE>
brettw@chromium.org0ecab7892014-03-11 21:15:40364const typename BasicStringPiece<STRING_TYPE>::size_type
365BasicStringPiece<STRING_TYPE>::npos =
366 typename BasicStringPiece<STRING_TYPE>::size_type(-1);
brettw@chromium.org7d7b0ac2013-03-28 20:07:40367
368// MSVC doesn't like complex extern templates and DLLs.
369#if !defined(COMPILER_MSVC)
brettw@chromium.org0ecab7892014-03-11 21:15:40370extern template class BASE_EXPORT BasicStringPiece<std::string>;
brettw@chromium.org50bc27c2014-03-07 01:45:03371extern template class BASE_EXPORT BasicStringPiece<string16>;
372#endif
brettw@chromium.org51cb94c2014-03-06 17:54:33373
brettw@chromium.org0ecab7892014-03-11 21:15:40374// StingPiece operators --------------------------------------------------------
375
brettw@chromium.org7d7b0ac2013-03-28 20:07:40376BASE_EXPORT bool operator==(const StringPiece& x, const StringPiece& y);
377
378inline bool operator!=(const StringPiece& x, const StringPiece& y) {
379 return !(x == y);
380}
381
382inline bool operator<(const StringPiece& x, const StringPiece& y) {
383 const int r = StringPiece::wordmemcmp(
384 x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size()));
385 return ((r < 0) || ((r == 0) && (x.size() < y.size())));
386}
387
388inline bool operator>(const StringPiece& x, const StringPiece& y) {
389 return y < x;
390}
391
392inline bool operator<=(const StringPiece& x, const StringPiece& y) {
393 return !(x > y);
394}
395
396inline bool operator>=(const StringPiece& x, const StringPiece& y) {
397 return !(x < y);
398}
399
brettw@chromium.org0ecab7892014-03-11 21:15:40400// StringPiece16 operators -----------------------------------------------------
401
brettw@chromium.org7d7b0ac2013-03-28 20:07:40402inline bool operator==(const StringPiece16& x, const StringPiece16& y) {
403 if (x.size() != y.size())
404 return false;
405
406 return StringPiece16::wordmemcmp(x.data(), y.data(), x.size()) == 0;
407}
408
409inline bool operator!=(const StringPiece16& x, const StringPiece16& y) {
410 return !(x == y);
411}
412
413inline bool operator<(const StringPiece16& x, const StringPiece16& y) {
414 const int r = StringPiece16::wordmemcmp(
415 x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size()));
416 return ((r < 0) || ((r == 0) && (x.size() < y.size())));
417}
418
419inline bool operator>(const StringPiece16& x, const StringPiece16& y) {
420 return y < x;
421}
422
423inline bool operator<=(const StringPiece16& x, const StringPiece16& y) {
424 return !(x > y);
425}
426
427inline bool operator>=(const StringPiece16& x, const StringPiece16& y) {
428 return !(x < y);
429}
430
431BASE_EXPORT std::ostream& operator<<(std::ostream& o,
432 const StringPiece& piece);
433
brettw@chromium.org0ecab7892014-03-11 21:15:40434// Hashing ---------------------------------------------------------------------
435
brettw@chromium.org7d7b0ac2013-03-28 20:07:40436// We provide appropriate hash functions so StringPiece and StringPiece16 can
437// be used as keys in hash sets and maps.
438
davidben411d3f72016-01-22 01:41:41439// This hash function is copied from base/strings/string16.h. We don't use the
440// ones already defined for string and string16 directly because it would
441// require the string constructors to be called, which we don't want.
bnc63c013002016-02-06 03:58:14442#define HASH_STRING_PIECE(StringPieceType, string_piece) \
443 std::size_t result = 0; \
444 for (StringPieceType::const_iterator i = string_piece.begin(); \
445 i != string_piece.end(); ++i) \
446 result = (result * 131) + *i; \
447 return result;
brettw@chromium.org7d7b0ac2013-03-28 20:07:40448
bnc63c013002016-02-06 03:58:14449struct StringPieceHash {
450 std::size_t operator()(const StringPiece& sp) const {
451 HASH_STRING_PIECE(StringPiece, sp);
brettw@chromium.org7d7b0ac2013-03-28 20:07:40452 }
453};
bnc63c013002016-02-06 03:58:14454struct StringPiece16Hash {
455 std::size_t operator()(const StringPiece16& sp16) const {
456 HASH_STRING_PIECE(StringPiece16, sp16);
brettw@chromium.org7d7b0ac2013-03-28 20:07:40457 }
458};
459
bnc63c013002016-02-06 03:58:14460} // namespace base
brettw@chromium.org7d7b0ac2013-03-28 20:07:40461
462#endif // BASE_STRINGS_STRING_PIECE_H_