libstdc++
string_view
Go to the documentation of this file.
1// Components for manipulating non-owning sequences of characters -*- C++ -*-
2
3// Copyright (C) 2013-2022 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 include/string_view
26 * This is a Standard C++ Library header.
27 */
28
29//
30// N3762 basic_string_view library
31//
32
33#ifndef _GLIBCXX_STRING_VIEW
34#define _GLIBCXX_STRING_VIEW 1
35
36#pragma GCC system_header
37
38#if __cplusplus >= 201703L
39
40#include <iosfwd>
41#include <bits/char_traits.h>
42#include <bits/functexcept.h>
44#include <bits/range_access.h>
45#include <bits/ostream_insert.h>
46#include <bits/stl_algobase.h>
47#include <ext/numeric_traits.h>
48
49#if __cplusplus >= 202002L
50# include <bits/ranges_base.h>
51#endif
52
53namespace std _GLIBCXX_VISIBILITY(default)
54{
55_GLIBCXX_BEGIN_NAMESPACE_VERSION
56
57# define __cpp_lib_string_view 201803L
58#if __cplusplus > 201703L
59# define __cpp_lib_constexpr_string_view 201811L
60#endif
61
62 // Helper for basic_string and basic_string_view members.
63 constexpr size_t
64 __sv_check(size_t __size, size_t __pos, const char* __s)
65 {
66 if (__pos > __size)
67 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > __size "
68 "(which is %zu)"), __s, __pos, __size);
69 return __pos;
70 }
71
72 // Helper for basic_string members.
73 // NB: __sv_limit doesn't check for a bad __pos value.
74 constexpr size_t
75 __sv_limit(size_t __size, size_t __pos, size_t __off) noexcept
76 {
77 const bool __testoff = __off < __size - __pos;
78 return __testoff ? __off : __size - __pos;
79 }
80
81 /**
82 * @class basic_string_view <string_view>
83 * @brief A non-owning reference to a string.
84 *
85 * @ingroup strings
86 * @ingroup sequences
87 *
88 * @tparam _CharT Type of character
89 * @tparam _Traits Traits for character type, defaults to
90 * char_traits<_CharT>.
91 *
92 * A basic_string_view looks like this:
93 *
94 * @code
95 * _CharT* _M_str
96 * size_t _M_len
97 * @endcode
98 */
99 template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
101 {
102 static_assert(!is_array_v<_CharT>);
103 static_assert(is_trivial_v<_CharT> && is_standard_layout_v<_CharT>);
104 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
105
106 public:
107
108 // types
109 using traits_type = _Traits;
110 using value_type = _CharT;
111 using pointer = value_type*;
112 using const_pointer = const value_type*;
113 using reference = value_type&;
114 using const_reference = const value_type&;
115 using const_iterator = const value_type*;
116 using iterator = const_iterator;
119 using size_type = size_t;
120 using difference_type = ptrdiff_t;
121 static constexpr size_type npos = size_type(-1);
122
123 // [string.view.cons], construction and assignment
124
125 constexpr
126 basic_string_view() noexcept
127 : _M_len{0}, _M_str{nullptr}
128 { }
129
130 constexpr basic_string_view(const basic_string_view&) noexcept = default;
131
132 [[__gnu__::__nonnull__]]
133 constexpr
134 basic_string_view(const _CharT* __str) noexcept
135 : _M_len{traits_type::length(__str)},
136 _M_str{__str}
137 { }
138
139 constexpr
140 basic_string_view(const _CharT* __str, size_type __len) noexcept
141 : _M_len{__len}, _M_str{__str}
142 { }
143
144#if __cplusplus >= 202002L && __cpp_lib_concepts
145 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
146 requires same_as<iter_value_t<_It>, _CharT>
148 constexpr
149 basic_string_view(_It __first, _End __last)
150 noexcept(noexcept(__last - __first))
151 : _M_len(__last - __first), _M_str(std::to_address(__first))
152 { }
153
154#if __cplusplus > 202002L
155 template<typename _Range, typename _DRange = remove_cvref_t<_Range>>
156 requires (!is_same_v<_DRange, basic_string_view>)
159 && is_same_v<ranges::range_value_t<_Range>, _CharT>
160 && (!is_convertible_v<_Range, const _CharT*>)
161 && (!requires (_DRange& __d) {
162 __d.operator ::std::basic_string_view<_CharT, _Traits>();
163 })
164 && (!requires { typename _DRange::traits_type; }
165 || is_same_v<typename _DRange::traits_type, _Traits>)
166 constexpr explicit
167 basic_string_view(_Range&& __r)
168 noexcept(noexcept(ranges::size(__r)) && noexcept(ranges::data(__r)))
169 : _M_len(ranges::size(__r)), _M_str(ranges::data(__r))
170 { }
171
172 basic_string_view(nullptr_t) = delete;
173#endif // C++23
174#endif // C++20
175
176 constexpr basic_string_view&
177 operator=(const basic_string_view&) noexcept = default;
178
179 // [string.view.iterators], iterator support
180
181 [[nodiscard]]
182 constexpr const_iterator
183 begin() const noexcept
184 { return this->_M_str; }
185
186 [[nodiscard]]
187 constexpr const_iterator
188 end() const noexcept
189 { return this->_M_str + this->_M_len; }
190
191 [[nodiscard]]
192 constexpr const_iterator
193 cbegin() const noexcept
194 { return this->_M_str; }
195
196 [[nodiscard]]
197 constexpr const_iterator
198 cend() const noexcept
199 { return this->_M_str + this->_M_len; }
200
201 [[nodiscard]]
202 constexpr const_reverse_iterator
203 rbegin() const noexcept
204 { return const_reverse_iterator(this->end()); }
205
206 [[nodiscard]]
207 constexpr const_reverse_iterator
208 rend() const noexcept
209 { return const_reverse_iterator(this->begin()); }
210
211 [[nodiscard]]
212 constexpr const_reverse_iterator
213 crbegin() const noexcept
214 { return const_reverse_iterator(this->end()); }
215
216 [[nodiscard]]
217 constexpr const_reverse_iterator
218 crend() const noexcept
219 { return const_reverse_iterator(this->begin()); }
220
221 // [string.view.capacity], capacity
222
223 [[nodiscard]]
224 constexpr size_type
225 size() const noexcept
226 { return this->_M_len; }
227
228 [[nodiscard]]
229 constexpr size_type
230 length() const noexcept
231 { return _M_len; }
232
233 [[nodiscard]]
234 constexpr size_type
235 max_size() const noexcept
236 {
237 return (npos - sizeof(size_type) - sizeof(void*))
238 / sizeof(value_type) / 4;
239 }
240
241 [[nodiscard]]
242 constexpr bool
243 empty() const noexcept
244 { return this->_M_len == 0; }
245
246 // [string.view.access], element access
247
248 [[nodiscard]]
249 constexpr const_reference
250 operator[](size_type __pos) const noexcept
251 {
252 __glibcxx_assert(__pos < this->_M_len);
253 return *(this->_M_str + __pos);
254 }
255
256 [[nodiscard]]
257 constexpr const_reference
258 at(size_type __pos) const
259 {
260 if (__pos >= _M_len)
261 __throw_out_of_range_fmt(__N("basic_string_view::at: __pos "
262 "(which is %zu) >= this->size() "
263 "(which is %zu)"), __pos, this->size());
264 return *(this->_M_str + __pos);
265 }
266
267 [[nodiscard]]
268 constexpr const_reference
269 front() const noexcept
270 {
271 __glibcxx_assert(this->_M_len > 0);
272 return *this->_M_str;
273 }
274
275 [[nodiscard]]
276 constexpr const_reference
277 back() const noexcept
278 {
279 __glibcxx_assert(this->_M_len > 0);
280 return *(this->_M_str + this->_M_len - 1);
281 }
282
283 [[nodiscard]]
284 constexpr const_pointer
285 data() const noexcept
286 { return this->_M_str; }
287
288 // [string.view.modifiers], modifiers:
289
290 constexpr void
291 remove_prefix(size_type __n) noexcept
292 {
293 __glibcxx_assert(this->_M_len >= __n);
294 this->_M_str += __n;
295 this->_M_len -= __n;
296 }
297
298 constexpr void
299 remove_suffix(size_type __n) noexcept
300 { this->_M_len -= __n; }
301
302 constexpr void
303 swap(basic_string_view& __sv) noexcept
304 {
305 auto __tmp = *this;
306 *this = __sv;
307 __sv = __tmp;
308 }
309
310 // [string.view.ops], string operations:
311
312 _GLIBCXX20_CONSTEXPR
313 size_type
314 copy(_CharT* __str, size_type __n, size_type __pos = 0) const
315 {
316 __glibcxx_requires_string_len(__str, __n);
317 __pos = std::__sv_check(size(), __pos, "basic_string_view::copy");
318 const size_type __rlen = std::min(__n, _M_len - __pos);
319 // _GLIBCXX_RESOLVE_LIB_DEFECTS
320 // 2777. basic_string_view::copy should use char_traits::copy
321 traits_type::copy(__str, data() + __pos, __rlen);
322 return __rlen;
323 }
324
325 [[nodiscard]]
326 constexpr basic_string_view
327 substr(size_type __pos = 0, size_type __n = npos) const noexcept(false)
328 {
329 __pos = std::__sv_check(size(), __pos, "basic_string_view::substr");
330 const size_type __rlen = std::min(__n, _M_len - __pos);
331 return basic_string_view{_M_str + __pos, __rlen};
332 }
333
334 [[nodiscard]]
335 constexpr int
336 compare(basic_string_view __str) const noexcept
337 {
338 const size_type __rlen = std::min(this->_M_len, __str._M_len);
339 int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
340 if (__ret == 0)
341 __ret = _S_compare(this->_M_len, __str._M_len);
342 return __ret;
343 }
344
345 [[nodiscard]]
346 constexpr int
347 compare(size_type __pos1, size_type __n1, basic_string_view __str) const
348 { return this->substr(__pos1, __n1).compare(__str); }
349
350 [[nodiscard]]
351 constexpr int
352 compare(size_type __pos1, size_type __n1,
353 basic_string_view __str, size_type __pos2, size_type __n2) const
354 {
355 return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
356 }
357
358 [[nodiscard, __gnu__::__nonnull__]]
359 constexpr int
360 compare(const _CharT* __str) const noexcept
361 { return this->compare(basic_string_view{__str}); }
362
363 [[nodiscard, __gnu__::__nonnull__]]
364 constexpr int
365 compare(size_type __pos1, size_type __n1, const _CharT* __str) const
366 { return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
367
368 [[nodiscard]]
369 constexpr int
370 compare(size_type __pos1, size_type __n1,
371 const _CharT* __str, size_type __n2) const noexcept(false)
372 {
373 return this->substr(__pos1, __n1)
374 .compare(basic_string_view(__str, __n2));
375 }
376
377#if __cplusplus > 201703L
378#define __cpp_lib_starts_ends_with 201711L
379 [[nodiscard]]
380 constexpr bool
381 starts_with(basic_string_view __x) const noexcept
382 { return this->substr(0, __x.size()) == __x; }
383
384 [[nodiscard]]
385 constexpr bool
386 starts_with(_CharT __x) const noexcept
387 { return !this->empty() && traits_type::eq(this->front(), __x); }
388
389 [[nodiscard, __gnu__::__nonnull__]]
390 constexpr bool
391 starts_with(const _CharT* __x) const noexcept
392 { return this->starts_with(basic_string_view(__x)); }
393
394 [[nodiscard]]
395 constexpr bool
396 ends_with(basic_string_view __x) const noexcept
397 {
398 const auto __len = this->size();
399 const auto __xlen = __x.size();
400 return __len >= __xlen
401 && traits_type::compare(end() - __xlen, __x.data(), __xlen) == 0;
402 }
403
404 [[nodiscard]]
405 constexpr bool
406 ends_with(_CharT __x) const noexcept
407 { return !this->empty() && traits_type::eq(this->back(), __x); }
408
409 [[nodiscard, __gnu__::__nonnull__]]
410 constexpr bool
411 ends_with(const _CharT* __x) const noexcept
412 { return this->ends_with(basic_string_view(__x)); }
413#endif // C++20
414
415#if __cplusplus > 202002L
416#define __cpp_lib_string_contains 202011L
417 [[nodiscard]]
418 constexpr bool
419 contains(basic_string_view __x) const noexcept
420 { return this->find(__x) != npos; }
421
422 [[nodiscard]]
423 constexpr bool
424 contains(_CharT __x) const noexcept
425 { return this->find(__x) != npos; }
426
427 [[nodiscard, __gnu__::__nonnull__]]
428 constexpr bool
429 contains(const _CharT* __x) const noexcept
430 { return this->find(__x) != npos; }
431#endif // C++23
432
433 // [string.view.find], searching
434
435 [[nodiscard]]
436 constexpr size_type
437 find(basic_string_view __str, size_type __pos = 0) const noexcept
438 { return this->find(__str._M_str, __pos, __str._M_len); }
439
440 [[nodiscard]]
441 constexpr size_type
442 find(_CharT __c, size_type __pos = 0) const noexcept;
443
444 [[nodiscard]]
445 constexpr size_type
446 find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
447
448 [[nodiscard, __gnu__::__nonnull__]]
449 constexpr size_type
450 find(const _CharT* __str, size_type __pos = 0) const noexcept
451 { return this->find(__str, __pos, traits_type::length(__str)); }
452
453 [[nodiscard]]
454 constexpr size_type
455 rfind(basic_string_view __str, size_type __pos = npos) const noexcept
456 { return this->rfind(__str._M_str, __pos, __str._M_len); }
457
458 [[nodiscard]]
459 constexpr size_type
460 rfind(_CharT __c, size_type __pos = npos) const noexcept;
461
462 [[nodiscard]]
463 constexpr size_type
464 rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
465
466 [[nodiscard, __gnu__::__nonnull__]]
467 constexpr size_type
468 rfind(const _CharT* __str, size_type __pos = npos) const noexcept
469 { return this->rfind(__str, __pos, traits_type::length(__str)); }
470
471 [[nodiscard]]
472 constexpr size_type
473 find_first_of(basic_string_view __str, size_type __pos = 0) const noexcept
474 { return this->find_first_of(__str._M_str, __pos, __str._M_len); }
475
476 [[nodiscard]]
477 constexpr size_type
478 find_first_of(_CharT __c, size_type __pos = 0) const noexcept
479 { return this->find(__c, __pos); }
480
481 [[nodiscard]]
482 constexpr size_type
483 find_first_of(const _CharT* __str, size_type __pos,
484 size_type __n) const noexcept;
485
486 [[nodiscard, __gnu__::__nonnull__]]
487 constexpr size_type
488 find_first_of(const _CharT* __str, size_type __pos = 0) const noexcept
489 { return this->find_first_of(__str, __pos, traits_type::length(__str)); }
490
491 [[nodiscard]]
492 constexpr size_type
493 find_last_of(basic_string_view __str,
494 size_type __pos = npos) const noexcept
495 { return this->find_last_of(__str._M_str, __pos, __str._M_len); }
496
497 [[nodiscard]]
498 constexpr size_type
499 find_last_of(_CharT __c, size_type __pos=npos) const noexcept
500 { return this->rfind(__c, __pos); }
501
502 [[nodiscard]]
503 constexpr size_type
504 find_last_of(const _CharT* __str, size_type __pos,
505 size_type __n) const noexcept;
506
507 [[nodiscard, __gnu__::__nonnull__]]
508 constexpr size_type
509 find_last_of(const _CharT* __str, size_type __pos = npos) const noexcept
510 { return this->find_last_of(__str, __pos, traits_type::length(__str)); }
511
512 [[nodiscard]]
513 constexpr size_type
514 find_first_not_of(basic_string_view __str,
515 size_type __pos = 0) const noexcept
516 { return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
517
518 [[nodiscard]]
519 constexpr size_type
520 find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
521
522 [[nodiscard]]
523 constexpr size_type
524 find_first_not_of(const _CharT* __str,
525 size_type __pos, size_type __n) const noexcept;
526
527 [[nodiscard, __gnu__::__nonnull__]]
528 constexpr size_type
529 find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
530 {
531 return this->find_first_not_of(__str, __pos,
532 traits_type::length(__str));
533 }
534
535 [[nodiscard]]
536 constexpr size_type
537 find_last_not_of(basic_string_view __str,
538 size_type __pos = npos) const noexcept
539 { return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
540
541 [[nodiscard]]
542 constexpr size_type
543 find_last_not_of(_CharT __c, size_type __pos = npos) const noexcept;
544
545 [[nodiscard]]
546 constexpr size_type
547 find_last_not_of(const _CharT* __str,
548 size_type __pos, size_type __n) const noexcept;
549
550 [[nodiscard, __gnu__::__nonnull__]]
551 constexpr size_type
552 find_last_not_of(const _CharT* __str,
553 size_type __pos = npos) const noexcept
554 {
555 return this->find_last_not_of(__str, __pos,
556 traits_type::length(__str));
557 }
558
559 private:
560
561 static constexpr int
562 _S_compare(size_type __n1, size_type __n2) noexcept
563 {
564 using __limits = __gnu_cxx::__int_traits<int>;
565 const difference_type __diff = __n1 - __n2;
566 if (__diff > __limits::__max)
567 return __limits::__max;
568 if (__diff < __limits::__min)
569 return __limits::__min;
570 return static_cast<int>(__diff);
571 }
572
573 size_t _M_len;
574 const _CharT* _M_str;
575 };
576
577#if __cplusplus > 201703L && __cpp_lib_concepts && __cpp_deduction_guides
578 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
580
581#if __cplusplus > 202002L
582 template<ranges::contiguous_range _Range>
583 basic_string_view(_Range&&)
585#endif
586#endif
587
588 // [string.view.comparison], non-member basic_string_view comparison function
589
590 // Several of these functions use type_identity_t to create a non-deduced
591 // context, so that only one argument participates in template argument
592 // deduction and the other argument gets implicitly converted to the deduced
593 // type (see N3766).
594
595 template<typename _CharT, typename _Traits>
596 [[nodiscard]]
597 constexpr bool
600 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
601
602 template<typename _CharT, typename _Traits>
603 [[nodiscard]]
604 constexpr bool
605 operator==(basic_string_view<_CharT, _Traits> __x,
606 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
607 noexcept
608 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
609
610#if __cpp_lib_three_way_comparison
611 template<typename _CharT, typename _Traits>
612 [[nodiscard]]
613 constexpr auto
614 operator<=>(basic_string_view<_CharT, _Traits> __x,
615 basic_string_view<_CharT, _Traits> __y) noexcept
616 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
617 { return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
618
619 template<typename _CharT, typename _Traits>
620 [[nodiscard]]
621 constexpr auto
622 operator<=>(basic_string_view<_CharT, _Traits> __x,
623 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
624 noexcept
625 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
626 { return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
627#else
628 template<typename _CharT, typename _Traits>
629 [[nodiscard]]
630 constexpr bool
631 operator==(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
632 basic_string_view<_CharT, _Traits> __y) noexcept
633 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
634
635 template<typename _CharT, typename _Traits>
636 [[nodiscard]]
637 constexpr bool
638 operator!=(basic_string_view<_CharT, _Traits> __x,
639 basic_string_view<_CharT, _Traits> __y) noexcept
640 { return !(__x == __y); }
641
642 template<typename _CharT, typename _Traits>
643 [[nodiscard]]
644 constexpr bool
645 operator!=(basic_string_view<_CharT, _Traits> __x,
646 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
647 noexcept
648 { return !(__x == __y); }
649
650 template<typename _CharT, typename _Traits>
651 [[nodiscard]]
652 constexpr bool
653 operator!=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
654 basic_string_view<_CharT, _Traits> __y) noexcept
655 { return !(__x == __y); }
656
657 template<typename _CharT, typename _Traits>
658 [[nodiscard]]
659 constexpr bool
660 operator< (basic_string_view<_CharT, _Traits> __x,
661 basic_string_view<_CharT, _Traits> __y) noexcept
662 { return __x.compare(__y) < 0; }
663
664 template<typename _CharT, typename _Traits>
665 [[nodiscard]]
666 constexpr bool
667 operator< (basic_string_view<_CharT, _Traits> __x,
668 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
669 noexcept
670 { return __x.compare(__y) < 0; }
671
672 template<typename _CharT, typename _Traits>
673 [[nodiscard]]
674 constexpr bool
675 operator< (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
676 basic_string_view<_CharT, _Traits> __y) noexcept
677 { return __x.compare(__y) < 0; }
678
679 template<typename _CharT, typename _Traits>
680 [[nodiscard]]
681 constexpr bool
682 operator> (basic_string_view<_CharT, _Traits> __x,
683 basic_string_view<_CharT, _Traits> __y) noexcept
684 { return __x.compare(__y) > 0; }
685
686 template<typename _CharT, typename _Traits>
687 [[nodiscard]]
688 constexpr bool
689 operator> (basic_string_view<_CharT, _Traits> __x,
690 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
691 noexcept
692 { return __x.compare(__y) > 0; }
693
694 template<typename _CharT, typename _Traits>
695 [[nodiscard]]
696 constexpr bool
697 operator> (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
698 basic_string_view<_CharT, _Traits> __y) noexcept
699 { return __x.compare(__y) > 0; }
700
701 template<typename _CharT, typename _Traits>
702 [[nodiscard]]
703 constexpr bool
704 operator<=(basic_string_view<_CharT, _Traits> __x,
705 basic_string_view<_CharT, _Traits> __y) noexcept
706 { return __x.compare(__y) <= 0; }
707
708 template<typename _CharT, typename _Traits>
709 [[nodiscard]]
710 constexpr bool
711 operator<=(basic_string_view<_CharT, _Traits> __x,
712 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
713 noexcept
714 { return __x.compare(__y) <= 0; }
715
716 template<typename _CharT, typename _Traits>
717 [[nodiscard]]
718 constexpr bool
719 operator<=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
720 basic_string_view<_CharT, _Traits> __y) noexcept
721 { return __x.compare(__y) <= 0; }
722
723 template<typename _CharT, typename _Traits>
724 [[nodiscard]]
725 constexpr bool
726 operator>=(basic_string_view<_CharT, _Traits> __x,
727 basic_string_view<_CharT, _Traits> __y) noexcept
728 { return __x.compare(__y) >= 0; }
729
730 template<typename _CharT, typename _Traits>
731 [[nodiscard]]
732 constexpr bool
733 operator>=(basic_string_view<_CharT, _Traits> __x,
734 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
735 noexcept
736 { return __x.compare(__y) >= 0; }
737
738 template<typename _CharT, typename _Traits>
739 [[nodiscard]]
740 constexpr bool
741 operator>=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
742 basic_string_view<_CharT, _Traits> __y) noexcept
743 { return __x.compare(__y) >= 0; }
744#endif // three-way comparison
745
746 // [string.view.io], Inserters and extractors
747 template<typename _CharT, typename _Traits>
748 inline basic_ostream<_CharT, _Traits>&
749 operator<<(basic_ostream<_CharT, _Traits>& __os,
750 basic_string_view<_CharT,_Traits> __str)
751 { return __ostream_insert(__os, __str.data(), __str.size()); }
752
753
754 // basic_string_view typedef names
755
756 using string_view = basic_string_view<char>;
757 using wstring_view = basic_string_view<wchar_t>;
758#ifdef _GLIBCXX_USE_CHAR8_T
759 using u8string_view = basic_string_view<char8_t>;
760#endif
761 using u16string_view = basic_string_view<char16_t>;
762 using u32string_view = basic_string_view<char32_t>;
763
764 // [string.view.hash], hash support:
765
766 template<typename _Tp>
767 struct hash;
768
769 template<>
770 struct hash<string_view>
771 : public __hash_base<size_t, string_view>
772 {
773 [[nodiscard]]
774 size_t
775 operator()(const string_view& __str) const noexcept
776 { return std::_Hash_impl::hash(__str.data(), __str.length()); }
777 };
778
779 template<>
780 struct __is_fast_hash<hash<string_view>> : std::false_type
781 { };
782
783 template<>
784 struct hash<wstring_view>
785 : public __hash_base<size_t, wstring_view>
786 {
787 [[nodiscard]]
788 size_t
789 operator()(const wstring_view& __s) const noexcept
790 { return std::_Hash_impl::hash(__s.data(),
791 __s.length() * sizeof(wchar_t)); }
792 };
793
794 template<>
795 struct __is_fast_hash<hash<wstring_view>> : std::false_type
796 { };
797
798#ifdef _GLIBCXX_USE_CHAR8_T
799 template<>
800 struct hash<u8string_view>
801 : public __hash_base<size_t, u8string_view>
802 {
803 [[nodiscard]]
804 size_t
805 operator()(const u8string_view& __str) const noexcept
806 { return std::_Hash_impl::hash(__str.data(), __str.length()); }
807 };
808
809 template<>
810 struct __is_fast_hash<hash<u8string_view>> : std::false_type
811 { };
812#endif
813
814 template<>
815 struct hash<u16string_view>
816 : public __hash_base<size_t, u16string_view>
817 {
818 [[nodiscard]]
819 size_t
820 operator()(const u16string_view& __s) const noexcept
821 { return std::_Hash_impl::hash(__s.data(),
822 __s.length() * sizeof(char16_t)); }
823 };
824
825 template<>
826 struct __is_fast_hash<hash<u16string_view>> : std::false_type
827 { };
828
829 template<>
830 struct hash<u32string_view>
831 : public __hash_base<size_t, u32string_view>
832 {
833 [[nodiscard]]
834 size_t
835 operator()(const u32string_view& __s) const noexcept
836 { return std::_Hash_impl::hash(__s.data(),
837 __s.length() * sizeof(char32_t)); }
838 };
839
840 template<>
841 struct __is_fast_hash<hash<u32string_view>> : std::false_type
842 { };
843
844 inline namespace literals
845 {
846 inline namespace string_view_literals
847 {
848#pragma GCC diagnostic push
849#pragma GCC diagnostic ignored "-Wliteral-suffix"
850 inline constexpr basic_string_view<char>
851 operator""sv(const char* __str, size_t __len) noexcept
852 { return basic_string_view<char>{__str, __len}; }
853
854 inline constexpr basic_string_view<wchar_t>
855 operator""sv(const wchar_t* __str, size_t __len) noexcept
856 { return basic_string_view<wchar_t>{__str, __len}; }
857
858#ifdef _GLIBCXX_USE_CHAR8_T
859 inline constexpr basic_string_view<char8_t>
860 operator""sv(const char8_t* __str, size_t __len) noexcept
861 { return basic_string_view<char8_t>{__str, __len}; }
862#endif
863
864 inline constexpr basic_string_view<char16_t>
865 operator""sv(const char16_t* __str, size_t __len) noexcept
866 { return basic_string_view<char16_t>{__str, __len}; }
867
868 inline constexpr basic_string_view<char32_t>
869 operator""sv(const char32_t* __str, size_t __len) noexcept
870 { return basic_string_view<char32_t>{__str, __len}; }
871
872#pragma GCC diagnostic pop
873 } // namespace string_literals
874 } // namespace literals
875
876#if __cpp_lib_concepts
877 namespace ranges
878 {
879 // Opt-in to borrowed_range concept
880 template<typename _CharT, typename _Traits>
881 inline constexpr bool
882 enable_borrowed_range<basic_string_view<_CharT, _Traits>> = true;
883
884 // Opt-in to view concept
885 template<typename _CharT, typename _Traits>
886 inline constexpr bool
887 enable_view<basic_string_view<_CharT, _Traits>> = true;
888 }
889#endif
890_GLIBCXX_END_NAMESPACE_VERSION
891} // namespace std
892
893#include <bits/string_view.tcc>
894
895#endif // __cplusplus <= 201402L
896
897#endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW
constexpr _Tp * to_address(_Tp *__ptr) noexcept
Obtain address referenced by a pointer to an object.
Definition: ptr_traits.h:247
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:230
ISO C++ entities toplevel namespace is std.
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition: bitset:1685
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
A non-owning reference to a string.
Definition: string_view:101
integral_constant
Definition: type_traits:63
[concept.same], concept same_as
Definition: concepts:63
[concept.convertible], concept convertible_to
Definition: concepts:72
[range.sized] The sized_range concept.
Definition: ranges_base.h:616
A range for which ranges::begin returns a contiguous iterator.
Definition: ranges_base.h:682