blob: 77612af93c68168df8774786a5c154c86907e40a [file] [log] [blame]
Marshall Clow5aa8fa22014-06-11 16:44:55 +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_LFTS_STRING_VIEW
12#define _LIBCPP_LFTS_STRING_VIEW
13
14/*
15string_view synopsis
16
17namespace std {
18 namespace experimental {
19 inline namespace library_fundamentals_v1 {
20
21 // 7.2, Class template basic_string_view
22 template<class charT, class traits = char_traits<charT>>
23 class basic_string_view;
24
25 // 7.9, basic_string_view non-member comparison functions
26 template<class charT, class traits>
27 constexpr bool operator==(basic_string_view<charT, traits> x,
28 basic_string_view<charT, traits> y) noexcept;
29 template<class charT, class traits>
30 constexpr bool operator!=(basic_string_view<charT, traits> x,
31 basic_string_view<charT, traits> y) noexcept;
32 template<class charT, class traits>
33 constexpr bool operator< (basic_string_view<charT, traits> x,
34 basic_string_view<charT, traits> y) noexcept;
35 template<class charT, class traits>
36 constexpr bool operator> (basic_string_view<charT, traits> x,
37 basic_string_view<charT, traits> y) noexcept;
38 template<class charT, class traits>
39 constexpr bool operator<=(basic_string_view<charT, traits> x,
40 basic_string_view<charT, traits> y) noexcept;
41 template<class charT, class traits>
42 constexpr bool operator>=(basic_string_view<charT, traits> x,
43 basic_string_view<charT, traits> y) noexcept;
44 // see below, sufficient additional overloads of comparison functions
45
46 // 7.10, Inserters and extractors
47 template<class charT, class traits>
48 basic_ostream<charT, traits>&
49 operator<<(basic_ostream<charT, traits>& os,
50 basic_string_view<charT, traits> str);
51
52 // basic_string_view typedef names
53 typedef basic_string_view<char> string_view;
54 typedef basic_string_view<char16_t> u16string_view;
55 typedef basic_string_view<char32_t> u32string_view;
56 typedef basic_string_view<wchar_t> wstring_view;
57
Marshall Clow2f4bfde2014-06-18 17:44:04 +000058 template<class charT, class traits = char_traits<charT>>
59 class basic_string_view {
60 public:
61 // types
62 typedef traits traits_type;
63 typedef charT value_type;
64 typedef charT* pointer;
65 typedef const charT* const_pointer;
66 typedef charT& reference;
67 typedef const charT& const_reference;
68 typedef implementation-defined const_iterator;
69 typedef const_iterator iterator;
70 typedef reverse_iterator<const_iterator> const_reverse_iterator;
71 typedef const_reverse_iterator reverse_iterator;
72 typedef size_t size_type;
73 typedef ptrdiff_t difference_type;
74 static constexpr size_type npos = size_type(-1);
75
76 // 7.3, basic_string_view constructors and assignment operators
77 constexpr basic_string_view() noexcept;
78 constexpr basic_string_view(const basic_string_view&) noexcept = default;
79 basic_string_view& operator=(const basic_string_view&) noexcept = default;
80 template<class Allocator>
81 basic_string_view(const basic_string<charT, traits, Allocator>& str) noexcept;
82 constexpr basic_string_view(const charT* str);
83 constexpr basic_string_view(const charT* str, size_type len);
84
85 // 7.4, basic_string_view iterator support
86 constexpr const_iterator begin() const noexcept;
87 constexpr const_iterator end() const noexcept;
88 constexpr const_iterator cbegin() const noexcept;
89 constexpr const_iterator cend() const noexcept;
90 const_reverse_iterator rbegin() const noexcept;
91 const_reverse_iterator rend() const noexcept;
92 const_reverse_iterator crbegin() const noexcept;
93 const_reverse_iterator crend() const noexcept;
94
95 // 7.5, basic_string_view capacity
96 constexpr size_type size() const noexcept;
97 constexpr size_type length() const noexcept;
98 constexpr size_type max_size() const noexcept;
99 constexpr bool empty() const noexcept;
100
101 // 7.6, basic_string_view element access
102 constexpr const_reference operator[](size_type pos) const;
103 constexpr const_reference at(size_type pos) const;
104 constexpr const_reference front() const;
105 constexpr const_reference back() const;
106 constexpr const_pointer data() const noexcept;
107
108 // 7.7, basic_string_view modifiers
109 constexpr void clear() noexcept;
110 constexpr void remove_prefix(size_type n);
111 constexpr void remove_suffix(size_type n);
112 constexpr void swap(basic_string_view& s) noexcept;
113
114 // 7.8, basic_string_view string operations
115 template<class Allocator>
116 explicit operator basic_string<charT, traits, Allocator>() const;
117 template<class Allocator = allocator<charT>>
118 basic_string<charT, traits, Allocator> to_string(
119 const Allocator& a = Allocator()) const;
120
121 size_type copy(charT* s, size_type n, size_type pos = 0) const;
122
123 constexpr basic_string_view substr(size_type pos = 0, size_type n = npos) const;
124 constexpr int compare(basic_string_view s) const noexcept;
125 constexpr int compare(size_type pos1, size_type n1, basic_string_view s) const;
126 constexpr int compare(size_type pos1, size_type n1,
127 basic_string_view s, size_type pos2, size_type n2) const;
128 constexpr int compare(const charT* s) const;
129 constexpr int compare(size_type pos1, size_type n1, const charT* s) const;
130 constexpr int compare(size_type pos1, size_type n1,
131 const charT* s, size_type n2) const;
132 constexpr size_type find(basic_string_view s, size_type pos = 0) const noexcept;
133 constexpr size_type find(charT c, size_type pos = 0) const noexcept;
134 constexpr size_type find(const charT* s, size_type pos, size_type n) const;
135 constexpr size_type find(const charT* s, size_type pos = 0) const;
136 constexpr size_type rfind(basic_string_view s, size_type pos = npos) const noexcept;
137 constexpr size_type rfind(charT c, size_type pos = npos) const noexcept;
138 constexpr size_type rfind(const charT* s, size_type pos, size_type n) const;
139 constexpr size_type rfind(const charT* s, size_type pos = npos) const;
140 constexpr size_type find_first_of(basic_string_view s, size_type pos = 0) const noexcept;
141 constexpr size_type find_first_of(charT c, size_type pos = 0) const noexcept;
142 constexpr size_type find_first_of(const charT* s, size_type pos, size_type n) const;
143 constexpr size_type find_first_of(const charT* s, size_type pos = 0) const;
144 constexpr size_type find_last_of(basic_string_view s, size_type pos = npos) const noexcept;
145 constexpr size_type find_last_of(charT c, size_type pos = npos) const noexcept;
146 constexpr size_type find_last_of(const charT* s, size_type pos, size_type n) const;
147 constexpr size_type find_last_of(const charT* s, size_type pos = npos) const;
148 constexpr size_type find_first_not_of(basic_string_view s, size_type pos = 0) const noexcept;
149 constexpr size_type find_first_not_of(charT c, size_type pos = 0) const noexcept;
150 constexpr size_type find_first_not_of(const charT* s, size_type pos, size_type n) const;
151 constexpr size_type find_first_not_of(const charT* s, size_type pos = 0) const;
152 constexpr size_type find_last_not_of(basic_string_view s, size_type pos = npos) const noexcept;
153 constexpr size_type find_last_not_of(charT c, size_type pos = npos) const noexcept;
154 constexpr size_type find_last_not_of(const charT* s, size_type pos, size_type n) const;
155 constexpr size_type find_last_not_of(const charT* s, size_type pos = npos) const;
156
157 private:
158 const_pointer data_; // exposition only
159 size_type size_; // exposition only
160 };
161
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000162 } // namespace fundamentals_v1
163 } // namespace experimental
164
165 // 7.11, Hash support
166 template <class T> struct hash;
167 template <> struct hash<experimental::string_view>;
168 template <> struct hash<experimental::u16string_view>;
169 template <> struct hash<experimental::u32string_view>;
170 template <> struct hash<experimental::wstring_view>;
171
172} // namespace std
173
174
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000175*/
176
177#include <__config>
178
179#include <string>
180#include <algorithm>
181#include <iterator>
182#include <ostream>
183#include <iomanip>
184
185#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
186#pragma GCC system_header
187#endif
188
189namespace std {
190 namespace experimental {
191 inline namespace library_fundamentals_v1 {
192
193 template<class _CharT, class _Traits = _VSTD::char_traits<_CharT> >
194 class _LIBCPP_TYPE_VIS_ONLY basic_string_view {
195 public:
196 // types
197 typedef _Traits traits_type;
198 typedef _CharT value_type;
199 typedef const _CharT* pointer;
200 typedef const _CharT* const_pointer;
201 typedef const _CharT& reference;
202 typedef const _CharT& const_reference;
203 typedef const_pointer const_iterator; // See [string.view.iterators]
204 typedef const_iterator iterator;
205 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
206 typedef const_reverse_iterator reverse_iterator;
207 typedef size_t size_type;
208 typedef ptrdiff_t difference_type;
209 static _LIBCPP_CONSTEXPR const size_type npos = -1; // size_type(-1);
210
211 // [string.view.cons], construct/copy
212 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
213 basic_string_view() _NOEXCEPT : __data (nullptr), __size(0) {}
214
215 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
216 basic_string_view(const basic_string_view&) _NOEXCEPT = default;
217
218 _LIBCPP_INLINE_VISIBILITY
219 basic_string_view& operator=(const basic_string_view&) _NOEXCEPT = default;
220
221 template<class _Allocator>
222 _LIBCPP_INLINE_VISIBILITY
223 basic_string_view(const basic_string<_CharT, _Traits, _Allocator>& __str) _NOEXCEPT
224 : __data (__str.data()), __size(__str.size()) {}
225
226 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
227 basic_string_view(const _CharT* __s, size_type __len)
228 : __data(__s), __size(__len)
229 {
230 _LIBCPP_ASSERT(__len == 0 || __s != nullptr, "string_view::string_view(_CharT *, size_t): recieved nullptr");
231 }
232
233 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
234 basic_string_view(const _CharT* __s)
235 : __data(__s), __size(_Traits::length(__s)) {}
236
237 // [string.view.iterators], iterators
238 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
239 const_iterator begin() const _NOEXCEPT { return cbegin(); }
240
241 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
242 const_iterator end() const _NOEXCEPT { return cend(); }
243
244 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
245 const_iterator cbegin() const _NOEXCEPT { return __data; }
246
247 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
248 const_iterator cend() const _NOEXCEPT { return __data + __size; }
249
250 _LIBCPP_INLINE_VISIBILITY
251 const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
252
253 _LIBCPP_INLINE_VISIBILITY
254 const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
255
256 _LIBCPP_INLINE_VISIBILITY
257 const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
258
259 _LIBCPP_INLINE_VISIBILITY
260 const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
261
262 // [string.view.capacity], capacity
263 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
264 size_type size() const _NOEXCEPT { return __size; }
265
266 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
267 size_type length() const _NOEXCEPT { return __size; }
268
269 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
270 size_type max_size() const _NOEXCEPT { return _VSTD::numeric_limits<size_type>::max(); }
271
272 _LIBCPP_CONSTEXPR bool _LIBCPP_INLINE_VISIBILITY
273 empty() const _NOEXCEPT { return __size == 0; }
274
275 // [string.view.access], element access
276 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
277 const_reference operator[](size_type __pos) const { return __data[__pos]; }
278
279 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
280 const_reference at(size_type __pos) const
281 {
282 if (__pos >= size())
283 throw out_of_range("string_view::at");
284 return __data[__pos];
285 }
286
287 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
288 const_reference front() const
289 {
290 _LIBCPP_ASSERT(!empty(), "string_view::front(): string is empty");
291 return __data[0];
292 }
293
294 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
295 const_reference back() const
296 {
297 _LIBCPP_ASSERT(!empty(), "string_view::back(): string is empty");
298 return __data[__size-1];
299 }
300
301 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
302 const_pointer data() const _NOEXCEPT { return __data; }
303
304 // [string.view.modifiers], modifiers:
305 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
306 void clear() _NOEXCEPT
307 {
308 __data = nullptr;
309 __size = 0;
310 }
311
312 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
313 void remove_prefix(size_type __n) _NOEXCEPT
314 {
315 if (__n > __size)
316 __n = __size;
317 __data += __n;
318 __size -= __n;
319 }
320
321 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
322 void remove_suffix(size_type __n) _NOEXCEPT
323 {
324 if (__n > __size)
325 __n = __size;
326 __size -= __n;
327 }
328
329 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
330 void swap(basic_string_view& __other) _NOEXCEPT
331 {
Marshall Clow2f4bfde2014-06-18 17:44:04 +0000332 const value_type *__p = __data;
333 __data = __other.__data;
334 __other.__data = __p;
335
336 size_type __sz = __size;
337 __size = __other.__size;
338 __other.__size = __sz;
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000339// _VSTD::swap( __data, __other.__data );
340// _VSTD::swap( __size, __other.__size );
341 }
342
343 // [string.view.ops], string operations:
344 template<class _Allocator>
345 _LIBCPP_INLINE_VISIBILITY
346 _LIBCPP_EXPLICIT operator basic_string<_CharT, _Traits, _Allocator>() const
347 { return basic_string<_CharT, _Traits, _Allocator>( begin(), end()); }
348
349 template<class _Allocator = allocator<_CharT> >
350 _LIBCPP_INLINE_VISIBILITY
351 basic_string<_CharT, _Traits, _Allocator> to_string( const _Allocator& __a = _Allocator())
352 { return basic_string<_CharT, _Traits, _Allocator> ( begin(), end(), __a ); }
353
354 size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const
355 {
356 if ( __pos > size())
357 throw out_of_range("string_view::copy");
358 size_type __rlen = _VSTD::min( __n, size() - __pos );
359 _VSTD::copy_n(begin() + __pos, __rlen, __s );
360 return __rlen;
361 }
362
363 _LIBCPP_CONSTEXPR_AFTER_CXX11 basic_string_view substr(size_type __pos = 0, size_type __n = npos) const
364 {
365 if (__pos > size())
366 throw out_of_range("string_view::substr");
367 size_type __rlen = _VSTD::min( __n, size() - __pos );
368 return basic_string_view(data() + __pos, __rlen);
369 }
370
371 _LIBCPP_CONSTEXPR_AFTER_CXX11 int compare(basic_string_view __sv) const _NOEXCEPT
372 {
373 size_type __rlen = _VSTD::min( size(), __sv.size());
374 int __retval = _Traits::compare(data(), __sv.data(), __rlen);
375 if ( __retval == 0 ) // first __rlen chars matched
376 __retval = size() == __sv.size() ? 0 : ( size() < __sv.size() ? -1 : 1 );
377 return __retval;
378 }
379
380 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
381 int compare(size_type __pos1, size_type __n1, basic_string_view __sv) const
382 {
383 return substr(__pos1, __n1).compare(__sv);
384 }
385
386 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
387 int compare( size_type __pos1, size_type __n1,
388 basic_string_view _sv, size_type __pos2, size_type __n2) const
389 {
390 return substr(__pos1, __n1).compare(_sv.substr(__pos2, __n2));
391 }
392
393 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
394 int compare(const _CharT* __s) const
395 {
396 return compare(basic_string_view(__s));
397 }
398
399 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
400 int compare(size_type __pos1, size_type __n1, const _CharT* __s) const
401 {
402 return substr(__pos1, __n1).compare(basic_string_view(__s));
403 }
404
405 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
406 int compare(size_type __pos1, size_type __n1, const _CharT* __s, size_type __n2) const
407 {
408 return substr(__pos1, __n1).compare(basic_string_view(__s, __n2));
409 }
410
411 // find
412 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
413 size_type find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
414 {
415 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): recieved nullptr");
416 return _VSTD::__str_find<value_type, size_type, traits_type, npos>
417 (data(), size(), __s.data(), __pos, __s.size());
418 }
419
420 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
421 size_type find(_CharT __c, size_type __pos = 0) const _NOEXCEPT
422 {
423 return _VSTD::__str_find<value_type, size_type, traits_type, npos>
424 (data(), size(), __c, __pos);
425 }
426
427 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
428 size_type find(const _CharT* __s, size_type __pos, size_type __n) const
429 {
430 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find(): recieved nullptr");
431 return _VSTD::__str_find<value_type, size_type, traits_type, npos>
432 (data(), size(), __s, __pos, __n);
433 }
434
435 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
436 size_type find(const _CharT* __s, size_type __pos = 0) const
437 {
438 _LIBCPP_ASSERT(__s != nullptr, "string_view::find(): recieved nullptr");
439 return _VSTD::__str_find<value_type, size_type, traits_type, npos>
440 (data(), size(), __s, __pos, traits_type::length(__s));
441 }
442
443 // rfind
444 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
445 size_type rfind(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT
446 {
447 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): recieved nullptr");
448 return _VSTD::__str_rfind<value_type, size_type, traits_type, npos>
449 (data(), size(), __s.data(), __pos, __s.size());
450 }
451
452 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
453 size_type rfind(_CharT __c, size_type __pos = npos) const _NOEXCEPT
454 {
455 return _VSTD::__str_rfind<value_type, size_type, traits_type, npos>
456 (data(), size(), __c, __pos);
457 }
458
459 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
460 size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const
461 {
462 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::rfind(): recieved nullptr");
463 return _VSTD::__str_rfind<value_type, size_type, traits_type, npos>
464 (data(), size(), __s, __pos, __n);
465 }
466
467 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
468 size_type rfind(const _CharT* __s, size_type __pos=npos) const
469 {
470 _LIBCPP_ASSERT(__s != nullptr, "string_view::rfind(): recieved nullptr");
471 return _VSTD::__str_rfind<value_type, size_type, traits_type, npos>
472 (data(), size(), __s, __pos, traits_type::length(__s));
473 }
474
475 // find_first_of
476 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
477 size_type find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
478 {
479 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_of(): recieved nullptr");
480 return _VSTD::__str_find_first_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
485 size_type find_first_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT
486 { return find(__c, __pos); }
487
488 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
489 size_type find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
490 {
491 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_of(): recieved nullptr");
492 return _VSTD::__str_find_first_of<value_type, size_type, traits_type, npos>
493 (data(), size(), __s, __pos, __n);
494 }
495
496 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
497 size_type find_first_of(const _CharT* __s, size_type __pos=0) const
498 {
499 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_of(): recieved nullptr");
500 return _VSTD::__str_find_first_of<value_type, size_type, traits_type, npos>
501 (data(), size(), __s, __pos, traits_type::length(__s));
502 }
503
504 // find_last_of
505 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
506 size_type find_last_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
507 {
508 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_of(): recieved nullptr");
509 return _VSTD::__str_find_last_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
514 size_type find_last_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT
515 { return rfind(__c, __pos); }
516
517 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
518 size_type find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
519 {
520 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_of(): recieved nullptr");
521 return _VSTD::__str_find_last_of<value_type, size_type, traits_type, npos>
522 (data(), size(), __s, __pos, __n);
523 }
524
525 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
526 size_type find_last_of(const _CharT* __s, size_type __pos=npos) const
527 {
528 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_of(): recieved nullptr");
529 return _VSTD::__str_find_last_of<value_type, size_type, traits_type, npos>
530 (data(), size(), __s, __pos, traits_type::length(__s));
531 }
532
533 // find_first_not_of
534 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
535 size_type find_first_not_of(basic_string_view __s, size_type __pos=0) const _NOEXCEPT
536 {
537 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_not_of(): recieved nullptr");
538 return _VSTD::__str_find_first_not_of<value_type, size_type, traits_type, npos>
539 (data(), size(), __s.data(), __pos, __s.size());
540 }
541
542 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
543 size_type find_first_not_of(_CharT __c, size_type __pos=0) const _NOEXCEPT
544 {
545 return _VSTD::__str_find_first_not_of<value_type, size_type, traits_type, npos>
546 (data(), size(), __c, __pos);
547 }
548
549 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
550 size_type find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
551 {
552 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_not_of(): recieved nullptr");
553 return _VSTD::__str_find_first_not_of<value_type, size_type, traits_type, npos>
554 (data(), size(), __s, __pos, __n);
555 }
556
557 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
558 size_type find_first_not_of(const _CharT* __s, size_type __pos=0) const
559 {
560 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_not_of(): recieved nullptr");
561 return _VSTD::__str_find_first_not_of<value_type, size_type, traits_type, npos>
562 (data(), size(), __s, __pos, traits_type::length(__s));
563 }
564
565 // find_last_not_of
566 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
567 size_type find_last_not_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
568 {
569 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_not_of(): recieved nullptr");
570 return _VSTD::__str_find_last_not_of<value_type, size_type, traits_type, npos>
571 (data(), size(), __s.data(), __pos, __s.size());
572 }
573
574 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
575 size_type find_last_not_of(_CharT __c, size_type __pos=npos) const _NOEXCEPT
576 {
577 return _VSTD::__str_find_last_not_of<value_type, size_type, traits_type, npos>
578 (data(), size(), __c, __pos);
579 }
580
581 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
582 size_type find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
583 {
584 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_not_of(): recieved nullptr");
585 return _VSTD::__str_find_last_not_of<value_type, size_type, traits_type, npos>
586 (data(), size(), __s, __pos, __n);
587 }
588
589 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
590 size_type find_last_not_of(const _CharT* __s, size_type __pos=npos) const
591 {
592 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_not_of(): recieved nullptr");
593 return _VSTD::__str_find_last_not_of<value_type, size_type, traits_type, npos>
594 (data(), size(), __s, __pos, traits_type::length(__s));
595 }
596
597 private:
598 const value_type* __data;
599 size_type __size;
600 };
601
602
603 // [string.view.comparison]
604 // operator ==
605 template<class _CharT, class _Traits>
606 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
607 bool operator==(basic_string_view<_CharT, _Traits> __lhs,
608 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
609 {
610 if ( __lhs.size() != __rhs.size()) return false;
611 return __lhs.compare(__rhs) == 0;
612 }
613
614 template<class _CharT, class _Traits>
615 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
616 bool operator==(basic_string_view<_CharT, _Traits> __lhs,
617 typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
618 {
619 if ( __lhs.size() != __rhs.size()) return false;
620 return __lhs.compare(__rhs) == 0;
621 }
622
623 template<class _CharT, class _Traits>
624 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
625 bool operator==(typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
626 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
627 {
628 if ( __lhs.size() != __rhs.size()) return false;
629 return __lhs.compare(__rhs) == 0;
630 }
631
632
633 // operator !=
634 template<class _CharT, class _Traits>
635 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
636 bool operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
637 {
638 if ( __lhs.size() != __rhs.size())
639 return true;
640 return __lhs.compare(__rhs) != 0;
641 }
642
643 template<class _CharT, class _Traits>
644 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
645 bool operator!=(basic_string_view<_CharT, _Traits> __lhs,
646 typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
647 {
648 if ( __lhs.size() != __rhs.size())
649 return true;
650 return __lhs.compare(__rhs) != 0;
651 }
652
653 template<class _CharT, class _Traits>
654 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
655 bool operator!=(typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
656 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
657 {
658 if ( __lhs.size() != __rhs.size())
659 return true;
660 return __lhs.compare(__rhs) != 0;
661 }
662
663
664 // operator <
665 template<class _CharT, class _Traits>
666 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
667 bool operator<(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
668 {
669 return __lhs.compare(__rhs) < 0;
670 }
671
672 template<class _CharT, class _Traits>
673 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
674 bool operator<(basic_string_view<_CharT, _Traits> __lhs,
675 typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
676 {
677 return __lhs.compare(__rhs) < 0;
678 }
679
680 template<class _CharT, class _Traits>
681 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
682 bool operator<(typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
683 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
684 {
685 return __lhs.compare(__rhs) < 0;
686 }
687
688
689 // operator >
690 template<class _CharT, class _Traits>
691 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
692 bool operator> (basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
693 {
694 return __lhs.compare(__rhs) > 0;
695 }
696
697 template<class _CharT, class _Traits>
698 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
699 bool operator>(basic_string_view<_CharT, _Traits> __lhs,
700 typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
701 {
702 return __lhs.compare(__rhs) > 0;
703 }
704
705 template<class _CharT, class _Traits>
706 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
707 bool operator>(typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
708 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
709 {
710 return __lhs.compare(__rhs) > 0;
711 }
712
713
714 // operator <=
715 template<class _CharT, class _Traits>
716 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
717 bool operator<=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
718 {
719 return __lhs.compare(__rhs) <= 0;
720 }
721
722 template<class _CharT, class _Traits>
723 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
724 bool operator<=(basic_string_view<_CharT, _Traits> __lhs,
725 typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
726 {
727 return __lhs.compare(__rhs) <= 0;
728 }
729
730 template<class _CharT, class _Traits>
731 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
732 bool operator<=(typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
733 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
734 {
735 return __lhs.compare(__rhs) <= 0;
736 }
737
738
739 // operator >=
740 template<class _CharT, class _Traits>
741 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
742 bool operator>=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
743 {
744 return __lhs.compare(__rhs) >= 0;
745 }
746
747
748 template<class _CharT, class _Traits>
749 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
750 bool operator>=(basic_string_view<_CharT, _Traits> __lhs,
751 typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
752 {
753 return __lhs.compare(__rhs) >= 0;
754 }
755
756 template<class _CharT, class _Traits>
757 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
758 bool operator>=(typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
759 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
760 {
761 return __lhs.compare(__rhs) >= 0;
762 }
763
764
765 // [string.view.io]
766 template<class _CharT, class _Traits>
767 basic_ostream<_CharT, _Traits>&
768 operator<<(basic_ostream<_CharT, _Traits>& __os, basic_string_view<_CharT, _Traits> __sv)
769 {
770 return _VSTD::__put_character_sequence(__os, __sv.data(), __sv.size());
771 }
772
773 typedef basic_string_view<char> string_view;
774 typedef basic_string_view<char16_t> u16string_view;
775 typedef basic_string_view<char32_t> u32string_view;
776 typedef basic_string_view<wchar_t> wstring_view;
777
778}}} // close std::experimental::library_fundamentals_v1
779
780_LIBCPP_BEGIN_NAMESPACE_STD
781
782// [string.view.hash]
783// Shamelessly stolen from <string>
784template<class _CharT, class _Traits>
785struct _LIBCPP_TYPE_VIS_ONLY hash<std::experimental::basic_string_view<_CharT, _Traits> >
786 : public unary_function<std::experimental::basic_string_view<_CharT, _Traits>, size_t>
787{
788 size_t operator()(const std::experimental::basic_string_view<_CharT, _Traits>& __val) const _NOEXCEPT;
789};
790
791template<class _CharT, class _Traits>
792size_t
793hash<std::experimental::basic_string_view<_CharT, _Traits> >::operator()(
794 const std::experimental::basic_string_view<_CharT, _Traits>& __val) const _NOEXCEPT
795{
796 return __do_string_hash(__val.data(), __val.data() + __val.size());
797}
798
799#if _LIBCPP_STD_VER > 11
800template <class _CharT, class _Traits>
801__quoted_output_proxy<_CharT, const _CharT *, _Traits>
802quoted ( std::experimental::basic_string_view <_CharT, _Traits> __sv,
803 _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
804{
805 return __quoted_output_proxy<_CharT, const _CharT *, _Traits>
806 ( __sv.data(), __sv.data() + __sv.size(), __delim, __escape );
807}
808#endif
809
810_LIBCPP_END_NAMESPACE_STD
811
812#endif // _LIBCPP_LFTS_STRING_VIEW