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/ranges_base.h>
45 #include <bits/ostream_insert.h>
46 #include <ext/numeric_traits.h>
47 
48 #if __cplusplus > 202002L
49 # include <bits/ranges_base.h>
50 #endif
51 
52 namespace std _GLIBCXX_VISIBILITY(default)
53 {
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 
56 # define __cpp_lib_string_view 201803L
57 #if __cplusplus > 201703L
58 # define __cpp_lib_constexpr_string_view 201811L
59 #endif
60 
61  // Helper for basic_string and basic_string_view members.
62  constexpr size_t
63  __sv_check(size_t __size, size_t __pos, const char* __s)
64  {
65  if (__pos > __size)
66  __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > __size "
67  "(which is %zu)"), __s, __pos, __size);
68  return __pos;
69  }
70 
71  // Helper for basic_string members.
72  // NB: __sv_limit doesn't check for a bad __pos value.
73  constexpr size_t
74  __sv_limit(size_t __size, size_t __pos, size_t __off) noexcept
75  {
76  const bool __testoff = __off < __size - __pos;
77  return __testoff ? __off : __size - __pos;
78  }
79 
80  /**
81  * @class basic_string_view <string_view>
82  * @brief A non-owning reference to a string.
83  *
84  * @ingroup strings
85  * @ingroup sequences
86  *
87  * @tparam _CharT Type of character
88  * @tparam _Traits Traits for character type, defaults to
89  * char_traits<_CharT>.
90  *
91  * A basic_string_view looks like this:
92  *
93  * @code
94  * _CharT* _M_str
95  * size_t _M_len
96  * @endcode
97  */
98  template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
100  {
101  static_assert(!is_array_v<_CharT>);
102  static_assert(is_trivial_v<_CharT> && is_standard_layout_v<_CharT>);
103  static_assert(is_same_v<_CharT, typename _Traits::char_type>);
104 
105  public:
106 
107  // types
108  using traits_type = _Traits;
109  using value_type = _CharT;
110  using pointer = value_type*;
111  using const_pointer = const value_type*;
112  using reference = value_type&;
113  using const_reference = const value_type&;
114  using const_iterator = const value_type*;
115  using iterator = const_iterator;
118  using size_type = size_t;
119  using difference_type = ptrdiff_t;
120  static constexpr size_type npos = size_type(-1);
121 
122  // [string.view.cons], construction and assignment
123 
124  constexpr
125  basic_string_view() noexcept
126  : _M_len{0}, _M_str{nullptr}
127  { }
128 
129  constexpr basic_string_view(const basic_string_view&) noexcept = default;
130 
131  __attribute__((__nonnull__)) constexpr
132  basic_string_view(const _CharT* __str) noexcept
133  : _M_len{traits_type::length(__str)},
134  _M_str{__str}
135  { }
136 
137  constexpr
138  basic_string_view(const _CharT* __str, size_type __len) noexcept
139  : _M_len{__len}, _M_str{__str}
140  { }
141 
142 #if __cplusplus >= 202002L && __cpp_lib_concepts
143  template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
144  requires same_as<iter_value_t<_It>, _CharT>
145  && (!convertible_to<_End, size_type>)
146  constexpr
147  basic_string_view(_It __first, _End __last)
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
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  { this->_M_len -= __n; }
279 
280  constexpr void
281  swap(basic_string_view& __sv) noexcept
282  {
283  auto __tmp = *this;
284  *this = __sv;
285  __sv = __tmp;
286  }
287 
288  // [string.view.ops], string operations:
289 
290  _GLIBCXX20_CONSTEXPR
291  size_type
292  copy(_CharT* __str, size_type __n, size_type __pos = 0) const
293  {
294  __glibcxx_requires_string_len(__str, __n);
295  __pos = std::__sv_check(size(), __pos, "basic_string_view::copy");
296  const size_type __rlen = std::min(__n, _M_len - __pos);
297  // _GLIBCXX_RESOLVE_LIB_DEFECTS
298  // 2777. basic_string_view::copy should use char_traits::copy
299  traits_type::copy(__str, data() + __pos, __rlen);
300  return __rlen;
301  }
302 
303  constexpr basic_string_view
304  substr(size_type __pos = 0, size_type __n = npos) const noexcept(false)
305  {
306  __pos = std::__sv_check(size(), __pos, "basic_string_view::substr");
307  const size_type __rlen = std::min(__n, _M_len - __pos);
308  return basic_string_view{_M_str + __pos, __rlen};
309  }
310 
311  constexpr int
312  compare(basic_string_view __str) const noexcept
313  {
314  const size_type __rlen = std::min(this->_M_len, __str._M_len);
315  int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
316  if (__ret == 0)
317  __ret = _S_compare(this->_M_len, __str._M_len);
318  return __ret;
319  }
320 
321  constexpr int
322  compare(size_type __pos1, size_type __n1, basic_string_view __str) const
323  { return this->substr(__pos1, __n1).compare(__str); }
324 
325  constexpr int
326  compare(size_type __pos1, size_type __n1,
327  basic_string_view __str, size_type __pos2, size_type __n2) const
328  {
329  return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
330  }
331 
332  __attribute__((__nonnull__)) constexpr int
333  compare(const _CharT* __str) const noexcept
334  { return this->compare(basic_string_view{__str}); }
335 
336  __attribute__((__nonnull__)) constexpr int
337  compare(size_type __pos1, size_type __n1, const _CharT* __str) const
338  { return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
339 
340  constexpr int
341  compare(size_type __pos1, size_type __n1,
342  const _CharT* __str, size_type __n2) const noexcept(false)
343  {
344  return this->substr(__pos1, __n1)
345  .compare(basic_string_view(__str, __n2));
346  }
347 
348 #if __cplusplus > 201703L
349 #define __cpp_lib_starts_ends_with 201711L
350  constexpr bool
351  starts_with(basic_string_view __x) const noexcept
352  { return this->substr(0, __x.size()) == __x; }
353 
354  constexpr bool
355  starts_with(_CharT __x) const noexcept
356  { return !this->empty() && traits_type::eq(this->front(), __x); }
357 
358  constexpr bool
359  starts_with(const _CharT* __x) const noexcept
360  { return this->starts_with(basic_string_view(__x)); }
361 
362  constexpr bool
363  ends_with(basic_string_view __x) const noexcept
364  {
365  return this->size() >= __x.size()
366  && this->compare(this->size() - __x.size(), npos, __x) == 0;
367  }
368 
369  constexpr bool
370  ends_with(_CharT __x) const noexcept
371  { return !this->empty() && traits_type::eq(this->back(), __x); }
372 
373  constexpr bool
374  ends_with(const _CharT* __x) const noexcept
375  { return this->ends_with(basic_string_view(__x)); }
376 #endif // C++20
377 
378 #if __cplusplus > 202002L
379 #define __cpp_lib_string_contains 202011L
380  constexpr bool
381  contains(basic_string_view __x) const noexcept
382  { return this->find(__x) != npos; }
383 
384  constexpr bool
385  contains(_CharT __x) const noexcept
386  { return this->find(__x) != npos; }
387 
388  constexpr bool
389  contains(const _CharT* __x) const noexcept
390  { return this->find(__x) != npos; }
391 #endif // C++23
392 
393  // [string.view.find], searching
394 
395  constexpr size_type
396  find(basic_string_view __str, size_type __pos = 0) const noexcept
397  { return this->find(__str._M_str, __pos, __str._M_len); }
398 
399  constexpr size_type
400  find(_CharT __c, size_type __pos = 0) const noexcept;
401 
402  constexpr size_type
403  find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
404 
405  __attribute__((__nonnull__)) constexpr size_type
406  find(const _CharT* __str, size_type __pos = 0) const noexcept
407  { return this->find(__str, __pos, traits_type::length(__str)); }
408 
409  constexpr size_type
410  rfind(basic_string_view __str, size_type __pos = npos) const noexcept
411  { return this->rfind(__str._M_str, __pos, __str._M_len); }
412 
413  constexpr size_type
414  rfind(_CharT __c, size_type __pos = npos) const noexcept;
415 
416  constexpr size_type
417  rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
418 
419  __attribute__((__nonnull__)) constexpr size_type
420  rfind(const _CharT* __str, size_type __pos = npos) const noexcept
421  { return this->rfind(__str, __pos, traits_type::length(__str)); }
422 
423  constexpr size_type
424  find_first_of(basic_string_view __str, size_type __pos = 0) const noexcept
425  { return this->find_first_of(__str._M_str, __pos, __str._M_len); }
426 
427  constexpr size_type
428  find_first_of(_CharT __c, size_type __pos = 0) const noexcept
429  { return this->find(__c, __pos); }
430 
431  constexpr size_type
432  find_first_of(const _CharT* __str, size_type __pos,
433  size_type __n) const noexcept;
434 
435  __attribute__((__nonnull__)) constexpr size_type
436  find_first_of(const _CharT* __str, size_type __pos = 0) const noexcept
437  { return this->find_first_of(__str, __pos, traits_type::length(__str)); }
438 
439  constexpr size_type
440  find_last_of(basic_string_view __str,
441  size_type __pos = npos) const noexcept
442  { return this->find_last_of(__str._M_str, __pos, __str._M_len); }
443 
444  constexpr size_type
445  find_last_of(_CharT __c, size_type __pos=npos) const noexcept
446  { return this->rfind(__c, __pos); }
447 
448  constexpr size_type
449  find_last_of(const _CharT* __str, size_type __pos,
450  size_type __n) const noexcept;
451 
452  __attribute__((__nonnull__)) constexpr size_type
453  find_last_of(const _CharT* __str, size_type __pos = npos) const noexcept
454  { return this->find_last_of(__str, __pos, traits_type::length(__str)); }
455 
456  constexpr size_type
457  find_first_not_of(basic_string_view __str,
458  size_type __pos = 0) const noexcept
459  { return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
460 
461  constexpr size_type
462  find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
463 
464  constexpr size_type
465  find_first_not_of(const _CharT* __str,
466  size_type __pos, size_type __n) const noexcept;
467 
468  __attribute__((__nonnull__)) constexpr size_type
469  find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
470  {
471  return this->find_first_not_of(__str, __pos,
472  traits_type::length(__str));
473  }
474 
475  constexpr size_type
476  find_last_not_of(basic_string_view __str,
477  size_type __pos = npos) const noexcept
478  { return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
479 
480  constexpr size_type
481  find_last_not_of(_CharT __c, size_type __pos = npos) const noexcept;
482 
483  constexpr size_type
484  find_last_not_of(const _CharT* __str,
485  size_type __pos, size_type __n) const noexcept;
486 
487  __attribute__((__nonnull__)) constexpr size_type
488  find_last_not_of(const _CharT* __str,
489  size_type __pos = npos) const noexcept
490  {
491  return this->find_last_not_of(__str, __pos,
492  traits_type::length(__str));
493  }
494 
495  private:
496 
497  static constexpr int
498  _S_compare(size_type __n1, size_type __n2) noexcept
499  {
500  using __limits = __gnu_cxx::__int_traits<int>;
501  const difference_type __diff = __n1 - __n2;
502  if (__diff > __limits::__max)
503  return __limits::__max;
504  if (__diff < __limits::__min)
505  return __limits::__min;
506  return static_cast<int>(__diff);
507  }
508 
509  size_t _M_len;
510  const _CharT* _M_str;
511  };
512 
513 #if __cplusplus > 201703L && __cpp_lib_concepts && __cpp_deduction_guides
514  template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
516 
517 #if __cplusplus > 202002L
518  template<ranges::contiguous_range _Range>
519  basic_string_view(_Range&&)
521 #endif
522 #endif
523 
524  // [string.view.comparison], non-member basic_string_view comparison function
525 
526  // Several of these functions use type_identity_t to create a non-deduced
527  // context, so that only one argument participates in template argument
528  // deduction and the other argument gets implicitly converted to the deduced
529  // type (see N3766).
530 
531  template<typename _CharT, typename _Traits>
532  constexpr bool
533  operator==(basic_string_view<_CharT, _Traits> __x,
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  __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
541  noexcept
542  { return __x.size() == __y.size() && __x.compare(__y) == 0; }
543 
544 #if __cpp_lib_three_way_comparison
545  template<typename _CharT, typename _Traits>
546  constexpr auto
547  operator<=>(basic_string_view<_CharT, _Traits> __x,
548  basic_string_view<_CharT, _Traits> __y) noexcept
549  -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
550  { return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
551 
552  template<typename _CharT, typename _Traits>
553  constexpr auto
554  operator<=>(basic_string_view<_CharT, _Traits> __x,
555  __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
556  noexcept
557  -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
558  { return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
559 #else
560  template<typename _CharT, typename _Traits>
561  constexpr bool
562  operator==(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
563  basic_string_view<_CharT, _Traits> __y) noexcept
564  { return __x.size() == __y.size() && __x.compare(__y) == 0; }
565 
566  template<typename _CharT, typename _Traits>
567  constexpr bool
568  operator!=(basic_string_view<_CharT, _Traits> __x,
569  basic_string_view<_CharT, _Traits> __y) noexcept
570  { return !(__x == __y); }
571 
572  template<typename _CharT, typename _Traits>
573  constexpr bool
574  operator!=(basic_string_view<_CharT, _Traits> __x,
575  __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
576  noexcept
577  { return !(__x == __y); }
578 
579  template<typename _CharT, typename _Traits>
580  constexpr bool
581  operator!=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
582  basic_string_view<_CharT, _Traits> __y) noexcept
583  { return !(__x == __y); }
584 
585  template<typename _CharT, typename _Traits>
586  constexpr bool
587  operator< (basic_string_view<_CharT, _Traits> __x,
588  basic_string_view<_CharT, _Traits> __y) noexcept
589  { return __x.compare(__y) < 0; }
590 
591  template<typename _CharT, typename _Traits>
592  constexpr bool
593  operator< (basic_string_view<_CharT, _Traits> __x,
594  __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
595  noexcept
596  { return __x.compare(__y) < 0; }
597 
598  template<typename _CharT, typename _Traits>
599  constexpr bool
600  operator< (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
601  basic_string_view<_CharT, _Traits> __y) noexcept
602  { return __x.compare(__y) < 0; }
603 
604  template<typename _CharT, typename _Traits>
605  constexpr bool
606  operator> (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  __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
614  noexcept
615  { return __x.compare(__y) > 0; }
616 
617  template<typename _CharT, typename _Traits>
618  constexpr bool
619  operator> (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
620  basic_string_view<_CharT, _Traits> __y) noexcept
621  { return __x.compare(__y) > 0; }
622 
623  template<typename _CharT, typename _Traits>
624  constexpr bool
625  operator<=(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  __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
633  noexcept
634  { return __x.compare(__y) <= 0; }
635 
636  template<typename _CharT, typename _Traits>
637  constexpr bool
638  operator<=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
639  basic_string_view<_CharT, _Traits> __y) noexcept
640  { return __x.compare(__y) <= 0; }
641 
642  template<typename _CharT, typename _Traits>
643  constexpr bool
644  operator>=(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  __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
652  noexcept
653  { return __x.compare(__y) >= 0; }
654 
655  template<typename _CharT, typename _Traits>
656  constexpr bool
657  operator>=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
658  basic_string_view<_CharT, _Traits> __y) noexcept
659  { return __x.compare(__y) >= 0; }
660 #endif // three-way comparison
661 
662  // [string.view.io], Inserters and extractors
663  template<typename _CharT, typename _Traits>
664  inline basic_ostream<_CharT, _Traits>&
665  operator<<(basic_ostream<_CharT, _Traits>& __os,
666  basic_string_view<_CharT,_Traits> __str)
667  { return __ostream_insert(__os, __str.data(), __str.size()); }
668 
669 
670  // basic_string_view typedef names
671 
672  using string_view = basic_string_view<char>;
673 #ifdef _GLIBCXX_USE_WCHAR_T
674  using wstring_view = basic_string_view<wchar_t>;
675 #endif
676 #ifdef _GLIBCXX_USE_CHAR8_T
677  using u8string_view = basic_string_view<char8_t>;
678 #endif
679  using u16string_view = basic_string_view<char16_t>;
680  using u32string_view = basic_string_view<char32_t>;
681 
682  // [string.view.hash], hash support:
683 
684  template<typename _Tp>
685  struct hash;
686 
687  template<>
688  struct hash<string_view>
689  : public __hash_base<size_t, string_view>
690  {
691  size_t
692  operator()(const string_view& __str) const noexcept
693  { return std::_Hash_impl::hash(__str.data(), __str.length()); }
694  };
695 
696  template<>
697  struct __is_fast_hash<hash<string_view>> : std::false_type
698  { };
699 
700 #ifdef _GLIBCXX_USE_WCHAR_T
701  template<>
702  struct hash<wstring_view>
703  : public __hash_base<size_t, wstring_view>
704  {
705  size_t
706  operator()(const wstring_view& __s) const noexcept
707  { return std::_Hash_impl::hash(__s.data(),
708  __s.length() * sizeof(wchar_t)); }
709  };
710 
711  template<>
712  struct __is_fast_hash<hash<wstring_view>> : std::false_type
713  { };
714 #endif
715 
716 #ifdef _GLIBCXX_USE_CHAR8_T
717  template<>
718  struct hash<u8string_view>
719  : public __hash_base<size_t, u8string_view>
720  {
721  size_t
722  operator()(const u8string_view& __str) const noexcept
723  { return std::_Hash_impl::hash(__str.data(), __str.length()); }
724  };
725 
726  template<>
727  struct __is_fast_hash<hash<u8string_view>> : std::false_type
728  { };
729 #endif
730 
731  template<>
732  struct hash<u16string_view>
733  : public __hash_base<size_t, u16string_view>
734  {
735  size_t
736  operator()(const u16string_view& __s) const noexcept
737  { return std::_Hash_impl::hash(__s.data(),
738  __s.length() * sizeof(char16_t)); }
739  };
740 
741  template<>
742  struct __is_fast_hash<hash<u16string_view>> : std::false_type
743  { };
744 
745  template<>
746  struct hash<u32string_view>
747  : public __hash_base<size_t, u32string_view>
748  {
749  size_t
750  operator()(const u32string_view& __s) const noexcept
751  { return std::_Hash_impl::hash(__s.data(),
752  __s.length() * sizeof(char32_t)); }
753  };
754 
755  template<>
756  struct __is_fast_hash<hash<u32string_view>> : std::false_type
757  { };
758 
759  inline namespace literals
760  {
761  inline namespace string_view_literals
762  {
763 #pragma GCC diagnostic push
764 #pragma GCC diagnostic ignored "-Wliteral-suffix"
765  inline constexpr basic_string_view<char>
766  operator""sv(const char* __str, size_t __len) noexcept
767  { return basic_string_view<char>{__str, __len}; }
768 
769 #ifdef _GLIBCXX_USE_WCHAR_T
770  inline constexpr basic_string_view<wchar_t>
771  operator""sv(const wchar_t* __str, size_t __len) noexcept
772  { return basic_string_view<wchar_t>{__str, __len}; }
773 #endif
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 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
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
Definition: range_access.h:245
constexpr auto data(_Container &__cont) noexcept(noexcept(__cont.data())) -> decltype(__cont.data())
Return the data pointer of a container.
Definition: range_access.h:290
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
A non-owning reference to a string.
Definition: string_view:100
integral_constant
Definition: type_traits:66