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