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