| 6 | // A string-like object that points to a sized piece of memory. |
| 7 | // |
| 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. |
| 13 | // |
| 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. |
| 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 | #include <type_traits> |
| 30 |
| 31 | #include "base/base_export.h" |
| 32 | #include "base/logging.h" |
| 33 | #include "base/strings/char_traits.h" |
| 34 | #include "base/strings/string16.h" |
| 35 | #include "base/strings/string_piece_forward.h" |
| 36 |
| 37 | namespace base { |
| 38 |
| 39 | // internal -------------------------------------------------------------------- |
| 40 |
| 41 | // Many of the StringPiece functions use different implementations for the |
| 42 | // 8-bit and 16-bit versions, and we don't want lots of template expansions in |
| 43 | // this (very common) header that will slow down compilation. |
| 44 | // |
| 45 | // So here we define overloaded functions called by the StringPiece template. |
| 46 | // For those that share an implementation, the two versions will expand to a |
| 47 | // template internal to the .cc file. |
| 48 | namespace internal { |
| 49 |
| 50 | BASE_EXPORT size_t copy(const StringPiece& self, |
| 51 | char* buf, |
| 52 | size_t n, |
| 53 | size_t pos); |
| 54 | BASE_EXPORT size_t copy(const StringPiece16& self, |
| 55 | char16* buf, |
| 56 | size_t n, |
| 57 | size_t pos); |
| 58 |
| 59 | BASE_EXPORT size_t find(const StringPiece& self, |
| 60 | const StringPiece& s, |
| 61 | size_t pos); |
| 62 | BASE_EXPORT size_t find(const StringPiece16& self, |
| 63 | const StringPiece16& s, |
| 64 | size_t pos); |
| 65 | BASE_EXPORT size_t find(const StringPiece& self, |
| 66 | char c, |
| 67 | size_t pos); |
| 68 | BASE_EXPORT size_t find(const StringPiece16& self, |
| 69 | char16 c, |
| 70 | size_t pos); |
| 71 |
| 72 | BASE_EXPORT size_t rfind(const StringPiece& self, |
| 73 | const StringPiece& s, |
| 74 | size_t pos); |
| 75 | BASE_EXPORT size_t rfind(const StringPiece16& self, |
| 76 | const StringPiece16& s, |
| 77 | size_t pos); |
| 78 | BASE_EXPORT size_t rfind(const StringPiece& self, |
| 79 | char c, |
| 80 | size_t pos); |
| 81 | BASE_EXPORT size_t rfind(const StringPiece16& self, |
| 82 | char16 c, |
| 83 | size_t pos); |
| 84 |
| 85 | BASE_EXPORT size_t find_first_of(const StringPiece& self, |
| 86 | const StringPiece& s, |
| 87 | size_t pos); |
| 88 | BASE_EXPORT size_t find_first_of(const StringPiece16& self, |
| 89 | const StringPiece16& s, |
| 90 | size_t pos); |
| 91 |
| 92 | BASE_EXPORT size_t find_first_not_of(const StringPiece& self, |
| 93 | const StringPiece& s, |
| 94 | size_t pos); |
| 95 | BASE_EXPORT size_t find_first_not_of(const StringPiece16& self, |
| 96 | const StringPiece16& s, |
| 97 | size_t pos); |
| 98 | BASE_EXPORT size_t find_first_not_of(const StringPiece& self, |
| 99 | char c, |
| 100 | size_t pos); |
| 101 | BASE_EXPORT size_t find_first_not_of(const StringPiece16& self, |
| 102 | char16 c, |
| 103 | size_t pos); |
| 104 |
| 105 | BASE_EXPORT size_t find_last_of(const StringPiece& self, |
| 106 | const StringPiece& s, |
| 107 | size_t pos); |
| 108 | BASE_EXPORT size_t find_last_of(const StringPiece16& self, |
| 109 | const StringPiece16& s, |
| 110 | size_t pos); |
| 111 | BASE_EXPORT size_t find_last_of(const StringPiece& self, |
| 112 | char c, |
| 113 | size_t pos); |
| 114 | BASE_EXPORT size_t find_last_of(const StringPiece16& self, |
| 115 | char16 c, |
| 116 | size_t pos); |
| 117 |
| 118 | BASE_EXPORT size_t find_last_not_of(const StringPiece& self, |
| 119 | const StringPiece& s, |
| 120 | size_t pos); |
| 121 | BASE_EXPORT size_t find_last_not_of(const StringPiece16& self, |
| 122 | const StringPiece16& s, |
| 123 | size_t pos); |
| 124 | BASE_EXPORT size_t find_last_not_of(const StringPiece16& self, |
| 125 | char16 c, |
| 126 | size_t pos); |
| 127 | BASE_EXPORT size_t find_last_not_of(const StringPiece& self, |
| 128 | char c, |
| 129 | size_t pos); |
| 130 |
| 131 | BASE_EXPORT StringPiece substr(const StringPiece& self, |
| 132 | size_t pos, |
| 133 | size_t n); |
| 134 | BASE_EXPORT StringPiece16 substr(const StringPiece16& self, |
| 135 | size_t pos, |
| 136 | size_t n); |
| 137 |
| 138 | } // namespace internal |
| 139 |
| 140 | // BasicStringPiece ------------------------------------------------------------ |
| 141 |
| 142 | // Defines the types, methods, operators, and data members common to both |
| 143 | // StringPiece and StringPiece16. |
| 144 | // |
| 145 | // This is templatized by string class type rather than character type, so |
| 146 | // BasicStringPiece<std::string> or BasicStringPiece<base::string16>. |
| 147 | template <typename STRING_TYPE> class BasicStringPiece { |
| 148 | public: |
| 149 | // Standard STL container boilerplate. |
| 150 | typedef size_t size_type; |
| 151 | typedef typename STRING_TYPE::value_type value_type; |
| 152 | typedef const value_type* pointer; |
| 153 | typedef const value_type& reference; |
| 154 | typedef const value_type& const_reference; |
| 155 | typedef ptrdiff_t difference_type; |
| 156 | typedef const value_type* const_iterator; |
| 157 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 158 |
| 159 | static const size_type npos; |
| 160 |
| 161 | public: |
| 162 | // We provide non-explicit singleton constructors so users can pass |
| 163 | // in a "const char*" or a "string" wherever a "StringPiece" is |
| 164 | // expected (likewise for char16, string16, StringPiece16). |
| 165 | constexpr BasicStringPiece() : ptr_(NULL), length_(0) {} |
| 166 | // TODO(crbug.com/1049498): Construction from nullptr is not allowed for |
| 167 | // std::basic_string_view, so remove the special handling for it. |
| 168 | // Note: This doesn't just use STRING_TYPE::traits_type::length(), since that |
| 169 | // isn't constexpr until C++17. |
| 170 | constexpr BasicStringPiece(const value_type* str) |
| 171 | : ptr_(str), length_(!str ? 0 : CharTraits<value_type>::length(str)) {} |
| 172 | // Explicitly disallow construction from nullptr. Note that this does not |
| 173 | // catch construction from runtime strings that might be null. |
| 174 | // Note: The following is just a more elaborate way of spelling |
| 175 | // `BasicStringPiece(nullptr_t) = delete`, but unfortunately the terse form is |
| 176 | // not supported by the PNaCl toolchain. |
| 177 | // TODO(crbug.com/1049498): Remove once we CHECK(str) in the constructor |
| 178 | // above. |
| 179 | template <class T, class = std::enable_if_t<std::is_null_pointer<T>::value>> |
| 180 | BasicStringPiece(T) { |
| 181 | static_assert(sizeof(T) == 0, // Always false. |
| 182 | "StringPiece does not support construction from nullptr, use " |
| 183 | "the default constructor instead."); |
| 184 | } |
| 185 | BasicStringPiece(const STRING_TYPE& str) |
| 186 | : ptr_(str.data()), length_(str.size()) {} |
| 187 | constexpr BasicStringPiece(const value_type* offset, size_type len) |
| 188 | : ptr_(offset), length_(len) {} |
| 189 | BasicStringPiece(const typename STRING_TYPE::const_iterator& begin, |
| 190 | const typename STRING_TYPE::const_iterator& end) { |
| 191 | DCHECK(begin <= end) << "StringPiece iterators swapped or invalid."; |
| 192 | length_ = static_cast<size_t>(std::distance(begin, end)); |
| 193 |
| 194 | // The length test before assignment is to avoid dereferencing an iterator |
| 195 | // that may point to the end() of a string. |
| 196 | ptr_ = length_ > 0 ? &*begin : nullptr; |
| 197 | } |
| 198 |
| 199 | // data() may return a pointer to a buffer with embedded NULs, and the |
| 200 | // returned buffer may or may not be null terminated. Therefore it is |
| 201 | // typically a mistake to pass data() to a routine that expects a NUL |
| 202 | // terminated string. |
| 203 | constexpr const value_type* data() const { return ptr_; } |
| 204 | constexpr size_type size() const noexcept { return length_; } |
| 205 | constexpr size_type length() const noexcept { return length_; } |
| 206 | bool empty() const { return length_ == 0; } |
| 207 |
| 208 | constexpr value_type operator[](size_type i) const { |
| 209 | CHECK(i < length_); |
| 210 | return ptr_[i]; |
| 211 | } |
| 212 |
| 213 | value_type front() const { |
| 214 | CHECK_NE(0UL, length_); |
| 215 | return ptr_[0]; |
| 216 | } |
| 217 |
| 218 | value_type back() const { |
| 219 | CHECK_NE(0UL, length_); |
| 220 | return ptr_[length_ - 1]; |
| 221 | } |
| 222 |
| 223 | constexpr void remove_prefix(size_type n) { |
| 224 | CHECK(n <= length_); |
| 225 | ptr_ += n; |
| 226 | length_ -= n; |
| 227 | } |
| 228 |
| 229 | constexpr void remove_suffix(size_type n) { |
| 230 | CHECK(n <= length_); |
| 231 | length_ -= n; |
| 232 | } |
| 233 |
| 234 | constexpr int compare(BasicStringPiece x) const noexcept { |
| 235 | int r = CharTraits<value_type>::compare( |
| 236 | ptr_, x.ptr_, (length_ < x.length_ ? length_ : x.length_)); |
| 237 | if (r == 0) { |
| 238 | if (length_ < x.length_) r = -1; |
| 239 | else if (length_ > x.length_) r = +1; |
| 240 | } |
| 241 | return r; |
| 242 | } |
| 243 |
| 244 | // This is the style of conversion preferred by std::string_view in C++17. |
| 245 | explicit operator STRING_TYPE() const { return as_string(); } |
| 246 |
| 247 | STRING_TYPE as_string() const { |
| 248 | // std::string doesn't like to take a NULL pointer even with a 0 size. |
| 249 | return empty() ? STRING_TYPE() : STRING_TYPE(data(), size()); |
| 250 | } |
| 251 |
| 252 | const_iterator begin() const { return ptr_; } |
| 253 | const_iterator end() const { return ptr_ + length_; } |
| 254 | const_reverse_iterator rbegin() const { |
| 255 | return const_reverse_iterator(ptr_ + length_); |
| 256 | } |
| 257 | const_reverse_iterator rend() const { |
| 258 | return const_reverse_iterator(ptr_); |
| 259 | } |
| 260 |
| 261 | size_type max_size() const { return length_; } |
| 262 | size_type capacity() const { return length_; } |
| 263 |
| 264 | size_type copy(value_type* buf, size_type n, size_type pos = 0) const { |
| 265 | return internal::copy(*this, buf, n, pos); |
| 266 | } |
| 267 |
| 268 | // Does "this" start with "x" |
| 269 | constexpr bool starts_with(BasicStringPiece x) const noexcept { |
| 270 | return ( |
| 271 | (this->length_ >= x.length_) && |
| 272 | (CharTraits<value_type>::compare(this->ptr_, x.ptr_, x.length_) == 0)); |
| 273 | } |
| 274 |
| 275 | // Does "this" end with "x" |
| 276 | constexpr bool ends_with(BasicStringPiece x) const noexcept { |
| 277 | return ((this->length_ >= x.length_) && |
| 278 | (CharTraits<value_type>::compare( |
| 279 | this->ptr_ + (this->length_ - x.length_), x.ptr_, x.length_) == |
| 280 | 0)); |
| 281 | } |
| 282 |
| 283 | // find: Search for a character or substring at a given offset. |
| 284 | size_type find(const BasicStringPiece<STRING_TYPE>& s, |
| 285 | size_type pos = 0) const { |
| 286 | return internal::find(*this, s, pos); |
| 287 | } |
| 288 | size_type find(value_type c, size_type pos = 0) const { |
| 289 | return internal::find(*this, c, pos); |
| 290 | } |
| 291 |
| 292 | // rfind: Reverse find. |
| 293 | size_type rfind(const BasicStringPiece& s, |
| 294 | size_type pos = BasicStringPiece::npos) const { |
| 295 | return internal::rfind(*this, s, pos); |
| 296 | } |
| 297 | size_type rfind(value_type c, size_type pos = BasicStringPiece::npos) const { |
| 298 | return internal::rfind(*this, c, pos); |
| 299 | } |
| 300 |
| 301 | // find_first_of: Find the first occurence of one of a set of characters. |
| 302 | size_type find_first_of(const BasicStringPiece& s, |
| 303 | size_type pos = 0) const { |
| 304 | return internal::find_first_of(*this, s, pos); |
| 305 | } |
| 306 | size_type find_first_of(value_type c, size_type pos = 0) const { |
| 307 | return find(c, pos); |
| 308 | } |
| 309 |
| 310 | // find_first_not_of: Find the first occurence not of a set of characters. |
| 311 | size_type find_first_not_of(const BasicStringPiece& s, |
| 312 | size_type pos = 0) const { |
| 313 | return internal::find_first_not_of(*this, s, pos); |
| 314 | } |
| 315 | size_type find_first_not_of(value_type c, size_type pos = 0) const { |
| 316 | return internal::find_first_not_of(*this, c, pos); |
| 317 | } |
| 318 |
| 319 | // find_last_of: Find the last occurence of one of a set of characters. |
| 320 | size_type find_last_of(const BasicStringPiece& s, |
| 321 | size_type pos = BasicStringPiece::npos) const { |
| 322 | return internal::find_last_of(*this, s, pos); |
| 323 | } |
| 324 | size_type find_last_of(value_type c, |
| 325 | size_type pos = BasicStringPiece::npos) const { |
| 326 | return rfind(c, pos); |
| 327 | } |
| 328 |
| 329 | // find_last_not_of: Find the last occurence not of a set of characters. |
| 330 | size_type find_last_not_of(const BasicStringPiece& s, |
| 331 | size_type pos = BasicStringPiece::npos) const { |
| 332 | return internal::find_last_not_of(*this, s, pos); |
| 333 | } |
| 334 | size_type find_last_not_of(value_type c, |
| 335 | size_type pos = BasicStringPiece::npos) const { |
| 336 | return internal::find_last_not_of(*this, c, pos); |
| 337 | } |
| 338 |
| 339 | // substr. |
| 340 | BasicStringPiece substr(size_type pos, |
| 341 | size_type n = BasicStringPiece::npos) const { |
| 342 | return internal::substr(*this, pos, n); |
| 343 | } |
| 344 |
| 345 | protected: |
| 346 | const value_type* ptr_; |
| 347 | size_type length_; |
| 348 | }; |
| 349 |
| 350 | template <typename STRING_TYPE> |
| 351 | const typename BasicStringPiece<STRING_TYPE>::size_type |
| 352 | BasicStringPiece<STRING_TYPE>::npos = |
| 353 | typename BasicStringPiece<STRING_TYPE>::size_type(-1); |
| 354 |
| 355 | // MSVC doesn't like complex extern templates and DLLs. |
| 356 | #if !defined(COMPILER_MSVC) |
| 357 | extern template class BASE_EXPORT BasicStringPiece<std::string>; |
| 358 | extern template class BASE_EXPORT BasicStringPiece<string16>; |
| 359 | #endif |
| 360 |
| 361 | // Comparison operators -------------------------------------------------------- |
| 362 | // operator == |
| 363 | template <typename StringT> |
| 364 | constexpr bool operator==(BasicStringPiece<StringT> lhs, |
| 365 | BasicStringPiece<StringT> rhs) noexcept { |
| 366 | return lhs.size() == rhs.size() && lhs.compare(rhs) == 0; |
| 367 | } |
| 368 |
| 369 | // Here and below we make use of std::common_type_t to emulate an identity type |
| 370 | // transformation. This creates a non-deduced context, so that we can compare |
| 371 | // StringPieces with types that implicitly convert to StringPieces. See |
| 372 | // https://wg21.link/n3766 for details. |
| 373 | // Furthermore, we require dummy template parameters for these overloads to work |
| 374 | // around a name mangling issue on Windows. |
| 375 | template <typename StringT, int = 1> |
| 376 | constexpr bool operator==( |
| 377 | BasicStringPiece<StringT> lhs, |
| 378 | std::common_type_t<BasicStringPiece<StringT>> rhs) noexcept { |
| 379 | return lhs.size() == rhs.size() && lhs.compare(rhs) == 0; |
| 380 | } |
| 381 |
| 382 | template <typename StringT, int = 2> |
| 383 | constexpr bool operator==(std::common_type_t<BasicStringPiece<StringT>> lhs, |
| 384 | BasicStringPiece<StringT> rhs) noexcept { |
| 385 | return lhs.size() == rhs.size() && lhs.compare(rhs) == 0; |
| 386 | } |
| 387 |
| 388 | // operator != |
| 389 | template <typename StringT> |
| 390 | constexpr bool operator!=(BasicStringPiece<StringT> lhs, |
| 391 | BasicStringPiece<StringT> rhs) noexcept { |
| 392 | return !(lhs == rhs); |
| 393 | } |
| 394 |
| 395 | template <typename StringT, int = 1> |
| 396 | constexpr bool operator!=( |
| 397 | BasicStringPiece<StringT> lhs, |
| 398 | std::common_type_t<BasicStringPiece<StringT>> rhs) noexcept { |
| 399 | return !(lhs == rhs); |
| 400 | } |
| 401 |
| 402 | template <typename StringT, int = 2> |
| 403 | constexpr bool operator!=(std::common_type_t<BasicStringPiece<StringT>> lhs, |
| 404 | BasicStringPiece<StringT> rhs) noexcept { |
| 405 | return !(lhs == rhs); |
| 406 | } |
| 407 |
| 408 | // operator < |
| 409 | template <typename StringT> |
| 410 | constexpr bool operator<(BasicStringPiece<StringT> lhs, |
| 411 | BasicStringPiece<StringT> rhs) noexcept { |
| 412 | return lhs.compare(rhs) < 0; |
| 413 | } |
| 414 |
| 415 | template <typename StringT, int = 1> |
| 416 | constexpr bool operator<( |
| 417 | BasicStringPiece<StringT> lhs, |
| 418 | std::common_type_t<BasicStringPiece<StringT>> rhs) noexcept { |
| 419 | return lhs.compare(rhs) < 0; |
| 420 | } |
| 421 |
| 422 | template <typename StringT, int = 2> |
| 423 | constexpr bool operator<(std::common_type_t<BasicStringPiece<StringT>> lhs, |
| 424 | BasicStringPiece<StringT> rhs) noexcept { |
| 425 | return lhs.compare(rhs) < 0; |
| 426 | } |
| 427 |
| 428 | // operator > |
| 429 | template <typename StringT> |
| 430 | constexpr bool operator>(BasicStringPiece<StringT> lhs, |
| 431 | BasicStringPiece<StringT> rhs) noexcept { |
| 432 | return rhs < lhs; |
| 433 | } |
| 434 |
| 435 | template <typename StringT, int = 1> |
| 436 | constexpr bool operator>( |
| 437 | BasicStringPiece<StringT> lhs, |
| 438 | std::common_type_t<BasicStringPiece<StringT>> rhs) noexcept { |
| 439 | return rhs < lhs; |
| 440 | } |
| 441 |
| 442 | template <typename StringT, int = 2> |
| 443 | constexpr bool operator>(std::common_type_t<BasicStringPiece<StringT>> lhs, |
| 444 | BasicStringPiece<StringT> rhs) noexcept { |
| 445 | return rhs < lhs; |
| 446 | } |
| 447 |
| 448 | // operator <= |
| 449 | template <typename StringT> |
| 450 | constexpr bool operator<=(BasicStringPiece<StringT> lhs, |
| 451 | BasicStringPiece<StringT> rhs) noexcept { |
| 452 | return !(rhs < lhs); |
| 453 | } |
| 454 |
| 455 | template <typename StringT, int = 1> |
| 456 | constexpr bool operator<=( |
| 457 | BasicStringPiece<StringT> lhs, |
| 458 | std::common_type_t<BasicStringPiece<StringT>> rhs) noexcept { |
| 459 | return !(rhs < lhs); |
| 460 | } |
| 461 |
| 462 | template <typename StringT, int = 2> |
| 463 | constexpr bool operator<=(std::common_type_t<BasicStringPiece<StringT>> lhs, |
| 464 | BasicStringPiece<StringT> rhs) noexcept { |
| 465 | return !(rhs < lhs); |
| 466 | } |
| 467 |
| 468 | // operator >= |
| 469 | template <typename StringT> |
| 470 | constexpr bool operator>=(BasicStringPiece<StringT> lhs, |
| 471 | BasicStringPiece<StringT> rhs) noexcept { |
| 472 | return !(lhs < rhs); |
| 473 | } |
| 474 |
| 475 | template <typename StringT, int = 1> |
| 476 | constexpr bool operator>=( |
| 477 | BasicStringPiece<StringT> lhs, |
| 478 | std::common_type_t<BasicStringPiece<StringT>> rhs) noexcept { |
| 479 | return !(lhs < rhs); |
| 480 | } |
| 481 |
| 482 | template <typename StringT, int = 2> |
| 483 | constexpr bool operator>=(std::common_type_t<BasicStringPiece<StringT>> lhs, |
| 484 | BasicStringPiece<StringT> rhs) noexcept { |
| 485 | return !(lhs < rhs); |
| 486 | } |
| 487 |
| 488 | BASE_EXPORT std::ostream& operator<<(std::ostream& o, |
| 489 | const StringPiece& piece); |
| 490 |
| 491 | BASE_EXPORT std::ostream& operator<<(std::ostream& o, |
| 492 | const StringPiece16& piece); |
| 493 |
| 494 | // Hashing --------------------------------------------------------------------- |
| 495 |
| 496 | // We provide appropriate hash functions so StringPiece and StringPiece16 can |
| 497 | // be used as keys in hash sets and maps. |
| 498 |
| 499 | // This hash function is copied from base/strings/string16.h. We don't use the |
| 500 | // ones already defined for string and string16 directly because it would |
| 501 | // require the string constructors to be called, which we don't want. |
| 502 |
| 503 | template <typename StringPieceType> |
| 504 | struct StringPieceHashImpl { |
| 505 | std::size_t operator()(StringPieceType sp) const { |
| 506 | std::size_t result = 0; |
| 507 | for (auto c : sp) |
| 508 | result = (result * 131) + c; |
| 509 | return result; |
| 510 | } |
| 511 | }; |
| 512 |
| 513 | using StringPieceHash = StringPieceHashImpl<StringPiece>; |
| 514 | using StringPiece16Hash = StringPieceHashImpl<StringPiece16>; |
| 515 | using WStringPieceHash = StringPieceHashImpl<WStringPiece>; |
| 516 |
| 517 | } // namespace base |
| 518 |
| 519 | #endif // BASE_STRINGS_STRING_PIECE_H_ |
| 520 |