33 #ifndef _GLIBCXX_STRING_VIEW
34 #define _GLIBCXX_STRING_VIEW 1
36 #pragma GCC system_header
38 #if __cplusplus >= 201703L
46 namespace std _GLIBCXX_VISIBILITY(default)
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 #define __cpp_lib_string_view 201803
54 __sv_check(
size_t __size,
size_t __pos,
const char* __s)
57 __throw_out_of_range_fmt(__N(
"%s: __pos (which is %zu) > __size "
58 "(which is %zu)"), __s, __pos, __size);
65 __sv_limit(
size_t __size,
size_t __pos,
size_t __off) noexcept
67 const bool __testoff = __off < __size - __pos;
68 return __testoff ? __off : __size - __pos;
89 template<
typename _CharT,
typename _Traits = std::
char_traits<_CharT>>
90 class basic_string_view
92 static_assert(!is_array_v<_CharT>);
93 static_assert(is_trivial_v<_CharT> && is_standard_layout_v<_CharT>);
94 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
99 using traits_type = _Traits;
100 using value_type = _CharT;
101 using pointer = value_type*;
102 using const_pointer =
const value_type*;
103 using reference = value_type&;
104 using const_reference =
const value_type&;
105 using const_iterator =
const value_type*;
106 using iterator = const_iterator;
108 using reverse_iterator = const_reverse_iterator;
109 using size_type = size_t;
110 using difference_type = ptrdiff_t;
111 static constexpr size_type npos = size_type(-1);
116 basic_string_view() noexcept
117 : _M_len{0}, _M_str{
nullptr}
120 constexpr basic_string_view(
const basic_string_view&) noexcept =
default;
122 __attribute__((__nonnull__)) constexpr
123 basic_string_view(
const _CharT* __str) noexcept
124 : _M_len{traits_type::length(__str)},
129 basic_string_view(
const _CharT* __str, size_type __len) noexcept
130 : _M_len{__len}, _M_str{__str}
133 #if __cplusplus > 201703L && __cpp_lib_concepts
134 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
135 requires same_as<iter_value_t<_It>, _CharT>
136 && (!convertible_to<_End, size_type>)
138 basic_string_view(_It __first, _End __last)
139 : _M_len(__last - __first), _M_str(
std::to_address(__first))
143 constexpr basic_string_view&
144 operator=(
const basic_string_view&) noexcept =
default;
148 constexpr const_iterator
149 begin() const noexcept
150 {
return this->_M_str; }
152 constexpr const_iterator
154 {
return this->_M_str + this->_M_len; }
156 constexpr const_iterator
158 {
return this->_M_str; }
160 constexpr const_iterator
161 cend() const noexcept
162 {
return this->_M_str + this->_M_len; }
164 constexpr const_reverse_iterator
166 {
return const_reverse_iterator(this->
end()); }
168 constexpr const_reverse_iterator
169 rend() const noexcept
170 {
return const_reverse_iterator(this->
begin()); }
172 constexpr const_reverse_iterator
174 {
return const_reverse_iterator(this->
end()); }
176 constexpr const_reverse_iterator
177 crend() const noexcept
178 {
return const_reverse_iterator(this->
begin()); }
183 size() const noexcept
184 {
return this->_M_len; }
187 length() const noexcept
191 max_size() const noexcept
193 return (npos -
sizeof(size_type) -
sizeof(
void*))
194 /
sizeof(value_type) / 4;
197 [[nodiscard]] constexpr
bool
198 empty() const noexcept
199 {
return this->_M_len == 0; }
203 constexpr const_reference
204 operator[](size_type __pos)
const noexcept
208 return *(this->_M_str + __pos);
211 constexpr const_reference
212 at(size_type __pos)
const
215 __throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
216 "(which is %zu) >= this->size() "
217 "(which is %zu)"), __pos, this->size());
218 return *(this->_M_str + __pos);
221 constexpr const_reference
222 front() const noexcept
226 return *this->_M_str;
229 constexpr const_reference
230 back() const noexcept
234 return *(this->_M_str + this->_M_len - 1);
237 constexpr const_pointer
238 data() const noexcept
239 {
return this->_M_str; }
244 remove_prefix(size_type __n) noexcept
246 __glibcxx_assert(this->_M_len >= __n);
252 remove_suffix(size_type __n) noexcept
253 { this->_M_len -= __n; }
256 swap(basic_string_view& __sv) noexcept
266 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
268 __glibcxx_requires_string_len(__str, __n);
269 __pos = std::__sv_check(size(), __pos,
"basic_string_view::copy");
270 const size_type __rlen =
std::min(__n, _M_len - __pos);
273 traits_type::copy(__str, data() + __pos, __rlen);
277 constexpr basic_string_view
278 substr(size_type __pos = 0, size_type __n = npos)
const noexcept(
false)
280 __pos = std::__sv_check(size(), __pos,
"basic_string_view::substr");
281 const size_type __rlen =
std::min(__n, _M_len - __pos);
282 return basic_string_view{_M_str + __pos, __rlen};
286 compare(basic_string_view __str)
const noexcept
288 const size_type __rlen =
std::min(this->_M_len, __str._M_len);
289 int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
291 __ret = _S_compare(this->_M_len, __str._M_len);
296 compare(size_type __pos1, size_type __n1, basic_string_view __str)
const
297 {
return this->substr(__pos1, __n1).compare(__str); }
300 compare(size_type __pos1, size_type __n1,
301 basic_string_view __str, size_type __pos2, size_type __n2)
const
303 return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
306 __attribute__((__nonnull__)) constexpr
int
307 compare(
const _CharT* __str)
const noexcept
308 {
return this->compare(basic_string_view{__str}); }
310 __attribute__((__nonnull__)) constexpr
int
311 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
312 {
return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
315 compare(size_type __pos1, size_type __n1,
316 const _CharT* __str, size_type __n2)
const noexcept(
false)
318 return this->substr(__pos1, __n1)
319 .compare(basic_string_view(__str, __n2));
322 #if __cplusplus > 201703L
324 starts_with(basic_string_view __x)
const noexcept
325 {
return this->substr(0, __x.size()) == __x; }
328 starts_with(_CharT __x)
const noexcept
329 {
return !this->empty() && traits_type::eq(this->front(), __x); }
332 starts_with(
const _CharT* __x)
const noexcept
333 {
return this->starts_with(basic_string_view(__x)); }
336 ends_with(basic_string_view __x)
const noexcept
338 return this->size() >= __x.size()
339 && this->compare(this->size() - __x.size(), npos, __x) == 0;
343 ends_with(_CharT __x)
const noexcept
344 {
return !this->empty() && traits_type::eq(this->back(), __x); }
347 ends_with(
const _CharT* __x)
const noexcept
348 {
return this->ends_with(basic_string_view(__x)); }
354 find(basic_string_view __str, size_type __pos = 0) const noexcept
355 {
return this->find(__str._M_str, __pos, __str._M_len); }
358 find(_CharT __c, size_type __pos = 0) const noexcept;
361 find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
363 __attribute__((__nonnull__)) constexpr size_type
364 find(const _CharT* __str, size_type __pos = 0) const noexcept
365 {
return this->find(__str, __pos, traits_type::length(__str)); }
368 rfind(basic_string_view __str, size_type __pos = npos)
const noexcept
369 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
372 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
375 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
377 __attribute__((__nonnull__)) constexpr size_type
378 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
379 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
382 find_first_of(basic_string_view __str, size_type __pos = 0) const noexcept
383 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
386 find_first_of(_CharT __c, size_type __pos = 0) const noexcept
387 {
return this->find(__c, __pos); }
390 find_first_of(
const _CharT* __str, size_type __pos,
391 size_type __n)
const noexcept;
393 __attribute__((__nonnull__)) constexpr size_type
394 find_first_of(
const _CharT* __str, size_type __pos = 0) const noexcept
395 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
398 find_last_of(basic_string_view __str,
399 size_type __pos = npos)
const noexcept
400 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
403 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
404 {
return this->rfind(__c, __pos); }
407 find_last_of(
const _CharT* __str, size_type __pos,
408 size_type __n)
const noexcept;
410 __attribute__((__nonnull__)) constexpr size_type
411 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
412 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
415 find_first_not_of(basic_string_view __str,
416 size_type __pos = 0) const noexcept
417 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
420 find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
423 find_first_not_of(const _CharT* __str,
424 size_type __pos, size_type __n) const noexcept;
426 __attribute__((__nonnull__)) constexpr size_type
427 find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
429 return this->find_first_not_of(__str, __pos,
430 traits_type::length(__str));
434 find_last_not_of(basic_string_view __str,
435 size_type __pos = npos)
const noexcept
436 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
439 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
442 find_last_not_of(
const _CharT* __str,
443 size_type __pos, size_type __n)
const noexcept;
445 __attribute__((__nonnull__)) constexpr size_type
446 find_last_not_of(
const _CharT* __str,
447 size_type __pos = npos)
const noexcept
449 return this->find_last_not_of(__str, __pos,
450 traits_type::length(__str));
456 _S_compare(size_type __n1, size_type __n2) noexcept
458 const difference_type __diff = __n1 - __n2;
463 return static_cast<int>(__diff);
467 const _CharT* _M_str;
470 #if __cplusplus > 201703L && __cpp_lib_concepts && __cpp_deduction_guides
471 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
472 basic_string_view(_It, _End) -> basic_string_view<iter_value_t<_It>>;
482 template<
typename _CharT,
typename _Traits>
484 operator==(basic_string_view<_CharT, _Traits> __x,
485 basic_string_view<_CharT, _Traits> __y) noexcept
486 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
488 template<
typename _CharT,
typename _Traits>
490 operator==(basic_string_view<_CharT, _Traits> __x,
491 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
493 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
495 template<
typename _CharT,
typename _Traits>
497 operator==(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
498 basic_string_view<_CharT, _Traits> __y) noexcept
499 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
501 template<
typename _CharT,
typename _Traits>
503 operator!=(basic_string_view<_CharT, _Traits> __x,
504 basic_string_view<_CharT, _Traits> __y) noexcept
505 {
return !(__x == __y); }
507 template<
typename _CharT,
typename _Traits>
509 operator!=(basic_string_view<_CharT, _Traits> __x,
510 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
512 {
return !(__x == __y); }
514 template<
typename _CharT,
typename _Traits>
516 operator!=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
517 basic_string_view<_CharT, _Traits> __y) noexcept
518 {
return !(__x == __y); }
520 template<
typename _CharT,
typename _Traits>
522 operator< (basic_string_view<_CharT, _Traits> __x,
523 basic_string_view<_CharT, _Traits> __y) noexcept
524 {
return __x.compare(__y) < 0; }
526 template<
typename _CharT,
typename _Traits>
528 operator< (basic_string_view<_CharT, _Traits> __x,
529 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
531 {
return __x.compare(__y) < 0; }
533 template<
typename _CharT,
typename _Traits>
535 operator< (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
536 basic_string_view<_CharT, _Traits> __y) noexcept
537 {
return __x.compare(__y) < 0; }
539 template<
typename _CharT,
typename _Traits>
541 operator> (basic_string_view<_CharT, _Traits> __x,
542 basic_string_view<_CharT, _Traits> __y) noexcept
543 {
return __x.compare(__y) > 0; }
545 template<
typename _CharT,
typename _Traits>
547 operator> (basic_string_view<_CharT, _Traits> __x,
548 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
550 {
return __x.compare(__y) > 0; }
552 template<
typename _CharT,
typename _Traits>
554 operator> (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
555 basic_string_view<_CharT, _Traits> __y) noexcept
556 {
return __x.compare(__y) > 0; }
558 template<
typename _CharT,
typename _Traits>
560 operator<=(basic_string_view<_CharT, _Traits> __x,
561 basic_string_view<_CharT, _Traits> __y) noexcept
562 {
return __x.compare(__y) <= 0; }
564 template<
typename _CharT,
typename _Traits>
566 operator<=(basic_string_view<_CharT, _Traits> __x,
567 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
569 {
return __x.compare(__y) <= 0; }
571 template<
typename _CharT,
typename _Traits>
573 operator<=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
574 basic_string_view<_CharT, _Traits> __y) noexcept
575 {
return __x.compare(__y) <= 0; }
577 template<
typename _CharT,
typename _Traits>
579 operator>=(basic_string_view<_CharT, _Traits> __x,
580 basic_string_view<_CharT, _Traits> __y) noexcept
581 {
return __x.compare(__y) >= 0; }
583 template<
typename _CharT,
typename _Traits>
585 operator>=(basic_string_view<_CharT, _Traits> __x,
586 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
588 {
return __x.compare(__y) >= 0; }
590 template<
typename _CharT,
typename _Traits>
592 operator>=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
593 basic_string_view<_CharT, _Traits> __y) noexcept
594 {
return __x.compare(__y) >= 0; }
597 template<
typename _CharT,
typename _Traits>
598 inline basic_ostream<_CharT, _Traits>&
599 operator<<(basic_ostream<_CharT, _Traits>& __os,
600 basic_string_view<_CharT,_Traits> __str)
601 {
return __ostream_insert(__os, __str.data(), __str.size()); }
606 using string_view = basic_string_view<char>;
607 #ifdef _GLIBCXX_USE_WCHAR_T
608 using wstring_view = basic_string_view<wchar_t>;
610 #ifdef _GLIBCXX_USE_CHAR8_T
611 using u8string_view = basic_string_view<char8_t>;
613 using u16string_view = basic_string_view<char16_t>;
614 using u32string_view = basic_string_view<char32_t>;
618 template<
typename _Tp>
622 struct hash<string_view>
623 :
public __hash_base<size_t, string_view>
626 operator()(
const string_view& __str)
const noexcept
627 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
634 #ifdef _GLIBCXX_USE_WCHAR_T
636 struct hash<wstring_view>
637 :
public __hash_base<size_t, wstring_view>
640 operator()(
const wstring_view& __s)
const noexcept
641 {
return std::_Hash_impl::hash(__s.data(),
642 __s.length() *
sizeof(wchar_t)); }
650 #ifdef _GLIBCXX_USE_CHAR8_T
652 struct hash<u8string_view>
653 :
public __hash_base<size_t, u8string_view>
656 operator()(
const u8string_view& __str)
const noexcept
657 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
666 struct hash<u16string_view>
667 :
public __hash_base<size_t, u16string_view>
670 operator()(
const u16string_view& __s)
const noexcept
671 {
return std::_Hash_impl::hash(__s.data(),
672 __s.length() *
sizeof(char16_t)); }
680 struct hash<u32string_view>
681 :
public __hash_base<size_t, u32string_view>
684 operator()(
const u32string_view& __s)
const noexcept
685 {
return std::_Hash_impl::hash(__s.data(),
686 __s.length() *
sizeof(char32_t)); }
693 inline namespace literals
695 inline namespace string_view_literals
697 #pragma GCC diagnostic push
698 #pragma GCC diagnostic ignored "-Wliteral-suffix"
699 inline constexpr basic_string_view<char>
700 operator""sv(
const char* __str,
size_t __len) noexcept
701 {
return basic_string_view<char>{__str, __len}; }
703 #ifdef _GLIBCXX_USE_WCHAR_T
704 inline constexpr basic_string_view<wchar_t>
705 operator""sv(
const wchar_t* __str,
size_t __len) noexcept
706 {
return basic_string_view<wchar_t>{__str, __len}; }
709 #ifdef _GLIBCXX_USE_CHAR8_T
710 inline constexpr basic_string_view<char8_t>
711 operator""sv(
const char8_t* __str,
size_t __len) noexcept
712 {
return basic_string_view<char8_t>{__str, __len}; }
715 inline constexpr basic_string_view<char16_t>
716 operator""sv(
const char16_t* __str,
size_t __len) noexcept
717 {
return basic_string_view<char16_t>{__str, __len}; }
719 inline constexpr basic_string_view<char32_t>
720 operator""sv(
const char32_t* __str,
size_t __len) noexcept
721 {
return basic_string_view<char32_t>{__str, __len}; }
723 #pragma GCC diagnostic pop
727 #if __cpp_lib_concepts
731 template<
typename _CharT,
typename _Traits>
732 inline constexpr
bool
733 enable_borrowed_range<basic_string_view<_CharT, _Traits>> =
true;
736 template<
typename _CharT,
typename _Traits>
737 inline constexpr
bool
738 enable_view<basic_string_view<_CharT, _Traits>> =
true;
741 _GLIBCXX_END_NAMESPACE_VERSION
744 #include <bits/string_view.tcc>
746 #endif // __cplusplus <= 201402L
748 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW