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

Side by Side Diff: base/string_piece.h

Issue 8846001: Add iterator range constructors to StringPiece. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove extra blank line. Created 9 years 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | base/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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 // Functions or methods may use const StringPiece& parameters to accept either 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 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 10 // a StringPiece. The implicit conversion means that it is often appropriate
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 // We provide non-explicit singleton constructors so users can pass 58 // We provide non-explicit singleton constructors so users can pass
59 // in a "const char*" or a "string" wherever a "StringPiece" is 59 // in a "const char*" or a "string" wherever a "StringPiece" is
60 // expected (likewise for char16, string16, StringPiece16). 60 // expected (likewise for char16, string16, StringPiece16).
61 StringPieceDetail() : ptr_(NULL), length_(0) {} 61 StringPieceDetail() : ptr_(NULL), length_(0) {}
62 StringPieceDetail(const value_type* str) 62 StringPieceDetail(const value_type* str)
63 : ptr_(str), 63 : ptr_(str),
64 length_((str == NULL) ? 0 : STRING_TYPE::traits_type::length(str)) {} 64 length_((str == NULL) ? 0 : STRING_TYPE::traits_type::length(str)) {}
65 StringPieceDetail(const STRING_TYPE& str) 65 StringPieceDetail(const STRING_TYPE& str)
66 : ptr_(str.data()), 66 : ptr_(str.data()),
67 length_(str.size()) {} 67 length_(str.size()) {}
68 StringPieceDetail(const typename STRING_TYPE::const_iterator& begin,
69 const typename STRING_TYPE::const_iterator& end)
70 : ptr_(begin == end ? NULL : &*begin),
71 length_(end - begin) {}
72 StringPieceDetail(const typename STRING_TYPE::iterator& begin,
73 const typename STRING_TYPE::iterator& end)
74 : ptr_(begin == end ? NULL : &*begin),
75 length_(end - begin) {}
68 StringPieceDetail(const value_type* offset, size_type len) 76 StringPieceDetail(const value_type* offset, size_type len)
69 : ptr_(offset), 77 : ptr_(offset),
70 length_(len) {} 78 length_(len) {}
71 79
72 // data() may return a pointer to a buffer with embedded NULs, and the 80 // data() may return a pointer to a buffer with embedded NULs, and the
73 // returned buffer may or may not be null terminated. Therefore it is 81 // returned buffer may or may not be null terminated. Therefore it is
74 // typically a mistake to pass data() to a routine that expects a NUL 82 // typically a mistake to pass data() to a routine that expects a NUL
75 // terminated string. 83 // terminated string.
76 const value_type* data() const { return ptr_; } 84 const value_type* data() const { return ptr_; }
77 size_type size() const { return length_; } 85 size_type size() const { return length_; }
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 typedef typename internal::StringPieceDetail<STRING_TYPE>::value_type 169 typedef typename internal::StringPieceDetail<STRING_TYPE>::value_type
162 value_type; 170 value_type;
163 typedef typename internal::StringPieceDetail<STRING_TYPE>::size_type 171 typedef typename internal::StringPieceDetail<STRING_TYPE>::size_type
164 size_type; 172 size_type;
165 173
166 BasicStringPiece() {} 174 BasicStringPiece() {}
167 BasicStringPiece(const value_type*str) 175 BasicStringPiece(const value_type*str)
168 : internal::StringPieceDetail<STRING_TYPE>(str) {} 176 : internal::StringPieceDetail<STRING_TYPE>(str) {}
169 BasicStringPiece(const STRING_TYPE& str) 177 BasicStringPiece(const STRING_TYPE& str)
170 : internal::StringPieceDetail<STRING_TYPE>(str) {} 178 : internal::StringPieceDetail<STRING_TYPE>(str) {}
179 BasicStringPiece(const typename STRING_TYPE::const_iterator& begin,
180 const typename STRING_TYPE::const_iterator& end)
181 : internal::StringPieceDetail<STRING_TYPE>(begin, end) {}
182 BasicStringPiece(const typename STRING_TYPE::iterator& begin,
183 const typename STRING_TYPE::iterator& end)
184 : internal::StringPieceDetail<STRING_TYPE>(begin, end) {}
171 BasicStringPiece(const value_type* offset, size_type len) 185 BasicStringPiece(const value_type* offset, size_type len)
172 : internal::StringPieceDetail<STRING_TYPE>(offset, len) {} 186 : internal::StringPieceDetail<STRING_TYPE>(offset, len) {}
173 }; 187 };
174 188
175 // Specializes BasicStringPiece for std::string to add a few operations that 189 // Specializes BasicStringPiece for std::string to add a few operations that
176 // are not needed for string16. 190 // are not needed for string16.
177 template <> class BASE_EXPORT BasicStringPiece<std::string> : 191 template <> class BASE_EXPORT BasicStringPiece<std::string> :
178 public internal::StringPieceDetail<std::string> { 192 public internal::StringPieceDetail<std::string> {
179 public: 193 public:
180 BasicStringPiece() {} 194 BasicStringPiece() {}
181 BasicStringPiece(const char* str) 195 BasicStringPiece(const char* str)
182 : internal::StringPieceDetail<std::string>(str) {} 196 : internal::StringPieceDetail<std::string>(str) {}
183 BasicStringPiece(const std::string& str) 197 BasicStringPiece(const std::string& str)
184 : internal::StringPieceDetail<std::string>(str) {} 198 : internal::StringPieceDetail<std::string>(str) {}
199 BasicStringPiece(const std::string::const_iterator& begin,
200 const std::string::const_iterator& end)
201 : internal::StringPieceDetail<std::string>(begin, end) {}
202 BasicStringPiece(const std::string::iterator& begin,
203 const std::string::iterator& end)
204 : internal::StringPieceDetail<std::string>(begin, end) {}
185 BasicStringPiece(const char* offset, size_type len) 205 BasicStringPiece(const char* offset, size_type len)
186 : internal::StringPieceDetail<std::string>(offset, len) {} 206 : internal::StringPieceDetail<std::string>(offset, len) {}
187 207
188 // Prevent the following overload of set() from hiding the definitions in the 208 // Prevent the following overload of set() from hiding the definitions in the
189 // base class. 209 // base class.
190 using internal::StringPieceDetail<std::string>::set; 210 using internal::StringPieceDetail<std::string>::set;
191 211
192 void set(const void* data, size_type len) { 212 void set(const void* data, size_type len) {
193 ptr_ = reinterpret_cast<const value_type*>(data); 213 ptr_ = reinterpret_cast<const value_type*>(data);
194 length_ = len; 214 length_ = len;
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 } 354 }
335 inline size_t hash_value(const base::StringPiece16& sp16) { 355 inline size_t hash_value(const base::StringPiece16& sp16) {
336 HASH_STRING_PIECE(base::StringPiece16, sp16); 356 HASH_STRING_PIECE(base::StringPiece16, sp16);
337 } 357 }
338 358
339 #endif // COMPILER 359 #endif // COMPILER
340 360
341 } // namespace BASE_HASH_NAMESPACE 361 } // namespace BASE_HASH_NAMESPACE
342 362
343 #endif // BASE_STRING_PIECE_H_ 363 #endif // BASE_STRING_PIECE_H_
OLDNEW
« no previous file with comments | « no previous file | base/string_piece_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698