Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | // Copied from strings/stringpiece.h with modifications |
michael@0 | 5 | // |
michael@0 | 6 | // A string-like object that points to a sized piece of memory. |
michael@0 | 7 | // |
michael@0 | 8 | // Functions or methods may use const StringPiece& parameters to accept either |
michael@0 | 9 | // a "const char*" or a "string" value that will be implicitly converted to |
michael@0 | 10 | // a StringPiece. The implicit conversion means that it is often appropriate |
michael@0 | 11 | // to include this .h file in other files rather than forward-declaring |
michael@0 | 12 | // StringPiece as would be appropriate for most other Google classes. |
michael@0 | 13 | // |
michael@0 | 14 | // Systematic usage of StringPiece is encouraged as it will reduce unnecessary |
michael@0 | 15 | // conversions from "const char*" to "string" and back again. |
michael@0 | 16 | // |
michael@0 | 17 | // StringPiece16 is similar to StringPiece but for base::string16 instead of |
michael@0 | 18 | // std::string. We do not define as large of a subset of the STL functions |
michael@0 | 19 | // from basic_string as in StringPiece, but this can be changed if these |
michael@0 | 20 | // functions (find, find_first_of, etc.) are found to be useful in this context. |
michael@0 | 21 | // |
michael@0 | 22 | |
michael@0 | 23 | #ifndef BASE_STRINGS_STRING_PIECE_H_ |
michael@0 | 24 | #define BASE_STRINGS_STRING_PIECE_H_ |
michael@0 | 25 | |
michael@0 | 26 | #include <stddef.h> |
michael@0 | 27 | |
michael@0 | 28 | #include <iosfwd> |
michael@0 | 29 | #include <string> |
michael@0 | 30 | |
michael@0 | 31 | #include "base/base_export.h" |
michael@0 | 32 | #include "base/basictypes.h" |
michael@0 | 33 | #include "base/containers/hash_tables.h" |
michael@0 | 34 | #include "base/strings/string16.h" |
michael@0 | 35 | |
michael@0 | 36 | namespace base { |
michael@0 | 37 | |
michael@0 | 38 | template <typename STRING_TYPE> class BasicStringPiece; |
michael@0 | 39 | typedef BasicStringPiece<std::string> StringPiece; |
michael@0 | 40 | typedef BasicStringPiece<string16> StringPiece16; |
michael@0 | 41 | |
michael@0 | 42 | namespace internal { |
michael@0 | 43 | |
michael@0 | 44 | // Defines the types, methods, operators, and data members common to both |
michael@0 | 45 | // StringPiece and StringPiece16. Do not refer to this class directly, but |
michael@0 | 46 | // rather to BasicStringPiece, StringPiece, or StringPiece16. |
michael@0 | 47 | template <typename STRING_TYPE> class StringPieceDetail { |
michael@0 | 48 | public: |
michael@0 | 49 | // standard STL container boilerplate |
michael@0 | 50 | typedef size_t size_type; |
michael@0 | 51 | typedef typename STRING_TYPE::value_type value_type; |
michael@0 | 52 | typedef const value_type* pointer; |
michael@0 | 53 | typedef const value_type& reference; |
michael@0 | 54 | typedef const value_type& const_reference; |
michael@0 | 55 | typedef ptrdiff_t difference_type; |
michael@0 | 56 | typedef const value_type* const_iterator; |
michael@0 | 57 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
michael@0 | 58 | |
michael@0 | 59 | static const size_type npos; |
michael@0 | 60 | |
michael@0 | 61 | public: |
michael@0 | 62 | // We provide non-explicit singleton constructors so users can pass |
michael@0 | 63 | // in a "const char*" or a "string" wherever a "StringPiece" is |
michael@0 | 64 | // expected (likewise for char16, string16, StringPiece16). |
michael@0 | 65 | StringPieceDetail() : ptr_(NULL), length_(0) {} |
michael@0 | 66 | StringPieceDetail(const value_type* str) |
michael@0 | 67 | : ptr_(str), |
michael@0 | 68 | length_((str == NULL) ? 0 : STRING_TYPE::traits_type::length(str)) {} |
michael@0 | 69 | StringPieceDetail(const STRING_TYPE& str) |
michael@0 | 70 | : ptr_(str.data()), length_(str.size()) {} |
michael@0 | 71 | StringPieceDetail(const value_type* offset, size_type len) |
michael@0 | 72 | : ptr_(offset), length_(len) {} |
michael@0 | 73 | StringPieceDetail(const typename STRING_TYPE::const_iterator& begin, |
michael@0 | 74 | const typename STRING_TYPE::const_iterator& end) |
michael@0 | 75 | : ptr_((end > begin) ? &(*begin) : NULL), |
michael@0 | 76 | length_((end > begin) ? (size_type)(end - begin) : 0) {} |
michael@0 | 77 | |
michael@0 | 78 | // data() may return a pointer to a buffer with embedded NULs, and the |
michael@0 | 79 | // returned buffer may or may not be null terminated. Therefore it is |
michael@0 | 80 | // typically a mistake to pass data() to a routine that expects a NUL |
michael@0 | 81 | // terminated string. |
michael@0 | 82 | const value_type* data() const { return ptr_; } |
michael@0 | 83 | size_type size() const { return length_; } |
michael@0 | 84 | size_type length() const { return length_; } |
michael@0 | 85 | bool empty() const { return length_ == 0; } |
michael@0 | 86 | |
michael@0 | 87 | void clear() { |
michael@0 | 88 | ptr_ = NULL; |
michael@0 | 89 | length_ = 0; |
michael@0 | 90 | } |
michael@0 | 91 | void set(const value_type* data, size_type len) { |
michael@0 | 92 | ptr_ = data; |
michael@0 | 93 | length_ = len; |
michael@0 | 94 | } |
michael@0 | 95 | void set(const value_type* str) { |
michael@0 | 96 | ptr_ = str; |
michael@0 | 97 | length_ = str ? STRING_TYPE::traits_type::length(str) : 0; |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | value_type operator[](size_type i) const { return ptr_[i]; } |
michael@0 | 101 | |
michael@0 | 102 | void remove_prefix(size_type n) { |
michael@0 | 103 | ptr_ += n; |
michael@0 | 104 | length_ -= n; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | void remove_suffix(size_type n) { |
michael@0 | 108 | length_ -= n; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | int compare(const BasicStringPiece<STRING_TYPE>& x) const { |
michael@0 | 112 | int r = wordmemcmp( |
michael@0 | 113 | ptr_, x.ptr_, (length_ < x.length_ ? length_ : x.length_)); |
michael@0 | 114 | if (r == 0) { |
michael@0 | 115 | if (length_ < x.length_) r = -1; |
michael@0 | 116 | else if (length_ > x.length_) r = +1; |
michael@0 | 117 | } |
michael@0 | 118 | return r; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | STRING_TYPE as_string() const { |
michael@0 | 122 | // std::string doesn't like to take a NULL pointer even with a 0 size. |
michael@0 | 123 | return empty() ? STRING_TYPE() : STRING_TYPE(data(), size()); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | const_iterator begin() const { return ptr_; } |
michael@0 | 127 | const_iterator end() const { return ptr_ + length_; } |
michael@0 | 128 | const_reverse_iterator rbegin() const { |
michael@0 | 129 | return const_reverse_iterator(ptr_ + length_); |
michael@0 | 130 | } |
michael@0 | 131 | const_reverse_iterator rend() const { |
michael@0 | 132 | return const_reverse_iterator(ptr_); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | size_type max_size() const { return length_; } |
michael@0 | 136 | size_type capacity() const { return length_; } |
michael@0 | 137 | |
michael@0 | 138 | static int wordmemcmp(const value_type* p, |
michael@0 | 139 | const value_type* p2, |
michael@0 | 140 | size_type N) { |
michael@0 | 141 | return STRING_TYPE::traits_type::compare(p, p2, N); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | protected: |
michael@0 | 145 | const value_type* ptr_; |
michael@0 | 146 | size_type length_; |
michael@0 | 147 | }; |
michael@0 | 148 | |
michael@0 | 149 | template <typename STRING_TYPE> |
michael@0 | 150 | const typename StringPieceDetail<STRING_TYPE>::size_type |
michael@0 | 151 | StringPieceDetail<STRING_TYPE>::npos = |
michael@0 | 152 | typename StringPieceDetail<STRING_TYPE>::size_type(-1); |
michael@0 | 153 | |
michael@0 | 154 | // MSVC doesn't like complex extern templates and DLLs. |
michael@0 | 155 | #if !defined(COMPILER_MSVC) |
michael@0 | 156 | extern template class BASE_EXPORT StringPieceDetail<std::string>; |
michael@0 | 157 | extern template class BASE_EXPORT StringPieceDetail<string16>; |
michael@0 | 158 | #endif |
michael@0 | 159 | |
michael@0 | 160 | BASE_EXPORT void CopyToString(const StringPiece& self, std::string* target); |
michael@0 | 161 | BASE_EXPORT void AppendToString(const StringPiece& self, std::string* target); |
michael@0 | 162 | BASE_EXPORT StringPieceDetail<std::string>::size_type copy( |
michael@0 | 163 | const StringPiece& self, |
michael@0 | 164 | char* buf, |
michael@0 | 165 | StringPieceDetail<std::string>::size_type n, |
michael@0 | 166 | StringPieceDetail<std::string>::size_type pos); |
michael@0 | 167 | BASE_EXPORT StringPieceDetail<std::string>::size_type find( |
michael@0 | 168 | const StringPiece& self, |
michael@0 | 169 | const StringPiece& s, |
michael@0 | 170 | StringPieceDetail<std::string>::size_type pos); |
michael@0 | 171 | BASE_EXPORT StringPieceDetail<std::string>::size_type find( |
michael@0 | 172 | const StringPiece& self, |
michael@0 | 173 | char c, |
michael@0 | 174 | StringPieceDetail<std::string>::size_type pos); |
michael@0 | 175 | BASE_EXPORT StringPieceDetail<std::string>::size_type rfind( |
michael@0 | 176 | const StringPiece& self, |
michael@0 | 177 | const StringPiece& s, |
michael@0 | 178 | StringPieceDetail<std::string>::size_type pos); |
michael@0 | 179 | BASE_EXPORT StringPieceDetail<std::string>::size_type rfind( |
michael@0 | 180 | const StringPiece& self, |
michael@0 | 181 | char c, |
michael@0 | 182 | StringPieceDetail<std::string>::size_type pos); |
michael@0 | 183 | BASE_EXPORT StringPieceDetail<std::string>::size_type find_first_of( |
michael@0 | 184 | const StringPiece& self, |
michael@0 | 185 | const StringPiece& s, |
michael@0 | 186 | StringPieceDetail<std::string>::size_type pos); |
michael@0 | 187 | BASE_EXPORT StringPieceDetail<std::string>::size_type find_first_not_of( |
michael@0 | 188 | const StringPiece& self, |
michael@0 | 189 | const StringPiece& s, |
michael@0 | 190 | StringPieceDetail<std::string>::size_type pos); |
michael@0 | 191 | BASE_EXPORT StringPieceDetail<std::string>::size_type find_first_not_of( |
michael@0 | 192 | const StringPiece& self, |
michael@0 | 193 | char c, |
michael@0 | 194 | StringPieceDetail<std::string>::size_type pos); |
michael@0 | 195 | BASE_EXPORT StringPieceDetail<std::string>::size_type find_last_of( |
michael@0 | 196 | const StringPiece& self, |
michael@0 | 197 | const StringPiece& s, |
michael@0 | 198 | StringPieceDetail<std::string>::size_type pos); |
michael@0 | 199 | BASE_EXPORT StringPieceDetail<std::string>::size_type find_last_of( |
michael@0 | 200 | const StringPiece& self, |
michael@0 | 201 | char c, |
michael@0 | 202 | StringPieceDetail<std::string>::size_type pos); |
michael@0 | 203 | BASE_EXPORT StringPieceDetail<std::string>::size_type find_last_not_of( |
michael@0 | 204 | const StringPiece& self, |
michael@0 | 205 | const StringPiece& s, |
michael@0 | 206 | StringPieceDetail<std::string>::size_type pos); |
michael@0 | 207 | BASE_EXPORT StringPieceDetail<std::string>::size_type find_last_not_of( |
michael@0 | 208 | const StringPiece& self, |
michael@0 | 209 | char c, |
michael@0 | 210 | StringPieceDetail<std::string>::size_type pos); |
michael@0 | 211 | BASE_EXPORT StringPiece substr(const StringPiece& self, |
michael@0 | 212 | StringPieceDetail<std::string>::size_type pos, |
michael@0 | 213 | StringPieceDetail<std::string>::size_type n); |
michael@0 | 214 | } // namespace internal |
michael@0 | 215 | |
michael@0 | 216 | // Defines the template type that is instantiated as either StringPiece or |
michael@0 | 217 | // StringPiece16. |
michael@0 | 218 | template <typename STRING_TYPE> class BasicStringPiece : |
michael@0 | 219 | public internal::StringPieceDetail<STRING_TYPE> { |
michael@0 | 220 | public: |
michael@0 | 221 | typedef typename internal::StringPieceDetail<STRING_TYPE>::value_type |
michael@0 | 222 | value_type; |
michael@0 | 223 | typedef typename internal::StringPieceDetail<STRING_TYPE>::size_type |
michael@0 | 224 | size_type; |
michael@0 | 225 | |
michael@0 | 226 | BasicStringPiece() {} |
michael@0 | 227 | BasicStringPiece(const value_type*str) |
michael@0 | 228 | : internal::StringPieceDetail<STRING_TYPE>(str) {} |
michael@0 | 229 | BasicStringPiece(const STRING_TYPE& str) |
michael@0 | 230 | : internal::StringPieceDetail<STRING_TYPE>(str) {} |
michael@0 | 231 | BasicStringPiece(const value_type* offset, size_type len) |
michael@0 | 232 | : internal::StringPieceDetail<STRING_TYPE>(offset, len) {} |
michael@0 | 233 | BasicStringPiece(const typename STRING_TYPE::const_iterator& begin, |
michael@0 | 234 | const typename STRING_TYPE::const_iterator& end) |
michael@0 | 235 | : internal::StringPieceDetail<STRING_TYPE>(begin, end) {} |
michael@0 | 236 | }; |
michael@0 | 237 | |
michael@0 | 238 | // Specializes BasicStringPiece for std::string to add a few operations that |
michael@0 | 239 | // are not needed for string16. |
michael@0 | 240 | template <> class BasicStringPiece<std::string> : |
michael@0 | 241 | public internal::StringPieceDetail<std::string> { |
michael@0 | 242 | public: |
michael@0 | 243 | BasicStringPiece() {} |
michael@0 | 244 | BasicStringPiece(const char* str) |
michael@0 | 245 | : internal::StringPieceDetail<std::string>(str) {} |
michael@0 | 246 | BasicStringPiece(const std::string& str) |
michael@0 | 247 | : internal::StringPieceDetail<std::string>(str) {} |
michael@0 | 248 | BasicStringPiece(const char* offset, size_type len) |
michael@0 | 249 | : internal::StringPieceDetail<std::string>(offset, len) {} |
michael@0 | 250 | BasicStringPiece(const std::string::const_iterator& begin, |
michael@0 | 251 | const std::string::const_iterator& end) |
michael@0 | 252 | : internal::StringPieceDetail<std::string>(begin, end) {} |
michael@0 | 253 | |
michael@0 | 254 | // Prevent the following overload of set() from hiding the definitions in the |
michael@0 | 255 | // base class. |
michael@0 | 256 | using internal::StringPieceDetail<std::string>::set; |
michael@0 | 257 | |
michael@0 | 258 | void set(const void* data, size_type len) { |
michael@0 | 259 | ptr_ = reinterpret_cast<const value_type*>(data); |
michael@0 | 260 | length_ = len; |
michael@0 | 261 | } |
michael@0 | 262 | |
michael@0 | 263 | void CopyToString(std::string* target) const { |
michael@0 | 264 | internal::CopyToString(*this, target); |
michael@0 | 265 | } |
michael@0 | 266 | |
michael@0 | 267 | void AppendToString(std::string* target) const { |
michael@0 | 268 | internal::AppendToString(*this, target); |
michael@0 | 269 | } |
michael@0 | 270 | |
michael@0 | 271 | // Does "this" start with "x" |
michael@0 | 272 | bool starts_with(const BasicStringPiece& x) const { |
michael@0 | 273 | return ((length_ >= x.length_) && |
michael@0 | 274 | (wordmemcmp(ptr_, x.ptr_, x.length_) == 0)); |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | // Does "this" end with "x" |
michael@0 | 278 | bool ends_with(const BasicStringPiece& x) const { |
michael@0 | 279 | return ((length_ >= x.length_) && |
michael@0 | 280 | (wordmemcmp(ptr_ + (length_-x.length_), x.ptr_, x.length_) == 0)); |
michael@0 | 281 | } |
michael@0 | 282 | |
michael@0 | 283 | size_type copy(char* buf, size_type n, size_type pos = 0) const { |
michael@0 | 284 | return internal::copy(*this, buf, n, pos); |
michael@0 | 285 | } |
michael@0 | 286 | |
michael@0 | 287 | size_type find(const BasicStringPiece& s, size_type pos = 0) const { |
michael@0 | 288 | return internal::find(*this, s, pos); |
michael@0 | 289 | } |
michael@0 | 290 | |
michael@0 | 291 | size_type find(char c, size_type pos = 0) const { |
michael@0 | 292 | return internal::find(*this, c, pos); |
michael@0 | 293 | } |
michael@0 | 294 | |
michael@0 | 295 | size_type rfind(const BasicStringPiece& s, size_type pos = npos) const { |
michael@0 | 296 | return internal::rfind(*this, s, pos); |
michael@0 | 297 | } |
michael@0 | 298 | |
michael@0 | 299 | size_type rfind(char c, size_type pos = npos) const { |
michael@0 | 300 | return internal::rfind(*this, c, pos); |
michael@0 | 301 | } |
michael@0 | 302 | |
michael@0 | 303 | size_type find_first_of(const BasicStringPiece& s, size_type pos = 0) const { |
michael@0 | 304 | return internal::find_first_of(*this, s, pos); |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | size_type find_first_of(char c, size_type pos = 0) const { |
michael@0 | 308 | return find(c, pos); |
michael@0 | 309 | } |
michael@0 | 310 | |
michael@0 | 311 | size_type find_first_not_of(const BasicStringPiece& s, |
michael@0 | 312 | size_type pos = 0) const { |
michael@0 | 313 | return internal::find_first_not_of(*this, s, pos); |
michael@0 | 314 | } |
michael@0 | 315 | |
michael@0 | 316 | size_type find_first_not_of(char c, size_type pos = 0) const { |
michael@0 | 317 | return internal::find_first_not_of(*this, c, pos); |
michael@0 | 318 | } |
michael@0 | 319 | |
michael@0 | 320 | size_type find_last_of(const BasicStringPiece& s, |
michael@0 | 321 | size_type pos = npos) const { |
michael@0 | 322 | return internal::find_last_of(*this, s, pos); |
michael@0 | 323 | } |
michael@0 | 324 | |
michael@0 | 325 | size_type find_last_of(char c, size_type pos = npos) const { |
michael@0 | 326 | return rfind(c, pos); |
michael@0 | 327 | } |
michael@0 | 328 | |
michael@0 | 329 | size_type find_last_not_of(const BasicStringPiece& s, |
michael@0 | 330 | size_type pos = npos) const { |
michael@0 | 331 | return internal::find_last_not_of(*this, s, pos); |
michael@0 | 332 | } |
michael@0 | 333 | |
michael@0 | 334 | size_type find_last_not_of(char c, size_type pos = npos) const { |
michael@0 | 335 | return internal::find_last_not_of(*this, c, pos); |
michael@0 | 336 | } |
michael@0 | 337 | |
michael@0 | 338 | BasicStringPiece substr(size_type pos, size_type n = npos) const { |
michael@0 | 339 | return internal::substr(*this, pos, n); |
michael@0 | 340 | } |
michael@0 | 341 | }; |
michael@0 | 342 | |
michael@0 | 343 | // MSVC doesn't like complex extern templates and DLLs. |
michael@0 | 344 | #if !defined(COMPILER_MSVC) |
michael@0 | 345 | // We can't explicitly declare the std::string instantiation here because it was |
michael@0 | 346 | // already instantiated when specialized, above. Not only is it a no-op, but |
michael@0 | 347 | // currently it also crashes Clang (see http://crbug.com/107412). |
michael@0 | 348 | extern template class BASE_EXPORT BasicStringPiece<string16>; |
michael@0 | 349 | #endif |
michael@0 | 350 | |
michael@0 | 351 | BASE_EXPORT bool operator==(const StringPiece& x, const StringPiece& y); |
michael@0 | 352 | |
michael@0 | 353 | inline bool operator!=(const StringPiece& x, const StringPiece& y) { |
michael@0 | 354 | return !(x == y); |
michael@0 | 355 | } |
michael@0 | 356 | |
michael@0 | 357 | inline bool operator<(const StringPiece& x, const StringPiece& y) { |
michael@0 | 358 | const int r = StringPiece::wordmemcmp( |
michael@0 | 359 | x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size())); |
michael@0 | 360 | return ((r < 0) || ((r == 0) && (x.size() < y.size()))); |
michael@0 | 361 | } |
michael@0 | 362 | |
michael@0 | 363 | inline bool operator>(const StringPiece& x, const StringPiece& y) { |
michael@0 | 364 | return y < x; |
michael@0 | 365 | } |
michael@0 | 366 | |
michael@0 | 367 | inline bool operator<=(const StringPiece& x, const StringPiece& y) { |
michael@0 | 368 | return !(x > y); |
michael@0 | 369 | } |
michael@0 | 370 | |
michael@0 | 371 | inline bool operator>=(const StringPiece& x, const StringPiece& y) { |
michael@0 | 372 | return !(x < y); |
michael@0 | 373 | } |
michael@0 | 374 | |
michael@0 | 375 | inline bool operator==(const StringPiece16& x, const StringPiece16& y) { |
michael@0 | 376 | if (x.size() != y.size()) |
michael@0 | 377 | return false; |
michael@0 | 378 | |
michael@0 | 379 | return StringPiece16::wordmemcmp(x.data(), y.data(), x.size()) == 0; |
michael@0 | 380 | } |
michael@0 | 381 | |
michael@0 | 382 | inline bool operator!=(const StringPiece16& x, const StringPiece16& y) { |
michael@0 | 383 | return !(x == y); |
michael@0 | 384 | } |
michael@0 | 385 | |
michael@0 | 386 | inline bool operator<(const StringPiece16& x, const StringPiece16& y) { |
michael@0 | 387 | const int r = StringPiece16::wordmemcmp( |
michael@0 | 388 | x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size())); |
michael@0 | 389 | return ((r < 0) || ((r == 0) && (x.size() < y.size()))); |
michael@0 | 390 | } |
michael@0 | 391 | |
michael@0 | 392 | inline bool operator>(const StringPiece16& x, const StringPiece16& y) { |
michael@0 | 393 | return y < x; |
michael@0 | 394 | } |
michael@0 | 395 | |
michael@0 | 396 | inline bool operator<=(const StringPiece16& x, const StringPiece16& y) { |
michael@0 | 397 | return !(x > y); |
michael@0 | 398 | } |
michael@0 | 399 | |
michael@0 | 400 | inline bool operator>=(const StringPiece16& x, const StringPiece16& y) { |
michael@0 | 401 | return !(x < y); |
michael@0 | 402 | } |
michael@0 | 403 | |
michael@0 | 404 | BASE_EXPORT std::ostream& operator<<(std::ostream& o, |
michael@0 | 405 | const StringPiece& piece); |
michael@0 | 406 | |
michael@0 | 407 | } // namespace base |
michael@0 | 408 | |
michael@0 | 409 | // We provide appropriate hash functions so StringPiece and StringPiece16 can |
michael@0 | 410 | // be used as keys in hash sets and maps. |
michael@0 | 411 | |
michael@0 | 412 | // This hash function is copied from base/containers/hash_tables.h. We don't |
michael@0 | 413 | // use the ones already defined for string and string16 directly because it |
michael@0 | 414 | // would require the string constructors to be called, which we don't want. |
michael@0 | 415 | #define HASH_STRING_PIECE(StringPieceType, string_piece) \ |
michael@0 | 416 | std::size_t result = 0; \ |
michael@0 | 417 | for (StringPieceType::const_iterator i = string_piece.begin(); \ |
michael@0 | 418 | i != string_piece.end(); ++i) \ |
michael@0 | 419 | result = (result * 131) + *i; \ |
michael@0 | 420 | return result; \ |
michael@0 | 421 | |
michael@0 | 422 | namespace BASE_HASH_NAMESPACE { |
michael@0 | 423 | #if defined(COMPILER_GCC) |
michael@0 | 424 | |
michael@0 | 425 | template<> |
michael@0 | 426 | struct hash<base::StringPiece> { |
michael@0 | 427 | std::size_t operator()(const base::StringPiece& sp) const { |
michael@0 | 428 | HASH_STRING_PIECE(base::StringPiece, sp); |
michael@0 | 429 | } |
michael@0 | 430 | }; |
michael@0 | 431 | template<> |
michael@0 | 432 | struct hash<base::StringPiece16> { |
michael@0 | 433 | std::size_t operator()(const base::StringPiece16& sp16) const { |
michael@0 | 434 | HASH_STRING_PIECE(base::StringPiece16, sp16); |
michael@0 | 435 | } |
michael@0 | 436 | }; |
michael@0 | 437 | |
michael@0 | 438 | #elif defined(COMPILER_MSVC) |
michael@0 | 439 | |
michael@0 | 440 | inline size_t hash_value(const base::StringPiece& sp) { |
michael@0 | 441 | HASH_STRING_PIECE(base::StringPiece, sp); |
michael@0 | 442 | } |
michael@0 | 443 | inline size_t hash_value(const base::StringPiece16& sp16) { |
michael@0 | 444 | HASH_STRING_PIECE(base::StringPiece16, sp16); |
michael@0 | 445 | } |
michael@0 | 446 | |
michael@0 | 447 | #endif // COMPILER |
michael@0 | 448 | |
michael@0 | 449 | } // namespace BASE_HASH_NAMESPACE |
michael@0 | 450 | |
michael@0 | 451 | #endif // BASE_STRINGS_STRING_PIECE_H_ |