]>
Commit | Line | Data |
---|---|---|
77cba5af ESR |
1 | // Components for manipulating non-owning sequences of characters -*- C++ -*- |
2 | ||
a5544970 | 3 | // Copyright (C) 2013-2019 Free Software Foundation, Inc. |
77cba5af ESR |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
8 | // Free Software Foundation; either version 3, or (at your option) | |
9 | // any later version. | |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
16 | // Under Section 7 of GPL version 3, you are granted additional | |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
19 | ||
20 | // You should have received a copy of the GNU General Public License and | |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
24 | ||
25 | /** @file experimental/string_view | |
6b4f8906 | 26 | * This is a TS C++ Library header. |
1ababc8b | 27 | * @ingroup libfund-ts |
77cba5af ESR |
28 | */ |
29 | ||
30 | // | |
b032ec0d | 31 | // N3762 basic_string_view library |
77cba5af ESR |
32 | // |
33 | ||
34 | #ifndef _GLIBCXX_EXPERIMENTAL_STRING_VIEW | |
35 | #define _GLIBCXX_EXPERIMENTAL_STRING_VIEW 1 | |
36 | ||
37 | #pragma GCC system_header | |
38 | ||
d681026d | 39 | #if __cplusplus >= 201402L |
77cba5af | 40 | |
77cba5af ESR |
41 | #include <string> |
42 | #include <limits> | |
e347987d | 43 | #include <experimental/bits/lfts_config.h> |
77cba5af | 44 | |
32f9be16 | 45 | namespace std _GLIBCXX_VISIBILITY(default) |
77cba5af | 46 | { |
4a15d842 FD |
47 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
48 | ||
77cba5af ESR |
49 | namespace experimental |
50 | { | |
7ce56fc8 JW |
51 | inline namespace fundamentals_v1 |
52 | { | |
65545817 | 53 | #define __cpp_lib_experimental_string_view 201411 |
c2513a1f | 54 | |
77cba5af | 55 | /** |
fb3bc977 | 56 | * @class basic_string_view <experimental/string_view> |
77cba5af ESR |
57 | * @brief A non-owning reference to a string. |
58 | * | |
59 | * @ingroup strings | |
60 | * @ingroup sequences | |
1ababc8b | 61 | * @ingroup libfund-ts |
77cba5af ESR |
62 | * |
63 | * @tparam _CharT Type of character | |
64 | * @tparam _Traits Traits for character type, defaults to | |
65 | * char_traits<_CharT>. | |
66 | * | |
67 | * A basic_string_view looks like this: | |
68 | * | |
69 | * @code | |
70 | * _CharT* _M_str | |
71 | * size_t _M_len | |
72 | * @endcode | |
73 | */ | |
16545743 | 74 | template<typename _CharT, typename _Traits = std::char_traits<_CharT>> |
77cba5af ESR |
75 | class basic_string_view |
76 | { | |
77cba5af ESR |
77 | public: |
78 | ||
79 | // types | |
80 | using traits_type = _Traits; | |
81 | using value_type = _CharT; | |
fb8b3e29 | 82 | using pointer = _CharT*; |
77cba5af | 83 | using const_pointer = const _CharT*; |
fb8b3e29 | 84 | using reference = _CharT&; |
77cba5af ESR |
85 | using const_reference = const _CharT&; |
86 | using const_iterator = const _CharT*; | |
87 | using iterator = const_iterator; | |
88 | using const_reverse_iterator = std::reverse_iterator<const_iterator>; | |
89 | using reverse_iterator = const_reverse_iterator; | |
90 | using size_type = size_t; | |
91 | using difference_type = ptrdiff_t; | |
92 | static constexpr size_type npos = size_type(-1); | |
93 | ||
94 | // [string.view.cons], construct/copy | |
95 | ||
96 | constexpr | |
97 | basic_string_view() noexcept | |
16545743 | 98 | : _M_len{0}, _M_str{nullptr} |
77cba5af ESR |
99 | { } |
100 | ||
101 | constexpr basic_string_view(const basic_string_view&) noexcept = default; | |
102 | ||
103 | template<typename _Allocator> | |
660dca79 PC |
104 | basic_string_view(const basic_string<_CharT, _Traits, |
105 | _Allocator>& __str) noexcept | |
77cba5af ESR |
106 | : _M_len{__str.length()}, _M_str{__str.data()} |
107 | { } | |
108 | ||
109 | constexpr basic_string_view(const _CharT* __str) | |
32f9be16 | 110 | : _M_len{__str == nullptr ? 0 : traits_type::length(__str)}, |
16545743 | 111 | _M_str{__str} |
77cba5af ESR |
112 | { } |
113 | ||
114 | constexpr basic_string_view(const _CharT* __str, size_type __len) | |
16545743 JW |
115 | : _M_len{__len}, |
116 | _M_str{__str} | |
77cba5af ESR |
117 | { } |
118 | ||
119 | basic_string_view& | |
120 | operator=(const basic_string_view&) noexcept = default; | |
121 | ||
122 | // [string.view.iterators], iterators | |
123 | ||
124 | constexpr const_iterator | |
125 | begin() const noexcept | |
126 | { return this->_M_str; } | |
127 | ||
128 | constexpr const_iterator | |
129 | end() const noexcept | |
130 | { return this->_M_str + this->_M_len; } | |
131 | ||
132 | constexpr const_iterator | |
133 | cbegin() const noexcept | |
134 | { return this->_M_str; } | |
135 | ||
136 | constexpr const_iterator | |
137 | cend() const noexcept | |
138 | { return this->_M_str + this->_M_len; } | |
139 | ||
140 | const_reverse_iterator | |
141 | rbegin() const noexcept | |
16545743 | 142 | { return const_reverse_iterator(this->end()); } |
77cba5af ESR |
143 | |
144 | const_reverse_iterator | |
145 | rend() const noexcept | |
16545743 | 146 | { return const_reverse_iterator(this->begin()); } |
77cba5af ESR |
147 | |
148 | const_reverse_iterator | |
149 | crbegin() const noexcept | |
16545743 | 150 | { return const_reverse_iterator(this->end()); } |
77cba5af ESR |
151 | |
152 | const_reverse_iterator | |
153 | crend() const noexcept | |
16545743 | 154 | { return const_reverse_iterator(this->begin()); } |
77cba5af ESR |
155 | |
156 | // [string.view.capacity], capacity | |
157 | ||
158 | constexpr size_type | |
159 | size() const noexcept | |
160 | { return this->_M_len; } | |
161 | ||
162 | constexpr size_type | |
163 | length() const noexcept | |
164 | { return _M_len; } | |
165 | ||
166 | constexpr size_type | |
167 | max_size() const noexcept | |
16545743 JW |
168 | { |
169 | return (npos - sizeof(size_type) - sizeof(void*)) | |
170 | / sizeof(value_type) / 4; | |
171 | } | |
77cba5af | 172 | |
d715f554 | 173 | _GLIBCXX_NODISCARD constexpr bool |
77cba5af ESR |
174 | empty() const noexcept |
175 | { return this->_M_len == 0; } | |
176 | ||
177 | // [string.view.access], element access | |
178 | ||
179 | constexpr const _CharT& | |
180 | operator[](size_type __pos) const | |
181 | { | |
9d0bb2ea | 182 | // TODO: Assert to restore in a way compatible with the constexpr. |
2f1e8e7c | 183 | // __glibcxx_assert(__pos < this->_M_len); |
77cba5af ESR |
184 | return *(this->_M_str + __pos); |
185 | } | |
186 | ||
187 | constexpr const _CharT& | |
188 | at(size_type __pos) const | |
189 | { | |
190 | return __pos < this->_M_len | |
191 | ? *(this->_M_str + __pos) | |
192 | : (__throw_out_of_range_fmt(__N("basic_string_view::at: __pos " | |
193 | "(which is %zu) >= this->size() " | |
194 | "(which is %zu)"), | |
195 | __pos, this->size()), | |
16545743 | 196 | *this->_M_str); |
77cba5af ESR |
197 | } |
198 | ||
199 | constexpr const _CharT& | |
200 | front() const | |
201 | { | |
9d0bb2ea | 202 | // TODO: Assert to restore in a way compatible with the constexpr. |
2f1e8e7c | 203 | // __glibcxx_assert(this->_M_len > 0); |
77cba5af ESR |
204 | return *this->_M_str; |
205 | } | |
206 | ||
207 | constexpr const _CharT& | |
208 | back() const | |
209 | { | |
9d0bb2ea | 210 | // TODO: Assert to restore in a way compatible with the constexpr. |
2f1e8e7c | 211 | // __glibcxx_assert(this->_M_len > 0); |
77cba5af ESR |
212 | return *(this->_M_str + this->_M_len - 1); |
213 | } | |
214 | ||
215 | constexpr const _CharT* | |
216 | data() const noexcept | |
217 | { return this->_M_str; } | |
218 | ||
219 | // [string.view.modifiers], modifiers: | |
16545743 | 220 | |
2be7a166 | 221 | constexpr void |
77cba5af ESR |
222 | remove_prefix(size_type __n) |
223 | { | |
2f1e8e7c | 224 | __glibcxx_assert(this->_M_len >= __n); |
77cba5af ESR |
225 | this->_M_str += __n; |
226 | this->_M_len -= __n; | |
227 | } | |
228 | ||
2be7a166 | 229 | constexpr void |
77cba5af ESR |
230 | remove_suffix(size_type __n) |
231 | { this->_M_len -= __n; } | |
232 | ||
2be7a166 | 233 | constexpr void |
77cba5af ESR |
234 | swap(basic_string_view& __sv) noexcept |
235 | { | |
2be7a166 JW |
236 | auto __tmp = *this; |
237 | *this = __sv; | |
238 | __sv = __tmp; | |
77cba5af ESR |
239 | } |
240 | ||
241 | ||
242 | // [string.view.ops], string operations: | |
243 | ||
244 | template<typename _Allocator> | |
245 | explicit operator basic_string<_CharT, _Traits, _Allocator>() const | |
246 | { | |
16545743 JW |
247 | return { this->_M_str, this->_M_len }; |
248 | } | |
249 | ||
250 | template<typename _Allocator = std::allocator<_CharT>> | |
251 | basic_string<_CharT, _Traits, _Allocator> | |
252 | to_string(const _Allocator& __alloc = _Allocator()) const | |
253 | { | |
254 | return { this->_M_str, this->_M_len, __alloc }; | |
77cba5af ESR |
255 | } |
256 | ||
257 | size_type | |
258 | copy(_CharT* __str, size_type __n, size_type __pos = 0) const | |
259 | { | |
260 | __glibcxx_requires_string_len(__str, __n); | |
bb393514 ESR |
261 | if (__pos > this->_M_len) |
262 | __throw_out_of_range_fmt(__N("basic_string_view::copy: __pos " | |
263 | "(which is %zu) > this->size() " | |
77cba5af ESR |
264 | "(which is %zu)"), |
265 | __pos, this->size()); | |
266 | size_type __rlen{std::min(__n, size_type{this->_M_len - __pos})}; | |
267 | for (auto __begin = this->_M_str + __pos, | |
bb393514 | 268 | __end = __begin + __rlen; __begin != __end;) |
77cba5af ESR |
269 | *__str++ = *__begin++; |
270 | return __rlen; | |
271 | } | |
272 | ||
273 | ||
274 | // [string.view.ops], string operations: | |
275 | ||
276 | constexpr basic_string_view | |
0a1369fa | 277 | substr(size_type __pos = 0, size_type __n = npos) const |
77cba5af | 278 | { |
bb393514 | 279 | return __pos <= this->_M_len |
77cba5af ESR |
280 | ? basic_string_view{this->_M_str + __pos, |
281 | std::min(__n, size_type{this->_M_len - __pos})} | |
bb393514 ESR |
282 | : (__throw_out_of_range_fmt(__N("basic_string_view::substr: __pos " |
283 | "(which is %zu) > this->size() " | |
77cba5af ESR |
284 | "(which is %zu)"), |
285 | __pos, this->size()), basic_string_view{}); | |
286 | } | |
287 | ||
2be7a166 | 288 | constexpr int |
77cba5af ESR |
289 | compare(basic_string_view __str) const noexcept |
290 | { | |
291 | int __ret = traits_type::compare(this->_M_str, __str._M_str, | |
292 | std::min(this->_M_len, __str._M_len)); | |
293 | if (__ret == 0) | |
294 | __ret = _S_compare(this->_M_len, __str._M_len); | |
295 | return __ret; | |
296 | } | |
297 | ||
2be7a166 | 298 | constexpr int |
77cba5af ESR |
299 | compare(size_type __pos1, size_type __n1, basic_string_view __str) const |
300 | { return this->substr(__pos1, __n1).compare(__str); } | |
301 | ||
2be7a166 | 302 | constexpr int |
77cba5af ESR |
303 | compare(size_type __pos1, size_type __n1, |
304 | basic_string_view __str, size_type __pos2, size_type __n2) const | |
305 | { return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2)); } | |
306 | ||
2be7a166 | 307 | constexpr int |
77cba5af ESR |
308 | compare(const _CharT* __str) const noexcept |
309 | { return this->compare(basic_string_view{__str}); } | |
310 | ||
2be7a166 | 311 | constexpr int |
77cba5af ESR |
312 | compare(size_type __pos1, size_type __n1, const _CharT* __str) const |
313 | { return this->substr(__pos1, __n1).compare(basic_string_view{__str}); } | |
314 | ||
2be7a166 | 315 | constexpr int |
77cba5af ESR |
316 | compare(size_type __pos1, size_type __n1, |
317 | const _CharT* __str, size_type __n2) const | |
318 | { | |
319 | return this->substr(__pos1, __n1) | |
320 | .compare(basic_string_view(__str, __n2)); | |
321 | } | |
322 | ||
2be7a166 | 323 | constexpr size_type |
77cba5af ESR |
324 | find(basic_string_view __str, size_type __pos = 0) const noexcept |
325 | { return this->find(__str._M_str, __pos, __str._M_len); } | |
326 | ||
2be7a166 | 327 | constexpr size_type |
77cba5af ESR |
328 | find(_CharT __c, size_type __pos=0) const noexcept; |
329 | ||
2be7a166 | 330 | constexpr size_type |
b9de84c9 | 331 | find(const _CharT* __str, size_type __pos, size_type __n) const noexcept; |
77cba5af | 332 | |
2be7a166 | 333 | constexpr size_type |
77cba5af ESR |
334 | find(const _CharT* __str, size_type __pos=0) const noexcept |
335 | { return this->find(__str, __pos, traits_type::length(__str)); } | |
336 | ||
2be7a166 | 337 | constexpr size_type |
77cba5af ESR |
338 | rfind(basic_string_view __str, size_type __pos = npos) const noexcept |
339 | { return this->rfind(__str._M_str, __pos, __str._M_len); } | |
340 | ||
2be7a166 | 341 | constexpr size_type |
77cba5af ESR |
342 | rfind(_CharT __c, size_type __pos = npos) const noexcept; |
343 | ||
2be7a166 | 344 | constexpr size_type |
b9de84c9 | 345 | rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept; |
77cba5af | 346 | |
2be7a166 | 347 | constexpr size_type |
77cba5af ESR |
348 | rfind(const _CharT* __str, size_type __pos = npos) const noexcept |
349 | { return this->rfind(__str, __pos, traits_type::length(__str)); } | |
350 | ||
2be7a166 | 351 | constexpr size_type |
77cba5af ESR |
352 | find_first_of(basic_string_view __str, size_type __pos = 0) const noexcept |
353 | { return this->find_first_of(__str._M_str, __pos, __str._M_len); } | |
354 | ||
2be7a166 | 355 | constexpr size_type |
77cba5af ESR |
356 | find_first_of(_CharT __c, size_type __pos = 0) const noexcept |
357 | { return this->find(__c, __pos); } | |
358 | ||
2be7a166 | 359 | constexpr size_type |
77cba5af ESR |
360 | find_first_of(const _CharT* __str, size_type __pos, size_type __n) const; |
361 | ||
2be7a166 | 362 | constexpr size_type |
77cba5af ESR |
363 | find_first_of(const _CharT* __str, size_type __pos = 0) const noexcept |
364 | { return this->find_first_of(__str, __pos, traits_type::length(__str)); } | |
365 | ||
2be7a166 | 366 | constexpr size_type |
77cba5af ESR |
367 | find_last_of(basic_string_view __str, |
368 | size_type __pos = npos) const noexcept | |
369 | { return this->find_last_of(__str._M_str, __pos, __str._M_len); } | |
370 | ||
2be7a166 | 371 | constexpr size_type |
77cba5af ESR |
372 | find_last_of(_CharT __c, size_type __pos=npos) const noexcept |
373 | { return this->rfind(__c, __pos); } | |
374 | ||
2be7a166 | 375 | constexpr size_type |
77cba5af ESR |
376 | find_last_of(const _CharT* __str, size_type __pos, size_type __n) const; |
377 | ||
2be7a166 | 378 | constexpr size_type |
77cba5af ESR |
379 | find_last_of(const _CharT* __str, size_type __pos = npos) const noexcept |
380 | { return this->find_last_of(__str, __pos, traits_type::length(__str)); } | |
381 | ||
2be7a166 | 382 | constexpr size_type |
77cba5af ESR |
383 | find_first_not_of(basic_string_view __str, |
384 | size_type __pos = 0) const noexcept | |
385 | { return this->find_first_not_of(__str._M_str, __pos, __str._M_len); } | |
386 | ||
2be7a166 | 387 | constexpr size_type |
77cba5af ESR |
388 | find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept; |
389 | ||
2be7a166 | 390 | constexpr size_type |
77cba5af ESR |
391 | find_first_not_of(const _CharT* __str, |
392 | size_type __pos, size_type __n) const; | |
393 | ||
2be7a166 | 394 | constexpr size_type |
77cba5af ESR |
395 | find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept |
396 | { | |
397 | return this->find_first_not_of(__str, __pos, | |
398 | traits_type::length(__str)); | |
399 | } | |
400 | ||
2be7a166 | 401 | constexpr size_type |
77cba5af ESR |
402 | find_last_not_of(basic_string_view __str, |
403 | size_type __pos = npos) const noexcept | |
404 | { return this->find_last_not_of(__str._M_str, __pos, __str._M_len); } | |
405 | ||
2be7a166 | 406 | constexpr size_type |
77cba5af ESR |
407 | find_last_not_of(_CharT __c, size_type __pos = npos) const noexcept; |
408 | ||
2be7a166 | 409 | constexpr size_type |
77cba5af ESR |
410 | find_last_not_of(const _CharT* __str, |
411 | size_type __pos, size_type __n) const; | |
412 | ||
2be7a166 | 413 | constexpr size_type |
77cba5af ESR |
414 | find_last_not_of(const _CharT* __str, |
415 | size_type __pos = npos) const noexcept | |
416 | { | |
417 | return this->find_last_not_of(__str, __pos, | |
418 | traits_type::length(__str)); | |
419 | } | |
420 | ||
421 | private: | |
32f9be16 | 422 | |
7571121f | 423 | static constexpr int |
77cba5af ESR |
424 | _S_compare(size_type __n1, size_type __n2) noexcept |
425 | { | |
ad8fda29 | 426 | return difference_type(__n1 - __n2) > std::numeric_limits<int>::max() |
77cba5af | 427 | ? std::numeric_limits<int>::max() |
ad8fda29 | 428 | : difference_type(__n1 - __n2) < std::numeric_limits<int>::min() |
77cba5af | 429 | ? std::numeric_limits<int>::min() |
ad8fda29 | 430 | : static_cast<int>(difference_type(__n1 - __n2)); |
77cba5af ESR |
431 | } |
432 | ||
433 | size_t _M_len; | |
434 | const _CharT* _M_str; | |
435 | }; | |
436 | ||
77cba5af ESR |
437 | // [string.view.comparison], non-member basic_string_view comparison functions |
438 | ||
439 | namespace __detail | |
440 | { | |
3e5fb20f JW |
441 | // Identity transform to create a non-deduced context, so that only one |
442 | // argument participates in template argument deduction and the other | |
443 | // argument gets implicitly converted to the deduced type. See n3766.html. | |
77cba5af | 444 | template<typename _Tp> |
3e5fb20f | 445 | using __idt = common_type_t<_Tp>; |
77cba5af ESR |
446 | } |
447 | ||
448 | template<typename _CharT, typename _Traits> | |
2be7a166 | 449 | constexpr bool |
77cba5af ESR |
450 | operator==(basic_string_view<_CharT, _Traits> __x, |
451 | basic_string_view<_CharT, _Traits> __y) noexcept | |
8cb7179d | 452 | { return __x.size() == __y.size() && __x.compare(__y) == 0; } |
77cba5af ESR |
453 | |
454 | template<typename _CharT, typename _Traits> | |
2be7a166 | 455 | constexpr bool |
77cba5af ESR |
456 | operator==(basic_string_view<_CharT, _Traits> __x, |
457 | __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept | |
8cb7179d | 458 | { return __x.size() == __y.size() && __x.compare(__y) == 0; } |
77cba5af ESR |
459 | |
460 | template<typename _CharT, typename _Traits> | |
2be7a166 | 461 | constexpr bool |
77cba5af ESR |
462 | operator==(__detail::__idt<basic_string_view<_CharT, _Traits>> __x, |
463 | basic_string_view<_CharT, _Traits> __y) noexcept | |
8cb7179d | 464 | { return __x.size() == __y.size() && __x.compare(__y) == 0; } |
77cba5af ESR |
465 | |
466 | template<typename _CharT, typename _Traits> | |
2be7a166 | 467 | constexpr bool |
77cba5af ESR |
468 | operator!=(basic_string_view<_CharT, _Traits> __x, |
469 | basic_string_view<_CharT, _Traits> __y) noexcept | |
470 | { return !(__x == __y); } | |
471 | ||
472 | template<typename _CharT, typename _Traits> | |
2be7a166 | 473 | constexpr bool |
77cba5af ESR |
474 | operator!=(basic_string_view<_CharT, _Traits> __x, |
475 | __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept | |
476 | { return !(__x == __y); } | |
477 | ||
478 | template<typename _CharT, typename _Traits> | |
2be7a166 | 479 | constexpr bool |
77cba5af ESR |
480 | operator!=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x, |
481 | basic_string_view<_CharT, _Traits> __y) noexcept | |
482 | { return !(__x == __y); } | |
483 | ||
484 | template<typename _CharT, typename _Traits> | |
2be7a166 | 485 | constexpr bool |
77cba5af ESR |
486 | operator< (basic_string_view<_CharT, _Traits> __x, |
487 | basic_string_view<_CharT, _Traits> __y) noexcept | |
488 | { return __x.compare(__y) < 0; } | |
489 | ||
490 | template<typename _CharT, typename _Traits> | |
2be7a166 | 491 | constexpr bool |
77cba5af ESR |
492 | operator< (basic_string_view<_CharT, _Traits> __x, |
493 | __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept | |
494 | { return __x.compare(__y) < 0; } | |
495 | ||
496 | template<typename _CharT, typename _Traits> | |
2be7a166 | 497 | constexpr bool |
77cba5af ESR |
498 | operator< (__detail::__idt<basic_string_view<_CharT, _Traits>> __x, |
499 | basic_string_view<_CharT, _Traits> __y) noexcept | |
500 | { return __x.compare(__y) < 0; } | |
501 | ||
502 | template<typename _CharT, typename _Traits> | |
2be7a166 | 503 | constexpr bool |
77cba5af ESR |
504 | operator> (basic_string_view<_CharT, _Traits> __x, |
505 | basic_string_view<_CharT, _Traits> __y) noexcept | |
506 | { return __x.compare(__y) > 0; } | |
507 | ||
508 | template<typename _CharT, typename _Traits> | |
2be7a166 | 509 | constexpr bool |
77cba5af ESR |
510 | operator> (basic_string_view<_CharT, _Traits> __x, |
511 | __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept | |
512 | { return __x.compare(__y) > 0; } | |
513 | ||
514 | template<typename _CharT, typename _Traits> | |
2be7a166 | 515 | constexpr bool |
77cba5af ESR |
516 | operator> (__detail::__idt<basic_string_view<_CharT, _Traits>> __x, |
517 | basic_string_view<_CharT, _Traits> __y) noexcept | |
518 | { return __x.compare(__y) > 0; } | |
519 | ||
520 | template<typename _CharT, typename _Traits> | |
2be7a166 | 521 | constexpr bool |
77cba5af ESR |
522 | operator<=(basic_string_view<_CharT, _Traits> __x, |
523 | basic_string_view<_CharT, _Traits> __y) noexcept | |
524 | { return __x.compare(__y) <= 0; } | |
525 | ||
526 | template<typename _CharT, typename _Traits> | |
2be7a166 | 527 | constexpr bool |
77cba5af ESR |
528 | operator<=(basic_string_view<_CharT, _Traits> __x, |
529 | __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept | |
530 | { return __x.compare(__y) <= 0; } | |
531 | ||
532 | template<typename _CharT, typename _Traits> | |
2be7a166 | 533 | constexpr bool |
77cba5af ESR |
534 | operator<=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x, |
535 | basic_string_view<_CharT, _Traits> __y) noexcept | |
536 | { return __x.compare(__y) <= 0; } | |
537 | ||
538 | template<typename _CharT, typename _Traits> | |
2be7a166 | 539 | constexpr bool |
77cba5af ESR |
540 | operator>=(basic_string_view<_CharT, _Traits> __x, |
541 | basic_string_view<_CharT, _Traits> __y) noexcept | |
542 | { return __x.compare(__y) >= 0; } | |
543 | ||
544 | template<typename _CharT, typename _Traits> | |
2be7a166 | 545 | constexpr bool |
77cba5af ESR |
546 | operator>=(basic_string_view<_CharT, _Traits> __x, |
547 | __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept | |
548 | { return __x.compare(__y) >= 0; } | |
549 | ||
550 | template<typename _CharT, typename _Traits> | |
2be7a166 | 551 | constexpr bool |
77cba5af ESR |
552 | operator>=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x, |
553 | basic_string_view<_CharT, _Traits> __y) noexcept | |
554 | { return __x.compare(__y) >= 0; } | |
555 | ||
16545743 | 556 | // [string.view.io], Inserters and extractors |
77cba5af | 557 | template<typename _CharT, typename _Traits> |
16545743 JW |
558 | inline basic_ostream<_CharT, _Traits>& |
559 | operator<<(basic_ostream<_CharT, _Traits>& __os, | |
560 | basic_string_view<_CharT,_Traits> __str) | |
561 | { return __ostream_insert(__os, __str.data(), __str.size()); } | |
77cba5af ESR |
562 | |
563 | ||
564 | // basic_string_view typedef names | |
565 | ||
566 | using string_view = basic_string_view<char>; | |
567 | #ifdef _GLIBCXX_USE_WCHAR_T | |
568 | using wstring_view = basic_string_view<wchar_t>; | |
c124af93 TH |
569 | #endif |
570 | #ifdef _GLIBCXX_USE_CHAR8_T | |
571 | using u8string_view = basic_string_view<char8_t>; | |
77cba5af | 572 | #endif |
77cba5af ESR |
573 | using u16string_view = basic_string_view<char16_t>; |
574 | using u32string_view = basic_string_view<char32_t>; | |
7ce56fc8 | 575 | } // namespace fundamentals_v1 |
77cba5af ESR |
576 | } // namespace experimental |
577 | ||
578 | ||
579 | // [string.view.hash], hash support: | |
77cba5af ESR |
580 | template<typename _Tp> |
581 | struct hash; | |
582 | ||
583 | template<> | |
584 | struct hash<experimental::string_view> | |
585 | : public __hash_base<size_t, experimental::string_view> | |
586 | { | |
587 | size_t | |
588 | operator()(const experimental::string_view& __str) const noexcept | |
589 | { return std::_Hash_impl::hash(__str.data(), __str.length()); } | |
590 | }; | |
591 | ||
592 | template<> | |
593 | struct __is_fast_hash<hash<experimental::string_view>> : std::false_type | |
594 | { }; | |
595 | ||
596 | #ifdef _GLIBCXX_USE_WCHAR_T | |
597 | template<> | |
598 | struct hash<experimental::wstring_view> | |
599 | : public __hash_base<size_t, wstring> | |
600 | { | |
601 | size_t | |
602 | operator()(const experimental::wstring_view& __s) const noexcept | |
603 | { return std::_Hash_impl::hash(__s.data(), | |
604 | __s.length() * sizeof(wchar_t)); } | |
605 | }; | |
606 | ||
607 | template<> | |
608 | struct __is_fast_hash<hash<experimental::wstring_view>> : std::false_type | |
609 | { }; | |
610 | #endif | |
611 | ||
c124af93 TH |
612 | #ifdef _GLIBCXX_USE_CHAR8_T |
613 | template<> | |
614 | struct hash<experimental::u8string_view> | |
615 | : public __hash_base<size_t, experimental::u8string_view> | |
616 | { | |
617 | size_t | |
618 | operator()(const experimental::u8string_view& __s) const noexcept | |
619 | { return std::_Hash_impl::hash(__s.data(), __s.length()); } | |
620 | }; | |
621 | ||
622 | template<> | |
623 | struct __is_fast_hash<hash<experimental::u8string_view>> : std::false_type | |
624 | { }; | |
625 | #endif | |
626 | ||
77cba5af ESR |
627 | template<> |
628 | struct hash<experimental::u16string_view> | |
629 | : public __hash_base<size_t, experimental::u16string_view> | |
630 | { | |
631 | size_t | |
632 | operator()(const experimental::u16string_view& __s) const noexcept | |
633 | { return std::_Hash_impl::hash(__s.data(), | |
634 | __s.length() * sizeof(char16_t)); } | |
635 | }; | |
636 | ||
637 | template<> | |
638 | struct __is_fast_hash<hash<experimental::u16string_view>> : std::false_type | |
639 | { }; | |
640 | ||
641 | template<> | |
642 | struct hash<experimental::u32string_view> | |
643 | : public __hash_base<size_t, experimental::u32string_view> | |
644 | { | |
645 | size_t | |
646 | operator()(const experimental::u32string_view& __s) const noexcept | |
647 | { return std::_Hash_impl::hash(__s.data(), | |
648 | __s.length() * sizeof(char32_t)); } | |
649 | }; | |
650 | ||
651 | template<> | |
652 | struct __is_fast_hash<hash<experimental::u32string_view>> : std::false_type | |
653 | { }; | |
77cba5af ESR |
654 | |
655 | namespace experimental | |
656 | { | |
77cba5af ESR |
657 | // I added these EMSR. |
658 | inline namespace literals | |
659 | { | |
660 | inline namespace string_view_literals | |
661 | { | |
f03858e5 JW |
662 | #pragma GCC diagnostic push |
663 | #pragma GCC diagnostic ignored "-Wliteral-suffix" | |
0a9f0ecb | 664 | inline constexpr basic_string_view<char> |
6a19d2a2 | 665 | operator""sv(const char* __str, size_t __len) noexcept |
77cba5af ESR |
666 | { return basic_string_view<char>{__str, __len}; } |
667 | ||
668 | #ifdef _GLIBCXX_USE_WCHAR_T | |
0a9f0ecb | 669 | inline constexpr basic_string_view<wchar_t> |
6a19d2a2 | 670 | operator""sv(const wchar_t* __str, size_t __len) noexcept |
77cba5af ESR |
671 | { return basic_string_view<wchar_t>{__str, __len}; } |
672 | #endif | |
673 | ||
c124af93 TH |
674 | #ifdef _GLIBCXX_USE_CHAR8_T |
675 | inline constexpr basic_string_view<char8_t> | |
676 | operator""sv(const char8_t* __str, size_t __len) noexcept | |
677 | { return basic_string_view<char8_t>{__str, __len}; } | |
678 | #endif | |
679 | ||
0a9f0ecb | 680 | inline constexpr basic_string_view<char16_t> |
6a19d2a2 | 681 | operator""sv(const char16_t* __str, size_t __len) noexcept |
77cba5af ESR |
682 | { return basic_string_view<char16_t>{__str, __len}; } |
683 | ||
0a9f0ecb | 684 | inline constexpr basic_string_view<char32_t> |
6a19d2a2 | 685 | operator""sv(const char32_t* __str, size_t __len) noexcept |
77cba5af | 686 | { return basic_string_view<char32_t>{__str, __len}; } |
f03858e5 | 687 | #pragma GCC diagnostic pop |
e347987d JW |
688 | } // namespace string_literals |
689 | } // namespace literals | |
77cba5af | 690 | } // namespace experimental |
4a15d842 FD |
691 | |
692 | _GLIBCXX_END_NAMESPACE_VERSION | |
77cba5af ESR |
693 | } // namespace std |
694 | ||
ea4554ea | 695 | #include <experimental/bits/string_view.tcc> |
77cba5af ESR |
696 | |
697 | #endif // __cplusplus <= 201103L | |
698 | ||
699 | #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW |