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