Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(181)

Side by Side Diff: base/strings/string_piece.h

Issue 2758163003: Use string(StringPiece) instead of StringPiece::as_string(). (Closed)
Patch Set: Add StringPiece unittests. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/strings/string_piece_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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_
OLDNEW
« no previous file with comments | « no previous file | base/strings/string_piece_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698