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