| 1 | // |
| 2 | // Copyright 2017 The Abseil Authors. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | // ----------------------------------------------------------------------------- |
| 17 | // File: string_view.h |
| 18 | // ----------------------------------------------------------------------------- |
| 19 | // |
| 20 | // This file contains the definition of the `absl::string_view` class. A |
| 21 | // `string_view` points to a contiguous span of characters, often part or all of |
| 22 | // another `std::string`, double-quoted string literal, character array, or even |
| 23 | // another `string_view`. |
| 24 | // |
| 25 | // This `absl::string_view` abstraction is designed to be a drop-in |
| 26 | // replacement for the C++17 `std::string_view` abstraction. |
| 27 | #ifndef ABSL_STRINGS_STRING_VIEW_H_ |
| 28 | #define ABSL_STRINGS_STRING_VIEW_H_ |
| 29 | |
| 30 | #include <algorithm> |
| 31 | #include <cassert> |
| 32 | #include <cstddef> |
| 33 | #include <cstring> |
| 34 | #include <iosfwd> |
| 35 | #include <iterator> |
| 36 | #include <limits> |
| 37 | #include <string> |
| 38 | |
| 39 | #include "absl/base/attributes.h" |
| 40 | #include "absl/base/config.h" |
| 41 | #include "absl/base/internal/throw_delegate.h" |
| 42 | #include "absl/base/macros.h" |
| 43 | #include "absl/base/optimization.h" |
| 44 | #include "absl/base/port.h" |
| 45 | |
| 46 | #ifdef ABSL_USES_STD_STRING_VIEW |
| 47 | |
| 48 | #include <string_view> // IWYU pragma: export |
| 49 | |
| 50 | namespace absl { |
| 51 | ABSL_NAMESPACE_BEGIN |
| 52 | using string_view = std::string_view; |
| 53 | ABSL_NAMESPACE_END |
| 54 | } // namespace absl |
| 55 | |
| 56 | #else // ABSL_USES_STD_STRING_VIEW |
| 57 | |
| 58 | #if ABSL_HAVE_BUILTIN(__builtin_memcmp) || \ |
| 59 | (defined(__GNUC__) && !defined(__clang__)) || \ |
| 60 | (defined(_MSC_VER) && _MSC_VER >= 1928) |
| 61 | #define ABSL_INTERNAL_STRING_VIEW_MEMCMP __builtin_memcmp |
| 62 | #else // ABSL_HAVE_BUILTIN(__builtin_memcmp) |
| 63 | #define ABSL_INTERNAL_STRING_VIEW_MEMCMP memcmp |
| 64 | #endif // ABSL_HAVE_BUILTIN(__builtin_memcmp) |
| 65 | |
| 66 | #if defined(__cplusplus) && __cplusplus >= 201402L |
| 67 | #define ABSL_INTERNAL_STRING_VIEW_CXX14_CONSTEXPR constexpr |
| 68 | #else |
| 69 | #define ABSL_INTERNAL_STRING_VIEW_CXX14_CONSTEXPR |
| 70 | #endif |
| 71 | |
| 72 | namespace absl { |
| 73 | ABSL_NAMESPACE_BEGIN |
| 74 | |
| 75 | // absl::string_view |
| 76 | // |
| 77 | // A `string_view` provides a lightweight view into the string data provided by |
| 78 | // a `std::string`, double-quoted string literal, character array, or even |
| 79 | // another `string_view`. A `string_view` does *not* own the string to which it |
| 80 | // points, and that data cannot be modified through the view. |
| 81 | // |
| 82 | // You can use `string_view` as a function or method parameter anywhere a |
| 83 | // parameter can receive a double-quoted string literal, `const char*`, |
| 84 | // `std::string`, or another `absl::string_view` argument with no need to copy |
| 85 | // the string data. Systematic use of `string_view` within function arguments |
| 86 | // reduces data copies and `strlen()` calls. |
| 87 | // |
| 88 | // Because of its small size, prefer passing `string_view` by value: |
| 89 | // |
| 90 | // void MyFunction(absl::string_view arg); |
| 91 | // |
| 92 | // If circumstances require, you may also pass one by const reference: |
| 93 | // |
| 94 | // void MyFunction(const absl::string_view& arg); // not preferred |
| 95 | // |
| 96 | // Passing by value generates slightly smaller code for many architectures. |
| 97 | // |
| 98 | // In either case, the source data of the `string_view` must outlive the |
| 99 | // `string_view` itself. |
| 100 | // |
| 101 | // A `string_view` is also suitable for local variables if you know that the |
| 102 | // lifetime of the underlying object is longer than the lifetime of your |
| 103 | // `string_view` variable. However, beware of binding a `string_view` to a |
| 104 | // temporary value: |
| 105 | // |
| 106 | // // BAD use of string_view: lifetime problem |
| 107 | // absl::string_view sv = obj.ReturnAString(); |
| 108 | // |
| 109 | // // GOOD use of string_view: str outlives sv |
| 110 | // std::string str = obj.ReturnAString(); |
| 111 | // absl::string_view sv = str; |
| 112 | // |
| 113 | // Due to lifetime issues, a `string_view` is sometimes a poor choice for a |
| 114 | // return value and usually a poor choice for a data member. If you do use a |
| 115 | // `string_view` this way, it is your responsibility to ensure that the object |
| 116 | // pointed to by the `string_view` outlives the `string_view`. |
| 117 | // |
| 118 | // A `string_view` may represent a whole string or just part of a string. For |
| 119 | // example, when splitting a string, `std::vector<absl::string_view>` is a |
| 120 | // natural data type for the output. |
| 121 | // |
| 122 | // For another example, a Cord is a non-contiguous, potentially very |
| 123 | // long string-like object. The Cord class has an interface that iteratively |
| 124 | // provides string_view objects that point to the successive pieces of a Cord |
| 125 | // object. |
| 126 | // |
| 127 | // When constructed from a source which is NUL-terminated, the `string_view` |
| 128 | // itself will not include the NUL-terminator unless a specific size (including |
| 129 | // the NUL) is passed to the constructor. As a result, common idioms that work |
| 130 | // on NUL-terminated strings do not work on `string_view` objects. If you write |
| 131 | // code that scans a `string_view`, you must check its length rather than test |
| 132 | // for nul, for example. Note, however, that nuls may still be embedded within |
| 133 | // a `string_view` explicitly. |
| 134 | // |
| 135 | // You may create a null `string_view` in two ways: |
| 136 | // |
| 137 | // absl::string_view sv; |
| 138 | // absl::string_view sv(nullptr, 0); |
| 139 | // |
| 140 | // For the above, `sv.data() == nullptr`, `sv.length() == 0`, and |
| 141 | // `sv.empty() == true`. Also, if you create a `string_view` with a non-null |
| 142 | // pointer then `sv.data() != nullptr`. Thus, you can use `string_view()` to |
| 143 | // signal an undefined value that is different from other `string_view` values |
| 144 | // in a similar fashion to how `const char* p1 = nullptr;` is different from |
| 145 | // `const char* p2 = "";`. However, in practice, it is not recommended to rely |
| 146 | // on this behavior. |
| 147 | // |
| 148 | // Be careful not to confuse a null `string_view` with an empty one. A null |
| 149 | // `string_view` is an empty `string_view`, but some empty `string_view`s are |
| 150 | // not null. Prefer checking for emptiness over checking for null. |
| 151 | // |
| 152 | // There are many ways to create an empty string_view: |
| 153 | // |
| 154 | // const char* nullcp = nullptr; |
| 155 | // // string_view.size() will return 0 in all cases. |
| 156 | // absl::string_view(); |
| 157 | // absl::string_view(nullcp, 0); |
| 158 | // absl::string_view(""); |
| 159 | // absl::string_view("", 0); |
| 160 | // absl::string_view("abcdef", 0); |
| 161 | // absl::string_view("abcdef" + 6, 0); |
| 162 | // |
| 163 | // All empty `string_view` objects whether null or not, are equal: |
| 164 | // |
| 165 | // absl::string_view() == absl::string_view("", 0) |
| 166 | // absl::string_view(nullptr, 0) == absl::string_view("abcdef"+6, 0) |
| 167 | class string_view { |
| 168 | public: |
| 169 | using traits_type = std::char_traits<char>; |
| 170 | using value_type = char; |
| 171 | using pointer = char*; |
| 172 | using const_pointer = const char*; |
| 173 | using reference = char&; |
| 174 | using const_reference = const char&; |
| 175 | using const_iterator = const char*; |
| 176 | using iterator = const_iterator; |
| 177 | using const_reverse_iterator = std::reverse_iterator<const_iterator>; |
| 178 | using reverse_iterator = const_reverse_iterator; |
| 179 | using size_type = size_t; |
| 180 | using difference_type = std::ptrdiff_t; |
| 181 | |
| 182 | static constexpr size_type npos = static_cast<size_type>(-1); |
| 183 | |
| 184 | // Null `string_view` constructor |
| 185 | constexpr string_view() noexcept : ptr_(nullptr), length_(0) {} |
| 186 | |
| 187 | // Implicit constructors |
| 188 | |
| 189 | template <typename Allocator> |
| 190 | string_view( // NOLINT(runtime/explicit) |
| 191 | const std::basic_string<char, std::char_traits<char>, Allocator>& str |
| 192 | ABSL_ATTRIBUTE_LIFETIME_BOUND) noexcept |
| 193 | // This is implemented in terms of `string_view(p, n)` so `str.size()` |
| 194 | // doesn't need to be reevaluated after `ptr_` is set. |
| 195 | // The length check is also skipped since it is unnecessary and causes |
| 196 | // code bloat. |
| 197 | : string_view(str.data(), str.size(), SkipCheckLengthTag{}) {} |
| 198 | |
| 199 | // Implicit constructor of a `string_view` from NUL-terminated `str`. When |
| 200 | // accepting possibly null strings, use `absl::NullSafeStringView(str)` |
| 201 | // instead (see below). |
| 202 | // The length check is skipped since it is unnecessary and causes code bloat. |
| 203 | constexpr string_view(const char* str) // NOLINT(runtime/explicit) |
| 204 | : ptr_(str), length_(str ? StrlenInternal(str) : 0) {} |
| 205 | |
| 206 | // Implicit constructor of a `string_view` from a `const char*` and length. |
| 207 | constexpr string_view(const char* data, size_type len) |
| 208 | : ptr_(data), length_(CheckLengthInternal(len)) {} |
| 209 | |
| 210 | // NOTE: Harmlessly omitted to work around gdb bug. |
| 211 | // constexpr string_view(const string_view&) noexcept = default; |
| 212 | // string_view& operator=(const string_view&) noexcept = default; |
| 213 | |
| 214 | // Iterators |
| 215 | |
| 216 | // string_view::begin() |
| 217 | // |
| 218 | // Returns an iterator pointing to the first character at the beginning of the |
| 219 | // `string_view`, or `end()` if the `string_view` is empty. |
| 220 | constexpr const_iterator begin() const noexcept { return ptr_; } |
| 221 | |
| 222 | // string_view::end() |
| 223 | // |
| 224 | // Returns an iterator pointing just beyond the last character at the end of |
| 225 | // the `string_view`. This iterator acts as a placeholder; attempting to |
| 226 | // access it results in undefined behavior. |
| 227 | constexpr const_iterator end() const noexcept { return ptr_ + length_; } |
| 228 | |
| 229 | // string_view::cbegin() |
| 230 | // |
| 231 | // Returns a const iterator pointing to the first character at the beginning |
| 232 | // of the `string_view`, or `end()` if the `string_view` is empty. |
| 233 | constexpr const_iterator cbegin() const noexcept { return begin(); } |
| 234 | |
| 235 | // string_view::cend() |
| 236 | // |
| 237 | // Returns a const iterator pointing just beyond the last character at the end |
| 238 | // of the `string_view`. This pointer acts as a placeholder; attempting to |
| 239 | // access its element results in undefined behavior. |
| 240 | constexpr const_iterator cend() const noexcept { return end(); } |
| 241 | |
| 242 | // string_view::rbegin() |
| 243 | // |
| 244 | // Returns a reverse iterator pointing to the last character at the end of the |
| 245 | // `string_view`, or `rend()` if the `string_view` is empty. |
| 246 | const_reverse_iterator rbegin() const noexcept { |
| 247 | return const_reverse_iterator(end()); |
| 248 | } |
| 249 | |
| 250 | // string_view::rend() |
| 251 | // |
| 252 | // Returns a reverse iterator pointing just before the first character at the |
| 253 | // beginning of the `string_view`. This pointer acts as a placeholder; |
| 254 | // attempting to access its element results in undefined behavior. |
| 255 | const_reverse_iterator rend() const noexcept { |
| 256 | return const_reverse_iterator(begin()); |
| 257 | } |
| 258 | |
| 259 | // string_view::crbegin() |
| 260 | // |
| 261 | // Returns a const reverse iterator pointing to the last character at the end |
| 262 | // of the `string_view`, or `crend()` if the `string_view` is empty. |
| 263 | const_reverse_iterator crbegin() const noexcept { return rbegin(); } |
| 264 | |
| 265 | // string_view::crend() |
| 266 | // |
| 267 | // Returns a const reverse iterator pointing just before the first character |
| 268 | // at the beginning of the `string_view`. This pointer acts as a placeholder; |
| 269 | // attempting to access its element results in undefined behavior. |
| 270 | const_reverse_iterator crend() const noexcept { return rend(); } |
| 271 | |
| 272 | // Capacity Utilities |
| 273 | |
| 274 | // string_view::size() |
| 275 | // |
| 276 | // Returns the number of characters in the `string_view`. |
| 277 | constexpr size_type size() const noexcept { return length_; } |
| 278 | |
| 279 | // string_view::length() |
| 280 | // |
| 281 | // Returns the number of characters in the `string_view`. Alias for `size()`. |
| 282 | constexpr size_type length() const noexcept { return size(); } |
| 283 | |
| 284 | // string_view::max_size() |
| 285 | // |
| 286 | // Returns the maximum number of characters the `string_view` can hold. |
| 287 | constexpr size_type max_size() const noexcept { return kMaxSize; } |
| 288 | |
| 289 | // string_view::empty() |
| 290 | // |
| 291 | // Checks if the `string_view` is empty (refers to no characters). |
| 292 | constexpr bool empty() const noexcept { return length_ == 0; } |
| 293 | |
| 294 | // string_view::operator[] |
| 295 | // |
| 296 | // Returns the ith element of the `string_view` using the array operator. |
| 297 | // Note that this operator does not perform any bounds checking. |
| 298 | constexpr const_reference operator[](size_type i) const { |
| 299 | return ABSL_HARDENING_ASSERT(i < size()), ptr_[i]; |
| 300 | } |
| 301 | |
| 302 | // string_view::at() |
| 303 | // |
| 304 | // Returns the ith element of the `string_view`. Bounds checking is performed, |
| 305 | // and an exception of type `std::out_of_range` will be thrown on invalid |
| 306 | // access. |
| 307 | constexpr const_reference at(size_type i) const { |
| 308 | return ABSL_PREDICT_TRUE(i < size()) |
| 309 | ? ptr_[i] |
| 310 | : ((void)base_internal::ThrowStdOutOfRange( |
| 311 | what_arg: "absl::string_view::at" ), |
| 312 | ptr_[i]); |
| 313 | } |
| 314 | |
| 315 | // string_view::front() |
| 316 | // |
| 317 | // Returns the first element of a `string_view`. |
| 318 | constexpr const_reference front() const { |
| 319 | return ABSL_HARDENING_ASSERT(!empty()), ptr_[0]; |
| 320 | } |
| 321 | |
| 322 | // string_view::back() |
| 323 | // |
| 324 | // Returns the last element of a `string_view`. |
| 325 | constexpr const_reference back() const { |
| 326 | return ABSL_HARDENING_ASSERT(!empty()), ptr_[size() - 1]; |
| 327 | } |
| 328 | |
| 329 | // string_view::data() |
| 330 | // |
| 331 | // Returns a pointer to the underlying character array (which is of course |
| 332 | // stored elsewhere). Note that `string_view::data()` may contain embedded nul |
| 333 | // characters, but the returned buffer may or may not be NUL-terminated; |
| 334 | // therefore, do not pass `data()` to a routine that expects a NUL-terminated |
| 335 | // string. |
| 336 | constexpr const_pointer data() const noexcept { return ptr_; } |
| 337 | |
| 338 | // Modifiers |
| 339 | |
| 340 | // string_view::remove_prefix() |
| 341 | // |
| 342 | // Removes the first `n` characters from the `string_view`. Note that the |
| 343 | // underlying string is not changed, only the view. |
| 344 | ABSL_INTERNAL_STRING_VIEW_CXX14_CONSTEXPR void remove_prefix(size_type n) { |
| 345 | ABSL_HARDENING_ASSERT(n <= length_); |
| 346 | ptr_ += n; |
| 347 | length_ -= n; |
| 348 | } |
| 349 | |
| 350 | // string_view::remove_suffix() |
| 351 | // |
| 352 | // Removes the last `n` characters from the `string_view`. Note that the |
| 353 | // underlying string is not changed, only the view. |
| 354 | ABSL_INTERNAL_STRING_VIEW_CXX14_CONSTEXPR void remove_suffix(size_type n) { |
| 355 | ABSL_HARDENING_ASSERT(n <= length_); |
| 356 | length_ -= n; |
| 357 | } |
| 358 | |
| 359 | // string_view::swap() |
| 360 | // |
| 361 | // Swaps this `string_view` with another `string_view`. |
| 362 | ABSL_INTERNAL_STRING_VIEW_CXX14_CONSTEXPR void swap(string_view& s) noexcept { |
| 363 | auto t = *this; |
| 364 | *this = s; |
| 365 | s = t; |
| 366 | } |
| 367 | |
| 368 | // Explicit conversion operators |
| 369 | |
| 370 | // Converts to `std::basic_string`. |
| 371 | template <typename A> |
| 372 | explicit operator std::basic_string<char, traits_type, A>() const { |
| 373 | if (!data()) return {}; |
| 374 | return std::basic_string<char, traits_type, A>(data(), size()); |
| 375 | } |
| 376 | |
| 377 | // string_view::copy() |
| 378 | // |
| 379 | // Copies the contents of the `string_view` at offset `pos` and length `n` |
| 380 | // into `buf`. |
| 381 | size_type copy(char* buf, size_type n, size_type pos = 0) const { |
| 382 | if (ABSL_PREDICT_FALSE(pos > length_)) { |
| 383 | base_internal::ThrowStdOutOfRange(what_arg: "absl::string_view::copy" ); |
| 384 | } |
| 385 | size_type rlen = (std::min)(a: length_ - pos, b: n); |
| 386 | if (rlen > 0) { |
| 387 | const char* start = ptr_ + pos; |
| 388 | traits_type::copy(s1: buf, s2: start, n: rlen); |
| 389 | } |
| 390 | return rlen; |
| 391 | } |
| 392 | |
| 393 | // string_view::substr() |
| 394 | // |
| 395 | // Returns a "substring" of the `string_view` (at offset `pos` and length |
| 396 | // `n`) as another string_view. This function throws `std::out_of_bounds` if |
| 397 | // `pos > size`. |
| 398 | // Use absl::ClippedSubstr if you need a truncating substr operation. |
| 399 | constexpr string_view substr(size_type pos = 0, size_type n = npos) const { |
| 400 | return ABSL_PREDICT_FALSE(pos > length_) |
| 401 | ? (base_internal::ThrowStdOutOfRange( |
| 402 | what_arg: "absl::string_view::substr" ), |
| 403 | string_view()) |
| 404 | : string_view(ptr_ + pos, Min(length_a: n, length_b: length_ - pos)); |
| 405 | } |
| 406 | |
| 407 | // string_view::compare() |
| 408 | // |
| 409 | // Performs a lexicographical comparison between this `string_view` and |
| 410 | // another `string_view` `x`, returning a negative value if `*this` is less |
| 411 | // than `x`, 0 if `*this` is equal to `x`, and a positive value if `*this` |
| 412 | // is greater than `x`. |
| 413 | constexpr int compare(string_view x) const noexcept { |
| 414 | return CompareImpl(length_a: length_, length_b: x.length_, |
| 415 | compare_result: Min(length_a: length_, length_b: x.length_) == 0 |
| 416 | ? 0 |
| 417 | : ABSL_INTERNAL_STRING_VIEW_MEMCMP( |
| 418 | ptr_, x.ptr_, Min(length_a: length_, length_b: x.length_))); |
| 419 | } |
| 420 | |
| 421 | // Overload of `string_view::compare()` for comparing a substring of the |
| 422 | // 'string_view` and another `absl::string_view`. |
| 423 | constexpr int compare(size_type pos1, size_type count1, string_view v) const { |
| 424 | return substr(pos: pos1, n: count1).compare(x: v); |
| 425 | } |
| 426 | |
| 427 | // Overload of `string_view::compare()` for comparing a substring of the |
| 428 | // `string_view` and a substring of another `absl::string_view`. |
| 429 | constexpr int compare(size_type pos1, size_type count1, string_view v, |
| 430 | size_type pos2, size_type count2) const { |
| 431 | return substr(pos: pos1, n: count1).compare(x: v.substr(pos: pos2, n: count2)); |
| 432 | } |
| 433 | |
| 434 | // Overload of `string_view::compare()` for comparing a `string_view` and a |
| 435 | // a different C-style string `s`. |
| 436 | constexpr int compare(const char* s) const { return compare(x: string_view(s)); } |
| 437 | |
| 438 | // Overload of `string_view::compare()` for comparing a substring of the |
| 439 | // `string_view` and a different string C-style string `s`. |
| 440 | constexpr int compare(size_type pos1, size_type count1, const char* s) const { |
| 441 | return substr(pos: pos1, n: count1).compare(x: string_view(s)); |
| 442 | } |
| 443 | |
| 444 | // Overload of `string_view::compare()` for comparing a substring of the |
| 445 | // `string_view` and a substring of a different C-style string `s`. |
| 446 | constexpr int compare(size_type pos1, size_type count1, const char* s, |
| 447 | size_type count2) const { |
| 448 | return substr(pos: pos1, n: count1).compare(x: string_view(s, count2)); |
| 449 | } |
| 450 | |
| 451 | // Find Utilities |
| 452 | |
| 453 | // string_view::find() |
| 454 | // |
| 455 | // Finds the first occurrence of the substring `s` within the `string_view`, |
| 456 | // returning the position of the first character's match, or `npos` if no |
| 457 | // match was found. |
| 458 | size_type find(string_view s, size_type pos = 0) const noexcept; |
| 459 | |
| 460 | // Overload of `string_view::find()` for finding the given character `c` |
| 461 | // within the `string_view`. |
| 462 | size_type find(char c, size_type pos = 0) const noexcept; |
| 463 | |
| 464 | // Overload of `string_view::find()` for finding a substring of a different |
| 465 | // C-style string `s` within the `string_view`. |
| 466 | size_type find(const char* s, size_type pos, size_type count) const { |
| 467 | return find(s: string_view(s, count), pos); |
| 468 | } |
| 469 | |
| 470 | // Overload of `string_view::find()` for finding a different C-style string |
| 471 | // `s` within the `string_view`. |
| 472 | size_type find(const char* s, size_type pos = 0) const { |
| 473 | return find(s: string_view(s), pos); |
| 474 | } |
| 475 | |
| 476 | // string_view::rfind() |
| 477 | // |
| 478 | // Finds the last occurrence of a substring `s` within the `string_view`, |
| 479 | // returning the position of the first character's match, or `npos` if no |
| 480 | // match was found. |
| 481 | size_type rfind(string_view s, size_type pos = npos) const noexcept; |
| 482 | |
| 483 | // Overload of `string_view::rfind()` for finding the last given character `c` |
| 484 | // within the `string_view`. |
| 485 | size_type rfind(char c, size_type pos = npos) const noexcept; |
| 486 | |
| 487 | // Overload of `string_view::rfind()` for finding a substring of a different |
| 488 | // C-style string `s` within the `string_view`. |
| 489 | size_type rfind(const char* s, size_type pos, size_type count) const { |
| 490 | return rfind(s: string_view(s, count), pos); |
| 491 | } |
| 492 | |
| 493 | // Overload of `string_view::rfind()` for finding a different C-style string |
| 494 | // `s` within the `string_view`. |
| 495 | size_type rfind(const char* s, size_type pos = npos) const { |
| 496 | return rfind(s: string_view(s), pos); |
| 497 | } |
| 498 | |
| 499 | // string_view::find_first_of() |
| 500 | // |
| 501 | // Finds the first occurrence of any of the characters in `s` within the |
| 502 | // `string_view`, returning the start position of the match, or `npos` if no |
| 503 | // match was found. |
| 504 | size_type find_first_of(string_view s, size_type pos = 0) const noexcept; |
| 505 | |
| 506 | // Overload of `string_view::find_first_of()` for finding a character `c` |
| 507 | // within the `string_view`. |
| 508 | size_type find_first_of(char c, size_type pos = 0) const noexcept { |
| 509 | return find(c, pos); |
| 510 | } |
| 511 | |
| 512 | // Overload of `string_view::find_first_of()` for finding a substring of a |
| 513 | // different C-style string `s` within the `string_view`. |
| 514 | size_type find_first_of(const char* s, size_type pos, |
| 515 | size_type count) const { |
| 516 | return find_first_of(s: string_view(s, count), pos); |
| 517 | } |
| 518 | |
| 519 | // Overload of `string_view::find_first_of()` for finding a different C-style |
| 520 | // string `s` within the `string_view`. |
| 521 | size_type find_first_of(const char* s, size_type pos = 0) const { |
| 522 | return find_first_of(s: string_view(s), pos); |
| 523 | } |
| 524 | |
| 525 | // string_view::find_last_of() |
| 526 | // |
| 527 | // Finds the last occurrence of any of the characters in `s` within the |
| 528 | // `string_view`, returning the start position of the match, or `npos` if no |
| 529 | // match was found. |
| 530 | size_type find_last_of(string_view s, size_type pos = npos) const noexcept; |
| 531 | |
| 532 | // Overload of `string_view::find_last_of()` for finding a character `c` |
| 533 | // within the `string_view`. |
| 534 | size_type find_last_of(char c, size_type pos = npos) const noexcept { |
| 535 | return rfind(c, pos); |
| 536 | } |
| 537 | |
| 538 | // Overload of `string_view::find_last_of()` for finding a substring of a |
| 539 | // different C-style string `s` within the `string_view`. |
| 540 | size_type find_last_of(const char* s, size_type pos, size_type count) const { |
| 541 | return find_last_of(s: string_view(s, count), pos); |
| 542 | } |
| 543 | |
| 544 | // Overload of `string_view::find_last_of()` for finding a different C-style |
| 545 | // string `s` within the `string_view`. |
| 546 | size_type find_last_of(const char* s, size_type pos = npos) const { |
| 547 | return find_last_of(s: string_view(s), pos); |
| 548 | } |
| 549 | |
| 550 | // string_view::find_first_not_of() |
| 551 | // |
| 552 | // Finds the first occurrence of any of the characters not in `s` within the |
| 553 | // `string_view`, returning the start position of the first non-match, or |
| 554 | // `npos` if no non-match was found. |
| 555 | size_type find_first_not_of(string_view s, size_type pos = 0) const noexcept; |
| 556 | |
| 557 | // Overload of `string_view::find_first_not_of()` for finding a character |
| 558 | // that is not `c` within the `string_view`. |
| 559 | size_type find_first_not_of(char c, size_type pos = 0) const noexcept; |
| 560 | |
| 561 | // Overload of `string_view::find_first_not_of()` for finding a substring of a |
| 562 | // different C-style string `s` within the `string_view`. |
| 563 | size_type find_first_not_of(const char* s, size_type pos, |
| 564 | size_type count) const { |
| 565 | return find_first_not_of(s: string_view(s, count), pos); |
| 566 | } |
| 567 | |
| 568 | // Overload of `string_view::find_first_not_of()` for finding a different |
| 569 | // C-style string `s` within the `string_view`. |
| 570 | size_type find_first_not_of(const char* s, size_type pos = 0) const { |
| 571 | return find_first_not_of(s: string_view(s), pos); |
| 572 | } |
| 573 | |
| 574 | // string_view::find_last_not_of() |
| 575 | // |
| 576 | // Finds the last occurrence of any of the characters not in `s` within the |
| 577 | // `string_view`, returning the start position of the last non-match, or |
| 578 | // `npos` if no non-match was found. |
| 579 | size_type find_last_not_of(string_view s, |
| 580 | size_type pos = npos) const noexcept; |
| 581 | |
| 582 | // Overload of `string_view::find_last_not_of()` for finding a character |
| 583 | // that is not `c` within the `string_view`. |
| 584 | size_type find_last_not_of(char c, size_type pos = npos) const noexcept; |
| 585 | |
| 586 | // Overload of `string_view::find_last_not_of()` for finding a substring of a |
| 587 | // different C-style string `s` within the `string_view`. |
| 588 | size_type find_last_not_of(const char* s, size_type pos, |
| 589 | size_type count) const { |
| 590 | return find_last_not_of(s: string_view(s, count), pos); |
| 591 | } |
| 592 | |
| 593 | // Overload of `string_view::find_last_not_of()` for finding a different |
| 594 | // C-style string `s` within the `string_view`. |
| 595 | size_type find_last_not_of(const char* s, size_type pos = npos) const { |
| 596 | return find_last_not_of(s: string_view(s), pos); |
| 597 | } |
| 598 | |
| 599 | private: |
| 600 | // The constructor from std::string delegates to this constructor. |
| 601 | // See the comment on that constructor for the rationale. |
| 602 | struct SkipCheckLengthTag {}; |
| 603 | string_view(const char* data, size_type len, SkipCheckLengthTag) noexcept |
| 604 | : ptr_(data), length_(len) {} |
| 605 | |
| 606 | static constexpr size_type kMaxSize = |
| 607 | (std::numeric_limits<difference_type>::max)(); |
| 608 | |
| 609 | static constexpr size_type CheckLengthInternal(size_type len) { |
| 610 | return ABSL_HARDENING_ASSERT(len <= kMaxSize), len; |
| 611 | } |
| 612 | |
| 613 | static constexpr size_type StrlenInternal(const char* str) { |
| 614 | #if defined(_MSC_VER) && _MSC_VER >= 1910 && !defined(__clang__) |
| 615 | // MSVC 2017+ can evaluate this at compile-time. |
| 616 | const char* begin = str; |
| 617 | while (*str != '\0') ++str; |
| 618 | return str - begin; |
| 619 | #elif ABSL_HAVE_BUILTIN(__builtin_strlen) || \ |
| 620 | (defined(__GNUC__) && !defined(__clang__)) |
| 621 | // GCC has __builtin_strlen according to |
| 622 | // https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Other-Builtins.html, but |
| 623 | // ABSL_HAVE_BUILTIN doesn't detect that, so we use the extra checks above. |
| 624 | // __builtin_strlen is constexpr. |
| 625 | return __builtin_strlen(str); |
| 626 | #else |
| 627 | return str ? strlen(str) : 0; |
| 628 | #endif |
| 629 | } |
| 630 | |
| 631 | static constexpr size_t Min(size_type length_a, size_type length_b) { |
| 632 | return length_a < length_b ? length_a : length_b; |
| 633 | } |
| 634 | |
| 635 | static constexpr int CompareImpl(size_type length_a, size_type length_b, |
| 636 | int compare_result) { |
| 637 | return compare_result == 0 ? static_cast<int>(length_a > length_b) - |
| 638 | static_cast<int>(length_a < length_b) |
| 639 | : (compare_result < 0 ? -1 : 1); |
| 640 | } |
| 641 | |
| 642 | const char* ptr_; |
| 643 | size_type length_; |
| 644 | }; |
| 645 | |
| 646 | // This large function is defined inline so that in a fairly common case where |
| 647 | // one of the arguments is a literal, the compiler can elide a lot of the |
| 648 | // following comparisons. |
| 649 | constexpr bool operator==(string_view x, string_view y) noexcept { |
| 650 | return x.size() == y.size() && |
| 651 | (x.empty() || |
| 652 | ABSL_INTERNAL_STRING_VIEW_MEMCMP(x.data(), y.data(), x.size()) == 0); |
| 653 | } |
| 654 | |
| 655 | constexpr bool operator!=(string_view x, string_view y) noexcept { |
| 656 | return !(x == y); |
| 657 | } |
| 658 | |
| 659 | constexpr bool operator<(string_view x, string_view y) noexcept { |
| 660 | return x.compare(x: y) < 0; |
| 661 | } |
| 662 | |
| 663 | constexpr bool operator>(string_view x, string_view y) noexcept { |
| 664 | return y < x; |
| 665 | } |
| 666 | |
| 667 | constexpr bool operator<=(string_view x, string_view y) noexcept { |
| 668 | return !(y < x); |
| 669 | } |
| 670 | |
| 671 | constexpr bool operator>=(string_view x, string_view y) noexcept { |
| 672 | return !(x < y); |
| 673 | } |
| 674 | |
| 675 | // IO Insertion Operator |
| 676 | std::ostream& operator<<(std::ostream& o, string_view piece); |
| 677 | |
| 678 | ABSL_NAMESPACE_END |
| 679 | } // namespace absl |
| 680 | |
| 681 | #undef ABSL_INTERNAL_STRING_VIEW_CXX14_CONSTEXPR |
| 682 | #undef ABSL_INTERNAL_STRING_VIEW_MEMCMP |
| 683 | |
| 684 | #endif // ABSL_USES_STD_STRING_VIEW |
| 685 | |
| 686 | namespace absl { |
| 687 | ABSL_NAMESPACE_BEGIN |
| 688 | |
| 689 | // ClippedSubstr() |
| 690 | // |
| 691 | // Like `s.substr(pos, n)`, but clips `pos` to an upper bound of `s.size()`. |
| 692 | // Provided because std::string_view::substr throws if `pos > size()` |
| 693 | inline string_view ClippedSubstr(string_view s, size_t pos, |
| 694 | size_t n = string_view::npos) { |
| 695 | pos = (std::min)(a: pos, b: static_cast<size_t>(s.size())); |
| 696 | return s.substr(pos, n); |
| 697 | } |
| 698 | |
| 699 | // NullSafeStringView() |
| 700 | // |
| 701 | // Creates an `absl::string_view` from a pointer `p` even if it's null-valued. |
| 702 | // This function should be used where an `absl::string_view` can be created from |
| 703 | // a possibly-null pointer. |
| 704 | constexpr string_view NullSafeStringView(const char* p) { |
| 705 | return p ? string_view(p) : string_view(); |
| 706 | } |
| 707 | |
| 708 | ABSL_NAMESPACE_END |
| 709 | } // namespace absl |
| 710 | |
| 711 | #endif // ABSL_STRINGS_STRING_VIEW_H_ |
| 712 | |