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/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 
53 namespace 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>>
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;
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
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 #endif // C++23
171 #endif // C++20
172 
173  basic_string_view(nullptr_t) = delete;
174 
175  constexpr basic_string_view&
176  operator=(const basic_string_view&) noexcept = default;
177 
178  // [string.view.iterators], iterator support
179 
180  constexpr const_iterator
181  begin() const noexcept
182  { return this->_M_str; }
183 
184  constexpr const_iterator
185  end() const noexcept
186  { return this->_M_str + this->_M_len; }
187 
188  constexpr const_iterator
189  cbegin() const noexcept
190  { return this->_M_str; }
191 
192  constexpr const_iterator
193  cend() const noexcept
194  { return this->_M_str + this->_M_len; }
195 
196  constexpr const_reverse_iterator
197  rbegin() const noexcept
198  { return const_reverse_iterator(this->end()); }
199 
200  constexpr const_reverse_iterator
201  rend() const noexcept
202  { return const_reverse_iterator(this->begin()); }
203 
204  constexpr const_reverse_iterator
205  crbegin() const noexcept
206  { return const_reverse_iterator(this->end()); }
207 
208  constexpr const_reverse_iterator
209  crend() const noexcept
210  { return const_reverse_iterator(this->begin()); }
211 
212  // [string.view.capacity], capacity
213 
214  constexpr size_type
215  size() const noexcept
216  { return this->_M_len; }
217 
218  constexpr size_type
219  length() const noexcept
220  { return _M_len; }
221 
222  constexpr size_type
223  max_size() const noexcept
224  {
225  return (npos - sizeof(size_type) - sizeof(void*))
226  / sizeof(value_type) / 4;
227  }
228 
229  [[nodiscard]] constexpr bool
230  empty() const noexcept
231  { return this->_M_len == 0; }
232 
233  // [string.view.access], element access
234 
235  constexpr const_reference
236  operator[](size_type __pos) const noexcept
237  {
238  __glibcxx_assert(__pos < this->_M_len);
239  return *(this->_M_str + __pos);
240  }
241 
242  constexpr const_reference
243  at(size_type __pos) const
244  {
245  if (__pos >= _M_len)
246  __throw_out_of_range_fmt(__N("basic_string_view::at: __pos "
247  "(which is %zu) >= this->size() "
248  "(which is %zu)"), __pos, this->size());
249  return *(this->_M_str + __pos);
250  }
251 
252  constexpr const_reference
253  front() const noexcept
254  {
255  __glibcxx_assert(this->_M_len > 0);
256  return *this->_M_str;
257  }
258 
259  constexpr const_reference
260  back() const noexcept
261  {
262  __glibcxx_assert(this->_M_len > 0);
263  return *(this->_M_str + this->_M_len - 1);
264  }
265 
266  constexpr const_pointer
267  data() const noexcept
268  { return this->_M_str; }
269 
270  // [string.view.modifiers], modifiers:
271 
272  constexpr void
273  remove_prefix(size_type __n) noexcept
274  {
275  __glibcxx_assert(this->_M_len >= __n);
276  this->_M_str += __n;
277  this->_M_len -= __n;
278  }
279 
280  constexpr void
281  remove_suffix(size_type __n) noexcept
282  { this->_M_len -= __n; }
283 
284  constexpr void
285  swap(basic_string_view& __sv) noexcept
286  {
287  auto __tmp = *this;
288  *this = __sv;
289  __sv = __tmp;
290  }
291 
292  // [string.view.ops], string operations:
293 
294  _GLIBCXX20_CONSTEXPR
295  size_type
296  copy(_CharT* __str, size_type __n, size_type __pos = 0) const
297  {
298  __glibcxx_requires_string_len(__str, __n);
299  __pos = std::__sv_check(size(), __pos, "basic_string_view::copy");
300  const size_type __rlen = std::min(__n, _M_len - __pos);
301  // _GLIBCXX_RESOLVE_LIB_DEFECTS
302  // 2777. basic_string_view::copy should use char_traits::copy
303  traits_type::copy(__str, data() + __pos, __rlen);
304  return __rlen;
305  }
306 
307  constexpr basic_string_view
308  substr(size_type __pos = 0, size_type __n = npos) const noexcept(false)
309  {
310  __pos = std::__sv_check(size(), __pos, "basic_string_view::substr");
311  const size_type __rlen = std::min(__n, _M_len - __pos);
312  return basic_string_view{_M_str + __pos, __rlen};
313  }
314 
315  constexpr int
316  compare(basic_string_view __str) const noexcept
317  {
318  const size_type __rlen = std::min(this->_M_len, __str._M_len);
319  int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
320  if (__ret == 0)
321  __ret = _S_compare(this->_M_len, __str._M_len);
322  return __ret;
323  }
324 
325  constexpr int
326  compare(size_type __pos1, size_type __n1, basic_string_view __str) const
327  { return this->substr(__pos1, __n1).compare(__str); }
328 
329  constexpr int
330  compare(size_type __pos1, size_type __n1,
331  basic_string_view __str, size_type __pos2, size_type __n2) const
332  {
333  return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
334  }
335 
336  __attribute__((__nonnull__)) constexpr int
337  compare(const _CharT* __str) const noexcept
338  { return this->compare(basic_string_view{__str}); }
339 
340  __attribute__((__nonnull__)) constexpr int
341  compare(size_type __pos1, size_type __n1, const _CharT* __str) const
342  { return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
343 
344  constexpr int
345  compare(size_type __pos1, size_type __n1,
346  const _CharT* __str, size_type __n2) const noexcept(false)
347  {
348  return this->substr(__pos1, __n1)
349  .compare(basic_string_view(__str, __n2));
350  }
351 
352 #if __cplusplus > 201703L
353 #define __cpp_lib_starts_ends_with 201711L
354  constexpr bool
355  starts_with(basic_string_view __x) const noexcept
356  { return this->substr(0, __x.size()) == __x; }
357 
358  constexpr bool
359  starts_with(_CharT __x) const noexcept
360  { return !this->empty() && traits_type::eq(this->front(), __x); }
361 
362  constexpr bool
363  starts_with(const _CharT* __x) const noexcept
364  { return this->starts_with(basic_string_view(__x)); }
365 
366  constexpr bool
367  ends_with(basic_string_view __x) const noexcept
368  {
369  const auto __len = this->size();
370  const auto __xlen = __x.size();
371  return __len >= __xlen
372  && traits_type::compare(end() - __xlen, __x.data(), __xlen) == 0;
373  }
374 
375  constexpr bool
376  ends_with(_CharT __x) const noexcept
377  { return !this->empty() && traits_type::eq(this->back(), __x); }
378 
379  constexpr bool
380  ends_with(const _CharT* __x) const noexcept
381  { return this->ends_with(basic_string_view(__x)); }
382 #endif // C++20
383 
384 #if __cplusplus > 202002L
385 #define __cpp_lib_string_contains 202011L
386  constexpr bool
387  contains(basic_string_view __x) const noexcept
388  { return this->find(__x) != npos; }
389 
390  constexpr bool
391  contains(_CharT __x) const noexcept
392  { return this->find(__x) != npos; }
393 
394  constexpr bool
395  contains(const _CharT* __x) const noexcept
396  { return this->find(__x) != npos; }
397 #endif // C++23
398 
399  // [string.view.find], searching
400 
401  constexpr size_type
402  find(basic_string_view __str, size_type __pos = 0) const noexcept
403  { return this->find(__str._M_str, __pos, __str._M_len); }
404 
405  constexpr size_type
406  find(_CharT __c, size_type __pos = 0) const noexcept;
407 
408  constexpr size_type
409  find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
410 
411  __attribute__((__nonnull__)) constexpr size_type
412  find(const _CharT* __str, size_type __pos = 0) const noexcept
413  { return this->find(__str, __pos, traits_type::length(__str)); }
414 
415  constexpr size_type
416  rfind(basic_string_view __str, size_type __pos = npos) const noexcept
417  { return this->rfind(__str._M_str, __pos, __str._M_len); }
418 
419  constexpr size_type
420  rfind(_CharT __c, size_type __pos = npos) const noexcept;
421 
422  constexpr size_type
423  rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
424 
425  __attribute__((__nonnull__)) constexpr size_type
426  rfind(const _CharT* __str, size_type __pos = npos) const noexcept
427  { return this->rfind(__str, __pos, traits_type::length(__str)); }
428 
429  constexpr size_type
430  find_first_of(basic_string_view __str, size_type __pos = 0) const noexcept
431  { return this->find_first_of(__str._M_str, __pos, __str._M_len); }
432 
433  constexpr size_type
434  find_first_of(_CharT __c, size_type __pos = 0) const noexcept
435  { return this->find(__c, __pos); }
436 
437  constexpr size_type
438  find_first_of(const _CharT* __str, size_type __pos,
439  size_type __n) const noexcept;
440 
441  __attribute__((__nonnull__)) constexpr size_type
442  find_first_of(const _CharT* __str, size_type __pos = 0) const noexcept
443  { return this->find_first_of(__str, __pos, traits_type::length(__str)); }
444 
445  constexpr size_type
446  find_last_of(basic_string_view __str,
447  size_type __pos = npos) const noexcept
448  { return this->find_last_of(__str._M_str, __pos, __str._M_len); }
449 
450  constexpr size_type
451  find_last_of(_CharT __c, size_type __pos=npos) const noexcept
452  { return this->rfind(__c, __pos); }
453 
454  constexpr size_type
455  find_last_of(const _CharT* __str, size_type __pos,
456  size_type __n) const noexcept;
457 
458  __attribute__((__nonnull__)) constexpr size_type
459  find_last_of(const _CharT* __str, size_type __pos = npos) const noexcept
460  { return this->find_last_of(__str, __pos, traits_type::length(__str)); }
461 
462  constexpr size_type
463  find_first_not_of(basic_string_view __str,
464  size_type __pos = 0) const noexcept
465  { return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
466 
467  constexpr size_type
468  find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
469 
470  constexpr size_type
471  find_first_not_of(const _CharT* __str,
472  size_type __pos, size_type __n) const noexcept;
473 
474  __attribute__((__nonnull__)) constexpr size_type
475  find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
476  {
477  return this->find_first_not_of(__str, __pos,
478  traits_type::length(__str));
479  }
480 
481  constexpr size_type
482  find_last_not_of(basic_string_view __str,
483  size_type __pos = npos) const noexcept
484  { return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
485 
486  constexpr size_type
487  find_last_not_of(_CharT __c, size_type __pos = npos) const noexcept;
488 
489  constexpr size_type
490  find_last_not_of(const _CharT* __str,
491  size_type __pos, size_type __n) const noexcept;
492 
493  __attribute__((__nonnull__)) constexpr size_type
494  find_last_not_of(const _CharT* __str,
495  size_type __pos = npos) const noexcept
496  {
497  return this->find_last_not_of(__str, __pos,
498  traits_type::length(__str));
499  }
500 
501  private:
502 
503  static constexpr int
504  _S_compare(size_type __n1, size_type __n2) noexcept
505  {
506  using __limits = __gnu_cxx::__int_traits<int>;
507  const difference_type __diff = __n1 - __n2;
508  if (__diff > __limits::__max)
509  return __limits::__max;
510  if (__diff < __limits::__min)
511  return __limits::__min;
512  return static_cast<int>(__diff);
513  }
514 
515  size_t _M_len;
516  const _CharT* _M_str;
517  };
518 
519 #if __cplusplus > 201703L && __cpp_lib_concepts && __cpp_deduction_guides
520  template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
522 
523 #if __cplusplus > 202002L
524  template<ranges::contiguous_range _Range>
525  basic_string_view(_Range&&)
527 #endif
528 #endif
529 
530  // [string.view.comparison], non-member basic_string_view comparison function
531 
532  // Several of these functions use type_identity_t to create a non-deduced
533  // context, so that only one argument participates in template argument
534  // deduction and the other argument gets implicitly converted to the deduced
535  // type (see N3766).
536 
537  template<typename _CharT, typename _Traits>
538  constexpr bool
539  operator==(basic_string_view<_CharT, _Traits> __x,
541  { return __x.size() == __y.size() && __x.compare(__y) == 0; }
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.size() == __y.size() && __x.compare(__y) == 0; }
549 
550 #if __cpp_lib_three_way_comparison
551  template<typename _CharT, typename _Traits>
552  constexpr auto
553  operator<=>(basic_string_view<_CharT, _Traits> __x,
554  basic_string_view<_CharT, _Traits> __y) noexcept
555  -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
556  { return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
557 
558  template<typename _CharT, typename _Traits>
559  constexpr auto
560  operator<=>(basic_string_view<_CharT, _Traits> __x,
561  __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
562  noexcept
563  -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
564  { return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
565 #else
566  template<typename _CharT, typename _Traits>
567  constexpr bool
568  operator==(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
569  basic_string_view<_CharT, _Traits> __y) noexcept
570  { return __x.size() == __y.size() && __x.compare(__y) == 0; }
571 
572  template<typename _CharT, typename _Traits>
573  constexpr bool
574  operator!=(basic_string_view<_CharT, _Traits> __x,
575  basic_string_view<_CharT, _Traits> __y) noexcept
576  { return !(__x == __y); }
577 
578  template<typename _CharT, typename _Traits>
579  constexpr bool
580  operator!=(basic_string_view<_CharT, _Traits> __x,
581  __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
582  noexcept
583  { return !(__x == __y); }
584 
585  template<typename _CharT, typename _Traits>
586  constexpr bool
587  operator!=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
588  basic_string_view<_CharT, _Traits> __y) noexcept
589  { return !(__x == __y); }
590 
591  template<typename _CharT, typename _Traits>
592  constexpr bool
593  operator< (basic_string_view<_CharT, _Traits> __x,
594  basic_string_view<_CharT, _Traits> __y) noexcept
595  { return __x.compare(__y) < 0; }
596 
597  template<typename _CharT, typename _Traits>
598  constexpr bool
599  operator< (basic_string_view<_CharT, _Traits> __x,
600  __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
601  noexcept
602  { return __x.compare(__y) < 0; }
603 
604  template<typename _CharT, typename _Traits>
605  constexpr bool
606  operator< (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
607  basic_string_view<_CharT, _Traits> __y) noexcept
608  { return __x.compare(__y) < 0; }
609 
610  template<typename _CharT, typename _Traits>
611  constexpr bool
612  operator> (basic_string_view<_CharT, _Traits> __x,
613  basic_string_view<_CharT, _Traits> __y) noexcept
614  { return __x.compare(__y) > 0; }
615 
616  template<typename _CharT, typename _Traits>
617  constexpr bool
618  operator> (basic_string_view<_CharT, _Traits> __x,
619  __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
620  noexcept
621  { return __x.compare(__y) > 0; }
622 
623  template<typename _CharT, typename _Traits>
624  constexpr bool
625  operator> (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
626  basic_string_view<_CharT, _Traits> __y) noexcept
627  { return __x.compare(__y) > 0; }
628 
629  template<typename _CharT, typename _Traits>
630  constexpr bool
631  operator<=(basic_string_view<_CharT, _Traits> __x,
632  basic_string_view<_CharT, _Traits> __y) noexcept
633  { return __x.compare(__y) <= 0; }
634 
635  template<typename _CharT, typename _Traits>
636  constexpr bool
637  operator<=(basic_string_view<_CharT, _Traits> __x,
638  __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
639  noexcept
640  { return __x.compare(__y) <= 0; }
641 
642  template<typename _CharT, typename _Traits>
643  constexpr bool
644  operator<=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
645  basic_string_view<_CharT, _Traits> __y) noexcept
646  { return __x.compare(__y) <= 0; }
647 
648  template<typename _CharT, typename _Traits>
649  constexpr bool
650  operator>=(basic_string_view<_CharT, _Traits> __x,
651  basic_string_view<_CharT, _Traits> __y) noexcept
652  { return __x.compare(__y) >= 0; }
653 
654  template<typename _CharT, typename _Traits>
655  constexpr bool
656  operator>=(basic_string_view<_CharT, _Traits> __x,
657  __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
658  noexcept
659  { return __x.compare(__y) >= 0; }
660 
661  template<typename _CharT, typename _Traits>
662  constexpr bool
663  operator>=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
664  basic_string_view<_CharT, _Traits> __y) noexcept
665  { return __x.compare(__y) >= 0; }
666 #endif // three-way comparison
667 
668  // [string.view.io], Inserters and extractors
669  template<typename _CharT, typename _Traits>
670  inline basic_ostream<_CharT, _Traits>&
671  operator<<(basic_ostream<_CharT, _Traits>& __os,
672  basic_string_view<_CharT,_Traits> __str)
673  { return __ostream_insert(__os, __str.data(), __str.size()); }
674 
675 
676  // basic_string_view typedef names
677 
678  using string_view = basic_string_view<char>;
679  using wstring_view = basic_string_view<wchar_t>;
680 #ifdef _GLIBCXX_USE_CHAR8_T
681  using u8string_view = basic_string_view<char8_t>;
682 #endif
683  using u16string_view = basic_string_view<char16_t>;
684  using u32string_view = basic_string_view<char32_t>;
685 
686  // [string.view.hash], hash support:
687 
688  template<typename _Tp>
689  struct hash;
690 
691  template<>
692  struct hash<string_view>
693  : public __hash_base<size_t, string_view>
694  {
695  size_t
696  operator()(const string_view& __str) const noexcept
697  { return std::_Hash_impl::hash(__str.data(), __str.length()); }
698  };
699 
700  template<>
701  struct __is_fast_hash<hash<string_view>> : std::false_type
702  { };
703 
704  template<>
705  struct hash<wstring_view>
706  : public __hash_base<size_t, wstring_view>
707  {
708  size_t
709  operator()(const wstring_view& __s) const noexcept
710  { return std::_Hash_impl::hash(__s.data(),
711  __s.length() * sizeof(wchar_t)); }
712  };
713 
714  template<>
715  struct __is_fast_hash<hash<wstring_view>> : std::false_type
716  { };
717 
718 #ifdef _GLIBCXX_USE_CHAR8_T
719  template<>
720  struct hash<u8string_view>
721  : public __hash_base<size_t, u8string_view>
722  {
723  size_t
724  operator()(const u8string_view& __str) const noexcept
725  { return std::_Hash_impl::hash(__str.data(), __str.length()); }
726  };
727 
728  template<>
729  struct __is_fast_hash<hash<u8string_view>> : std::false_type
730  { };
731 #endif
732 
733  template<>
734  struct hash<u16string_view>
735  : public __hash_base<size_t, u16string_view>
736  {
737  size_t
738  operator()(const u16string_view& __s) const noexcept
739  { return std::_Hash_impl::hash(__s.data(),
740  __s.length() * sizeof(char16_t)); }
741  };
742 
743  template<>
744  struct __is_fast_hash<hash<u16string_view>> : std::false_type
745  { };
746 
747  template<>
748  struct hash<u32string_view>
749  : public __hash_base<size_t, u32string_view>
750  {
751  size_t
752  operator()(const u32string_view& __s) const noexcept
753  { return std::_Hash_impl::hash(__s.data(),
754  __s.length() * sizeof(char32_t)); }
755  };
756 
757  template<>
758  struct __is_fast_hash<hash<u32string_view>> : std::false_type
759  { };
760 
761  inline namespace literals
762  {
763  inline namespace string_view_literals
764  {
765 #pragma GCC diagnostic push
766 #pragma GCC diagnostic ignored "-Wliteral-suffix"
767  inline constexpr basic_string_view<char>
768  operator""sv(const char* __str, size_t __len) noexcept
769  { return basic_string_view<char>{__str, __len}; }
770 
771  inline constexpr basic_string_view<wchar_t>
772  operator""sv(const wchar_t* __str, size_t __len) noexcept
773  { return basic_string_view<wchar_t>{__str, __len}; }
774 
775 #ifdef _GLIBCXX_USE_CHAR8_T
776  inline constexpr basic_string_view<char8_t>
777  operator""sv(const char8_t* __str, size_t __len) noexcept
778  { return basic_string_view<char8_t>{__str, __len}; }
779 #endif
780 
781  inline constexpr basic_string_view<char16_t>
782  operator""sv(const char16_t* __str, size_t __len) noexcept
783  { return basic_string_view<char16_t>{__str, __len}; }
784 
785  inline constexpr basic_string_view<char32_t>
786  operator""sv(const char32_t* __str, size_t __len) noexcept
787  { return basic_string_view<char32_t>{__str, __len}; }
788 
789 #pragma GCC diagnostic pop
790  } // namespace string_literals
791  } // namespace literals
792 
793 #if __cpp_lib_concepts
794  namespace ranges
795  {
796  // Opt-in to borrowed_range concept
797  template<typename _CharT, typename _Traits>
798  inline constexpr bool
799  enable_borrowed_range<basic_string_view<_CharT, _Traits>> = true;
800 
801  // Opt-in to view concept
802  template<typename _CharT, typename _Traits>
803  inline constexpr bool
804  enable_view<basic_string_view<_CharT, _Traits>> = true;
805  }
806 #endif
807 _GLIBCXX_END_NAMESPACE_VERSION
808 } // namespace std
809 
810 #include <bits/string_view.tcc>
811 
812 #endif // __cplusplus <= 201402L
813 
814 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW
constexpr _Tp * to_address(_Tp *__ptr) noexcept
Obtain address referenced by a pointer to an object.
Definition: ptr_traits.h:263
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:230
ISO C++ entities toplevel namespace is std.
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition: bitset:1540
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
A non-owning reference to a string.
Definition: string_view:101
integral_constant
Definition: type_traits:63