tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

string_view.h (29699B)


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