blob: 6377aeb6d648ebff378bcadc251fc0bb064df96d [file] [log] [blame]
Marshall Clow1e00d6d2016-07-21 05:31:24 +00001// -*- C++ -*-
2//===------------------------ string_view ---------------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_STRING_VIEW
12#define _LIBCPP_STRING_VIEW
13
14/*
15string_view synopsis
16
17namespace std {
18
19 // 7.2, Class template basic_string_view
20 template<class charT, class traits = char_traits<charT>>
21 class basic_string_view;
22
23 // 7.9, basic_string_view non-member comparison functions
24 template<class charT, class traits>
25 constexpr bool operator==(basic_string_view<charT, traits> x,
26 basic_string_view<charT, traits> y) noexcept;
27 template<class charT, class traits>
28 constexpr bool operator!=(basic_string_view<charT, traits> x,
29 basic_string_view<charT, traits> y) noexcept;
30 template<class charT, class traits>
31 constexpr bool operator< (basic_string_view<charT, traits> x,
32 basic_string_view<charT, traits> y) noexcept;
33 template<class charT, class traits>
34 constexpr bool operator> (basic_string_view<charT, traits> x,
35 basic_string_view<charT, traits> y) noexcept;
36 template<class charT, class traits>
37 constexpr bool operator<=(basic_string_view<charT, traits> x,
38 basic_string_view<charT, traits> y) noexcept;
39 template<class charT, class traits>
40 constexpr bool operator>=(basic_string_view<charT, traits> x,
41 basic_string_view<charT, traits> y) noexcept;
42 // see below, sufficient additional overloads of comparison functions
43
44 // 7.10, Inserters and extractors
45 template<class charT, class traits>
46 basic_ostream<charT, traits>&
47 operator<<(basic_ostream<charT, traits>& os,
48 basic_string_view<charT, traits> str);
49
50 // basic_string_view typedef names
51 typedef basic_string_view<char> string_view;
52 typedef basic_string_view<char16_t> u16string_view;
53 typedef basic_string_view<char32_t> u32string_view;
54 typedef basic_string_view<wchar_t> wstring_view;
55
56 template<class charT, class traits = char_traits<charT>>
57 class basic_string_view {
58 public:
59 // types
60 typedef traits traits_type;
61 typedef charT value_type;
62 typedef charT* pointer;
63 typedef const charT* const_pointer;
64 typedef charT& reference;
65 typedef const charT& const_reference;
66 typedef implementation-defined const_iterator;
67 typedef const_iterator iterator;
68 typedef reverse_iterator<const_iterator> const_reverse_iterator;
69 typedef const_reverse_iterator reverse_iterator;
70 typedef size_t size_type;
71 typedef ptrdiff_t difference_type;
72 static constexpr size_type npos = size_type(-1);
73
74 // 7.3, basic_string_view constructors and assignment operators
75 constexpr basic_string_view() noexcept;
76 constexpr basic_string_view(const basic_string_view&) noexcept = default;
77 basic_string_view& operator=(const basic_string_view&) noexcept = default;
78 template<class Allocator>
79 constexpr basic_string_view(const charT* str);
80 constexpr basic_string_view(const charT* str, size_type len);
81
82 // 7.4, basic_string_view iterator support
83 constexpr const_iterator begin() const noexcept;
84 constexpr const_iterator end() const noexcept;
85 constexpr const_iterator cbegin() const noexcept;
86 constexpr const_iterator cend() const noexcept;
87 const_reverse_iterator rbegin() const noexcept;
88 const_reverse_iterator rend() const noexcept;
89 const_reverse_iterator crbegin() const noexcept;
90 const_reverse_iterator crend() const noexcept;
91
92 // 7.5, basic_string_view capacity
93 constexpr size_type size() const noexcept;
94 constexpr size_type length() const noexcept;
95 constexpr size_type max_size() const noexcept;
96 constexpr bool empty() const noexcept;
97
98 // 7.6, basic_string_view element access
99 constexpr const_reference operator[](size_type pos) const;
100 constexpr const_reference at(size_type pos) const;
101 constexpr const_reference front() const;
102 constexpr const_reference back() const;
103 constexpr const_pointer data() const noexcept;
104
105 // 7.7, basic_string_view modifiers
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000106 constexpr void remove_prefix(size_type n);
107 constexpr void remove_suffix(size_type n);
108 constexpr void swap(basic_string_view& s) noexcept;
109
110 size_type copy(charT* s, size_type n, size_type pos = 0) const;
111
112 constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const;
113 constexpr int compare(basic_string_view s) const noexcept;
114 constexpr int compare(size_type pos1, size_type n1, basic_string_view s) const;
115 constexpr int compare(size_type pos1, size_type n1,
116 basic_string_view s, size_type pos2, size_type n2) const;
117 constexpr int compare(const charT* s) const;
118 constexpr int compare(size_type pos1, size_type n1, const charT* s) const;
119 constexpr int compare(size_type pos1, size_type n1,
120 const charT* s, size_type n2) const;
121 constexpr size_type find(basic_string_view s, size_type pos = 0) const noexcept;
122 constexpr size_type find(charT c, size_type pos = 0) const noexcept;
123 constexpr size_type find(const charT* s, size_type pos, size_type n) const;
124 constexpr size_type find(const charT* s, size_type pos = 0) const;
125 constexpr size_type rfind(basic_string_view s, size_type pos = npos) const noexcept;
126 constexpr size_type rfind(charT c, size_type pos = npos) const noexcept;
127 constexpr size_type rfind(const charT* s, size_type pos, size_type n) const;
128 constexpr size_type rfind(const charT* s, size_type pos = npos) const;
129 constexpr size_type find_first_of(basic_string_view s, size_type pos = 0) const noexcept;
130 constexpr size_type find_first_of(charT c, size_type pos = 0) const noexcept;
131 constexpr size_type find_first_of(const charT* s, size_type pos, size_type n) const;
132 constexpr size_type find_first_of(const charT* s, size_type pos = 0) const;
133 constexpr size_type find_last_of(basic_string_view s, size_type pos = npos) const noexcept;
134 constexpr size_type find_last_of(charT c, size_type pos = npos) const noexcept;
135 constexpr size_type find_last_of(const charT* s, size_type pos, size_type n) const;
136 constexpr size_type find_last_of(const charT* s, size_type pos = npos) const;
137 constexpr size_type find_first_not_of(basic_string_view s, size_type pos = 0) const noexcept;
138 constexpr size_type find_first_not_of(charT c, size_type pos = 0) const noexcept;
139 constexpr size_type find_first_not_of(const charT* s, size_type pos, size_type n) const;
140 constexpr size_type find_first_not_of(const charT* s, size_type pos = 0) const;
141 constexpr size_type find_last_not_of(basic_string_view s, size_type pos = npos) const noexcept;
142 constexpr size_type find_last_not_of(charT c, size_type pos = npos) const noexcept;
143 constexpr size_type find_last_not_of(const charT* s, size_type pos, size_type n) const;
144 constexpr size_type find_last_not_of(const charT* s, size_type pos = npos) const;
145
Marshall Clow46b4ad52017-12-04 20:11:38 +0000146 constexpr bool starts_with(basic_string_view s) const noexcept; // C++2a
147 constexpr bool starts_with(charT c) const noexcept; // C++2a
148 constexpr bool starts_with(const charT* s) const; // C++2a
149 constexpr bool ends_with(basic_string_view s) const noexcept; // C++2a
150 constexpr bool ends_with(charT c) const noexcept; // C++2a
151 constexpr bool ends_with(const charT* s) const; // C++2a
152
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000153 private:
154 const_pointer data_; // exposition only
155 size_type size_; // exposition only
156 };
157
158 // 7.11, Hash support
159 template <class T> struct hash;
160 template <> struct hash<string_view>;
161 template <> struct hash<u16string_view>;
162 template <> struct hash<u32string_view>;
163 template <> struct hash<wstring_view>;
164
Marshall Clow66db4e42017-10-24 14:06:00 +0000165 constexpr basic_string_view<char> operator "" sv( const char *str, size_t len ) noexcept;
166 constexpr basic_string_view<wchar_t> operator "" sv( const wchar_t *str, size_t len ) noexcept;
167 constexpr basic_string_view<char16_t> operator "" sv( const char16_t *str, size_t len ) noexcept;
168 constexpr basic_string_view<char32_t> operator "" sv( const char32_t *str, size_t len ) noexcept;
Marshall Clow7d32d2f2017-01-09 18:07:34 +0000169
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000170} // namespace std
171
172
173*/
174
175#include <__config>
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000176#include <__string>
Eric Fiselier72a5c772016-12-05 23:53:23 +0000177#include <algorithm>
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000178#include <iterator>
Eric Fiselier72a5c772016-12-05 23:53:23 +0000179#include <limits>
180#include <stdexcept>
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000181#include <__debug>
182
183#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
184#pragma GCC system_header
185#endif
186
Eric Fiselier018a3d52017-05-31 22:07:49 +0000187_LIBCPP_PUSH_MACROS
188#include <__undef_macros>
189
190
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000191_LIBCPP_BEGIN_NAMESPACE_STD
192
193template<class _CharT, class _Traits = char_traits<_CharT> >
Eric Fiselierc3589a82017-01-04 23:56:00 +0000194class _LIBCPP_TEMPLATE_VIS basic_string_view {
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000195public:
Marshall Clowf1729d92017-11-15 20:02:27 +0000196 // types
197 typedef _Traits traits_type;
198 typedef _CharT value_type;
Marshall Clowde0811a2017-12-20 16:31:40 +0000199 typedef _CharT* pointer;
Marshall Clowf1729d92017-11-15 20:02:27 +0000200 typedef const _CharT* const_pointer;
Marshall Clowde0811a2017-12-20 16:31:40 +0000201 typedef _CharT& reference;
Marshall Clowf1729d92017-11-15 20:02:27 +0000202 typedef const _CharT& const_reference;
203 typedef const_pointer const_iterator; // See [string.view.iterators]
204 typedef const_iterator iterator;
205 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
206 typedef const_reverse_iterator reverse_iterator;
207 typedef size_t size_type;
208 typedef ptrdiff_t difference_type;
209 static _LIBCPP_CONSTEXPR const size_type npos = -1; // size_type(-1);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000210
Marshall Clow256f1872018-03-21 00:36:05 +0000211 static_assert((!is_array<value_type>::value), "Character type of basic_string_view must not be an array");
212 static_assert(( is_standard_layout<value_type>::value), "Character type of basic_string_view must be standard-layout");
213 static_assert(( is_trivial<value_type>::value), "Character type of basic_string_view must be trivial");
Marshall Clow2d4c3fa2017-03-15 18:41:11 +0000214 static_assert((is_same<_CharT, typename traits_type::char_type>::value),
215 "traits_type::char_type must be the same type as CharT");
216
Marshall Clowf1729d92017-11-15 20:02:27 +0000217 // [string.view.cons], construct/copy
218 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
219 basic_string_view() _NOEXCEPT : __data (nullptr), __size(0) {}
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000220
Marshall Clowf1729d92017-11-15 20:02:27 +0000221 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
222 basic_string_view(const basic_string_view&) _NOEXCEPT = default;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000223
Marshall Clowf1729d92017-11-15 20:02:27 +0000224 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
225 basic_string_view& operator=(const basic_string_view&) _NOEXCEPT = default;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000226
Marshall Clowf1729d92017-11-15 20:02:27 +0000227 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
228 basic_string_view(const _CharT* __s, size_type __len) _NOEXCEPT
229 : __data(__s), __size(__len)
230 {
Marshall Clow15362332016-07-21 06:24:04 +0000231// #if _LIBCPP_STD_VER > 11
232// _LIBCPP_ASSERT(__len == 0 || __s != nullptr, "string_view::string_view(_CharT *, size_t): received nullptr");
233// #endif
Marshall Clowf1729d92017-11-15 20:02:27 +0000234 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000235
Marshall Clowf1729d92017-11-15 20:02:27 +0000236 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
237 basic_string_view(const _CharT* __s)
238 : __data(__s), __size(_Traits::length(__s)) {}
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000239
Marshall Clowf1729d92017-11-15 20:02:27 +0000240 // [string.view.iterators], iterators
241 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
242 const_iterator begin() const _NOEXCEPT { return cbegin(); }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000243
Marshall Clowf1729d92017-11-15 20:02:27 +0000244 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
245 const_iterator end() const _NOEXCEPT { return cend(); }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000246
Marshall Clowf1729d92017-11-15 20:02:27 +0000247 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
248 const_iterator cbegin() const _NOEXCEPT { return __data; }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000249
Marshall Clowf1729d92017-11-15 20:02:27 +0000250 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
251 const_iterator cend() const _NOEXCEPT { return __data + __size; }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000252
Marshall Clowf1729d92017-11-15 20:02:27 +0000253 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
254 const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000255
Marshall Clowf1729d92017-11-15 20:02:27 +0000256 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
257 const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000258
Marshall Clowf1729d92017-11-15 20:02:27 +0000259 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
260 const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000261
Marshall Clowf1729d92017-11-15 20:02:27 +0000262 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
263 const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000264
Marshall Clowf1729d92017-11-15 20:02:27 +0000265 // [string.view.capacity], capacity
266 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
267 size_type size() const _NOEXCEPT { return __size; }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000268
Marshall Clowf1729d92017-11-15 20:02:27 +0000269 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
270 size_type length() const _NOEXCEPT { return __size; }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000271
Marshall Clowf1729d92017-11-15 20:02:27 +0000272 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
273 size_type max_size() const _NOEXCEPT { return numeric_limits<size_type>::max(); }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000274
Marshall Clowf1729d92017-11-15 20:02:27 +0000275 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
276 bool empty() const _NOEXCEPT { return __size == 0; }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000277
Marshall Clowf1729d92017-11-15 20:02:27 +0000278 // [string.view.access], element access
279 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
280 const_reference operator[](size_type __pos) const _NOEXCEPT { return __data[__pos]; }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000281
Marshall Clowf1729d92017-11-15 20:02:27 +0000282 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
283 const_reference at(size_type __pos) const
284 {
285 return __pos >= size()
286 ? (__throw_out_of_range("string_view::at"), __data[0])
287 : __data[__pos];
288 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000289
Marshall Clowf1729d92017-11-15 20:02:27 +0000290 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
291 const_reference front() const
292 {
293 return _LIBCPP_ASSERT(!empty(), "string_view::front(): string is empty"), __data[0];
294 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000295
Marshall Clowf1729d92017-11-15 20:02:27 +0000296 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
297 const_reference back() const
298 {
299 return _LIBCPP_ASSERT(!empty(), "string_view::back(): string is empty"), __data[__size-1];
300 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000301
Marshall Clowf1729d92017-11-15 20:02:27 +0000302 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
303 const_pointer data() const _NOEXCEPT { return __data; }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000304
Marshall Clowf1729d92017-11-15 20:02:27 +0000305 // [string.view.modifiers], modifiers:
306 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
307 void remove_prefix(size_type __n) _NOEXCEPT
308 {
309 _LIBCPP_ASSERT(__n <= size(), "remove_prefix() can't remove more than size()");
310 __data += __n;
311 __size -= __n;
312 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000313
Marshall Clowf1729d92017-11-15 20:02:27 +0000314 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
315 void remove_suffix(size_type __n) _NOEXCEPT
316 {
317 _LIBCPP_ASSERT(__n <= size(), "remove_suffix() can't remove more than size()");
318 __size -= __n;
319 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000320
Marshall Clowf1729d92017-11-15 20:02:27 +0000321 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
322 void swap(basic_string_view& __other) _NOEXCEPT
323 {
324 const value_type *__p = __data;
325 __data = __other.__data;
326 __other.__data = __p;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000327
Marshall Clowf1729d92017-11-15 20:02:27 +0000328 size_type __sz = __size;
329 __size = __other.__size;
330 __other.__size = __sz;
331 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000332
Marshall Clowf1729d92017-11-15 20:02:27 +0000333 _LIBCPP_INLINE_VISIBILITY
334 size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const
335 {
336 if (__pos > size())
337 __throw_out_of_range("string_view::copy");
338 size_type __rlen = _VSTD::min(__n, size() - __pos);
339 _Traits::copy(__s, data() + __pos, __rlen);
340 return __rlen;
341 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000342
Marshall Clowf1729d92017-11-15 20:02:27 +0000343 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
344 basic_string_view substr(size_type __pos = 0, size_type __n = npos) const
345 {
346 return __pos > size()
347 ? (__throw_out_of_range("string_view::substr"), basic_string_view())
348 : basic_string_view(data() + __pos, _VSTD::min(__n, size() - __pos));
349 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000350
Marshall Clowf1729d92017-11-15 20:02:27 +0000351 _LIBCPP_CONSTEXPR_AFTER_CXX11 int compare(basic_string_view __sv) const _NOEXCEPT
352 {
353 size_type __rlen = _VSTD::min( size(), __sv.size());
354 int __retval = _Traits::compare(data(), __sv.data(), __rlen);
355 if ( __retval == 0 ) // first __rlen chars matched
356 __retval = size() == __sv.size() ? 0 : ( size() < __sv.size() ? -1 : 1 );
357 return __retval;
358 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000359
Marshall Clowf1729d92017-11-15 20:02:27 +0000360 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
361 int compare(size_type __pos1, size_type __n1, basic_string_view __sv) const
362 {
363 return substr(__pos1, __n1).compare(__sv);
364 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000365
Marshall Clowf1729d92017-11-15 20:02:27 +0000366 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
367 int compare( size_type __pos1, size_type __n1,
368 basic_string_view __sv, size_type __pos2, size_type __n2) const
369 {
370 return substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
371 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000372
Marshall Clowf1729d92017-11-15 20:02:27 +0000373 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
374 int compare(const _CharT* __s) const _NOEXCEPT
375 {
376 return compare(basic_string_view(__s));
377 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000378
Marshall Clowf1729d92017-11-15 20:02:27 +0000379 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
380 int compare(size_type __pos1, size_type __n1, const _CharT* __s) const
381 {
382 return substr(__pos1, __n1).compare(basic_string_view(__s));
383 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000384
Marshall Clowf1729d92017-11-15 20:02:27 +0000385 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
386 int compare(size_type __pos1, size_type __n1, const _CharT* __s, size_type __n2) const
387 {
388 return substr(__pos1, __n1).compare(basic_string_view(__s, __n2));
389 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000390
Marshall Clowf1729d92017-11-15 20:02:27 +0000391 // find
392 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
393 size_type find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
394 {
395 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
396 return __str_find<value_type, size_type, traits_type, npos>
397 (data(), size(), __s.data(), __pos, __s.size());
398 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000399
Marshall Clowf1729d92017-11-15 20:02:27 +0000400 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
401 size_type find(_CharT __c, size_type __pos = 0) const _NOEXCEPT
402 {
403 return __str_find<value_type, size_type, traits_type, npos>
404 (data(), size(), __c, __pos);
405 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000406
Marshall Clowf1729d92017-11-15 20:02:27 +0000407 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
408 size_type find(const _CharT* __s, size_type __pos, size_type __n) const
409 {
410 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find(): received nullptr");
411 return __str_find<value_type, size_type, traits_type, npos>
412 (data(), size(), __s, __pos, __n);
413 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000414
Marshall Clowf1729d92017-11-15 20:02:27 +0000415 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
416 size_type find(const _CharT* __s, size_type __pos = 0) const
417 {
418 _LIBCPP_ASSERT(__s != nullptr, "string_view::find(): received nullptr");
419 return __str_find<value_type, size_type, traits_type, npos>
420 (data(), size(), __s, __pos, traits_type::length(__s));
421 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000422
Marshall Clowf1729d92017-11-15 20:02:27 +0000423 // rfind
424 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
425 size_type rfind(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT
426 {
427 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
428 return __str_rfind<value_type, size_type, traits_type, npos>
429 (data(), size(), __s.data(), __pos, __s.size());
430 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000431
Marshall Clowf1729d92017-11-15 20:02:27 +0000432 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
433 size_type rfind(_CharT __c, size_type __pos = npos) const _NOEXCEPT
434 {
435 return __str_rfind<value_type, size_type, traits_type, npos>
436 (data(), size(), __c, __pos);
437 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000438
Marshall Clowf1729d92017-11-15 20:02:27 +0000439 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
440 size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const
441 {
442 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::rfind(): received nullptr");
443 return __str_rfind<value_type, size_type, traits_type, npos>
444 (data(), size(), __s, __pos, __n);
445 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000446
Marshall Clowf1729d92017-11-15 20:02:27 +0000447 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
448 size_type rfind(const _CharT* __s, size_type __pos=npos) const
449 {
450 _LIBCPP_ASSERT(__s != nullptr, "string_view::rfind(): received nullptr");
451 return __str_rfind<value_type, size_type, traits_type, npos>
452 (data(), size(), __s, __pos, traits_type::length(__s));
453 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000454
Marshall Clowf1729d92017-11-15 20:02:27 +0000455 // find_first_of
456 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
457 size_type find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
458 {
459 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_of(): received nullptr");
460 return __str_find_first_of<value_type, size_type, traits_type, npos>
461 (data(), size(), __s.data(), __pos, __s.size());
462 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000463
Marshall Clowf1729d92017-11-15 20:02:27 +0000464 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
465 size_type find_first_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT
466 { return find(__c, __pos); }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000467
Marshall Clowf1729d92017-11-15 20:02:27 +0000468 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
469 size_type find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
470 {
471 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_of(): received nullptr");
472 return __str_find_first_of<value_type, size_type, traits_type, npos>
473 (data(), size(), __s, __pos, __n);
474 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000475
Marshall Clowf1729d92017-11-15 20:02:27 +0000476 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
477 size_type find_first_of(const _CharT* __s, size_type __pos=0) const
478 {
479 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_of(): received nullptr");
480 return __str_find_first_of<value_type, size_type, traits_type, npos>
481 (data(), size(), __s, __pos, traits_type::length(__s));
482 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000483
Marshall Clowf1729d92017-11-15 20:02:27 +0000484 // find_last_of
485 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
486 size_type find_last_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
487 {
488 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_of(): received nullptr");
489 return __str_find_last_of<value_type, size_type, traits_type, npos>
490 (data(), size(), __s.data(), __pos, __s.size());
491 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000492
Marshall Clowf1729d92017-11-15 20:02:27 +0000493 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
494 size_type find_last_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT
495 { return rfind(__c, __pos); }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000496
Marshall Clowf1729d92017-11-15 20:02:27 +0000497 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
498 size_type find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
499 {
500 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_of(): received nullptr");
501 return __str_find_last_of<value_type, size_type, traits_type, npos>
502 (data(), size(), __s, __pos, __n);
503 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000504
Marshall Clowf1729d92017-11-15 20:02:27 +0000505 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
506 size_type find_last_of(const _CharT* __s, size_type __pos=npos) const
507 {
508 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_of(): received nullptr");
509 return __str_find_last_of<value_type, size_type, traits_type, npos>
510 (data(), size(), __s, __pos, traits_type::length(__s));
511 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000512
Marshall Clowf1729d92017-11-15 20:02:27 +0000513 // find_first_not_of
514 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
515 size_type find_first_not_of(basic_string_view __s, size_type __pos=0) const _NOEXCEPT
516 {
517 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_not_of(): received nullptr");
518 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
519 (data(), size(), __s.data(), __pos, __s.size());
520 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000521
Marshall Clowf1729d92017-11-15 20:02:27 +0000522 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
523 size_type find_first_not_of(_CharT __c, size_type __pos=0) const _NOEXCEPT
524 {
525 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
526 (data(), size(), __c, __pos);
527 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000528
Marshall Clowf1729d92017-11-15 20:02:27 +0000529 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
530 size_type find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
531 {
532 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_not_of(): received nullptr");
533 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
534 (data(), size(), __s, __pos, __n);
535 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000536
Marshall Clowf1729d92017-11-15 20:02:27 +0000537 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
538 size_type find_first_not_of(const _CharT* __s, size_type __pos=0) const
539 {
540 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_not_of(): received nullptr");
541 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
542 (data(), size(), __s, __pos, traits_type::length(__s));
543 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000544
Marshall Clowf1729d92017-11-15 20:02:27 +0000545 // find_last_not_of
546 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
547 size_type find_last_not_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
548 {
549 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_not_of(): received nullptr");
550 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
551 (data(), size(), __s.data(), __pos, __s.size());
552 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000553
Marshall Clowf1729d92017-11-15 20:02:27 +0000554 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
555 size_type find_last_not_of(_CharT __c, size_type __pos=npos) const _NOEXCEPT
556 {
557 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
558 (data(), size(), __c, __pos);
559 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000560
Marshall Clowf1729d92017-11-15 20:02:27 +0000561 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
562 size_type find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
563 {
564 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_not_of(): received nullptr");
565 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
566 (data(), size(), __s, __pos, __n);
567 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000568
Marshall Clowf1729d92017-11-15 20:02:27 +0000569 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
570 size_type find_last_not_of(const _CharT* __s, size_type __pos=npos) const
571 {
572 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_not_of(): received nullptr");
573 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
574 (data(), size(), __s, __pos, traits_type::length(__s));
575 }
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000576
Marshall Clow46b4ad52017-12-04 20:11:38 +0000577#if _LIBCPP_STD_VER > 17
578 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
579 bool starts_with(basic_string_view __s) const _NOEXCEPT
580 { return size() >= __s.size() && compare(0, __s.size(), __s) == 0; }
581
582 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
583 bool starts_with(value_type __c) const _NOEXCEPT
584 { return !empty() && _Traits::eq(front(), __c); }
585
586 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
587 bool starts_with(const value_type* __s) const _NOEXCEPT
588 { return starts_with(basic_string_view(__s)); }
589
590 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
591 bool ends_with(basic_string_view __s) const _NOEXCEPT
592 { return size() >= __s.size() && compare(size() - __s.size(), npos, __s) == 0; }
593
594 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
595 bool ends_with(value_type __c) const _NOEXCEPT
596 { return !empty() && _Traits::eq(back(), __c); }
597
598 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
599 bool ends_with(const value_type* __s) const _NOEXCEPT
600 { return ends_with(basic_string_view(__s)); }
601#endif
602
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000603private:
Marshall Clowf1729d92017-11-15 20:02:27 +0000604 const value_type* __data;
605 size_type __size;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000606};
607
608
609// [string.view.comparison]
610// operator ==
611template<class _CharT, class _Traits>
612_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
613bool operator==(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowf1729d92017-11-15 20:02:27 +0000614 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000615{
Marshall Clowf1729d92017-11-15 20:02:27 +0000616 if ( __lhs.size() != __rhs.size()) return false;
617 return __lhs.compare(__rhs) == 0;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000618}
619
620template<class _CharT, class _Traits>
621_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
622bool operator==(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowf1729d92017-11-15 20:02:27 +0000623 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000624{
Marshall Clowf1729d92017-11-15 20:02:27 +0000625 if ( __lhs.size() != __rhs.size()) return false;
626 return __lhs.compare(__rhs) == 0;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000627}
628
629template<class _CharT, class _Traits>
630_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
631bool operator==(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowf1729d92017-11-15 20:02:27 +0000632 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000633{
Marshall Clowf1729d92017-11-15 20:02:27 +0000634 if ( __lhs.size() != __rhs.size()) return false;
635 return __lhs.compare(__rhs) == 0;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000636}
637
638
639// operator !=
640template<class _CharT, class _Traits>
641_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
642bool operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
643{
Marshall Clowf1729d92017-11-15 20:02:27 +0000644 if ( __lhs.size() != __rhs.size())
645 return true;
646 return __lhs.compare(__rhs) != 0;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000647}
648
649template<class _CharT, class _Traits>
650_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
651bool operator!=(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowf1729d92017-11-15 20:02:27 +0000652 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000653{
Marshall Clowf1729d92017-11-15 20:02:27 +0000654 if ( __lhs.size() != __rhs.size())
655 return true;
656 return __lhs.compare(__rhs) != 0;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000657}
658
659template<class _CharT, class _Traits>
660_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
661bool operator!=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowf1729d92017-11-15 20:02:27 +0000662 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000663{
Marshall Clowf1729d92017-11-15 20:02:27 +0000664 if ( __lhs.size() != __rhs.size())
665 return true;
666 return __lhs.compare(__rhs) != 0;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000667}
668
669
670// operator <
671template<class _CharT, class _Traits>
672_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
673bool operator<(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
674{
Marshall Clowf1729d92017-11-15 20:02:27 +0000675 return __lhs.compare(__rhs) < 0;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000676}
677
678template<class _CharT, class _Traits>
679_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
680bool operator<(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowf1729d92017-11-15 20:02:27 +0000681 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000682{
Marshall Clowf1729d92017-11-15 20:02:27 +0000683 return __lhs.compare(__rhs) < 0;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000684}
685
686template<class _CharT, class _Traits>
687_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
688bool operator<(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowf1729d92017-11-15 20:02:27 +0000689 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000690{
Marshall Clowf1729d92017-11-15 20:02:27 +0000691 return __lhs.compare(__rhs) < 0;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000692}
693
694
695// operator >
696template<class _CharT, class _Traits>
697_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
698bool operator> (basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
699{
Marshall Clowf1729d92017-11-15 20:02:27 +0000700 return __lhs.compare(__rhs) > 0;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000701}
702
703template<class _CharT, class _Traits>
704_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
705bool operator>(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowf1729d92017-11-15 20:02:27 +0000706 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000707{
Marshall Clowf1729d92017-11-15 20:02:27 +0000708 return __lhs.compare(__rhs) > 0;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000709}
710
711template<class _CharT, class _Traits>
712_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
713bool operator>(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowf1729d92017-11-15 20:02:27 +0000714 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000715{
Marshall Clowf1729d92017-11-15 20:02:27 +0000716 return __lhs.compare(__rhs) > 0;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000717}
718
719
720// operator <=
721template<class _CharT, class _Traits>
722_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
723bool operator<=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
724{
Marshall Clowf1729d92017-11-15 20:02:27 +0000725 return __lhs.compare(__rhs) <= 0;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000726}
727
728template<class _CharT, class _Traits>
729_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
730bool operator<=(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowf1729d92017-11-15 20:02:27 +0000731 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000732{
Marshall Clowf1729d92017-11-15 20:02:27 +0000733 return __lhs.compare(__rhs) <= 0;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000734}
735
736template<class _CharT, class _Traits>
737_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
738bool operator<=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowf1729d92017-11-15 20:02:27 +0000739 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000740{
Marshall Clowf1729d92017-11-15 20:02:27 +0000741 return __lhs.compare(__rhs) <= 0;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000742}
743
744
745// operator >=
746template<class _CharT, class _Traits>
747_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
748bool operator>=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
749{
Marshall Clowf1729d92017-11-15 20:02:27 +0000750 return __lhs.compare(__rhs) >= 0;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000751}
752
753
754template<class _CharT, class _Traits>
755_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
756bool operator>=(basic_string_view<_CharT, _Traits> __lhs,
Marshall Clowf1729d92017-11-15 20:02:27 +0000757 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000758{
Marshall Clowf1729d92017-11-15 20:02:27 +0000759 return __lhs.compare(__rhs) >= 0;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000760}
761
762template<class _CharT, class _Traits>
763_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
764bool operator>=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
Marshall Clowf1729d92017-11-15 20:02:27 +0000765 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000766{
Marshall Clowf1729d92017-11-15 20:02:27 +0000767 return __lhs.compare(__rhs) >= 0;
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000768}
769
770typedef basic_string_view<char> string_view;
771typedef basic_string_view<char16_t> u16string_view;
772typedef basic_string_view<char32_t> u32string_view;
773typedef basic_string_view<wchar_t> wstring_view;
774
775// [string.view.hash]
776template<class _CharT, class _Traits>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000777struct _LIBCPP_TEMPLATE_VIS hash<basic_string_view<_CharT, _Traits> >
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000778 : public unary_function<basic_string_view<_CharT, _Traits>, size_t>
779{
780 size_t operator()(const basic_string_view<_CharT, _Traits> __val) const _NOEXCEPT;
781};
782
783template<class _CharT, class _Traits>
784size_t
785hash<basic_string_view<_CharT, _Traits> >::operator()(
786 const basic_string_view<_CharT, _Traits> __val) const _NOEXCEPT
787{
788 return __do_string_hash(__val.data(), __val.data() + __val.size());
789}
790
Marshall Clow7d32d2f2017-01-09 18:07:34 +0000791
792#if _LIBCPP_STD_VER > 11
793inline namespace literals
794{
795 inline namespace string_view_literals
796 {
797 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow66db4e42017-10-24 14:06:00 +0000798 basic_string_view<char> operator "" sv(const char *__str, size_t __len) _NOEXCEPT
Marshall Clow7d32d2f2017-01-09 18:07:34 +0000799 {
800 return basic_string_view<char> (__str, __len);
801 }
802
803 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow66db4e42017-10-24 14:06:00 +0000804 basic_string_view<wchar_t> operator "" sv(const wchar_t *__str, size_t __len) _NOEXCEPT
Marshall Clow7d32d2f2017-01-09 18:07:34 +0000805 {
806 return basic_string_view<wchar_t> (__str, __len);
807 }
808
809 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow66db4e42017-10-24 14:06:00 +0000810 basic_string_view<char16_t> operator "" sv(const char16_t *__str, size_t __len) _NOEXCEPT
Marshall Clow7d32d2f2017-01-09 18:07:34 +0000811 {
812 return basic_string_view<char16_t> (__str, __len);
813 }
814
815 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Marshall Clow66db4e42017-10-24 14:06:00 +0000816 basic_string_view<char32_t> operator "" sv(const char32_t *__str, size_t __len) _NOEXCEPT
Marshall Clow7d32d2f2017-01-09 18:07:34 +0000817 {
818 return basic_string_view<char32_t> (__str, __len);
819 }
820 }
821}
822#endif
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000823_LIBCPP_END_NAMESPACE_STD
824
Eric Fiselier018a3d52017-05-31 22:07:49 +0000825_LIBCPP_POP_MACROS
826
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000827#endif // _LIBCPP_STRING_VIEW