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