OLD | NEW |
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.h with modifications | 4 // Copied from strings/stringpiece.h with modifications |
5 // | 5 // |
6 // A string-like object that points to a sized piece of memory. | 6 // A string-like object that points to a sized piece of memory. |
7 // | 7 // |
8 // You can use StringPiece as a function or method parameter. A StringPiece | 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 | 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 | 10 // char*" argument, a string argument, or a StringPiece argument with no data |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 int compare(const BasicStringPiece<STRING_TYPE>& x) const { | 238 int compare(const BasicStringPiece<STRING_TYPE>& x) const { |
239 int r = wordmemcmp( | 239 int r = wordmemcmp( |
240 ptr_, x.ptr_, (length_ < x.length_ ? length_ : x.length_)); | 240 ptr_, x.ptr_, (length_ < x.length_ ? length_ : x.length_)); |
241 if (r == 0) { | 241 if (r == 0) { |
242 if (length_ < x.length_) r = -1; | 242 if (length_ < x.length_) r = -1; |
243 else if (length_ > x.length_) r = +1; | 243 else if (length_ > x.length_) r = +1; |
244 } | 244 } |
245 return r; | 245 return r; |
246 } | 246 } |
247 | 247 |
| 248 // This is the style of conversion preferred by std::string_view in C++17. |
| 249 explicit operator STRING_TYPE() const { return as_string(); } |
| 250 |
248 STRING_TYPE as_string() const { | 251 STRING_TYPE as_string() const { |
249 // std::string doesn't like to take a NULL pointer even with a 0 size. | 252 // std::string doesn't like to take a NULL pointer even with a 0 size. |
250 return empty() ? STRING_TYPE() : STRING_TYPE(data(), size()); | 253 return empty() ? STRING_TYPE() : STRING_TYPE(data(), size()); |
251 } | 254 } |
252 | 255 |
253 const_iterator begin() const { return ptr_; } | 256 const_iterator begin() const { return ptr_; } |
254 const_iterator end() const { return ptr_ + length_; } | 257 const_iterator end() const { return ptr_ + length_; } |
255 const_reverse_iterator rbegin() const { | 258 const_reverse_iterator rbegin() const { |
256 return const_reverse_iterator(ptr_ + length_); | 259 return const_reverse_iterator(ptr_ + length_); |
257 } | 260 } |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 }; | 458 }; |
456 struct StringPiece16Hash { | 459 struct StringPiece16Hash { |
457 std::size_t operator()(const StringPiece16& sp16) const { | 460 std::size_t operator()(const StringPiece16& sp16) const { |
458 HASH_STRING_PIECE(StringPiece16, sp16); | 461 HASH_STRING_PIECE(StringPiece16, sp16); |
459 } | 462 } |
460 }; | 463 }; |
461 | 464 |
462 } // namespace base | 465 } // namespace base |
463 | 466 |
464 #endif // BASE_STRINGS_STRING_PIECE_H_ | 467 #endif // BASE_STRINGS_STRING_PIECE_H_ |
OLD | NEW |