33 #ifndef _GLIBCXX_STRING_VIEW
34 #define _GLIBCXX_STRING_VIEW 1
36 #ifdef _GLIBCXX_SYSHDR
37 #pragma GCC system_header
40 #define __glibcxx_want_constexpr_char_traits
41 #define __glibcxx_want_constexpr_string_view
42 #define __glibcxx_want_freestanding_string_view
43 #define __glibcxx_want_starts_ends_with
44 #define __glibcxx_want_string_contains
45 #define __glibcxx_want_string_subview
46 #define __glibcxx_want_string_view
49 #if __cplusplus >= 201703L
52 #ifndef __glibcxx_exc_in_string
53 #define __glibcxx_exc_in_string 2
61 #if __cplusplus >= 202002L
70 namespace std _GLIBCXX_VISIBILITY(default)
72 _GLIBCXX_BEGIN_NAMESPACE_VERSION
76 __sv_check(
size_t __size,
size_t __pos,
const char* __s)
79 __throw_out_of_range_fmt(__N(
"%s: __pos (which is %zu) > __size "
80 "(which is %zu)"), __s, __pos, __size);
87 __sv_limit(
size_t __size,
size_t __pos,
size_t __off) noexcept
89 const bool __testoff = __off < __size - __pos;
90 return __testoff ? __off : __size - __pos;
111 template<
typename _CharT,
typename _Traits = std::
char_traits<_CharT>>
114 static_assert(!is_array_v<_CharT>);
115 static_assert(is_trivially_copyable_v<_CharT>
116 && is_trivially_default_constructible_v<_CharT>
117 && is_standard_layout_v<_CharT>);
118 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
123 using traits_type = _Traits;
124 using value_type = _CharT;
125 using pointer = value_type*;
126 using const_pointer =
const value_type*;
127 using reference = value_type&;
128 using const_reference =
const value_type&;
129 using const_iterator =
const value_type*;
130 using iterator = const_iterator;
133 using size_type = size_t;
134 using difference_type = ptrdiff_t;
135 static constexpr size_type npos = size_type(-1);
141 : _M_len{0}, _M_str{
nullptr}
146 [[__gnu__::__nonnull__]]
149 : _M_len{traits_type::length(__str)},
155 : _M_len{__len}, _M_str{__str}
158 #if __cplusplus >= 202002L && __cpp_lib_concepts
159 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
160 requires same_as<iter_value_t<_It>, _CharT>
161 && (!convertible_to<_End, size_type>)
164 noexcept(noexcept(__last - __first))
168 #if __cplusplus > 202002L
169 template<
typename _Range,
typename _DRange = remove_cvref_t<_Range>>
170 requires (!is_same_v<_DRange, basic_string_view>)
171 && ranges::contiguous_range<_Range>
172 && ranges::sized_range<_Range>
173 && is_same_v<ranges::range_value_t<_Range>, _CharT>
174 && (!is_convertible_v<_Range, const _CharT*>)
175 && (!requires (_DRange& __d) {
176 __d.operator ::std::basic_string_view<_CharT, _Traits>();
180 noexcept(noexcept(ranges::size(__r)) && noexcept(ranges::data(__r)))
181 : _M_len(ranges::size(__r)), _M_str(ranges::data(__r))
194 constexpr const_iterator
195 begin()
const noexcept
196 {
return this->_M_str; }
199 constexpr const_iterator
201 {
return this->_M_str + this->_M_len; }
204 constexpr const_iterator
205 cbegin()
const noexcept
206 {
return this->_M_str; }
209 constexpr const_iterator
210 cend()
const noexcept
211 {
return this->_M_str + this->_M_len; }
215 rbegin()
const noexcept
220 rend()
const noexcept
225 crbegin()
const noexcept
230 crend()
const noexcept
237 size()
const noexcept
238 {
return this->_M_len; }
242 length()
const noexcept
247 max_size()
const noexcept
249 return (npos -
sizeof(size_type) -
sizeof(
void*))
250 /
sizeof(value_type) / 4;
255 empty()
const noexcept
256 {
return this->_M_len == 0; }
261 constexpr const_reference
262 operator[](size_type __pos)
const noexcept
264 __glibcxx_assert(__pos < this->_M_len);
265 return *(this->_M_str + __pos);
269 constexpr const_reference
270 at(size_type __pos)
const
273 __throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
274 "(which is %zu) >= this->size() "
275 "(which is %zu)"), __pos, this->size());
276 return *(this->_M_str + __pos);
280 constexpr const_reference
281 front()
const noexcept
283 __glibcxx_assert(this->_M_len > 0);
284 return *this->_M_str;
288 constexpr const_reference
289 back()
const noexcept
291 __glibcxx_assert(this->_M_len > 0);
292 return *(this->_M_str + this->_M_len - 1);
296 constexpr const_pointer
297 data()
const noexcept
298 {
return this->_M_str; }
303 remove_prefix(size_type __n) noexcept
305 __glibcxx_assert(this->_M_len >= __n);
311 remove_suffix(size_type __n) noexcept
313 __glibcxx_assert(this->_M_len >= __n);
329 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
331 __glibcxx_requires_string_len(__str, __n);
332 __pos = std::__sv_check(size(), __pos,
"basic_string_view::copy");
333 const size_type __rlen = std::min<size_t>(__n, _M_len - __pos);
336 traits_type::copy(__str, data() + __pos, __rlen);
342 substr(size_type __pos = 0, size_type __n = npos)
const noexcept(
false)
344 __pos = std::__sv_check(size(), __pos,
"basic_string_view::substr");
345 const size_type __rlen = std::min<size_t>(__n, _M_len - __pos);
349 #ifdef __glibcxx_string_subview // >= C++26
352 subview(size_type __pos = 0, size_type __n = npos)
const
353 {
return substr(__pos, __n); }
360 const size_type __rlen =
std::min(this->_M_len, __str._M_len);
361 int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
363 __ret = _S_compare(this->_M_len, __str._M_len);
370 {
return this->substr(__pos1, __n1).compare(__str); }
374 compare(size_type __pos1, size_type __n1,
377 return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
380 [[nodiscard, __gnu__::__nonnull__]]
382 compare(
const _CharT* __str)
const noexcept
385 [[nodiscard, __gnu__::__nonnull__]]
387 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
392 compare(size_type __pos1, size_type __n1,
393 const _CharT* __str, size_type __n2)
const noexcept(
false)
395 return this->substr(__pos1, __n1)
399 #ifdef __cpp_lib_starts_ends_with // C++ >= 20
404 return _M_len >= __x._M_len
405 && traits_type::compare(_M_str, __x._M_str, __x._M_len) == 0;
410 starts_with(_CharT __x)
const noexcept
411 {
return !this->empty() && traits_type::eq(this->front(), __x); }
413 [[nodiscard, __gnu__::__nonnull__]]
415 starts_with(
const _CharT* __x)
const noexcept
422 const auto __len = this->size();
423 const auto __xlen = __x.size();
424 return __len >= __xlen
425 && traits_type::compare(end() - __xlen, __x.data(), __xlen) == 0;
430 ends_with(_CharT __x)
const noexcept
431 {
return !this->empty() && traits_type::eq(this->back(), __x); }
433 [[nodiscard, __gnu__::__nonnull__]]
435 ends_with(
const _CharT* __x)
const noexcept
437 #endif // __cpp_lib_starts_ends_with
439 #if __cplusplus > 202002L
440 #if _GLIBCXX_HOSTED && !defined(__cpp_lib_string_contains)
443 # error "libstdc++ bug: string_contains not defined when it should be"
448 {
return this->find(__x) != npos; }
452 contains(_CharT __x)
const noexcept
453 {
return this->find(__x) != npos; }
455 [[nodiscard, __gnu__::__nonnull__]]
457 contains(
const _CharT* __x)
const noexcept
458 {
return this->find(__x) != npos; }
466 {
return this->find(__str._M_str, __pos, __str._M_len); }
470 find(_CharT __c, size_type __pos = 0)
const noexcept;
474 find(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
476 [[nodiscard, __gnu__::__nonnull__]]
478 find(
const _CharT* __str, size_type __pos = 0)
const noexcept
479 {
return this->find(__str, __pos, traits_type::length(__str)); }
484 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
488 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
492 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
494 [[nodiscard, __gnu__::__nonnull__]]
496 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
497 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
502 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
506 find_first_of(_CharT __c, size_type __pos = 0)
const noexcept
507 {
return this->find(__c, __pos); }
511 find_first_of(
const _CharT* __str, size_type __pos,
512 size_type __n)
const noexcept;
514 [[nodiscard, __gnu__::__nonnull__]]
516 find_first_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
517 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
522 size_type __pos = npos)
const noexcept
523 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
527 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
528 {
return this->rfind(__c, __pos); }
532 find_last_of(
const _CharT* __str, size_type __pos,
533 size_type __n)
const noexcept;
535 [[nodiscard, __gnu__::__nonnull__]]
537 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
538 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
543 size_type __pos = 0)
const noexcept
544 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
548 find_first_not_of(_CharT __c, size_type __pos = 0)
const noexcept;
552 find_first_not_of(
const _CharT* __str,
553 size_type __pos, size_type __n)
const noexcept;
555 [[nodiscard, __gnu__::__nonnull__]]
557 find_first_not_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
559 return this->find_first_not_of(__str, __pos,
560 traits_type::length(__str));
566 size_type __pos = npos)
const noexcept
567 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
571 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
575 find_last_not_of(
const _CharT* __str,
576 size_type __pos, size_type __n)
const noexcept;
578 [[nodiscard, __gnu__::__nonnull__]]
580 find_last_not_of(
const _CharT* __str,
581 size_type __pos = npos)
const noexcept
583 return this->find_last_not_of(__str, __pos,
584 traits_type::length(__str));
590 _S_compare(size_type __n1, size_type __n2) noexcept
593 const difference_type __diff = __n1 - __n2;
594 if (__diff > __limits::__max)
595 return __limits::__max;
596 if (__diff < __limits::__min)
597 return __limits::__min;
598 return static_cast<int>(__diff);
602 const _CharT* _M_str;
605 #if __cplusplus > 201703L && __cpp_lib_concepts && __cpp_deduction_guides
606 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
609 #if __cplusplus > 202002L
610 template<ranges::contiguous_range _Range>
623 #if __cpp_lib_three_way_comparison
624 template<
typename _CharT,
typename _Traits>
630 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
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==(basic_string_view<_CharT, _Traits> __x,
645 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
647 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
649 template<
typename _CharT,
typename _Traits>
652 operator==(basic_string_view<_CharT, _Traits> __x,
653 basic_string_view<_CharT, _Traits> __y) noexcept
654 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
656 template<
typename _CharT,
typename _Traits>
659 operator==(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
660 basic_string_view<_CharT, _Traits> __y) noexcept
661 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
663 template<
typename _CharT,
typename _Traits>
666 operator!=(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 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
676 {
return !(__x == __y); }
678 template<
typename _CharT,
typename _Traits>
681 operator!=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
682 basic_string_view<_CharT, _Traits> __y) noexcept
683 {
return !(__x == __y); }
685 template<
typename _CharT,
typename _Traits>
688 operator< (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 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
698 {
return __x.compare(__y) < 0; }
700 template<
typename _CharT,
typename _Traits>
703 operator< (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
704 basic_string_view<_CharT, _Traits> __y) noexcept
705 {
return __x.compare(__y) < 0; }
707 template<
typename _CharT,
typename _Traits>
710 operator> (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 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
720 {
return __x.compare(__y) > 0; }
722 template<
typename _CharT,
typename _Traits>
725 operator> (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
726 basic_string_view<_CharT, _Traits> __y) noexcept
727 {
return __x.compare(__y) > 0; }
729 template<
typename _CharT,
typename _Traits>
732 operator<=(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 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
742 {
return __x.compare(__y) <= 0; }
744 template<
typename _CharT,
typename _Traits>
747 operator<=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
748 basic_string_view<_CharT, _Traits> __y) noexcept
749 {
return __x.compare(__y) <= 0; }
751 template<
typename _CharT,
typename _Traits>
754 operator>=(basic_string_view<_CharT, _Traits> __x,
755 basic_string_view<_CharT, _Traits> __y) noexcept
756 {
return __x.compare(__y) >= 0; }
758 template<
typename _CharT,
typename _Traits>
761 operator>=(basic_string_view<_CharT, _Traits> __x,
762 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
764 {
return __x.compare(__y) >= 0; }
766 template<
typename _CharT,
typename _Traits>
769 operator>=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
770 basic_string_view<_CharT, _Traits> __y) noexcept
771 {
return __x.compare(__y) >= 0; }
772 #endif // three-way comparison
776 template<
typename _CharT,
typename _Traits>
777 inline basic_ostream<_CharT, _Traits>&
778 operator<<(basic_ostream<_CharT, _Traits>& __os,
779 basic_string_view<_CharT,_Traits> __str)
780 {
return __ostream_insert(__os, __str.data(), __str.size()); }
785 using string_view = basic_string_view<char>;
786 using wstring_view = basic_string_view<wchar_t>;
787 #ifdef _GLIBCXX_USE_CHAR8_T
788 using u8string_view = basic_string_view<char8_t>;
790 using u16string_view = basic_string_view<char16_t>;
791 using u32string_view = basic_string_view<char32_t>;
795 template<
typename _Tp>
800 :
public __hash_base<size_t, string_view>
804 operator()(
const string_view& __str)
const noexcept
805 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
813 struct hash<wstring_view>
814 :
public __hash_base<size_t, wstring_view>
818 operator()(
const wstring_view& __s)
const noexcept
819 {
return std::_Hash_impl::hash(__s.data(),
820 __s.length() *
sizeof(
wchar_t)); }
827 #ifdef _GLIBCXX_USE_CHAR8_T
829 struct hash<u8string_view>
830 :
public __hash_base<size_t, u8string_view>
834 operator()(
const u8string_view& __str)
const noexcept
835 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
844 struct hash<u16string_view>
845 :
public __hash_base<size_t, u16string_view>
849 operator()(
const u16string_view& __s)
const noexcept
850 {
return std::_Hash_impl::hash(__s.data(),
851 __s.length() *
sizeof(char16_t)); }
859 struct hash<u32string_view>
860 :
public __hash_base<size_t, u32string_view>
864 operator()(
const u32string_view& __s)
const noexcept
865 {
return std::_Hash_impl::hash(__s.data(),
866 __s.length() *
sizeof(char32_t)); }
873 inline namespace literals
875 inline namespace string_view_literals
877 #pragma GCC diagnostic push
878 #pragma GCC diagnostic ignored "-Wliteral-suffix"
879 inline constexpr basic_string_view<char>
880 operator""sv(
const char* __str,
size_t __len) noexcept
881 {
return basic_string_view<char>{__str, __len}; }
883 inline constexpr basic_string_view<wchar_t>
884 operator""sv(
const wchar_t* __str,
size_t __len) noexcept
885 {
return basic_string_view<wchar_t>{__str, __len}; }
887 #ifdef _GLIBCXX_USE_CHAR8_T
888 inline constexpr basic_string_view<char8_t>
889 operator""sv(
const char8_t* __str,
size_t __len) noexcept
890 {
return basic_string_view<char8_t>{__str, __len}; }
893 inline constexpr basic_string_view<char16_t>
894 operator""sv(
const char16_t* __str,
size_t __len) noexcept
895 {
return basic_string_view<char16_t>{__str, __len}; }
897 inline constexpr basic_string_view<char32_t>
898 operator""sv(
const char32_t* __str,
size_t __len) noexcept
899 {
return basic_string_view<char32_t>{__str, __len}; }
901 #pragma GCC diagnostic pop
905 #if __cpp_lib_concepts
909 template<
typename _CharT,
typename _Traits>
910 inline constexpr
bool
911 enable_borrowed_range<basic_string_view<_CharT, _Traits>> =
true;
914 template<
typename _CharT,
typename _Traits>
915 inline constexpr
bool
916 enable_view<basic_string_view<_CharT, _Traits>> =
true;
919 _GLIBCXX_END_NAMESPACE_VERSION
922 #include <bits/string_view.tcc>
924 #if __glibcxx_exc_in_string == 2
925 #undef __glibcxx_exc_in_string
926 #if (_GLIBCXX_HOSTED && __cpp_exceptions && __cplusplus > 202302L \
927 && __cpp_constexpr_exceptions >= 202411L)
934 #endif // _GLIBCXX_STRING_VIEW