33 #ifndef _GLIBCXX_STRING_VIEW
34 #define _GLIBCXX_STRING_VIEW 1
36 #pragma GCC system_header
38 #if __cplusplus >= 201703L
49 #if __cplusplus >= 202002L
53 namespace std _GLIBCXX_VISIBILITY(default)
55 _GLIBCXX_BEGIN_NAMESPACE_VERSION
57 # define __cpp_lib_string_view 201803L
58 #if __cplusplus > 201703L
59 # define __cpp_lib_constexpr_string_view 201811L
64 __sv_check(
size_t __size,
size_t __pos,
const char* __s)
67 __throw_out_of_range_fmt(__N(
"%s: __pos (which is %zu) > __size "
68 "(which is %zu)"), __s, __pos, __size);
75 __sv_limit(
size_t __size,
size_t __pos,
size_t __off) noexcept
77 const bool __testoff = __off < __size - __pos;
78 return __testoff ? __off : __size - __pos;
99 template<
typename _CharT,
typename _Traits = std::
char_traits<_CharT>>
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>);
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);
127 : _M_len{0}, _M_str{
nullptr}
132 __attribute__((__nonnull__)) constexpr
134 : _M_len{traits_type::length(__str)},
140 : _M_len{__len}, _M_str{__str}
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>)
149 noexcept(noexcept(__last - __first))
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>();
163 && (!requires {
typename _DRange::traits_type; }
164 || is_same_v<typename _DRange::traits_type, _Traits>)
167 noexcept(noexcept(ranges::size(__r)) && noexcept(ranges::data(__r)))
168 : _M_len(ranges::size(__r)), _M_str(ranges::data(__r))
181 constexpr const_iterator
182 begin()
const noexcept
183 {
return this->_M_str; }
185 constexpr const_iterator
187 {
return this->_M_str + this->_M_len; }
189 constexpr const_iterator
190 cbegin()
const noexcept
191 {
return this->_M_str; }
193 constexpr const_iterator
194 cend()
const noexcept
195 {
return this->_M_str + this->_M_len; }
198 rbegin()
const noexcept
202 rend()
const noexcept
206 crbegin()
const noexcept
210 crend()
const noexcept
216 size()
const noexcept
217 {
return this->_M_len; }
220 length()
const noexcept
224 max_size()
const noexcept
226 return (npos -
sizeof(size_type) -
sizeof(
void*))
227 /
sizeof(value_type) / 4;
230 [[nodiscard]] constexpr
bool
231 empty()
const noexcept
232 {
return this->_M_len == 0; }
236 constexpr const_reference
237 operator[](size_type __pos)
const noexcept
239 __glibcxx_assert(__pos < this->_M_len);
240 return *(this->_M_str + __pos);
243 constexpr const_reference
244 at(size_type __pos)
const
247 __throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
248 "(which is %zu) >= this->size() "
249 "(which is %zu)"), __pos, this->size());
250 return *(this->_M_str + __pos);
253 constexpr const_reference
254 front()
const noexcept
256 __glibcxx_assert(this->_M_len > 0);
257 return *this->_M_str;
260 constexpr const_reference
261 back()
const noexcept
263 __glibcxx_assert(this->_M_len > 0);
264 return *(this->_M_str + this->_M_len - 1);
267 constexpr const_pointer
268 data()
const noexcept
269 {
return this->_M_str; }
274 remove_prefix(size_type __n) noexcept
276 __glibcxx_assert(this->_M_len >= __n);
282 remove_suffix(size_type __n) noexcept
283 { this->_M_len -= __n; }
297 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
299 __glibcxx_requires_string_len(__str, __n);
300 __pos = std::__sv_check(size(), __pos,
"basic_string_view::copy");
301 const size_type __rlen =
std::min(__n, _M_len - __pos);
304 traits_type::copy(__str, data() + __pos, __rlen);
309 substr(size_type __pos = 0, size_type __n = npos)
const noexcept(
false)
311 __pos = std::__sv_check(size(), __pos,
"basic_string_view::substr");
312 const size_type __rlen =
std::min(__n, _M_len - __pos);
319 const size_type __rlen =
std::min(this->_M_len, __str._M_len);
320 int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
322 __ret = _S_compare(this->_M_len, __str._M_len);
328 {
return this->substr(__pos1, __n1).compare(__str); }
331 compare(size_type __pos1, size_type __n1,
334 return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
337 __attribute__((__nonnull__)) constexpr
int
338 compare(
const _CharT* __str)
const noexcept
341 __attribute__((__nonnull__)) constexpr
int
342 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
346 compare(size_type __pos1, size_type __n1,
347 const _CharT* __str, size_type __n2)
const noexcept(
false)
349 return this->substr(__pos1, __n1)
353 #if __cplusplus > 201703L
354 #define __cpp_lib_starts_ends_with 201711L
357 {
return this->substr(0, __x.size()) == __x; }
360 starts_with(_CharT __x)
const noexcept
361 {
return !this->empty() && traits_type::eq(this->front(), __x); }
364 starts_with(
const _CharT* __x)
const noexcept
370 const auto __len = this->size();
371 const auto __xlen = __x.size();
372 return __len >= __xlen
373 && traits_type::compare(end() - __xlen, __x.data(), __xlen) == 0;
377 ends_with(_CharT __x)
const noexcept
378 {
return !this->empty() && traits_type::eq(this->back(), __x); }
381 ends_with(
const _CharT* __x)
const noexcept
385 #if __cplusplus > 202002L
386 #define __cpp_lib_string_contains 202011L
389 {
return this->find(__x) != npos; }
392 contains(_CharT __x)
const noexcept
393 {
return this->find(__x) != npos; }
396 contains(
const _CharT* __x)
const noexcept
397 {
return this->find(__x) != npos; }
404 {
return this->find(__str._M_str, __pos, __str._M_len); }
407 find(_CharT __c, size_type __pos = 0)
const noexcept;
410 find(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
412 __attribute__((__nonnull__)) constexpr size_type
413 find(
const _CharT* __str, size_type __pos = 0)
const noexcept
414 {
return this->find(__str, __pos, traits_type::length(__str)); }
418 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
421 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
424 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
426 __attribute__((__nonnull__)) constexpr size_type
427 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
428 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
432 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
435 find_first_of(_CharT __c, size_type __pos = 0)
const noexcept
436 {
return this->find(__c, __pos); }
439 find_first_of(
const _CharT* __str, size_type __pos,
440 size_type __n)
const noexcept;
442 __attribute__((__nonnull__)) constexpr size_type
443 find_first_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
444 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
448 size_type __pos = npos)
const noexcept
449 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
452 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
453 {
return this->rfind(__c, __pos); }
456 find_last_of(
const _CharT* __str, size_type __pos,
457 size_type __n)
const noexcept;
459 __attribute__((__nonnull__)) constexpr size_type
460 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
461 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
465 size_type __pos = 0)
const noexcept
466 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
469 find_first_not_of(_CharT __c, size_type __pos = 0)
const noexcept;
472 find_first_not_of(
const _CharT* __str,
473 size_type __pos, size_type __n)
const noexcept;
475 __attribute__((__nonnull__)) constexpr size_type
476 find_first_not_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
478 return this->find_first_not_of(__str, __pos,
479 traits_type::length(__str));
484 size_type __pos = npos)
const noexcept
485 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
488 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
491 find_last_not_of(
const _CharT* __str,
492 size_type __pos, size_type __n)
const noexcept;
494 __attribute__((__nonnull__)) constexpr size_type
495 find_last_not_of(
const _CharT* __str,
496 size_type __pos = npos)
const noexcept
498 return this->find_last_not_of(__str, __pos,
499 traits_type::length(__str));
505 _S_compare(size_type __n1, size_type __n2) noexcept
508 const difference_type __diff = __n1 - __n2;
509 if (__diff > __limits::__max)
510 return __limits::__max;
511 if (__diff < __limits::__min)
512 return __limits::__min;
513 return static_cast<int>(__diff);
517 const _CharT* _M_str;
520 #if __cplusplus > 201703L && __cpp_lib_concepts && __cpp_deduction_guides
521 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
524 #if __cplusplus > 202002L
525 template<ranges::contiguous_range _Range>
538 template<
typename _CharT,
typename _Traits>
542 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
544 template<
typename _CharT,
typename _Traits>
546 operator==(basic_string_view<_CharT, _Traits> __x,
547 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
549 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
551 #if __cpp_lib_three_way_comparison
552 template<
typename _CharT,
typename _Traits>
554 operator<=>(basic_string_view<_CharT, _Traits> __x,
555 basic_string_view<_CharT, _Traits> __y) noexcept
556 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
557 {
return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
559 template<
typename _CharT,
typename _Traits>
561 operator<=>(basic_string_view<_CharT, _Traits> __x,
562 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
564 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
565 {
return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
567 template<
typename _CharT,
typename _Traits>
569 operator==(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
570 basic_string_view<_CharT, _Traits> __y) noexcept
571 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
573 template<
typename _CharT,
typename _Traits>
575 operator!=(basic_string_view<_CharT, _Traits> __x,
576 basic_string_view<_CharT, _Traits> __y) noexcept
577 {
return !(__x == __y); }
579 template<
typename _CharT,
typename _Traits>
581 operator!=(basic_string_view<_CharT, _Traits> __x,
582 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
584 {
return !(__x == __y); }
586 template<
typename _CharT,
typename _Traits>
588 operator!=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
589 basic_string_view<_CharT, _Traits> __y) noexcept
590 {
return !(__x == __y); }
592 template<
typename _CharT,
typename _Traits>
594 operator< (basic_string_view<_CharT, _Traits> __x,
595 basic_string_view<_CharT, _Traits> __y) noexcept
596 {
return __x.compare(__y) < 0; }
598 template<
typename _CharT,
typename _Traits>
600 operator< (basic_string_view<_CharT, _Traits> __x,
601 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
603 {
return __x.compare(__y) < 0; }
605 template<
typename _CharT,
typename _Traits>
607 operator< (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
608 basic_string_view<_CharT, _Traits> __y) noexcept
609 {
return __x.compare(__y) < 0; }
611 template<
typename _CharT,
typename _Traits>
613 operator> (basic_string_view<_CharT, _Traits> __x,
614 basic_string_view<_CharT, _Traits> __y) noexcept
615 {
return __x.compare(__y) > 0; }
617 template<
typename _CharT,
typename _Traits>
619 operator> (basic_string_view<_CharT, _Traits> __x,
620 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
622 {
return __x.compare(__y) > 0; }
624 template<
typename _CharT,
typename _Traits>
626 operator> (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
627 basic_string_view<_CharT, _Traits> __y) noexcept
628 {
return __x.compare(__y) > 0; }
630 template<
typename _CharT,
typename _Traits>
632 operator<=(basic_string_view<_CharT, _Traits> __x,
633 basic_string_view<_CharT, _Traits> __y) noexcept
634 {
return __x.compare(__y) <= 0; }
636 template<
typename _CharT,
typename _Traits>
638 operator<=(basic_string_view<_CharT, _Traits> __x,
639 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
641 {
return __x.compare(__y) <= 0; }
643 template<
typename _CharT,
typename _Traits>
645 operator<=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
646 basic_string_view<_CharT, _Traits> __y) noexcept
647 {
return __x.compare(__y) <= 0; }
649 template<
typename _CharT,
typename _Traits>
651 operator>=(basic_string_view<_CharT, _Traits> __x,
652 basic_string_view<_CharT, _Traits> __y) noexcept
653 {
return __x.compare(__y) >= 0; }
655 template<
typename _CharT,
typename _Traits>
657 operator>=(basic_string_view<_CharT, _Traits> __x,
658 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
660 {
return __x.compare(__y) >= 0; }
662 template<
typename _CharT,
typename _Traits>
664 operator>=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
665 basic_string_view<_CharT, _Traits> __y) noexcept
666 {
return __x.compare(__y) >= 0; }
667 #endif // three-way comparison
670 template<
typename _CharT,
typename _Traits>
671 inline basic_ostream<_CharT, _Traits>&
672 operator<<(basic_ostream<_CharT, _Traits>& __os,
673 basic_string_view<_CharT,_Traits> __str)
674 {
return __ostream_insert(__os, __str.data(), __str.size()); }
679 using string_view = basic_string_view<char>;
680 using wstring_view = basic_string_view<wchar_t>;
681 #ifdef _GLIBCXX_USE_CHAR8_T
682 using u8string_view = basic_string_view<char8_t>;
684 using u16string_view = basic_string_view<char16_t>;
685 using u32string_view = basic_string_view<char32_t>;
689 template<
typename _Tp>
694 :
public __hash_base<size_t, string_view>
697 operator()(
const string_view& __str)
const noexcept
698 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
706 struct hash<wstring_view>
707 :
public __hash_base<size_t, wstring_view>
710 operator()(
const wstring_view& __s)
const noexcept
711 {
return std::_Hash_impl::hash(__s.data(),
712 __s.length() *
sizeof(wchar_t)); }
719 #ifdef _GLIBCXX_USE_CHAR8_T
721 struct hash<u8string_view>
722 :
public __hash_base<size_t, u8string_view>
725 operator()(
const u8string_view& __str)
const noexcept
726 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
735 struct hash<u16string_view>
736 :
public __hash_base<size_t, u16string_view>
739 operator()(
const u16string_view& __s)
const noexcept
740 {
return std::_Hash_impl::hash(__s.data(),
741 __s.length() *
sizeof(char16_t)); }
749 struct hash<u32string_view>
750 :
public __hash_base<size_t, u32string_view>
753 operator()(
const u32string_view& __s)
const noexcept
754 {
return std::_Hash_impl::hash(__s.data(),
755 __s.length() *
sizeof(char32_t)); }
762 inline namespace literals
764 inline namespace string_view_literals
766 #pragma GCC diagnostic push
767 #pragma GCC diagnostic ignored "-Wliteral-suffix"
768 inline constexpr basic_string_view<char>
769 operator""sv(
const char* __str,
size_t __len) noexcept
770 {
return basic_string_view<char>{__str, __len}; }
772 inline constexpr basic_string_view<wchar_t>
773 operator""sv(
const wchar_t* __str,
size_t __len) noexcept
774 {
return basic_string_view<wchar_t>{__str, __len}; }
776 #ifdef _GLIBCXX_USE_CHAR8_T
777 inline constexpr basic_string_view<char8_t>
778 operator""sv(
const char8_t* __str,
size_t __len) noexcept
779 {
return basic_string_view<char8_t>{__str, __len}; }
782 inline constexpr basic_string_view<char16_t>
783 operator""sv(
const char16_t* __str,
size_t __len) noexcept
784 {
return basic_string_view<char16_t>{__str, __len}; }
786 inline constexpr basic_string_view<char32_t>
787 operator""sv(
const char32_t* __str,
size_t __len) noexcept
788 {
return basic_string_view<char32_t>{__str, __len}; }
790 #pragma GCC diagnostic pop
794 #if __cpp_lib_concepts
798 template<
typename _CharT,
typename _Traits>
799 inline constexpr
bool
800 enable_borrowed_range<basic_string_view<_CharT, _Traits>> =
true;
803 template<
typename _CharT,
typename _Traits>
804 inline constexpr
bool
805 enable_view<basic_string_view<_CharT, _Traits>> =
true;
808 _GLIBCXX_END_NAMESPACE_VERSION
811 #include <bits/string_view.tcc>
813 #endif // __cplusplus <= 201402L
815 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW
A non-owning reference to a string.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
Primary class template hash.
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
constexpr _Tp * to_address(_Tp *__ptr) noexcept
Obtain address referenced by a pointer to an object.