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