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
210 __glibcxx_assert(__pos < this->_M_len);
211 return *(this->_M_str + __pos);
214 constexpr const_reference
215 at(size_type __pos)
const
218 __throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
219 "(which is %zu) >= this->size() "
220 "(which is %zu)"), __pos, this->size());
221 return *(this->_M_str + __pos);
224 constexpr const_reference
225 front() const noexcept
227 __glibcxx_assert(this->_M_len > 0);
228 return *this->_M_str;
231 constexpr const_reference
232 back() const noexcept
234 __glibcxx_assert(this->_M_len > 0);
235 return *(this->_M_str + this->_M_len - 1);
238 constexpr const_pointer
239 data() const noexcept
240 {
return this->_M_str; }
245 remove_prefix(size_type __n) noexcept
247 __glibcxx_assert(this->_M_len >= __n);
253 remove_suffix(size_type __n) noexcept
254 { this->_M_len -= __n; }
257 swap(basic_string_view& __sv) noexcept
268 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
270 __glibcxx_requires_string_len(__str, __n);
271 __pos = std::__sv_check(size(), __pos,
"basic_string_view::copy");
272 const size_type __rlen =
std::min(__n, _M_len - __pos);
275 traits_type::copy(__str, data() + __pos, __rlen);
279 constexpr basic_string_view
280 substr(size_type __pos = 0, size_type __n = npos)
const noexcept(
false)
282 __pos = std::__sv_check(size(), __pos,
"basic_string_view::substr");
283 const size_type __rlen =
std::min(__n, _M_len - __pos);
284 return basic_string_view{_M_str + __pos, __rlen};
288 compare(basic_string_view __str)
const noexcept
290 const size_type __rlen =
std::min(this->_M_len, __str._M_len);
291 int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
293 __ret = _S_compare(this->_M_len, __str._M_len);
298 compare(size_type __pos1, size_type __n1, basic_string_view __str)
const
299 {
return this->substr(__pos1, __n1).compare(__str); }
302 compare(size_type __pos1, size_type __n1,
303 basic_string_view __str, size_type __pos2, size_type __n2)
const
305 return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
308 __attribute__((__nonnull__)) constexpr
int
309 compare(
const _CharT* __str)
const noexcept
310 {
return this->compare(basic_string_view{__str}); }
312 __attribute__((__nonnull__)) constexpr
int
313 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
314 {
return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
317 compare(size_type __pos1, size_type __n1,
318 const _CharT* __str, size_type __n2)
const noexcept(
false)
320 return this->substr(__pos1, __n1)
321 .compare(basic_string_view(__str, __n2));
324 #if __cplusplus > 201703L
325 #define __cpp_lib_starts_ends_with 201711L
327 starts_with(basic_string_view __x)
const noexcept
328 {
return this->substr(0, __x.size()) == __x; }
331 starts_with(_CharT __x)
const noexcept
332 {
return !this->empty() && traits_type::eq(this->front(), __x); }
335 starts_with(
const _CharT* __x)
const noexcept
336 {
return this->starts_with(basic_string_view(__x)); }
339 ends_with(basic_string_view __x)
const noexcept
341 const auto __len = this->size();
342 const auto __xlen = __x.size();
343 return __len >= __xlen
344 && traits_type::compare(
end() - __xlen, __x.data(), __xlen) == 0;
348 ends_with(_CharT __x)
const noexcept
349 {
return !this->empty() && traits_type::eq(this->back(), __x); }
352 ends_with(
const _CharT* __x)
const noexcept
353 {
return this->ends_with(basic_string_view(__x)); }
359 find(basic_string_view __str, size_type __pos = 0) const noexcept
360 {
return this->find(__str._M_str, __pos, __str._M_len); }
363 find(_CharT __c, size_type __pos = 0) const noexcept;
366 find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
368 __attribute__((__nonnull__)) constexpr size_type
369 find(const _CharT* __str, size_type __pos = 0) const noexcept
370 {
return this->find(__str, __pos, traits_type::length(__str)); }
373 rfind(basic_string_view __str, size_type __pos = npos)
const noexcept
374 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
377 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
380 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
382 __attribute__((__nonnull__)) constexpr size_type
383 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
384 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
387 find_first_of(basic_string_view __str, size_type __pos = 0) const noexcept
388 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
391 find_first_of(_CharT __c, size_type __pos = 0) const noexcept
392 {
return this->find(__c, __pos); }
395 find_first_of(
const _CharT* __str, size_type __pos,
396 size_type __n)
const noexcept;
398 __attribute__((__nonnull__)) constexpr size_type
399 find_first_of(
const _CharT* __str, size_type __pos = 0) const noexcept
400 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
403 find_last_of(basic_string_view __str,
404 size_type __pos = npos)
const noexcept
405 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
408 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
409 {
return this->rfind(__c, __pos); }
412 find_last_of(
const _CharT* __str, size_type __pos,
413 size_type __n)
const noexcept;
415 __attribute__((__nonnull__)) constexpr size_type
416 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
417 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
420 find_first_not_of(basic_string_view __str,
421 size_type __pos = 0) const noexcept
422 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
425 find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
428 find_first_not_of(const _CharT* __str,
429 size_type __pos, size_type __n) const noexcept;
431 __attribute__((__nonnull__)) constexpr size_type
432 find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
434 return this->find_first_not_of(__str, __pos,
435 traits_type::length(__str));
439 find_last_not_of(basic_string_view __str,
440 size_type __pos = npos)
const noexcept
441 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
444 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
447 find_last_not_of(
const _CharT* __str,
448 size_type __pos, size_type __n)
const noexcept;
450 __attribute__((__nonnull__)) constexpr size_type
451 find_last_not_of(
const _CharT* __str,
452 size_type __pos = npos)
const noexcept
454 return this->find_last_not_of(__str, __pos,
455 traits_type::length(__str));
461 _S_compare(size_type __n1, size_type __n2) noexcept
463 const difference_type __diff = __n1 - __n2;
468 return static_cast<int>(__diff);
472 const _CharT* _M_str;
475 #if __cplusplus > 201703L && __cpp_lib_concepts && __cpp_deduction_guides
476 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
477 basic_string_view(_It, _End) -> basic_string_view<iter_value_t<_It>>;
487 template<
typename _CharT,
typename _Traits>
489 operator==(basic_string_view<_CharT, _Traits> __x,
490 basic_string_view<_CharT, _Traits> __y) noexcept
491 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
493 template<
typename _CharT,
typename _Traits>
495 operator==(basic_string_view<_CharT, _Traits> __x,
496 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
498 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
500 #if __cpp_lib_three_way_comparison
501 template<
typename _CharT,
typename _Traits>
503 operator<=>(basic_string_view<_CharT, _Traits> __x,
504 basic_string_view<_CharT, _Traits> __y) noexcept
505 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
506 {
return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
508 template<
typename _CharT,
typename _Traits>
510 operator<=>(basic_string_view<_CharT, _Traits> __x,
511 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
513 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
514 {
return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
516 template<
typename _CharT,
typename _Traits>
518 operator==(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
519 basic_string_view<_CharT, _Traits> __y) noexcept
520 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
522 template<
typename _CharT,
typename _Traits>
524 operator!=(basic_string_view<_CharT, _Traits> __x,
525 basic_string_view<_CharT, _Traits> __y) noexcept
526 {
return !(__x == __y); }
528 template<
typename _CharT,
typename _Traits>
530 operator!=(basic_string_view<_CharT, _Traits> __x,
531 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
533 {
return !(__x == __y); }
535 template<
typename _CharT,
typename _Traits>
537 operator!=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
538 basic_string_view<_CharT, _Traits> __y) noexcept
539 {
return !(__x == __y); }
541 template<
typename _CharT,
typename _Traits>
543 operator< (basic_string_view<_CharT, _Traits> __x,
544 basic_string_view<_CharT, _Traits> __y) noexcept
545 {
return __x.compare(__y) < 0; }
547 template<
typename _CharT,
typename _Traits>
549 operator< (basic_string_view<_CharT, _Traits> __x,
550 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
552 {
return __x.compare(__y) < 0; }
554 template<
typename _CharT,
typename _Traits>
556 operator< (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
557 basic_string_view<_CharT, _Traits> __y) noexcept
558 {
return __x.compare(__y) < 0; }
560 template<
typename _CharT,
typename _Traits>
562 operator> (basic_string_view<_CharT, _Traits> __x,
563 basic_string_view<_CharT, _Traits> __y) noexcept
564 {
return __x.compare(__y) > 0; }
566 template<
typename _CharT,
typename _Traits>
568 operator> (basic_string_view<_CharT, _Traits> __x,
569 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
571 {
return __x.compare(__y) > 0; }
573 template<
typename _CharT,
typename _Traits>
575 operator> (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
576 basic_string_view<_CharT, _Traits> __y) noexcept
577 {
return __x.compare(__y) > 0; }
579 template<
typename _CharT,
typename _Traits>
581 operator<=(basic_string_view<_CharT, _Traits> __x,
582 basic_string_view<_CharT, _Traits> __y) noexcept
583 {
return __x.compare(__y) <= 0; }
585 template<
typename _CharT,
typename _Traits>
587 operator<=(basic_string_view<_CharT, _Traits> __x,
588 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
590 {
return __x.compare(__y) <= 0; }
592 template<
typename _CharT,
typename _Traits>
594 operator<=(__type_identity_t<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 basic_string_view<_CharT, _Traits> __y) noexcept
602 {
return __x.compare(__y) >= 0; }
604 template<
typename _CharT,
typename _Traits>
606 operator>=(basic_string_view<_CharT, _Traits> __x,
607 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
609 {
return __x.compare(__y) >= 0; }
611 template<
typename _CharT,
typename _Traits>
613 operator>=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
614 basic_string_view<_CharT, _Traits> __y) noexcept
615 {
return __x.compare(__y) >= 0; }
619 template<
typename _CharT,
typename _Traits>
620 inline basic_ostream<_CharT, _Traits>&
621 operator<<(basic_ostream<_CharT, _Traits>& __os,
622 basic_string_view<_CharT,_Traits> __str)
623 {
return __ostream_insert(__os, __str.data(), __str.size()); }
628 using string_view = basic_string_view<char>;
629 #ifdef _GLIBCXX_USE_WCHAR_T
630 using wstring_view = basic_string_view<wchar_t>;
632 #ifdef _GLIBCXX_USE_CHAR8_T
633 using u8string_view = basic_string_view<char8_t>;
635 using u16string_view = basic_string_view<char16_t>;
636 using u32string_view = basic_string_view<char32_t>;
640 template<
typename _Tp>
644 struct hash<string_view>
645 :
public __hash_base<size_t, string_view>
648 operator()(
const string_view& __str)
const noexcept
649 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
656 #ifdef _GLIBCXX_USE_WCHAR_T
658 struct hash<wstring_view>
659 :
public __hash_base<size_t, wstring_view>
662 operator()(
const wstring_view& __s)
const noexcept
663 {
return std::_Hash_impl::hash(__s.data(),
664 __s.length() *
sizeof(
wchar_t)); }
672 #ifdef _GLIBCXX_USE_CHAR8_T
674 struct hash<u8string_view>
675 :
public __hash_base<size_t, u8string_view>
678 operator()(
const u8string_view& __str)
const noexcept
679 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
688 struct hash<u16string_view>
689 :
public __hash_base<size_t, u16string_view>
692 operator()(
const u16string_view& __s)
const noexcept
693 {
return std::_Hash_impl::hash(__s.data(),
694 __s.length() *
sizeof(char16_t)); }
702 struct hash<u32string_view>
703 :
public __hash_base<size_t, u32string_view>
706 operator()(
const u32string_view& __s)
const noexcept
707 {
return std::_Hash_impl::hash(__s.data(),
708 __s.length() *
sizeof(char32_t)); }
715 inline namespace literals
717 inline namespace string_view_literals
719 #pragma GCC diagnostic push
720 #pragma GCC diagnostic ignored "-Wliteral-suffix"
721 inline constexpr basic_string_view<char>
722 operator""sv(
const char* __str,
size_t __len) noexcept
723 {
return basic_string_view<char>{__str, __len}; }
725 #ifdef _GLIBCXX_USE_WCHAR_T
726 inline constexpr basic_string_view<wchar_t>
727 operator""sv(
const wchar_t* __str,
size_t __len) noexcept
728 {
return basic_string_view<wchar_t>{__str, __len}; }
731 #ifdef _GLIBCXX_USE_CHAR8_T
732 inline constexpr basic_string_view<char8_t>
733 operator""sv(
const char8_t* __str,
size_t __len) noexcept
734 {
return basic_string_view<char8_t>{__str, __len}; }
737 inline constexpr basic_string_view<char16_t>
738 operator""sv(
const char16_t* __str,
size_t __len) noexcept
739 {
return basic_string_view<char16_t>{__str, __len}; }
741 inline constexpr basic_string_view<char32_t>
742 operator""sv(
const char32_t* __str,
size_t __len) noexcept
743 {
return basic_string_view<char32_t>{__str, __len}; }
745 #pragma GCC diagnostic pop
749 #if __cpp_lib_concepts
753 template<
typename _CharT,
typename _Traits>
754 inline constexpr
bool
755 enable_borrowed_range<basic_string_view<_CharT, _Traits>> =
true;
758 template<
typename _CharT,
typename _Traits>
759 inline constexpr
bool
760 enable_view<basic_string_view<_CharT, _Traits>> =
true;
763 _GLIBCXX_END_NAMESPACE_VERSION
766 #include <bits/string_view.tcc>
_Tp * end(valarray< _Tp > &__va) noexcept
Return an iterator pointing to one past the last element of the valarray.
_Tp * begin(valarray< _Tp > &__va) noexcept
Return an iterator pointing to the first element of the valarray.
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 crend(const _Container &__cont) -> decltype(std::rend(__cont))
Return a reverse iterator pointing one past the first element of the const container.
constexpr auto rend(_Container &__cont) -> decltype(__cont.rend())
Return a reverse iterator pointing one past the first element of the container.
constexpr auto cend(const _Container &__cont) noexcept(noexcept(std::end(__cont))) -> decltype(std::end(__cont))
Return an iterator pointing to one past the last element of the const container.
constexpr auto rbegin(_Container &__cont) -> decltype(__cont.rbegin())
Return a reverse iterator pointing to the last element of the container.
constexpr auto crbegin(const _Container &__cont) -> decltype(std::rbegin(__cont))
Return a reverse iterator pointing to the last element of the const container.
constexpr auto cbegin(const _Container &__cont) noexcept(noexcept(std::begin(__cont))) -> decltype(std::begin(__cont))
Return an iterator pointing to the first element of the const container.
__numeric_traits_integer< _Tp > __int_traits
Convenience alias for __numeric_traits<integer-type>.