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 201803
55 __sv_check(
size_t __size,
size_t __pos,
const char* __s)
58 __throw_out_of_range_fmt(__N(
"%s: __pos (which is %zu) > __size "
59 "(which is %zu)"), __s, __pos, __size);
66 __sv_limit(
size_t __size,
size_t __pos,
size_t __off) noexcept
68 const bool __testoff = __off < __size - __pos;
69 return __testoff ? __off : __size - __pos;
90 template<
typename _CharT,
typename _Traits = std::
char_traits<_CharT>>
91 class basic_string_view
93 static_assert(!is_array_v<_CharT>);
94 static_assert(is_trivial_v<_CharT> && is_standard_layout_v<_CharT>);
95 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
100 using traits_type = _Traits;
101 using value_type = _CharT;
102 using pointer = value_type*;
103 using const_pointer =
const value_type*;
104 using reference = value_type&;
105 using const_reference =
const value_type&;
106 using const_iterator =
const value_type*;
107 using iterator = const_iterator;
109 using reverse_iterator = const_reverse_iterator;
110 using size_type = size_t;
111 using difference_type = ptrdiff_t;
112 static constexpr size_type npos = size_type(-1);
117 basic_string_view() noexcept
118 : _M_len{0}, _M_str{
nullptr}
121 constexpr basic_string_view(
const basic_string_view&) noexcept =
default;
123 __attribute__((__nonnull__)) constexpr
124 basic_string_view(
const _CharT* __str) noexcept
125 : _M_len{traits_type::length(__str)},
130 basic_string_view(
const _CharT* __str, size_type __len) noexcept
131 : _M_len{__len}, _M_str{__str}
134 #if __cplusplus > 201703L && __cpp_lib_concepts
135 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
136 requires same_as<iter_value_t<_It>, _CharT>
137 && (!convertible_to<_End, size_type>)
139 basic_string_view(_It __first, _End __last)
140 : _M_len(__last - __first), _M_str(
std::to_address(__first))
144 constexpr basic_string_view&
145 operator=(
const basic_string_view&) noexcept =
default;
149 constexpr const_iterator
150 begin() const noexcept
151 {
return this->_M_str; }
153 constexpr const_iterator
155 {
return this->_M_str + this->_M_len; }
157 constexpr const_iterator
159 {
return this->_M_str; }
161 constexpr const_iterator
162 cend() const noexcept
163 {
return this->_M_str + this->_M_len; }
165 constexpr const_reverse_iterator
167 {
return const_reverse_iterator(this->
end()); }
169 constexpr const_reverse_iterator
170 rend() const noexcept
171 {
return const_reverse_iterator(this->
begin()); }
173 constexpr const_reverse_iterator
175 {
return const_reverse_iterator(this->
end()); }
177 constexpr const_reverse_iterator
178 crend() const noexcept
179 {
return const_reverse_iterator(this->
begin()); }
184 size() const noexcept
185 {
return this->_M_len; }
188 length() const noexcept
192 max_size() const noexcept
194 return (npos -
sizeof(size_type) -
sizeof(
void*))
195 /
sizeof(value_type) / 4;
198 [[nodiscard]] constexpr
bool
199 empty() const noexcept
200 {
return this->_M_len == 0; }
204 constexpr const_reference
205 operator[](size_type __pos)
const noexcept
209 return *(this->_M_str + __pos);
212 constexpr const_reference
213 at(size_type __pos)
const
216 __throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
217 "(which is %zu) >= this->size() "
218 "(which is %zu)"), __pos, this->size());
219 return *(this->_M_str + __pos);
222 constexpr const_reference
223 front() const noexcept
227 return *this->_M_str;
230 constexpr const_reference
231 back() const noexcept
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
326 starts_with(basic_string_view __x)
const noexcept
327 {
return this->substr(0, __x.size()) == __x; }
330 starts_with(_CharT __x)
const noexcept
331 {
return !this->empty() && traits_type::eq(this->front(), __x); }
334 starts_with(
const _CharT* __x)
const noexcept
335 {
return this->starts_with(basic_string_view(__x)); }
338 ends_with(basic_string_view __x)
const noexcept
340 return this->size() >= __x.size()
341 && this->compare(this->size() - __x.size(), npos, __x) == 0;
345 ends_with(_CharT __x)
const noexcept
346 {
return !this->empty() && traits_type::eq(this->back(), __x); }
349 ends_with(
const _CharT* __x)
const noexcept
350 {
return this->ends_with(basic_string_view(__x)); }
356 find(basic_string_view __str, size_type __pos = 0) const noexcept
357 {
return this->find(__str._M_str, __pos, __str._M_len); }
360 find(_CharT __c, size_type __pos = 0) const noexcept;
363 find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
365 __attribute__((__nonnull__)) constexpr size_type
366 find(const _CharT* __str, size_type __pos = 0) const noexcept
367 {
return this->find(__str, __pos, traits_type::length(__str)); }
370 rfind(basic_string_view __str, size_type __pos = npos)
const noexcept
371 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
374 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
377 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
379 __attribute__((__nonnull__)) constexpr size_type
380 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
381 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
384 find_first_of(basic_string_view __str, size_type __pos = 0) const noexcept
385 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
388 find_first_of(_CharT __c, size_type __pos = 0) const noexcept
389 {
return this->find(__c, __pos); }
392 find_first_of(
const _CharT* __str, size_type __pos,
393 size_type __n)
const noexcept;
395 __attribute__((__nonnull__)) constexpr size_type
396 find_first_of(
const _CharT* __str, size_type __pos = 0) const noexcept
397 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
400 find_last_of(basic_string_view __str,
401 size_type __pos = npos)
const noexcept
402 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
405 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
406 {
return this->rfind(__c, __pos); }
409 find_last_of(
const _CharT* __str, size_type __pos,
410 size_type __n)
const noexcept;
412 __attribute__((__nonnull__)) constexpr size_type
413 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
414 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
417 find_first_not_of(basic_string_view __str,
418 size_type __pos = 0) const noexcept
419 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
422 find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
425 find_first_not_of(const _CharT* __str,
426 size_type __pos, size_type __n) const noexcept;
428 __attribute__((__nonnull__)) constexpr size_type
429 find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
431 return this->find_first_not_of(__str, __pos,
432 traits_type::length(__str));
436 find_last_not_of(basic_string_view __str,
437 size_type __pos = npos)
const noexcept
438 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
441 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
444 find_last_not_of(
const _CharT* __str,
445 size_type __pos, size_type __n)
const noexcept;
447 __attribute__((__nonnull__)) constexpr size_type
448 find_last_not_of(
const _CharT* __str,
449 size_type __pos = npos)
const noexcept
451 return this->find_last_not_of(__str, __pos,
452 traits_type::length(__str));
458 _S_compare(size_type __n1, size_type __n2) noexcept
460 const difference_type __diff = __n1 - __n2;
465 return static_cast<int>(__diff);
469 const _CharT* _M_str;
472 #if __cplusplus > 201703L && __cpp_lib_concepts && __cpp_deduction_guides
473 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
474 basic_string_view(_It, _End) -> basic_string_view<iter_value_t<_It>>;
484 template<
typename _CharT,
typename _Traits>
486 operator==(basic_string_view<_CharT, _Traits> __x,
487 basic_string_view<_CharT, _Traits> __y) noexcept
488 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
490 template<
typename _CharT,
typename _Traits>
492 operator==(basic_string_view<_CharT, _Traits> __x,
493 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
495 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
497 template<
typename _CharT,
typename _Traits>
499 operator==(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
500 basic_string_view<_CharT, _Traits> __y) noexcept
501 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
503 template<
typename _CharT,
typename _Traits>
505 operator!=(basic_string_view<_CharT, _Traits> __x,
506 basic_string_view<_CharT, _Traits> __y) noexcept
507 {
return !(__x == __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 {
return !(__x == __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 == __y); }
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.compare(__y) < 0; }
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.compare(__y) < 0; }
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.compare(__y) < 0; }
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; }
599 template<
typename _CharT,
typename _Traits>
600 inline basic_ostream<_CharT, _Traits>&
601 operator<<(basic_ostream<_CharT, _Traits>& __os,
602 basic_string_view<_CharT,_Traits> __str)
603 {
return __ostream_insert(__os, __str.data(), __str.size()); }
608 using string_view = basic_string_view<char>;
609 #ifdef _GLIBCXX_USE_WCHAR_T
610 using wstring_view = basic_string_view<wchar_t>;
612 #ifdef _GLIBCXX_USE_CHAR8_T
613 using u8string_view = basic_string_view<char8_t>;
615 using u16string_view = basic_string_view<char16_t>;
616 using u32string_view = basic_string_view<char32_t>;
620 template<
typename _Tp>
624 struct hash<string_view>
625 :
public __hash_base<size_t, string_view>
628 operator()(
const string_view& __str)
const noexcept
629 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
636 #ifdef _GLIBCXX_USE_WCHAR_T
638 struct hash<wstring_view>
639 :
public __hash_base<size_t, wstring_view>
642 operator()(
const wstring_view& __s)
const noexcept
643 {
return std::_Hash_impl::hash(__s.data(),
644 __s.length() *
sizeof(
wchar_t)); }
652 #ifdef _GLIBCXX_USE_CHAR8_T
654 struct hash<u8string_view>
655 :
public __hash_base<size_t, u8string_view>
658 operator()(
const u8string_view& __str)
const noexcept
659 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
668 struct hash<u16string_view>
669 :
public __hash_base<size_t, u16string_view>
672 operator()(
const u16string_view& __s)
const noexcept
673 {
return std::_Hash_impl::hash(__s.data(),
674 __s.length() *
sizeof(char16_t)); }
682 struct hash<u32string_view>
683 :
public __hash_base<size_t, u32string_view>
686 operator()(
const u32string_view& __s)
const noexcept
687 {
return std::_Hash_impl::hash(__s.data(),
688 __s.length() *
sizeof(char32_t)); }
695 inline namespace literals
697 inline namespace string_view_literals
699 #pragma GCC diagnostic push
700 #pragma GCC diagnostic ignored "-Wliteral-suffix"
701 inline constexpr basic_string_view<char>
702 operator""sv(
const char* __str,
size_t __len) noexcept
703 {
return basic_string_view<char>{__str, __len}; }
705 #ifdef _GLIBCXX_USE_WCHAR_T
706 inline constexpr basic_string_view<wchar_t>
707 operator""sv(
const wchar_t* __str,
size_t __len) noexcept
708 {
return basic_string_view<wchar_t>{__str, __len}; }
711 #ifdef _GLIBCXX_USE_CHAR8_T
712 inline constexpr basic_string_view<char8_t>
713 operator""sv(
const char8_t* __str,
size_t __len) noexcept
714 {
return basic_string_view<char8_t>{__str, __len}; }
717 inline constexpr basic_string_view<char16_t>
718 operator""sv(
const char16_t* __str,
size_t __len) noexcept
719 {
return basic_string_view<char16_t>{__str, __len}; }
721 inline constexpr basic_string_view<char32_t>
722 operator""sv(
const char32_t* __str,
size_t __len) noexcept
723 {
return basic_string_view<char32_t>{__str, __len}; }
725 #pragma GCC diagnostic pop
729 #if __cpp_lib_concepts
733 template<
typename _CharT,
typename _Traits>
734 inline constexpr
bool
735 enable_borrowed_range<basic_string_view<_CharT, _Traits>> =
true;
738 template<
typename _CharT,
typename _Traits>
739 inline constexpr
bool
740 enable_view<basic_string_view<_CharT, _Traits>> =
true;
743 _GLIBCXX_END_NAMESPACE_VERSION
746 #include <bits/string_view.tcc>
748 #endif // __cplusplus <= 201402L
750 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW