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