blob: 5c42b36ca565c9f2348683512d8688f5000160de [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
106 constexpr void clear() noexcept;
107 constexpr void remove_prefix(size_type n);
108 constexpr void remove_suffix(size_type n);
109 constexpr void swap(basic_string_view& s) noexcept;
110
111 size_type copy(charT* s, size_type n, size_type pos = 0) const;
112
113 constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const;
114 constexpr int compare(basic_string_view s) const noexcept;
115 constexpr int compare(size_type pos1, size_type n1, basic_string_view s) const;
116 constexpr int compare(size_type pos1, size_type n1,
117 basic_string_view s, size_type pos2, size_type n2) const;
118 constexpr int compare(const charT* s) const;
119 constexpr int compare(size_type pos1, size_type n1, const charT* s) const;
120 constexpr int compare(size_type pos1, size_type n1,
121 const charT* s, size_type n2) const;
122 constexpr size_type find(basic_string_view s, size_type pos = 0) const noexcept;
123 constexpr size_type find(charT c, size_type pos = 0) const noexcept;
124 constexpr size_type find(const charT* s, size_type pos, size_type n) const;
125 constexpr size_type find(const charT* s, size_type pos = 0) const;
126 constexpr size_type rfind(basic_string_view s, size_type pos = npos) const noexcept;
127 constexpr size_type rfind(charT c, size_type pos = npos) const noexcept;
128 constexpr size_type rfind(const charT* s, size_type pos, size_type n) const;
129 constexpr size_type rfind(const charT* s, size_type pos = npos) const;
130 constexpr size_type find_first_of(basic_string_view s, size_type pos = 0) const noexcept;
131 constexpr size_type find_first_of(charT c, size_type pos = 0) const noexcept;
132 constexpr size_type find_first_of(const charT* s, size_type pos, size_type n) const;
133 constexpr size_type find_first_of(const charT* s, size_type pos = 0) const;
134 constexpr size_type find_last_of(basic_string_view s, size_type pos = npos) const noexcept;
135 constexpr size_type find_last_of(charT c, size_type pos = npos) const noexcept;
136 constexpr size_type find_last_of(const charT* s, size_type pos, size_type n) const;
137 constexpr size_type find_last_of(const charT* s, size_type pos = npos) const;
138 constexpr size_type find_first_not_of(basic_string_view s, size_type pos = 0) const noexcept;
139 constexpr size_type find_first_not_of(charT c, size_type pos = 0) const noexcept;
140 constexpr size_type find_first_not_of(const charT* s, size_type pos, size_type n) const;
141 constexpr size_type find_first_not_of(const charT* s, size_type pos = 0) const;
142 constexpr size_type find_last_not_of(basic_string_view s, size_type pos = npos) const noexcept;
143 constexpr size_type find_last_not_of(charT c, size_type pos = npos) const noexcept;
144 constexpr size_type find_last_not_of(const charT* s, size_type pos, size_type n) const;
145 constexpr size_type find_last_not_of(const charT* s, size_type pos = npos) const;
146
147 private:
148 const_pointer data_; // exposition only
149 size_type size_; // exposition only
150 };
151
152 // 7.11, Hash support
153 template <class T> struct hash;
154 template <> struct hash<string_view>;
155 template <> struct hash<u16string_view>;
156 template <> struct hash<u32string_view>;
157 template <> struct hash<wstring_view>;
158
Marshall Clow7d32d2f2017-01-09 18:07:34 +0000159 constexpr basic_string<char> operator "" s( const char *str, size_t len ); // C++17
160 constexpr basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++17
161 constexpr basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++17
162 constexpr basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++17
163
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000164} // namespace std
165
166
167*/
168
169#include <__config>
170
171#include <__string>
Eric Fiselier72a5c772016-12-05 23:53:23 +0000172#include <algorithm>
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000173#include <iterator>
Eric Fiselier72a5c772016-12-05 23:53:23 +0000174#include <limits>
175#include <stdexcept>
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000176#include <__debug>
177
178#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
179#pragma GCC system_header
180#endif
181
182_LIBCPP_BEGIN_NAMESPACE_STD
183
184template<class _CharT, class _Traits = char_traits<_CharT> >
Eric Fiselierc3589a82017-01-04 23:56:00 +0000185class _LIBCPP_TEMPLATE_VIS basic_string_view {
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000186public:
187 // types
188 typedef _Traits traits_type;
189 typedef _CharT value_type;
190 typedef const _CharT* pointer;
191 typedef const _CharT* const_pointer;
192 typedef const _CharT& reference;
193 typedef const _CharT& const_reference;
194 typedef const_pointer const_iterator; // See [string.view.iterators]
195 typedef const_iterator iterator;
196 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
197 typedef const_reverse_iterator reverse_iterator;
198 typedef size_t size_type;
199 typedef ptrdiff_t difference_type;
200 static _LIBCPP_CONSTEXPR const size_type npos = -1; // size_type(-1);
201
Marshall Clow2d4c3fa2017-03-15 18:41:11 +0000202 static_assert(is_pod<value_type>::value, "Character type of basic_string_view must be a POD");
203 static_assert((is_same<_CharT, typename traits_type::char_type>::value),
204 "traits_type::char_type must be the same type as CharT");
205
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000206 // [string.view.cons], construct/copy
207 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
208 basic_string_view() _NOEXCEPT : __data (nullptr), __size(0) {}
209
210 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
211 basic_string_view(const basic_string_view&) _NOEXCEPT = default;
212
Marshall Clowfbe68a62017-01-23 19:53:28 +0000213 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000214 basic_string_view& operator=(const basic_string_view&) _NOEXCEPT = default;
215
216 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
217 basic_string_view(const _CharT* __s, size_type __len)
218 : __data(__s), __size(__len)
219 {
Marshall Clow15362332016-07-21 06:24:04 +0000220// #if _LIBCPP_STD_VER > 11
221// _LIBCPP_ASSERT(__len == 0 || __s != nullptr, "string_view::string_view(_CharT *, size_t): received nullptr");
222// #endif
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000223 }
224
225 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
226 basic_string_view(const _CharT* __s)
227 : __data(__s), __size(_Traits::length(__s)) {}
228
229 // [string.view.iterators], iterators
230 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
231 const_iterator begin() const _NOEXCEPT { return cbegin(); }
232
233 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
234 const_iterator end() const _NOEXCEPT { return cend(); }
235
236 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
237 const_iterator cbegin() const _NOEXCEPT { return __data; }
238
239 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
240 const_iterator cend() const _NOEXCEPT { return __data + __size; }
241
Marshall Clowfbe68a62017-01-23 19:53:28 +0000242 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000243 const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
244
Marshall Clowfbe68a62017-01-23 19:53:28 +0000245 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000246 const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
247
Marshall Clowfbe68a62017-01-23 19:53:28 +0000248 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000249 const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
250
Marshall Clowfbe68a62017-01-23 19:53:28 +0000251 _LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000252 const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
253
254 // [string.view.capacity], capacity
255 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
256 size_type size() const _NOEXCEPT { return __size; }
257
258 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
259 size_type length() const _NOEXCEPT { return __size; }
260
261 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
262 size_type max_size() const _NOEXCEPT { return numeric_limits<size_type>::max(); }
263
264 _LIBCPP_CONSTEXPR bool _LIBCPP_INLINE_VISIBILITY
265 empty() const _NOEXCEPT { return __size == 0; }
266
267 // [string.view.access], element access
268 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
269 const_reference operator[](size_type __pos) const _NOEXCEPT { return __data[__pos]; }
270
271 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
272 const_reference at(size_type __pos) const
273 {
274 return __pos >= size()
Marshall Clowe7acb0e2016-08-25 17:47:09 +0000275 ? (__throw_out_of_range("string_view::at"), __data[0])
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000276 : __data[__pos];
277 }
278
279 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
280 const_reference front() const
281 {
282 return _LIBCPP_ASSERT(!empty(), "string_view::front(): string is empty"), __data[0];
283 }
284
285 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
286 const_reference back() const
287 {
288 return _LIBCPP_ASSERT(!empty(), "string_view::back(): string is empty"), __data[__size-1];
289 }
290
291 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
292 const_pointer data() const _NOEXCEPT { return __data; }
293
294 // [string.view.modifiers], modifiers:
295 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
296 void clear() _NOEXCEPT
297 {
298 __data = nullptr;
299 __size = 0;
300 }
301
302 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
303 void remove_prefix(size_type __n) _NOEXCEPT
304 {
305 _LIBCPP_ASSERT(__n <= size(), "remove_prefix() can't remove more than size()");
306 __data += __n;
307 __size -= __n;
308 }
309
310 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
311 void remove_suffix(size_type __n) _NOEXCEPT
312 {
313 _LIBCPP_ASSERT(__n <= size(), "remove_suffix() can't remove more than size()");
314 __size -= __n;
315 }
316
317 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
318 void swap(basic_string_view& __other) _NOEXCEPT
319 {
320 const value_type *__p = __data;
321 __data = __other.__data;
322 __other.__data = __p;
323
324 size_type __sz = __size;
325 __size = __other.__size;
326 __other.__size = __sz;
327 }
328
329 _LIBCPP_INLINE_VISIBILITY
330 size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const
331 {
332 if (__pos > size())
Marshall Clowe7acb0e2016-08-25 17:47:09 +0000333 __throw_out_of_range("string_view::copy");
Marshall Clowdb7fa112016-11-14 18:22:19 +0000334 size_type __rlen = _VSTD::min(__n, size() - __pos);
335 _Traits::copy(__s, data() + __pos, __rlen);
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000336 return __rlen;
337 }
338
339 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
340 basic_string_view substr(size_type __pos = 0, size_type __n = npos) const
341 {
342 return __pos > size()
Marshall Clowe7acb0e2016-08-25 17:47:09 +0000343 ? (__throw_out_of_range("string_view::substr"), basic_string_view())
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000344 : basic_string_view(data() + __pos, _VSTD::min(__n, size() - __pos));
345 }
346
347 _LIBCPP_CONSTEXPR_AFTER_CXX11 int compare(basic_string_view __sv) const _NOEXCEPT
348 {
349 size_type __rlen = _VSTD::min( size(), __sv.size());
350 int __retval = _Traits::compare(data(), __sv.data(), __rlen);
351 if ( __retval == 0 ) // first __rlen chars matched
352 __retval = size() == __sv.size() ? 0 : ( size() < __sv.size() ? -1 : 1 );
353 return __retval;
354 }
355
356 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
357 int compare(size_type __pos1, size_type __n1, basic_string_view __sv) const
358 {
359 return substr(__pos1, __n1).compare(__sv);
360 }
361
362 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
363 int compare( size_type __pos1, size_type __n1,
364 basic_string_view _sv, size_type __pos2, size_type __n2) const
365 {
366 return substr(__pos1, __n1).compare(_sv.substr(__pos2, __n2));
367 }
368
369 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe6521d62016-07-28 04:52:02 +0000370 int compare(const _CharT* __s) const _NOEXCEPT
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000371 {
372 return compare(basic_string_view(__s));
373 }
374
375 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
376 int compare(size_type __pos1, size_type __n1, const _CharT* __s) const
377 {
378 return substr(__pos1, __n1).compare(basic_string_view(__s));
379 }
380
381 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
382 int compare(size_type __pos1, size_type __n1, const _CharT* __s, size_type __n2) const
383 {
384 return substr(__pos1, __n1).compare(basic_string_view(__s, __n2));
385 }
386
387 // find
388 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
389 size_type find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
390 {
391 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
392 return __str_find<value_type, size_type, traits_type, npos>
393 (data(), size(), __s.data(), __pos, __s.size());
394 }
395
396 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
397 size_type find(_CharT __c, size_type __pos = 0) const _NOEXCEPT
398 {
399 return __str_find<value_type, size_type, traits_type, npos>
400 (data(), size(), __c, __pos);
401 }
402
403 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
404 size_type find(const _CharT* __s, size_type __pos, size_type __n) const
405 {
406 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find(): received nullptr");
407 return __str_find<value_type, size_type, traits_type, npos>
408 (data(), size(), __s, __pos, __n);
409 }
410
411 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
412 size_type find(const _CharT* __s, size_type __pos = 0) const
413 {
414 _LIBCPP_ASSERT(__s != nullptr, "string_view::find(): received nullptr");
415 return __str_find<value_type, size_type, traits_type, npos>
416 (data(), size(), __s, __pos, traits_type::length(__s));
417 }
418
419 // rfind
420 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
421 size_type rfind(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT
422 {
423 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
424 return __str_rfind<value_type, size_type, traits_type, npos>
425 (data(), size(), __s.data(), __pos, __s.size());
426 }
427
428 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
429 size_type rfind(_CharT __c, size_type __pos = npos) const _NOEXCEPT
430 {
431 return __str_rfind<value_type, size_type, traits_type, npos>
432 (data(), size(), __c, __pos);
433 }
434
435 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
436 size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const
437 {
438 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::rfind(): received nullptr");
439 return __str_rfind<value_type, size_type, traits_type, npos>
440 (data(), size(), __s, __pos, __n);
441 }
442
443 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
444 size_type rfind(const _CharT* __s, size_type __pos=npos) const
445 {
446 _LIBCPP_ASSERT(__s != nullptr, "string_view::rfind(): received nullptr");
447 return __str_rfind<value_type, size_type, traits_type, npos>
448 (data(), size(), __s, __pos, traits_type::length(__s));
449 }
450
451 // find_first_of
452 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
453 size_type find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
454 {
455 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_of(): received nullptr");
456 return __str_find_first_of<value_type, size_type, traits_type, npos>
457 (data(), size(), __s.data(), __pos, __s.size());
458 }
459
460 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
461 size_type find_first_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT
462 { return find(__c, __pos); }
463
464 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
465 size_type find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
466 {
467 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_of(): received nullptr");
468 return __str_find_first_of<value_type, size_type, traits_type, npos>
469 (data(), size(), __s, __pos, __n);
470 }
471
472 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
473 size_type find_first_of(const _CharT* __s, size_type __pos=0) const
474 {
475 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_of(): received nullptr");
476 return __str_find_first_of<value_type, size_type, traits_type, npos>
477 (data(), size(), __s, __pos, traits_type::length(__s));
478 }
479
480 // find_last_of
481 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
482 size_type find_last_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
483 {
484 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_of(): received nullptr");
485 return __str_find_last_of<value_type, size_type, traits_type, npos>
486 (data(), size(), __s.data(), __pos, __s.size());
487 }
488
489 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
490 size_type find_last_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT
491 { return rfind(__c, __pos); }
492
493 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
494 size_type find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
495 {
496 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_of(): received nullptr");
497 return __str_find_last_of<value_type, size_type, traits_type, npos>
498 (data(), size(), __s, __pos, __n);
499 }
500
501 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
502 size_type find_last_of(const _CharT* __s, size_type __pos=npos) const
503 {
504 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_of(): received nullptr");
505 return __str_find_last_of<value_type, size_type, traits_type, npos>
506 (data(), size(), __s, __pos, traits_type::length(__s));
507 }
508
509 // find_first_not_of
510 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
511 size_type find_first_not_of(basic_string_view __s, size_type __pos=0) const _NOEXCEPT
512 {
513 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_not_of(): received nullptr");
514 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
515 (data(), size(), __s.data(), __pos, __s.size());
516 }
517
518 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
519 size_type find_first_not_of(_CharT __c, size_type __pos=0) const _NOEXCEPT
520 {
521 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
522 (data(), size(), __c, __pos);
523 }
524
525 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
526 size_type find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
527 {
528 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_not_of(): received nullptr");
529 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
530 (data(), size(), __s, __pos, __n);
531 }
532
533 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
534 size_type find_first_not_of(const _CharT* __s, size_type __pos=0) const
535 {
536 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_not_of(): received nullptr");
537 return __str_find_first_not_of<value_type, size_type, traits_type, npos>
538 (data(), size(), __s, __pos, traits_type::length(__s));
539 }
540
541 // find_last_not_of
542 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
543 size_type find_last_not_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
544 {
545 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_not_of(): received nullptr");
546 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
547 (data(), size(), __s.data(), __pos, __s.size());
548 }
549
550 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
551 size_type find_last_not_of(_CharT __c, size_type __pos=npos) const _NOEXCEPT
552 {
553 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
554 (data(), size(), __c, __pos);
555 }
556
557 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
558 size_type find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
559 {
560 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_not_of(): received nullptr");
561 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
562 (data(), size(), __s, __pos, __n);
563 }
564
565 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
566 size_type find_last_not_of(const _CharT* __s, size_type __pos=npos) const
567 {
568 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_not_of(): received nullptr");
569 return __str_find_last_not_of<value_type, size_type, traits_type, npos>
570 (data(), size(), __s, __pos, traits_type::length(__s));
571 }
572
573private:
574 const value_type* __data;
575 size_type __size;
576};
577
578
579// [string.view.comparison]
580// operator ==
581template<class _CharT, class _Traits>
582_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
583bool operator==(basic_string_view<_CharT, _Traits> __lhs,
584 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
585{
586 if ( __lhs.size() != __rhs.size()) return false;
587 return __lhs.compare(__rhs) == 0;
588}
589
590template<class _CharT, class _Traits>
591_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
592bool operator==(basic_string_view<_CharT, _Traits> __lhs,
593 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
594{
595 if ( __lhs.size() != __rhs.size()) return false;
596 return __lhs.compare(__rhs) == 0;
597}
598
599template<class _CharT, class _Traits>
600_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
601bool operator==(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
602 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
603{
604 if ( __lhs.size() != __rhs.size()) return false;
605 return __lhs.compare(__rhs) == 0;
606}
607
608
609// operator !=
610template<class _CharT, class _Traits>
611_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
612bool operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
613{
614 if ( __lhs.size() != __rhs.size())
615 return true;
616 return __lhs.compare(__rhs) != 0;
617}
618
619template<class _CharT, class _Traits>
620_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
621bool operator!=(basic_string_view<_CharT, _Traits> __lhs,
622 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
623{
624 if ( __lhs.size() != __rhs.size())
625 return true;
626 return __lhs.compare(__rhs) != 0;
627}
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,
632 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
633{
634 if ( __lhs.size() != __rhs.size())
635 return true;
636 return __lhs.compare(__rhs) != 0;
637}
638
639
640// operator <
641template<class _CharT, class _Traits>
642_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
643bool operator<(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
644{
645 return __lhs.compare(__rhs) < 0;
646}
647
648template<class _CharT, class _Traits>
649_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
650bool operator<(basic_string_view<_CharT, _Traits> __lhs,
651 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
652{
653 return __lhs.compare(__rhs) < 0;
654}
655
656template<class _CharT, class _Traits>
657_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
658bool operator<(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
659 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
660{
661 return __lhs.compare(__rhs) < 0;
662}
663
664
665// operator >
666template<class _CharT, class _Traits>
667_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
668bool operator> (basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
669{
670 return __lhs.compare(__rhs) > 0;
671}
672
673template<class _CharT, class _Traits>
674_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
675bool operator>(basic_string_view<_CharT, _Traits> __lhs,
676 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
677{
678 return __lhs.compare(__rhs) > 0;
679}
680
681template<class _CharT, class _Traits>
682_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
683bool operator>(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
684 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
685{
686 return __lhs.compare(__rhs) > 0;
687}
688
689
690// operator <=
691template<class _CharT, class _Traits>
692_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
693bool operator<=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
694{
695 return __lhs.compare(__rhs) <= 0;
696}
697
698template<class _CharT, class _Traits>
699_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
700bool operator<=(basic_string_view<_CharT, _Traits> __lhs,
701 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
702{
703 return __lhs.compare(__rhs) <= 0;
704}
705
706template<class _CharT, class _Traits>
707_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
708bool operator<=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
709 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
710{
711 return __lhs.compare(__rhs) <= 0;
712}
713
714
715// operator >=
716template<class _CharT, class _Traits>
717_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
718bool operator>=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
719{
720 return __lhs.compare(__rhs) >= 0;
721}
722
723
724template<class _CharT, class _Traits>
725_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
726bool operator>=(basic_string_view<_CharT, _Traits> __lhs,
727 typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
728{
729 return __lhs.compare(__rhs) >= 0;
730}
731
732template<class _CharT, class _Traits>
733_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
734bool operator>=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
735 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
736{
737 return __lhs.compare(__rhs) >= 0;
738}
739
740typedef basic_string_view<char> string_view;
741typedef basic_string_view<char16_t> u16string_view;
742typedef basic_string_view<char32_t> u32string_view;
743typedef basic_string_view<wchar_t> wstring_view;
744
745// [string.view.hash]
746template<class _CharT, class _Traits>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000747struct _LIBCPP_TEMPLATE_VIS hash<basic_string_view<_CharT, _Traits> >
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000748 : public unary_function<basic_string_view<_CharT, _Traits>, size_t>
749{
750 size_t operator()(const basic_string_view<_CharT, _Traits> __val) const _NOEXCEPT;
751};
752
753template<class _CharT, class _Traits>
754size_t
755hash<basic_string_view<_CharT, _Traits> >::operator()(
756 const basic_string_view<_CharT, _Traits> __val) const _NOEXCEPT
757{
758 return __do_string_hash(__val.data(), __val.data() + __val.size());
759}
760
Marshall Clow7d32d2f2017-01-09 18:07:34 +0000761
762#if _LIBCPP_STD_VER > 11
763inline namespace literals
764{
765 inline namespace string_view_literals
766 {
767 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
768 basic_string_view<char> operator "" sv(const char *__str, size_t __len)
769 {
770 return basic_string_view<char> (__str, __len);
771 }
772
773 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
774 basic_string_view<wchar_t> operator "" sv(const wchar_t *__str, size_t __len)
775 {
776 return basic_string_view<wchar_t> (__str, __len);
777 }
778
779 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
780 basic_string_view<char16_t> operator "" sv(const char16_t *__str, size_t __len)
781 {
782 return basic_string_view<char16_t> (__str, __len);
783 }
784
785 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
786 basic_string_view<char32_t> operator "" sv(const char32_t *__str, size_t __len)
787 {
788 return basic_string_view<char32_t> (__str, __len);
789 }
790 }
791}
792#endif
Marshall Clow1e00d6d2016-07-21 05:31:24 +0000793_LIBCPP_END_NAMESPACE_STD
794
795#endif // _LIBCPP_STRING_VIEW