xref: /llvm-project/libcxx/include/string_view (revision b9a2658a3e8bd13b0f9e7a8a440832a95b377216)
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_STRING_VIEW
11#define _LIBCPP_STRING_VIEW
12
13// clang-format off
14
15/*
16
17    string_view synopsis
18
19#include <compare>
20
21namespace std {
22
23    // 7.2, Class template basic_string_view
24    template<class charT, class traits = char_traits<charT>>
25        class basic_string_view;
26
27    template<class charT, class traits>
28    inline constexpr bool ranges::enable_view<basic_string_view<charT, traits>> = true;
29
30    template<class charT, class traits>
31    inline constexpr bool ranges::enable_borrowed_range<basic_string_view<charT, traits>> = true;  // C++20
32
33    // 7.9, basic_string_view non-member comparison functions
34    template<class charT, class traits>
35    constexpr bool operator==(basic_string_view<charT, traits> x,
36                              basic_string_view<charT, traits> y) noexcept;
37    template<class charT, class traits>                                                            // Removed in C++20
38    constexpr bool operator!=(basic_string_view<charT, traits> x,
39                              basic_string_view<charT, traits> y) noexcept;
40    template<class charT, class traits>                                                            // Removed in C++20
41    constexpr bool operator< (basic_string_view<charT, traits> x,
42                                 basic_string_view<charT, traits> y) noexcept;
43    template<class charT, class traits>                                                            // Removed in C++20
44    constexpr bool operator> (basic_string_view<charT, traits> x,
45                              basic_string_view<charT, traits> y) noexcept;
46    template<class charT, class traits>                                                            // Removed in C++20
47    constexpr bool operator<=(basic_string_view<charT, traits> x,
48                                 basic_string_view<charT, traits> y) noexcept;
49    template<class charT, class traits>                                                            // Removed in C++20
50    constexpr bool operator>=(basic_string_view<charT, traits> x,
51                              basic_string_view<charT, traits> y) noexcept;
52    template<class charT, class traits>                                                            // Since C++20
53    constexpr see below operator<=>(basic_string_view<charT, traits> x,
54                                    basic_string_view<charT, traits> y) noexcept;
55
56    // see below, sufficient additional overloads of comparison functions
57
58    // 7.10, Inserters and extractors
59    template<class charT, class traits>
60      basic_ostream<charT, traits>&
61        operator<<(basic_ostream<charT, traits>& os,
62                   basic_string_view<charT, traits> str);
63
64    // basic_string_view typedef names
65    typedef basic_string_view<char> string_view;
66    typedef basic_string_view<char8_t> u8string_view; // C++20
67    typedef basic_string_view<char16_t> u16string_view;
68    typedef basic_string_view<char32_t> u32string_view;
69    typedef basic_string_view<wchar_t> wstring_view;
70
71    template<class charT, class traits = char_traits<charT>>
72    class basic_string_view {
73      public:
74      // types
75      typedef traits traits_type;
76      typedef charT value_type;
77      typedef charT* pointer;
78      typedef const charT* const_pointer;
79      typedef charT& reference;
80      typedef const charT& const_reference;
81      typedef implementation-defined const_iterator;
82      typedef const_iterator iterator;
83      typedef reverse_iterator<const_iterator> const_reverse_iterator;
84      typedef const_reverse_iterator reverse_iterator;
85      typedef size_t size_type;
86      typedef ptrdiff_t difference_type;
87      static constexpr size_type npos = size_type(-1);
88
89      // 7.3, basic_string_view constructors and assignment operators
90      constexpr basic_string_view() noexcept;
91      constexpr basic_string_view(const basic_string_view&) noexcept = default;
92      basic_string_view& operator=(const basic_string_view&) noexcept = default;
93      template<class Allocator>
94      constexpr basic_string_view(const charT* str);
95      basic_string_view(nullptr_t) = delete; // C++23
96      constexpr basic_string_view(const charT* str, size_type len);
97      template <class It, class End>
98      constexpr basic_string_view(It begin, End end); // C++20
99      template <class Range>
100      constexpr basic_string_view(Range&& r); // C++23
101
102      // 7.4, basic_string_view iterator support
103      constexpr const_iterator begin() const noexcept;
104      constexpr const_iterator end() const noexcept;
105      constexpr const_iterator cbegin() const noexcept;
106      constexpr const_iterator cend() const noexcept;
107      const_reverse_iterator rbegin() const noexcept;
108      const_reverse_iterator rend() const noexcept;
109      const_reverse_iterator crbegin() const noexcept;
110      const_reverse_iterator crend() const noexcept;
111
112      // 7.5, basic_string_view capacity
113      constexpr size_type size() const noexcept;
114      constexpr size_type length() const noexcept;
115      constexpr size_type max_size() const noexcept;
116      constexpr bool empty() const noexcept;
117
118      // 7.6, basic_string_view element access
119      constexpr const_reference operator[](size_type pos) const;
120      constexpr const_reference at(size_type pos) const;
121      constexpr const_reference front() const;
122      constexpr const_reference back() const;
123      constexpr const_pointer data() const noexcept;
124
125      // 7.7, basic_string_view modifiers
126      constexpr void remove_prefix(size_type n);
127      constexpr void remove_suffix(size_type n);
128      constexpr void swap(basic_string_view& s) noexcept;
129
130      size_type copy(charT* s, size_type n, size_type pos = 0) const;  // constexpr in C++20
131
132      constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const;
133      constexpr int compare(basic_string_view s) const noexcept;
134      constexpr int compare(size_type pos1, size_type n1, basic_string_view s) const;
135      constexpr int compare(size_type pos1, size_type n1,
136                            basic_string_view s, size_type pos2, size_type n2) const;
137      constexpr int compare(const charT* s) const;
138      constexpr int compare(size_type pos1, size_type n1, const charT* s) const;
139      constexpr int compare(size_type pos1, size_type n1,
140                            const charT* s, size_type n2) const;
141      constexpr size_type find(basic_string_view s, size_type pos = 0) const noexcept;
142      constexpr size_type find(charT c, size_type pos = 0) const noexcept;
143      constexpr size_type find(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
144      constexpr size_type find(const charT* s, size_type pos = 0) const noexcept; // noexcept as an extension
145      constexpr size_type rfind(basic_string_view s, size_type pos = npos) const noexcept;
146      constexpr size_type rfind(charT c, size_type pos = npos) const noexcept;
147      constexpr size_type rfind(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
148      constexpr size_type rfind(const charT* s, size_type pos = npos) const noexcept; // noexcept as an extension
149      constexpr size_type find_first_of(basic_string_view s, size_type pos = 0) const noexcept;
150      constexpr size_type find_first_of(charT c, size_type pos = 0) const noexcept;
151      constexpr size_type find_first_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
152      constexpr size_type find_first_of(const charT* s, size_type pos = 0) const noexcept; // noexcept as an extension
153      constexpr size_type find_last_of(basic_string_view s, size_type pos = npos) const noexcept;
154      constexpr size_type find_last_of(charT c, size_type pos = npos) const noexcept;
155      constexpr size_type find_last_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
156      constexpr size_type find_last_of(const charT* s, size_type pos = npos) const noexcept; // noexcept as an extension
157      constexpr size_type find_first_not_of(basic_string_view s, size_type pos = 0) const noexcept;
158      constexpr size_type find_first_not_of(charT c, size_type pos = 0) const noexcept;
159      constexpr size_type find_first_not_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
160      constexpr size_type find_first_not_of(const charT* s, size_type pos = 0) const noexcept; // noexcept as an extension
161      constexpr size_type find_last_not_of(basic_string_view s, size_type pos = npos) const noexcept;
162      constexpr size_type find_last_not_of(charT c, size_type pos = npos) const noexcept;
163      constexpr size_type find_last_not_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
164      constexpr size_type find_last_not_of(const charT* s, size_type pos = npos) const noexcept; // noexcept as an extension
165
166      constexpr bool starts_with(basic_string_view s) const noexcept; // C++20
167      constexpr bool starts_with(charT c) const noexcept;             // C++20
168      constexpr bool starts_with(const charT* s) const;               // C++20
169      constexpr bool ends_with(basic_string_view s) const noexcept;   // C++20
170      constexpr bool ends_with(charT c) const noexcept;               // C++20
171      constexpr bool ends_with(const charT* s) const;                 // C++20
172
173      constexpr bool contains(basic_string_view s) const noexcept; // C++23
174      constexpr bool contains(charT c) const noexcept;             // C++23
175      constexpr bool contains(const charT* s) const;               // C++23
176
177     private:
178      const_pointer data_;  // exposition only
179      size_type     size_;  // exposition only
180    };
181
182  // basic_string_view deduction guides
183  template<class It, class End>
184    basic_string_view(It, End) -> basic_string_view<iter_value_t<It>>; // C++20
185  template<class Range>
186    basic_string_view(Range&&) -> basic_string_view<ranges::range_value_t<Range>>; // C++23
187
188  // 7.11, Hash support
189  template <class T> struct hash;
190  template <> struct hash<string_view>;
191  template <> struct hash<u8string_view>; // C++20
192  template <> struct hash<u16string_view>;
193  template <> struct hash<u32string_view>;
194  template <> struct hash<wstring_view>;
195
196  constexpr basic_string_view<char>     operator""sv(const char *str,     size_t len) noexcept;
197  constexpr basic_string_view<wchar_t>  operator""sv(const wchar_t *str,  size_t len) noexcept;
198  constexpr basic_string_view<char8_t>  operator""sv(const char8_t *str,  size_t len) noexcept; // C++20
199  constexpr basic_string_view<char16_t> operator""sv(const char16_t *str, size_t len) noexcept;
200  constexpr basic_string_view<char32_t> operator""sv(const char32_t *str, size_t len) noexcept;
201
202}  // namespace std
203
204*/
205
206// clang-format on
207
208#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
209#  include <__cxx03/string_view>
210#else
211#  include <__algorithm/min.h>
212#  include <__assert>
213#  include <__config>
214#  include <__cstddef/nullptr_t.h>
215#  include <__cstddef/ptrdiff_t.h>
216#  include <__cstddef/size_t.h>
217#  include <__functional/hash.h>
218#  include <__functional/unary_function.h>
219#  include <__fwd/ostream.h>
220#  include <__fwd/string.h>
221#  include <__fwd/string_view.h>
222#  include <__iterator/bounded_iter.h>
223#  include <__iterator/concepts.h>
224#  include <__iterator/iterator_traits.h>
225#  include <__iterator/reverse_iterator.h>
226#  include <__iterator/wrap_iter.h>
227#  include <__memory/pointer_traits.h>
228#  include <__ranges/concepts.h>
229#  include <__ranges/data.h>
230#  include <__ranges/enable_borrowed_range.h>
231#  include <__ranges/enable_view.h>
232#  include <__ranges/size.h>
233#  include <__string/char_traits.h>
234#  include <__type_traits/is_array.h>
235#  include <__type_traits/is_convertible.h>
236#  include <__type_traits/is_same.h>
237#  include <__type_traits/is_standard_layout.h>
238#  include <__type_traits/is_trivial.h>
239#  include <__type_traits/remove_cvref.h>
240#  include <__type_traits/remove_reference.h>
241#  include <__type_traits/type_identity.h>
242#  include <iosfwd>
243#  include <limits>
244#  include <stdexcept>
245#  include <version>
246
247// standard-mandated includes
248
249// [iterator.range]
250#  include <__iterator/access.h>
251#  include <__iterator/data.h>
252#  include <__iterator/empty.h>
253#  include <__iterator/reverse_access.h>
254#  include <__iterator/size.h>
255
256// [string.view.synop]
257#  include <compare>
258
259#  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
260#    pragma GCC system_header
261#  endif
262
263_LIBCPP_PUSH_MACROS
264#  include <__undef_macros>
265
266_LIBCPP_BEGIN_NAMESPACE_STD
267
268// TODO: This is a workaround for some vendors to carry a downstream diff to accept `nullptr` in
269//       string_view constructors. This can be refactored when this exact form isn't needed anymore.
270template <class _Traits>
271_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR inline size_t
272__char_traits_length_checked(const typename _Traits::char_type* __s) _NOEXCEPT {
273  // This needs to be a single statement for C++11 constexpr
274  return _LIBCPP_ASSERT_NON_NULL(
275             __s != nullptr, "null pointer passed to non-null argument of char_traits<...>::length"),
276         _Traits::length(__s);
277}
278
279template <class _CharT, class _Traits>
280class basic_string_view {
281public:
282  // types
283  using traits_type     = _Traits;
284  using value_type      = _CharT;
285  using pointer         = _CharT*;
286  using const_pointer   = const _CharT*;
287  using reference       = _CharT&;
288  using const_reference = const _CharT&;
289#  if defined(_LIBCPP_ABI_BOUNDED_ITERATORS)
290  using const_iterator = __bounded_iter<const_pointer>;
291#  elif defined(_LIBCPP_ABI_USE_WRAP_ITER_IN_STD_STRING_VIEW)
292  using const_iterator = __wrap_iter<const_pointer>;
293#  else
294  using const_iterator = const_pointer;
295#  endif
296  using iterator                                = const_iterator;
297  using const_reverse_iterator                  = std::reverse_iterator<const_iterator>;
298  using reverse_iterator                        = const_reverse_iterator;
299  using size_type                               = size_t;
300  using difference_type                         = ptrdiff_t;
301  static _LIBCPP_CONSTEXPR const size_type npos = -1; // size_type(-1);
302
303  static_assert(!is_array<value_type>::value, "Character type of basic_string_view must not be an array");
304  static_assert(is_standard_layout<value_type>::value, "Character type of basic_string_view must be standard-layout");
305  static_assert(is_trivial<value_type>::value, "Character type of basic_string_view must be trivial");
306  static_assert(is_same<_CharT, typename traits_type::char_type>::value,
307                "traits_type::char_type must be the same type as CharT");
308
309  // [string.view.cons], construct/copy
310  _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI basic_string_view() _NOEXCEPT : __data_(nullptr), __size_(0) {}
311
312  _LIBCPP_HIDE_FROM_ABI basic_string_view(const basic_string_view&) _NOEXCEPT = default;
313
314  _LIBCPP_HIDE_FROM_ABI basic_string_view& operator=(const basic_string_view&) _NOEXCEPT = default;
315
316  _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI basic_string_view(const _CharT* __s, size_type __len) _NOEXCEPT
317      : __data_(__s),
318        __size_(__len) {
319#  if _LIBCPP_STD_VER >= 14
320    // Allocations must fit in `ptrdiff_t` for pointer arithmetic to work. If `__len` exceeds it, the input
321    // range could not have been valid. Most likely the caller underflowed some arithmetic and inadvertently
322    // passed in a negative length.
323    _LIBCPP_ASSERT_VALID_INPUT_RANGE(
324        __len <= static_cast<size_type>(numeric_limits<difference_type>::max()),
325        "string_view::string_view(_CharT *, size_t): length does not fit in difference_type");
326    _LIBCPP_ASSERT_NON_NULL(
327        __len == 0 || __s != nullptr, "string_view::string_view(_CharT *, size_t): received nullptr");
328#  endif
329  }
330
331#  if _LIBCPP_STD_VER >= 20
332  template <contiguous_iterator _It, sized_sentinel_for<_It> _End>
333    requires(is_same_v<iter_value_t<_It>, _CharT> && !is_convertible_v<_End, size_type>)
334  constexpr _LIBCPP_HIDE_FROM_ABI basic_string_view(_It __begin, _End __end)
335      : __data_(std::to_address(__begin)), __size_(__end - __begin) {
336    _LIBCPP_ASSERT_VALID_INPUT_RANGE(
337        (__end - __begin) >= 0, "std::string_view::string_view(iterator, sentinel) received invalid range");
338  }
339#  endif // _LIBCPP_STD_VER >= 20
340
341#  if _LIBCPP_STD_VER >= 23
342  template <class _Range>
343    requires(!is_same_v<remove_cvref_t<_Range>, basic_string_view> && ranges::contiguous_range<_Range> &&
344             ranges::sized_range<_Range> && is_same_v<ranges::range_value_t<_Range>, _CharT> &&
345             !is_convertible_v<_Range, const _CharT*> &&
346             (!requires(remove_cvref_t<_Range>& __d) { __d.operator std::basic_string_view<_CharT, _Traits>(); }))
347  constexpr explicit _LIBCPP_HIDE_FROM_ABI basic_string_view(_Range&& __r)
348      : __data_(ranges::data(__r)), __size_(ranges::size(__r)) {}
349#  endif // _LIBCPP_STD_VER >= 23
350
351  _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI basic_string_view(const _CharT* __s)
352      : __data_(__s), __size_(std::__char_traits_length_checked<_Traits>(__s)) {}
353
354#  if _LIBCPP_STD_VER >= 23
355  basic_string_view(nullptr_t) = delete;
356#  endif
357
358  // [string.view.iterators], iterators
359  _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT { return cbegin(); }
360
361  _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT { return cend(); }
362
363  _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT {
364#  ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
365    return std::__make_bounded_iter(data(), data(), data() + size());
366#  else
367    return const_iterator(__data_);
368#  endif
369  }
370
371  _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT {
372#  ifdef _LIBCPP_ABI_BOUNDED_ITERATORS
373    return std::__make_bounded_iter(data() + size(), data(), data() + size());
374#  else
375    return const_iterator(__data_ + __size_);
376#  endif
377  }
378
379  _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rbegin() const _NOEXCEPT {
380    return const_reverse_iterator(cend());
381  }
382
383  _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT {
384    return const_reverse_iterator(cbegin());
385  }
386
387  _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crbegin() const _NOEXCEPT {
388    return const_reverse_iterator(cend());
389  }
390
391  _LIBCPP_CONSTEXPR_SINCE_CXX17 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT {
392    return const_reverse_iterator(cbegin());
393  }
394
395  // [string.view.capacity], capacity
396  _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT { return __size_; }
397
398  _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI size_type length() const _NOEXCEPT { return __size_; }
399
400  _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
401    return numeric_limits<size_type>::max() / sizeof(value_type);
402  }
403
404  [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT { return __size_ == 0; }
405
406  // [string.view.access], element access
407  _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_reference operator[](size_type __pos) const _NOEXCEPT {
408    return _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__pos < size(), "string_view[] index out of bounds"), __data_[__pos];
409  }
410
411  _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_reference at(size_type __pos) const {
412    return __pos >= size() ? (__throw_out_of_range("string_view::at"), __data_[0]) : __data_[__pos];
413  }
414
415  _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_reference front() const _NOEXCEPT {
416    return _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string_view::front(): string is empty"), __data_[0];
417  }
418
419  _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_reference back() const _NOEXCEPT {
420    return _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(!empty(), "string_view::back(): string is empty"), __data_[__size_ - 1];
421  }
422
423  _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI const_pointer data() const _NOEXCEPT { return __data_; }
424
425  // [string.view.modifiers], modifiers:
426  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI void remove_prefix(size_type __n) _NOEXCEPT {
427    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n <= size(), "remove_prefix() can't remove more than size()");
428    __data_ += __n;
429    __size_ -= __n;
430  }
431
432  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI void remove_suffix(size_type __n) _NOEXCEPT {
433    _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__n <= size(), "remove_suffix() can't remove more than size()");
434    __size_ -= __n;
435  }
436
437  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI void swap(basic_string_view& __other) _NOEXCEPT {
438    const value_type* __p = __data_;
439    __data_               = __other.__data_;
440    __other.__data_       = __p;
441
442    size_type __sz  = __size_;
443    __size_         = __other.__size_;
444    __other.__size_ = __sz;
445  }
446
447  _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 size_type
448  copy(_CharT* __s, size_type __n, size_type __pos = 0) const {
449    if (__pos > size())
450      __throw_out_of_range("string_view::copy");
451    size_type __rlen = std::min(__n, size() - __pos);
452    _Traits::copy(__s, data() + __pos, __rlen);
453    return __rlen;
454  }
455
456  _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI basic_string_view substr(size_type __pos = 0, size_type __n = npos) const {
457    // Use the `__assume_valid` form of the constructor to avoid an unnecessary check. Any substring of a view is a
458    // valid view. In particular, `size()` is known to be smaller than `numeric_limits<difference_type>::max()`, so the
459    // new size is also smaller. See also https://github.com/llvm/llvm-project/issues/91634.
460    return __pos > size() ? (__throw_out_of_range("string_view::substr"), basic_string_view())
461                          : basic_string_view(__assume_valid(), data() + __pos, std::min(__n, size() - __pos));
462  }
463
464  _LIBCPP_CONSTEXPR_SINCE_CXX14 int compare(basic_string_view __sv) const _NOEXCEPT {
465    size_type __rlen = std::min(size(), __sv.size());
466    int __retval     = _Traits::compare(data(), __sv.data(), __rlen);
467    if (__retval == 0) // first __rlen chars matched
468      __retval = size() == __sv.size() ? 0 : (size() < __sv.size() ? -1 : 1);
469    return __retval;
470  }
471
472  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI int
473  compare(size_type __pos1, size_type __n1, basic_string_view __sv) const {
474    return substr(__pos1, __n1).compare(__sv);
475  }
476
477  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI int
478  compare(size_type __pos1, size_type __n1, basic_string_view __sv, size_type __pos2, size_type __n2) const {
479    return substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
480  }
481
482  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI int compare(const _CharT* __s) const _NOEXCEPT {
483    return compare(basic_string_view(__s));
484  }
485
486  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI int
487  compare(size_type __pos1, size_type __n1, const _CharT* __s) const {
488    return substr(__pos1, __n1).compare(basic_string_view(__s));
489  }
490
491  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI int
492  compare(size_type __pos1, size_type __n1, const _CharT* __s, size_type __n2) const {
493    return substr(__pos1, __n1).compare(basic_string_view(__s, __n2));
494  }
495
496  // find
497  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
498  find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT {
499    _LIBCPP_ASSERT_NON_NULL(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
500    return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __s.data(), __pos, __s.size());
501  }
502
503  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type find(_CharT __c, size_type __pos = 0) const _NOEXCEPT {
504    return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
505  }
506
507  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
508  find(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT {
509    _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string_view::find(): received nullptr");
510    return std::__str_find<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
511  }
512
513  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
514  find(const _CharT* __s, size_type __pos = 0) const _NOEXCEPT {
515    _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string_view::find(): received nullptr");
516    return std::__str_find<value_type, size_type, traits_type, npos>(
517        data(), size(), __s, __pos, traits_type::length(__s));
518  }
519
520  // rfind
521  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
522  rfind(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT {
523    _LIBCPP_ASSERT_NON_NULL(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
524    return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __s.data(), __pos, __s.size());
525  }
526
527  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
528  rfind(_CharT __c, size_type __pos = npos) const _NOEXCEPT {
529    return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
530  }
531
532  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
533  rfind(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT {
534    _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string_view::rfind(): received nullptr");
535    return std::__str_rfind<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
536  }
537
538  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
539  rfind(const _CharT* __s, size_type __pos = npos) const _NOEXCEPT {
540    _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string_view::rfind(): received nullptr");
541    return std::__str_rfind<value_type, size_type, traits_type, npos>(
542        data(), size(), __s, __pos, traits_type::length(__s));
543  }
544
545  // find_first_of
546  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
547  find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT {
548    _LIBCPP_ASSERT_NON_NULL(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_of(): received nullptr");
549    return std::__str_find_first_of<value_type, size_type, traits_type, npos>(
550        data(), size(), __s.data(), __pos, __s.size());
551  }
552
553  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
554  find_first_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT {
555    return find(__c, __pos);
556  }
557
558  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
559  find_first_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT {
560    _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string_view::find_first_of(): received nullptr");
561    return std::__str_find_first_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
562  }
563
564  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
565  find_first_of(const _CharT* __s, size_type __pos = 0) const _NOEXCEPT {
566    _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string_view::find_first_of(): received nullptr");
567    return std::__str_find_first_of<value_type, size_type, traits_type, npos>(
568        data(), size(), __s, __pos, traits_type::length(__s));
569  }
570
571  // find_last_of
572  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
573  find_last_of(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT {
574    _LIBCPP_ASSERT_NON_NULL(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_of(): received nullptr");
575    return std::__str_find_last_of<value_type, size_type, traits_type, npos>(
576        data(), size(), __s.data(), __pos, __s.size());
577  }
578
579  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
580  find_last_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT {
581    return rfind(__c, __pos);
582  }
583
584  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
585  find_last_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT {
586    _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string_view::find_last_of(): received nullptr");
587    return std::__str_find_last_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
588  }
589
590  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
591  find_last_of(const _CharT* __s, size_type __pos = npos) const _NOEXCEPT {
592    _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string_view::find_last_of(): received nullptr");
593    return std::__str_find_last_of<value_type, size_type, traits_type, npos>(
594        data(), size(), __s, __pos, traits_type::length(__s));
595  }
596
597  // find_first_not_of
598  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
599  find_first_not_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT {
600    _LIBCPP_ASSERT_NON_NULL(
601        __s.size() == 0 || __s.data() != nullptr, "string_view::find_first_not_of(): received nullptr");
602    return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(
603        data(), size(), __s.data(), __pos, __s.size());
604  }
605
606  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
607  find_first_not_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT {
608    return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
609  }
610
611  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
612  find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT {
613    _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string_view::find_first_not_of(): received nullptr");
614    return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
615  }
616
617  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
618  find_first_not_of(const _CharT* __s, size_type __pos = 0) const _NOEXCEPT {
619    _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string_view::find_first_not_of(): received nullptr");
620    return std::__str_find_first_not_of<value_type, size_type, traits_type, npos>(
621        data(), size(), __s, __pos, traits_type::length(__s));
622  }
623
624  // find_last_not_of
625  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
626  find_last_not_of(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT {
627    _LIBCPP_ASSERT_NON_NULL(
628        __s.size() == 0 || __s.data() != nullptr, "string_view::find_last_not_of(): received nullptr");
629    return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(
630        data(), size(), __s.data(), __pos, __s.size());
631  }
632
633  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
634  find_last_not_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT {
635    return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(data(), size(), __c, __pos);
636  }
637
638  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
639  find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT {
640    _LIBCPP_ASSERT_NON_NULL(__n == 0 || __s != nullptr, "string_view::find_last_not_of(): received nullptr");
641    return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(data(), size(), __s, __pos, __n);
642  }
643
644  _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI size_type
645  find_last_not_of(const _CharT* __s, size_type __pos = npos) const _NOEXCEPT {
646    _LIBCPP_ASSERT_NON_NULL(__s != nullptr, "string_view::find_last_not_of(): received nullptr");
647    return std::__str_find_last_not_of<value_type, size_type, traits_type, npos>(
648        data(), size(), __s, __pos, traits_type::length(__s));
649  }
650
651#  if _LIBCPP_STD_VER >= 20
652  constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(basic_string_view __s) const noexcept {
653    return size() >= __s.size() && compare(0, __s.size(), __s) == 0;
654  }
655
656  constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(value_type __c) const noexcept {
657    return !empty() && _Traits::eq(front(), __c);
658  }
659
660  constexpr _LIBCPP_HIDE_FROM_ABI bool starts_with(const value_type* __s) const noexcept {
661    return starts_with(basic_string_view(__s));
662  }
663
664  constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(basic_string_view __s) const noexcept {
665    return size() >= __s.size() && compare(size() - __s.size(), npos, __s) == 0;
666  }
667
668  constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(value_type __c) const noexcept {
669    return !empty() && _Traits::eq(back(), __c);
670  }
671
672  constexpr _LIBCPP_HIDE_FROM_ABI bool ends_with(const value_type* __s) const noexcept {
673    return ends_with(basic_string_view(__s));
674  }
675#  endif
676
677#  if _LIBCPP_STD_VER >= 23
678  constexpr _LIBCPP_HIDE_FROM_ABI bool contains(basic_string_view __sv) const noexcept { return find(__sv) != npos; }
679
680  constexpr _LIBCPP_HIDE_FROM_ABI bool contains(value_type __c) const noexcept { return find(__c) != npos; }
681
682  constexpr _LIBCPP_HIDE_FROM_ABI bool contains(const value_type* __s) const { return find(__s) != npos; }
683#  endif
684
685private:
686  struct __assume_valid {};
687
688  // This is the same as the pointer and length constructor, but without the additional hardening checks. It is intended
689  // for use within the class, when the class invariants already guarantee the resulting object is valid. The compiler
690  // usually cannot eliminate the redundant checks because it does not know class invariants.
691  _LIBCPP_CONSTEXPR _LIBCPP_HIDE_FROM_ABI
692  basic_string_view(__assume_valid, const _CharT* __s, size_type __len) _NOEXCEPT
693      : __data_(__s),
694        __size_(__len) {}
695
696  const value_type* __data_;
697  size_type __size_;
698
699  template <class, class, class>
700  friend class basic_string;
701};
702_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(basic_string_view);
703
704#  if _LIBCPP_STD_VER >= 20
705template <class _CharT, class _Traits>
706inline constexpr bool ranges::enable_view<basic_string_view<_CharT, _Traits>> = true;
707
708template <class _CharT, class _Traits>
709inline constexpr bool ranges::enable_borrowed_range<basic_string_view<_CharT, _Traits> > = true;
710#  endif // _LIBCPP_STD_VER >= 20
711
712// [string.view.deduct]
713
714#  if _LIBCPP_STD_VER >= 20
715template <contiguous_iterator _It, sized_sentinel_for<_It> _End>
716basic_string_view(_It, _End) -> basic_string_view<iter_value_t<_It>>;
717#  endif // _LIBCPP_STD_VER >= 20
718
719#  if _LIBCPP_STD_VER >= 23
720template <ranges::contiguous_range _Range>
721basic_string_view(_Range) -> basic_string_view<ranges::range_value_t<_Range>>;
722#  endif
723
724// [string.view.comparison]
725
726// The dummy default template parameters are used to work around a MSVC issue with mangling, see VSO-409326 for details.
727// This applies to the other sufficient overloads below for the other comparison operators.
728template <class _CharT, class _Traits, int = 1>
729_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
730operator==(basic_string_view<_CharT, _Traits> __lhs,
731           __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
732  if (__lhs.size() != __rhs.size())
733    return false;
734  return __lhs.compare(__rhs) == 0;
735}
736
737#  if _LIBCPP_STD_VER >= 20
738
739template <class _CharT, class _Traits>
740_LIBCPP_HIDE_FROM_ABI constexpr auto operator<=>(basic_string_view<_CharT, _Traits> __lhs,
741                                                 type_identity_t<basic_string_view<_CharT, _Traits>> __rhs) noexcept {
742  if constexpr (requires { typename _Traits::comparison_category; }) {
743    // [string.view]/4
744    static_assert(
745        __comparison_category<typename _Traits::comparison_category>, "return type is not a comparison category type");
746    return static_cast<typename _Traits::comparison_category>(__lhs.compare(__rhs) <=> 0);
747  } else {
748    return static_cast<weak_ordering>(__lhs.compare(__rhs) <=> 0);
749  }
750}
751
752#  else
753
754// operator ==
755
756template <class _CharT, class _Traits>
757_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
758operator==(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
759  if (__lhs.size() != __rhs.size())
760    return false;
761  return __lhs.compare(__rhs) == 0;
762}
763
764template <class _CharT, class _Traits, int = 2>
765_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
766operator==(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
767           basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
768  return __lhs == __rhs;
769}
770
771// operator !=
772template <class _CharT, class _Traits>
773_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
774operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
775  return !(__lhs == __rhs);
776}
777
778template <class _CharT, class _Traits, int = 1>
779_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
780operator!=(basic_string_view<_CharT, _Traits> __lhs,
781           __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
782  return !(__lhs == __rhs);
783}
784
785template <class _CharT, class _Traits, int = 2>
786_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
787operator!=(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
788           basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
789  return !(__lhs == __rhs);
790}
791
792// operator <
793template <class _CharT, class _Traits>
794_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
795operator<(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
796  return __lhs.compare(__rhs) < 0;
797}
798
799template <class _CharT, class _Traits, int = 1>
800_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
801operator<(basic_string_view<_CharT, _Traits> __lhs,
802          __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
803  return __lhs.compare(__rhs) < 0;
804}
805
806template <class _CharT, class _Traits, int = 2>
807_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
808operator<(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
809          basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
810  return __lhs.compare(__rhs) < 0;
811}
812
813// operator >
814template <class _CharT, class _Traits>
815_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
816operator>(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
817  return __lhs.compare(__rhs) > 0;
818}
819
820template <class _CharT, class _Traits, int = 1>
821_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
822operator>(basic_string_view<_CharT, _Traits> __lhs,
823          __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
824  return __lhs.compare(__rhs) > 0;
825}
826
827template <class _CharT, class _Traits, int = 2>
828_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
829operator>(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
830          basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
831  return __lhs.compare(__rhs) > 0;
832}
833
834// operator <=
835template <class _CharT, class _Traits>
836_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
837operator<=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
838  return __lhs.compare(__rhs) <= 0;
839}
840
841template <class _CharT, class _Traits, int = 1>
842_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
843operator<=(basic_string_view<_CharT, _Traits> __lhs,
844           __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
845  return __lhs.compare(__rhs) <= 0;
846}
847
848template <class _CharT, class _Traits, int = 2>
849_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
850operator<=(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
851           basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
852  return __lhs.compare(__rhs) <= 0;
853}
854
855// operator >=
856template <class _CharT, class _Traits>
857_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
858operator>=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
859  return __lhs.compare(__rhs) >= 0;
860}
861
862template <class _CharT, class _Traits, int = 1>
863_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
864operator>=(basic_string_view<_CharT, _Traits> __lhs,
865           __type_identity_t<basic_string_view<_CharT, _Traits> > __rhs) _NOEXCEPT {
866  return __lhs.compare(__rhs) >= 0;
867}
868
869template <class _CharT, class _Traits, int = 2>
870_LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool
871operator>=(__type_identity_t<basic_string_view<_CharT, _Traits> > __lhs,
872           basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT {
873  return __lhs.compare(__rhs) >= 0;
874}
875
876#  endif //  _LIBCPP_STD_VER >= 20
877
878template <class _CharT, class _Traits>
879_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
880operator<<(basic_ostream<_CharT, _Traits>& __os, basic_string_view<_CharT, _Traits> __str);
881
882// [string.view.hash]
883template <class _CharT>
884struct __string_view_hash : public __unary_function<basic_string_view<_CharT, char_traits<_CharT> >, size_t> {
885  _LIBCPP_HIDE_FROM_ABI size_t operator()(const basic_string_view<_CharT, char_traits<_CharT> > __val) const _NOEXCEPT {
886    return std::__do_string_hash(__val.data(), __val.data() + __val.size());
887  }
888};
889
890template <>
891struct hash<basic_string_view<char, char_traits<char> > > : __string_view_hash<char> {};
892
893#  if _LIBCPP_HAS_CHAR8_T
894template <>
895struct hash<basic_string_view<char8_t, char_traits<char8_t> > > : __string_view_hash<char8_t> {};
896#  endif
897
898template <>
899struct hash<basic_string_view<char16_t, char_traits<char16_t> > > : __string_view_hash<char16_t> {};
900
901template <>
902struct hash<basic_string_view<char32_t, char_traits<char32_t> > > : __string_view_hash<char32_t> {};
903
904#  if _LIBCPP_HAS_WIDE_CHARACTERS
905template <>
906struct hash<basic_string_view<wchar_t, char_traits<wchar_t> > > : __string_view_hash<wchar_t> {};
907#  endif
908
909#  if _LIBCPP_STD_VER >= 14
910inline namespace literals {
911inline namespace string_view_literals {
912inline _LIBCPP_HIDE_FROM_ABI constexpr basic_string_view<char> operator""sv(const char* __str, size_t __len) noexcept {
913  return basic_string_view<char>(__str, __len);
914}
915
916#    if _LIBCPP_HAS_WIDE_CHARACTERS
917inline _LIBCPP_HIDE_FROM_ABI constexpr basic_string_view<wchar_t>
918operator""sv(const wchar_t* __str, size_t __len) noexcept {
919  return basic_string_view<wchar_t>(__str, __len);
920}
921#    endif
922
923#    if _LIBCPP_HAS_CHAR8_T
924inline _LIBCPP_HIDE_FROM_ABI constexpr basic_string_view<char8_t>
925operator""sv(const char8_t* __str, size_t __len) noexcept {
926  return basic_string_view<char8_t>(__str, __len);
927}
928#    endif
929
930inline _LIBCPP_HIDE_FROM_ABI constexpr basic_string_view<char16_t>
931operator""sv(const char16_t* __str, size_t __len) noexcept {
932  return basic_string_view<char16_t>(__str, __len);
933}
934
935inline _LIBCPP_HIDE_FROM_ABI constexpr basic_string_view<char32_t>
936operator""sv(const char32_t* __str, size_t __len) noexcept {
937  return basic_string_view<char32_t>(__str, __len);
938}
939} // namespace string_view_literals
940} // namespace literals
941#  endif
942_LIBCPP_END_NAMESPACE_STD
943
944_LIBCPP_POP_MACROS
945
946#  if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
947#    include <algorithm>
948#    include <concepts>
949#    include <cstdlib>
950#    include <iterator>
951#    include <type_traits>
952#  endif
953#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
954
955#endif // _LIBCPP_STRING_VIEW
956