33 #ifndef _GLIBCXX_STRING_VIEW
34 #define _GLIBCXX_STRING_VIEW 1
36 #pragma GCC system_header
38 #if __cplusplus >= 201703L
47 #if __cplusplus >= 202002L
51 namespace std _GLIBCXX_VISIBILITY(default)
53 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 # define __cpp_lib_string_view 201803L
56 #if __cplusplus > 201703L
57 # define __cpp_lib_constexpr_string_view 201811L
62 __sv_check(
size_t __size,
size_t __pos,
const char* __s)
65 __throw_out_of_range_fmt(__N(
"%s: __pos (which is %zu) > __size "
66 "(which is %zu)"), __s, __pos, __size);
73 __sv_limit(
size_t __size,
size_t __pos,
size_t __off) noexcept
75 const bool __testoff = __off < __size - __pos;
76 return __testoff ? __off : __size - __pos;
97 template<
typename _CharT,
typename _Traits = std::
char_traits<_CharT>>
100 static_assert(!is_array_v<_CharT>);
101 static_assert(is_trivial_v<_CharT> && is_standard_layout_v<_CharT>);
102 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
107 using traits_type = _Traits;
108 using value_type = _CharT;
109 using pointer = value_type*;
110 using const_pointer =
const value_type*;
111 using reference = value_type&;
112 using const_reference =
const value_type&;
113 using const_iterator =
const value_type*;
114 using iterator = const_iterator;
117 using size_type = size_t;
118 using difference_type = ptrdiff_t;
119 static constexpr size_type npos = size_type(-1);
125 : _M_len{0}, _M_str{
nullptr}
130 __attribute__((__nonnull__)) constexpr
132 : _M_len{traits_type::length(__str)},
138 : _M_len{__len}, _M_str{__str}
141 #if __cplusplus >= 202002L && __cpp_lib_concepts
142 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
143 requires same_as<iter_value_t<_It>, _CharT>
144 && (!convertible_to<_End, size_type>)
147 noexcept(noexcept(__last - __first))
148 : _M_len(__last - __first), _M_str(std::to_address(__first))
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>();
161 && (!requires {
typename _DRange::traits_type; }
162 || is_same_v<typename _DRange::traits_type, _Traits>)
176 constexpr const_iterator
177 begin()
const noexcept
178 {
return this->_M_str; }
180 constexpr const_iterator
182 {
return this->_M_str + this->_M_len; }
184 constexpr const_iterator
185 cbegin()
const noexcept
186 {
return this->_M_str; }
188 constexpr const_iterator
189 cend()
const noexcept
190 {
return this->_M_str + this->_M_len; }
193 rbegin()
const noexcept
197 rend()
const noexcept
201 crbegin()
const noexcept
205 crend()
const noexcept
211 size()
const noexcept
212 {
return this->_M_len; }
215 length()
const noexcept
219 max_size()
const noexcept
221 return (npos -
sizeof(size_type) -
sizeof(
void*))
222 /
sizeof(value_type) / 4;
225 [[nodiscard]] constexpr
bool
226 empty()
const noexcept
227 {
return this->_M_len == 0; }
231 constexpr const_reference
232 operator[](size_type __pos)
const noexcept
234 __glibcxx_assert(__pos < this->_M_len);
235 return *(this->_M_str + __pos);
238 constexpr const_reference
239 at(size_type __pos)
const
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);
248 constexpr const_reference
249 front()
const noexcept
251 __glibcxx_assert(this->_M_len > 0);
252 return *this->_M_str;
255 constexpr const_reference
256 back()
const noexcept
258 __glibcxx_assert(this->_M_len > 0);
259 return *(this->_M_str + this->_M_len - 1);
262 constexpr const_pointer
263 data()
const noexcept
264 {
return this->_M_str; }
269 remove_prefix(size_type __n) noexcept
271 __glibcxx_assert(this->_M_len >= __n);
277 remove_suffix(size_type __n) noexcept
278 { this->_M_len -= __n; }
292 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
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);
299 traits_type::copy(__str, data() + __pos, __rlen);
304 substr(size_type __pos = 0, size_type __n = npos)
const noexcept(
false)
306 __pos = std::__sv_check(size(), __pos,
"basic_string_view::substr");
307 const size_type __rlen =
std::min(__n, _M_len - __pos);
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);
317 __ret = _S_compare(this->_M_len, __str._M_len);
323 {
return this->substr(__pos1, __n1).compare(__str); }
326 compare(size_type __pos1, size_type __n1,
329 return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
332 __attribute__((__nonnull__)) constexpr
int
333 compare(
const _CharT* __str)
const noexcept
336 __attribute__((__nonnull__)) constexpr
int
337 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
341 compare(size_type __pos1, size_type __n1,
342 const _CharT* __str, size_type __n2)
const noexcept(
false)
344 return this->substr(__pos1, __n1)
348 #if __cplusplus > 201703L
349 #define __cpp_lib_starts_ends_with 201711L
352 {
return this->substr(0, __x.size()) == __x; }
355 starts_with(_CharT __x)
const noexcept
356 {
return !this->empty() && traits_type::eq(this->front(), __x); }
359 starts_with(
const _CharT* __x)
const noexcept
365 const auto __len = this->size();
366 const auto __xlen = __x.size();
367 return __len >= __xlen
368 && traits_type::compare(end() - __xlen, __x.data(), __xlen) == 0;
372 ends_with(_CharT __x)
const noexcept
373 {
return !this->empty() && traits_type::eq(this->back(), __x); }
376 ends_with(
const _CharT* __x)
const noexcept
380 #if __cplusplus > 202002L
381 #define __cpp_lib_string_contains 202011L
384 {
return this->find(__x) != npos; }
387 contains(_CharT __x)
const noexcept
388 {
return this->find(__x) != npos; }
391 contains(
const _CharT* __x)
const noexcept
392 {
return this->find(__x) != npos; }
399 {
return this->find(__str._M_str, __pos, __str._M_len); }
402 find(_CharT __c, size_type __pos = 0)
const noexcept;
405 find(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
407 __attribute__((__nonnull__)) constexpr size_type
408 find(
const _CharT* __str, size_type __pos = 0)
const noexcept
409 {
return this->find(__str, __pos, traits_type::length(__str)); }
413 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
416 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
419 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
421 __attribute__((__nonnull__)) constexpr size_type
422 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
423 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
427 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
430 find_first_of(_CharT __c, size_type __pos = 0)
const noexcept
431 {
return this->find(__c, __pos); }
434 find_first_of(
const _CharT* __str, size_type __pos,
435 size_type __n)
const noexcept;
437 __attribute__((__nonnull__)) constexpr size_type
438 find_first_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
439 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
443 size_type __pos = npos)
const noexcept
444 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
447 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
448 {
return this->rfind(__c, __pos); }
451 find_last_of(
const _CharT* __str, size_type __pos,
452 size_type __n)
const noexcept;
454 __attribute__((__nonnull__)) constexpr size_type
455 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
456 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
460 size_type __pos = 0)
const noexcept
461 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
464 find_first_not_of(_CharT __c, size_type __pos = 0)
const noexcept;
467 find_first_not_of(
const _CharT* __str,
468 size_type __pos, size_type __n)
const noexcept;
470 __attribute__((__nonnull__)) constexpr size_type
471 find_first_not_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
473 return this->find_first_not_of(__str, __pos,
474 traits_type::length(__str));
479 size_type __pos = npos)
const noexcept
480 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
483 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
486 find_last_not_of(
const _CharT* __str,
487 size_type __pos, size_type __n)
const noexcept;
489 __attribute__((__nonnull__)) constexpr size_type
490 find_last_not_of(
const _CharT* __str,
491 size_type __pos = npos)
const noexcept
493 return this->find_last_not_of(__str, __pos,
494 traits_type::length(__str));
500 _S_compare(size_type __n1, size_type __n2) noexcept
503 const difference_type __diff = __n1 - __n2;
504 if (__diff > __limits::__max)
505 return __limits::__max;
506 if (__diff < __limits::__min)
507 return __limits::__min;
508 return static_cast<int>(__diff);
512 const _CharT* _M_str;
515 #if __cplusplus > 201703L && __cpp_lib_concepts && __cpp_deduction_guides
516 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
519 #if __cplusplus > 202002L
520 template<ranges::contiguous_range _Range>
533 template<
typename _CharT,
typename _Traits>
537 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
539 template<
typename _CharT,
typename _Traits>
541 operator==(basic_string_view<_CharT, _Traits> __x,
542 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
544 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
546 #if __cpp_lib_three_way_comparison
547 template<
typename _CharT,
typename _Traits>
549 operator<=>(basic_string_view<_CharT, _Traits> __x,
550 basic_string_view<_CharT, _Traits> __y) noexcept
551 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
552 {
return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
554 template<
typename _CharT,
typename _Traits>
556 operator<=>(basic_string_view<_CharT, _Traits> __x,
557 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
559 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
560 {
return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
562 template<
typename _CharT,
typename _Traits>
564 operator==(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
565 basic_string_view<_CharT, _Traits> __y) noexcept
566 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
568 template<
typename _CharT,
typename _Traits>
570 operator!=(basic_string_view<_CharT, _Traits> __x,
571 basic_string_view<_CharT, _Traits> __y) noexcept
572 {
return !(__x == __y); }
574 template<
typename _CharT,
typename _Traits>
576 operator!=(basic_string_view<_CharT, _Traits> __x,
577 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
579 {
return !(__x == __y); }
581 template<
typename _CharT,
typename _Traits>
583 operator!=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
584 basic_string_view<_CharT, _Traits> __y) noexcept
585 {
return !(__x == __y); }
587 template<
typename _CharT,
typename _Traits>
589 operator< (basic_string_view<_CharT, _Traits> __x,
590 basic_string_view<_CharT, _Traits> __y) noexcept
591 {
return __x.compare(__y) < 0; }
593 template<
typename _CharT,
typename _Traits>
595 operator< (basic_string_view<_CharT, _Traits> __x,
596 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
598 {
return __x.compare(__y) < 0; }
600 template<
typename _CharT,
typename _Traits>
602 operator< (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
603 basic_string_view<_CharT, _Traits> __y) noexcept
604 {
return __x.compare(__y) < 0; }
606 template<
typename _CharT,
typename _Traits>
608 operator> (basic_string_view<_CharT, _Traits> __x,
609 basic_string_view<_CharT, _Traits> __y) noexcept
610 {
return __x.compare(__y) > 0; }
612 template<
typename _CharT,
typename _Traits>
614 operator> (basic_string_view<_CharT, _Traits> __x,
615 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
617 {
return __x.compare(__y) > 0; }
619 template<
typename _CharT,
typename _Traits>
621 operator> (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
622 basic_string_view<_CharT, _Traits> __y) noexcept
623 {
return __x.compare(__y) > 0; }
625 template<
typename _CharT,
typename _Traits>
627 operator<=(basic_string_view<_CharT, _Traits> __x,
628 basic_string_view<_CharT, _Traits> __y) noexcept
629 {
return __x.compare(__y) <= 0; }
631 template<
typename _CharT,
typename _Traits>
633 operator<=(basic_string_view<_CharT, _Traits> __x,
634 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
636 {
return __x.compare(__y) <= 0; }
638 template<
typename _CharT,
typename _Traits>
640 operator<=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
641 basic_string_view<_CharT, _Traits> __y) noexcept
642 {
return __x.compare(__y) <= 0; }
644 template<
typename _CharT,
typename _Traits>
646 operator>=(basic_string_view<_CharT, _Traits> __x,
647 basic_string_view<_CharT, _Traits> __y) noexcept
648 {
return __x.compare(__y) >= 0; }
650 template<
typename _CharT,
typename _Traits>
652 operator>=(basic_string_view<_CharT, _Traits> __x,
653 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
655 {
return __x.compare(__y) >= 0; }
657 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.compare(__y) >= 0; }
665 template<
typename _CharT,
typename _Traits>
666 inline basic_ostream<_CharT, _Traits>&
667 operator<<(basic_ostream<_CharT, _Traits>& __os,
668 basic_string_view<_CharT,_Traits> __str)
669 {
return __ostream_insert(__os, __str.data(), __str.size()); }
674 using string_view = basic_string_view<char>;
675 #ifdef _GLIBCXX_USE_WCHAR_T
676 using wstring_view = basic_string_view<wchar_t>;
678 #ifdef _GLIBCXX_USE_CHAR8_T
679 using u8string_view = basic_string_view<char8_t>;
681 using u16string_view = basic_string_view<char16_t>;
682 using u32string_view = basic_string_view<char32_t>;
686 template<
typename _Tp>
690 struct hash<string_view>
691 :
public __hash_base<size_t, string_view>
694 operator()(
const string_view& __str)
const noexcept
695 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
702 #ifdef _GLIBCXX_USE_WCHAR_T
704 struct hash<wstring_view>
705 :
public __hash_base<size_t, wstring_view>
708 operator()(
const wstring_view& __s)
const noexcept
709 {
return std::_Hash_impl::hash(__s.data(),
710 __s.length() *
sizeof(
wchar_t)); }
718 #ifdef _GLIBCXX_USE_CHAR8_T
720 struct hash<u8string_view>
721 :
public __hash_base<size_t, u8string_view>
724 operator()(
const u8string_view& __str)
const noexcept
725 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
734 struct hash<u16string_view>
735 :
public __hash_base<size_t, u16string_view>
738 operator()(
const u16string_view& __s)
const noexcept
739 {
return std::_Hash_impl::hash(__s.data(),
740 __s.length() *
sizeof(char16_t)); }
748 struct hash<u32string_view>
749 :
public __hash_base<size_t, u32string_view>
752 operator()(
const u32string_view& __s)
const noexcept
753 {
return std::_Hash_impl::hash(__s.data(),
754 __s.length() *
sizeof(char32_t)); }
761 inline namespace literals
763 inline namespace string_view_literals
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}; }
771 #ifdef _GLIBCXX_USE_WCHAR_T
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}; }
777 #ifdef _GLIBCXX_USE_CHAR8_T
778 inline constexpr basic_string_view<char8_t>
779 operator""sv(
const char8_t* __str,
size_t __len) noexcept
780 {
return basic_string_view<char8_t>{__str, __len}; }
783 inline constexpr basic_string_view<char16_t>
784 operator""sv(
const char16_t* __str,
size_t __len) noexcept
785 {
return basic_string_view<char16_t>{__str, __len}; }
787 inline constexpr basic_string_view<char32_t>
788 operator""sv(
const char32_t* __str,
size_t __len) noexcept
789 {
return basic_string_view<char32_t>{__str, __len}; }
791 #pragma GCC diagnostic pop
795 #if __cpp_lib_concepts
799 template<
typename _CharT,
typename _Traits>
800 inline constexpr
bool
801 enable_borrowed_range<basic_string_view<_CharT, _Traits>> =
true;
804 template<
typename _CharT,
typename _Traits>
805 inline constexpr
bool
806 enable_view<basic_string_view<_CharT, _Traits>> =
true;
809 _GLIBCXX_END_NAMESPACE_VERSION
812 #include <bits/string_view.tcc>
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.
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
constexpr auto data(_Container &__cont) noexcept(noexcept(__cont.data())) -> decltype(__cont.data())
Return the data pointer of a container.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.
A non-owning reference to a string.