34 #ifndef _GLIBCXX_EXPERIMENTAL_STRING_VIEW
35 #define _GLIBCXX_EXPERIMENTAL_STRING_VIEW 1
37 #pragma GCC system_header
39 #if __cplusplus >= 201402L
45 namespace std _GLIBCXX_VISIBILITY(default)
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49 namespace experimental
51 inline namespace fundamentals_v1
53 #define __cpp_lib_experimental_string_view 201411
74 template<
typename _CharT,
typename _Traits = std::
char_traits<_CharT>>
80 using traits_type = _Traits;
81 using value_type = _CharT;
82 using pointer = _CharT*;
83 using const_pointer =
const _CharT*;
84 using reference = _CharT&;
85 using const_reference =
const _CharT&;
86 using const_iterator =
const _CharT*;
87 using iterator = const_iterator;
90 using size_type = size_t;
91 using difference_type = ptrdiff_t;
92 static constexpr size_type npos = size_type(-1);
98 : _M_len{0}, _M_str{
nullptr}
103 template<
typename _Allocator>
105 _Allocator>& __str) noexcept
106 : _M_len{__str.length()}, _M_str{__str.data()}
110 : _M_len{__str ==
nullptr ? 0 : traits_type::length(__str)},
124 constexpr const_iterator
125 begin()
const noexcept
126 {
return this->_M_str; }
128 constexpr const_iterator
130 {
return this->_M_str + this->_M_len; }
132 constexpr const_iterator
133 cbegin()
const noexcept
134 {
return this->_M_str; }
136 constexpr const_iterator
137 cend()
const noexcept
138 {
return this->_M_str + this->_M_len; }
141 rbegin()
const noexcept
145 rend()
const noexcept
149 crbegin()
const noexcept
153 crend()
const noexcept
159 size()
const noexcept
160 {
return this->_M_len; }
163 length()
const noexcept
167 max_size()
const noexcept
169 return (npos -
sizeof(size_type) -
sizeof(
void*))
170 /
sizeof(value_type) / 4;
173 _GLIBCXX_NODISCARD constexpr
bool
174 empty()
const noexcept
175 {
return this->_M_len == 0; }
179 constexpr
const _CharT&
180 operator[](size_type __pos)
const
184 return *(this->_M_str + __pos);
187 constexpr
const _CharT&
188 at(size_type __pos)
const
190 return __pos < this->_M_len
191 ? *(this->_M_str + __pos)
192 : (__throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
193 "(which is %zu) >= this->size() "
195 __pos, this->size()),
199 constexpr
const _CharT&
204 return *this->_M_str;
207 constexpr
const _CharT&
212 return *(this->_M_str + this->_M_len - 1);
215 constexpr
const _CharT*
216 data()
const noexcept
217 {
return this->_M_str; }
222 remove_prefix(size_type __n)
224 __glibcxx_assert(this->_M_len >= __n);
230 remove_suffix(size_type __n)
231 { this->_M_len -= __n; }
244 template<
typename _Allocator>
247 return { this->_M_str, this->_M_len };
250 template<
typename _Allocator = std::allocator<_CharT>>
252 to_string(
const _Allocator& __alloc = _Allocator())
const
254 return { this->_M_str, this->_M_len, __alloc };
258 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
260 __glibcxx_requires_string_len(__str, __n);
261 if (__pos > this->_M_len)
262 __throw_out_of_range_fmt(__N(
"basic_string_view::copy: __pos "
263 "(which is %zu) > this->size() "
265 __pos, this->size());
266 size_type __rlen{
std::min(__n, size_type{this->_M_len - __pos})};
267 for (
auto __begin = this->_M_str + __pos,
268 __end = __begin + __rlen; __begin != __end;)
269 *__str++ = *__begin++;
277 substr(size_type __pos = 0, size_type __n = npos)
const
279 return __pos <= this->_M_len
281 std::min(__n, size_type{this->_M_len - __pos})}
282 : (__throw_out_of_range_fmt(__N(
"basic_string_view::substr: __pos "
283 "(which is %zu) > this->size() "
291 int __ret = traits_type::compare(this->_M_str, __str._M_str,
292 std::min(this->_M_len, __str._M_len));
294 __ret = _S_compare(this->_M_len, __str._M_len);
300 {
return this->substr(__pos1, __n1).compare(__str); }
303 compare(size_type __pos1, size_type __n1,
305 {
return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2)); }
308 compare(
const _CharT* __str)
const noexcept
312 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
316 compare(size_type __pos1, size_type __n1,
317 const _CharT* __str, size_type __n2)
const
319 return this->substr(__pos1, __n1)
325 {
return this->find(__str._M_str, __pos, __str._M_len); }
328 find(_CharT __c, size_type __pos=0)
const noexcept;
331 find(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
334 find(
const _CharT* __str, size_type __pos=0)
const noexcept
335 {
return this->find(__str, __pos, traits_type::length(__str)); }
339 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
342 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
345 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
348 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
349 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
353 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
356 find_first_of(_CharT __c, size_type __pos = 0)
const noexcept
357 {
return this->find(__c, __pos); }
360 find_first_of(
const _CharT* __str, size_type __pos, size_type __n)
const;
363 find_first_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
364 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
368 size_type __pos = npos)
const noexcept
369 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
372 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
373 {
return this->rfind(__c, __pos); }
376 find_last_of(
const _CharT* __str, size_type __pos, size_type __n)
const;
379 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
380 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
384 size_type __pos = 0)
const noexcept
385 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
388 find_first_not_of(_CharT __c, size_type __pos = 0)
const noexcept;
391 find_first_not_of(
const _CharT* __str,
392 size_type __pos, size_type __n)
const;
395 find_first_not_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
397 return this->find_first_not_of(__str, __pos,
398 traits_type::length(__str));
403 size_type __pos = npos)
const noexcept
404 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
407 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
410 find_last_not_of(
const _CharT* __str,
411 size_type __pos, size_type __n)
const;
414 find_last_not_of(
const _CharT* __str,
415 size_type __pos = npos)
const noexcept
417 return this->find_last_not_of(__str, __pos,
418 traits_type::length(__str));
424 _S_compare(size_type __n1, size_type __n2) noexcept
430 : static_cast<int>(difference_type(__n1 - __n2));
434 const _CharT* _M_str;
444 template<
typename _CharT,
typename _Traits>
448 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
450 template<
typename _CharT,
typename _Traits>
455 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
457 template<
typename _CharT,
typename _Traits>
459 operator==(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
460 basic_string_view<_CharT, _Traits> __y) noexcept
461 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
463 template<
typename _CharT,
typename _Traits>
465 operator!=(basic_string_view<_CharT, _Traits> __x,
466 basic_string_view<_CharT, _Traits> __y) noexcept
467 {
return !(__x == __y); }
469 template<
typename _CharT,
typename _Traits>
471 operator!=(basic_string_view<_CharT, _Traits> __x,
472 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
474 {
return !(__x == __y); }
476 template<
typename _CharT,
typename _Traits>
478 operator!=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
479 basic_string_view<_CharT, _Traits> __y) noexcept
480 {
return !(__x == __y); }
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.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.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.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.compare(__y) > 0; }
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.compare(__y) > 0; }
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.compare(__y) > 0; }
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; }
559 template<
typename _CharT,
typename _Traits>
560 inline basic_ostream<_CharT, _Traits>&
561 operator<<(basic_ostream<_CharT, _Traits>& __os,
562 basic_string_view<_CharT,_Traits> __str)
563 {
return __ostream_insert(__os, __str.data(), __str.size()); }
568 using string_view = basic_string_view<char>;
569 #ifdef _GLIBCXX_USE_WCHAR_T
570 using wstring_view = basic_string_view<wchar_t>;
572 #ifdef _GLIBCXX_USE_CHAR8_T
573 using u8string_view = basic_string_view<char8_t>;
575 using u16string_view = basic_string_view<char16_t>;
576 using u32string_view = basic_string_view<char32_t>;
582 template<
typename _Tp>
586 struct hash<experimental::string_view>
587 :
public __hash_base<size_t, experimental::string_view>
590 operator()(
const experimental::string_view& __str)
const noexcept
591 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
595 struct __is_fast_hash<hash<experimental::string_view>> :
std::false_type
598 #ifdef _GLIBCXX_USE_WCHAR_T
600 struct hash<experimental::wstring_view>
601 :
public __hash_base<size_t, wstring>
604 operator()(
const experimental::wstring_view& __s)
const noexcept
605 {
return std::_Hash_impl::hash(__s.data(),
606 __s.length() *
sizeof(wchar_t)); }
610 struct __is_fast_hash<hash<experimental::wstring_view>> :
std::false_type
614 #ifdef _GLIBCXX_USE_CHAR8_T
616 struct hash<experimental::u8string_view>
617 :
public __hash_base<size_t, experimental::u8string_view>
620 operator()(
const experimental::u8string_view& __s)
const noexcept
621 {
return std::_Hash_impl::hash(__s.data(), __s.length()); }
625 struct __is_fast_hash<hash<experimental::u8string_view>> :
std::false_type
630 struct hash<experimental::u16string_view>
631 :
public __hash_base<size_t, experimental::u16string_view>
634 operator()(
const experimental::u16string_view& __s)
const noexcept
635 {
return std::_Hash_impl::hash(__s.data(),
636 __s.length() *
sizeof(char16_t)); }
640 struct __is_fast_hash<hash<experimental::u16string_view>> :
std::false_type
644 struct hash<experimental::u32string_view>
645 :
public __hash_base<size_t, experimental::u32string_view>
648 operator()(
const experimental::u32string_view& __s)
const noexcept
649 {
return std::_Hash_impl::hash(__s.data(),
650 __s.length() *
sizeof(char32_t)); }
654 struct __is_fast_hash<hash<experimental::u32string_view>> :
std::false_type
657 namespace experimental
660 inline namespace literals
662 inline namespace string_view_literals
664 #pragma GCC diagnostic push
665 #pragma GCC diagnostic ignored "-Wliteral-suffix"
666 inline constexpr basic_string_view<char>
667 operator""sv(
const char* __str,
size_t __len) noexcept
668 {
return basic_string_view<char>{__str, __len}; }
670 #ifdef _GLIBCXX_USE_WCHAR_T
671 inline constexpr basic_string_view<wchar_t>
672 operator""sv(
const wchar_t* __str,
size_t __len) noexcept
673 {
return basic_string_view<wchar_t>{__str, __len}; }
676 #ifdef _GLIBCXX_USE_CHAR8_T
677 inline constexpr basic_string_view<char8_t>
678 operator""sv(
const char8_t* __str,
size_t __len) noexcept
679 {
return basic_string_view<char8_t>{__str, __len}; }
682 inline constexpr basic_string_view<char16_t>
683 operator""sv(
const char16_t* __str,
size_t __len) noexcept
684 {
return basic_string_view<char16_t>{__str, __len}; }
686 inline constexpr basic_string_view<char32_t>
687 operator""sv(
const char32_t* __str,
size_t __len) noexcept
688 {
return basic_string_view<char32_t>{__str, __len}; }
689 #pragma GCC diagnostic pop
694 #if __cpp_lib_concepts
697 template<
typename>
extern inline const bool enable_safe_range;
699 template<
typename _CharT,
typename _Traits>
700 inline constexpr
bool
701 enable_safe_range<experimental::basic_string_view<_CharT, _Traits>>
706 _GLIBCXX_END_NAMESPACE_VERSION
711 #endif // __cplusplus <= 201103L
713 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW