blob: 44ffb02c432de4b2afdf64ce1198ddaa139d71b7 [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;
122 constexpr size_type find(const charT* s, size_type pos, size_type n) const;
123 constexpr size_type find(const charT* s, size_type pos = 0) const;
124 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;
126 constexpr size_type rfind(const charT* s, size_type pos, size_type n) const;
127 constexpr size_type rfind(const charT* s, size_type pos = npos) const;
128 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;
130 constexpr size_type find_first_of(const charT* s, size_type pos, size_type n) const;
131 constexpr size_type find_first_of(const charT* s, size_type pos = 0) const;
132 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;
134 constexpr size_type find_last_of(const charT* s, size_type pos, size_type n) const;
135 constexpr size_type find_last_of(const charT* s, size_type pos = npos) const;
136 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;
138 constexpr size_type find_first_not_of(const charT* s, size_type pos, size_type n) const;
139 constexpr size_type find_first_not_of(const charT* s, size_type pos = 0) const;
140 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;
142 constexpr size_type find_last_not_of(const charT* s, size_type pos, size_type n) const;
143 constexpr size_type find_last_not_of(const charT* s, size_type pos = npos) const;
144
Marshall Clow18c293b2017-12-04 20:11:38 +0000145 constexpr bool starts_with(basic_string_view s) const noexcept; // C++2a
146 constexpr bool starts_with(charT c) const noexcept; // C++2a
147 constexpr bool starts_with(const charT* s) const; // C++2a
148 constexpr bool ends_with(basic_string_view s) const noexcept; // C++2a
149 constexpr bool ends_with(charT c) const noexcept; // C++2a
150 constexpr bool ends_with(const charT* s) const; // C++2a
151
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000152 private:
153 const_pointer data_; // exposition only
154 size_type size_; // exposition only
155 };
156
157 // 7.11, Hash support
158 template <class T> struct hash;
159 template <> struct hash<string_view>;
160 template <> struct hash<u16string_view>;
161 template <> struct hash<u32string_view>;
162 template <> struct hash<wstring_view>;
163
Marshall Clowabe6daf2017-10-24 14:06:00 +0000164 constexpr basic_string_view<char> operator "" sv( const char *str, size_t len ) noexcept;
165 constexpr basic_string_view<wchar_t> operator "" sv( const wchar_t *str, size_t len ) noexcept;
166 constexpr basic_string_view<char16_t> operator "" sv( const char16_t *str, size_t len ) noexcept;
167 constexpr basic_string_view<char32_t> operator "" sv( const char32_t *str, size_t len ) noexcept;
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000168
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000169} // namespace std
170
171
172*/
173
174#include <__config>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000175#include <__string>
Eric Fiselier14116922019-09-25 18:56:54 +0000176#include <iosfwd>
Eric Fiselier2d357f72016-12-05 23:53:23 +0000177#include <algorithm>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000178#include <iterator>
Eric Fiselier2d357f72016-12-05 23:53:23 +0000179#include <limits>
180#include <stdexcept>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000181#include <version>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000182#include <__debug>
183
184#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
185#pragma GCC system_header
186#endif
187
Eric Fiselierf4433a32017-05-31 22:07:49 +0000188_LIBCPP_PUSH_MACROS
189#include <__undef_macros>
190
191
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000192_LIBCPP_BEGIN_NAMESPACE_STD
193
194template<class _CharT, class _Traits = char_traits<_CharT> >
Richard Smith256954d2020-11-11 17:12:18 -0800195 class _LIBCPP_TEMPLATE_VIS basic_string_view;
196
197typedef basic_string_view<char> string_view;
198#ifndef _LIBCPP_NO_HAS_CHAR8_T
199typedef basic_string_view<char8_t> u8string_view;
200#endif
201typedef basic_string_view<char16_t> u16string_view;
202typedef basic_string_view<char32_t> u32string_view;
203typedef basic_string_view<wchar_t> wstring_view;
204
205template<class _CharT, class _Traits>
206class
207 _LIBCPP_PREFERRED_NAME(string_view)
208#ifndef _LIBCPP_NO_HAS_CHAR8_T
209 _LIBCPP_PREFERRED_NAME(u8string_view)
210#endif
211 _LIBCPP_PREFERRED_NAME(u16string_view)
212 _LIBCPP_PREFERRED_NAME(u32string_view)
213 _LIBCPP_PREFERRED_NAME(wstring_view)
214 basic_string_view {
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000215public:
Marshall Clowb7db4972017-11-15 20:02:27 +0000216 // types
217 typedef _Traits traits_type;
218 typedef _CharT value_type;
Marshall Clow7d2c5182017-12-20 16:31:40 +0000219 typedef _CharT* pointer;
Marshall Clowb7db4972017-11-15 20:02:27 +0000220 typedef const _CharT* const_pointer;
Marshall Clow7d2c5182017-12-20 16:31:40 +0000221 typedef _CharT& reference;
Marshall Clowb7db4972017-11-15 20:02:27 +0000222 typedef const _CharT& const_reference;
223 typedef const_pointer const_iterator; // See [string.view.iterators]
224 typedef const_iterator iterator;
225 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
226 typedef const_reverse_iterator reverse_iterator;
227 typedef size_t size_type;
228 typedef ptrdiff_t difference_type;
229 static _LIBCPP_CONSTEXPR const size_type npos = -1; // size_type(-1);
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000230
Marshall Clow79f33542018-03-21 00:36:05 +0000231 static_assert((!is_array<value_type>::value), "Character type of basic_string_view must not be an array");
232 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string_view must be standard-layout");
233 static_assert(( is_trivial<value_type>::value), "Character type of basic_string_view must be trivial");
Marshall Clowa3a74e02017-03-15 18:41:11 +0000234 static_assert((is_same<_CharT, typename traits_type::char_type>::value),
235 "traits_type::char_type must be the same type as CharT");
236
Marshall Clowb7db4972017-11-15 20:02:27 +0000237 // [string.view.cons], construct/copy
238 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
239 basic_string_view() _NOEXCEPT : __data (nullptr), __size(0) {}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000240
Marshall Clowb7db4972017-11-15 20:02:27 +0000241 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
242 basic_string_view(const basic_string_view&) _NOEXCEPT = default;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000243
Marshall Clowb7db4972017-11-15 20:02:27 +0000244 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
245 basic_string_view& operator=(const basic_string_view&) _NOEXCEPT = default;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000246
Marshall Clowb7db4972017-11-15 20:02:27 +0000247 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
248 basic_string_view(const _CharT* __s, size_type __len) _NOEXCEPT
249 : __data(__s), __size(__len)
250 {
Marshall Clowca1bcdb2019-06-04 02:07:11 +0000251#if _LIBCPP_STD_VER > 11
252 _LIBCPP_ASSERT(__len == 0 || __s != nullptr, "string_view::string_view(_CharT *, size_t): received nullptr");
253#endif
Marshall Clowb7db4972017-11-15 20:02:27 +0000254 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000255
Marshall Clowb7db4972017-11-15 20:02:27 +0000256 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
257 basic_string_view(const _CharT* __s)
Arthur O'Dwyer07b22492020-11-27 11:02:06 -0500258 : __data(__s), __size(_VSTD::__char_traits_length_checked<_Traits>(__s)) {}
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000259
Marshall Clowb7db4972017-11-15 20:02:27 +0000260 // [string.view.iterators], iterators
261 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
262 const_iterator begin() const _NOEXCEPT { return cbegin(); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000263
Marshall Clowb7db4972017-11-15 20:02:27 +0000264 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
265 const_iterator end() const _NOEXCEPT { return cend(); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000266
Marshall Clowb7db4972017-11-15 20:02:27 +0000267 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
268 const_iterator cbegin() const _NOEXCEPT { return __data; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000269
Marshall Clowb7db4972017-11-15 20:02:27 +0000270 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
271 const_iterator cend() const _NOEXCEPT { return __data + __size; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000272
Marshall Clowb7db4972017-11-15 20:02:27 +0000273 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
274 const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000275
Marshall Clowb7db4972017-11-15 20:02:27 +0000276 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
277 const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000278
Marshall Clowb7db4972017-11-15 20:02:27 +0000279 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
280 const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000281
Marshall Clowb7db4972017-11-15 20:02:27 +0000282 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
283 const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000284
Marshall Clowb7db4972017-11-15 20:02:27 +0000285 // [string.view.capacity], capacity
286 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
287 size_type size() const _NOEXCEPT { return __size; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000288
Marshall Clowb7db4972017-11-15 20:02:27 +0000289 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
290 size_type length() const _NOEXCEPT { return __size; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000291
Marshall Clowb7db4972017-11-15 20:02:27 +0000292 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
293 size_type max_size() const _NOEXCEPT { return numeric_limits<size_type>::max(); }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000294
Marshall Clowb7db4972017-11-15 20:02:27 +0000295 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
296 bool empty() const _NOEXCEPT { return __size == 0; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000297
Marshall Clowb7db4972017-11-15 20:02:27 +0000298 // [string.view.access], element access
299 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Chris Palmer1b114a52020-10-06 13:01:50 -0400300 const_reference operator[](size_type __pos) const _NOEXCEPT {
301 return _LIBCPP_ASSERT(__pos < size(), "string_view[] index out of bounds"), __data[__pos];
302 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000303
Marshall Clowb7db4972017-11-15 20:02:27 +0000304 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
305 const_reference at(size_type __pos) const
306 {
307 return __pos >= size()
308 ? (__throw_out_of_range("string_view::at"), __data[0])
309 : __data[__pos];
310 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000311
Marshall Clowb7db4972017-11-15 20:02:27 +0000312 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +0000313 const_reference front() const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000314 {
315 return _LIBCPP_ASSERT(!empty(), "string_view::front(): string is empty"), __data[0];
316 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000317
Marshall Clowb7db4972017-11-15 20:02:27 +0000318 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +0000319 const_reference back() const _NOEXCEPT
Marshall Clowb7db4972017-11-15 20:02:27 +0000320 {
321 return _LIBCPP_ASSERT(!empty(), "string_view::back(): string is empty"), __data[__size-1];
322 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000323
Marshall Clowb7db4972017-11-15 20:02:27 +0000324 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
325 const_pointer data() const _NOEXCEPT { return __data; }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000326
Marshall Clowb7db4972017-11-15 20:02:27 +0000327 // [string.view.modifiers], modifiers:
328 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
329 void remove_prefix(size_type __n) _NOEXCEPT
330 {
331 _LIBCPP_ASSERT(__n <= size(), "remove_prefix() can't remove more than size()");
332 __data += __n;
333 __size -= __n;
334 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000335
Marshall Clowb7db4972017-11-15 20:02:27 +0000336 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
337 void remove_suffix(size_type __n) _NOEXCEPT
338 {
339 _LIBCPP_ASSERT(__n <= size(), "remove_suffix() can't remove more than size()");
340 __size -= __n;
341 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000342
Marshall Clowb7db4972017-11-15 20:02:27 +0000343 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
344 void swap(basic_string_view& __other) _NOEXCEPT
345 {
346 const value_type *__p = __data;
347 __data = __other.__data;
348 __other.__data = __p;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000349
Marshall Clowb7db4972017-11-15 20:02:27 +0000350 size_type __sz = __size;
351 __size = __other.__size;
352 __other.__size = __sz;
353 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000354
Marshall Clowb7db4972017-11-15 20:02:27 +0000355 _LIBCPP_INLINE_VISIBILITY
356 size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const
357 {
358 if (__pos > size())
359 __throw_out_of_range("string_view::copy");
360 size_type __rlen = _VSTD::min(__n, size() - __pos);
361 _Traits::copy(__s, data() + __pos, __rlen);
362 return __rlen;
363 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000364
Marshall Clowb7db4972017-11-15 20:02:27 +0000365 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
366 basic_string_view substr(size_type __pos = 0, size_type __n = npos) const
367 {
368 return __pos > size()
369 ? (__throw_out_of_range("string_view::substr"), basic_string_view())
370 : basic_string_view(data() + __pos, _VSTD::min(__n, size() - __pos));
371 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000372
Marshall Clowb7db4972017-11-15 20:02:27 +0000373 _LIBCPP_CONSTEXPR_AFTER_CXX11 int compare(basic_string_view __sv) const _NOEXCEPT
374 {
375 size_type __rlen = _VSTD::min( size(), __sv.size());
376 int __retval = _Traits::compare(data(), __sv.data(), __rlen);
377 if ( __retval == 0 ) // first __rlen chars matched
378 __retval = size() == __sv.size() ? 0 : ( size() < __sv.size() ? -1 : 1 );
379 return __retval;
380 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000381
Marshall Clowb7db4972017-11-15 20:02:27 +0000382 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
383 int compare(size_type __pos1, size_type __n1, basic_string_view __sv) const
384 {
385 return substr(__pos1, __n1).compare(__sv);
386 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000387
Marshall Clowb7db4972017-11-15 20:02:27 +0000388 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000389 int compare( size_type __pos1, size_type __n1,
Marshall Clowb7db4972017-11-15 20:02:27 +0000390 basic_string_view __sv, size_type __pos2, size_type __n2) const
391 {
392 return substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
393 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000394
Marshall Clowb7db4972017-11-15 20:02:27 +0000395 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
396 int compare(const _CharT* __s) const _NOEXCEPT
397 {
398 return compare(basic_string_view(__s));
399 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000400
Marshall Clowb7db4972017-11-15 20:02:27 +0000401 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
402 int compare(size_type __pos1, size_type __n1, const _CharT* __s) const
403 {
404 return substr(__pos1, __n1).compare(basic_string_view(__s));
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(size_type __pos1, size_type __n1, const _CharT* __s, size_type __n2) const
409 {
410 return substr(__pos1, __n1).compare(basic_string_view(__s, __n2));
411 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000412
Marshall Clowb7db4972017-11-15 20:02:27 +0000413 // find
414 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
415 size_type find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
416 {
417 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
418 return __str_find<value_type, size_type, traits_type, npos>
419 (data(), size(), __s.data(), __pos, __s.size());
420 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000421
Marshall Clowb7db4972017-11-15 20:02:27 +0000422 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
423 size_type find(_CharT __c, size_type __pos = 0) const _NOEXCEPT
424 {
425 return __str_find<value_type, size_type, traits_type, npos>
426 (data(), size(), __c, __pos);
427 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000428
Marshall Clowb7db4972017-11-15 20:02:27 +0000429 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
430 size_type find(const _CharT* __s, size_type __pos, size_type __n) const
431 {
432 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find(): received nullptr");
433 return __str_find<value_type, size_type, traits_type, npos>
434 (data(), size(), __s, __pos, __n);
435 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000436
Marshall Clowb7db4972017-11-15 20:02:27 +0000437 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
438 size_type find(const _CharT* __s, size_type __pos = 0) const
439 {
440 _LIBCPP_ASSERT(__s != nullptr, "string_view::find(): received nullptr");
441 return __str_find<value_type, size_type, traits_type, npos>
442 (data(), size(), __s, __pos, traits_type::length(__s));
443 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000444
Marshall Clowb7db4972017-11-15 20:02:27 +0000445 // rfind
446 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
447 size_type rfind(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT
448 {
449 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
450 return __str_rfind<value_type, size_type, traits_type, npos>
451 (data(), size(), __s.data(), __pos, __s.size());
452 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000453
Marshall Clowb7db4972017-11-15 20:02:27 +0000454 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
455 size_type rfind(_CharT __c, size_type __pos = npos) const _NOEXCEPT
456 {
457 return __str_rfind<value_type, size_type, traits_type, npos>
458 (data(), size(), __c, __pos);
459 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000460
Marshall Clowb7db4972017-11-15 20:02:27 +0000461 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
462 size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const
463 {
464 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::rfind(): received nullptr");
465 return __str_rfind<value_type, size_type, traits_type, npos>
466 (data(), size(), __s, __pos, __n);
467 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000468
Marshall Clowb7db4972017-11-15 20:02:27 +0000469 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
470 size_type rfind(const _CharT* __s, size_type __pos=npos) const
471 {
472 _LIBCPP_ASSERT(__s != nullptr, "string_view::rfind(): received nullptr");
473 return __str_rfind<value_type, size_type, traits_type, npos>
474 (data(), size(), __s, __pos, traits_type::length(__s));
475 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000476
Marshall Clowb7db4972017-11-15 20:02:27 +0000477 // find_first_of
478 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
479 size_type find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
480 {
481 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_of(): received nullptr");
482 return __str_find_first_of<value_type, size_type, traits_type, npos>
483 (data(), size(), __s.data(), __pos, __s.size());
484 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000485
Marshall Clowb7db4972017-11-15 20:02:27 +0000486 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
487 size_type find_first_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT
488 { return find(__c, __pos); }
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(const _CharT* __s, size_type __pos, size_type __n) const
492 {
493 _LIBCPP_ASSERT(__n == 0 || __s != 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, __pos, __n);
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(const _CharT* __s, size_type __pos=0) const
500 {
501 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_of(): received nullptr");
502 return __str_find_first_of<value_type, size_type, traits_type, npos>
503 (data(), size(), __s, __pos, traits_type::length(__s));
504 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000505
Marshall Clowb7db4972017-11-15 20:02:27 +0000506 // find_last_of
507 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
508 size_type find_last_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
509 {
510 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_of(): received nullptr");
511 return __str_find_last_of<value_type, size_type, traits_type, npos>
512 (data(), size(), __s.data(), __pos, __s.size());
513 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000514
Marshall Clowb7db4972017-11-15 20:02:27 +0000515 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
516 size_type find_last_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT
517 { return rfind(__c, __pos); }
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(const _CharT* __s, size_type __pos, size_type __n) const
521 {
522 _LIBCPP_ASSERT(__n == 0 || __s != 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, __pos, __n);
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(const _CharT* __s, size_type __pos=npos) const
529 {
530 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_of(): received nullptr");
531 return __str_find_last_of<value_type, size_type, traits_type, npos>
532 (data(), size(), __s, __pos, traits_type::length(__s));
533 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000534
Marshall Clowb7db4972017-11-15 20:02:27 +0000535 // find_first_not_of
536 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
537 size_type find_first_not_of(basic_string_view __s, size_type __pos=0) const _NOEXCEPT
538 {
539 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_not_of(): received nullptr");
540 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
541 (data(), size(), __s.data(), __pos, __s.size());
542 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000543
Marshall Clowb7db4972017-11-15 20:02:27 +0000544 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
545 size_type find_first_not_of(_CharT __c, size_type __pos=0) const _NOEXCEPT
546 {
547 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
548 (data(), size(), __c, __pos);
549 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000550
Marshall Clowb7db4972017-11-15 20:02:27 +0000551 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
552 size_type find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
553 {
554 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_not_of(): received nullptr");
555 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
556 (data(), size(), __s, __pos, __n);
557 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000558
Marshall Clowb7db4972017-11-15 20:02:27 +0000559 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
560 size_type find_first_not_of(const _CharT* __s, size_type __pos=0) const
561 {
562 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_not_of(): received nullptr");
563 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
564 (data(), size(), __s, __pos, traits_type::length(__s));
565 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000566
Marshall Clowb7db4972017-11-15 20:02:27 +0000567 // find_last_not_of
568 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
569 size_type find_last_not_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
570 {
571 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_not_of(): received nullptr");
572 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
573 (data(), size(), __s.data(), __pos, __s.size());
574 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000575
Marshall Clowb7db4972017-11-15 20:02:27 +0000576 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
577 size_type find_last_not_of(_CharT __c, size_type __pos=npos) const _NOEXCEPT
578 {
579 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
580 (data(), size(), __c, __pos);
581 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000582
Marshall Clowb7db4972017-11-15 20:02:27 +0000583 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
584 size_type find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
585 {
586 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_not_of(): received nullptr");
587 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
588 (data(), size(), __s, __pos, __n);
589 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000590
Marshall Clowb7db4972017-11-15 20:02:27 +0000591 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
592 size_type find_last_not_of(const _CharT* __s, size_type __pos=npos) const
593 {
594 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_not_of(): received nullptr");
595 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
596 (data(), size(), __s, __pos, traits_type::length(__s));
597 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000598
Marshall Clow18c293b2017-12-04 20:11:38 +0000599#if _LIBCPP_STD_VER > 17
600 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
601 bool starts_with(basic_string_view __s) const _NOEXCEPT
602 { return size() >= __s.size() && compare(0, __s.size(), __s) == 0; }
603
604 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
605 bool starts_with(value_type __c) const _NOEXCEPT
606 { return !empty() && _Traits::eq(front(), __c); }
607
608 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
609 bool starts_with(const value_type* __s) const _NOEXCEPT
610 { return starts_with(basic_string_view(__s)); }
611
612 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
613 bool ends_with(basic_string_view __s) const _NOEXCEPT
614 { return size() >= __s.size() && compare(size() - __s.size(), npos, __s) == 0; }
615
616 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
617 bool ends_with(value_type __c) const _NOEXCEPT
618 { return !empty() && _Traits::eq(back(), __c); }
619
620 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
621 bool ends_with(const value_type* __s) const _NOEXCEPT
622 { return ends_with(basic_string_view(__s)); }
623#endif
624
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000625private:
Marshall Clowb7db4972017-11-15 20:02:27 +0000626 const value_type* __data;
627 size_type __size;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000628};
629
630
631// [string.view.comparison]
632// operator ==
633template<class _CharT, class _Traits>
634_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
635bool operator==(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000636 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000637{
Marshall Clowb7db4972017-11-15 20:02:27 +0000638 if ( __lhs.size() != __rhs.size()) return false;
639 return __lhs.compare(__rhs) == 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000640}
641
642template<class _CharT, class _Traits>
643_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
644bool operator==(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000645 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000646{
Marshall Clowb7db4972017-11-15 20:02:27 +0000647 if ( __lhs.size() != __rhs.size()) return false;
648 return __lhs.compare(__rhs) == 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000649}
650
651template<class _CharT, class _Traits>
652_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000653bool operator==(typename common_type<basic_string_view<_CharT, _Traits> >::type __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
660
661// operator !=
662template<class _CharT, class _Traits>
663_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
664bool operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
665{
Marshall Clowb7db4972017-11-15 20:02:27 +0000666 if ( __lhs.size() != __rhs.size())
667 return true;
668 return __lhs.compare(__rhs) != 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000669}
670
671template<class _CharT, class _Traits>
672_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
673bool operator!=(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000674 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000675{
Marshall Clowb7db4972017-11-15 20:02:27 +0000676 if ( __lhs.size() != __rhs.size())
677 return true;
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())
687 return true;
688 return __lhs.compare(__rhs) != 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000689}
690
691
692// operator <
693template<class _CharT, class _Traits>
694_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
695bool operator<(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
696{
Marshall Clowb7db4972017-11-15 20:02:27 +0000697 return __lhs.compare(__rhs) < 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000698}
699
700template<class _CharT, class _Traits>
701_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
702bool operator<(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000703 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000704{
Marshall Clowb7db4972017-11-15 20:02:27 +0000705 return __lhs.compare(__rhs) < 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000706}
707
708template<class _CharT, class _Traits>
709_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000710bool operator<(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000711 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000712{
Marshall Clowb7db4972017-11-15 20:02:27 +0000713 return __lhs.compare(__rhs) < 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000714}
715
716
717// operator >
718template<class _CharT, class _Traits>
719_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
720bool operator> (basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
721{
Marshall Clowb7db4972017-11-15 20:02:27 +0000722 return __lhs.compare(__rhs) > 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000723}
724
725template<class _CharT, class _Traits>
726_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
727bool operator>(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000728 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000729{
Marshall Clowb7db4972017-11-15 20:02:27 +0000730 return __lhs.compare(__rhs) > 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000731}
732
733template<class _CharT, class _Traits>
734_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000735bool operator>(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000736 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000737{
Marshall Clowb7db4972017-11-15 20:02:27 +0000738 return __lhs.compare(__rhs) > 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000739}
740
741
742// operator <=
743template<class _CharT, class _Traits>
744_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
745bool operator<=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
746{
Marshall Clowb7db4972017-11-15 20:02:27 +0000747 return __lhs.compare(__rhs) <= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000748}
749
750template<class _CharT, class _Traits>
751_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
752bool operator<=(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000753 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000754{
Marshall Clowb7db4972017-11-15 20:02:27 +0000755 return __lhs.compare(__rhs) <= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000756}
757
758template<class _CharT, class _Traits>
759_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000760bool operator<=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000761 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000762{
Marshall Clowb7db4972017-11-15 20:02:27 +0000763 return __lhs.compare(__rhs) <= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000764}
765
766
767// operator >=
768template<class _CharT, class _Traits>
769_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
770bool operator>=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
771{
Marshall Clowb7db4972017-11-15 20:02:27 +0000772 return __lhs.compare(__rhs) >= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000773}
774
775
776template<class _CharT, class _Traits>
777_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
778bool operator>=(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000779 typename common_type<basic_string_view<_CharT, _Traits> >::type __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
784template<class _CharT, class _Traits>
785_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Louis Dionne173f29e2019-05-29 16:01:36 +0000786bool operator>=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowb7db4972017-11-15 20:02:27 +0000787 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000788{
Marshall Clowb7db4972017-11-15 20:02:27 +0000789 return __lhs.compare(__rhs) >= 0;
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000790}
791
Eric Fiselier14116922019-09-25 18:56:54 +0000792
793template<class _CharT, class _Traits>
794basic_ostream<_CharT, _Traits>&
795operator<<(basic_ostream<_CharT, _Traits>& __os,
796 basic_string_view<_CharT, _Traits> __str);
797
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000798// [string.view.hash]
Marshall Clowab313df2019-06-27 14:18:32 +0000799template<class _CharT>
800struct _LIBCPP_TEMPLATE_VIS hash<basic_string_view<_CharT, char_traits<_CharT> > >
801 : public unary_function<basic_string_view<_CharT, char_traits<_CharT> >, size_t>
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000802{
Louis Dionned83f2e62018-11-28 15:22:30 +0000803 _LIBCPP_INLINE_VISIBILITY
Marshall Clowab313df2019-06-27 14:18:32 +0000804 size_t operator()(const basic_string_view<_CharT, char_traits<_CharT> > __val) const _NOEXCEPT {
Louis Dionned83f2e62018-11-28 15:22:30 +0000805 return __do_string_hash(__val.data(), __val.data() + __val.size());
806 }
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000807};
808
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000809
Louis Dionne173f29e2019-05-29 16:01:36 +0000810#if _LIBCPP_STD_VER > 11
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000811inline namespace literals
812{
813 inline namespace string_view_literals
814 {
815 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clowabe6daf2017-10-24 14:06:00 +0000816 basic_string_view<char> operator "" sv(const char *__str, size_t __len) _NOEXCEPT
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000817 {
818 return basic_string_view<char> (__str, __len);
819 }
820
821 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clowabe6daf2017-10-24 14:06:00 +0000822 basic_string_view<wchar_t> operator "" sv(const wchar_t *__str, size_t __len) _NOEXCEPT
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000823 {
824 return basic_string_view<wchar_t> (__str, __len);
825 }
826
Marshall Clow8732fed2018-12-11 04:35:44 +0000827#ifndef _LIBCPP_NO_HAS_CHAR8_T
828 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
829 basic_string_view<char8_t> operator "" sv(const char8_t *__str, size_t __len) _NOEXCEPT
830 {
831 return basic_string_view<char8_t> (__str, __len);
832 }
833#endif
834
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000835 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clowabe6daf2017-10-24 14:06:00 +0000836 basic_string_view<char16_t> operator "" sv(const char16_t *__str, size_t __len) _NOEXCEPT
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000837 {
838 return basic_string_view<char16_t> (__str, __len);
839 }
840
841 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clowabe6daf2017-10-24 14:06:00 +0000842 basic_string_view<char32_t> operator "" sv(const char32_t *__str, size_t __len) _NOEXCEPT
Marshall Clow27c8a4c2017-01-09 18:07:34 +0000843 {
844 return basic_string_view<char32_t> (__str, __len);
845 }
846 }
847}
848#endif
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000849_LIBCPP_END_NAMESPACE_STD
850
Eric Fiselierf4433a32017-05-31 22:07:49 +0000851_LIBCPP_POP_MACROS
852
Marshall Clowdf63a6d2016-07-21 05:31:24 +0000853#endif // _LIBCPP_STRING_VIEW