blob: f8c63571f3c42e685f90221e4361adc25b20100c [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
22 // 7.9, basic_string_view non-member comparison functions
23 template<class charT, class traits>
24 constexpr bool operator==(basic_string_view<charT, traits> x,
25 basic_string_view<charT, traits> y) noexcept;
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 // see below, sufficient additional overloads of comparison functions
42
43 // 7.10, Inserters and extractors
44 template<class charT, class traits>
45 basic_ostream<charT, traits>&
46 operator<<(basic_ostream<charT, traits>& os,
47 basic_string_view<charT, traits> str);
48
49 // basic_string_view typedef names
50 typedef basic_string_view<char> string_view;
51 typedef basic_string_view<char16_t> u16string_view;
52 typedef basic_string_view<char32_t> u32string_view;
53 typedef basic_string_view<wchar_t> wstring_view;
54
55 template<class charT, class traits = char_traits<charT>>
56 class basic_string_view {
57 public:
58 // types
59 typedef traits traits_type;
60 typedef charT value_type;
61 typedef charT* pointer;
62 typedef const charT* const_pointer;
63 typedef charT& reference;
64 typedef const charT& const_reference;
65 typedef implementation-defined const_iterator;
66 typedef const_iterator iterator;
67 typedef reverse_iterator<const_iterator> const_reverse_iterator;
68 typedef const_reverse_iterator reverse_iterator;
69 typedef size_t size_type;
70 typedef ptrdiff_t difference_type;
71 static constexpr size_type npos = size_type(-1);
72
73 // 7.3, basic_string_view constructors and assignment operators
74 constexpr basic_string_view() noexcept;
75 constexpr basic_string_view(const basic_string_view&) noexcept = default;
76 basic_string_view& operator=(const basic_string_view&) noexcept = default;
77 template<class Allocator>
78 constexpr basic_string_view(const charT* str);
79 constexpr basic_string_view(const charT* str, size_type len);
80
81 // 7.4, basic_string_view iterator support
82 constexpr const_iterator begin() const noexcept;
83 constexpr const_iterator end() const noexcept;
84 constexpr const_iterator cbegin() const noexcept;
85 constexpr const_iterator cend() const noexcept;
86 const_reverse_iterator rbegin() const noexcept;
87 const_reverse_iterator rend() const noexcept;
88 const_reverse_iterator crbegin() const noexcept;
89 const_reverse_iterator crend() const noexcept;
90
91 // 7.5, basic_string_view capacity
92 constexpr size_type size() const noexcept;
93 constexpr size_type length() const noexcept;
94 constexpr size_type max_size() const noexcept;
95 constexpr bool empty() const noexcept;
96
97 // 7.6, basic_string_view element access
98 constexpr const_reference operator[](size_type pos) const;
99 constexpr const_reference at(size_type pos) const;
100 constexpr const_reference front() const;
101 constexpr const_reference back() const;
102 constexpr const_pointer data() const noexcept;
103
104 // 7.7, basic_string_view modifiers
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000105 constexpr void remove_prefix(size_type n);
106 constexpr void remove_suffix(size_type n);
107 constexpr void swap(basic_string_view& s) noexcept;
108
109 size_type copy(charT* s, size_type n, size_type pos = 0) const;
110
111 constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const;
112 constexpr int compare(basic_string_view s) const noexcept;
113 constexpr int compare(size_type pos1, size_type n1, basic_string_view s) const;
114 constexpr int compare(size_type pos1, size_type n1,
115 basic_string_view s, size_type pos2, size_type n2) const;
116 constexpr int compare(const charT* s) const;
117 constexpr int compare(size_type pos1, size_type n1, const charT* s) const;
118 constexpr int compare(size_type pos1, size_type n1,
119 const charT* s, size_type n2) const;
120 constexpr size_type find(basic_string_view s, size_type pos = 0) const noexcept;
121 constexpr size_type find(charT c, size_type pos = 0) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800122 constexpr size_type find(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
123 constexpr size_type find(const charT* s, size_type pos = 0) const noexcept; // noexcept as an extension
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000124 constexpr size_type rfind(basic_string_view s, size_type pos = npos) const noexcept;
125 constexpr size_type rfind(charT c, size_type pos = npos) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800126 constexpr size_type rfind(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
127 constexpr size_type rfind(const charT* s, size_type pos = npos) const noexcept; // noexcept as an extension
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000128 constexpr size_type find_first_of(basic_string_view s, size_type pos = 0) const noexcept;
129 constexpr size_type find_first_of(charT c, size_type pos = 0) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800130 constexpr size_type find_first_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
131 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 +0000132 constexpr size_type find_last_of(basic_string_view s, size_type pos = npos) const noexcept;
133 constexpr size_type find_last_of(charT c, size_type pos = npos) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800134 constexpr size_type find_last_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
135 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 +0000136 constexpr size_type find_first_not_of(basic_string_view s, size_type pos = 0) const noexcept;
137 constexpr size_type find_first_not_of(charT c, size_type pos = 0) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800138 constexpr size_type find_first_not_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
139 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 +0000140 constexpr size_type find_last_not_of(basic_string_view s, size_type pos = npos) const noexcept;
141 constexpr size_type find_last_not_of(charT c, size_type pos = npos) const noexcept;
zoecarver1997e0a2021-02-05 11:54:47 -0800142 constexpr size_type find_last_not_of(const charT* s, size_type pos, size_type n) const noexcept; // noexcept as an extension
143 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 +0000144
Marek Kurdej24b4c512021-01-07 12:29:04 +0100145 constexpr bool starts_with(basic_string_view s) const noexcept; // C++20
146 constexpr bool starts_with(charT c) const noexcept; // C++20
147 constexpr bool starts_with(const charT* s) const; // C++20
148 constexpr bool ends_with(basic_string_view s) const noexcept; // C++20
149 constexpr bool ends_with(charT c) const noexcept; // C++20
150 constexpr bool ends_with(const charT* s) const; // C++20
Marshall Clow18c293b2017-12-04 20:11:38 +0000151
Wim Leflere023c3542021-01-19 14:33:30 -0500152 constexpr bool contains(basic_string_view s) const noexcept; // C++2b
153 constexpr bool contains(charT c) const noexcept; // C++2b
154 constexpr bool contains(const charT* s) const; // C++2b
155
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000156 private:
157 const_pointer data_; // exposition only
158 size_type size_; // exposition only
159 };
160
161 // 7.11, Hash support
162 template <class T> struct hash;
163 template <> struct hash<string_view>;
164 template <> struct hash<u16string_view>;
165 template <> struct hash<u32string_view>;
166 template <> struct hash<wstring_view>;
167
Marshall Clowabe6daf2017-10-24 14:06:00 +0000168 constexpr basic_string_view<char> operator "" sv( const char *str, size_t len ) noexcept;
169 constexpr basic_string_view<wchar_t> operator "" sv( const wchar_t *str, size_t len ) noexcept;
170 constexpr basic_string_view<char16_t> operator "" sv( const char16_t *str, size_t len ) noexcept;
171 constexpr basic_string_view<char32_t> operator "" sv( const char32_t *str, size_t len ) noexcept;
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000172
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000173} // namespace std
174
175
176*/
177
178#include <__config>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000179#include <__string>
Eric Fiselier14116922019-09-25 18:56:54 +0000180#include <iosfwd>
Eric Fiselier2d357f72016-12-05 23:53:23 +0000181#include <algorithm>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000182#include <iterator>
Eric Fiselier2d357f72016-12-05 23:53:23 +0000183#include <limits>
184#include <stdexcept>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000185#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000186#include <__debug>
187
188#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
189#pragma GCC system_header
190#endif
191
Eric Fiselierf4433a32017-05-31 22:07:49 +0000192_LIBCPP_PUSH_MACROS
193#include <__undef_macros>
194
195
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000196_LIBCPP_BEGIN_NAMESPACE_STD
197
198template<class _CharT, class _Traits = char_traits<_CharT> >
Richard Smith256954d2020-11-11 17:12:18 -0800199 class _LIBCPP_TEMPLATE_VIS basic_string_view;
200
201typedef basic_string_view<char> string_view;
202#ifndef _LIBCPP_NO_HAS_CHAR8_T
203typedef basic_string_view<char8_t> u8string_view;
204#endif
205typedef basic_string_view<char16_t> u16string_view;
206typedef basic_string_view<char32_t> u32string_view;
207typedef basic_string_view<wchar_t> wstring_view;
208
209template<class _CharT, class _Traits>
210class
211 _LIBCPP_PREFERRED_NAME(string_view)
212#ifndef _LIBCPP_NO_HAS_CHAR8_T
213 _LIBCPP_PREFERRED_NAME(u8string_view)
214#endif
215 _LIBCPP_PREFERRED_NAME(u16string_view)
216 _LIBCPP_PREFERRED_NAME(u32string_view)
217 _LIBCPP_PREFERRED_NAME(wstring_view)
218 basic_string_view {
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000219public:
Marshall Clowb7db4972017-11-15 20:02:27 +0000220 // types
221 typedef _Traits traits_type;
222 typedef _CharT value_type;
Marshall Clow7d2c5182017-12-20 16:31:40 +0000223 typedef _CharT* pointer;
Marshall Clowb7db4972017-11-15 20:02:27 +0000224 typedef const _CharT* const_pointer;
Marshall Clow7d2c5182017-12-20 16:31:40 +0000225 typedef _CharT& reference;
Marshall Clowb7db4972017-11-15 20:02:27 +0000226 typedef const _CharT& const_reference;
227 typedef const_pointer const_iterator; // See [string.view.iterators]
228 typedef const_iterator iterator;
229 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
230 typedef const_reverse_iterator reverse_iterator;
231 typedef size_t size_type;
232 typedef ptrdiff_t difference_type;
233 static _LIBCPP_CONSTEXPR const size_type npos = -1; // size_type(-1);
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000234
Marshall Clow79f33542018-03-21 00:36:05 +0000235 static_assert((!is_array<value_type>::value), "Character type of basic_string_view must not be an array");
236 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string_view must be standard-layout");
237 static_assert(( is_trivial<value_type>::value), "Character type of basic_string_view must be trivial");
Marshall Clowa3a74e02017-03-15 18:41:11 +0000238 static_assert((is_same<_CharT, typename traits_type::char_type>::value),
239 "traits_type::char_type must be the same type as CharT");
240
Marshall Clowb7db4972017-11-15 20:02:27 +0000241 // [string.view.cons], construct/copy
242 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
243 basic_string_view() _NOEXCEPT : __data (nullptr), __size(0) {}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000244
Marshall Clowb7db4972017-11-15 20:02:27 +0000245 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
246 basic_string_view(const basic_string_view&) _NOEXCEPT = default;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000247
Marshall Clowb7db4972017-11-15 20:02:27 +0000248 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
249 basic_string_view& operator=(const basic_string_view&) _NOEXCEPT = default;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000250
Marshall Clowb7db4972017-11-15 20:02:27 +0000251 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
252 basic_string_view(const _CharT* __s, size_type __len) _NOEXCEPT
253 : __data(__s), __size(__len)
254 {
Marshall Clowca1bcdb2019-06-04 02:07:11 +0000255#if _LIBCPP_STD_VER > 11
256 _LIBCPP_ASSERT(__len == 0 || __s != nullptr, "string_view::string_view(_CharT *, size_t): received nullptr");
257#endif
Marshall Clowb7db4972017-11-15 20:02:27 +0000258 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000259
Marshall Clowb7db4972017-11-15 20:02:27 +0000260 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
261 basic_string_view(const _CharT* __s)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500262 : __data(__s), __size(_VSTD::__char_traits_length_checked<_Traits>(__s)) {}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000263
Marshall Clowb7db4972017-11-15 20:02:27 +0000264 // [string.view.iterators], iterators
265 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
266 const_iterator begin() const _NOEXCEPT { return cbegin(); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000267
Marshall Clowb7db4972017-11-15 20:02:27 +0000268 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
269 const_iterator end() const _NOEXCEPT { return cend(); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000270
Marshall Clowb7db4972017-11-15 20:02:27 +0000271 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
272 const_iterator cbegin() const _NOEXCEPT { return __data; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000273
Marshall Clowb7db4972017-11-15 20:02:27 +0000274 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
275 const_iterator cend() const _NOEXCEPT { return __data + __size; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000276
Marshall Clowb7db4972017-11-15 20:02:27 +0000277 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
278 const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000279
Marshall Clowb7db4972017-11-15 20:02:27 +0000280 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
281 const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000282
Marshall Clowb7db4972017-11-15 20:02:27 +0000283 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
284 const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000285
Marshall Clowb7db4972017-11-15 20:02:27 +0000286 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
287 const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000288
Marshall Clowb7db4972017-11-15 20:02:27 +0000289 // [string.view.capacity], capacity
290 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
291 size_type size() const _NOEXCEPT { return __size; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000292
Marshall Clowb7db4972017-11-15 20:02:27 +0000293 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
294 size_type length() const _NOEXCEPT { return __size; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000295
Marshall Clowb7db4972017-11-15 20:02:27 +0000296 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
297 size_type max_size() const _NOEXCEPT { return numeric_limits<size_type>::max(); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000298
Marshall Clowb7db4972017-11-15 20:02:27 +0000299 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
300 bool empty() const _NOEXCEPT { return __size == 0; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000301
Marshall Clowb7db4972017-11-15 20:02:27 +0000302 // [string.view.access], element access
303 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Chris Palmer1b114a52020-10-06 13:01:50 -0400304 const_reference operator[](size_type __pos) const _NOEXCEPT {
305 return _LIBCPP_ASSERT(__pos < size(), "string_view[] index out of bounds"), __data[__pos];
306 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000307
Marshall Clowb7db4972017-11-15 20:02:27 +0000308 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
309 const_reference at(size_type __pos) const
310 {
311 return __pos >= size()
312 ? (__throw_out_of_range("string_view::at"), __data[0])
313 : __data[__pos];
314 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000315
Marshall Clowb7db4972017-11-15 20:02:27 +0000316 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +0000317 const_reference front() const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000318 {
319 return _LIBCPP_ASSERT(!empty(), "string_view::front(): string is empty"), __data[0];
320 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000321
Marshall Clowb7db4972017-11-15 20:02:27 +0000322 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +0000323 const_reference back() const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000324 {
325 return _LIBCPP_ASSERT(!empty(), "string_view::back(): string is empty"), __data[__size-1];
326 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000327
Marshall Clowb7db4972017-11-15 20:02:27 +0000328 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
329 const_pointer data() const _NOEXCEPT { return __data; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000330
Marshall Clowb7db4972017-11-15 20:02:27 +0000331 // [string.view.modifiers], modifiers:
332 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
333 void remove_prefix(size_type __n) _NOEXCEPT
334 {
335 _LIBCPP_ASSERT(__n <= size(), "remove_prefix() can't remove more than size()");
336 __data += __n;
337 __size -= __n;
338 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000339
Marshall Clowb7db4972017-11-15 20:02:27 +0000340 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
341 void remove_suffix(size_type __n) _NOEXCEPT
342 {
343 _LIBCPP_ASSERT(__n <= size(), "remove_suffix() can't remove more than size()");
344 __size -= __n;
345 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000346
Marshall Clowb7db4972017-11-15 20:02:27 +0000347 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
348 void swap(basic_string_view& __other) _NOEXCEPT
349 {
350 const value_type *__p = __data;
351 __data = __other.__data;
352 __other.__data = __p;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000353
Marshall Clowb7db4972017-11-15 20:02:27 +0000354 size_type __sz = __size;
355 __size = __other.__size;
356 __other.__size = __sz;
357 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000358
Marshall Clowb7db4972017-11-15 20:02:27 +0000359 _LIBCPP_INLINE_VISIBILITY
360 size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const
361 {
362 if (__pos > size())
363 __throw_out_of_range("string_view::copy");
364 size_type __rlen = _VSTD::min(__n, size() - __pos);
365 _Traits::copy(__s, data() + __pos, __rlen);
366 return __rlen;
367 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000368
Marshall Clowb7db4972017-11-15 20:02:27 +0000369 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
370 basic_string_view substr(size_type __pos = 0, size_type __n = npos) const
371 {
372 return __pos > size()
373 ? (__throw_out_of_range("string_view::substr"), basic_string_view())
374 : basic_string_view(data() + __pos, _VSTD::min(__n, size() - __pos));
375 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000376
Marshall Clowb7db4972017-11-15 20:02:27 +0000377 _LIBCPP_CONSTEXPR_AFTER_CXX11 int compare(basic_string_view __sv) const _NOEXCEPT
378 {
379 size_type __rlen = _VSTD::min( size(), __sv.size());
380 int __retval = _Traits::compare(data(), __sv.data(), __rlen);
381 if ( __retval == 0 ) // first __rlen chars matched
382 __retval = size() == __sv.size() ? 0 : ( size() < __sv.size() ? -1 : 1 );
383 return __retval;
384 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000385
Marshall Clowb7db4972017-11-15 20:02:27 +0000386 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
387 int compare(size_type __pos1, size_type __n1, basic_string_view __sv) const
388 {
389 return substr(__pos1, __n1).compare(__sv);
390 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000391
Marshall Clowb7db4972017-11-15 20:02:27 +0000392 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000393 int compare( size_type __pos1, size_type __n1,
Marshall Clowb7db4972017-11-15 20:02:27 +0000394 basic_string_view __sv, size_type __pos2, size_type __n2) const
395 {
396 return substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
397 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000398
Marshall Clowb7db4972017-11-15 20:02:27 +0000399 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
400 int compare(const _CharT* __s) const _NOEXCEPT
401 {
402 return compare(basic_string_view(__s));
403 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000404
Marshall Clowb7db4972017-11-15 20:02:27 +0000405 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
406 int compare(size_type __pos1, size_type __n1, const _CharT* __s) const
407 {
408 return substr(__pos1, __n1).compare(basic_string_view(__s));
409 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000410
Marshall Clowb7db4972017-11-15 20:02:27 +0000411 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
412 int compare(size_type __pos1, size_type __n1, const _CharT* __s, size_type __n2) const
413 {
414 return substr(__pos1, __n1).compare(basic_string_view(__s, __n2));
415 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000416
Marshall Clowb7db4972017-11-15 20:02:27 +0000417 // find
418 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
419 size_type find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
420 {
421 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
422 return __str_find<value_type, size_type, traits_type, npos>
423 (data(), size(), __s.data(), __pos, __s.size());
424 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000425
Marshall Clowb7db4972017-11-15 20:02:27 +0000426 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
427 size_type find(_CharT __c, size_type __pos = 0) const _NOEXCEPT
428 {
429 return __str_find<value_type, size_type, traits_type, npos>
430 (data(), size(), __c, __pos);
431 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000432
Marshall Clowb7db4972017-11-15 20:02:27 +0000433 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800434 size_type find(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000435 {
436 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find(): received nullptr");
437 return __str_find<value_type, size_type, traits_type, npos>
438 (data(), size(), __s, __pos, __n);
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 = 0) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000443 {
444 _LIBCPP_ASSERT(__s != nullptr, "string_view::find(): received nullptr");
445 return __str_find<value_type, size_type, traits_type, npos>
446 (data(), size(), __s, __pos, traits_type::length(__s));
447 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000448
Marshall Clowb7db4972017-11-15 20:02:27 +0000449 // rfind
450 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
451 size_type rfind(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT
452 {
453 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
454 return __str_rfind<value_type, size_type, traits_type, npos>
455 (data(), size(), __s.data(), __pos, __s.size());
456 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000457
Marshall Clowb7db4972017-11-15 20:02:27 +0000458 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
459 size_type rfind(_CharT __c, size_type __pos = npos) const _NOEXCEPT
460 {
461 return __str_rfind<value_type, size_type, traits_type, npos>
462 (data(), size(), __c, __pos);
463 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000464
Marshall Clowb7db4972017-11-15 20:02:27 +0000465 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800466 size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000467 {
468 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::rfind(): received nullptr");
469 return __str_rfind<value_type, size_type, traits_type, npos>
470 (data(), size(), __s, __pos, __n);
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=npos) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000475 {
476 _LIBCPP_ASSERT(__s != nullptr, "string_view::rfind(): received nullptr");
477 return __str_rfind<value_type, size_type, traits_type, npos>
478 (data(), size(), __s, __pos, traits_type::length(__s));
479 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000480
Marshall Clowb7db4972017-11-15 20:02:27 +0000481 // find_first_of
482 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
483 size_type find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
484 {
485 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_of(): received nullptr");
486 return __str_find_first_of<value_type, size_type, traits_type, npos>
487 (data(), size(), __s.data(), __pos, __s.size());
488 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000489
Marshall Clowb7db4972017-11-15 20:02:27 +0000490 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
491 size_type find_first_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT
492 { return find(__c, __pos); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000493
Marshall Clowb7db4972017-11-15 20:02:27 +0000494 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800495 size_type find_first_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000496 {
497 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_of(): received nullptr");
498 return __str_find_first_of<value_type, size_type, traits_type, npos>
499 (data(), size(), __s, __pos, __n);
500 }
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=0) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000504 {
505 _LIBCPP_ASSERT(__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, traits_type::length(__s));
508 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000509
Marshall Clowb7db4972017-11-15 20:02:27 +0000510 // find_last_of
511 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
512 size_type find_last_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
513 {
514 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_of(): received nullptr");
515 return __str_find_last_of<value_type, size_type, traits_type, npos>
516 (data(), size(), __s.data(), __pos, __s.size());
517 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000518
Marshall Clowb7db4972017-11-15 20:02:27 +0000519 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
520 size_type find_last_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT
521 { return rfind(__c, __pos); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000522
Marshall Clowb7db4972017-11-15 20:02:27 +0000523 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800524 size_type find_last_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000525 {
526 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_of(): received nullptr");
527 return __str_find_last_of<value_type, size_type, traits_type, npos>
528 (data(), size(), __s, __pos, __n);
529 }
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=npos) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000533 {
534 _LIBCPP_ASSERT(__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, traits_type::length(__s));
537 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000538
Marshall Clowb7db4972017-11-15 20:02:27 +0000539 // find_first_not_of
540 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
541 size_type find_first_not_of(basic_string_view __s, size_type __pos=0) const _NOEXCEPT
542 {
543 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_not_of(): received nullptr");
544 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
545 (data(), size(), __s.data(), __pos, __s.size());
546 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000547
Marshall Clowb7db4972017-11-15 20:02:27 +0000548 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
549 size_type find_first_not_of(_CharT __c, size_type __pos=0) const _NOEXCEPT
550 {
551 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
552 (data(), size(), __c, __pos);
553 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000554
Marshall Clowb7db4972017-11-15 20:02:27 +0000555 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800556 size_type find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000557 {
558 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_not_of(): received nullptr");
559 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
560 (data(), size(), __s, __pos, __n);
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=0) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000565 {
566 _LIBCPP_ASSERT(__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, traits_type::length(__s));
569 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000570
Marshall Clowb7db4972017-11-15 20:02:27 +0000571 // find_last_not_of
572 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
573 size_type find_last_not_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
574 {
575 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_not_of(): received nullptr");
576 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
577 (data(), size(), __s.data(), __pos, __s.size());
578 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000579
Marshall Clowb7db4972017-11-15 20:02:27 +0000580 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
581 size_type find_last_not_of(_CharT __c, size_type __pos=npos) const _NOEXCEPT
582 {
583 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
584 (data(), size(), __c, __pos);
585 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000586
Marshall Clowb7db4972017-11-15 20:02:27 +0000587 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
zoecarver1997e0a2021-02-05 11:54:47 -0800588 size_type find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000589 {
590 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_not_of(): received nullptr");
591 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
592 (data(), size(), __s, __pos, __n);
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=npos) const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000597 {
598 _LIBCPP_ASSERT(__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, traits_type::length(__s));
601 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000602
Marshall Clow18c293b2017-12-04 20:11:38 +0000603#if _LIBCPP_STD_VER > 17
604 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
605 bool starts_with(basic_string_view __s) const _NOEXCEPT
606 { return size() >= __s.size() && compare(0, __s.size(), __s) == 0; }
607
608 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
609 bool starts_with(value_type __c) const _NOEXCEPT
610 { return !empty() && _Traits::eq(front(), __c); }
611
612 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
613 bool starts_with(const value_type* __s) const _NOEXCEPT
614 { return starts_with(basic_string_view(__s)); }
615
616 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
617 bool ends_with(basic_string_view __s) const _NOEXCEPT
618 { return size() >= __s.size() && compare(size() - __s.size(), npos, __s) == 0; }
619
620 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
621 bool ends_with(value_type __c) const _NOEXCEPT
622 { return !empty() && _Traits::eq(back(), __c); }
623
624 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
625 bool ends_with(const value_type* __s) const _NOEXCEPT
626 { return ends_with(basic_string_view(__s)); }
627#endif
628
Wim Leflere023c3542021-01-19 14:33:30 -0500629#if _LIBCPP_STD_VER > 20
630 constexpr _LIBCPP_INLINE_VISIBILITY
631 bool contains(basic_string_view __sv) const noexcept
632 { return find(__sv) != npos; }
633
634 constexpr _LIBCPP_INLINE_VISIBILITY
635 bool contains(value_type __c) const noexcept
636 { return find(__c) != npos; }
637
638 constexpr _LIBCPP_INLINE_VISIBILITY
639 bool contains(const value_type* __s) const
640 { return find(__s) != npos; }
641#endif
642
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000643private:
Marshall Clowb7db4972017-11-15 20:02:27 +0000644 const value_type* __data;
645 size_type __size;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000646};
647
648
649// [string.view.comparison]
650// operator ==
651template<class _CharT, class _Traits>
652_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
653bool operator==(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000654 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000655{
Marshall Clowb7db4972017-11-15 20:02:27 +0000656 if ( __lhs.size() != __rhs.size()) return false;
657 return __lhs.compare(__rhs) == 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000658}
659
660template<class _CharT, class _Traits>
661_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
662bool operator==(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000663 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000664{
Marshall Clowb7db4972017-11-15 20:02:27 +0000665 if ( __lhs.size() != __rhs.size()) return false;
666 return __lhs.compare(__rhs) == 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000667}
668
669template<class _CharT, class _Traits>
670_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000671bool operator==(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000672 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000673{
Marshall Clowb7db4972017-11-15 20:02:27 +0000674 if ( __lhs.size() != __rhs.size()) return false;
675 return __lhs.compare(__rhs) == 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000676}
677
678
679// operator !=
680template<class _CharT, class _Traits>
681_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
682bool operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
683{
Marshall Clowb7db4972017-11-15 20:02:27 +0000684 if ( __lhs.size() != __rhs.size())
685 return true;
686 return __lhs.compare(__rhs) != 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000687}
688
689template<class _CharT, class _Traits>
690_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
691bool operator!=(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000692 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000693{
Marshall Clowb7db4972017-11-15 20:02:27 +0000694 if ( __lhs.size() != __rhs.size())
695 return true;
696 return __lhs.compare(__rhs) != 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000697}
698
699template<class _CharT, class _Traits>
700_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000701bool operator!=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000702 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000703{
Marshall Clowb7db4972017-11-15 20:02:27 +0000704 if ( __lhs.size() != __rhs.size())
705 return true;
706 return __lhs.compare(__rhs) != 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000707}
708
709
710// operator <
711template<class _CharT, class _Traits>
712_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
713bool operator<(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
714{
Marshall Clowb7db4972017-11-15 20:02:27 +0000715 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
720bool operator<(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000721 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000722{
Marshall Clowb7db4972017-11-15 20:02:27 +0000723 return __lhs.compare(__rhs) < 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000724}
725
726template<class _CharT, class _Traits>
727_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000728bool operator<(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000729 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000730{
Marshall Clowb7db4972017-11-15 20:02:27 +0000731 return __lhs.compare(__rhs) < 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000732}
733
734
735// operator >
736template<class _CharT, class _Traits>
737_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
738bool operator> (basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
739{
Marshall Clowb7db4972017-11-15 20:02:27 +0000740 return __lhs.compare(__rhs) > 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000741}
742
743template<class _CharT, class _Traits>
744_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
745bool operator>(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000746 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000747{
Marshall Clowb7db4972017-11-15 20:02:27 +0000748 return __lhs.compare(__rhs) > 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000749}
750
751template<class _CharT, class _Traits>
752_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000753bool operator>(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000754 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000755{
Marshall Clowb7db4972017-11-15 20:02:27 +0000756 return __lhs.compare(__rhs) > 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000757}
758
759
760// operator <=
761template<class _CharT, class _Traits>
762_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
763bool operator<=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
764{
Marshall Clowb7db4972017-11-15 20:02:27 +0000765 return __lhs.compare(__rhs) <= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000766}
767
768template<class _CharT, class _Traits>
769_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
770bool operator<=(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000771 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000772{
Marshall Clowb7db4972017-11-15 20:02:27 +0000773 return __lhs.compare(__rhs) <= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000774}
775
776template<class _CharT, class _Traits>
777_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000778bool operator<=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000779 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000780{
Marshall Clowb7db4972017-11-15 20:02:27 +0000781 return __lhs.compare(__rhs) <= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000782}
783
784
785// operator >=
786template<class _CharT, class _Traits>
787_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
788bool operator>=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
789{
Marshall Clowb7db4972017-11-15 20:02:27 +0000790 return __lhs.compare(__rhs) >= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000791}
792
793
794template<class _CharT, class _Traits>
795_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
796bool operator>=(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000797 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000798{
Marshall Clowb7db4972017-11-15 20:02:27 +0000799 return __lhs.compare(__rhs) >= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000800}
801
802template<class _CharT, class _Traits>
803_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000804bool operator>=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000805 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000806{
Marshall Clowb7db4972017-11-15 20:02:27 +0000807 return __lhs.compare(__rhs) >= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000808}
809
Eric Fiselier14116922019-09-25 18:56:54 +0000810
811template<class _CharT, class _Traits>
812basic_ostream<_CharT, _Traits>&
813operator<<(basic_ostream<_CharT, _Traits>& __os,
814 basic_string_view<_CharT, _Traits> __str);
815
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000816// [string.view.hash]
Marshall Clowab313df2019-06-27 14:18:32 +0000817template<class _CharT>
818struct _LIBCPP_TEMPLATE_VIS hash<basic_string_view<_CharT, char_traits<_CharT> > >
819 : public unary_function<basic_string_view<_CharT, char_traits<_CharT> >, size_t>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000820{
Louis Dionned83f2e62018-11-28 15:22:30 +0000821 _LIBCPP_INLINE_VISIBILITY
Marshall Clowab313df2019-06-27 14:18:32 +0000822 size_t operator()(const basic_string_view<_CharT, char_traits<_CharT> > __val) const _NOEXCEPT {
Louis Dionned83f2e62018-11-28 15:22:30 +0000823 return __do_string_hash(__val.data(), __val.data() + __val.size());
824 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000825};
826
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000827
Louis Dionne173f29e2019-05-29 16:01:36 +0000828#if _LIBCPP_STD_VER > 11
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000829inline namespace literals
830{
831 inline namespace string_view_literals
832 {
833 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clowabe6daf2017-10-24 14:06:00 +0000834 basic_string_view<char> operator "" sv(const char *__str, size_t __len) _NOEXCEPT
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000835 {
836 return basic_string_view<char> (__str, __len);
837 }
838
839 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clowabe6daf2017-10-24 14:06:00 +0000840 basic_string_view<wchar_t> operator "" sv(const wchar_t *__str, size_t __len) _NOEXCEPT
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000841 {
842 return basic_string_view<wchar_t> (__str, __len);
843 }
844
Marshall Clow8732fed2018-12-11 04:35:44 +0000845#ifndef _LIBCPP_NO_HAS_CHAR8_T
846 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
847 basic_string_view<char8_t> operator "" sv(const char8_t *__str, size_t __len) _NOEXCEPT
848 {
849 return basic_string_view<char8_t> (__str, __len);
850 }
851#endif
852
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000853 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clowabe6daf2017-10-24 14:06:00 +0000854 basic_string_view<char16_t> operator "" sv(const char16_t *__str, size_t __len) _NOEXCEPT
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000855 {
856 return basic_string_view<char16_t> (__str, __len);
857 }
858
859 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clowabe6daf2017-10-24 14:06:00 +0000860 basic_string_view<char32_t> operator "" sv(const char32_t *__str, size_t __len) _NOEXCEPT
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000861 {
862 return basic_string_view<char32_t> (__str, __len);
863 }
864 }
865}
866#endif
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000867_LIBCPP_END_NAMESPACE_STD
868
Eric Fiselierf4433a32017-05-31 22:07:49 +0000869_LIBCPP_POP_MACROS
870
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000871#endif // _LIBCPP_STRING_VIEW