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