libstdc++: string_view Source File

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