OLD | NEW |
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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 // We provide non-explicit singleton constructors so users can pass | 53 // We provide non-explicit singleton constructors so users can pass |
54 // in a "const char*" or a "string" wherever a "StringPiece" is | 54 // in a "const char*" or a "string" wherever a "StringPiece" is |
55 // expected. | 55 // expected. |
56 StringPiece() : ptr_(NULL), length_(0) { } | 56 StringPiece() : ptr_(NULL), length_(0) { } |
57 StringPiece(const char* str) | 57 StringPiece(const char* str) |
58 : ptr_(str), length_((str == NULL) ? 0 : strlen(str)) { } | 58 : ptr_(str), length_((str == NULL) ? 0 : strlen(str)) { } |
59 StringPiece(const std::string& str) | 59 StringPiece(const std::string& str) |
60 : ptr_(str.data()), length_(str.size()) { } | 60 : ptr_(str.data()), length_(str.size()) { } |
61 StringPiece(const char* offset, size_type len) | 61 StringPiece(const char* offset, size_type len) |
62 : ptr_(offset), length_(len) { } | 62 : ptr_(offset), length_(len) { } |
| 63 StringPiece(const std::string::const_iterator& begin, |
| 64 const std::string::const_iterator& end) |
| 65 : ptr_(&(*begin)), length_((size_type)(end - begin)) { } |
63 | 66 |
64 // data() may return a pointer to a buffer with embedded NULs, and the | 67 // data() may return a pointer to a buffer with embedded NULs, and the |
65 // returned buffer may or may not be null terminated. Therefore it is | 68 // returned buffer may or may not be null terminated. Therefore it is |
66 // typically a mistake to pass data() to a routine that expects a NUL | 69 // typically a mistake to pass data() to a routine that expects a NUL |
67 // terminated string. | 70 // terminated string. |
68 const char* data() const { return ptr_; } | 71 const char* data() const { return ptr_; } |
69 size_type size() const { return length_; } | 72 size_type size() const { return length_; } |
70 size_type length() const { return length_; } | 73 size_type length() const { return length_; } |
71 bool empty() const { return length_ == 0; } | 74 bool empty() const { return length_ == 0; } |
72 | 75 |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 // in a "const char16*" or a "string16" wherever a "StringPiece16" is | 193 // in a "const char16*" or a "string16" wherever a "StringPiece16" is |
191 // expected. | 194 // expected. |
192 StringPiece16() : ptr_(NULL), length_(0) { } | 195 StringPiece16() : ptr_(NULL), length_(0) { } |
193 StringPiece16(const char16* str) | 196 StringPiece16(const char16* str) |
194 : ptr_(str), | 197 : ptr_(str), |
195 length_((str == NULL) ? 0 : string16::traits_type::length(str)) { } | 198 length_((str == NULL) ? 0 : string16::traits_type::length(str)) { } |
196 StringPiece16(const string16& str) | 199 StringPiece16(const string16& str) |
197 : ptr_(str.data()), length_(str.size()) { } | 200 : ptr_(str.data()), length_(str.size()) { } |
198 StringPiece16(const char16* offset, size_type len) | 201 StringPiece16(const char16* offset, size_type len) |
199 : ptr_(offset), length_(len) { } | 202 : ptr_(offset), length_(len) { } |
| 203 StringPiece16(const string16::const_iterator& begin, |
| 204 const string16::const_iterator& end) |
| 205 : ptr_(&(*begin)), length_((size_type)(end - begin)) { } |
200 | 206 |
201 // data() may return a pointer to a buffer with embedded NULs, and the | 207 // data() may return a pointer to a buffer with embedded NULs, and the |
202 // returned buffer may or may not be null terminated. Therefore it is | 208 // returned buffer may or may not be null terminated. Therefore it is |
203 // typically a mistake to pass data() to a routine that expects a NUL | 209 // typically a mistake to pass data() to a routine that expects a NUL |
204 // terminated string. | 210 // terminated string. |
205 const char16* data() const { return ptr_; } | 211 const char16* data() const { return ptr_; } |
206 size_type size() const { return length_; } | 212 size_type size() const { return length_; } |
207 size_type length() const { return length_; } | 213 size_type length() const { return length_; } |
208 bool empty() const { return length_ == 0; } | 214 bool empty() const { return length_ == 0; } |
209 | 215 |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 } | 346 } |
341 inline size_t hash_value(const base::StringPiece16& sp16) { | 347 inline size_t hash_value(const base::StringPiece16& sp16) { |
342 HASH_STRING_PIECE(base::StringPiece16, sp16); | 348 HASH_STRING_PIECE(base::StringPiece16, sp16); |
343 } | 349 } |
344 | 350 |
345 #endif // COMPILER | 351 #endif // COMPILER |
346 | 352 |
347 } // namespace BASE_HASH_NAMESPACE | 353 } // namespace BASE_HASH_NAMESPACE |
348 | 354 |
349 #endif // BASE_STRING_PIECE_H_ | 355 #endif // BASE_STRING_PIECE_H_ |
OLD | NEW |