blob: 491f49f0653479b8f3ff1140630be869b3a9bbf9 [file] [log] [blame]
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001// -*- C++ -*-
2//===------------------------ string_view ---------------------------------===//
3//
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>
23 inline constexpr bool ranges::enable_borrowed_range<basic_string_view<charT, traits>> = true; // C++20
24
Marshall Clowdf63a6d2016-07-21 05:31:24 +000025 // 7.9, basic_string_view non-member comparison functions
26 template<class charT, class traits>
27 constexpr bool operator==(basic_string_view<charT, traits> x,
28 basic_string_view<charT, traits> y) noexcept;
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 // see below, sufficient additional overloads of comparison functions
45
46 // 7.10, Inserters and extractors
47 template<class charT, class traits>
48 basic_ostream<charT, traits>&
49 operator<<(basic_ostream<charT, traits>& os,
50 basic_string_view<charT, traits> str);
51
52 // basic_string_view typedef names
53 typedef basic_string_view<char> string_view;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +010054 typedef basic_string_view<char8_t> u8string_view; // C++20
Marshall Clowdf63a6d2016-07-21 05:31:24 +000055 typedef basic_string_view<char16_t> u16string_view;
56 typedef basic_string_view<char32_t> u32string_view;
57 typedef basic_string_view<wchar_t> wstring_view;
58
59 template<class charT, class traits = char_traits<charT>>
60 class basic_string_view {
61 public:
62 // types
63 typedef traits traits_type;
64 typedef charT value_type;
65 typedef charT* pointer;
66 typedef const charT* const_pointer;
67 typedef charT& reference;
68 typedef const charT& const_reference;
69 typedef implementation-defined const_iterator;
70 typedef const_iterator iterator;
71 typedef reverse_iterator<const_iterator> const_reverse_iterator;
72 typedef const_reverse_iterator reverse_iterator;
73 typedef size_t size_type;
74 typedef ptrdiff_t difference_type;
75 static constexpr size_type npos = size_type(-1);
76
77 // 7.3, basic_string_view constructors and assignment operators
78 constexpr basic_string_view() noexcept;
79 constexpr basic_string_view(const basic_string_view&) noexcept = default;
80 basic_string_view& operator=(const basic_string_view&) noexcept = default;
81 template<class Allocator>
82 constexpr basic_string_view(const charT* str);
83 constexpr basic_string_view(const charT* str, size_type len);
84
85 // 7.4, basic_string_view iterator support
86 constexpr const_iterator begin() const noexcept;
87 constexpr const_iterator end() const noexcept;
88 constexpr const_iterator cbegin() const noexcept;
89 constexpr const_iterator cend() const noexcept;
90 const_reverse_iterator rbegin() const noexcept;
91 const_reverse_iterator rend() const noexcept;
92 const_reverse_iterator crbegin() const noexcept;
93 const_reverse_iterator crend() const noexcept;
94
95 // 7.5, basic_string_view capacity
96 constexpr size_type size() const noexcept;
97 constexpr size_type length() const noexcept;
98 constexpr size_type max_size() const noexcept;
99 constexpr bool empty() const noexcept;
100
101 // 7.6, basic_string_view element access
102 constexpr const_reference operator[](size_type pos) const;
103 constexpr const_reference at(size_type pos) const;
104 constexpr const_reference front() const;
105 constexpr const_reference back() const;
106 constexpr const_pointer data() const noexcept;
107
108 // 7.7, basic_string_view modifiers
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000109 constexpr void remove_prefix(size_type n);
110 constexpr void remove_suffix(size_type n);
111 constexpr void swap(basic_string_view& s) noexcept;
112
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500113 size_type copy(charT* s, size_type n, size_type pos = 0) const; // constexpr in C++20
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000114
115 constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const;
116 constexpr int compare(basic_string_view s) const noexcept;
117 constexpr int compare(size_type pos1, size_type n1, basic_string_view s) const;
118 constexpr int compare(size_type pos1, size_type n1,
119 basic_string_view s, size_type pos2, size_type n2) const;
120 constexpr int compare(const charT* s) const;
121 constexpr int compare(size_type pos1, size_type n1, const charT* s) const;
122 constexpr int compare(size_type pos1, size_type n1,
123 const charT* s, size_type n2) const;
124 constexpr size_type find(basic_string_view s, size_type pos = 0) const noexcept;
125 constexpr size_type find(charT c, size_type pos = 0) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800126 constexpr size_type find(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
127 constexpr size_type find(const charT* s, size_type pos = 0) const noexcept; // noexcept as an extension
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000128 constexpr size_type rfind(basic_string_view s, size_type pos = npos) const noexcept;
129 constexpr size_type rfind(charT c, size_type pos = npos) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800130 constexpr size_type rfind(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
131 constexpr size_type rfind(const charT* s, size_type pos = npos) const noexcept; // noexcept as an extension
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000132 constexpr size_type find_first_of(basic_string_view s, size_type pos = 0) const noexcept;
133 constexpr size_type find_first_of(charT c, size_type pos = 0) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800134 constexpr size_type find_first_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
135 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 +0000136 constexpr size_type find_last_of(basic_string_view s, size_type pos = npos) const noexcept;
137 constexpr size_type find_last_of(charT c, size_type pos = npos) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800138 constexpr size_type find_last_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
139 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 +0000140 constexpr size_type find_first_not_of(basic_string_view s, size_type pos = 0) const noexcept;
141 constexpr size_type find_first_not_of(charT c, size_type pos = 0) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800142 constexpr size_type find_first_not_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
143 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 +0000144 constexpr size_type find_last_not_of(basic_string_view s, size_type pos = npos) const noexcept;
145 constexpr size_type find_last_not_of(charT c, size_type pos = npos) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800146 constexpr size_type find_last_not_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
147 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 +0000148
Marek Kurdej24b4c512021-01-07 12:29:04 +0100149 constexpr bool starts_with(basic_string_view s) const noexcept; // C++20
150 constexpr bool starts_with(charT c) const noexcept; // C++20
151 constexpr bool starts_with(const charT* s) const; // C++20
152 constexpr bool ends_with(basic_string_view s) const noexcept; // C++20
153 constexpr bool ends_with(charT c) const noexcept; // C++20
154 constexpr bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000155
Wim Leflere023c3542021-01-19 14:33:30 -0500156 constexpr bool contains(basic_string_view s) const noexcept; // C++2b
157 constexpr bool contains(charT c) const noexcept; // C++2b
158 constexpr bool contains(const charT* s) const; // C++2b
159
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000160 private:
161 const_pointer data_; // exposition only
162 size_type size_; // exposition only
163 };
164
165 // 7.11, Hash support
166 template <class T> struct hash;
167 template <> struct hash<string_view>;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100168 template <> struct hash<u8string_view>; // C++20
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000169 template <> struct hash<u16string_view>;
170 template <> struct hash<u32string_view>;
171 template <> struct hash<wstring_view>;
172
Marshall Clowabe6daf2017-10-24 14:06:00 +0000173 constexpr basic_string_view<char> operator "" sv( const char *str, size_t len ) noexcept;
174 constexpr basic_string_view<wchar_t> operator "" sv( const wchar_t *str, size_t len ) noexcept;
Marek Kurdeje3ac4e22021-03-23 17:15:07 +0100175 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 +0000176 constexpr basic_string_view<char16_t> operator "" sv( const char16_t *str, size_t len ) noexcept;
177 constexpr basic_string_view<char32_t> operator "" sv( const char32_t *str, size_t len ) noexcept;
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000178
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000179} // namespace std
180
181
182*/
183
184#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400185#include <__debug>
Mark de Weveraf59b2d2020-11-24 18:08:02 +0100186#include <__ranges/enable_borrowed_range.h>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000187#include <__string>
Eric Fiselier2d357f72016-12-05 23:53:23 +0000188#include <algorithm>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400189#include <compare>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400190#include <iosfwd>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000191#include <iterator>
Eric Fiselier2d357f72016-12-05 23:53:23 +0000192#include <limits>
193#include <stdexcept>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000194#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000195
196#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
197#pragma GCC system_header
198#endif
199
Eric Fiselierf4433a32017-05-31 22:07:49 +0000200_LIBCPP_PUSH_MACROS
201#include <__undef_macros>
202
203
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000204_LIBCPP_BEGIN_NAMESPACE_STD
205
206template<class _CharT, class _Traits = char_traits<_CharT> >
Richard Smith256954d2020-11-11 17:12:18 -0800207 class _LIBCPP_TEMPLATE_VIS basic_string_view;
208
209typedef basic_string_view<char> string_view;
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400210#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800211typedef basic_string_view<char8_t> u8string_view;
212#endif
213typedef basic_string_view<char16_t> u16string_view;
214typedef basic_string_view<char32_t> u32string_view;
215typedef basic_string_view<wchar_t> wstring_view;
216
217template<class _CharT, class _Traits>
218class
219 _LIBCPP_PREFERRED_NAME(string_view)
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400220#ifndef _LIBCPP_HAS_NO_CHAR8_T
Richard Smith256954d2020-11-11 17:12:18 -0800221 _LIBCPP_PREFERRED_NAME(u8string_view)
222#endif
223 _LIBCPP_PREFERRED_NAME(u16string_view)
224 _LIBCPP_PREFERRED_NAME(u32string_view)
225 _LIBCPP_PREFERRED_NAME(wstring_view)
226 basic_string_view {
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000227public:
Marshall Clowb7db4972017-11-15 20:02:27 +0000228 // types
229 typedef _Traits traits_type;
230 typedef _CharT value_type;
Marshall Clow7d2c5182017-12-20 16:31:40 +0000231 typedef _CharT* pointer;
Marshall Clowb7db4972017-11-15 20:02:27 +0000232 typedef const _CharT* const_pointer;
Marshall Clow7d2c5182017-12-20 16:31:40 +0000233 typedef _CharT& reference;
Marshall Clowb7db4972017-11-15 20:02:27 +0000234 typedef const _CharT& const_reference;
235 typedef const_pointer const_iterator; // See [string.view.iterators]
236 typedef const_iterator iterator;
237 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
238 typedef const_reverse_iterator reverse_iterator;
239 typedef size_t size_type;
240 typedef ptrdiff_t difference_type;
241 static _LIBCPP_CONSTEXPR const size_type npos = -1; // size_type(-1);
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000242
Marshall Clow79f33542018-03-21 00:36:05 +0000243 static_assert((!is_array<value_type>::value), "Character type of basic_string_view must not be an array");
244 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string_view must be standard-layout");
245 static_assert(( is_trivial<value_type>::value), "Character type of basic_string_view must be trivial");
Marshall Clowa3a74e02017-03-15 18:41:11 +0000246 static_assert((is_same<_CharT, typename traits_type::char_type>::value),
247 "traits_type::char_type must be the same type as CharT");
248
Marshall Clowb7db4972017-11-15 20:02:27 +0000249 // [string.view.cons], construct/copy
250 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
251 basic_string_view() _NOEXCEPT : __data (nullptr), __size(0) {}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000252
Marshall Clowb7db4972017-11-15 20:02:27 +0000253 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
254 basic_string_view(const basic_string_view&) _NOEXCEPT = default;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000255
Marshall Clowb7db4972017-11-15 20:02:27 +0000256 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
257 basic_string_view& operator=(const basic_string_view&) _NOEXCEPT = default;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000258
Marshall Clowb7db4972017-11-15 20:02:27 +0000259 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
260 basic_string_view(const _CharT* __s, size_type __len) _NOEXCEPT
261 : __data(__s), __size(__len)
262 {
Marshall Clowca1bcdb2019-06-04 02:07:11 +0000263#if _LIBCPP_STD_VER > 11
264 _LIBCPP_ASSERT(__len == 0 || __s != nullptr, "string_view::string_view(_CharT *, size_t): received nullptr");
265#endif
Marshall Clowb7db4972017-11-15 20:02:27 +0000266 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000267
Marshall Clowb7db4972017-11-15 20:02:27 +0000268 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
269 basic_string_view(const _CharT* __s)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500270 : __data(__s), __size(_VSTD::__char_traits_length_checked<_Traits>(__s)) {}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000271
Marshall Clowb7db4972017-11-15 20:02:27 +0000272 // [string.view.iterators], iterators
273 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
274 const_iterator begin() const _NOEXCEPT { return cbegin(); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000275
Marshall Clowb7db4972017-11-15 20:02:27 +0000276 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
277 const_iterator end() const _NOEXCEPT { return cend(); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000278
Marshall Clowb7db4972017-11-15 20:02:27 +0000279 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
280 const_iterator cbegin() const _NOEXCEPT { return __data; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000281
Marshall Clowb7db4972017-11-15 20:02:27 +0000282 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
283 const_iterator cend() const _NOEXCEPT { return __data + __size; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000284
Marshall Clowb7db4972017-11-15 20:02:27 +0000285 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
286 const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000287
Marshall Clowb7db4972017-11-15 20:02:27 +0000288 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
289 const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000290
Marshall Clowb7db4972017-11-15 20:02:27 +0000291 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
292 const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000293
Marshall Clowb7db4972017-11-15 20:02:27 +0000294 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
295 const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000296
Marshall Clowb7db4972017-11-15 20:02:27 +0000297 // [string.view.capacity], capacity
298 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
299 size_type size() const _NOEXCEPT { return __size; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000300
Marshall Clowb7db4972017-11-15 20:02:27 +0000301 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
302 size_type length() const _NOEXCEPT { return __size; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000303
Marshall Clowb7db4972017-11-15 20:02:27 +0000304 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
305 size_type max_size() const _NOEXCEPT { return numeric_limits<size_type>::max(); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000306
Marshall Clowb7db4972017-11-15 20:02:27 +0000307 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
308 bool empty() const _NOEXCEPT { return __size == 0; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000309
Marshall Clowb7db4972017-11-15 20:02:27 +0000310 // [string.view.access], element access
311 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Chris Palmer1b114a52020-10-06 13:01:50 -0400312 const_reference operator[](size_type __pos) const _NOEXCEPT {
313 return _LIBCPP_ASSERT(__pos < size(), "string_view[] index out of bounds"), __data[__pos];
314 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000315
Marshall Clowb7db4972017-11-15 20:02:27 +0000316 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
317 const_reference at(size_type __pos) const
318 {
319 return __pos >= size()
320 ? (__throw_out_of_range("string_view::at"), __data[0])
321 : __data[__pos];
322 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000323
Marshall Clowb7db4972017-11-15 20:02:27 +0000324 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +0000325 const_reference front() const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000326 {
327 return _LIBCPP_ASSERT(!empty(), "string_view::front(): string is empty"), __data[0];
328 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000329
Marshall Clowb7db4972017-11-15 20:02:27 +0000330 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +0000331 const_reference back() const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000332 {
333 return _LIBCPP_ASSERT(!empty(), "string_view::back(): string is empty"), __data[__size-1];
334 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000335
Marshall Clowb7db4972017-11-15 20:02:27 +0000336 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
337 const_pointer data() const _NOEXCEPT { return __data; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000338
Marshall Clowb7db4972017-11-15 20:02:27 +0000339 // [string.view.modifiers], modifiers:
340 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
341 void remove_prefix(size_type __n) _NOEXCEPT
342 {
343 _LIBCPP_ASSERT(__n <= size(), "remove_prefix() can't remove more than size()");
344 __data += __n;
345 __size -= __n;
346 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000347
Marshall Clowb7db4972017-11-15 20:02:27 +0000348 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
349 void remove_suffix(size_type __n) _NOEXCEPT
350 {
351 _LIBCPP_ASSERT(__n <= size(), "remove_suffix() can't remove more than size()");
352 __size -= __n;
353 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000354
Marshall Clowb7db4972017-11-15 20:02:27 +0000355 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
356 void swap(basic_string_view& __other) _NOEXCEPT
357 {
358 const value_type *__p = __data;
359 __data = __other.__data;
360 __other.__data = __p;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000361
Marshall Clowb7db4972017-11-15 20:02:27 +0000362 size_type __sz = __size;
363 __size = __other.__size;
364 __other.__size = __sz;
365 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000366
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500367 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
Marshall Clowb7db4972017-11-15 20:02:27 +0000368 size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const
369 {
370 if (__pos > size())
371 __throw_out_of_range("string_view::copy");
372 size_type __rlen = _VSTD::min(__n, size() - __pos);
373 _Traits::copy(__s, data() + __pos, __rlen);
374 return __rlen;
375 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000376
Marshall Clowb7db4972017-11-15 20:02:27 +0000377 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
378 basic_string_view substr(size_type __pos = 0, size_type __n = npos) const
379 {
380 return __pos > size()
381 ? (__throw_out_of_range("string_view::substr"), basic_string_view())
382 : basic_string_view(data() + __pos, _VSTD::min(__n, size() - __pos));
383 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000384
Marshall Clowb7db4972017-11-15 20:02:27 +0000385 _LIBCPP_CONSTEXPR_AFTER_CXX11 int compare(basic_string_view __sv) const _NOEXCEPT
386 {
387 size_type __rlen = _VSTD::min( size(), __sv.size());
388 int __retval = _Traits::compare(data(), __sv.data(), __rlen);
389 if ( __retval == 0 ) // first __rlen chars matched
390 __retval = size() == __sv.size() ? 0 : ( size() < __sv.size() ? -1 : 1 );
391 return __retval;
392 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000393
Marshall Clowb7db4972017-11-15 20:02:27 +0000394 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
395 int compare(size_type __pos1, size_type __n1, basic_string_view __sv) const
396 {
397 return substr(__pos1, __n1).compare(__sv);
398 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000399
Marshall Clowb7db4972017-11-15 20:02:27 +0000400 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000401 int compare( size_type __pos1, size_type __n1,
Marshall Clowb7db4972017-11-15 20:02:27 +0000402 basic_string_view __sv, size_type __pos2, size_type __n2) const
403 {
404 return substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
405 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000406
Marshall Clowb7db4972017-11-15 20:02:27 +0000407 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
408 int compare(const _CharT* __s) const _NOEXCEPT
409 {
410 return compare(basic_string_view(__s));
411 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000412
Marshall Clowb7db4972017-11-15 20:02:27 +0000413 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
414 int compare(size_type __pos1, size_type __n1, const _CharT* __s) const
415 {
416 return substr(__pos1, __n1).compare(basic_string_view(__s));
417 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000418
Marshall Clowb7db4972017-11-15 20:02:27 +0000419 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
420 int compare(size_type __pos1, size_type __n1, const _CharT* __s, size_type __n2) const
421 {
422 return substr(__pos1, __n1).compare(basic_string_view(__s, __n2));
423 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000424
Marshall Clowb7db4972017-11-15 20:02:27 +0000425 // find
426 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
427 size_type find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
428 {
429 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
430 return __str_find<value_type, size_type, traits_type, npos>
431 (data(), size(), __s.data(), __pos, __s.size());
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 size_type find(_CharT __c, size_type __pos = 0) const _NOEXCEPT
436 {
437 return __str_find<value_type, size_type, traits_type, npos>
438 (data(), size(), __c, __pos);
439 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000440
Marshall Clowb7db4972017-11-15 20:02:27 +0000441 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800442 size_type find(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000443 {
444 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find(): received nullptr");
445 return __str_find<value_type, size_type, traits_type, npos>
446 (data(), size(), __s, __pos, __n);
447 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000448
Marshall Clowb7db4972017-11-15 20:02:27 +0000449 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800450 size_type find(const _CharT* __s, size_type __pos = 0) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000451 {
452 _LIBCPP_ASSERT(__s != nullptr, "string_view::find(): received nullptr");
453 return __str_find<value_type, size_type, traits_type, npos>
454 (data(), size(), __s, __pos, traits_type::length(__s));
455 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000456
Marshall Clowb7db4972017-11-15 20:02:27 +0000457 // rfind
458 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
459 size_type rfind(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT
460 {
461 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
462 return __str_rfind<value_type, size_type, traits_type, npos>
463 (data(), size(), __s.data(), __pos, __s.size());
464 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000465
Marshall Clowb7db4972017-11-15 20:02:27 +0000466 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
467 size_type rfind(_CharT __c, size_type __pos = npos) const _NOEXCEPT
468 {
469 return __str_rfind<value_type, size_type, traits_type, npos>
470 (data(), size(), __c, __pos);
471 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000472
Marshall Clowb7db4972017-11-15 20:02:27 +0000473 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800474 size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000475 {
476 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::rfind(): received nullptr");
477 return __str_rfind<value_type, size_type, traits_type, npos>
478 (data(), size(), __s, __pos, __n);
479 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000480
Marshall Clowb7db4972017-11-15 20:02:27 +0000481 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800482 size_type rfind(const _CharT* __s, size_type __pos=npos) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000483 {
484 _LIBCPP_ASSERT(__s != nullptr, "string_view::rfind(): received nullptr");
485 return __str_rfind<value_type, size_type, traits_type, npos>
486 (data(), size(), __s, __pos, traits_type::length(__s));
487 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000488
Marshall Clowb7db4972017-11-15 20:02:27 +0000489 // find_first_of
490 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
491 size_type find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
492 {
493 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_of(): received nullptr");
494 return __str_find_first_of<value_type, size_type, traits_type, npos>
495 (data(), size(), __s.data(), __pos, __s.size());
496 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000497
Marshall Clowb7db4972017-11-15 20:02:27 +0000498 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
499 size_type find_first_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT
500 { return find(__c, __pos); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000501
Marshall Clowb7db4972017-11-15 20:02:27 +0000502 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800503 size_type find_first_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000504 {
505 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_of(): received nullptr");
506 return __str_find_first_of<value_type, size_type, traits_type, npos>
507 (data(), size(), __s, __pos, __n);
508 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000509
Marshall Clowb7db4972017-11-15 20:02:27 +0000510 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800511 size_type find_first_of(const _CharT* __s, size_type __pos=0) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000512 {
513 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_of(): received nullptr");
514 return __str_find_first_of<value_type, size_type, traits_type, npos>
515 (data(), size(), __s, __pos, traits_type::length(__s));
516 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000517
Marshall Clowb7db4972017-11-15 20:02:27 +0000518 // find_last_of
519 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
520 size_type find_last_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
521 {
522 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_of(): received nullptr");
523 return __str_find_last_of<value_type, size_type, traits_type, npos>
524 (data(), size(), __s.data(), __pos, __s.size());
525 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000526
Marshall Clowb7db4972017-11-15 20:02:27 +0000527 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
528 size_type find_last_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT
529 { return rfind(__c, __pos); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000530
Marshall Clowb7db4972017-11-15 20:02:27 +0000531 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800532 size_type find_last_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000533 {
534 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_of(): received nullptr");
535 return __str_find_last_of<value_type, size_type, traits_type, npos>
536 (data(), size(), __s, __pos, __n);
537 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000538
Marshall Clowb7db4972017-11-15 20:02:27 +0000539 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800540 size_type find_last_of(const _CharT* __s, size_type __pos=npos) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000541 {
542 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_of(): received nullptr");
543 return __str_find_last_of<value_type, size_type, traits_type, npos>
544 (data(), size(), __s, __pos, traits_type::length(__s));
545 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000546
Marshall Clowb7db4972017-11-15 20:02:27 +0000547 // find_first_not_of
548 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
549 size_type find_first_not_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_not_of(): received nullptr");
552 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
553 (data(), size(), __s.data(), __pos, __s.size());
554 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000555
Marshall Clowb7db4972017-11-15 20:02:27 +0000556 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
557 size_type find_first_not_of(_CharT __c, size_type __pos=0) const _NOEXCEPT
558 {
559 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
560 (data(), size(), __c, __pos);
561 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000562
Marshall Clowb7db4972017-11-15 20:02:27 +0000563 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800564 size_type find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000565 {
566 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_not_of(): received nullptr");
567 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
568 (data(), size(), __s, __pos, __n);
569 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000570
Marshall Clowb7db4972017-11-15 20:02:27 +0000571 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800572 size_type find_first_not_of(const _CharT* __s, size_type __pos=0) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000573 {
574 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_not_of(): received nullptr");
575 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
576 (data(), size(), __s, __pos, traits_type::length(__s));
577 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000578
Marshall Clowb7db4972017-11-15 20:02:27 +0000579 // find_last_not_of
580 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
581 size_type find_last_not_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
582 {
583 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_not_of(): received nullptr");
584 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
585 (data(), size(), __s.data(), __pos, __s.size());
586 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000587
Marshall Clowb7db4972017-11-15 20:02:27 +0000588 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
589 size_type find_last_not_of(_CharT __c, size_type __pos=npos) const _NOEXCEPT
590 {
591 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
592 (data(), size(), __c, __pos);
593 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000594
Marshall Clowb7db4972017-11-15 20:02:27 +0000595 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800596 size_type find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000597 {
598 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_not_of(): received nullptr");
599 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
600 (data(), size(), __s, __pos, __n);
601 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000602
Marshall Clowb7db4972017-11-15 20:02:27 +0000603 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800604 size_type find_last_not_of(const _CharT* __s, size_type __pos=npos) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000605 {
606 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_not_of(): received nullptr");
607 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
608 (data(), size(), __s, __pos, traits_type::length(__s));
609 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000610
Marshall Clow18c293b2017-12-04 20:11:38 +0000611#if _LIBCPP_STD_VER > 17
612 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
613 bool starts_with(basic_string_view __s) const _NOEXCEPT
614 { return size() >= __s.size() && compare(0, __s.size(), __s) == 0; }
615
616 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
617 bool starts_with(value_type __c) const _NOEXCEPT
618 { return !empty() && _Traits::eq(front(), __c); }
619
620 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
621 bool starts_with(const value_type* __s) const _NOEXCEPT
622 { return starts_with(basic_string_view(__s)); }
623
624 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
625 bool ends_with(basic_string_view __s) const _NOEXCEPT
626 { return size() >= __s.size() && compare(size() - __s.size(), npos, __s) == 0; }
627
628 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
629 bool ends_with(value_type __c) const _NOEXCEPT
630 { return !empty() && _Traits::eq(back(), __c); }
631
632 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
633 bool ends_with(const value_type* __s) const _NOEXCEPT
634 { return ends_with(basic_string_view(__s)); }
635#endif
636
Wim Leflere023c3542021-01-19 14:33:30 -0500637#if _LIBCPP_STD_VER > 20
638 constexpr _LIBCPP_INLINE_VISIBILITY
639 bool contains(basic_string_view __sv) const noexcept
640 { return find(__sv) != npos; }
641
642 constexpr _LIBCPP_INLINE_VISIBILITY
643 bool contains(value_type __c) const noexcept
644 { return find(__c) != npos; }
645
646 constexpr _LIBCPP_INLINE_VISIBILITY
647 bool contains(const value_type* __s) const
648 { return find(__s) != npos; }
649#endif
650
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000651private:
Marshall Clowb7db4972017-11-15 20:02:27 +0000652 const value_type* __data;
653 size_type __size;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000654};
655
Mark de Weveraf59b2d2020-11-24 18:08:02 +0100656#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_RANGES)
657template <class _CharT, class _Traits>
658inline constexpr bool ranges::enable_borrowed_range<basic_string_view<_CharT, _Traits> > = true;
659#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_RANGES)
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000660
661// [string.view.comparison]
662// operator ==
663template<class _CharT, class _Traits>
664_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
665bool operator==(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000666 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000667{
Marshall Clowb7db4972017-11-15 20:02:27 +0000668 if ( __lhs.size() != __rhs.size()) return false;
669 return __lhs.compare(__rhs) == 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000670}
671
672template<class _CharT, class _Traits>
673_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
674bool operator==(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000675 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000676{
Marshall Clowb7db4972017-11-15 20:02:27 +0000677 if ( __lhs.size() != __rhs.size()) return false;
678 return __lhs.compare(__rhs) == 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000679}
680
681template<class _CharT, class _Traits>
682_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000683bool operator==(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000684 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000685{
Marshall Clowb7db4972017-11-15 20:02:27 +0000686 if ( __lhs.size() != __rhs.size()) return false;
687 return __lhs.compare(__rhs) == 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000688}
689
690
691// operator !=
692template<class _CharT, class _Traits>
693_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
694bool operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
695{
Marshall Clowb7db4972017-11-15 20:02:27 +0000696 if ( __lhs.size() != __rhs.size())
697 return true;
698 return __lhs.compare(__rhs) != 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000699}
700
701template<class _CharT, class _Traits>
702_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
703bool operator!=(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000704 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000705{
Marshall Clowb7db4972017-11-15 20:02:27 +0000706 if ( __lhs.size() != __rhs.size())
707 return true;
708 return __lhs.compare(__rhs) != 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000709}
710
711template<class _CharT, class _Traits>
712_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000713bool operator!=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000714 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000715{
Marshall Clowb7db4972017-11-15 20:02:27 +0000716 if ( __lhs.size() != __rhs.size())
717 return true;
718 return __lhs.compare(__rhs) != 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000719}
720
721
722// operator <
723template<class _CharT, class _Traits>
724_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
725bool operator<(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
726{
Marshall Clowb7db4972017-11-15 20:02:27 +0000727 return __lhs.compare(__rhs) < 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000728}
729
730template<class _CharT, class _Traits>
731_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
732bool operator<(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000733 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000734{
Marshall Clowb7db4972017-11-15 20:02:27 +0000735 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
Louis Dionne173f29e2019-05-29 16:01:36 +0000740bool operator<(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000741 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000742{
Marshall Clowb7db4972017-11-15 20:02:27 +0000743 return __lhs.compare(__rhs) < 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000744}
745
746
747// operator >
748template<class _CharT, class _Traits>
749_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
750bool operator> (basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
751{
Marshall Clowb7db4972017-11-15 20:02:27 +0000752 return __lhs.compare(__rhs) > 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000753}
754
755template<class _CharT, class _Traits>
756_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
757bool operator>(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000758 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000759{
Marshall Clowb7db4972017-11-15 20:02:27 +0000760 return __lhs.compare(__rhs) > 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000761}
762
763template<class _CharT, class _Traits>
764_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000765bool operator>(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000766 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000767{
Marshall Clowb7db4972017-11-15 20:02:27 +0000768 return __lhs.compare(__rhs) > 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000769}
770
771
772// operator <=
773template<class _CharT, class _Traits>
774_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
775bool operator<=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
776{
Marshall Clowb7db4972017-11-15 20:02:27 +0000777 return __lhs.compare(__rhs) <= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000778}
779
780template<class _CharT, class _Traits>
781_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
782bool operator<=(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000783 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000784{
Marshall Clowb7db4972017-11-15 20:02:27 +0000785 return __lhs.compare(__rhs) <= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000786}
787
788template<class _CharT, class _Traits>
789_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000790bool operator<=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000791 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000792{
Marshall Clowb7db4972017-11-15 20:02:27 +0000793 return __lhs.compare(__rhs) <= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000794}
795
796
797// operator >=
798template<class _CharT, class _Traits>
799_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
800bool operator>=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
801{
Marshall Clowb7db4972017-11-15 20:02:27 +0000802 return __lhs.compare(__rhs) >= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000803}
804
805
806template<class _CharT, class _Traits>
807_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
808bool operator>=(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000809 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000810{
Marshall Clowb7db4972017-11-15 20:02:27 +0000811 return __lhs.compare(__rhs) >= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000812}
813
814template<class _CharT, class _Traits>
815_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000816bool operator>=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000817 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000818{
Marshall Clowb7db4972017-11-15 20:02:27 +0000819 return __lhs.compare(__rhs) >= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000820}
821
Eric Fiselier14116922019-09-25 18:56:54 +0000822
823template<class _CharT, class _Traits>
824basic_ostream<_CharT, _Traits>&
825operator<<(basic_ostream<_CharT, _Traits>& __os,
826 basic_string_view<_CharT, _Traits> __str);
827
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000828// [string.view.hash]
Marshall Clowab313df2019-06-27 14:18:32 +0000829template<class _CharT>
830struct _LIBCPP_TEMPLATE_VIS hash<basic_string_view<_CharT, char_traits<_CharT> > >
831 : public unary_function<basic_string_view<_CharT, char_traits<_CharT> >, size_t>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000832{
Louis Dionned83f2e62018-11-28 15:22:30 +0000833 _LIBCPP_INLINE_VISIBILITY
Marshall Clowab313df2019-06-27 14:18:32 +0000834 size_t operator()(const basic_string_view<_CharT, char_traits<_CharT> > __val) const _NOEXCEPT {
Louis Dionned83f2e62018-11-28 15:22:30 +0000835 return __do_string_hash(__val.data(), __val.data() + __val.size());
836 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000837};
838
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000839
Louis Dionne173f29e2019-05-29 16:01:36 +0000840#if _LIBCPP_STD_VER > 11
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000841inline namespace literals
842{
843 inline namespace string_view_literals
844 {
845 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clowabe6daf2017-10-24 14:06:00 +0000846 basic_string_view<char> operator "" sv(const char *__str, size_t __len) _NOEXCEPT
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000847 {
848 return basic_string_view<char> (__str, __len);
849 }
850
851 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clowabe6daf2017-10-24 14:06:00 +0000852 basic_string_view<wchar_t> operator "" sv(const wchar_t *__str, size_t __len) _NOEXCEPT
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000853 {
854 return basic_string_view<wchar_t> (__str, __len);
855 }
856
Arthur O'Dwyerafa5d5f2021-04-18 21:47:08 -0400857#ifndef _LIBCPP_HAS_NO_CHAR8_T
Marshall Clow8732fed2018-12-11 04:35:44 +0000858 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
859 basic_string_view<char8_t> operator "" sv(const char8_t *__str, size_t __len) _NOEXCEPT
860 {
861 return basic_string_view<char8_t> (__str, __len);
862 }
863#endif
864
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000865 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clowabe6daf2017-10-24 14:06:00 +0000866 basic_string_view<char16_t> operator "" sv(const char16_t *__str, size_t __len) _NOEXCEPT
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000867 {
868 return basic_string_view<char16_t> (__str, __len);
869 }
870
871 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clowabe6daf2017-10-24 14:06:00 +0000872 basic_string_view<char32_t> operator "" sv(const char32_t *__str, size_t __len) _NOEXCEPT
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000873 {
874 return basic_string_view<char32_t> (__str, __len);
875 }
876 }
877}
878#endif
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000879_LIBCPP_END_NAMESPACE_STD
880
Eric Fiselierf4433a32017-05-31 22:07:49 +0000881_LIBCPP_POP_MACROS
882
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000883#endif // _LIBCPP_STRING_VIEW