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