blob: 14ee635abf1eb574e441cf46811ee3d82f7cf7ba [file] [log] [blame]
Vitaly Bukacbed2062015-08-17 12:54:05 -07001// 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//
8// 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.
13//
14// 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.
21
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"
31#include "base/basictypes.h"
Vitaly Bukacbed2062015-08-17 12:54:05 -070032
33namespace base {
34
35template <typename STRING_TYPE> class BasicStringPiece;
36typedef BasicStringPiece<std::string> StringPiece;
Vitaly Bukacbed2062015-08-17 12:54:05 -070037
38// internal --------------------------------------------------------------------
39
40// Many of the StringPiece functions use different implementations for the
41// 8-bit and 16-bit versions, and we don't want lots of template expansions in
42// this (very common) header that will slow down compilation.
43//
44// So here we define overloaded functions called by the StringPiece template.
45// For those that share an implementation, the two versions will expand to a
46// template internal to the .cc file.
47namespace internal {
48
Vitaly Buka60b8f002015-08-20 13:47:48 -070049void CopyToString(const StringPiece& self, std::string* target);
Vitaly Bukacbed2062015-08-17 12:54:05 -070050
Vitaly Buka60b8f002015-08-20 13:47:48 -070051void AppendToString(const StringPiece& self, std::string* target);
Vitaly Bukacbed2062015-08-17 12:54:05 -070052
Vitaly Buka60b8f002015-08-20 13:47:48 -070053size_t copy(const StringPiece& self, char* buf, size_t n, size_t pos);
Vitaly Bukacbed2062015-08-17 12:54:05 -070054
Vitaly Buka60b8f002015-08-20 13:47:48 -070055size_t find(const StringPiece& self, const StringPiece& s, size_t pos);
56size_t find(const StringPiece& self, char c, size_t pos);
Vitaly Bukacbed2062015-08-17 12:54:05 -070057
Vitaly Buka60b8f002015-08-20 13:47:48 -070058size_t rfind(const StringPiece& self, const StringPiece& s, size_t pos);
59size_t rfind(const StringPiece& self, char c, size_t pos);
60
61size_t find_first_of(const StringPiece& self, const StringPiece& s, size_t pos);
62
63size_t find_first_not_of(const StringPiece& self,
Vitaly Bukacbed2062015-08-17 12:54:05 -070064 const StringPiece& s,
65 size_t pos);
Vitaly Buka60b8f002015-08-20 13:47:48 -070066size_t find_first_not_of(const StringPiece& self, char c, size_t pos);
Vitaly Bukacbed2062015-08-17 12:54:05 -070067
Vitaly Buka60b8f002015-08-20 13:47:48 -070068size_t find_last_of(const StringPiece& self, const StringPiece& s, size_t pos);
69size_t find_last_of(const StringPiece& self, char c, size_t pos);
Vitaly Bukacbed2062015-08-17 12:54:05 -070070
Vitaly Buka60b8f002015-08-20 13:47:48 -070071size_t find_last_not_of(const StringPiece& self,
72 const StringPiece& s,
73 size_t pos);
74size_t find_last_not_of(const StringPiece& self, char c, size_t pos);
Vitaly Bukacbed2062015-08-17 12:54:05 -070075
Vitaly Buka60b8f002015-08-20 13:47:48 -070076StringPiece substr(const StringPiece& self, size_t pos, size_t n);
Vitaly Bukacbed2062015-08-17 12:54:05 -070077
78} // namespace internal
79
80// BasicStringPiece ------------------------------------------------------------
81
82// Defines the types, methods, operators, and data members common to both
83// StringPiece and StringPiece16. Do not refer to this class directly, but
84// rather to BasicStringPiece, StringPiece, or StringPiece16.
85//
86// This is templatized by string class type rather than character type, so
87// BasicStringPiece<std::string> or BasicStringPiece<base::string16>.
88template <typename STRING_TYPE> class BasicStringPiece {
89 public:
90 // Standard STL container boilerplate.
91 typedef size_t size_type;
92 typedef typename STRING_TYPE::value_type value_type;
93 typedef const value_type* pointer;
94 typedef const value_type& reference;
95 typedef const value_type& const_reference;
96 typedef ptrdiff_t difference_type;
97 typedef const value_type* const_iterator;
98 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
99
100 static const size_type npos;
101
102 public:
103 // We provide non-explicit singleton constructors so users can pass
104 // in a "const char*" or a "string" wherever a "StringPiece" is
105 // expected (likewise for char16, string16, StringPiece16).
106 BasicStringPiece() : ptr_(NULL), length_(0) {}
107 BasicStringPiece(const value_type* str)
108 : ptr_(str),
109 length_((str == NULL) ? 0 : STRING_TYPE::traits_type::length(str)) {}
110 BasicStringPiece(const STRING_TYPE& str)
111 : ptr_(str.data()), length_(str.size()) {}
112 BasicStringPiece(const value_type* offset, size_type len)
113 : ptr_(offset), length_(len) {}
114 BasicStringPiece(const typename STRING_TYPE::const_iterator& begin,
115 const typename STRING_TYPE::const_iterator& end)
116 : ptr_((end > begin) ? &(*begin) : NULL),
117 length_((end > begin) ? (size_type)(end - begin) : 0) {}
118
119 // data() may return a pointer to a buffer with embedded NULs, and the
120 // returned buffer may or may not be null terminated. Therefore it is
121 // typically a mistake to pass data() to a routine that expects a NUL
122 // terminated string.
123 const value_type* data() const { return ptr_; }
124 size_type size() const { return length_; }
125 size_type length() const { return length_; }
126 bool empty() const { return length_ == 0; }
127
128 void clear() {
129 ptr_ = NULL;
130 length_ = 0;
131 }
132 void set(const value_type* data, size_type len) {
133 ptr_ = data;
134 length_ = len;
135 }
136 void set(const value_type* str) {
137 ptr_ = str;
138 length_ = str ? STRING_TYPE::traits_type::length(str) : 0;
139 }
140
141 value_type operator[](size_type i) const { return ptr_[i]; }
142
143 void remove_prefix(size_type n) {
144 ptr_ += n;
145 length_ -= n;
146 }
147
148 void remove_suffix(size_type n) {
149 length_ -= n;
150 }
151
152 int compare(const BasicStringPiece<STRING_TYPE>& x) const {
153 int r = wordmemcmp(
154 ptr_, x.ptr_, (length_ < x.length_ ? length_ : x.length_));
155 if (r == 0) {
156 if (length_ < x.length_) r = -1;
157 else if (length_ > x.length_) r = +1;
158 }
159 return r;
160 }
161
162 STRING_TYPE as_string() const {
163 // std::string doesn't like to take a NULL pointer even with a 0 size.
164 return empty() ? STRING_TYPE() : STRING_TYPE(data(), size());
165 }
166
167 const_iterator begin() const { return ptr_; }
168 const_iterator end() const { return ptr_ + length_; }
169 const_reverse_iterator rbegin() const {
170 return const_reverse_iterator(ptr_ + length_);
171 }
172 const_reverse_iterator rend() const {
173 return const_reverse_iterator(ptr_);
174 }
175
176 size_type max_size() const { return length_; }
177 size_type capacity() const { return length_; }
178
179 static int wordmemcmp(const value_type* p,
180 const value_type* p2,
181 size_type N) {
182 return STRING_TYPE::traits_type::compare(p, p2, N);
183 }
184
185 // Sets the value of the given string target type to be the current string.
186 // This saves a temporary over doing |a = b.as_string()|
187 void CopyToString(STRING_TYPE* target) const {
188 internal::CopyToString(*this, target);
189 }
190
191 void AppendToString(STRING_TYPE* target) const {
192 internal::AppendToString(*this, target);
193 }
194
195 size_type copy(value_type* buf, size_type n, size_type pos = 0) const {
196 return internal::copy(*this, buf, n, pos);
197 }
198
199 // Does "this" start with "x"
200 bool starts_with(const BasicStringPiece& x) const {
201 return ((this->length_ >= x.length_) &&
202 (wordmemcmp(this->ptr_, x.ptr_, x.length_) == 0));
203 }
204
205 // Does "this" end with "x"
206 bool ends_with(const BasicStringPiece& x) const {
207 return ((this->length_ >= x.length_) &&
208 (wordmemcmp(this->ptr_ + (this->length_-x.length_),
209 x.ptr_, x.length_) == 0));
210 }
211
212 // find: Search for a character or substring at a given offset.
213 size_type find(const BasicStringPiece<STRING_TYPE>& s,
214 size_type pos = 0) const {
215 return internal::find(*this, s, pos);
216 }
217 size_type find(value_type c, size_type pos = 0) const {
218 return internal::find(*this, c, pos);
219 }
220
221 // rfind: Reverse find.
222 size_type rfind(const BasicStringPiece& s,
223 size_type pos = BasicStringPiece::npos) const {
224 return internal::rfind(*this, s, pos);
225 }
226 size_type rfind(value_type c, size_type pos = BasicStringPiece::npos) const {
227 return internal::rfind(*this, c, pos);
228 }
229
230 // find_first_of: Find the first occurence of one of a set of characters.
231 size_type find_first_of(const BasicStringPiece& s,
232 size_type pos = 0) const {
233 return internal::find_first_of(*this, s, pos);
234 }
235 size_type find_first_of(value_type c, size_type pos = 0) const {
236 return find(c, pos);
237 }
238
239 // find_first_not_of: Find the first occurence not of a set of characters.
240 size_type find_first_not_of(const BasicStringPiece& s,
241 size_type pos = 0) const {
242 return internal::find_first_not_of(*this, s, pos);
243 }
244 size_type find_first_not_of(value_type c, size_type pos = 0) const {
245 return internal::find_first_not_of(*this, c, pos);
246 }
247
248 // find_last_of: Find the last occurence of one of a set of characters.
249 size_type find_last_of(const BasicStringPiece& s,
250 size_type pos = BasicStringPiece::npos) const {
251 return internal::find_last_of(*this, s, pos);
252 }
253 size_type find_last_of(value_type c,
254 size_type pos = BasicStringPiece::npos) const {
255 return rfind(c, pos);
256 }
257
258 // find_last_not_of: Find the last occurence not of a set of characters.
259 size_type find_last_not_of(const BasicStringPiece& s,
260 size_type pos = BasicStringPiece::npos) const {
261 return internal::find_last_not_of(*this, s, pos);
262 }
263 size_type find_last_not_of(value_type c,
264 size_type pos = BasicStringPiece::npos) const {
265 return internal::find_last_not_of(*this, c, pos);
266 }
267
268 // substr.
269 BasicStringPiece substr(size_type pos,
270 size_type n = BasicStringPiece::npos) const {
271 return internal::substr(*this, pos, n);
272 }
273
274 protected:
275 const value_type* ptr_;
276 size_type length_;
277};
278
279template <typename STRING_TYPE>
280const typename BasicStringPiece<STRING_TYPE>::size_type
281BasicStringPiece<STRING_TYPE>::npos =
282 typename BasicStringPiece<STRING_TYPE>::size_type(-1);
283
284// MSVC doesn't like complex extern templates and DLLs.
285#if !defined(COMPILER_MSVC)
Vitaly Buka60b8f002015-08-20 13:47:48 -0700286extern template class BasicStringPiece<std::string>;
Vitaly Bukacbed2062015-08-17 12:54:05 -0700287#endif
288
289// StingPiece operators --------------------------------------------------------
290
Vitaly Buka60b8f002015-08-20 13:47:48 -0700291bool operator==(const StringPiece& x, const StringPiece& y);
Vitaly Bukacbed2062015-08-17 12:54:05 -0700292
293inline bool operator!=(const StringPiece& x, const StringPiece& y) {
294 return !(x == y);
295}
296
297inline bool operator<(const StringPiece& x, const StringPiece& y) {
298 const int r = StringPiece::wordmemcmp(
299 x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size()));
300 return ((r < 0) || ((r == 0) && (x.size() < y.size())));
301}
302
303inline bool operator>(const StringPiece& x, const StringPiece& y) {
304 return y < x;
305}
306
307inline bool operator<=(const StringPiece& x, const StringPiece& y) {
308 return !(x > y);
309}
310
311inline bool operator>=(const StringPiece& x, const StringPiece& y) {
312 return !(x < y);
313}
314
Vitaly Buka60b8f002015-08-20 13:47:48 -0700315std::ostream& operator<<(std::ostream& o, const StringPiece& piece);
Vitaly Bukacbed2062015-08-17 12:54:05 -0700316
317} // namespace base
318
Vitaly Bukacbed2062015-08-17 12:54:05 -0700319#endif // BASE_STRINGS_STRING_PIECE_H_