33#ifndef _GLIBCXX_STRING_VIEW
34#define _GLIBCXX_STRING_VIEW 1
36#pragma GCC system_header
38#if __cplusplus >= 201703L
47#if __cplusplus >= 202002L
56namespace std _GLIBCXX_VISIBILITY(default)
58_GLIBCXX_BEGIN_NAMESPACE_VERSION
61# define __cpp_lib_string_view 201803L
64#if __cplusplus > 201703L
65# define __cpp_lib_constexpr_string_view 201811L
70 __sv_check(
size_t __size,
size_t __pos,
const char* __s)
73 __throw_out_of_range_fmt(__N(
"%s: __pos (which is %zu) > __size "
74 "(which is %zu)"), __s, __pos, __size);
81 __sv_limit(
size_t __size,
size_t __pos,
size_t __off)
noexcept
83 const bool __testoff = __off < __size - __pos;
84 return __testoff ? __off : __size - __pos;
105 template<
typename _CharT,
typename _Traits = std::
char_traits<_CharT>>
108 static_assert(!is_array_v<_CharT>);
109 static_assert(is_trivial_v<_CharT> && is_standard_layout_v<_CharT>);
110 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
115 using traits_type = _Traits;
116 using value_type = _CharT;
117 using pointer = value_type*;
118 using const_pointer =
const value_type*;
119 using reference = value_type&;
120 using const_reference =
const value_type&;
121 using const_iterator =
const value_type*;
122 using iterator = const_iterator;
125 using size_type = size_t;
126 using difference_type = ptrdiff_t;
127 static constexpr size_type npos = size_type(-1);
133 : _M_len{0}, _M_str{
nullptr}
138 [[__gnu__::__nonnull__]]
141 : _M_len{traits_type::length(__str)},
147 : _M_len{__len}, _M_str{__str}
150#if __cplusplus >= 202002L && __cpp_lib_concepts
151 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
156 noexcept(
noexcept(__last - __first))
160#if __cplusplus > 202002L
161 template<
typename _Range,
typename _DRange = remove_cvref_t<_Range>>
162 requires (!is_same_v<_DRange, basic_string_view>)
165 && is_same_v<ranges::range_value_t<_Range>, _CharT>
166 && (!is_convertible_v<_Range, const _CharT*>)
167 && (!
requires (_DRange& __d) {
168 __d.operator ::std::basic_string_view<_CharT, _Traits>();
170 && (!
requires {
typename _DRange::traits_type; }
171 || is_same_v<typename _DRange::traits_type, _Traits>)
174 noexcept(
noexcept(ranges::size(__r)) &&
noexcept(ranges::data(__r)))
175 : _M_len(ranges::size(__r)), _M_str(ranges::data(__r))
188 constexpr const_iterator
189 begin()
const noexcept
190 {
return this->_M_str; }
193 constexpr const_iterator
195 {
return this->_M_str + this->_M_len; }
198 constexpr const_iterator
199 cbegin()
const noexcept
200 {
return this->_M_str; }
203 constexpr const_iterator
204 cend()
const noexcept
205 {
return this->_M_str + this->_M_len; }
209 rbegin()
const noexcept
214 rend()
const noexcept
219 crbegin()
const noexcept
224 crend()
const noexcept
231 size()
const noexcept
232 {
return this->_M_len; }
236 length()
const noexcept
241 max_size()
const noexcept
243 return (npos -
sizeof(size_type) -
sizeof(
void*))
244 /
sizeof(value_type) / 4;
249 empty()
const noexcept
250 {
return this->_M_len == 0; }
255 constexpr const_reference
256 operator[](size_type __pos)
const noexcept
258 __glibcxx_assert(__pos < this->_M_len);
259 return *(this->_M_str + __pos);
263 constexpr const_reference
264 at(size_type __pos)
const
267 __throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
268 "(which is %zu) >= this->size() "
269 "(which is %zu)"), __pos, this->size());
270 return *(this->_M_str + __pos);
274 constexpr const_reference
275 front()
const noexcept
277 __glibcxx_assert(this->_M_len > 0);
278 return *this->_M_str;
282 constexpr const_reference
283 back()
const noexcept
285 __glibcxx_assert(this->_M_len > 0);
286 return *(this->_M_str + this->_M_len - 1);
290 constexpr const_pointer
291 data()
const noexcept
292 {
return this->_M_str; }
297 remove_prefix(size_type __n)
noexcept
299 __glibcxx_assert(this->_M_len >= __n);
305 remove_suffix(size_type __n)
noexcept
307 __glibcxx_assert(this->_M_len >= __n);
323 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
325 __glibcxx_requires_string_len(__str, __n);
326 __pos = std::__sv_check(size(), __pos,
"basic_string_view::copy");
327 const size_type __rlen = std::min<size_t>(__n, _M_len - __pos);
330 traits_type::copy(__str, data() + __pos, __rlen);
336 substr(size_type __pos = 0, size_type __n = npos)
const noexcept(
false)
338 __pos = std::__sv_check(size(), __pos,
"basic_string_view::substr");
339 const size_type __rlen = std::min<size_t>(__n, _M_len - __pos);
347 const size_type __rlen =
std::min(this->_M_len, __str._M_len);
348 int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
350 __ret = _S_compare(this->_M_len, __str._M_len);
357 {
return this->substr(__pos1, __n1).compare(__str); }
361 compare(size_type __pos1, size_type __n1,
364 return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
367 [[nodiscard, __gnu__::__nonnull__]]
369 compare(
const _CharT* __str)
const noexcept
372 [[nodiscard, __gnu__::__nonnull__]]
374 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
379 compare(size_type __pos1, size_type __n1,
380 const _CharT* __str, size_type __n2)
const noexcept(
false)
382 return this->substr(__pos1, __n1)
386#if __cplusplus > 201703L
387#define __cpp_lib_starts_ends_with 201711L
391 {
return this->substr(0, __x.size()) == __x; }
395 starts_with(_CharT __x)
const noexcept
396 {
return !this->empty() && traits_type::eq(this->front(), __x); }
398 [[nodiscard, __gnu__::__nonnull__]]
400 starts_with(
const _CharT* __x)
const noexcept
407 const auto __len = this->size();
408 const auto __xlen = __x.size();
409 return __len >= __xlen
410 && traits_type::compare(end() - __xlen, __x.data(), __xlen) == 0;
415 ends_with(_CharT __x)
const noexcept
416 {
return !this->empty() && traits_type::eq(this->back(), __x); }
418 [[nodiscard, __gnu__::__nonnull__]]
420 ends_with(
const _CharT* __x)
const noexcept
424#if __cplusplus > 202002L
428# define __cpp_lib_string_contains 202011L
433 {
return this->find(__x) != npos; }
437 contains(_CharT __x)
const noexcept
438 {
return this->find(__x) != npos; }
440 [[nodiscard, __gnu__::__nonnull__]]
442 contains(
const _CharT* __x)
const noexcept
443 {
return this->find(__x) != npos; }
451 {
return this->find(__str._M_str, __pos, __str._M_len); }
455 find(_CharT __c, size_type __pos = 0)
const noexcept;
459 find(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
461 [[nodiscard, __gnu__::__nonnull__]]
463 find(
const _CharT* __str, size_type __pos = 0)
const noexcept
464 {
return this->find(__str, __pos, traits_type::length(__str)); }
469 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
473 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
477 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
479 [[nodiscard, __gnu__::__nonnull__]]
481 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
482 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
487 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
491 find_first_of(_CharT __c, size_type __pos = 0)
const noexcept
492 {
return this->find(__c, __pos); }
496 find_first_of(
const _CharT* __str, size_type __pos,
497 size_type __n)
const noexcept;
499 [[nodiscard, __gnu__::__nonnull__]]
501 find_first_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
502 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
507 size_type __pos = npos)
const noexcept
508 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
512 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
513 {
return this->rfind(__c, __pos); }
517 find_last_of(
const _CharT* __str, size_type __pos,
518 size_type __n)
const noexcept;
520 [[nodiscard, __gnu__::__nonnull__]]
522 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
523 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
528 size_type __pos = 0)
const noexcept
529 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
533 find_first_not_of(_CharT __c, size_type __pos = 0)
const noexcept;
537 find_first_not_of(
const _CharT* __str,
538 size_type __pos, size_type __n)
const noexcept;
540 [[nodiscard, __gnu__::__nonnull__]]
542 find_first_not_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
544 return this->find_first_not_of(__str, __pos,
545 traits_type::length(__str));
551 size_type __pos = npos)
const noexcept
552 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
556 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
560 find_last_not_of(
const _CharT* __str,
561 size_type __pos, size_type __n)
const noexcept;
563 [[nodiscard, __gnu__::__nonnull__]]
565 find_last_not_of(
const _CharT* __str,
566 size_type __pos = npos)
const noexcept
568 return this->find_last_not_of(__str, __pos,
569 traits_type::length(__str));
575 _S_compare(size_type __n1, size_type __n2)
noexcept
578 const difference_type __diff = __n1 - __n2;
579 if (__diff > __limits::__max)
580 return __limits::__max;
581 if (__diff < __limits::__min)
582 return __limits::__min;
583 return static_cast<int>(__diff);
587 const _CharT* _M_str;
590#if __cplusplus > 201703L && __cpp_lib_concepts && __cpp_deduction_guides
591 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
594#if __cplusplus > 202002L
595 template<ranges::contiguous_range _Range>
608 template<
typename _CharT,
typename _Traits>
613 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
615 template<
typename _CharT,
typename _Traits>
618 operator==(basic_string_view<_CharT, _Traits> __x,
619 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
621 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
623#if __cpp_lib_three_way_comparison
624 template<
typename _CharT,
typename _Traits>
627 operator<=>(basic_string_view<_CharT, _Traits> __x,
628 basic_string_view<_CharT, _Traits> __y)
noexcept
629 ->
decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
630 {
return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
632 template<
typename _CharT,
typename _Traits>
635 operator<=>(basic_string_view<_CharT, _Traits> __x,
636 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
638 ->
decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
639 {
return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
641 template<
typename _CharT,
typename _Traits>
644 operator==(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
645 basic_string_view<_CharT, _Traits> __y)
noexcept
646 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
648 template<
typename _CharT,
typename _Traits>
651 operator!=(basic_string_view<_CharT, _Traits> __x,
652 basic_string_view<_CharT, _Traits> __y)
noexcept
653 {
return !(__x == __y); }
655 template<
typename _CharT,
typename _Traits>
658 operator!=(basic_string_view<_CharT, _Traits> __x,
659 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
661 {
return !(__x == __y); }
663 template<
typename _CharT,
typename _Traits>
666 operator!=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
667 basic_string_view<_CharT, _Traits> __y)
noexcept
668 {
return !(__x == __y); }
670 template<
typename _CharT,
typename _Traits>
673 operator< (basic_string_view<_CharT, _Traits> __x,
674 basic_string_view<_CharT, _Traits> __y)
noexcept
675 {
return __x.compare(__y) < 0; }
677 template<
typename _CharT,
typename _Traits>
680 operator< (basic_string_view<_CharT, _Traits> __x,
681 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
683 {
return __x.compare(__y) < 0; }
685 template<
typename _CharT,
typename _Traits>
688 operator< (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
689 basic_string_view<_CharT, _Traits> __y)
noexcept
690 {
return __x.compare(__y) < 0; }
692 template<
typename _CharT,
typename _Traits>
695 operator> (basic_string_view<_CharT, _Traits> __x,
696 basic_string_view<_CharT, _Traits> __y)
noexcept
697 {
return __x.compare(__y) > 0; }
699 template<
typename _CharT,
typename _Traits>
702 operator> (basic_string_view<_CharT, _Traits> __x,
703 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
705 {
return __x.compare(__y) > 0; }
707 template<
typename _CharT,
typename _Traits>
710 operator> (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
711 basic_string_view<_CharT, _Traits> __y)
noexcept
712 {
return __x.compare(__y) > 0; }
714 template<
typename _CharT,
typename _Traits>
717 operator<=(basic_string_view<_CharT, _Traits> __x,
718 basic_string_view<_CharT, _Traits> __y)
noexcept
719 {
return __x.compare(__y) <= 0; }
721 template<
typename _CharT,
typename _Traits>
724 operator<=(basic_string_view<_CharT, _Traits> __x,
725 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
727 {
return __x.compare(__y) <= 0; }
729 template<
typename _CharT,
typename _Traits>
732 operator<=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
733 basic_string_view<_CharT, _Traits> __y)
noexcept
734 {
return __x.compare(__y) <= 0; }
736 template<
typename _CharT,
typename _Traits>
739 operator>=(basic_string_view<_CharT, _Traits> __x,
740 basic_string_view<_CharT, _Traits> __y)
noexcept
741 {
return __x.compare(__y) >= 0; }
743 template<
typename _CharT,
typename _Traits>
746 operator>=(basic_string_view<_CharT, _Traits> __x,
747 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
749 {
return __x.compare(__y) >= 0; }
751 template<
typename _CharT,
typename _Traits>
754 operator>=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
755 basic_string_view<_CharT, _Traits> __y)
noexcept
756 {
return __x.compare(__y) >= 0; }
761 template<
typename _CharT,
typename _Traits>
762 inline basic_ostream<_CharT, _Traits>&
763 operator<<(basic_ostream<_CharT, _Traits>& __os,
764 basic_string_view<_CharT,_Traits> __str)
765 {
return __ostream_insert(__os, __str.data(), __str.size()); }
770 using string_view = basic_string_view<char>;
771 using wstring_view = basic_string_view<wchar_t>;
772#ifdef _GLIBCXX_USE_CHAR8_T
773 using u8string_view = basic_string_view<char8_t>;
775 using u16string_view = basic_string_view<char16_t>;
776 using u32string_view = basic_string_view<char32_t>;
780 template<
typename _Tp>
784 struct hash<string_view>
785 :
public __hash_base<size_t, string_view>
789 operator()(
const string_view& __str)
const noexcept
790 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
798 struct hash<wstring_view>
799 :
public __hash_base<size_t, wstring_view>
803 operator()(
const wstring_view& __s)
const noexcept
804 {
return std::_Hash_impl::hash(__s.data(),
805 __s.length() *
sizeof(
wchar_t)); }
812#ifdef _GLIBCXX_USE_CHAR8_T
814 struct hash<u8string_view>
815 :
public __hash_base<size_t, u8string_view>
819 operator()(
const u8string_view& __str)
const noexcept
820 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
829 struct hash<u16string_view>
830 :
public __hash_base<size_t, u16string_view>
834 operator()(
const u16string_view& __s)
const noexcept
835 {
return std::_Hash_impl::hash(__s.data(),
836 __s.length() *
sizeof(
char16_t)); }
844 struct hash<u32string_view>
845 :
public __hash_base<size_t, u32string_view>
849 operator()(
const u32string_view& __s)
const noexcept
850 {
return std::_Hash_impl::hash(__s.data(),
851 __s.length() *
sizeof(
char32_t)); }
858 inline namespace literals
860 inline namespace string_view_literals
862#pragma GCC diagnostic push
863#pragma GCC diagnostic ignored "-Wliteral-suffix"
864 inline constexpr basic_string_view<char>
865 operator""sv(
const char* __str,
size_t __len)
noexcept
866 {
return basic_string_view<char>{__str, __len}; }
868 inline constexpr basic_string_view<wchar_t>
869 operator""sv(
const wchar_t* __str,
size_t __len)
noexcept
870 {
return basic_string_view<wchar_t>{__str, __len}; }
872#ifdef _GLIBCXX_USE_CHAR8_T
873 inline constexpr basic_string_view<char8_t>
874 operator""sv(
const char8_t* __str,
size_t __len)
noexcept
875 {
return basic_string_view<char8_t>{__str, __len}; }
878 inline constexpr basic_string_view<char16_t>
879 operator""sv(
const char16_t* __str,
size_t __len)
noexcept
880 {
return basic_string_view<char16_t>{__str, __len}; }
882 inline constexpr basic_string_view<char32_t>
883 operator""sv(
const char32_t* __str,
size_t __len)
noexcept
884 {
return basic_string_view<char32_t>{__str, __len}; }
886#pragma GCC diagnostic pop
890#if __cpp_lib_concepts
894 template<
typename _CharT,
typename _Traits>
895 inline constexpr bool
896 enable_borrowed_range<basic_string_view<_CharT, _Traits>> =
true;
899 template<
typename _CharT,
typename _Traits>
900 inline constexpr bool
901 enable_view<basic_string_view<_CharT, _Traits>> =
true;
904_GLIBCXX_END_NAMESPACE_VERSION
907#include <bits/string_view.tcc>
constexpr _Tp * to_address(_Tp *__ptr) noexcept
Obtain address referenced by a pointer to an object.
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
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.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
A non-owning reference to a string.
[concept.same], concept same_as
[concept.convertible], concept convertible_to
[range.sized] The sized_range concept.
A range for which ranges::begin returns a contiguous iterator.