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