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