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