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