33 #ifndef _GLIBCXX_STRING_VIEW
34 #define _GLIBCXX_STRING_VIEW 1
36 #pragma GCC system_header
38 #if __cplusplus >= 201703L
47 namespace std _GLIBCXX_VISIBILITY(default)
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
51 # define __cpp_lib_string_view 201803L
52 #if __cplusplus > 201703L
53 # define __cpp_lib_constexpr_string_view 201811L
58 __sv_check(
size_t __size,
size_t __pos,
const char* __s)
61 __throw_out_of_range_fmt(__N(
"%s: __pos (which is %zu) > __size "
62 "(which is %zu)"), __s, __pos, __size);
69 __sv_limit(
size_t __size,
size_t __pos,
size_t __off) noexcept
71 const bool __testoff = __off < __size - __pos;
72 return __testoff ? __off : __size - __pos;
93 template<
typename _CharT,
typename _Traits = std::
char_traits<_CharT>>
94 class basic_string_view
96 static_assert(!is_array_v<_CharT>);
97 static_assert(is_trivial_v<_CharT> && is_standard_layout_v<_CharT>);
98 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
103 using traits_type = _Traits;
104 using value_type = _CharT;
105 using pointer = value_type*;
106 using const_pointer =
const value_type*;
107 using reference = value_type&;
108 using const_reference =
const value_type&;
109 using const_iterator =
const value_type*;
110 using iterator = const_iterator;
112 using reverse_iterator = const_reverse_iterator;
113 using size_type = size_t;
114 using difference_type = ptrdiff_t;
115 static constexpr size_type npos = size_type(-1);
120 basic_string_view() noexcept
121 : _M_len{0}, _M_str{
nullptr}
124 constexpr basic_string_view(
const basic_string_view&) noexcept =
default;
126 __attribute__((__nonnull__)) constexpr
127 basic_string_view(
const _CharT* __str) noexcept
128 : _M_len{traits_type::length(__str)},
133 basic_string_view(
const _CharT* __str, size_type __len) noexcept
134 : _M_len{__len}, _M_str{__str}
137 #if __cplusplus > 201703L && __cpp_lib_concepts
138 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
139 requires same_as<iter_value_t<_It>, _CharT>
140 && (!convertible_to<_End, size_type>)
142 basic_string_view(_It __first, _End __last)
143 : _M_len(__last - __first), _M_str(
std::to_address(__first))
147 constexpr basic_string_view&
148 operator=(
const basic_string_view&) noexcept =
default;
152 constexpr const_iterator
153 begin() const noexcept
154 {
return this->_M_str; }
156 constexpr const_iterator
158 {
return this->_M_str + this->_M_len; }
160 constexpr const_iterator
162 {
return this->_M_str; }
164 constexpr const_iterator
165 cend() const noexcept
166 {
return this->_M_str + this->_M_len; }
168 constexpr const_reverse_iterator
170 {
return const_reverse_iterator(this->
end()); }
172 constexpr const_reverse_iterator
173 rend() const noexcept
174 {
return const_reverse_iterator(this->
begin()); }
176 constexpr const_reverse_iterator
178 {
return const_reverse_iterator(this->
end()); }
180 constexpr const_reverse_iterator
181 crend() const noexcept
182 {
return const_reverse_iterator(this->
begin()); }
187 size() const noexcept
188 {
return this->_M_len; }
191 length() const noexcept
195 max_size() const noexcept
197 return (npos -
sizeof(size_type) -
sizeof(
void*))
198 /
sizeof(value_type) / 4;
201 [[nodiscard]] constexpr
bool
202 empty() const noexcept
203 {
return this->_M_len == 0; }
207 constexpr const_reference
208 operator[](size_type __pos)
const noexcept
212 return *(this->_M_str + __pos);
215 constexpr const_reference
216 at(size_type __pos)
const
219 __throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
220 "(which is %zu) >= this->size() "
221 "(which is %zu)"), __pos, this->size());
222 return *(this->_M_str + __pos);
225 constexpr const_reference
226 front() const noexcept
230 return *this->_M_str;
233 constexpr const_reference
234 back() const noexcept
238 return *(this->_M_str + this->_M_len - 1);
241 constexpr const_pointer
242 data() const noexcept
243 {
return this->_M_str; }
248 remove_prefix(size_type __n) noexcept
250 __glibcxx_assert(this->_M_len >= __n);
256 remove_suffix(size_type __n) noexcept
257 { this->_M_len -= __n; }
260 swap(basic_string_view& __sv) noexcept
271 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
273 __glibcxx_requires_string_len(__str, __n);
274 __pos = std::__sv_check(size(), __pos,
"basic_string_view::copy");
275 const size_type __rlen =
std::min(__n, _M_len - __pos);
278 traits_type::copy(__str, data() + __pos, __rlen);
282 constexpr basic_string_view
283 substr(size_type __pos = 0, size_type __n = npos)
const noexcept(
false)
285 __pos = std::__sv_check(size(), __pos,
"basic_string_view::substr");
286 const size_type __rlen =
std::min(__n, _M_len - __pos);
287 return basic_string_view{_M_str + __pos, __rlen};
291 compare(basic_string_view __str)
const noexcept
293 const size_type __rlen =
std::min(this->_M_len, __str._M_len);
294 int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
296 __ret = _S_compare(this->_M_len, __str._M_len);
301 compare(size_type __pos1, size_type __n1, basic_string_view __str)
const
302 {
return this->substr(__pos1, __n1).compare(__str); }
305 compare(size_type __pos1, size_type __n1,
306 basic_string_view __str, size_type __pos2, size_type __n2)
const
308 return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
311 __attribute__((__nonnull__)) constexpr
int
312 compare(
const _CharT* __str)
const noexcept
313 {
return this->compare(basic_string_view{__str}); }
315 __attribute__((__nonnull__)) constexpr
int
316 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
317 {
return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
320 compare(size_type __pos1, size_type __n1,
321 const _CharT* __str, size_type __n2)
const noexcept(
false)
323 return this->substr(__pos1, __n1)
324 .compare(basic_string_view(__str, __n2));
327 #if __cplusplus > 201703L
328 #define __cpp_lib_starts_ends_with 201711L
330 starts_with(basic_string_view __x)
const noexcept
331 {
return this->substr(0, __x.size()) == __x; }
334 starts_with(_CharT __x)
const noexcept
335 {
return !this->empty() && traits_type::eq(this->front(), __x); }
338 starts_with(
const _CharT* __x)
const noexcept
339 {
return this->starts_with(basic_string_view(__x)); }
342 ends_with(basic_string_view __x)
const noexcept
344 return this->size() >= __x.size()
345 && this->compare(this->size() - __x.size(), npos, __x) == 0;
349 ends_with(_CharT __x)
const noexcept
350 {
return !this->empty() && traits_type::eq(this->back(), __x); }
353 ends_with(
const _CharT* __x)
const noexcept
354 {
return this->ends_with(basic_string_view(__x)); }
360 find(basic_string_view __str, size_type __pos = 0) const noexcept
361 {
return this->find(__str._M_str, __pos, __str._M_len); }
364 find(_CharT __c, size_type __pos = 0) const noexcept;
367 find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
369 __attribute__((__nonnull__)) constexpr size_type
370 find(const _CharT* __str, size_type __pos = 0) const noexcept
371 {
return this->find(__str, __pos, traits_type::length(__str)); }
374 rfind(basic_string_view __str, size_type __pos = npos)
const noexcept
375 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
378 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
381 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
383 __attribute__((__nonnull__)) constexpr size_type
384 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
385 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
388 find_first_of(basic_string_view __str, size_type __pos = 0) const noexcept
389 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
392 find_first_of(_CharT __c, size_type __pos = 0) const noexcept
393 {
return this->find(__c, __pos); }
396 find_first_of(
const _CharT* __str, size_type __pos,
397 size_type __n)
const noexcept;
399 __attribute__((__nonnull__)) constexpr size_type
400 find_first_of(
const _CharT* __str, size_type __pos = 0) const noexcept
401 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
404 find_last_of(basic_string_view __str,
405 size_type __pos = npos)
const noexcept
406 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
409 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
410 {
return this->rfind(__c, __pos); }
413 find_last_of(
const _CharT* __str, size_type __pos,
414 size_type __n)
const noexcept;
416 __attribute__((__nonnull__)) constexpr size_type
417 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
418 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
421 find_first_not_of(basic_string_view __str,
422 size_type __pos = 0) const noexcept
423 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
426 find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
429 find_first_not_of(const _CharT* __str,
430 size_type __pos, size_type __n) const noexcept;
432 __attribute__((__nonnull__)) constexpr size_type
433 find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
435 return this->find_first_not_of(__str, __pos,
436 traits_type::length(__str));
440 find_last_not_of(basic_string_view __str,
441 size_type __pos = npos)
const noexcept
442 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
445 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
448 find_last_not_of(
const _CharT* __str,
449 size_type __pos, size_type __n)
const noexcept;
451 __attribute__((__nonnull__)) constexpr size_type
452 find_last_not_of(
const _CharT* __str,
453 size_type __pos = npos)
const noexcept
455 return this->find_last_not_of(__str, __pos,
456 traits_type::length(__str));
462 _S_compare(size_type __n1, size_type __n2) noexcept
464 const difference_type __diff = __n1 - __n2;
469 return static_cast<int>(__diff);
473 const _CharT* _M_str;
476 #if __cplusplus > 201703L && __cpp_lib_concepts && __cpp_deduction_guides
477 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
478 basic_string_view(_It, _End) -> basic_string_view<iter_value_t<_It>>;
488 template<
typename _CharT,
typename _Traits>
490 operator==(basic_string_view<_CharT, _Traits> __x,
491 basic_string_view<_CharT, _Traits> __y) noexcept
492 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
494 template<
typename _CharT,
typename _Traits>
496 operator==(basic_string_view<_CharT, _Traits> __x,
497 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
499 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
501 #if __cpp_lib_three_way_comparison
502 template<
typename _CharT,
typename _Traits>
504 operator<=>(basic_string_view<_CharT, _Traits> __x,
505 basic_string_view<_CharT, _Traits> __y) noexcept
506 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
507 {
return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
509 template<
typename _CharT,
typename _Traits>
511 operator<=>(basic_string_view<_CharT, _Traits> __x,
512 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
514 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
515 {
return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
517 template<
typename _CharT,
typename _Traits>
519 operator==(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
520 basic_string_view<_CharT, _Traits> __y) noexcept
521 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
523 template<
typename _CharT,
typename _Traits>
525 operator!=(basic_string_view<_CharT, _Traits> __x,
526 basic_string_view<_CharT, _Traits> __y) noexcept
527 {
return !(__x == __y); }
529 template<
typename _CharT,
typename _Traits>
531 operator!=(basic_string_view<_CharT, _Traits> __x,
532 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
534 {
return !(__x == __y); }
536 template<
typename _CharT,
typename _Traits>
538 operator!=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
539 basic_string_view<_CharT, _Traits> __y) noexcept
540 {
return !(__x == __y); }
542 template<
typename _CharT,
typename _Traits>
544 operator< (basic_string_view<_CharT, _Traits> __x,
545 basic_string_view<_CharT, _Traits> __y) noexcept
546 {
return __x.compare(__y) < 0; }
548 template<
typename _CharT,
typename _Traits>
550 operator< (basic_string_view<_CharT, _Traits> __x,
551 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
553 {
return __x.compare(__y) < 0; }
555 template<
typename _CharT,
typename _Traits>
557 operator< (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
558 basic_string_view<_CharT, _Traits> __y) noexcept
559 {
return __x.compare(__y) < 0; }
561 template<
typename _CharT,
typename _Traits>
563 operator> (basic_string_view<_CharT, _Traits> __x,
564 basic_string_view<_CharT, _Traits> __y) noexcept
565 {
return __x.compare(__y) > 0; }
567 template<
typename _CharT,
typename _Traits>
569 operator> (basic_string_view<_CharT, _Traits> __x,
570 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
572 {
return __x.compare(__y) > 0; }
574 template<
typename _CharT,
typename _Traits>
576 operator> (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
577 basic_string_view<_CharT, _Traits> __y) noexcept
578 {
return __x.compare(__y) > 0; }
580 template<
typename _CharT,
typename _Traits>
582 operator<=(basic_string_view<_CharT, _Traits> __x,
583 basic_string_view<_CharT, _Traits> __y) noexcept
584 {
return __x.compare(__y) <= 0; }
586 template<
typename _CharT,
typename _Traits>
588 operator<=(basic_string_view<_CharT, _Traits> __x,
589 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
591 {
return __x.compare(__y) <= 0; }
593 template<
typename _CharT,
typename _Traits>
595 operator<=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
596 basic_string_view<_CharT, _Traits> __y) noexcept
597 {
return __x.compare(__y) <= 0; }
599 template<
typename _CharT,
typename _Traits>
601 operator>=(basic_string_view<_CharT, _Traits> __x,
602 basic_string_view<_CharT, _Traits> __y) noexcept
603 {
return __x.compare(__y) >= 0; }
605 template<
typename _CharT,
typename _Traits>
607 operator>=(basic_string_view<_CharT, _Traits> __x,
608 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
610 {
return __x.compare(__y) >= 0; }
612 template<
typename _CharT,
typename _Traits>
614 operator>=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
615 basic_string_view<_CharT, _Traits> __y) noexcept
616 {
return __x.compare(__y) >= 0; }
617 #endif // three-way comparison
620 template<
typename _CharT,
typename _Traits>
621 inline basic_ostream<_CharT, _Traits>&
622 operator<<(basic_ostream<_CharT, _Traits>& __os,
623 basic_string_view<_CharT,_Traits> __str)
624 {
return __ostream_insert(__os, __str.data(), __str.size()); }
629 using string_view = basic_string_view<char>;
630 #ifdef _GLIBCXX_USE_WCHAR_T
631 using wstring_view = basic_string_view<wchar_t>;
633 #ifdef _GLIBCXX_USE_CHAR8_T
634 using u8string_view = basic_string_view<char8_t>;
636 using u16string_view = basic_string_view<char16_t>;
637 using u32string_view = basic_string_view<char32_t>;
641 template<
typename _Tp>
645 struct hash<string_view>
646 :
public __hash_base<size_t, string_view>
649 operator()(
const string_view& __str)
const noexcept
650 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
657 #ifdef _GLIBCXX_USE_WCHAR_T
659 struct hash<wstring_view>
660 :
public __hash_base<size_t, wstring_view>
663 operator()(
const wstring_view& __s)
const noexcept
664 {
return std::_Hash_impl::hash(__s.data(),
665 __s.length() *
sizeof(
wchar_t)); }
673 #ifdef _GLIBCXX_USE_CHAR8_T
675 struct hash<u8string_view>
676 :
public __hash_base<size_t, u8string_view>
679 operator()(
const u8string_view& __str)
const noexcept
680 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
689 struct hash<u16string_view>
690 :
public __hash_base<size_t, u16string_view>
693 operator()(
const u16string_view& __s)
const noexcept
694 {
return std::_Hash_impl::hash(__s.data(),
695 __s.length() *
sizeof(char16_t)); }
703 struct hash<u32string_view>
704 :
public __hash_base<size_t, u32string_view>
707 operator()(
const u32string_view& __s)
const noexcept
708 {
return std::_Hash_impl::hash(__s.data(),
709 __s.length() *
sizeof(char32_t)); }
716 inline namespace literals
718 inline namespace string_view_literals
720 #pragma GCC diagnostic push
721 #pragma GCC diagnostic ignored "-Wliteral-suffix"
722 inline constexpr basic_string_view<char>
723 operator""sv(
const char* __str,
size_t __len) noexcept
724 {
return basic_string_view<char>{__str, __len}; }
726 #ifdef _GLIBCXX_USE_WCHAR_T
727 inline constexpr basic_string_view<wchar_t>
728 operator""sv(
const wchar_t* __str,
size_t __len) noexcept
729 {
return basic_string_view<wchar_t>{__str, __len}; }
732 #ifdef _GLIBCXX_USE_CHAR8_T
733 inline constexpr basic_string_view<char8_t>
734 operator""sv(
const char8_t* __str,
size_t __len) noexcept
735 {
return basic_string_view<char8_t>{__str, __len}; }
738 inline constexpr basic_string_view<char16_t>
739 operator""sv(
const char16_t* __str,
size_t __len) noexcept
740 {
return basic_string_view<char16_t>{__str, __len}; }
742 inline constexpr basic_string_view<char32_t>
743 operator""sv(
const char32_t* __str,
size_t __len) noexcept
744 {
return basic_string_view<char32_t>{__str, __len}; }
746 #pragma GCC diagnostic pop
750 #if __cpp_lib_concepts
754 template<
typename _CharT,
typename _Traits>
755 inline constexpr
bool
756 enable_borrowed_range<basic_string_view<_CharT, _Traits>> =
true;
759 template<
typename _CharT,
typename _Traits>
760 inline constexpr
bool
761 enable_view<basic_string_view<_CharT, _Traits>> =
true;
764 _GLIBCXX_END_NAMESPACE_VERSION
767 #include <bits/string_view.tcc>
769 #endif // __cplusplus <= 201402L
771 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW