brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | // Copied from strings/stringpiece.h with modifications |
| 5 | // |
| 6 | // A string-like object that points to a sized piece of memory. |
| 7 | // |
rch@chromium.org | 16b200c | 2014-06-24 02:55:21 | [diff] [blame] | 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 |
| 10 | // char*" argument, a string argument, or a StringPiece argument with no data |
| 11 | // copying. Systematic use of StringPiece for arguments reduces data |
| 12 | // copies and strlen() calls. |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 13 | // |
rch@chromium.org | 16b200c | 2014-06-24 02:55:21 | [diff] [blame] | 14 | // Prefer passing StringPieces by value: |
| 15 | // void MyFunction(StringPiece arg); |
| 16 | // If circumstances require, you may also pass by const reference: |
| 17 | // void MyFunction(const StringPiece& arg); // not preferred |
| 18 | // Both of these have the same lifetime semantics. Passing by value |
| 19 | // generates slightly smaller code. For more discussion, Googlers can see |
| 20 | // the thread go/stringpiecebyvalue on c-users. |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 21 | |
| 22 | #ifndef BASE_STRINGS_STRING_PIECE_H_ |
| 23 | #define BASE_STRINGS_STRING_PIECE_H_ |
| 24 | |
| 25 | #include <stddef.h> |
| 26 | |
| 27 | #include <iosfwd> |
| 28 | #include <string> |
| 29 | |
| 30 | #include "base/base_export.h" |
olli.raula | 570930f | 2015-10-12 22:10:56 | [diff] [blame] | 31 | #include "base/logging.h" |
avi@chromium.org | c851cfd | 2013-06-10 20:11:14 | [diff] [blame] | 32 | #include "base/strings/string16.h" |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 33 | #include "base/strings/string_piece_forward.h" |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 34 | |
| 35 | namespace base { |
| 36 | |
brettw@chromium.org | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 37 | // internal -------------------------------------------------------------------- |
| 38 | |
| 39 | // Many of the StringPiece functions use different implementations for the |
| 40 | // 8-bit and 16-bit versions, and we don't want lots of template expansions in |
| 41 | // this (very common) header that will slow down compilation. |
| 42 | // |
| 43 | // So here we define overloaded functions called by the StringPiece template. |
| 44 | // For those that share an implementation, the two versions will expand to a |
| 45 | // template internal to the .cc file. |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 46 | namespace internal { |
| 47 | |
brettw@chromium.org | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 48 | BASE_EXPORT void CopyToString(const StringPiece& self, std::string* target); |
| 49 | BASE_EXPORT void CopyToString(const StringPiece16& self, string16* target); |
| 50 | |
| 51 | BASE_EXPORT void AppendToString(const StringPiece& self, std::string* target); |
| 52 | BASE_EXPORT void AppendToString(const StringPiece16& self, string16* target); |
| 53 | |
| 54 | BASE_EXPORT size_t copy(const StringPiece& self, |
| 55 | char* buf, |
| 56 | size_t n, |
| 57 | size_t pos); |
| 58 | BASE_EXPORT size_t copy(const StringPiece16& self, |
| 59 | char16* buf, |
| 60 | size_t n, |
| 61 | size_t pos); |
| 62 | |
| 63 | BASE_EXPORT size_t find(const StringPiece& self, |
| 64 | const StringPiece& s, |
| 65 | size_t pos); |
| 66 | BASE_EXPORT size_t find(const StringPiece16& self, |
| 67 | const StringPiece16& s, |
| 68 | size_t pos); |
| 69 | BASE_EXPORT size_t find(const StringPiece& self, |
| 70 | char c, |
| 71 | size_t pos); |
| 72 | BASE_EXPORT size_t find(const StringPiece16& self, |
| 73 | char16 c, |
| 74 | size_t pos); |
| 75 | |
| 76 | BASE_EXPORT size_t rfind(const StringPiece& self, |
| 77 | const StringPiece& s, |
| 78 | size_t pos); |
| 79 | BASE_EXPORT size_t rfind(const StringPiece16& self, |
| 80 | const StringPiece16& s, |
| 81 | size_t pos); |
| 82 | BASE_EXPORT size_t rfind(const StringPiece& self, |
| 83 | char c, |
| 84 | size_t pos); |
| 85 | BASE_EXPORT size_t rfind(const StringPiece16& self, |
| 86 | char16 c, |
| 87 | size_t pos); |
| 88 | |
| 89 | BASE_EXPORT size_t find_first_of(const StringPiece& self, |
| 90 | const StringPiece& s, |
| 91 | size_t pos); |
| 92 | BASE_EXPORT size_t find_first_of(const StringPiece16& self, |
| 93 | const StringPiece16& s, |
| 94 | size_t pos); |
| 95 | |
| 96 | BASE_EXPORT size_t find_first_not_of(const StringPiece& self, |
| 97 | const StringPiece& s, |
| 98 | size_t pos); |
| 99 | BASE_EXPORT size_t find_first_not_of(const StringPiece16& self, |
| 100 | const StringPiece16& s, |
| 101 | size_t pos); |
| 102 | BASE_EXPORT size_t find_first_not_of(const StringPiece& self, |
| 103 | char c, |
| 104 | size_t pos); |
| 105 | BASE_EXPORT size_t find_first_not_of(const StringPiece16& self, |
| 106 | char16 c, |
| 107 | size_t pos); |
| 108 | |
| 109 | BASE_EXPORT size_t find_last_of(const StringPiece& self, |
| 110 | const StringPiece& s, |
| 111 | size_t pos); |
| 112 | BASE_EXPORT size_t find_last_of(const StringPiece16& self, |
| 113 | const StringPiece16& s, |
| 114 | size_t pos); |
| 115 | BASE_EXPORT size_t find_last_of(const StringPiece& self, |
| 116 | char c, |
| 117 | size_t pos); |
| 118 | BASE_EXPORT size_t find_last_of(const StringPiece16& self, |
| 119 | char16 c, |
| 120 | size_t pos); |
| 121 | |
| 122 | BASE_EXPORT size_t find_last_not_of(const StringPiece& self, |
| 123 | const StringPiece& s, |
| 124 | size_t pos); |
| 125 | BASE_EXPORT size_t find_last_not_of(const StringPiece16& self, |
| 126 | const StringPiece16& s, |
| 127 | size_t pos); |
| 128 | BASE_EXPORT size_t find_last_not_of(const StringPiece16& self, |
| 129 | char16 c, |
| 130 | size_t pos); |
| 131 | BASE_EXPORT size_t find_last_not_of(const StringPiece& self, |
| 132 | char c, |
| 133 | size_t pos); |
| 134 | |
| 135 | BASE_EXPORT StringPiece substr(const StringPiece& self, |
| 136 | size_t pos, |
| 137 | size_t n); |
| 138 | BASE_EXPORT StringPiece16 substr(const StringPiece16& self, |
| 139 | size_t pos, |
| 140 | size_t n); |
| 141 | |
olli.raula | 570930f | 2015-10-12 22:10:56 | [diff] [blame] | 142 | #if DCHECK_IS_ON() |
brettw | f22866d | 2015-07-27 22:22:26 | [diff] [blame] | 143 | // Asserts that begin <= end to catch some errors with iterator usage. |
| 144 | BASE_EXPORT void AssertIteratorsInOrder(std::string::const_iterator begin, |
| 145 | std::string::const_iterator end); |
| 146 | BASE_EXPORT void AssertIteratorsInOrder(string16::const_iterator begin, |
| 147 | string16::const_iterator end); |
| 148 | #endif |
| 149 | |
brettw@chromium.org | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 150 | } // namespace internal |
| 151 | |
| 152 | // BasicStringPiece ------------------------------------------------------------ |
| 153 | |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 154 | // Defines the types, methods, operators, and data members common to both |
| 155 | // StringPiece and StringPiece16. Do not refer to this class directly, but |
| 156 | // rather to BasicStringPiece, StringPiece, or StringPiece16. |
brettw@chromium.org | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 157 | // |
| 158 | // This is templatized by string class type rather than character type, so |
| 159 | // BasicStringPiece<std::string> or BasicStringPiece<base::string16>. |
| 160 | template <typename STRING_TYPE> class BasicStringPiece { |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 161 | public: |
brettw@chromium.org | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 162 | // Standard STL container boilerplate. |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 163 | typedef size_t size_type; |
| 164 | typedef typename STRING_TYPE::value_type value_type; |
| 165 | typedef const value_type* pointer; |
| 166 | typedef const value_type& reference; |
| 167 | typedef const value_type& const_reference; |
| 168 | typedef ptrdiff_t difference_type; |
| 169 | typedef const value_type* const_iterator; |
| 170 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 171 | |
| 172 | static const size_type npos; |
| 173 | |
| 174 | public: |
| 175 | // We provide non-explicit singleton constructors so users can pass |
| 176 | // in a "const char*" or a "string" wherever a "StringPiece" is |
| 177 | // expected (likewise for char16, string16, StringPiece16). |
Jeremy Roman | 40a4dc1 | 2017-10-10 18:04:47 | [diff] [blame] | 178 | constexpr BasicStringPiece() : ptr_(NULL), length_(0) {} |
brettw@chromium.org | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 179 | BasicStringPiece(const value_type* str) |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 180 | : ptr_(str), |
| 181 | length_((str == NULL) ? 0 : STRING_TYPE::traits_type::length(str)) {} |
brettw@chromium.org | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 182 | BasicStringPiece(const STRING_TYPE& str) |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 183 | : ptr_(str.data()), length_(str.size()) {} |
Jeremy Roman | 40a4dc1 | 2017-10-10 18:04:47 | [diff] [blame] | 184 | constexpr BasicStringPiece(const value_type* offset, size_type len) |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 185 | : ptr_(offset), length_(len) {} |
brettw@chromium.org | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 186 | BasicStringPiece(const typename STRING_TYPE::const_iterator& begin, |
brettw | f22866d | 2015-07-27 22:22:26 | [diff] [blame] | 187 | const typename STRING_TYPE::const_iterator& end) { |
olli.raula | 570930f | 2015-10-12 22:10:56 | [diff] [blame] | 188 | #if DCHECK_IS_ON() |
brettw | f22866d | 2015-07-27 22:22:26 | [diff] [blame] | 189 | // This assertion is done out-of-line to avoid bringing in logging.h and |
| 190 | // instantiating logging macros for every instantiation. |
| 191 | internal::AssertIteratorsInOrder(begin, end); |
| 192 | #endif |
| 193 | length_ = static_cast<size_t>(std::distance(begin, end)); |
| 194 | |
| 195 | // The length test before assignment is to avoid dereferencing an iterator |
| 196 | // that may point to the end() of a string. |
| 197 | ptr_ = length_ > 0 ? &*begin : nullptr; |
| 198 | } |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 199 | |
| 200 | // data() may return a pointer to a buffer with embedded NULs, and the |
| 201 | // returned buffer may or may not be null terminated. Therefore it is |
| 202 | // typically a mistake to pass data() to a routine that expects a NUL |
| 203 | // terminated string. |
Jeremy Roman | 40a4dc1 | 2017-10-10 18:04:47 | [diff] [blame] | 204 | constexpr const value_type* data() const { return ptr_; } |
| 205 | constexpr size_type size() const { return length_; } |
| 206 | constexpr size_type length() const { return length_; } |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 207 | bool empty() const { return length_ == 0; } |
| 208 | |
| 209 | void clear() { |
| 210 | ptr_ = NULL; |
| 211 | length_ = 0; |
| 212 | } |
| 213 | void set(const value_type* data, size_type len) { |
| 214 | ptr_ = data; |
| 215 | length_ = len; |
| 216 | } |
| 217 | void set(const value_type* str) { |
| 218 | ptr_ = str; |
| 219 | length_ = str ? STRING_TYPE::traits_type::length(str) : 0; |
| 220 | } |
| 221 | |
Jeremy Roman | 40a4dc1 | 2017-10-10 18:04:47 | [diff] [blame] | 222 | constexpr value_type operator[](size_type i) const { return ptr_[i]; } |
pkasting | 9022cb4 | 2016-02-05 00:08:56 | [diff] [blame] | 223 | value_type front() const { return ptr_[0]; } |
| 224 | value_type back() const { return ptr_[length_ - 1]; } |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 225 | |
Jeremy Roman | 40a4dc1 | 2017-10-10 18:04:47 | [diff] [blame] | 226 | constexpr void remove_prefix(size_type n) { |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 227 | ptr_ += n; |
| 228 | length_ -= n; |
| 229 | } |
| 230 | |
Jeremy Roman | 40a4dc1 | 2017-10-10 18:04:47 | [diff] [blame] | 231 | constexpr void remove_suffix(size_type n) { length_ -= n; } |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 232 | |
| 233 | int compare(const BasicStringPiece<STRING_TYPE>& x) const { |
| 234 | int r = wordmemcmp( |
| 235 | ptr_, x.ptr_, (length_ < x.length_ ? length_ : x.length_)); |
| 236 | if (r == 0) { |
| 237 | if (length_ < x.length_) r = -1; |
| 238 | else if (length_ > x.length_) r = +1; |
| 239 | } |
| 240 | return r; |
| 241 | } |
| 242 | |
bnc | 222e8f3 | 2017-03-21 19:10:49 | [diff] [blame] | 243 | // This is the style of conversion preferred by std::string_view in C++17. |
| 244 | explicit operator STRING_TYPE() const { return as_string(); } |
| 245 | |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 246 | STRING_TYPE as_string() const { |
| 247 | // std::string doesn't like to take a NULL pointer even with a 0 size. |
| 248 | return empty() ? STRING_TYPE() : STRING_TYPE(data(), size()); |
| 249 | } |
| 250 | |
| 251 | const_iterator begin() const { return ptr_; } |
| 252 | const_iterator end() const { return ptr_ + length_; } |
| 253 | const_reverse_iterator rbegin() const { |
| 254 | return const_reverse_iterator(ptr_ + length_); |
| 255 | } |
| 256 | const_reverse_iterator rend() const { |
| 257 | return const_reverse_iterator(ptr_); |
| 258 | } |
| 259 | |
| 260 | size_type max_size() const { return length_; } |
| 261 | size_type capacity() const { return length_; } |
| 262 | |
| 263 | static int wordmemcmp(const value_type* p, |
| 264 | const value_type* p2, |
| 265 | size_type N) { |
| 266 | return STRING_TYPE::traits_type::compare(p, p2, N); |
| 267 | } |
| 268 | |
brettw@chromium.org | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 269 | // Sets the value of the given string target type to be the current string. |
| 270 | // This saves a temporary over doing |a = b.as_string()| |
| 271 | void CopyToString(STRING_TYPE* target) const { |
| 272 | internal::CopyToString(*this, target); |
| 273 | } |
| 274 | |
| 275 | void AppendToString(STRING_TYPE* target) const { |
| 276 | internal::AppendToString(*this, target); |
| 277 | } |
| 278 | |
| 279 | size_type copy(value_type* buf, size_type n, size_type pos = 0) const { |
| 280 | return internal::copy(*this, buf, n, pos); |
| 281 | } |
| 282 | |
| 283 | // Does "this" start with "x" |
| 284 | bool starts_with(const BasicStringPiece& x) const { |
| 285 | return ((this->length_ >= x.length_) && |
| 286 | (wordmemcmp(this->ptr_, x.ptr_, x.length_) == 0)); |
| 287 | } |
| 288 | |
| 289 | // Does "this" end with "x" |
| 290 | bool ends_with(const BasicStringPiece& x) const { |
| 291 | return ((this->length_ >= x.length_) && |
| 292 | (wordmemcmp(this->ptr_ + (this->length_-x.length_), |
| 293 | x.ptr_, x.length_) == 0)); |
| 294 | } |
| 295 | |
| 296 | // find: Search for a character or substring at a given offset. |
| 297 | size_type find(const BasicStringPiece<STRING_TYPE>& s, |
| 298 | size_type pos = 0) const { |
| 299 | return internal::find(*this, s, pos); |
| 300 | } |
| 301 | size_type find(value_type c, size_type pos = 0) const { |
| 302 | return internal::find(*this, c, pos); |
| 303 | } |
| 304 | |
| 305 | // rfind: Reverse find. |
| 306 | size_type rfind(const BasicStringPiece& s, |
| 307 | size_type pos = BasicStringPiece::npos) const { |
| 308 | return internal::rfind(*this, s, pos); |
| 309 | } |
| 310 | size_type rfind(value_type c, size_type pos = BasicStringPiece::npos) const { |
| 311 | return internal::rfind(*this, c, pos); |
| 312 | } |
| 313 | |
| 314 | // find_first_of: Find the first occurence of one of a set of characters. |
| 315 | size_type find_first_of(const BasicStringPiece& s, |
| 316 | size_type pos = 0) const { |
| 317 | return internal::find_first_of(*this, s, pos); |
| 318 | } |
| 319 | size_type find_first_of(value_type c, size_type pos = 0) const { |
| 320 | return find(c, pos); |
| 321 | } |
| 322 | |
| 323 | // find_first_not_of: Find the first occurence not of a set of characters. |
| 324 | size_type find_first_not_of(const BasicStringPiece& s, |
| 325 | size_type pos = 0) const { |
| 326 | return internal::find_first_not_of(*this, s, pos); |
| 327 | } |
| 328 | size_type find_first_not_of(value_type c, size_type pos = 0) const { |
| 329 | return internal::find_first_not_of(*this, c, pos); |
| 330 | } |
| 331 | |
| 332 | // find_last_of: Find the last occurence of one of a set of characters. |
| 333 | size_type find_last_of(const BasicStringPiece& s, |
| 334 | size_type pos = BasicStringPiece::npos) const { |
| 335 | return internal::find_last_of(*this, s, pos); |
| 336 | } |
| 337 | size_type find_last_of(value_type c, |
| 338 | size_type pos = BasicStringPiece::npos) const { |
| 339 | return rfind(c, pos); |
| 340 | } |
| 341 | |
| 342 | // find_last_not_of: Find the last occurence not of a set of characters. |
| 343 | size_type find_last_not_of(const BasicStringPiece& s, |
| 344 | size_type pos = BasicStringPiece::npos) const { |
| 345 | return internal::find_last_not_of(*this, s, pos); |
| 346 | } |
| 347 | size_type find_last_not_of(value_type c, |
| 348 | size_type pos = BasicStringPiece::npos) const { |
| 349 | return internal::find_last_not_of(*this, c, pos); |
| 350 | } |
| 351 | |
| 352 | // substr. |
| 353 | BasicStringPiece substr(size_type pos, |
| 354 | size_type n = BasicStringPiece::npos) const { |
| 355 | return internal::substr(*this, pos, n); |
| 356 | } |
| 357 | |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 358 | protected: |
| 359 | const value_type* ptr_; |
| 360 | size_type length_; |
| 361 | }; |
| 362 | |
| 363 | template <typename STRING_TYPE> |
brettw@chromium.org | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 364 | const typename BasicStringPiece<STRING_TYPE>::size_type |
| 365 | BasicStringPiece<STRING_TYPE>::npos = |
| 366 | typename BasicStringPiece<STRING_TYPE>::size_type(-1); |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 367 | |
| 368 | // MSVC doesn't like complex extern templates and DLLs. |
| 369 | #if !defined(COMPILER_MSVC) |
brettw@chromium.org | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 370 | extern template class BASE_EXPORT BasicStringPiece<std::string>; |
brettw@chromium.org | 50bc27c | 2014-03-07 01:45:03 | [diff] [blame] | 371 | extern template class BASE_EXPORT BasicStringPiece<string16>; |
| 372 | #endif |
brettw@chromium.org | 51cb94c | 2014-03-06 17:54:33 | [diff] [blame] | 373 | |
brettw@chromium.org | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 374 | // StingPiece operators -------------------------------------------------------- |
| 375 | |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 376 | BASE_EXPORT bool operator==(const StringPiece& x, const StringPiece& y); |
| 377 | |
| 378 | inline bool operator!=(const StringPiece& x, const StringPiece& y) { |
| 379 | return !(x == y); |
| 380 | } |
| 381 | |
| 382 | inline bool operator<(const StringPiece& x, const StringPiece& y) { |
| 383 | const int r = StringPiece::wordmemcmp( |
| 384 | x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size())); |
| 385 | return ((r < 0) || ((r == 0) && (x.size() < y.size()))); |
| 386 | } |
| 387 | |
| 388 | inline bool operator>(const StringPiece& x, const StringPiece& y) { |
| 389 | return y < x; |
| 390 | } |
| 391 | |
| 392 | inline bool operator<=(const StringPiece& x, const StringPiece& y) { |
| 393 | return !(x > y); |
| 394 | } |
| 395 | |
| 396 | inline bool operator>=(const StringPiece& x, const StringPiece& y) { |
| 397 | return !(x < y); |
| 398 | } |
| 399 | |
brettw@chromium.org | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 400 | // StringPiece16 operators ----------------------------------------------------- |
| 401 | |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 402 | inline bool operator==(const StringPiece16& x, const StringPiece16& y) { |
| 403 | if (x.size() != y.size()) |
| 404 | return false; |
| 405 | |
| 406 | return StringPiece16::wordmemcmp(x.data(), y.data(), x.size()) == 0; |
| 407 | } |
| 408 | |
| 409 | inline bool operator!=(const StringPiece16& x, const StringPiece16& y) { |
| 410 | return !(x == y); |
| 411 | } |
| 412 | |
| 413 | inline bool operator<(const StringPiece16& x, const StringPiece16& y) { |
| 414 | const int r = StringPiece16::wordmemcmp( |
| 415 | x.data(), y.data(), (x.size() < y.size() ? x.size() : y.size())); |
| 416 | return ((r < 0) || ((r == 0) && (x.size() < y.size()))); |
| 417 | } |
| 418 | |
| 419 | inline bool operator>(const StringPiece16& x, const StringPiece16& y) { |
| 420 | return y < x; |
| 421 | } |
| 422 | |
| 423 | inline bool operator<=(const StringPiece16& x, const StringPiece16& y) { |
| 424 | return !(x > y); |
| 425 | } |
| 426 | |
| 427 | inline bool operator>=(const StringPiece16& x, const StringPiece16& y) { |
| 428 | return !(x < y); |
| 429 | } |
| 430 | |
| 431 | BASE_EXPORT std::ostream& operator<<(std::ostream& o, |
| 432 | const StringPiece& piece); |
| 433 | |
brettw@chromium.org | 0ecab789 | 2014-03-11 21:15:40 | [diff] [blame] | 434 | // Hashing --------------------------------------------------------------------- |
| 435 | |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 436 | // We provide appropriate hash functions so StringPiece and StringPiece16 can |
| 437 | // be used as keys in hash sets and maps. |
| 438 | |
davidben | 411d3f7 | 2016-01-22 01:41:41 | [diff] [blame] | 439 | // This hash function is copied from base/strings/string16.h. We don't use the |
| 440 | // ones already defined for string and string16 directly because it would |
| 441 | // require the string constructors to be called, which we don't want. |
bnc | 63c01300 | 2016-02-06 03:58:14 | [diff] [blame] | 442 | #define HASH_STRING_PIECE(StringPieceType, string_piece) \ |
| 443 | std::size_t result = 0; \ |
| 444 | for (StringPieceType::const_iterator i = string_piece.begin(); \ |
| 445 | i != string_piece.end(); ++i) \ |
| 446 | result = (result * 131) + *i; \ |
| 447 | return result; |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 448 | |
bnc | 63c01300 | 2016-02-06 03:58:14 | [diff] [blame] | 449 | struct StringPieceHash { |
| 450 | std::size_t operator()(const StringPiece& sp) const { |
| 451 | HASH_STRING_PIECE(StringPiece, sp); |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 452 | } |
| 453 | }; |
bnc | 63c01300 | 2016-02-06 03:58:14 | [diff] [blame] | 454 | struct StringPiece16Hash { |
| 455 | std::size_t operator()(const StringPiece16& sp16) const { |
| 456 | HASH_STRING_PIECE(StringPiece16, sp16); |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 457 | } |
| 458 | }; |
| 459 | |
bnc | 63c01300 | 2016-02-06 03:58:14 | [diff] [blame] | 460 | } // namespace base |
brettw@chromium.org | 7d7b0ac | 2013-03-28 20:07:40 | [diff] [blame] | 461 | |
| 462 | #endif // BASE_STRINGS_STRING_PIECE_H_ |