blob: 373b3943b9e02d70421933827270244d94a9ab2e [file] [log] [blame]
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Marshall Clowdf63a6d2016-07-21 05:31:24 +00003//
Chandler Carruth7642bb12019-01-19 08:50:56 +00004// 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
Marshall Clowdf63a6d2016-07-21 05:31:24 +00007//
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
Mark de Weveraf59b2d2020-11-24 18:08:02 +010022 template<class charT, class traits>
Christopher Di Bella92072c22021-05-14 06:20:07 +000023 inline constexpr bool ranges::enable_view<basic_string_view<charT, traits>> = true;
24
25 template<class charT, class traits>
Mark de Weveraf59b2d2020-11-24 18:08:02 +010026 inline constexpr bool ranges::enable_borrowed_range<basic_string_view<charT, traits>> = true; // C++20
27
Marshall Clowdf63a6d2016-07-21 05:31:24 +000028 // 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;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010057 typedef basic_string_view<char8_t> u8string_view; // C++20
Marshall Clowdf63a6d2016-07-21 05:31:24 +000058 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);
Marek Kurdej90d79712021-07-27 16:16:21 +020086 basic_string_view(nullptr_t) = delete; // C++2b
Marshall Clowdf63a6d2016-07-21 05:31:24 +000087 constexpr basic_string_view(const charT* str, size_type len);
Joe Loser28dcceb2021-10-06 14:17:02 -040088 template <class It, class End>
89 constexpr basic_string_view(It begin, End end); // C++20
Marshall Clowdf63a6d2016-07-21 05:31:24 +000090
91 // 7.4, basic_string_view iterator support
92 constexpr const_iterator begin() const noexcept;
93 constexpr const_iterator end() const noexcept;
94 constexpr const_iterator cbegin() const noexcept;
95 constexpr const_iterator cend() const noexcept;
96 const_reverse_iterator rbegin() const noexcept;
97 const_reverse_iterator rend() const noexcept;
98 const_reverse_iterator crbegin() const noexcept;
99 const_reverse_iterator crend() const noexcept;
100
101 // 7.5, basic_string_view capacity
102 constexpr size_type size() const noexcept;
103 constexpr size_type length() const noexcept;
104 constexpr size_type max_size() const noexcept;
105 constexpr bool empty() const noexcept;
106
107 // 7.6, basic_string_view element access
108 constexpr const_reference operator[](size_type pos) const;
109 constexpr const_reference at(size_type pos) const;
110 constexpr const_reference front() const;
111 constexpr const_reference back() const;
112 constexpr const_pointer data() const noexcept;
113
114 // 7.7, basic_string_view modifiers
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000115 constexpr void remove_prefix(size_type n);
116 constexpr void remove_suffix(size_type n);
117 constexpr void swap(basic_string_view& s) noexcept;
118
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500119 size_type copy(charT* s, size_type n, size_type pos = 0) const; // constexpr in C++20
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000120
121 constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const;
122 constexpr int compare(basic_string_view s) const noexcept;
123 constexpr int compare(size_type pos1, size_type n1, basic_string_view s) const;
124 constexpr int compare(size_type pos1, size_type n1,
125 basic_string_view s, size_type pos2, size_type n2) const;
126 constexpr int compare(const charT* s) const;
127 constexpr int compare(size_type pos1, size_type n1, const charT* s) const;
128 constexpr int compare(size_type pos1, size_type n1,
129 const charT* s, size_type n2) const;
130 constexpr size_type find(basic_string_view s, size_type pos = 0) const noexcept;
131 constexpr size_type find(charT c, size_type pos = 0) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800132 constexpr size_type find(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
133 constexpr size_type find(const charT* s, size_type pos = 0) const noexcept; // noexcept as an extension
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000134 constexpr size_type rfind(basic_string_view s, size_type pos = npos) const noexcept;
135 constexpr size_type rfind(charT c, size_type pos = npos) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800136 constexpr size_type rfind(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
137 constexpr size_type rfind(const charT* s, size_type pos = npos) const noexcept; // noexcept as an extension
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000138 constexpr size_type find_first_of(basic_string_view s, size_type pos = 0) const noexcept;
139 constexpr size_type find_first_of(charT c, size_type pos = 0) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800140 constexpr size_type find_first_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
141 constexpr size_type find_first_of(const charT* s, size_type pos = 0) const noexcept; // noexcept as an extension
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000142 constexpr size_type find_last_of(basic_string_view s, size_type pos = npos) const noexcept;
143 constexpr size_type find_last_of(charT c, size_type pos = npos) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800144 constexpr size_type find_last_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
145 constexpr size_type find_last_of(const charT* s, size_type pos = npos) const noexcept; // noexcept as an extension
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000146 constexpr size_type find_first_not_of(basic_string_view s, size_type pos = 0) const noexcept;
147 constexpr size_type find_first_not_of(charT c, size_type pos = 0) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800148 constexpr size_type find_first_not_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
149 constexpr size_type find_first_not_of(const charT* s, size_type pos = 0) const noexcept; // noexcept as an extension
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000150 constexpr size_type find_last_not_of(basic_string_view s, size_type pos = npos) const noexcept;
151 constexpr size_type find_last_not_of(charT c, size_type pos = npos) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800152 constexpr size_type find_last_not_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
153 constexpr size_type find_last_not_of(const charT* s, size_type pos = npos) const noexcept; // noexcept as an extension
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000154
Marek Kurdej24b4c512021-01-07 12:29:04 +0100155 constexpr bool starts_with(basic_string_view s) const noexcept; // C++20
156 constexpr bool starts_with(charT c) const noexcept; // C++20
157 constexpr bool starts_with(const charT* s) const; // C++20
158 constexpr bool ends_with(basic_string_view s) const noexcept; // C++20
159 constexpr bool ends_with(charT c) const noexcept; // C++20
160 constexpr bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000161
Wim Leflere023c3542021-01-19 14:33:30 -0500162 constexpr bool contains(basic_string_view s) const noexcept; // C++2b
163 constexpr bool contains(charT c) const noexcept; // C++2b
164 constexpr bool contains(const charT* s) const; // C++2b
165
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000166 private:
167 const_pointer data_; // exposition only
168 size_type size_; // exposition only
169 };
170
Joe Loser28dcceb2021-10-06 14:17:02 -0400171 // basic_string_view deduction guides
172 template<class It, class End>
173 basic_string_view(It, End) -> basic_string_view<iter_value_t<It>>; // C++20
174
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000175 // 7.11, Hash support
176 template <class T> struct hash;
177 template <> struct hash<string_view>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100178 template <> struct hash<u8string_view>; // C++20
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000179 template <> struct hash<u16string_view>;
180 template <> struct hash<u32string_view>;
181 template <> struct hash<wstring_view>;
182
Marshall Clowabe6daf2017-10-24 14:06:00 +0000183 constexpr basic_string_view<char> operator "" sv( const char *str, size_t len ) noexcept;
184 constexpr basic_string_view<wchar_t> operator "" sv( const wchar_t *str, size_t len ) noexcept;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100185 constexpr basic_string_view<char8_t> operator "" sv( const char8_t *str, size_t len ) noexcept; // C++20
Marshall Clowabe6daf2017-10-24 14:06:00 +0000186 constexpr basic_string_view<char16_t> operator "" sv( const char16_t *str, size_t len ) noexcept;
187 constexpr basic_string_view<char32_t> operator "" sv( const char32_t *str, size_t len ) noexcept;
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000188
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000189} // namespace std
190
191
192*/
193
194#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400195#include <__debug>
Mark de Weveraf59b2d2020-11-24 18:08:02 +0100196#include <__ranges/enable_borrowed_range.h>
Christopher Di Bella92072c22021-05-14 06:20:07 +0000197#include <__ranges/enable_view.h>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000198#include <__string>
Eric Fiselier2d357f72016-12-05 23:53:23 +0000199#include <algorithm>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400200#include <compare>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400201#include <iosfwd>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000202#include <iterator>
Eric Fiselier2d357f72016-12-05 23:53:23 +0000203#include <limits>
204#include <stdexcept>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000205#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000206
207#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
208#pragma GCC system_header
209#endif
210
Eric Fiselierf4433a32017-05-31 22:07:49 +0000211_LIBCPP_PUSH_MACROS
212#include <__undef_macros>
213
214
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000215_LIBCPP_BEGIN_NAMESPACE_STD
216
217template<class _CharT, class _Traits = char_traits<_CharT> >
Richard Smith256954d2020-11-11 17:12:18 -0800218 class _LIBCPP_TEMPLATE_VIS basic_string_view;
219
220typedef basic_string_view<char> string_view;
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400221#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800222typedef basic_string_view<char8_t> u8string_view;
223#endif
224typedef basic_string_view<char16_t> u16string_view;
225typedef basic_string_view<char32_t> u32string_view;
Louis Dionne89258142021-08-23 15:32:36 -0400226#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Richard Smith256954d2020-11-11 17:12:18 -0800227typedef basic_string_view<wchar_t> wstring_view;
Louis Dionne89258142021-08-23 15:32:36 -0400228#endif
Richard Smith256954d2020-11-11 17:12:18 -0800229
230template<class _CharT, class _Traits>
231class
232 _LIBCPP_PREFERRED_NAME(string_view)
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400233#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800234 _LIBCPP_PREFERRED_NAME(u8string_view)
235#endif
236 _LIBCPP_PREFERRED_NAME(u16string_view)
237 _LIBCPP_PREFERRED_NAME(u32string_view)
Louis Dionne89258142021-08-23 15:32:36 -0400238 _LIBCPP_IF_WIDE_CHARACTERS(_LIBCPP_PREFERRED_NAME(wstring_view))
Richard Smith256954d2020-11-11 17:12:18 -0800239 basic_string_view {
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000240public:
Marshall Clowb7db4972017-11-15 20:02:27 +0000241 // types
242 typedef _Traits traits_type;
243 typedef _CharT value_type;
Marshall Clow7d2c5182017-12-20 16:31:40 +0000244 typedef _CharT* pointer;
Marshall Clowb7db4972017-11-15 20:02:27 +0000245 typedef const _CharT* const_pointer;
Marshall Clow7d2c5182017-12-20 16:31:40 +0000246 typedef _CharT& reference;
Marshall Clowb7db4972017-11-15 20:02:27 +0000247 typedef const _CharT& const_reference;
248 typedef const_pointer const_iterator; // See [string.view.iterators]
249 typedef const_iterator iterator;
250 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
251 typedef const_reverse_iterator reverse_iterator;
252 typedef size_t size_type;
253 typedef ptrdiff_t difference_type;
254 static _LIBCPP_CONSTEXPR const size_type npos = -1; // size_type(-1);
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000255
Marshall Clow79f33542018-03-21 00:36:05 +0000256 static_assert((!is_array<value_type>::value), "Character type of basic_string_view must not be an array");
257 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string_view must be standard-layout");
258 static_assert(( is_trivial<value_type>::value), "Character type of basic_string_view must be trivial");
Marshall Clowa3a74e02017-03-15 18:41:11 +0000259 static_assert((is_same<_CharT, typename traits_type::char_type>::value),
260 "traits_type::char_type must be the same type as CharT");
261
Marshall Clowb7db4972017-11-15 20:02:27 +0000262 // [string.view.cons], construct/copy
263 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
264 basic_string_view() _NOEXCEPT : __data (nullptr), __size(0) {}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000265
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -0400266 _LIBCPP_INLINE_VISIBILITY
Marshall Clowb7db4972017-11-15 20:02:27 +0000267 basic_string_view(const basic_string_view&) _NOEXCEPT = default;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000268
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -0400269 _LIBCPP_INLINE_VISIBILITY
Marshall Clowb7db4972017-11-15 20:02:27 +0000270 basic_string_view& operator=(const basic_string_view&) _NOEXCEPT = default;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000271
Marshall Clowb7db4972017-11-15 20:02:27 +0000272 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
273 basic_string_view(const _CharT* __s, size_type __len) _NOEXCEPT
274 : __data(__s), __size(__len)
275 {
Marshall Clowca1bcdb2019-06-04 02:07:11 +0000276#if _LIBCPP_STD_VER > 11
277 _LIBCPP_ASSERT(__len == 0 || __s != nullptr, "string_view::string_view(_CharT *, size_t): received nullptr");
278#endif
Marshall Clowb7db4972017-11-15 20:02:27 +0000279 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000280
Joe Loser28dcceb2021-10-06 14:17:02 -0400281#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_RANGES)
282 template <contiguous_iterator _It, sized_sentinel_for<_It> _End>
Joe Loserf9dfe432021-11-24 15:35:15 -0500283 requires (is_same_v<iter_value_t<_It>, _CharT> && !is_convertible_v<_End, size_type>)
Joe Loser28dcceb2021-10-06 14:17:02 -0400284 constexpr _LIBCPP_HIDE_FROM_ABI basic_string_view(_It __begin, _End __end)
285 : __data(_VSTD::to_address(__begin)), __size(__end - __begin)
286 {
287 _LIBCPP_ASSERT((__end - __begin) >= 0, "std::string_view::string_view(iterator, sentinel) received invalid range");
288 }
289#endif
290
Marshall Clowb7db4972017-11-15 20:02:27 +0000291 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
292 basic_string_view(const _CharT* __s)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500293 : __data(__s), __size(_VSTD::__char_traits_length_checked<_Traits>(__s)) {}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000294
Marek Kurdej90d79712021-07-27 16:16:21 +0200295#if _LIBCPP_STD_VER > 20
296 basic_string_view(nullptr_t) = delete;
297#endif
298
Marshall Clowb7db4972017-11-15 20:02:27 +0000299 // [string.view.iterators], iterators
300 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
301 const_iterator begin() const _NOEXCEPT { return cbegin(); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000302
Marshall Clowb7db4972017-11-15 20:02:27 +0000303 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
304 const_iterator end() const _NOEXCEPT { return cend(); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000305
Marshall Clowb7db4972017-11-15 20:02:27 +0000306 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
307 const_iterator cbegin() const _NOEXCEPT { return __data; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000308
Marshall Clowb7db4972017-11-15 20:02:27 +0000309 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
310 const_iterator cend() const _NOEXCEPT { return __data + __size; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000311
Marshall Clowb7db4972017-11-15 20:02:27 +0000312 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
313 const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000314
Marshall Clowb7db4972017-11-15 20:02:27 +0000315 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
316 const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000317
Marshall Clowb7db4972017-11-15 20:02:27 +0000318 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
319 const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000320
Marshall Clowb7db4972017-11-15 20:02:27 +0000321 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
322 const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000323
Marshall Clowb7db4972017-11-15 20:02:27 +0000324 // [string.view.capacity], capacity
325 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
326 size_type size() const _NOEXCEPT { return __size; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000327
Marshall Clowb7db4972017-11-15 20:02:27 +0000328 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
329 size_type length() const _NOEXCEPT { return __size; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000330
Marshall Clowb7db4972017-11-15 20:02:27 +0000331 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
332 size_type max_size() const _NOEXCEPT { return numeric_limits<size_type>::max(); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000333
Marshall Clowb7db4972017-11-15 20:02:27 +0000334 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
335 bool empty() const _NOEXCEPT { return __size == 0; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000336
Marshall Clowb7db4972017-11-15 20:02:27 +0000337 // [string.view.access], element access
338 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Chris Palmer1b114a52020-10-06 13:01:50 -0400339 const_reference operator[](size_type __pos) const _NOEXCEPT {
340 return _LIBCPP_ASSERT(__pos < size(), "string_view[] index out of bounds"), __data[__pos];
341 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000342
Marshall Clowb7db4972017-11-15 20:02:27 +0000343 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
344 const_reference at(size_type __pos) const
345 {
346 return __pos >= size()
347 ? (__throw_out_of_range("string_view::at"), __data[0])
348 : __data[__pos];
349 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000350
Marshall Clowb7db4972017-11-15 20:02:27 +0000351 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +0000352 const_reference front() const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000353 {
354 return _LIBCPP_ASSERT(!empty(), "string_view::front(): string is empty"), __data[0];
355 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000356
Marshall Clowb7db4972017-11-15 20:02:27 +0000357 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +0000358 const_reference back() const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000359 {
360 return _LIBCPP_ASSERT(!empty(), "string_view::back(): string is empty"), __data[__size-1];
361 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000362
Marshall Clowb7db4972017-11-15 20:02:27 +0000363 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
364 const_pointer data() const _NOEXCEPT { return __data; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000365
Marshall Clowb7db4972017-11-15 20:02:27 +0000366 // [string.view.modifiers], modifiers:
367 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
368 void remove_prefix(size_type __n) _NOEXCEPT
369 {
370 _LIBCPP_ASSERT(__n <= size(), "remove_prefix() can't remove more than size()");
371 __data += __n;
372 __size -= __n;
373 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000374
Marshall Clowb7db4972017-11-15 20:02:27 +0000375 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
376 void remove_suffix(size_type __n) _NOEXCEPT
377 {
378 _LIBCPP_ASSERT(__n <= size(), "remove_suffix() can't remove more than size()");
379 __size -= __n;
380 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000381
Marshall Clowb7db4972017-11-15 20:02:27 +0000382 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
383 void swap(basic_string_view& __other) _NOEXCEPT
384 {
385 const value_type *__p = __data;
386 __data = __other.__data;
387 __other.__data = __p;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000388
Marshall Clowb7db4972017-11-15 20:02:27 +0000389 size_type __sz = __size;
390 __size = __other.__size;
391 __other.__size = __sz;
392 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000393
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500394 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowb7db4972017-11-15 20:02:27 +0000395 size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const
396 {
397 if (__pos > size())
398 __throw_out_of_range("string_view::copy");
399 size_type __rlen = _VSTD::min(__n, size() - __pos);
400 _Traits::copy(__s, data() + __pos, __rlen);
401 return __rlen;
402 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000403
Marshall Clowb7db4972017-11-15 20:02:27 +0000404 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
405 basic_string_view substr(size_type __pos = 0, size_type __n = npos) const
406 {
407 return __pos > size()
408 ? (__throw_out_of_range("string_view::substr"), basic_string_view())
409 : basic_string_view(data() + __pos, _VSTD::min(__n, size() - __pos));
410 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000411
Marshall Clowb7db4972017-11-15 20:02:27 +0000412 _LIBCPP_CONSTEXPR_AFTER_CXX11 int compare(basic_string_view __sv) const _NOEXCEPT
413 {
414 size_type __rlen = _VSTD::min( size(), __sv.size());
415 int __retval = _Traits::compare(data(), __sv.data(), __rlen);
416 if ( __retval == 0 ) // first __rlen chars matched
417 __retval = size() == __sv.size() ? 0 : ( size() < __sv.size() ? -1 : 1 );
418 return __retval;
419 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000420
Marshall Clowb7db4972017-11-15 20:02:27 +0000421 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
422 int compare(size_type __pos1, size_type __n1, basic_string_view __sv) const
423 {
424 return substr(__pos1, __n1).compare(__sv);
425 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000426
Marshall Clowb7db4972017-11-15 20:02:27 +0000427 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000428 int compare( size_type __pos1, size_type __n1,
Marshall Clowb7db4972017-11-15 20:02:27 +0000429 basic_string_view __sv, size_type __pos2, size_type __n2) const
430 {
431 return substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
432 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000433
Marshall Clowb7db4972017-11-15 20:02:27 +0000434 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
435 int compare(const _CharT* __s) const _NOEXCEPT
436 {
437 return compare(basic_string_view(__s));
438 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000439
Marshall Clowb7db4972017-11-15 20:02:27 +0000440 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
441 int compare(size_type __pos1, size_type __n1, const _CharT* __s) const
442 {
443 return substr(__pos1, __n1).compare(basic_string_view(__s));
444 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000445
Marshall Clowb7db4972017-11-15 20:02:27 +0000446 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
447 int compare(size_type __pos1, size_type __n1, const _CharT* __s, size_type __n2) const
448 {
449 return substr(__pos1, __n1).compare(basic_string_view(__s, __n2));
450 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000451
Marshall Clowb7db4972017-11-15 20:02:27 +0000452 // find
453 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
454 size_type find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
455 {
456 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
457 return __str_find<value_type, size_type, traits_type, npos>
458 (data(), size(), __s.data(), __pos, __s.size());
459 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000460
Marshall Clowb7db4972017-11-15 20:02:27 +0000461 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
462 size_type find(_CharT __c, size_type __pos = 0) const _NOEXCEPT
463 {
464 return __str_find<value_type, size_type, traits_type, npos>
465 (data(), size(), __c, __pos);
466 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000467
Marshall Clowb7db4972017-11-15 20:02:27 +0000468 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800469 size_type find(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000470 {
471 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find(): received nullptr");
472 return __str_find<value_type, size_type, traits_type, npos>
473 (data(), size(), __s, __pos, __n);
474 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000475
Marshall Clowb7db4972017-11-15 20:02:27 +0000476 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800477 size_type find(const _CharT* __s, size_type __pos = 0) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000478 {
479 _LIBCPP_ASSERT(__s != nullptr, "string_view::find(): received nullptr");
480 return __str_find<value_type, size_type, traits_type, npos>
481 (data(), size(), __s, __pos, traits_type::length(__s));
482 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000483
Marshall Clowb7db4972017-11-15 20:02:27 +0000484 // rfind
485 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
486 size_type rfind(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT
487 {
488 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
489 return __str_rfind<value_type, size_type, traits_type, npos>
490 (data(), size(), __s.data(), __pos, __s.size());
491 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000492
Marshall Clowb7db4972017-11-15 20:02:27 +0000493 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
494 size_type rfind(_CharT __c, size_type __pos = npos) const _NOEXCEPT
495 {
496 return __str_rfind<value_type, size_type, traits_type, npos>
497 (data(), size(), __c, __pos);
498 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000499
Marshall Clowb7db4972017-11-15 20:02:27 +0000500 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800501 size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000502 {
503 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::rfind(): received nullptr");
504 return __str_rfind<value_type, size_type, traits_type, npos>
505 (data(), size(), __s, __pos, __n);
506 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000507
Marshall Clowb7db4972017-11-15 20:02:27 +0000508 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800509 size_type rfind(const _CharT* __s, size_type __pos=npos) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000510 {
511 _LIBCPP_ASSERT(__s != nullptr, "string_view::rfind(): received nullptr");
512 return __str_rfind<value_type, size_type, traits_type, npos>
513 (data(), size(), __s, __pos, traits_type::length(__s));
514 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000515
Marshall Clowb7db4972017-11-15 20:02:27 +0000516 // find_first_of
517 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
518 size_type find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
519 {
520 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_of(): received nullptr");
521 return __str_find_first_of<value_type, size_type, traits_type, npos>
522 (data(), size(), __s.data(), __pos, __s.size());
523 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000524
Marshall Clowb7db4972017-11-15 20:02:27 +0000525 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
526 size_type find_first_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT
527 { return find(__c, __pos); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000528
Marshall Clowb7db4972017-11-15 20:02:27 +0000529 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800530 size_type find_first_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000531 {
532 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_of(): received nullptr");
533 return __str_find_first_of<value_type, size_type, traits_type, npos>
534 (data(), size(), __s, __pos, __n);
535 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000536
Marshall Clowb7db4972017-11-15 20:02:27 +0000537 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800538 size_type find_first_of(const _CharT* __s, size_type __pos=0) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000539 {
540 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_of(): received nullptr");
541 return __str_find_first_of<value_type, size_type, traits_type, npos>
542 (data(), size(), __s, __pos, traits_type::length(__s));
543 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000544
Marshall Clowb7db4972017-11-15 20:02:27 +0000545 // find_last_of
546 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
547 size_type find_last_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
548 {
549 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_of(): received nullptr");
550 return __str_find_last_of<value_type, size_type, traits_type, npos>
551 (data(), size(), __s.data(), __pos, __s.size());
552 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000553
Marshall Clowb7db4972017-11-15 20:02:27 +0000554 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
555 size_type find_last_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT
556 { return rfind(__c, __pos); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000557
Marshall Clowb7db4972017-11-15 20:02:27 +0000558 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800559 size_type find_last_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000560 {
561 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_of(): received nullptr");
562 return __str_find_last_of<value_type, size_type, traits_type, npos>
563 (data(), size(), __s, __pos, __n);
564 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000565
Marshall Clowb7db4972017-11-15 20:02:27 +0000566 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800567 size_type find_last_of(const _CharT* __s, size_type __pos=npos) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000568 {
569 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_of(): received nullptr");
570 return __str_find_last_of<value_type, size_type, traits_type, npos>
571 (data(), size(), __s, __pos, traits_type::length(__s));
572 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000573
Marshall Clowb7db4972017-11-15 20:02:27 +0000574 // find_first_not_of
575 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
576 size_type find_first_not_of(basic_string_view __s, size_type __pos=0) const _NOEXCEPT
577 {
578 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_not_of(): received nullptr");
579 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
580 (data(), size(), __s.data(), __pos, __s.size());
581 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000582
Marshall Clowb7db4972017-11-15 20:02:27 +0000583 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
584 size_type find_first_not_of(_CharT __c, size_type __pos=0) const _NOEXCEPT
585 {
586 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
587 (data(), size(), __c, __pos);
588 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000589
Marshall Clowb7db4972017-11-15 20:02:27 +0000590 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800591 size_type find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000592 {
593 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_not_of(): received nullptr");
594 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
595 (data(), size(), __s, __pos, __n);
596 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000597
Marshall Clowb7db4972017-11-15 20:02:27 +0000598 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800599 size_type find_first_not_of(const _CharT* __s, size_type __pos=0) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000600 {
601 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_not_of(): received nullptr");
602 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
603 (data(), size(), __s, __pos, traits_type::length(__s));
604 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000605
Marshall Clowb7db4972017-11-15 20:02:27 +0000606 // find_last_not_of
607 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
608 size_type find_last_not_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
609 {
610 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_not_of(): received nullptr");
611 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
612 (data(), size(), __s.data(), __pos, __s.size());
613 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000614
Marshall Clowb7db4972017-11-15 20:02:27 +0000615 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
616 size_type find_last_not_of(_CharT __c, size_type __pos=npos) const _NOEXCEPT
617 {
618 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
619 (data(), size(), __c, __pos);
620 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000621
Marshall Clowb7db4972017-11-15 20:02:27 +0000622 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800623 size_type find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000624 {
625 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_not_of(): received nullptr");
626 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
627 (data(), size(), __s, __pos, __n);
628 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000629
Marshall Clowb7db4972017-11-15 20:02:27 +0000630 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800631 size_type find_last_not_of(const _CharT* __s, size_type __pos=npos) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000632 {
633 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_not_of(): received nullptr");
634 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
635 (data(), size(), __s, __pos, traits_type::length(__s));
636 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000637
Marshall Clow18c293b2017-12-04 20:11:38 +0000638#if _LIBCPP_STD_VER > 17
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -0400639 constexpr _LIBCPP_INLINE_VISIBILITY
640 bool starts_with(basic_string_view __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +0000641 { return size() >= __s.size() && compare(0, __s.size(), __s) == 0; }
642
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -0400643 constexpr _LIBCPP_INLINE_VISIBILITY
644 bool starts_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +0000645 { return !empty() && _Traits::eq(front(), __c); }
646
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -0400647 constexpr _LIBCPP_INLINE_VISIBILITY
648 bool starts_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +0000649 { return starts_with(basic_string_view(__s)); }
650
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -0400651 constexpr _LIBCPP_INLINE_VISIBILITY
652 bool ends_with(basic_string_view __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +0000653 { return size() >= __s.size() && compare(size() - __s.size(), npos, __s) == 0; }
654
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -0400655 constexpr _LIBCPP_INLINE_VISIBILITY
656 bool ends_with(value_type __c) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +0000657 { return !empty() && _Traits::eq(back(), __c); }
658
Arthur O'Dwyera0cb1e82021-09-28 12:19:35 -0400659 constexpr _LIBCPP_INLINE_VISIBILITY
660 bool ends_with(const value_type* __s) const noexcept
Marshall Clow18c293b2017-12-04 20:11:38 +0000661 { return ends_with(basic_string_view(__s)); }
662#endif
663
Wim Leflere023c3542021-01-19 14:33:30 -0500664#if _LIBCPP_STD_VER > 20
665 constexpr _LIBCPP_INLINE_VISIBILITY
666 bool contains(basic_string_view __sv) const noexcept
667 { return find(__sv) != npos; }
668
669 constexpr _LIBCPP_INLINE_VISIBILITY
670 bool contains(value_type __c) const noexcept
671 { return find(__c) != npos; }
672
673 constexpr _LIBCPP_INLINE_VISIBILITY
674 bool contains(const value_type* __s) const
675 { return find(__s) != npos; }
676#endif
677
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000678private:
Marshall Clowb7db4972017-11-15 20:02:27 +0000679 const value_type* __data;
680 size_type __size;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000681};
682
Mark de Weveraf59b2d2020-11-24 18:08:02 +0100683#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_RANGES)
684template <class _CharT, class _Traits>
Christopher Di Bella92072c22021-05-14 06:20:07 +0000685inline constexpr bool ranges::enable_view<basic_string_view<_CharT, _Traits>> = true;
686
687template <class _CharT, class _Traits>
Mark de Weveraf59b2d2020-11-24 18:08:02 +0100688inline constexpr bool ranges::enable_borrowed_range<basic_string_view<_CharT, _Traits> > = true;
689#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_RANGES)
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000690
Joe Loser28dcceb2021-10-06 14:17:02 -0400691// [string.view.deduct]
692
693#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_RANGES)
694template <contiguous_iterator _It, sized_sentinel_for<_It> _End>
695 basic_string_view(_It, _End) -> basic_string_view<iter_value_t<_It>>;
696#endif
697
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000698// [string.view.comparison]
699// operator ==
700template<class _CharT, class _Traits>
701_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
702bool operator==(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000703 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000704{
Marshall Clowb7db4972017-11-15 20:02:27 +0000705 if ( __lhs.size() != __rhs.size()) return false;
706 return __lhs.compare(__rhs) == 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000707}
708
709template<class _CharT, class _Traits>
710_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
711bool operator==(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000712 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000713{
Marshall Clowb7db4972017-11-15 20:02:27 +0000714 if ( __lhs.size() != __rhs.size()) return false;
715 return __lhs.compare(__rhs) == 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000716}
717
718template<class _CharT, class _Traits>
719_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000720bool operator==(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000721 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000722{
Marshall Clowb7db4972017-11-15 20:02:27 +0000723 if ( __lhs.size() != __rhs.size()) return false;
724 return __lhs.compare(__rhs) == 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000725}
726
727
728// operator !=
729template<class _CharT, class _Traits>
730_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
731bool operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
732{
Marshall Clowb7db4972017-11-15 20:02:27 +0000733 if ( __lhs.size() != __rhs.size())
734 return true;
735 return __lhs.compare(__rhs) != 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000736}
737
738template<class _CharT, class _Traits>
739_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
740bool operator!=(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000741 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000742{
Marshall Clowb7db4972017-11-15 20:02:27 +0000743 if ( __lhs.size() != __rhs.size())
744 return true;
745 return __lhs.compare(__rhs) != 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000746}
747
748template<class _CharT, class _Traits>
749_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000750bool operator!=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000751 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000752{
Marshall Clowb7db4972017-11-15 20:02:27 +0000753 if ( __lhs.size() != __rhs.size())
754 return true;
755 return __lhs.compare(__rhs) != 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000756}
757
758
759// operator <
760template<class _CharT, class _Traits>
761_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
762bool operator<(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
763{
Marshall Clowb7db4972017-11-15 20:02:27 +0000764 return __lhs.compare(__rhs) < 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000765}
766
767template<class _CharT, class _Traits>
768_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
769bool operator<(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000770 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000771{
Marshall Clowb7db4972017-11-15 20:02:27 +0000772 return __lhs.compare(__rhs) < 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000773}
774
775template<class _CharT, class _Traits>
776_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000777bool operator<(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000778 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000779{
Marshall Clowb7db4972017-11-15 20:02:27 +0000780 return __lhs.compare(__rhs) < 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000781}
782
783
784// operator >
785template<class _CharT, class _Traits>
786_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
787bool operator> (basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
788{
Marshall Clowb7db4972017-11-15 20:02:27 +0000789 return __lhs.compare(__rhs) > 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000790}
791
792template<class _CharT, class _Traits>
793_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
794bool operator>(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000795 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000796{
Marshall Clowb7db4972017-11-15 20:02:27 +0000797 return __lhs.compare(__rhs) > 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000798}
799
800template<class _CharT, class _Traits>
801_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000802bool operator>(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000803 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000804{
Marshall Clowb7db4972017-11-15 20:02:27 +0000805 return __lhs.compare(__rhs) > 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000806}
807
808
809// operator <=
810template<class _CharT, class _Traits>
811_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
812bool operator<=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
813{
Marshall Clowb7db4972017-11-15 20:02:27 +0000814 return __lhs.compare(__rhs) <= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000815}
816
817template<class _CharT, class _Traits>
818_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
819bool operator<=(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000820 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000821{
Marshall Clowb7db4972017-11-15 20:02:27 +0000822 return __lhs.compare(__rhs) <= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000823}
824
825template<class _CharT, class _Traits>
826_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000827bool operator<=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000828 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000829{
Marshall Clowb7db4972017-11-15 20:02:27 +0000830 return __lhs.compare(__rhs) <= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000831}
832
833
834// operator >=
835template<class _CharT, class _Traits>
836_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
837bool operator>=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
838{
Marshall Clowb7db4972017-11-15 20:02:27 +0000839 return __lhs.compare(__rhs) >= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000840}
841
842
843template<class _CharT, class _Traits>
844_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
845bool operator>=(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000846 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000847{
Marshall Clowb7db4972017-11-15 20:02:27 +0000848 return __lhs.compare(__rhs) >= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000849}
850
851template<class _CharT, class _Traits>
852_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000853bool operator>=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000854 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000855{
Marshall Clowb7db4972017-11-15 20:02:27 +0000856 return __lhs.compare(__rhs) >= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000857}
858
Eric Fiselier14116922019-09-25 18:56:54 +0000859
860template<class _CharT, class _Traits>
861basic_ostream<_CharT, _Traits>&
862operator<<(basic_ostream<_CharT, _Traits>& __os,
863 basic_string_view<_CharT, _Traits> __str);
864
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000865// [string.view.hash]
Marshall Clowab313df2019-06-27 14:18:32 +0000866template<class _CharT>
867struct _LIBCPP_TEMPLATE_VIS hash<basic_string_view<_CharT, char_traits<_CharT> > >
868 : public unary_function<basic_string_view<_CharT, char_traits<_CharT> >, size_t>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000869{
Louis Dionned83f2e62018-11-28 15:22:30 +0000870 _LIBCPP_INLINE_VISIBILITY
Marshall Clowab313df2019-06-27 14:18:32 +0000871 size_t operator()(const basic_string_view<_CharT, char_traits<_CharT> > __val) const _NOEXCEPT {
Louis Dionned83f2e62018-11-28 15:22:30 +0000872 return __do_string_hash(__val.data(), __val.data() + __val.size());
873 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000874};
875
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000876
Louis Dionne173f29e2019-05-29 16:01:36 +0000877#if _LIBCPP_STD_VER > 11
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000878inline namespace literals
879{
880 inline namespace string_view_literals
881 {
882 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clowabe6daf2017-10-24 14:06:00 +0000883 basic_string_view<char> operator "" sv(const char *__str, size_t __len) _NOEXCEPT
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000884 {
885 return basic_string_view<char> (__str, __len);
886 }
887
Louis Dionne89258142021-08-23 15:32:36 -0400888#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000889 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clowabe6daf2017-10-24 14:06:00 +0000890 basic_string_view<wchar_t> operator "" sv(const wchar_t *__str, size_t __len) _NOEXCEPT
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000891 {
892 return basic_string_view<wchar_t> (__str, __len);
893 }
Louis Dionne89258142021-08-23 15:32:36 -0400894#endif
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000895
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400896#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +0000897 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
898 basic_string_view<char8_t> operator "" sv(const char8_t *__str, size_t __len) _NOEXCEPT
899 {
900 return basic_string_view<char8_t> (__str, __len);
901 }
902#endif
903
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000904 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clowabe6daf2017-10-24 14:06:00 +0000905 basic_string_view<char16_t> operator "" sv(const char16_t *__str, size_t __len) _NOEXCEPT
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000906 {
907 return basic_string_view<char16_t> (__str, __len);
908 }
909
910 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clowabe6daf2017-10-24 14:06:00 +0000911 basic_string_view<char32_t> operator "" sv(const char32_t *__str, size_t __len) _NOEXCEPT
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000912 {
913 return basic_string_view<char32_t> (__str, __len);
914 }
915 }
916}
917#endif
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000918_LIBCPP_END_NAMESPACE_STD
919
Eric Fiselierf4433a32017-05-31 22:07:49 +0000920_LIBCPP_POP_MACROS
921
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000922#endif // _LIBCPP_STRING_VIEW