blob: 75374d2cb2a1b35b1b75ba9972e10813dcc99f6b [file] [log] [blame]
Marshall Clow1e00d6d2016-07-21 05:31:241// -*- 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:24106 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
146 private:
147 const_pointer data_; // exposition only
148 size_type size_; // exposition only
149 };
150
151 // 7.11, Hash support
152 template <class T> struct hash;
153 template <> struct hash<string_view>;
154 template <> struct hash<u16string_view>;
155 template <> struct hash<u32string_view>;
156 template <> struct hash<wstring_view>;
157
Marshall Clow7d32d2f2017-01-09 18:07:34158 constexpr basic_string<char> operator "" s( const char *str, size_t len ); // C++17
159 constexpr basic_string<wchar_t> operator "" s( const wchar_t *str, size_t len ); // C++17
160 constexpr basic_string<char16_t> operator "" s( const char16_t *str, size_t len ); // C++17
161 constexpr basic_string<char32_t> operator "" s( const char32_t *str, size_t len ); // C++17
162
Marshall Clow1e00d6d2016-07-21 05:31:24163} // namespace std
164
165
166*/
167
168#include <__config>
Marshall Clow1e00d6d2016-07-21 05:31:24169#include <__string>
Eric Fiselier72a5c772016-12-05 23:53:23170#include <algorithm>
Marshall Clow1e00d6d2016-07-21 05:31:24171#include <iterator>
Eric Fiselier72a5c772016-12-05 23:53:23172#include <limits>
173#include <stdexcept>
Marshall Clow1e00d6d2016-07-21 05:31:24174#include <__debug>
175
176#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
177#pragma GCC system_header
178#endif
179
Eric Fiselier018a3d52017-05-31 22:07:49180_LIBCPP_PUSH_MACROS
181#include <__undef_macros>
182
183
Marshall Clow1e00d6d2016-07-21 05:31:24184_LIBCPP_BEGIN_NAMESPACE_STD
185
186template<class _CharT, class _Traits = char_traits<_CharT> >
Eric Fiselierc3589a82017-01-04 23:56:00187class _LIBCPP_TEMPLATE_VIS basic_string_view {
Marshall Clow1e00d6d2016-07-21 05:31:24188public:
189// types
190typedef _Traits traits_type;
191typedef _CharT value_type;
192typedef const _CharT* pointer;
193typedef const _CharT* const_pointer;
194typedef const _CharT& reference;
195typedef const _CharT& const_reference;
196typedef const_pointer const_iterator; // See [string.view.iterators]
197typedef const_iterator iterator;
198typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
199typedef const_reverse_iterator reverse_iterator;
200typedef size_t size_type;
201typedef ptrdiff_t difference_type;
202static _LIBCPP_CONSTEXPR const size_type npos = -1; // size_type(-1);
203
Marshall Clow2d4c3fa2017-03-15 18:41:11204 static_assert(is_pod<value_type>::value, "Character type of basic_string_view must be a POD");
205 static_assert((is_same<_CharT, typename traits_type::char_type>::value),
206 "traits_type::char_type must be the same type as CharT");
207
Marshall Clow1e00d6d2016-07-21 05:31:24208// [string.view.cons], construct/copy
209_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
210basic_string_view() _NOEXCEPT : __data (nullptr), __size(0) {}
211
212_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
213basic_string_view(const basic_string_view&) _NOEXCEPT = default;
214
Marshall Clowfbe68a62017-01-23 19:53:28215_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24216basic_string_view& operator=(const basic_string_view&) _NOEXCEPT = default;
217
218_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
219basic_string_view(const _CharT* __s, size_type __len)
220: __data(__s), __size(__len)
221{
Marshall Clow15362332016-07-21 06:24:04222// #if _LIBCPP_STD_VER > 11
223// _LIBCPP_ASSERT(__len == 0 || __s != nullptr, "string_view::string_view(_CharT *, size_t): received nullptr");
224// #endif
Marshall Clow1e00d6d2016-07-21 05:31:24225}
226
227_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
228basic_string_view(const _CharT* __s)
229: __data(__s), __size(_Traits::length(__s)) {}
230
231// [string.view.iterators], iterators
232_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
233const_iterator begin() const _NOEXCEPT { return cbegin(); }
234
235_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
236const_iterator end() const _NOEXCEPT { return cend(); }
237
238_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
239const_iterator cbegin() const _NOEXCEPT { return __data; }
240
241_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
242const_iterator cend() const _NOEXCEPT { return __data + __size; }
243
Marshall Clowfbe68a62017-01-23 19:53:28244_LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24245const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
246
Marshall Clowfbe68a62017-01-23 19:53:28247_LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24248const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
249
Marshall Clowfbe68a62017-01-23 19:53:28250_LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24251const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
252
Marshall Clowfbe68a62017-01-23 19:53:28253_LIBCPP_CONSTEXPR_AFTER_CXX14 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24254const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
255
256// [string.view.capacity], capacity
257_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
258size_type size() const _NOEXCEPT { return __size; }
259
260_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
261size_type length() const _NOEXCEPT { return __size; }
262
263_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
264size_type max_size() const _NOEXCEPT { return numeric_limits<size_type>::max(); }
265
266_LIBCPP_CONSTEXPR bool _LIBCPP_INLINE_VISIBILITY
267empty() const _NOEXCEPT { return __size == 0; }
268
269// [string.view.access], element access
270_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
271const_reference operator[](size_type __pos) const _NOEXCEPT { return __data[__pos]; }
272
273_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
274const_reference at(size_type __pos) const
275{
276return __pos >= size()
Marshall Clowe7acb0e2016-08-25 17:47:09277? (__throw_out_of_range("string_view::at"), __data[0])
Marshall Clow1e00d6d2016-07-21 05:31:24278: __data[__pos];
279}
280
281_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
282const_reference front() const
283{
284return _LIBCPP_ASSERT(!empty(), "string_view::front(): string is empty"), __data[0];
285}
286
287_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
288const_reference back() const
289{
290return _LIBCPP_ASSERT(!empty(), "string_view::back(): string is empty"), __data[__size-1];
291}
292
293_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
294const_pointer data() const _NOEXCEPT { return __data; }
295
296// [string.view.modifiers], modifiers:
297_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow1e00d6d2016-07-21 05:31:24298void remove_prefix(size_type __n) _NOEXCEPT
299{
300_LIBCPP_ASSERT(__n <= size(), "remove_prefix() can't remove more than size()");
301__data += __n;
302__size -= __n;
303}
304
305_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
306void remove_suffix(size_type __n) _NOEXCEPT
307{
308_LIBCPP_ASSERT(__n <= size(), "remove_suffix() can't remove more than size()");
309__size -= __n;
310}
311
312_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
313void swap(basic_string_view& __other) _NOEXCEPT
314{
315const value_type *__p = __data;
316__data = __other.__data;
317__other.__data = __p;
318
319size_type __sz = __size;
320__size = __other.__size;
321__other.__size = __sz;
322}
323
324_LIBCPP_INLINE_VISIBILITY
325size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const
326{
327if (__pos > size())
Marshall Clowe7acb0e2016-08-25 17:47:09328__throw_out_of_range("string_view::copy");
Marshall Clowdb7fa112016-11-14 18:22:19329size_type __rlen = _VSTD::min(__n, size() - __pos);
330_Traits::copy(__s, data() + __pos, __rlen);
Marshall Clow1e00d6d2016-07-21 05:31:24331return __rlen;
332}
333
334_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
335basic_string_view substr(size_type __pos = 0, size_type __n = npos) const
336{
337return __pos > size()
Marshall Clowe7acb0e2016-08-25 17:47:09338? (__throw_out_of_range("string_view::substr"), basic_string_view())
Marshall Clow1e00d6d2016-07-21 05:31:24339: basic_string_view(data() + __pos, _VSTD::min(__n, size() - __pos));
340}
341
342_LIBCPP_CONSTEXPR_AFTER_CXX11 int compare(basic_string_view __sv) const _NOEXCEPT
343{
344size_type __rlen = _VSTD::min( size(), __sv.size());
345int __retval = _Traits::compare(data(), __sv.data(), __rlen);
346if ( __retval == 0 ) // first __rlen chars matched
347__retval = size() == __sv.size() ? 0 : ( size() < __sv.size() ? -1 : 1 );
348return __retval;
349}
350
351_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
352int compare(size_type __pos1, size_type __n1, basic_string_view __sv) const
353{
354return substr(__pos1, __n1).compare(__sv);
355}
356
357_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
358int compare( size_type __pos1, size_type __n1,
359basic_string_view _sv, size_type __pos2, size_type __n2) const
360{
361return substr(__pos1, __n1).compare(_sv.substr(__pos2, __n2));
362}
363
364_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clowe6521d62016-07-28 04:52:02365int compare(const _CharT* __s) const _NOEXCEPT
Marshall Clow1e00d6d2016-07-21 05:31:24366{
367return compare(basic_string_view(__s));
368}
369
370_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
371int compare(size_type __pos1, size_type __n1, const _CharT* __s) const
372{
373return substr(__pos1, __n1).compare(basic_string_view(__s));
374}
375
376_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
377int compare(size_type __pos1, size_type __n1, const _CharT* __s, size_type __n2) const
378{
379return substr(__pos1, __n1).compare(basic_string_view(__s, __n2));
380}
381
382// find
383_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
384size_type find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
385{
386_LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
387return __str_find<value_type, size_type, traits_type, npos>
388(data(), size(), __s.data(), __pos, __s.size());
389}
390
391_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
392size_type find(_CharT __c, size_type __pos = 0) const _NOEXCEPT
393{
394return __str_find<value_type, size_type, traits_type, npos>
395(data(), size(), __c, __pos);
396}
397
398_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
399size_type find(const _CharT* __s, size_type __pos, size_type __n) const
400{
401_LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find(): received nullptr");
402return __str_find<value_type, size_type, traits_type, npos>
403(data(), size(), __s, __pos, __n);
404}
405
406_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
407size_type find(const _CharT* __s, size_type __pos = 0) const
408{
409_LIBCPP_ASSERT(__s != nullptr, "string_view::find(): received nullptr");
410return __str_find<value_type, size_type, traits_type, npos>
411(data(), size(), __s, __pos, traits_type::length(__s));
412}
413
414// rfind
415_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
416size_type rfind(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT
417{
418_LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
419return __str_rfind<value_type, size_type, traits_type, npos>
420(data(), size(), __s.data(), __pos, __s.size());
421}
422
423_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
424size_type rfind(_CharT __c, size_type __pos = npos) const _NOEXCEPT
425{
426return __str_rfind<value_type, size_type, traits_type, npos>
427(data(), size(), __c, __pos);
428}
429
430_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
431size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const
432{
433_LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::rfind(): received nullptr");
434return __str_rfind<value_type, size_type, traits_type, npos>
435(data(), size(), __s, __pos, __n);
436}
437
438_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
439size_type rfind(const _CharT* __s, size_type __pos=npos) const
440{
441_LIBCPP_ASSERT(__s != nullptr, "string_view::rfind(): received nullptr");
442return __str_rfind<value_type, size_type, traits_type, npos>
443(data(), size(), __s, __pos, traits_type::length(__s));
444}
445
446// find_first_of
447_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
448size_type find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
449{
450_LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_of(): received nullptr");
451return __str_find_first_of<value_type, size_type, traits_type, npos>
452(data(), size(), __s.data(), __pos, __s.size());
453}
454
455_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
456size_type find_first_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT
457{ return find(__c, __pos); }
458
459_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
460size_type find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
461{
462_LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_of(): received nullptr");
463return __str_find_first_of<value_type, size_type, traits_type, npos>
464(data(), size(), __s, __pos, __n);
465}
466
467_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
468size_type find_first_of(const _CharT* __s, size_type __pos=0) const
469{
470_LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_of(): received nullptr");
471return __str_find_first_of<value_type, size_type, traits_type, npos>
472(data(), size(), __s, __pos, traits_type::length(__s));
473}
474
475// find_last_of
476_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
477size_type find_last_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
478{
479_LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_of(): received nullptr");
480return __str_find_last_of<value_type, size_type, traits_type, npos>
481(data(), size(), __s.data(), __pos, __s.size());
482}
483
484_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
485size_type find_last_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT
486{ return rfind(__c, __pos); }
487
488_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
489size_type find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
490{
491_LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_of(): received nullptr");
492return __str_find_last_of<value_type, size_type, traits_type, npos>
493(data(), size(), __s, __pos, __n);
494}
495
496_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
497size_type find_last_of(const _CharT* __s, size_type __pos=npos) const
498{
499_LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_of(): received nullptr");
500return __str_find_last_of<value_type, size_type, traits_type, npos>
501(data(), size(), __s, __pos, traits_type::length(__s));
502}
503
504// find_first_not_of
505_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
506size_type find_first_not_of(basic_string_view __s, size_type __pos=0) const _NOEXCEPT
507{
508_LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_not_of(): received nullptr");
509return __str_find_first_not_of<value_type, size_type, traits_type, npos>
510(data(), size(), __s.data(), __pos, __s.size());
511}
512
513_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
514size_type find_first_not_of(_CharT __c, size_type __pos=0) const _NOEXCEPT
515{
516return __str_find_first_not_of<value_type, size_type, traits_type, npos>
517(data(), size(), __c, __pos);
518}
519
520_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
521size_type find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
522{
523_LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_not_of(): received nullptr");
524return __str_find_first_not_of<value_type, size_type, traits_type, npos>
525(data(), size(), __s, __pos, __n);
526}
527
528_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
529size_type find_first_not_of(const _CharT* __s, size_type __pos=0) const
530{
531_LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_not_of(): received nullptr");
532return __str_find_first_not_of<value_type, size_type, traits_type, npos>
533(data(), size(), __s, __pos, traits_type::length(__s));
534}
535
536// find_last_not_of
537_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
538size_type find_last_not_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
539{
540_LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_not_of(): received nullptr");
541return __str_find_last_not_of<value_type, size_type, traits_type, npos>
542(data(), size(), __s.data(), __pos, __s.size());
543}
544
545_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
546size_type find_last_not_of(_CharT __c, size_type __pos=npos) const _NOEXCEPT
547{
548return __str_find_last_not_of<value_type, size_type, traits_type, npos>
549(data(), size(), __c, __pos);
550}
551
552_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
553size_type find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
554{
555_LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_not_of(): received nullptr");
556return __str_find_last_not_of<value_type, size_type, traits_type, npos>
557(data(), size(), __s, __pos, __n);
558}
559
560_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
561size_type find_last_not_of(const _CharT* __s, size_type __pos=npos) const
562{
563_LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_not_of(): received nullptr");
564return __str_find_last_not_of<value_type, size_type, traits_type, npos>
565(data(), size(), __s, __pos, traits_type::length(__s));
566}
567
568private:
569const value_type* __data;
570size_type __size;
571};
572
573
574// [string.view.comparison]
575// operator ==
576template<class _CharT, class _Traits>
577_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
578bool operator==(basic_string_view<_CharT, _Traits> __lhs,
579basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
580{
581if ( __lhs.size() != __rhs.size()) return false;
582return __lhs.compare(__rhs) == 0;
583}
584
585template<class _CharT, class _Traits>
586_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
587bool operator==(basic_string_view<_CharT, _Traits> __lhs,
588typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
589{
590if ( __lhs.size() != __rhs.size()) return false;
591return __lhs.compare(__rhs) == 0;
592}
593
594template<class _CharT, class _Traits>
595_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
596bool operator==(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
597basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
598{
599if ( __lhs.size() != __rhs.size()) return false;
600return __lhs.compare(__rhs) == 0;
601}
602
603
604// operator !=
605template<class _CharT, class _Traits>
606_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
607bool operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
608{
609if ( __lhs.size() != __rhs.size())
610return true;
611return __lhs.compare(__rhs) != 0;
612}
613
614template<class _CharT, class _Traits>
615_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
616bool operator!=(basic_string_view<_CharT, _Traits> __lhs,
617typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
618{
619if ( __lhs.size() != __rhs.size())
620return true;
621return __lhs.compare(__rhs) != 0;
622}
623
624template<class _CharT, class _Traits>
625_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
626bool operator!=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
627basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
628{
629if ( __lhs.size() != __rhs.size())
630return true;
631return __lhs.compare(__rhs) != 0;
632}
633
634
635// operator <
636template<class _CharT, class _Traits>
637_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
638bool operator<(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
639{
640return __lhs.compare(__rhs) < 0;
641}
642
643template<class _CharT, class _Traits>
644_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
645bool operator<(basic_string_view<_CharT, _Traits> __lhs,
646typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
647{
648return __lhs.compare(__rhs) < 0;
649}
650
651template<class _CharT, class _Traits>
652_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
653bool operator<(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
654basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
655{
656return __lhs.compare(__rhs) < 0;
657}
658
659
660// operator >
661template<class _CharT, class _Traits>
662_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
663bool operator> (basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
664{
665return __lhs.compare(__rhs) > 0;
666}
667
668template<class _CharT, class _Traits>
669_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
670bool operator>(basic_string_view<_CharT, _Traits> __lhs,
671typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
672{
673return __lhs.compare(__rhs) > 0;
674}
675
676template<class _CharT, class _Traits>
677_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
678bool operator>(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
679basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
680{
681return __lhs.compare(__rhs) > 0;
682}
683
684
685// operator <=
686template<class _CharT, class _Traits>
687_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
688bool operator<=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
689{
690return __lhs.compare(__rhs) <= 0;
691}
692
693template<class _CharT, class _Traits>
694_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
695bool operator<=(basic_string_view<_CharT, _Traits> __lhs,
696typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
697{
698return __lhs.compare(__rhs) <= 0;
699}
700
701template<class _CharT, class _Traits>
702_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
703bool operator<=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
704basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
705{
706return __lhs.compare(__rhs) <= 0;
707}
708
709
710// operator >=
711template<class _CharT, class _Traits>
712_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
713bool operator>=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
714{
715return __lhs.compare(__rhs) >= 0;
716}
717
718
719template<class _CharT, class _Traits>
720_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
721bool operator>=(basic_string_view<_CharT, _Traits> __lhs,
722typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
723{
724return __lhs.compare(__rhs) >= 0;
725}
726
727template<class _CharT, class _Traits>
728_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
729bool operator>=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
730basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
731{
732return __lhs.compare(__rhs) >= 0;
733}
734
735typedef basic_string_view<char> string_view;
736typedef basic_string_view<char16_t> u16string_view;
737typedef basic_string_view<char32_t> u32string_view;
738typedef basic_string_view<wchar_t> wstring_view;
739
740// [string.view.hash]
741template<class _CharT, class _Traits>
Eric Fiselierc3589a82017-01-04 23:56:00742struct _LIBCPP_TEMPLATE_VIS hash<basic_string_view<_CharT, _Traits> >
Marshall Clow1e00d6d2016-07-21 05:31:24743 : public unary_function<basic_string_view<_CharT, _Traits>, size_t>
744{
745 size_t operator()(const basic_string_view<_CharT, _Traits> __val) const _NOEXCEPT;
746};
747
748template<class _CharT, class _Traits>
749size_t
750hash<basic_string_view<_CharT, _Traits> >::operator()(
751 const basic_string_view<_CharT, _Traits> __val) const _NOEXCEPT
752{
753 return __do_string_hash(__val.data(), __val.data() + __val.size());
754}
755
Marshall Clow7d32d2f2017-01-09 18:07:34756
757#if _LIBCPP_STD_VER > 11
758inline namespace literals
759{
760 inline namespace string_view_literals
761 {
762 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
763 basic_string_view<char> operator "" sv(const char *__str, size_t __len)
764 {
765 return basic_string_view<char> (__str, __len);
766 }
767
768 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
769 basic_string_view<wchar_t> operator "" sv(const wchar_t *__str, size_t __len)
770 {
771 return basic_string_view<wchar_t> (__str, __len);
772 }
773
774 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
775 basic_string_view<char16_t> operator "" sv(const char16_t *__str, size_t __len)
776 {
777 return basic_string_view<char16_t> (__str, __len);
778 }
779
780 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
781 basic_string_view<char32_t> operator "" sv(const char32_t *__str, size_t __len)
782 {
783 return basic_string_view<char32_t> (__str, __len);
784 }
785 }
786}
787#endif
Marshall Clow1e00d6d2016-07-21 05:31:24788_LIBCPP_END_NAMESPACE_STD
789
Eric Fiselier018a3d52017-05-31 22:07:49790_LIBCPP_POP_MACROS
791
Marshall Clow1e00d6d2016-07-21 05:31:24792#endif // _LIBCPP_STRING_VIEW