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*;
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; }
140 const_reverse_iterator
141 rbegin()
const noexcept
142 {
return const_reverse_iterator(this->end()); }
144 const_reverse_iterator
145 rend()
const noexcept
146 {
return const_reverse_iterator(this->begin()); }
148 const_reverse_iterator
149 crbegin()
const noexcept
150 {
return const_reverse_iterator(this->end()); }
152 const_reverse_iterator
153 crend()
const noexcept
154 {
return const_reverse_iterator(this->begin()); }
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
182 __glibcxx_assert(__pos < this->_M_len);
183 return *(this->_M_str + __pos);
186 constexpr
const _CharT&
187 at(size_type __pos)
const
189 return __pos < this->_M_len
190 ? *(this->_M_str + __pos)
191 : (__throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
192 "(which is %zu) >= this->size() "
194 __pos, this->size()),
198 constexpr
const _CharT&
201 __glibcxx_assert(this->_M_len > 0);
202 return *this->_M_str;
205 constexpr
const _CharT&
208 __glibcxx_assert(this->_M_len > 0);
209 return *(this->_M_str + this->_M_len - 1);
212 constexpr
const _CharT*
213 data()
const noexcept
214 {
return this->_M_str; }
219 remove_prefix(size_type __n)
221 __glibcxx_assert(this->_M_len >= __n);
227 remove_suffix(size_type __n)
228 { this->_M_len -= __n; }
241 template<
typename _Allocator>
244 return { this->_M_str, this->_M_len };
247 template<
typename _Allocator = std::allocator<_CharT>>
249 to_string(
const _Allocator& __alloc = _Allocator())
const
251 return { this->_M_str, this->_M_len, __alloc };
255 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
257 __glibcxx_requires_string_len(__str, __n);
258 if (__pos > this->_M_len)
259 __throw_out_of_range_fmt(__N(
"basic_string_view::copy: __pos "
260 "(which is %zu) > this->size() "
262 __pos, this->size());
263 size_type __rlen{
std::min(__n, size_type{this->_M_len - __pos})};
264 for (
auto __begin = this->_M_str + __pos,
265 __end = __begin + __rlen; __begin != __end;)
266 *__str++ = *__begin++;
274 substr(size_type __pos = 0, size_type __n = npos)
const
276 return __pos <= this->_M_len
278 std::min(__n, size_type{this->_M_len - __pos})}
279 : (__throw_out_of_range_fmt(__N(
"basic_string_view::substr: __pos "
280 "(which is %zu) > this->size() "
288 int __ret = traits_type::compare(this->_M_str, __str._M_str,
289 std::min(this->_M_len, __str._M_len));
291 __ret = _S_compare(this->_M_len, __str._M_len);
297 {
return this->substr(__pos1, __n1).compare(__str); }
300 compare(size_type __pos1, size_type __n1,
302 {
return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2)); }
305 compare(
const _CharT* __str)
const noexcept
309 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
313 compare(size_type __pos1, size_type __n1,
314 const _CharT* __str, size_type __n2)
const
316 return this->substr(__pos1, __n1)
322 {
return this->find(__str._M_str, __pos, __str._M_len); }
325 find(_CharT __c, size_type __pos=0)
const noexcept;
328 find(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
331 find(
const _CharT* __str, size_type __pos=0)
const noexcept
332 {
return this->find(__str, __pos, traits_type::length(__str)); }
336 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
339 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
342 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
345 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
346 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
350 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
353 find_first_of(_CharT __c, size_type __pos = 0)
const noexcept
354 {
return this->find(__c, __pos); }
357 find_first_of(
const _CharT* __str, size_type __pos, size_type __n)
const;
360 find_first_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
361 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
365 size_type __pos = npos)
const noexcept
366 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
369 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
370 {
return this->rfind(__c, __pos); }
373 find_last_of(
const _CharT* __str, size_type __pos, size_type __n)
const;
376 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
377 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
381 size_type __pos = 0)
const noexcept
382 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
385 find_first_not_of(_CharT __c, size_type __pos = 0)
const noexcept;
388 find_first_not_of(
const _CharT* __str,
389 size_type __pos, size_type __n)
const;
392 find_first_not_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
394 return this->find_first_not_of(__str, __pos,
395 traits_type::length(__str));
400 size_type __pos = npos)
const noexcept
401 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
404 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
407 find_last_not_of(
const _CharT* __str,
408 size_type __pos, size_type __n)
const;
411 find_last_not_of(
const _CharT* __str,
412 size_type __pos = npos)
const noexcept
414 return this->find_last_not_of(__str, __pos,
415 traits_type::length(__str));
421 _S_compare(size_type __n1, size_type __n2) noexcept
427 :
static_cast<int>(difference_type(__n1 - __n2));
431 const _CharT* _M_str;
441 template<
typename _CharT,
typename _Traits>
445 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
447 template<
typename _CharT,
typename _Traits>
449 operator==(basic_string_view<_CharT, _Traits> __x,
450 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
452 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
454 template<
typename _CharT,
typename _Traits>
456 operator==(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
457 basic_string_view<_CharT, _Traits> __y) noexcept
458 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
460 template<
typename _CharT,
typename _Traits>
462 operator!=(basic_string_view<_CharT, _Traits> __x,
463 basic_string_view<_CharT, _Traits> __y) noexcept
464 {
return !(__x == __y); }
466 template<
typename _CharT,
typename _Traits>
468 operator!=(basic_string_view<_CharT, _Traits> __x,
469 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
471 {
return !(__x == __y); }
473 template<
typename _CharT,
typename _Traits>
475 operator!=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
476 basic_string_view<_CharT, _Traits> __y) noexcept
477 {
return !(__x == __y); }
479 template<
typename _CharT,
typename _Traits>
481 operator< (basic_string_view<_CharT, _Traits> __x,
482 basic_string_view<_CharT, _Traits> __y) noexcept
483 {
return __x.compare(__y) < 0; }
485 template<
typename _CharT,
typename _Traits>
487 operator< (basic_string_view<_CharT, _Traits> __x,
488 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
490 {
return __x.compare(__y) < 0; }
492 template<
typename _CharT,
typename _Traits>
494 operator< (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
495 basic_string_view<_CharT, _Traits> __y) noexcept
496 {
return __x.compare(__y) < 0; }
498 template<
typename _CharT,
typename _Traits>
500 operator> (basic_string_view<_CharT, _Traits> __x,
501 basic_string_view<_CharT, _Traits> __y) noexcept
502 {
return __x.compare(__y) > 0; }
504 template<
typename _CharT,
typename _Traits>
506 operator> (basic_string_view<_CharT, _Traits> __x,
507 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
509 {
return __x.compare(__y) > 0; }
511 template<
typename _CharT,
typename _Traits>
513 operator> (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
514 basic_string_view<_CharT, _Traits> __y) noexcept
515 {
return __x.compare(__y) > 0; }
517 template<
typename _CharT,
typename _Traits>
519 operator<=(basic_string_view<_CharT, _Traits> __x,
520 basic_string_view<_CharT, _Traits> __y) noexcept
521 {
return __x.compare(__y) <= 0; }
523 template<
typename _CharT,
typename _Traits>
525 operator<=(basic_string_view<_CharT, _Traits> __x,
526 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
528 {
return __x.compare(__y) <= 0; }
530 template<
typename _CharT,
typename _Traits>
532 operator<=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
533 basic_string_view<_CharT, _Traits> __y) noexcept
534 {
return __x.compare(__y) <= 0; }
536 template<
typename _CharT,
typename _Traits>
538 operator>=(basic_string_view<_CharT, _Traits> __x,
539 basic_string_view<_CharT, _Traits> __y) noexcept
540 {
return __x.compare(__y) >= 0; }
542 template<
typename _CharT,
typename _Traits>
544 operator>=(basic_string_view<_CharT, _Traits> __x,
545 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
547 {
return __x.compare(__y) >= 0; }
549 template<
typename _CharT,
typename _Traits>
551 operator>=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
552 basic_string_view<_CharT, _Traits> __y) noexcept
553 {
return __x.compare(__y) >= 0; }
556 template<
typename _CharT,
typename _Traits>
557 inline basic_ostream<_CharT, _Traits>&
558 operator<<(basic_ostream<_CharT, _Traits>& __os,
559 basic_string_view<_CharT,_Traits> __str)
560 {
return __ostream_insert(__os, __str.data(), __str.size()); }
565 using string_view = basic_string_view<char>;
566 #ifdef _GLIBCXX_USE_WCHAR_T
567 using wstring_view = basic_string_view<wchar_t>;
569 #ifdef _GLIBCXX_USE_CHAR8_T
570 using u8string_view = basic_string_view<char8_t>;
572 using u16string_view = basic_string_view<char16_t>;
573 using u32string_view = basic_string_view<char32_t>;
579 template<
typename _Tp>
583 struct hash<experimental::string_view>
584 :
public __hash_base<size_t, experimental::string_view>
587 operator()(
const experimental::string_view& __str)
const noexcept
588 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
592 struct __is_fast_hash<hash<experimental::string_view>> :
std::false_type
595 #ifdef _GLIBCXX_USE_WCHAR_T
597 struct hash<experimental::wstring_view>
598 :
public __hash_base<size_t, wstring>
601 operator()(
const experimental::wstring_view& __s)
const noexcept
602 {
return std::_Hash_impl::hash(__s.data(),
603 __s.length() *
sizeof(
wchar_t)); }
607 struct __is_fast_hash<hash<experimental::wstring_view>> :
std::false_type
611 #ifdef _GLIBCXX_USE_CHAR8_T
613 struct hash<experimental::u8string_view>
614 :
public __hash_base<size_t, experimental::u8string_view>
617 operator()(
const experimental::u8string_view& __s)
const noexcept
618 {
return std::_Hash_impl::hash(__s.data(), __s.length()); }
622 struct __is_fast_hash<hash<experimental::u8string_view>> :
std::false_type
627 struct hash<experimental::u16string_view>
628 :
public __hash_base<size_t, experimental::u16string_view>
631 operator()(
const experimental::u16string_view& __s)
const noexcept
632 {
return std::_Hash_impl::hash(__s.data(),
633 __s.length() *
sizeof(char16_t)); }
637 struct __is_fast_hash<hash<experimental::u16string_view>> :
std::false_type
641 struct hash<experimental::u32string_view>
642 :
public __hash_base<size_t, experimental::u32string_view>
645 operator()(
const experimental::u32string_view& __s)
const noexcept
646 {
return std::_Hash_impl::hash(__s.data(),
647 __s.length() *
sizeof(char32_t)); }
651 struct __is_fast_hash<hash<experimental::u32string_view>> :
std::false_type
654 namespace experimental
657 inline namespace literals
659 inline namespace string_view_literals
661 #pragma GCC diagnostic push
662 #pragma GCC diagnostic ignored "-Wliteral-suffix"
663 inline constexpr basic_string_view<char>
664 operator""sv(
const char* __str,
size_t __len) noexcept
665 {
return basic_string_view<char>{__str, __len}; }
667 #ifdef _GLIBCXX_USE_WCHAR_T
668 inline constexpr basic_string_view<wchar_t>
669 operator""sv(
const wchar_t* __str,
size_t __len) noexcept
670 {
return basic_string_view<wchar_t>{__str, __len}; }
673 #ifdef _GLIBCXX_USE_CHAR8_T
674 inline constexpr basic_string_view<char8_t>
675 operator""sv(
const char8_t* __str,
size_t __len) noexcept
676 {
return basic_string_view<char8_t>{__str, __len}; }
679 inline constexpr basic_string_view<char16_t>
680 operator""sv(
const char16_t* __str,
size_t __len) noexcept
681 {
return basic_string_view<char16_t>{__str, __len}; }
683 inline constexpr basic_string_view<char32_t>
684 operator""sv(
const char32_t* __str,
size_t __len) noexcept
685 {
return basic_string_view<char32_t>{__str, __len}; }
686 #pragma GCC diagnostic pop
691 #if __cpp_lib_concepts
695 template<
typename _CharT,
typename _Traits>
696 inline constexpr
bool
697 enable_borrowed_range<experimental::basic_string_view<_CharT, _Traits>>
701 template<
typename _CharT,
typename _Traits>
702 inline constexpr
bool
703 enable_view<experimental::basic_string_view<_CharT, _Traits>> =
true;
707 _GLIBCXX_END_NAMESPACE_VERSION
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
ISO C++ entities toplevel namespace is std.
static constexpr _Tp max() noexcept
static constexpr _Tp min() noexcept
Managing sequences of characters and character-like objects.
A non-owning reference to a string.