blob: 3147e221e6229e0e4b1a11a8ea95e0bdff3d6b82 [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
Marshall Clow926731b2014-07-08 22:38:11 +0000177#include <experimental/__config>
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000178
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
Marshall Clow926731b2014-07-08 22:38:11 +0000189_LIBCPP_BEGIN_NAMESPACE_LFTS
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000190
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000191 template<class _CharT, class _Traits = _VSTD::char_traits<_CharT> >
192 class _LIBCPP_TYPE_VIS_ONLY basic_string_view {
193 public:
194 // types
195 typedef _Traits traits_type;
196 typedef _CharT value_type;
197 typedef const _CharT* pointer;
198 typedef const _CharT* const_pointer;
199 typedef const _CharT& reference;
200 typedef const _CharT& const_reference;
201 typedef const_pointer const_iterator; // See [string.view.iterators]
202 typedef const_iterator iterator;
203 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
204 typedef const_reverse_iterator reverse_iterator;
205 typedef size_t size_type;
206 typedef ptrdiff_t difference_type;
207 static _LIBCPP_CONSTEXPR const size_type npos = -1; // size_type(-1);
208
209 // [string.view.cons], construct/copy
210 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
211 basic_string_view() _NOEXCEPT : __data (nullptr), __size(0) {}
212
213 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
214 basic_string_view(const basic_string_view&) _NOEXCEPT = default;
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000215
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000216 _LIBCPP_INLINE_VISIBILITY
217 basic_string_view& operator=(const basic_string_view&) _NOEXCEPT = default;
218
219 template<class _Allocator>
220 _LIBCPP_INLINE_VISIBILITY
221 basic_string_view(const basic_string<_CharT, _Traits, _Allocator>& __str) _NOEXCEPT
222 : __data (__str.data()), __size(__str.size()) {}
223
Marshall Clow35af19a2014-07-02 15:45:57 +0000224 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000225 basic_string_view(const _CharT* __s, size_type __len)
226 : __data(__s), __size(__len)
227 {
Marshall Clow35af19a2014-07-02 15:45:57 +0000228// _LIBCPP_ASSERT(__len == 0 || __s != nullptr, "string_view::string_view(_CharT *, size_t): recieved nullptr");
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000229 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000230
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000231 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
232 basic_string_view(const _CharT* __s)
233 : __data(__s), __size(_Traits::length(__s)) {}
234
235 // [string.view.iterators], iterators
236 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
237 const_iterator begin() const _NOEXCEPT { return cbegin(); }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000238
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000239 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
240 const_iterator end() const _NOEXCEPT { return cend(); }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000241
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000242 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
243 const_iterator cbegin() const _NOEXCEPT { return __data; }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000244
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000245 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
246 const_iterator cend() const _NOEXCEPT { return __data + __size; }
247
248 _LIBCPP_INLINE_VISIBILITY
249 const_reverse_iterator rbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000250
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000251 _LIBCPP_INLINE_VISIBILITY
252 const_reverse_iterator rend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000253
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000254 _LIBCPP_INLINE_VISIBILITY
255 const_reverse_iterator crbegin() const _NOEXCEPT { return const_reverse_iterator(cend()); }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000256
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000257 _LIBCPP_INLINE_VISIBILITY
258 const_reverse_iterator crend() const _NOEXCEPT { return const_reverse_iterator(cbegin()); }
259
260 // [string.view.capacity], capacity
261 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
262 size_type size() const _NOEXCEPT { return __size; }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000263
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000264 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
265 size_type length() const _NOEXCEPT { return __size; }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000266
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000267 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
268 size_type max_size() const _NOEXCEPT { return _VSTD::numeric_limits<size_type>::max(); }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000269
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000270 _LIBCPP_CONSTEXPR bool _LIBCPP_INLINE_VISIBILITY
271 empty() const _NOEXCEPT { return __size == 0; }
272
273 // [string.view.access], element access
274 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
275 const_reference operator[](size_type __pos) const { return __data[__pos]; }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000276
Marshall Clow926731b2014-07-08 22:38:11 +0000277 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000278 const_reference at(size_type __pos) const
279 {
Marshall Clow926731b2014-07-08 22:38:11 +0000280 return __pos >= size()
281 ? throw out_of_range("string_view::at")
282 : __data[__pos];
283// if (__pos >= size())
284// throw out_of_range("string_view::at");
285// return __data[__pos];
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000286 }
287
Marshall Clow35af19a2014-07-02 15:45:57 +0000288 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000289 const_reference front() const
290 {
Marshall Clow35af19a2014-07-02 15:45:57 +0000291 return _LIBCPP_ASSERT(!empty(), "string_view::front(): string is empty"), __data[0];
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000292 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000293
Marshall Clow35af19a2014-07-02 15:45:57 +0000294 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000295 const_reference back() const
296 {
Marshall Clow35af19a2014-07-02 15:45:57 +0000297 return _LIBCPP_ASSERT(!empty(), "string_view::back(): string is empty"), __data[__size-1];
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000298 }
299
300 _LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
301 const_pointer data() const _NOEXCEPT { return __data; }
302
303 // [string.view.modifiers], modifiers:
304 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
305 void clear() _NOEXCEPT
306 {
307 __data = nullptr;
308 __size = 0;
309 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000310
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000311 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
312 void remove_prefix(size_type __n) _NOEXCEPT
313 {
314 if (__n > __size)
315 __n = __size;
316 __data += __n;
317 __size -= __n;
318 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000319
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000320 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
321 void remove_suffix(size_type __n) _NOEXCEPT
322 {
323 if (__n > __size)
324 __n = __size;
325 __size -= __n;
326 }
327
328 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
329 void swap(basic_string_view& __other) _NOEXCEPT
330 {
Marshall Clow2f4bfde2014-06-18 17:44:04 +0000331 const value_type *__p = __data;
332 __data = __other.__data;
333 __other.__data = __p;
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000334
Marshall Clow2f4bfde2014-06-18 17:44:04 +0000335 size_type __sz = __size;
336 __size = __other.__size;
337 __other.__size = __sz;
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000338// _VSTD::swap( __data, __other.__data );
339// _VSTD::swap( __size, __other.__size );
340 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000341
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000342 // [string.view.ops], string operations:
343 template<class _Allocator>
344 _LIBCPP_INLINE_VISIBILITY
345 _LIBCPP_EXPLICIT operator basic_string<_CharT, _Traits, _Allocator>() const
346 { return basic_string<_CharT, _Traits, _Allocator>( begin(), end()); }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000347
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000348 template<class _Allocator = allocator<_CharT> >
349 _LIBCPP_INLINE_VISIBILITY
350 basic_string<_CharT, _Traits, _Allocator> to_string( const _Allocator& __a = _Allocator())
351 { return basic_string<_CharT, _Traits, _Allocator> ( begin(), end(), __a ); }
352
353 size_type copy(_CharT* __s, size_type __n, size_type __pos = 0) const
354 {
355 if ( __pos > size())
356 throw out_of_range("string_view::copy");
357 size_type __rlen = _VSTD::min( __n, size() - __pos );
358 _VSTD::copy_n(begin() + __pos, __rlen, __s );
359 return __rlen;
360 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000361
Marshall Clow926731b2014-07-08 22:38:11 +0000362 _LIBCPP_CONSTEXPR
363 basic_string_view substr(size_type __pos = 0, size_type __n = npos) const
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000364 {
Marshall Clow35af19a2014-07-02 15:45:57 +0000365// 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);
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000369 return __pos > size()
370 ? throw out_of_range("string_view::substr")
371 : basic_string_view(data() + __pos, _VSTD::min(__n, size() - __pos));
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000372 }
373
374 _LIBCPP_CONSTEXPR_AFTER_CXX11 int compare(basic_string_view __sv) const _NOEXCEPT
375 {
376 size_type __rlen = _VSTD::min( size(), __sv.size());
377 int __retval = _Traits::compare(data(), __sv.data(), __rlen);
378 if ( __retval == 0 ) // first __rlen chars matched
379 __retval = size() == __sv.size() ? 0 : ( size() < __sv.size() ? -1 : 1 );
380 return __retval;
381 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000382
Marshall Clow35af19a2014-07-02 15:45:57 +0000383 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000384 int compare(size_type __pos1, size_type __n1, basic_string_view __sv) const
385 {
386 return substr(__pos1, __n1).compare(__sv);
387 }
388
Marshall Clow35af19a2014-07-02 15:45:57 +0000389 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000390 int compare( size_type __pos1, size_type __n1,
391 basic_string_view _sv, size_type __pos2, size_type __n2) const
392 {
393 return substr(__pos1, __n1).compare(_sv.substr(__pos2, __n2));
394 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000395
Marshall Clow35af19a2014-07-02 15:45:57 +0000396 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000397 int compare(const _CharT* __s) const
398 {
399 return compare(basic_string_view(__s));
400 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000401
Marshall Clow35af19a2014-07-02 15:45:57 +0000402 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000403 int compare(size_type __pos1, size_type __n1, const _CharT* __s) const
404 {
405 return substr(__pos1, __n1).compare(basic_string_view(__s));
406 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000407
Marshall Clow35af19a2014-07-02 15:45:57 +0000408 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000409 int compare(size_type __pos1, size_type __n1, const _CharT* __s, size_type __n2) const
410 {
411 return substr(__pos1, __n1).compare(basic_string_view(__s, __n2));
412 }
413
414 // find
415 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
416 size_type find(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
417 {
418 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): recieved nullptr");
419 return _VSTD::__str_find<value_type, size_type, traits_type, npos>
420 (data(), size(), __s.data(), __pos, __s.size());
421 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000422
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000423 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
424 size_type find(_CharT __c, size_type __pos = 0) const _NOEXCEPT
425 {
426 return _VSTD::__str_find<value_type, size_type, traits_type, npos>
427 (data(), size(), __c, __pos);
428 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000429
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000430 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
431 size_type find(const _CharT* __s, size_type __pos, size_type __n) const
432 {
433 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find(): recieved nullptr");
434 return _VSTD::__str_find<value_type, size_type, traits_type, npos>
435 (data(), size(), __s, __pos, __n);
436 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000437
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000438 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
439 size_type find(const _CharT* __s, size_type __pos = 0) const
440 {
441 _LIBCPP_ASSERT(__s != nullptr, "string_view::find(): recieved nullptr");
442 return _VSTD::__str_find<value_type, size_type, traits_type, npos>
443 (data(), size(), __s, __pos, traits_type::length(__s));
444 }
445
446 // rfind
447 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
448 size_type rfind(basic_string_view __s, size_type __pos = npos) const _NOEXCEPT
449 {
450 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find(): recieved nullptr");
451 return _VSTD::__str_rfind<value_type, size_type, traits_type, npos>
452 (data(), size(), __s.data(), __pos, __s.size());
453 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000454
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000455 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
456 size_type rfind(_CharT __c, size_type __pos = npos) const _NOEXCEPT
457 {
458 return _VSTD::__str_rfind<value_type, size_type, traits_type, npos>
459 (data(), size(), __c, __pos);
460 }
461
462 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
463 size_type rfind(const _CharT* __s, size_type __pos, size_type __n) const
464 {
465 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::rfind(): recieved nullptr");
466 return _VSTD::__str_rfind<value_type, size_type, traits_type, npos>
467 (data(), size(), __s, __pos, __n);
468 }
469
470 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
471 size_type rfind(const _CharT* __s, size_type __pos=npos) const
472 {
473 _LIBCPP_ASSERT(__s != nullptr, "string_view::rfind(): recieved nullptr");
474 return _VSTD::__str_rfind<value_type, size_type, traits_type, npos>
475 (data(), size(), __s, __pos, traits_type::length(__s));
476 }
477
478 // find_first_of
479 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
480 size_type find_first_of(basic_string_view __s, size_type __pos = 0) const _NOEXCEPT
481 {
482 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_of(): recieved nullptr");
483 return _VSTD::__str_find_first_of<value_type, size_type, traits_type, npos>
484 (data(), size(), __s.data(), __pos, __s.size());
485 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000486
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000487 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
488 size_type find_first_of(_CharT __c, size_type __pos = 0) const _NOEXCEPT
489 { return find(__c, __pos); }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000490
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000491 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
492 size_type find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
493 {
494 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_of(): recieved nullptr");
495 return _VSTD::__str_find_first_of<value_type, size_type, traits_type, npos>
496 (data(), size(), __s, __pos, __n);
497 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000498
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000499 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
500 size_type find_first_of(const _CharT* __s, size_type __pos=0) const
501 {
502 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_of(): recieved nullptr");
503 return _VSTD::__str_find_first_of<value_type, size_type, traits_type, npos>
504 (data(), size(), __s, __pos, traits_type::length(__s));
505 }
506
507 // find_last_of
508 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
509 size_type find_last_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
510 {
511 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_of(): recieved nullptr");
512 return _VSTD::__str_find_last_of<value_type, size_type, traits_type, npos>
513 (data(), size(), __s.data(), __pos, __s.size());
514 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000515
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000516 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
517 size_type find_last_of(_CharT __c, size_type __pos = npos) const _NOEXCEPT
518 { return rfind(__c, __pos); }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000519
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000520 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
521 size_type find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
522 {
523 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_of(): recieved nullptr");
524 return _VSTD::__str_find_last_of<value_type, size_type, traits_type, npos>
525 (data(), size(), __s, __pos, __n);
526 }
527
528 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
529 size_type find_last_of(const _CharT* __s, size_type __pos=npos) const
530 {
531 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_of(): recieved nullptr");
532 return _VSTD::__str_find_last_of<value_type, size_type, traits_type, npos>
533 (data(), size(), __s, __pos, traits_type::length(__s));
534 }
535
536 // find_first_not_of
537 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
538 size_type find_first_not_of(basic_string_view __s, size_type __pos=0) const _NOEXCEPT
539 {
540 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_first_not_of(): recieved nullptr");
541 return _VSTD::__str_find_first_not_of<value_type, size_type, traits_type, npos>
542 (data(), size(), __s.data(), __pos, __s.size());
543 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000544
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000545 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
546 size_type find_first_not_of(_CharT __c, size_type __pos=0) const _NOEXCEPT
547 {
548 return _VSTD::__str_find_first_not_of<value_type, size_type, traits_type, npos>
549 (data(), size(), __c, __pos);
550 }
551
552 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
553 size_type find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
554 {
555 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_first_not_of(): recieved nullptr");
556 return _VSTD::__str_find_first_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
561 size_type find_first_not_of(const _CharT* __s, size_type __pos=0) const
562 {
563 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_first_not_of(): recieved nullptr");
564 return _VSTD::__str_find_first_not_of<value_type, size_type, traits_type, npos>
565 (data(), size(), __s, __pos, traits_type::length(__s));
566 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000567
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000568 // find_last_not_of
569 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
570 size_type find_last_not_of(basic_string_view __s, size_type __pos=npos) const _NOEXCEPT
571 {
572 _LIBCPP_ASSERT(__s.size() == 0 || __s.data() != nullptr, "string_view::find_last_not_of(): recieved nullptr");
573 return _VSTD::__str_find_last_not_of<value_type, size_type, traits_type, npos>
574 (data(), size(), __s.data(), __pos, __s.size());
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000575 }
576
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000577 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
578 size_type find_last_not_of(_CharT __c, size_type __pos=npos) const _NOEXCEPT
579 {
580 return _VSTD::__str_find_last_not_of<value_type, size_type, traits_type, npos>
581 (data(), size(), __c, __pos);
582 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000583
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000584 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
585 size_type find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
586 {
587 _LIBCPP_ASSERT(__n == 0 || __s != nullptr, "string_view::find_last_not_of(): recieved nullptr");
588 return _VSTD::__str_find_last_not_of<value_type, size_type, traits_type, npos>
589 (data(), size(), __s, __pos, __n);
590 }
591
592 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
593 size_type find_last_not_of(const _CharT* __s, size_type __pos=npos) const
594 {
595 _LIBCPP_ASSERT(__s != nullptr, "string_view::find_last_not_of(): recieved nullptr");
596 return _VSTD::__str_find_last_not_of<value_type, size_type, traits_type, npos>
597 (data(), size(), __s, __pos, traits_type::length(__s));
598 }
599
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000600 private:
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000601 const value_type* __data;
602 size_type __size;
603 };
604
605
606 // [string.view.comparison]
607 // operator ==
608 template<class _CharT, class _Traits>
609 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
610 bool operator==(basic_string_view<_CharT, _Traits> __lhs,
611 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
612 {
613 if ( __lhs.size() != __rhs.size()) return false;
614 return __lhs.compare(__rhs) == 0;
615 }
616
617 template<class _CharT, class _Traits>
618 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
619 bool operator==(basic_string_view<_CharT, _Traits> __lhs,
620 typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
621 {
622 if ( __lhs.size() != __rhs.size()) return false;
623 return __lhs.compare(__rhs) == 0;
624 }
625
626 template<class _CharT, class _Traits>
627 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
628 bool operator==(typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
629 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
630 {
631 if ( __lhs.size() != __rhs.size()) return false;
632 return __lhs.compare(__rhs) == 0;
633 }
634
635
636 // operator !=
637 template<class _CharT, class _Traits>
638 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
639 bool operator!=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
640 {
641 if ( __lhs.size() != __rhs.size())
642 return true;
643 return __lhs.compare(__rhs) != 0;
644 }
Eric Fiselier04c39ec2014-08-10 20:56:31 +0000645
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000646 template<class _CharT, class _Traits>
647 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
648 bool operator!=(basic_string_view<_CharT, _Traits> __lhs,
649 typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
650 {
651 if ( __lhs.size() != __rhs.size())
652 return true;
653 return __lhs.compare(__rhs) != 0;
654 }
655
656 template<class _CharT, class _Traits>
657 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
658 bool operator!=(typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
659 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
660 {
661 if ( __lhs.size() != __rhs.size())
662 return true;
663 return __lhs.compare(__rhs) != 0;
664 }
665
666
667 // operator <
668 template<class _CharT, class _Traits>
669 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
670 bool operator<(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
671 {
672 return __lhs.compare(__rhs) < 0;
673 }
674
675 template<class _CharT, class _Traits>
676 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
677 bool operator<(basic_string_view<_CharT, _Traits> __lhs,
678 typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
679 {
680 return __lhs.compare(__rhs) < 0;
681 }
682
683 template<class _CharT, class _Traits>
684 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
685 bool operator<(typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
686 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
687 {
688 return __lhs.compare(__rhs) < 0;
689 }
690
691
692 // operator >
693 template<class _CharT, class _Traits>
694 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
695 bool operator> (basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
696 {
697 return __lhs.compare(__rhs) > 0;
698 }
699
700 template<class _CharT, class _Traits>
701 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
702 bool operator>(basic_string_view<_CharT, _Traits> __lhs,
703 typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
704 {
705 return __lhs.compare(__rhs) > 0;
706 }
707
708 template<class _CharT, class _Traits>
709 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
710 bool operator>(typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
711 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
712 {
713 return __lhs.compare(__rhs) > 0;
714 }
715
716
717 // operator <=
718 template<class _CharT, class _Traits>
719 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
720 bool operator<=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
721 {
722 return __lhs.compare(__rhs) <= 0;
723 }
724
725 template<class _CharT, class _Traits>
726 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
727 bool operator<=(basic_string_view<_CharT, _Traits> __lhs,
728 typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
729 {
730 return __lhs.compare(__rhs) <= 0;
731 }
732
733 template<class _CharT, class _Traits>
734 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
735 bool operator<=(typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
736 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
737 {
738 return __lhs.compare(__rhs) <= 0;
739 }
740
741
742 // operator >=
743 template<class _CharT, class _Traits>
744 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
745 bool operator>=(basic_string_view<_CharT, _Traits> __lhs, basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
746 {
747 return __lhs.compare(__rhs) >= 0;
748 }
749
750
751 template<class _CharT, class _Traits>
752 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
753 bool operator>=(basic_string_view<_CharT, _Traits> __lhs,
754 typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __rhs) _NOEXCEPT
755 {
756 return __lhs.compare(__rhs) >= 0;
757 }
758
759 template<class _CharT, class _Traits>
760 _LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
761 bool operator>=(typename _VSTD::common_type<basic_string_view<_CharT, _Traits> >::type __lhs,
762 basic_string_view<_CharT, _Traits> __rhs) _NOEXCEPT
763 {
764 return __lhs.compare(__rhs) >= 0;
765 }
766
767
768 // [string.view.io]
769 template<class _CharT, class _Traits>
770 basic_ostream<_CharT, _Traits>&
771 operator<<(basic_ostream<_CharT, _Traits>& __os, basic_string_view<_CharT, _Traits> __sv)
772 {
773 return _VSTD::__put_character_sequence(__os, __sv.data(), __sv.size());
774 }
775
776 typedef basic_string_view<char> string_view;
777 typedef basic_string_view<char16_t> u16string_view;
778 typedef basic_string_view<char32_t> u32string_view;
779 typedef basic_string_view<wchar_t> wstring_view;
780
Marshall Clow926731b2014-07-08 22:38:11 +0000781_LIBCPP_END_NAMESPACE_LFTS
Marshall Clow5aa8fa22014-06-11 16:44:55 +0000782_LIBCPP_BEGIN_NAMESPACE_STD
783
784// [string.view.hash]
785// Shamelessly stolen from <string>
786template<class _CharT, class _Traits>
787struct _LIBCPP_TYPE_VIS_ONLY hash<std::experimental::basic_string_view<_CharT, _Traits> >
788 : public unary_function<std::experimental::basic_string_view<_CharT, _Traits>, size_t>
789{
790 size_t operator()(const std::experimental::basic_string_view<_CharT, _Traits>& __val) const _NOEXCEPT;
791};
792
793template<class _CharT, class _Traits>
794size_t
795hash<std::experimental::basic_string_view<_CharT, _Traits> >::operator()(
796 const std::experimental::basic_string_view<_CharT, _Traits>& __val) const _NOEXCEPT
797{
798 return __do_string_hash(__val.data(), __val.data() + __val.size());
799}
800
801#if _LIBCPP_STD_VER > 11
802template <class _CharT, class _Traits>
803__quoted_output_proxy<_CharT, const _CharT *, _Traits>
804quoted ( std::experimental::basic_string_view <_CharT, _Traits> __sv,
805 _CharT __delim = _CharT('"'), _CharT __escape=_CharT('\\'))
806{
807 return __quoted_output_proxy<_CharT, const _CharT *, _Traits>
808 ( __sv.data(), __sv.data() + __sv.size(), __delim, __escape );
809}
810#endif
811
812_LIBCPP_END_NAMESPACE_STD
813
814#endif // _LIBCPP_LFTS_STRING_VIEW