1// -*- 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
159}  // namespace std
160
161
162*/
163
164#include <__config>
165
166#include <__string>
167#include <algorithm>
168#include <iterator>
169#include <limits>
170#include <stdexcept>
171#include <__debug>
172
173#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
174#pragma GCC system_header
175#endif
176
177_LIBCPP_BEGIN_NAMESPACE_STD
178
179template<class _CharT, class _Traits = char_traits<_CharT> >
180class _LIBCPP_TEMPLATE_VIS basic_string_view {
181public:
182	// types
183	typedef _Traits                                    traits_type;
184	typedef _CharT                                     value_type;
185	typedef const _CharT*                              pointer;
186	typedef const _CharT*                              const_pointer;
187	typedef const _CharT&                              reference;
188	typedef const _CharT&                              const_reference;
189	typedef const_pointer                              const_iterator; // See [string.view.iterators]
190	typedef const_iterator                             iterator;
191	typedef _VSTD::reverse_iterator<const_iterator>    const_reverse_iterator;
192	typedef const_reverse_iterator                     reverse_iterator;
193	typedef size_t                                     size_type;
194	typedef ptrdiff_t                                  difference_type;
195	static _LIBCPP_CONSTEXPR const size_type npos = -1; // size_type(-1);
196
197	// [string.view.cons], construct/copy
198	_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
199	basic_string_view() _NOEXCEPT : __data (nullptr), __size(0) {}
200
201	_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
202	basic_string_view(const basic_string_view&) _NOEXCEPT = default;
203
204	_LIBCPP_INLINE_VISIBILITY
205	basic_string_view& operator=(const basic_string_view&) _NOEXCEPT = default;
206
207	_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
208	basic_string_view(const _CharT* __s, size_type __len)
209		: __data(__s), __size(__len)
210	{
211// #if _LIBCPP_STD_VER > 11
212//         _LIBCPP_ASSERT(__len == 0 || __s != nullptr, "string_view::string_view(_CharT *, size_t): received nullptr");
213// #endif
214	}
215
216	_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
217	basic_string_view(const _CharT* __s)
218		: __data(__s), __size(_Traits::length(__s)) {}
219
220	// [string.view.iterators], iterators
221	_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
222	const_iterator begin()  const _NOEXCEPT { return cbegin(); }
223
224	_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
225	const_iterator end()    const _NOEXCEPT { return cend(); }
226
227	_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
228	const_iterator cbegin() const _NOEXCEPT { return __data; }
229
230	_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
231	const_iterator cend()   const _NOEXCEPT { return __data + __size; }
232
233	_LIBCPP_INLINE_VISIBILITY
234	const_reverse_iterator rbegin()   const _NOEXCEPT { return const_reverse_iterator(cend()); }
235
236	_LIBCPP_INLINE_VISIBILITY
237	const_reverse_iterator rend()     const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
238
239	_LIBCPP_INLINE_VISIBILITY
240	const_reverse_iterator crbegin()  const _NOEXCEPT { return const_reverse_iterator(cend()); }
241
242	_LIBCPP_INLINE_VISIBILITY
243	const_reverse_iterator crend()    const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
244
245	// [string.view.capacity], capacity
246	_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
247	size_type size()     const _NOEXCEPT { return __size; }
248
249	_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
250	size_type length()   const _NOEXCEPT { return __size; }
251
252	_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
253	size_type max_size() const _NOEXCEPT { return numeric_limits<size_type>::max(); }
254
255	_LIBCPP_CONSTEXPR bool _LIBCPP_INLINE_VISIBILITY
256	empty()         const _NOEXCEPT { return __size == 0; }
257
258	// [string.view.access], element access
259	_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
260	const_reference operator[](size_type __pos) const _NOEXCEPT { return __data[__pos]; }
261
262	_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
263	const_reference at(size_type __pos) const
264	{
265		return __pos >= size()
266			? (__throw_out_of_range("string_view::at"), __data[0])
267			: __data[__pos];
268	}
269
270	_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
271	const_reference front() const
272	{
273		return _LIBCPP_ASSERT(!empty(), "string_view::front(): string is empty"), __data[0];
274	}
275
276	_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
277	const_reference back() const
278	{
279		return _LIBCPP_ASSERT(!empty(), "string_view::back(): string is empty"), __data[__size-1];
280	}
281
282	_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
283	const_pointer data() const _NOEXCEPT { return __data; }
284
285	// [string.view.modifiers], modifiers:
286	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
287	void clear() _NOEXCEPT
288	{
289		__data = nullptr;
290		__size = 0;
291	}
292
293	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
294	void remove_prefix(size_type __n) _NOEXCEPT
295	{
296		_LIBCPP_ASSERT(__n <= size(), "remove_prefix() can't remove more than size()");
297		__data += __n;
298		__size -= __n;
299	}
300
301	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
302	void remove_suffix(size_type __n) _NOEXCEPT
303	{
304		_LIBCPP_ASSERT(__n <= size(), "remove_suffix() can't remove more than size()");
305		__size -= __n;
306	}
307
308	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
309	void swap(basic_string_view& __other) _NOEXCEPT
310	{
311		const value_type *__p = __data;
312		__data = __other.__data;
313		__other.__data = __p;
314
315		size_type __sz = __size;
316		__size = __other.__size;
317		__other.__size = __sz;
318	}
319
320	_LIBCPP_INLINE_VISIBILITY
321	size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const
322	{
323		if (__pos > size())
324			__throw_out_of_range("string_view::copy");
325		size_type __rlen = _VSTD::min(__n, size() - __pos);
326		_Traits::copy(__s, data() + __pos, __rlen);
327		return __rlen;
328	}
329
330	_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
331	basic_string_view substr(size_type __pos = 0, size_type __n = npos) const
332	{
333		return __pos > size()
334			? (__throw_out_of_range("string_view::substr"), basic_string_view())
335			: basic_string_view(data() + __pos, _VSTD::min(__n, size() - __pos));
336	}
337
338	_LIBCPP_CONSTEXPR_AFTER_CXX11 int compare(basic_string_view __sv) const _NOEXCEPT
339	{
340		size_type __rlen = _VSTD::min( size(), __sv.size());
341		int __retval = _Traits::compare(data(), __sv.data(), __rlen);
342		if ( __retval == 0 ) // first __rlen chars matched
343			__retval = size() == __sv.size() ? 0 : ( size() < __sv.size() ? -1 : 1 );
344		return __retval;
345	}
346
347	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
348	int compare(size_type __pos1, size_type __n1, basic_string_view __sv) const
349	{
350		return substr(__pos1, __n1).compare(__sv);
351	}
352
353	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
354	int compare(                       size_type __pos1, size_type __n1,
355				basic_string_view _sv, size_type __pos2, size_type __n2) const
356	{
357		return substr(__pos1, __n1).compare(_sv.substr(__pos2, __n2));
358	}
359
360	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
361	int compare(const _CharT* __s) const _NOEXCEPT
362	{
363		return compare(basic_string_view(__s));
364	}
365
366	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
367	int compare(size_type __pos1, size_type __n1, const _CharT* __s) const
368	{
369		return substr(__pos1, __n1).compare(basic_string_view(__s));
370	}
371
372	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
373	int compare(size_type __pos1, size_type __n1, const _CharT* __s, size_type __n2) const
374	{
375		return substr(__pos1, __n1).compare(basic_string_view(__s, __n2));
376	}
377
378	// find
379	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
380	size_type find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
381	{
382		_LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
383		return __str_find<value_type, size_type, traits_type, npos>
384			(data(), size(), __s.data(), __pos, __s.size());
385	}
386
387	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
388	size_type find(_CharT __c, size_type __pos = 0) const _NOEXCEPT
389	{
390		return __str_find<value_type, size_type, traits_type, npos>
391			(data(), size(), __c, __pos);
392	}
393
394	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
395	size_type find(const _CharT* __s, size_type __pos, size_type __n) const
396	{
397		_LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find(): received nullptr");
398		return __str_find<value_type, size_type, traits_type, npos>
399			(data(), size(), __s, __pos, __n);
400	}
401
402	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
403	size_type find(const _CharT* __s, size_type __pos = 0) const
404	{
405		_LIBCPP_ASSERT(__s != nullptr, "string_view::find(): received nullptr");
406		return __str_find<value_type, size_type, traits_type, npos>
407			(data(), size(), __s, __pos, traits_type::length(__s));
408	}
409
410	// rfind
411	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
412	size_type rfind(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT
413	{
414		_LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): received nullptr");
415		return __str_rfind<value_type, size_type, traits_type, npos>
416			(data(), size(), __s.data(), __pos, __s.size());
417	}
418
419	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
420	size_type rfind(_CharT __c, size_type __pos = npos) const _NOEXCEPT
421	{
422		return __str_rfind<value_type, size_type, traits_type, npos>
423			(data(), size(), __c, __pos);
424	}
425
426	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
427	size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const
428	{
429		_LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::rfind(): received nullptr");
430		return __str_rfind<value_type, size_type, traits_type, npos>
431			(data(), size(), __s, __pos, __n);
432	}
433
434	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
435	size_type rfind(const _CharT* __s, size_type __pos=npos) const
436	{
437		_LIBCPP_ASSERT(__s != nullptr, "string_view::rfind(): received nullptr");
438		return __str_rfind<value_type, size_type, traits_type, npos>
439			(data(), size(), __s, __pos, traits_type::length(__s));
440	}
441
442	// find_first_of
443	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
444	size_type find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
445	{
446		_LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_of(): received nullptr");
447		return __str_find_first_of<value_type, size_type, traits_type, npos>
448			(data(), size(), __s.data(), __pos, __s.size());
449	}
450
451	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
452	size_type find_first_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT
453	{ return find(__c, __pos); }
454
455	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
456	size_type find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
457	{
458		_LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_of(): received nullptr");
459		return __str_find_first_of<value_type, size_type, traits_type, npos>
460			(data(), size(), __s, __pos, __n);
461	}
462
463	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
464	size_type find_first_of(const _CharT* __s, size_type __pos=0) const
465	{
466		_LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_of(): received nullptr");
467		return __str_find_first_of<value_type, size_type, traits_type, npos>
468			(data(), size(), __s, __pos, traits_type::length(__s));
469	}
470
471	// find_last_of
472	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
473	size_type find_last_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
474	{
475		_LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_of(): received nullptr");
476		return __str_find_last_of<value_type, size_type, traits_type, npos>
477			(data(), size(), __s.data(), __pos, __s.size());
478	}
479
480	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
481	size_type find_last_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT
482	{ return rfind(__c, __pos); }
483
484	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
485	size_type find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
486	{
487		_LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_of(): received nullptr");
488		return __str_find_last_of<value_type, size_type, traits_type, npos>
489			(data(), size(), __s, __pos, __n);
490	}
491
492	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
493	size_type find_last_of(const _CharT* __s, size_type __pos=npos) const
494	{
495		_LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_of(): received nullptr");
496		return __str_find_last_of<value_type, size_type, traits_type, npos>
497			(data(), size(), __s, __pos, traits_type::length(__s));
498	}
499
500	// find_first_not_of
501	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
502	size_type find_first_not_of(basic_string_view __s, size_type __pos=0) const _NOEXCEPT
503	{
504		_LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_not_of(): received nullptr");
505		return __str_find_first_not_of<value_type, size_type, traits_type, npos>
506			(data(), size(), __s.data(), __pos, __s.size());
507	}
508
509	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
510	size_type find_first_not_of(_CharT __c, size_type __pos=0) const _NOEXCEPT
511	{
512		return __str_find_first_not_of<value_type, size_type, traits_type, npos>
513			(data(), size(), __c, __pos);
514	}
515
516	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
517	size_type find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
518	{
519		_LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_not_of(): received nullptr");
520		return __str_find_first_not_of<value_type, size_type, traits_type, npos>
521			(data(), size(), __s, __pos, __n);
522	}
523
524	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
525	size_type find_first_not_of(const _CharT* __s, size_type __pos=0) const
526	{
527		_LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_not_of(): received nullptr");
528		return __str_find_first_not_of<value_type, size_type, traits_type, npos>
529			(data(), size(), __s, __pos, traits_type::length(__s));
530	}
531
532	// find_last_not_of
533	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
534	size_type find_last_not_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
535	{
536		_LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_not_of(): received nullptr");
537		return __str_find_last_not_of<value_type, size_type, traits_type, npos>
538			(data(), size(), __s.data(), __pos, __s.size());
539	}
540
541	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
542	size_type find_last_not_of(_CharT __c, size_type __pos=npos) const _NOEXCEPT
543	{
544		return __str_find_last_not_of<value_type, size_type, traits_type, npos>
545			(data(), size(), __c, __pos);
546	}
547
548	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
549	size_type find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
550	{
551		_LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_not_of(): received nullptr");
552		return __str_find_last_not_of<value_type, size_type, traits_type, npos>
553			(data(), size(), __s, __pos, __n);
554	}
555
556	_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
557	size_type find_last_not_of(const _CharT* __s, size_type __pos=npos) const
558	{
559		_LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_not_of(): received nullptr");
560		return __str_find_last_not_of<value_type, size_type, traits_type, npos>
561			(data(), size(), __s, __pos, traits_type::length(__s));
562	}
563
564private:
565	const   value_type* __data;
566	size_type           __size;
567};
568
569
570// [string.view.comparison]
571// operator ==
572template<class _CharT, class _Traits>
573_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
574bool operator==(basic_string_view<_CharT, _Traits> __lhs,
575				basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
576{
577	if ( __lhs.size() != __rhs.size()) return false;
578	return __lhs.compare(__rhs) == 0;
579}
580
581template<class _CharT, class _Traits>
582_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
583bool operator==(basic_string_view<_CharT, _Traits> __lhs,
584				typename common_type<basic_string_view<_CharT, _Traits> >::type __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==(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
593				basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
594{
595	if ( __lhs.size() != __rhs.size()) return false;
596	return __lhs.compare(__rhs) == 0;
597}
598
599
600// operator !=
601template<class _CharT, class _Traits>
602_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
603bool operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
604{
605	if ( __lhs.size() != __rhs.size())
606		return true;
607	return __lhs.compare(__rhs) != 0;
608}
609
610template<class _CharT, class _Traits>
611_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
612bool operator!=(basic_string_view<_CharT, _Traits> __lhs,
613				typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
614{
615	if ( __lhs.size() != __rhs.size())
616		return true;
617	return __lhs.compare(__rhs) != 0;
618}
619
620template<class _CharT, class _Traits>
621_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
622bool operator!=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
623				basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
624{
625	if ( __lhs.size() != __rhs.size())
626		return true;
627	return __lhs.compare(__rhs) != 0;
628}
629
630
631// operator <
632template<class _CharT, class _Traits>
633_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
634bool operator<(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
635{
636	return __lhs.compare(__rhs) < 0;
637}
638
639template<class _CharT, class _Traits>
640_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
641bool operator<(basic_string_view<_CharT, _Traits> __lhs,
642				typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
643{
644	return __lhs.compare(__rhs) < 0;
645}
646
647template<class _CharT, class _Traits>
648_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
649bool operator<(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
650				basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
651{
652	return __lhs.compare(__rhs) < 0;
653}
654
655
656// operator >
657template<class _CharT, class _Traits>
658_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
659bool operator> (basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
660{
661	return __lhs.compare(__rhs) > 0;
662}
663
664template<class _CharT, class _Traits>
665_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
666bool operator>(basic_string_view<_CharT, _Traits> __lhs,
667				typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
668{
669	return __lhs.compare(__rhs) > 0;
670}
671
672template<class _CharT, class _Traits>
673_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
674bool operator>(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
675				basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
676{
677	return __lhs.compare(__rhs) > 0;
678}
679
680
681// operator <=
682template<class _CharT, class _Traits>
683_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
684bool operator<=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
685{
686	return __lhs.compare(__rhs) <= 0;
687}
688
689template<class _CharT, class _Traits>
690_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
691bool operator<=(basic_string_view<_CharT, _Traits> __lhs,
692				typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
693{
694	return __lhs.compare(__rhs) <= 0;
695}
696
697template<class _CharT, class _Traits>
698_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
699bool operator<=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
700				basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
701{
702	return __lhs.compare(__rhs) <= 0;
703}
704
705
706// operator >=
707template<class _CharT, class _Traits>
708_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
709bool operator>=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
710{
711	return __lhs.compare(__rhs) >= 0;
712}
713
714
715template<class _CharT, class _Traits>
716_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
717bool operator>=(basic_string_view<_CharT, _Traits> __lhs,
718				typename common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
719{
720	return __lhs.compare(__rhs) >= 0;
721}
722
723template<class _CharT, class _Traits>
724_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
725bool operator>=(typename common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
726				basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
727{
728	return __lhs.compare(__rhs) >= 0;
729}
730
731typedef basic_string_view<char>     string_view;
732typedef basic_string_view<char16_t> u16string_view;
733typedef basic_string_view<char32_t> u32string_view;
734typedef basic_string_view<wchar_t>  wstring_view;
735
736// [string.view.hash]
737template<class _CharT, class _Traits>
738struct _LIBCPP_TEMPLATE_VIS hash<basic_string_view<_CharT, _Traits> >
739    : public unary_function<basic_string_view<_CharT, _Traits>, size_t>
740{
741    size_t operator()(const basic_string_view<_CharT, _Traits> __val) const _NOEXCEPT;
742};
743
744template<class _CharT, class _Traits>
745size_t
746hash<basic_string_view<_CharT, _Traits> >::operator()(
747        const basic_string_view<_CharT, _Traits> __val) const _NOEXCEPT
748{
749    return __do_string_hash(__val.data(), __val.data() + __val.size());
750}
751
752_LIBCPP_END_NAMESPACE_STD
753
754#endif // _LIBCPP_STRING_VIEW
755