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
3 // found in the LICENSE file.
4 // Copied from strings/stringpiece.h with modifications
6 // A string-like object that points to a sized piece of memory.
8 // Functions or methods may use const StringPiece& parameters to accept either
9 // a "const char*" or a "string" value that will be implicitly converted to
10 // a StringPiece. The implicit conversion means that it is often appropriate
11 // to include this .h file in other files rather than forward-declaring
12 // StringPiece as would be appropriate for most other Google classes.
14 // Systematic usage of StringPiece is encouraged as it will reduce unnecessary
15 // conversions from "const char*" to "string" and back again.
17 // StringPiece16 is similar to StringPiece but for base::string16 instead of
18 // std::string. We do not define as large of a subset of the STL functions
19 // from basic_string as in StringPiece, but this can be changed if these
20 // functions (find, find_first_of, etc.) are found to be useful in this context.
23 #ifndef BASE_STRING_PIECE_H_
24 #define BASE_STRING_PIECE_H_
32 #include "base/base_export.h"
33 #include "base/basictypes.h"
34 #include "base/hash_tables.h"
35 #include "base/string16.h"
39 template <typename STRING_TYPE
> class BasicStringPiece
;
40 typedef BasicStringPiece
<std::string
> StringPiece
;
41 typedef BasicStringPiece
<string16
> StringPiece16
;
45 // Defines the types, methods, operators, and data members common to both
46 // StringPiece and StringPiece16. Do not refer to this class directly, but
47 // rather to BasicStringPiece, StringPiece, or StringPiece16.
48 template <typename STRING_TYPE
> class StringPieceDetail
{
50 // standard STL container boilerplate
51 typedef size_t size_type
;
52 typedef typename
STRING_TYPE::value_type value_type
;
53 typedef const value_type
* pointer
;
54 typedef const value_type
& reference
;
55 typedef const value_type
& const_reference
;
56 typedef ptrdiff_t difference_type
;
57 typedef const value_type
* const_iterator
;
58 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
60 static const size_type npos
;
63 // We provide non-explicit singleton constructors so users can pass
64 // in a "const char*" or a "string" wherever a "StringPiece" is
65 // expected (likewise for char16, string16, StringPiece16).
66 StringPieceDetail() : ptr_(NULL
), length_(0) {}
67 StringPieceDetail(const value_type
* str
)
69 length_((str
== NULL
) ? 0 : STRING_TYPE::traits_type::length(str
)) {}
70 StringPieceDetail(const STRING_TYPE
& str
)
71 : ptr_(str
.data()), length_(str
.size()) {}
72 StringPieceDetail(const value_type
* offset
, size_type len
)
73 : ptr_(offset
), length_(len
) {}
74 StringPieceDetail(const typename
STRING_TYPE::const_iterator
& begin
,
75 const typename
STRING_TYPE::const_iterator
& end
)
76 : ptr_((end
> begin
) ? &(*begin
) : NULL
),
77 length_((end
> begin
) ? (size_type
)(end
- begin
) : 0) {}
79 // data() may return a pointer to a buffer with embedded NULs, and the
80 // returned buffer may or may not be null terminated. Therefore it is
81 // typically a mistake to pass data() to a routine that expects a NUL
83 const value_type
* data() const { return ptr_
; }
84 size_type
size() const { return length_
; }
85 size_type
length() const { return length_
; }
86 bool empty() const { return length_
== 0; }
92 void set(const value_type
* data
, size_type len
) {
96 void set(const value_type
* str
) {
98 length_
= str
? STRING_TYPE::traits_type::length(str
) : 0;
101 value_type
operator[](size_type i
) const { return ptr_
[i
]; }
103 void remove_prefix(size_type n
) {
108 void remove_suffix(size_type n
) {
112 int compare(const BasicStringPiece
<STRING_TYPE
>& x
) const {
114 ptr_
, x
.ptr_
, (length_
< x
.length_
? length_
: x
.length_
));
116 if (length_
< x
.length_
) r
= -1;
117 else if (length_
> x
.length_
) r
= +1;
122 STRING_TYPE
as_string() const {
123 // std::string doesn't like to take a NULL pointer even with a 0 size.
124 return empty() ? STRING_TYPE() : STRING_TYPE(data(), size());
127 const_iterator
begin() const { return ptr_
; }
128 const_iterator
end() const { return ptr_
+ length_
; }
129 const_reverse_iterator
rbegin() const {
130 return const_reverse_iterator(ptr_
+ length_
);
132 const_reverse_iterator
rend() const {
133 return const_reverse_iterator(ptr_
);
136 size_type
max_size() const { return length_
; }
137 size_type
capacity() const { return length_
; }
139 static int wordmemcmp(const value_type
* p
,
140 const value_type
* p2
,
142 return STRING_TYPE::traits_type::compare(p
, p2
, N
);
146 const value_type
* ptr_
;
150 template <typename STRING_TYPE
>
151 const typename StringPieceDetail
<STRING_TYPE
>::size_type
152 StringPieceDetail
<STRING_TYPE
>::npos
=
153 typename StringPieceDetail
<STRING_TYPE
>::size_type(-1);
155 // MSVC doesn't like complex extern templates and DLLs.
156 #if !defined(COMPILER_MSVC)
157 extern template class BASE_EXPORT StringPieceDetail
<std::string
>;
158 extern template class BASE_EXPORT StringPieceDetail
<string16
>;
161 BASE_EXPORT
void CopyToString(const StringPiece
& self
, std::string
* target
);
162 BASE_EXPORT
void AppendToString(const StringPiece
& self
, std::string
* target
);
163 BASE_EXPORT StringPieceDetail
<std::string
>::size_type
copy(
164 const StringPiece
& self
,
166 StringPieceDetail
<std::string
>::size_type n
,
167 StringPieceDetail
<std::string
>::size_type pos
);
168 BASE_EXPORT StringPieceDetail
<std::string
>::size_type
find(
169 const StringPiece
& self
,
170 const StringPiece
& s
,
171 StringPieceDetail
<std::string
>::size_type pos
);
172 BASE_EXPORT StringPieceDetail
<std::string
>::size_type
find(
173 const StringPiece
& self
,
175 StringPieceDetail
<std::string
>::size_type pos
);
176 BASE_EXPORT StringPieceDetail
<std::string
>::size_type
rfind(
177 const StringPiece
& self
,
178 const StringPiece
& s
,
179 StringPieceDetail
<std::string
>::size_type pos
);
180 BASE_EXPORT StringPieceDetail
<std::string
>::size_type
rfind(
181 const StringPiece
& self
,
183 StringPieceDetail
<std::string
>::size_type pos
);
184 BASE_EXPORT StringPieceDetail
<std::string
>::size_type
find_first_of(
185 const StringPiece
& self
,
186 const StringPiece
& s
,
187 StringPieceDetail
<std::string
>::size_type pos
);
188 BASE_EXPORT StringPieceDetail
<std::string
>::size_type
find_first_not_of(
189 const StringPiece
& self
,
190 const StringPiece
& s
,
191 StringPieceDetail
<std::string
>::size_type pos
);
192 BASE_EXPORT StringPieceDetail
<std::string
>::size_type
find_first_not_of(
193 const StringPiece
& self
,
195 StringPieceDetail
<std::string
>::size_type pos
);
196 BASE_EXPORT StringPieceDetail
<std::string
>::size_type
find_last_of(
197 const StringPiece
& self
,
198 const StringPiece
& s
,
199 StringPieceDetail
<std::string
>::size_type pos
);
200 BASE_EXPORT StringPieceDetail
<std::string
>::size_type
find_last_of(
201 const StringPiece
& self
,
203 StringPieceDetail
<std::string
>::size_type pos
);
204 BASE_EXPORT StringPieceDetail
<std::string
>::size_type
find_last_not_of(
205 const StringPiece
& self
,
206 const StringPiece
& s
,
207 StringPieceDetail
<std::string
>::size_type pos
);
208 BASE_EXPORT StringPieceDetail
<std::string
>::size_type
find_last_not_of(
209 const StringPiece
& self
,
211 StringPieceDetail
<std::string
>::size_type pos
);
212 BASE_EXPORT StringPiece
substr(const StringPiece
& self
,
213 StringPieceDetail
<std::string
>::size_type pos
,
214 StringPieceDetail
<std::string
>::size_type n
);
215 } // namespace internal
217 // Defines the template type that is instantiated as either StringPiece or
219 template <typename STRING_TYPE
> class BasicStringPiece
:
220 public internal::StringPieceDetail
<STRING_TYPE
> {
222 typedef typename
internal::StringPieceDetail
<STRING_TYPE
>::value_type
224 typedef typename
internal::StringPieceDetail
<STRING_TYPE
>::size_type
227 BasicStringPiece() {}
228 BasicStringPiece(const value_type
*str
)
229 : internal::StringPieceDetail
<STRING_TYPE
>(str
) {}
230 BasicStringPiece(const STRING_TYPE
& str
)
231 : internal::StringPieceDetail
<STRING_TYPE
>(str
) {}
232 BasicStringPiece(const value_type
* offset
, size_type len
)
233 : internal::StringPieceDetail
<STRING_TYPE
>(offset
, len
) {}
234 BasicStringPiece(const typename
STRING_TYPE::const_iterator
& begin
,
235 const typename
STRING_TYPE::const_iterator
& end
)
236 : internal::StringPieceDetail
<STRING_TYPE
>(begin
, end
) {}
239 // Specializes BasicStringPiece for std::string to add a few operations that
240 // are not needed for string16.
241 template <> class BasicStringPiece
<std::string
> :
242 public internal::StringPieceDetail
<std::string
> {
244 BasicStringPiece() {}
245 BasicStringPiece(const char* str
)
246 : internal::StringPieceDetail
<std::string
>(str
) {}
247 BasicStringPiece(const std::string
& str
)
248 : internal::StringPieceDetail
<std::string
>(str
) {}
249 BasicStringPiece(const char* offset
, size_type len
)
250 : internal::StringPieceDetail
<std::string
>(offset
, len
) {}
251 BasicStringPiece(const std::string::const_iterator
& begin
,
252 const std::string::const_iterator
& end
)
253 : internal::StringPieceDetail
<std::string
>(begin
, end
) {}
255 // Prevent the following overload of set() from hiding the definitions in the
257 using internal::StringPieceDetail
<std::string
>::set
;
259 void set(const void* data
, size_type len
) {
260 ptr_
= reinterpret_cast<const value_type
*>(data
);
264 void CopyToString(std::string
* target
) const {
265 internal::CopyToString(*this, target
);
268 void AppendToString(std::string
* target
) const {
269 internal::AppendToString(*this, target
);
272 // Does "this" start with "x"
273 bool starts_with(const BasicStringPiece
& x
) const {
274 return ((length_
>= x
.length_
) &&
275 (wordmemcmp(ptr_
, x
.ptr_
, x
.length_
) == 0));
278 // Does "this" end with "x"
279 bool ends_with(const BasicStringPiece
& x
) const {
280 return ((length_
>= x
.length_
) &&
281 (wordmemcmp(ptr_
+ (length_
-x
.length_
), x
.ptr_
, x
.length_
) == 0));
284 size_type
copy(char* buf
, size_type n
, size_type pos
= 0) const {
285 return internal::copy(*this, buf
, n
, pos
);
288 size_type
find(const BasicStringPiece
& s
, size_type pos
= 0) const {
289 return internal::find(*this, s
, pos
);
292 size_type
find(char c
, size_type pos
= 0) const {
293 return internal::find(*this, c
, pos
);
296 size_type
rfind(const BasicStringPiece
& s
, size_type pos
= npos
) const {
297 return internal::rfind(*this, s
, pos
);
300 size_type
rfind(char c
, size_type pos
= npos
) const {
301 return internal::rfind(*this, c
, pos
);
304 size_type
find_first_of(const BasicStringPiece
& s
, size_type pos
= 0) const {
305 return internal::find_first_of(*this, s
, pos
);
308 size_type
find_first_of(char c
, size_type pos
= 0) const {
312 size_type
find_first_not_of(const BasicStringPiece
& s
,
313 size_type pos
= 0) const {
314 return internal::find_first_not_of(*this, s
, pos
);
317 size_type
find_first_not_of(char c
, size_type pos
= 0) const {
318 return internal::find_first_not_of(*this, c
, pos
);
321 size_type
find_last_of(const BasicStringPiece
& s
,
322 size_type pos
= npos
) const {
323 return internal::find_last_of(*this, s
, pos
);
326 size_type
find_last_of(char c
, size_type pos
= npos
) const {
327 return rfind(c
, pos
);
330 size_type
find_last_not_of(const BasicStringPiece
& s
,
331 size_type pos
= npos
) const {
332 return internal::find_last_not_of(*this, s
, pos
);
335 size_type
find_last_not_of(char c
, size_type pos
= npos
) const {
336 return internal::find_last_not_of(*this, c
, pos
);
339 BasicStringPiece
substr(size_type pos
, size_type n
= npos
) const {
340 return internal::substr(*this, pos
, n
);
344 // MSVC doesn't like complex extern templates and DLLs.
345 #if !defined(COMPILER_MSVC)
346 // We can't explicitly declare the std::string instantiation here because it was
347 // already instantiated when specialized, above. Not only is it a no-op, but
348 // currently it also crashes Clang (see http://crbug.com/107412).
349 extern template class BASE_EXPORT BasicStringPiece
<string16
>;
352 BASE_EXPORT
bool operator==(const StringPiece
& x
, const StringPiece
& y
);
354 inline bool operator!=(const StringPiece
& x
, const StringPiece
& y
) {
358 inline bool operator<(const StringPiece
& x
, const StringPiece
& y
) {
359 const int r
= StringPiece::wordmemcmp(
360 x
.data(), y
.data(), (x
.size() < y
.size() ? x
.size() : y
.size()));
361 return ((r
< 0) || ((r
== 0) && (x
.size() < y
.size())));
364 inline bool operator>(const StringPiece
& x
, const StringPiece
& y
) {
368 inline bool operator<=(const StringPiece
& x
, const StringPiece
& y
) {
372 inline bool operator>=(const StringPiece
& x
, const StringPiece
& y
) {
376 inline bool operator==(const StringPiece16
& x
, const StringPiece16
& y
) {
377 if (x
.size() != y
.size())
380 return StringPiece16::wordmemcmp(x
.data(), y
.data(), x
.size()) == 0;
383 inline bool operator!=(const StringPiece16
& x
, const StringPiece16
& y
) {
387 inline bool operator<(const StringPiece16
& x
, const StringPiece16
& y
) {
388 const int r
= StringPiece16::wordmemcmp(
389 x
.data(), y
.data(), (x
.size() < y
.size() ? x
.size() : y
.size()));
390 return ((r
< 0) || ((r
== 0) && (x
.size() < y
.size())));
393 inline bool operator>(const StringPiece16
& x
, const StringPiece16
& y
) {
397 inline bool operator<=(const StringPiece16
& x
, const StringPiece16
& y
) {
401 inline bool operator>=(const StringPiece16
& x
, const StringPiece16
& y
) {
405 BASE_EXPORT
std::ostream
& operator<<(std::ostream
& o
,
406 const StringPiece
& piece
);
410 // We provide appropriate hash functions so StringPiece and StringPiece16 can
411 // be used as keys in hash sets and maps.
413 // This hash function is copied from base/hash_tables.h. We don't use the
414 // ones already defined for string and string16 directly because it would
415 // require the string constructors to be called, which we don't want.
416 #define HASH_STRING_PIECE(StringPieceType, string_piece) \
417 std::size_t result = 0; \
418 for (StringPieceType::const_iterator i = string_piece.begin(); \
419 i != string_piece.end(); ++i) \
420 result = (result * 131) + *i; \
423 namespace BASE_HASH_NAMESPACE {
424 #if defined(COMPILER_GCC)
427 struct hash
<base::StringPiece
> {
428 std::size_t operator()(const base::StringPiece
& sp
) const {
429 HASH_STRING_PIECE(base::StringPiece
, sp
);
433 struct hash
<base::StringPiece16
> {
434 std::size_t operator()(const base::StringPiece16
& sp16
) const {
435 HASH_STRING_PIECE(base::StringPiece16
, sp16
);
439 #elif defined(COMPILER_MSVC)
441 inline size_t hash_value(const base::StringPiece
& sp
) {
442 HASH_STRING_PIECE(base::StringPiece
, sp
);
444 inline size_t hash_value(const base::StringPiece16
& sp16
) {
445 HASH_STRING_PIECE(base::StringPiece16
, sp16
);
450 } // namespace BASE_HASH_NAMESPACE
452 #endif // BASE_STRING_PIECE_H_