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 |
11 // to include this .h file in other files rather than forward-declaring | 11 // to include this .h file in other files rather than forward-declaring |
12 // StringPiece as would be appropriate for most other Google classes. | 12 // StringPiece as would be appropriate for most other Google classes. |
13 // | 13 // |
14 // Systematic usage of StringPiece is encouraged as it will reduce unnecessary | 14 // Systematic usage of StringPiece is encouraged as it will reduce unnecessary |
15 // conversions from "const char*" to "string" and back again. | 15 // conversions from "const char*" to "string" and back again. |
16 // | 16 // |
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. | |
21 // | |
17 | 22 |
18 #ifndef BASE_STRING_PIECE_H_ | 23 #ifndef BASE_STRING_PIECE_H_ |
19 #define BASE_STRING_PIECE_H_ | 24 #define BASE_STRING_PIECE_H_ |
20 #pragma once | 25 #pragma once |
21 | 26 |
22 #include <string> | 27 #include <string> |
23 | 28 |
24 #include "base/base_api.h" | 29 #include "base/base_api.h" |
25 #include "base/basictypes.h" | 30 #include "base/basictypes.h" |
31 #include "base/hash_tables.h" | |
32 #include "base/string16.h" | |
26 | 33 |
27 namespace base { | 34 namespace base { |
28 | 35 |
29 class BASE_API StringPiece { | 36 class BASE_API StringPiece { |
30 public: | 37 public: |
31 // standard STL container boilerplate | 38 // standard STL container boilerplate |
32 typedef size_t size_type; | 39 typedef size_t size_type; |
33 typedef char value_type; | 40 typedef char value_type; |
34 typedef const char* pointer; | 41 typedef const char* pointer; |
35 typedef const char& reference; | 42 typedef const char& reference; |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
157 | 164 |
158 static int wordmemcmp(const char* p, const char* p2, size_type N) { | 165 static int wordmemcmp(const char* p, const char* p2, size_type N) { |
159 return memcmp(p, p2, N); | 166 return memcmp(p, p2, N); |
160 } | 167 } |
161 | 168 |
162 private: | 169 private: |
163 const char* ptr_; | 170 const char* ptr_; |
164 size_type length_; | 171 size_type length_; |
165 }; | 172 }; |
166 | 173 |
174 class BASE_API StringPiece16 { | |
175 public: | |
176 // standard STL container boilerplate | |
177 typedef size_t size_type; | |
178 typedef char16 value_type; | |
179 typedef const char16* pointer; | |
180 typedef const char16& reference; | |
181 typedef const char16& const_reference; | |
182 typedef ptrdiff_t difference_type; | |
183 typedef const char16* const_iterator; | |
184 typedef const char16* iterator; | |
185 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; | |
186 typedef std::reverse_iterator<iterator> reverse_iterator; | |
187 | |
188 public: | |
189 // We provide non-explicit singleton constructors so users can pass | |
190 // in a "const char16*" or a "string16" wherever a "StringPiece16" is | |
191 // expected. | |
192 StringPiece16() : ptr_(NULL), length_(0) { } | |
193 StringPiece16(const char16* str) | |
194 : ptr_(str), | |
195 length_((str == NULL) ? 0 : string16::traits_type::length(str)) { } | |
196 StringPiece16(const string16& str) | |
197 : ptr_(str.data()), length_(str.size()) { } | |
198 StringPiece16(const char16* offset, size_type len) | |
199 : ptr_(offset), length_(len) { } | |
200 | |
201 // 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 | |
203 // typically a mistake to pass data() to a routine that expects a NUL | |
204 // terminated string. | |
205 const char16* data() const { return ptr_; } | |
206 size_type size() const { return length_; } | |
207 size_type length() const { return length_; } | |
208 bool empty() const { return length_ == 0; } | |
209 | |
210 void clear() { | |
211 ptr_ = NULL; | |
212 length_ = 0; | |
213 } | |
214 void set(const char16* data, size_type len) { | |
215 ptr_ = data; | |
216 length_ = len; | |
217 } | |
218 void set(const char16* str) { | |
219 ptr_ = str; | |
220 length_ = str ? string16::traits_type::length(str) : 0; | |
221 } | |
222 void set(const void* data, size_type len) { | |
223 ptr_ = reinterpret_cast<const char16*>(data); | |
224 length_ = len; | |
225 } | |
226 | |
227 char16 operator[](size_type i) const { return ptr_[i]; } | |
228 | |
229 string16 as_string16() const { | |
230 // StringPiece claims that this is bad when data() is NULL, but unittesting | |
231 // seems to say otherwise. | |
232 return string16(data(), size()); | |
233 } | |
234 | |
235 iterator begin() const { return ptr_; } | |
236 iterator end() const { return ptr_ + length_; } | |
237 const_reverse_iterator rbegin() const { | |
238 return const_reverse_iterator(ptr_ + length_); | |
239 } | |
240 const_reverse_iterator rend() const { | |
241 return const_reverse_iterator(ptr_); | |
242 } | |
243 | |
244 size_type max_size() const { return length_; } | |
245 size_type capacity() const { return length_; } | |
246 | |
247 static int wordmemcmp(const char16* p, const char16* p2, size_type N) { | |
248 return string16::traits_type::compare(p, p2, N); | |
249 } | |
250 | |
251 private: | |
252 const char16* ptr_; | |
253 size_type length_; | |
254 }; | |
255 | |
256 | |
167 BASE_API bool operator==(const StringPiece& x, const StringPiece& y); | 257 BASE_API bool operator==(const StringPiece& x, const StringPiece& y); |
168 | 258 |
169 inline bool operator!=(const StringPiece& x, const StringPiece& y) { | 259 inline bool operator!=(const StringPiece& x, const StringPiece& y) { |
170 return !(x == y); | 260 return !(x == y); |
171 } | 261 } |
172 | 262 |
173 inline bool operator<(const StringPiece& x, const StringPiece& y) { | 263 inline bool operator<(const StringPiece& x, const StringPiece& y) { |
174 const int r = StringPiece::wordmemcmp( | 264 const int r = StringPiece::wordmemcmp( |
175 x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size())); | 265 x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size())); |
176 return ((r < 0) || ((r == 0) && (x.size() < y.size()))); | 266 return ((r < 0) || ((r == 0) && (x.size() < y.size()))); |
177 } | 267 } |
178 | 268 |
179 inline bool operator>(const StringPiece& x, const StringPiece& y) { | 269 inline bool operator>(const StringPiece& x, const StringPiece& y) { |
180 return y < x; | 270 return y < x; |
181 } | 271 } |
182 | 272 |
183 inline bool operator<=(const StringPiece& x, const StringPiece& y) { | 273 inline bool operator<=(const StringPiece& x, const StringPiece& y) { |
184 return !(x > y); | 274 return !(x > y); |
185 } | 275 } |
186 | 276 |
187 inline bool operator>=(const StringPiece& x, const StringPiece& y) { | 277 inline bool operator>=(const StringPiece& x, const StringPiece& y) { |
188 return !(x < y); | 278 return !(x < y); |
189 } | 279 } |
190 | 280 |
281 inline bool operator==(const StringPiece16& x, const StringPiece16& y) { | |
282 if (x.size() != y.size()) | |
283 return false; | |
284 | |
285 return StringPiece16::wordmemcmp(x.data(), y.data(), x.size()) == 0; | |
286 } | |
287 | |
288 inline bool operator!=(const StringPiece16& x, const StringPiece16& y) { | |
289 return !(x == y); | |
290 } | |
291 | |
292 inline bool operator<(const StringPiece16& x, const StringPiece16& y) { | |
293 const int r = StringPiece16::wordmemcmp( | |
294 x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size())); | |
295 return ((r < 0) || ((r == 0) && (x.size() < y.size()))); | |
296 } | |
297 | |
298 inline bool operator>(const StringPiece16& x, const StringPiece16& y) { | |
299 return y < x; | |
300 } | |
301 | |
302 inline bool operator<=(const StringPiece16& x, const StringPiece16& y) { | |
303 return !(x > y); | |
304 } | |
305 | |
306 inline bool operator>=(const StringPiece16& x, const StringPiece16& y) { | |
307 return !(x < y); | |
308 } | |
309 | |
191 } // namespace base | 310 } // namespace base |
192 | 311 |
312 // We provide appropriate hash functions so StringPiece16 can be used as a | |
313 // key in hash sets and maps. | |
314 | |
315 // This hash function is copied from base/hash_tables.h. We don't use the | |
316 // already defined string16 directly because it would require the string16 | |
317 // constructor to be called, which we don't want. | |
318 #define HASH_WIDE_STRING_PIECE(wsp) \ | |
brettw
2011/08/05 23:00:23
It doesn't look like regular StringPiece has a has
Garrett Casto
2011/08/08 18:48:05
Done.
| |
319 std::size_t result = 0; \ | |
320 for (base::StringPiece16::const_iterator i = wsp.begin(); \ | |
brettw
2011/08/05 23:00:23
Fix whitespace.
Garrett Casto
2011/08/08 18:48:05
Done.
| |
321 i != wsp.end(); ++i) \ | |
322 result = (result * 131) + *i; \ | |
323 return result; \ | |
324 | |
325 namespace BASE_HASH_NAMESPACE { | |
326 #if defined(COMPILER_GCC) | |
327 | |
328 template<> | |
329 struct hash<base::StringPiece16> { | |
330 std::size_t operator()(const base::StringPiece16& wsp) const { | |
331 HASH_WIDE_STRING_PIECE(wsp); | |
332 } | |
333 }; | |
334 | |
335 #elif defined(COMPILER_MSVC) | |
336 | |
337 inline size_t hash_value(const base::StringPiece16& wsp) { | |
338 HASH_WIDE_STRING_PIECE(wsp); | |
339 } | |
340 | |
341 #endif // COMPILER | |
342 | |
343 } // namespace BASE_HASH_NAMESPACE | |
344 | |
193 #endif // BASE_STRING_PIECE_H_ | 345 #endif // BASE_STRING_PIECE_H_ |
OLD | NEW |