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#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_string_view
44#define __glibcxx_want_starts_ends_with
45#define __glibcxx_want_string_contains
46#include <bits/version.h>
47
48#if __cplusplus >= 201703L
49
50#include <bits/char_traits.h>
51#include <bits/functexcept.h>
52#include <bits/functional_hash.h>
53#include <bits/range_access.h>
54#include <bits/stl_algobase.h>
55#include <ext/numeric_traits.h>
56
57#if __cplusplus >= 202002L
58# include <bits/ranges_base.h>
59#endif
60
61#if _GLIBCXX_HOSTED
62# include <iosfwd>
63# include <bits/ostream_insert.h>
64#endif
65
66namespace std _GLIBCXX_VISIBILITY(default)
67{
68_GLIBCXX_BEGIN_NAMESPACE_VERSION
69
70 // Helper for basic_string and basic_string_view members.
71 constexpr size_t
72 __sv_check(size_t __size, size_t __pos, const char* __s)
73 {
74 if (__pos > __size)
75 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > __size "
76 "(which is %zu)"), __s, __pos, __size);
77 return __pos;
78 }
79
80 // Helper for basic_string members.
81 // NB: __sv_limit doesn't check for a bad __pos value.
82 constexpr size_t
83 __sv_limit(size_t __size, size_t __pos, size_t __off) noexcept
84 {
85 const bool __testoff = __off < __size - __pos;
86 return __testoff ? __off : __size - __pos;
87 }
88
89 /**
90 * @class basic_string_view <string_view>
91 * @brief A non-owning reference to a string.
92 *
93 * @ingroup strings
94 * @ingroup sequences
95 *
96 * @tparam _CharT Type of character
97 * @tparam _Traits Traits for character type, defaults to
98 * char_traits<_CharT>.
99 *
100 * A basic_string_view looks like this:
101 *
102 * @code
103 * _CharT* _M_str
104 * size_t _M_len
105 * @endcode
106 */
107 template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
108 class basic_string_view
109 {
110 static_assert(!is_array_v<_CharT>);
111 static_assert(is_trivial_v<_CharT> && is_standard_layout_v<_CharT>);
112 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
113
114 public:
115
116 // types
117 using traits_type = _Traits;
118 using value_type = _CharT;
119 using pointer = value_type*;
120 using const_pointer = const value_type*;
121 using reference = value_type&;
122 using const_reference = const value_type&;
123 using const_iterator = const value_type*;
124 using iterator = const_iterator;
125 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
126 using reverse_iterator = const_reverse_iterator;
127 using size_type = size_t;
128 using difference_type = ptrdiff_t;
129 static constexpr size_type npos = size_type(-1);
130
131 // [string.view.cons], construction and assignment
132
133 constexpr
134 basic_string_view() noexcept
135 : _M_len{0}, _M_str{nullptr}
136 { }
137
138 constexpr basic_string_view(const basic_string_view&) noexcept = default;
139
140 [[__gnu__::__nonnull__]]
141 constexpr
142 basic_string_view(const _CharT* __str) noexcept
143 : _M_len{traits_type::length(__str)},
144 _M_str{__str}
145 { }
146
147 constexpr
148 basic_string_view(const _CharT* __str, size_type __len) noexcept
149 : _M_len{__len}, _M_str{__str}
150 { }
151
152#if __cplusplus >= 202002L && __cpp_lib_concepts
153 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
154 requires same_as<iter_value_t<_It>, _CharT>
155 && (!convertible_to<_End, size_type>)
156 constexpr
157 basic_string_view(_It __first, _End __last)
158 noexcept(noexcept(__last - __first))
159 : _M_len(__last - __first), _M_str(std::to_address(__first))
160 { }
161
162#if __cplusplus > 202002L
163 template<typename _Range, typename _DRange = remove_cvref_t<_Range>>
164 requires (!is_same_v<_DRange, basic_string_view>)
165 && ranges::contiguous_range<_Range>
166 && ranges::sized_range<_Range>
167 && is_same_v<ranges::range_value_t<_Range>, _CharT>
168 && (!is_convertible_v<_Range, const _CharT*>)
169 && (!requires (_DRange& __d) {
170 __d.operator ::std::basic_string_view<_CharT, _Traits>();
171 })
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 {
391 return _M_len >= __x._M_len
392 && traits_type::compare(_M_str, __x._M_str, __x._M_len) == 0;
393 }
394
395 [[nodiscard]]
396 constexpr bool
397 starts_with(_CharT __x) const noexcept
398 { return !this->empty() && traits_type::eq(this->front(), __x); }
399
400 [[nodiscard, __gnu__::__nonnull__]]
401 constexpr bool
402 starts_with(const _CharT* __x) const noexcept
403 { return this->starts_with(basic_string_view(__x)); }
404
405 [[nodiscard]]
406 constexpr bool
407 ends_with(basic_string_view __x) const noexcept
408 {
409 const auto __len = this->size();
410 const auto __xlen = __x.size();
411 return __len >= __xlen
412 && traits_type::compare(end() - __xlen, __x.data(), __xlen) == 0;
413 }
414
415 [[nodiscard]]
416 constexpr bool
417 ends_with(_CharT __x) const noexcept
418 { return !this->empty() && traits_type::eq(this->back(), __x); }
419
420 [[nodiscard, __gnu__::__nonnull__]]
421 constexpr bool
422 ends_with(const _CharT* __x) const noexcept
423 { return this->ends_with(basic_string_view(__x)); }
424#endif // __cpp_lib_starts_ends_with
425
426#if __cplusplus > 202002L
427#if _GLIBCXX_HOSTED && !defined(__cpp_lib_string_contains)
428 // This FTM is not freestanding as it also implies matching <string>
429 // support, and <string> is omitted from the freestanding subset.
430# error "libstdc++ bug: string_contains not defined when it should be"
431#endif // HOSTED
432 [[nodiscard]]
433 constexpr bool
434 contains(basic_string_view __x) const noexcept
435 { return this->find(__x) != npos; }
436
437 [[nodiscard]]
438 constexpr bool
439 contains(_CharT __x) const noexcept
440 { return this->find(__x) != npos; }
441
442 [[nodiscard, __gnu__::__nonnull__]]
443 constexpr bool
444 contains(const _CharT* __x) const noexcept
445 { return this->find(__x) != npos; }
446#endif // C++23
447
448 // [string.view.find], searching
449
450 [[nodiscard]]
451 constexpr size_type
452 find(basic_string_view __str, size_type __pos = 0) const noexcept
453 { return this->find(__str._M_str, __pos, __str._M_len); }
454
455 [[nodiscard]]
456 constexpr size_type
457 find(_CharT __c, size_type __pos = 0) const noexcept;
458
459 [[nodiscard]]
460 constexpr size_type
461 find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
462
463 [[nodiscard, __gnu__::__nonnull__]]
464 constexpr size_type
465 find(const _CharT* __str, size_type __pos = 0) const noexcept
466 { return this->find(__str, __pos, traits_type::length(__str)); }
467
468 [[nodiscard]]
469 constexpr size_type
470 rfind(basic_string_view __str, size_type __pos = npos) const noexcept
471 { return this->rfind(__str._M_str, __pos, __str._M_len); }
472
473 [[nodiscard]]
474 constexpr size_type
475 rfind(_CharT __c, size_type __pos = npos) const noexcept;
476
477 [[nodiscard]]
478 constexpr size_type
479 rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
480
481 [[nodiscard, __gnu__::__nonnull__]]
482 constexpr size_type
483 rfind(const _CharT* __str, size_type __pos = npos) const noexcept
484 { return this->rfind(__str, __pos, traits_type::length(__str)); }
485
486 [[nodiscard]]
487 constexpr size_type
488 find_first_of(basic_string_view __str, size_type __pos = 0) const noexcept
489 { return this->find_first_of(__str._M_str, __pos, __str._M_len); }
490
491 [[nodiscard]]
492 constexpr size_type
493 find_first_of(_CharT __c, size_type __pos = 0) const noexcept
494 { return this->find(__c, __pos); }
495
496 [[nodiscard]]
497 constexpr size_type
498 find_first_of(const _CharT* __str, size_type __pos,
499 size_type __n) const noexcept;
500
501 [[nodiscard, __gnu__::__nonnull__]]
502 constexpr size_type
503 find_first_of(const _CharT* __str, size_type __pos = 0) const noexcept
504 { return this->find_first_of(__str, __pos, traits_type::length(__str)); }
505
506 [[nodiscard]]
507 constexpr size_type
508 find_last_of(basic_string_view __str,
509 size_type __pos = npos) const noexcept
510 { return this->find_last_of(__str._M_str, __pos, __str._M_len); }
511
512 [[nodiscard]]
513 constexpr size_type
514 find_last_of(_CharT __c, size_type __pos=npos) const noexcept
515 { return this->rfind(__c, __pos); }
516
517 [[nodiscard]]
518 constexpr size_type
519 find_last_of(const _CharT* __str, size_type __pos,
520 size_type __n) const noexcept;
521
522 [[nodiscard, __gnu__::__nonnull__]]
523 constexpr size_type
524 find_last_of(const _CharT* __str, size_type __pos = npos) const noexcept
525 { return this->find_last_of(__str, __pos, traits_type::length(__str)); }
526
527 [[nodiscard]]
528 constexpr size_type
529 find_first_not_of(basic_string_view __str,
530 size_type __pos = 0) const noexcept
531 { return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
532
533 [[nodiscard]]
534 constexpr size_type
535 find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
536
537 [[nodiscard]]
538 constexpr size_type
539 find_first_not_of(const _CharT* __str,
540 size_type __pos, size_type __n) const noexcept;
541
542 [[nodiscard, __gnu__::__nonnull__]]
543 constexpr size_type
544 find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
545 {
546 return this->find_first_not_of(__str, __pos,
547 traits_type::length(__str));
548 }
549
550 [[nodiscard]]
551 constexpr size_type
552 find_last_not_of(basic_string_view __str,
553 size_type __pos = npos) const noexcept
554 { return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
555
556 [[nodiscard]]
557 constexpr size_type
558 find_last_not_of(_CharT __c, size_type __pos = npos) const noexcept;
559
560 [[nodiscard]]
561 constexpr size_type
562 find_last_not_of(const _CharT* __str,
563 size_type __pos, size_type __n) const noexcept;
564
565 [[nodiscard, __gnu__::__nonnull__]]
566 constexpr size_type
567 find_last_not_of(const _CharT* __str,
568 size_type __pos = npos) const noexcept
569 {
570 return this->find_last_not_of(__str, __pos,
571 traits_type::length(__str));
572 }
573
574 private:
575
576 static constexpr int
577 _S_compare(size_type __n1, size_type __n2) noexcept
578 {
579 using __limits = __gnu_cxx::__int_traits<int>;
580 const difference_type __diff = __n1 - __n2;
581 if (__diff > __limits::__max)
582 return __limits::__max;
583 if (__diff < __limits::__min)
584 return __limits::__min;
585 return static_cast<int>(__diff);
586 }
587
588 size_t _M_len;
589 const _CharT* _M_str;
590 };
591
592#if __cplusplus > 201703L && __cpp_lib_concepts && __cpp_deduction_guides
593 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
594 basic_string_view(_It, _End) -> basic_string_view<iter_value_t<_It>>;
595
596#if __cplusplus > 202002L
597 template<ranges::contiguous_range _Range>
598 basic_string_view(_Range&&)
599 -> basic_string_view<ranges::range_value_t<_Range>>;
600#endif
601#endif
602
603 // [string.view.comparison], non-member basic_string_view comparison function
604
605 // Several of these functions use type_identity_t to create a non-deduced
606 // context, so that only one argument participates in template argument
607 // deduction and the other argument gets implicitly converted to the deduced
608 // type (see N3766).
609
610#if __cpp_lib_three_way_comparison
611 template<typename _CharT, typename _Traits>
612 [[nodiscard]]
613 constexpr bool
614 operator==(basic_string_view<_CharT, _Traits> __x,
615 type_identity_t<basic_string_view<_CharT, _Traits>> __y)
616 noexcept
617 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
618
619 template<typename _CharT, typename _Traits>
620 [[nodiscard]]
621 constexpr auto
622 operator<=>(basic_string_view<_CharT, _Traits> __x,
623 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
624 noexcept
625 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
626 { return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
627#else
628 template<typename _CharT, typename _Traits>
629 [[nodiscard]]
630 constexpr bool
631 operator==(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 bool
639 operator==(basic_string_view<_CharT, _Traits> __x,
640 basic_string_view<_CharT, _Traits> __y) noexcept
641 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
642
643 template<typename _CharT, typename _Traits>
644 [[nodiscard]]
645 constexpr bool
646 operator==(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
647 basic_string_view<_CharT, _Traits> __y) noexcept
648 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
649
650 template<typename _CharT, typename _Traits>
651 [[nodiscard]]
652 constexpr bool
653 operator!=(basic_string_view<_CharT, _Traits> __x,
654 basic_string_view<_CharT, _Traits> __y) noexcept
655 { return !(__x == __y); }
656
657 template<typename _CharT, typename _Traits>
658 [[nodiscard]]
659 constexpr bool
660 operator!=(basic_string_view<_CharT, _Traits> __x,
661 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
662 noexcept
663 { return !(__x == __y); }
664
665 template<typename _CharT, typename _Traits>
666 [[nodiscard]]
667 constexpr bool
668 operator!=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
669 basic_string_view<_CharT, _Traits> __y) noexcept
670 { return !(__x == __y); }
671
672 template<typename _CharT, typename _Traits>
673 [[nodiscard]]
674 constexpr bool
675 operator< (basic_string_view<_CharT, _Traits> __x,
676 basic_string_view<_CharT, _Traits> __y) noexcept
677 { return __x.compare(__y) < 0; }
678
679 template<typename _CharT, typename _Traits>
680 [[nodiscard]]
681 constexpr bool
682 operator< (basic_string_view<_CharT, _Traits> __x,
683 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
684 noexcept
685 { return __x.compare(__y) < 0; }
686
687 template<typename _CharT, typename _Traits>
688 [[nodiscard]]
689 constexpr bool
690 operator< (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
691 basic_string_view<_CharT, _Traits> __y) noexcept
692 { return __x.compare(__y) < 0; }
693
694 template<typename _CharT, typename _Traits>
695 [[nodiscard]]
696 constexpr bool
697 operator> (basic_string_view<_CharT, _Traits> __x,
698 basic_string_view<_CharT, _Traits> __y) noexcept
699 { return __x.compare(__y) > 0; }
700
701 template<typename _CharT, typename _Traits>
702 [[nodiscard]]
703 constexpr bool
704 operator> (basic_string_view<_CharT, _Traits> __x,
705 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
706 noexcept
707 { return __x.compare(__y) > 0; }
708
709 template<typename _CharT, typename _Traits>
710 [[nodiscard]]
711 constexpr bool
712 operator> (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
713 basic_string_view<_CharT, _Traits> __y) noexcept
714 { return __x.compare(__y) > 0; }
715
716 template<typename _CharT, typename _Traits>
717 [[nodiscard]]
718 constexpr bool
719 operator<=(basic_string_view<_CharT, _Traits> __x,
720 basic_string_view<_CharT, _Traits> __y) noexcept
721 { return __x.compare(__y) <= 0; }
722
723 template<typename _CharT, typename _Traits>
724 [[nodiscard]]
725 constexpr bool
726 operator<=(basic_string_view<_CharT, _Traits> __x,
727 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
728 noexcept
729 { return __x.compare(__y) <= 0; }
730
731 template<typename _CharT, typename _Traits>
732 [[nodiscard]]
733 constexpr bool
734 operator<=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
735 basic_string_view<_CharT, _Traits> __y) noexcept
736 { return __x.compare(__y) <= 0; }
737
738 template<typename _CharT, typename _Traits>
739 [[nodiscard]]
740 constexpr bool
741 operator>=(basic_string_view<_CharT, _Traits> __x,
742 basic_string_view<_CharT, _Traits> __y) noexcept
743 { return __x.compare(__y) >= 0; }
744
745 template<typename _CharT, typename _Traits>
746 [[nodiscard]]
747 constexpr bool
748 operator>=(basic_string_view<_CharT, _Traits> __x,
749 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
750 noexcept
751 { return __x.compare(__y) >= 0; }
752
753 template<typename _CharT, typename _Traits>
754 [[nodiscard]]
755 constexpr bool
756 operator>=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
757 basic_string_view<_CharT, _Traits> __y) noexcept
758 { return __x.compare(__y) >= 0; }
759#endif // three-way comparison
760
761#if _GLIBCXX_HOSTED
762 // [string.view.io], Inserters and extractors
763 template<typename _CharT, typename _Traits>
764 inline basic_ostream<_CharT, _Traits>&
765 operator<<(basic_ostream<_CharT, _Traits>& __os,
766 basic_string_view<_CharT,_Traits> __str)
767 { return __ostream_insert(__os, __str.data(), __str.size()); }
768#endif // HOSTED
769
770 // basic_string_view typedef names
771
772 using string_view = basic_string_view<char>;
773 using wstring_view = basic_string_view<wchar_t>;
774#ifdef _GLIBCXX_USE_CHAR8_T
775 using u8string_view = basic_string_view<char8_t>;
776#endif
777 using u16string_view = basic_string_view<char16_t>;
778 using u32string_view = basic_string_view<char32_t>;
779
780 // [string.view.hash], hash support:
781
782 template<typename _Tp>
783 struct hash;
784
785 template<>
786 struct hash<string_view>
787 : public __hash_base<size_t, string_view>
788 {
789 [[nodiscard]]
790 size_t
791 operator()(const string_view& __str) const noexcept
792 { return std::_Hash_impl::hash(__str.data(), __str.length()); }
793 };
794
795 template<>
796 struct __is_fast_hash<hash<string_view>> : std::false_type
797 { };
798
799 template<>
800 struct hash<wstring_view>
801 : public __hash_base<size_t, wstring_view>
802 {
803 [[nodiscard]]
804 size_t
805 operator()(const wstring_view& __s) const noexcept
806 { return std::_Hash_impl::hash(__s.data(),
807 __s.length() * sizeof(wchar_t)); }
808 };
809
810 template<>
811 struct __is_fast_hash<hash<wstring_view>> : std::false_type
812 { };
813
814#ifdef _GLIBCXX_USE_CHAR8_T
815 template<>
816 struct hash<u8string_view>
817 : public __hash_base<size_t, u8string_view>
818 {
819 [[nodiscard]]
820 size_t
821 operator()(const u8string_view& __str) const noexcept
822 { return std::_Hash_impl::hash(__str.data(), __str.length()); }
823 };
824
825 template<>
826 struct __is_fast_hash<hash<u8string_view>> : std::false_type
827 { };
828#endif
829
830 template<>
831 struct hash<u16string_view>
832 : public __hash_base<size_t, u16string_view>
833 {
834 [[nodiscard]]
835 size_t
836 operator()(const u16string_view& __s) const noexcept
837 { return std::_Hash_impl::hash(__s.data(),
838 __s.length() * sizeof(char16_t)); }
839 };
840
841 template<>
842 struct __is_fast_hash<hash<u16string_view>> : std::false_type
843 { };
844
845 template<>
846 struct hash<u32string_view>
847 : public __hash_base<size_t, u32string_view>
848 {
849 [[nodiscard]]
850 size_t
851 operator()(const u32string_view& __s) const noexcept
852 { return std::_Hash_impl::hash(__s.data(),
853 __s.length() * sizeof(char32_t)); }
854 };
855
856 template<>
857 struct __is_fast_hash<hash<u32string_view>> : std::false_type
858 { };
859
860 inline namespace literals
861 {
862 inline namespace string_view_literals
863 {
864#pragma GCC diagnostic push
865#pragma GCC diagnostic ignored "-Wliteral-suffix"
866 inline constexpr basic_string_view<char>
867 operator""sv(const char* __str, size_t __len) noexcept
868 { return basic_string_view<char>{__str, __len}; }
869
870 inline constexpr basic_string_view<wchar_t>
871 operator""sv(const wchar_t* __str, size_t __len) noexcept
872 { return basic_string_view<wchar_t>{__str, __len}; }
873
874#ifdef _GLIBCXX_USE_CHAR8_T
875 inline constexpr basic_string_view<char8_t>
876 operator""sv(const char8_t* __str, size_t __len) noexcept
877 { return basic_string_view<char8_t>{__str, __len}; }
878#endif
879
880 inline constexpr basic_string_view<char16_t>
881 operator""sv(const char16_t* __str, size_t __len) noexcept
882 { return basic_string_view<char16_t>{__str, __len}; }
883
884 inline constexpr basic_string_view<char32_t>
885 operator""sv(const char32_t* __str, size_t __len) noexcept
886 { return basic_string_view<char32_t>{__str, __len}; }
887
888#pragma GCC diagnostic pop
889 } // namespace string_literals
890 } // namespace literals
891
892#if __cpp_lib_concepts
893 namespace ranges
894 {
895 // Opt-in to borrowed_range concept
896 template<typename _CharT, typename _Traits>
897 inline constexpr bool
898 enable_borrowed_range<basic_string_view<_CharT, _Traits>> = true;
899
900 // Opt-in to view concept
901 template<typename _CharT, typename _Traits>
902 inline constexpr bool
903 enable_view<basic_string_view<_CharT, _Traits>> = true;
904 }
905#endif
906_GLIBCXX_END_NAMESPACE_VERSION
907} // namespace std
908
909#include <bits/string_view.tcc>
910
911#endif // __cplusplus <= 201402L
912
913#endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW