33 #ifndef _GLIBCXX_EXPERIMENTAL_STRING_VIEW
34 #define _GLIBCXX_EXPERIMENTAL_STRING_VIEW 1
36 #pragma GCC system_header
38 #if __cplusplus >= 201402L
44 namespace std _GLIBCXX_VISIBILITY(default)
46 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48 namespace experimental
50 inline namespace fundamentals_v1
52 #define __cpp_lib_experimental_string_view 201411
73 template<
typename _CharT,
typename _Traits = std::
char_traits<_CharT>>
79 using traits_type = _Traits;
80 using value_type = _CharT;
81 using pointer = _CharT*;
82 using const_pointer =
const _CharT*;
83 using reference = _CharT&;
84 using const_reference =
const _CharT&;
85 using const_iterator =
const _CharT*;
86 using iterator = const_iterator;
89 using size_type = size_t;
90 using difference_type = ptrdiff_t;
91 static constexpr size_type npos = size_type(-1);
97 : _M_len{0}, _M_str{
nullptr}
102 template<
typename _Allocator>
104 _Allocator>& __str) noexcept
105 : _M_len{__str.length()}, _M_str{__str.data()}
109 : _M_len{__str ==
nullptr ? 0 : traits_type::length(__str)},
113 constexpr basic_string_view(
const _CharT* __str, size_type __len)
119 operator=(
const basic_string_view&) noexcept =
default;
123 constexpr const_iterator
124 begin()
const noexcept
125 {
return this->_M_str; }
127 constexpr const_iterator
129 {
return this->_M_str + this->_M_len; }
131 constexpr const_iterator
132 cbegin()
const noexcept
133 {
return this->_M_str; }
135 constexpr const_iterator
136 cend()
const noexcept
137 {
return this->_M_str + this->_M_len; }
140 rbegin()
const noexcept
144 rend()
const noexcept
148 crbegin()
const noexcept
152 crend()
const noexcept
158 size()
const noexcept
159 {
return this->_M_len; }
162 length()
const noexcept
166 max_size()
const noexcept
168 return (npos -
sizeof(size_type) -
sizeof(
void*))
169 /
sizeof(value_type) / 4;
172 _GLIBCXX_NODISCARD constexpr
bool
173 empty()
const noexcept
174 {
return this->_M_len == 0; }
178 constexpr
const _CharT&
179 operator[](size_type __pos)
const
181 __glibcxx_assert(__pos < this->_M_len);
182 return *(this->_M_str + __pos);
185 constexpr
const _CharT&
186 at(size_type __pos)
const
188 return __pos < this->_M_len
189 ? *(this->_M_str + __pos)
190 : (__throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
191 "(which is %zu) >= this->size() "
193 __pos, this->size()),
197 constexpr
const _CharT&
200 __glibcxx_assert(this->_M_len > 0);
201 return *this->_M_str;
204 constexpr
const _CharT&
207 __glibcxx_assert(this->_M_len > 0);
208 return *(this->_M_str + this->_M_len - 1);
211 constexpr
const _CharT*
212 data()
const noexcept
213 {
return this->_M_str; }
218 remove_prefix(size_type __n)
220 __glibcxx_assert(this->_M_len >= __n);
226 remove_suffix(size_type __n)
227 { this->_M_len -= __n; }
230 swap(basic_string_view& __sv) noexcept
240 template<
typename _Allocator>
243 return { this->_M_str, this->_M_len };
246 template<
typename _Allocator = std::allocator<_CharT>>
248 to_string(
const _Allocator& __alloc = _Allocator())
const
250 return { this->_M_str, this->_M_len, __alloc };
254 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
256 __glibcxx_requires_string_len(__str, __n);
257 if (__pos > this->_M_len)
258 __throw_out_of_range_fmt(__N(
"basic_string_view::copy: __pos "
259 "(which is %zu) > this->size() "
261 __pos, this->size());
262 size_type __rlen{
std::min(__n, size_type{this->_M_len - __pos})};
263 for (
auto __begin = this->_M_str + __pos,
264 __end = __begin + __rlen; __begin != __end;)
265 *__str++ = *__begin++;
272 constexpr basic_string_view
273 substr(size_type __pos = 0, size_type __n = npos)
const
275 return __pos <= this->_M_len
276 ? basic_string_view{this->_M_str + __pos,
277 std::min(__n, size_type{this->_M_len - __pos})}
278 : (__throw_out_of_range_fmt(__N(
"basic_string_view::substr: __pos "
279 "(which is %zu) > this->size() "
281 __pos, this->size()), basic_string_view{});
285 compare(basic_string_view __str)
const noexcept
287 int __ret = traits_type::compare(this->_M_str, __str._M_str,
288 std::min(this->_M_len, __str._M_len));
290 __ret = _S_compare(this->_M_len, __str._M_len);
295 compare(size_type __pos1, size_type __n1, basic_string_view __str)
const
296 {
return this->substr(__pos1, __n1).compare(__str); }
299 compare(size_type __pos1, size_type __n1,
300 basic_string_view __str, size_type __pos2, size_type __n2)
const
301 {
return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2)); }
304 compare(
const _CharT* __str)
const noexcept
305 {
return this->compare(basic_string_view{__str}); }
308 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
309 {
return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
312 compare(size_type __pos1, size_type __n1,
313 const _CharT* __str, size_type __n2)
const
315 return this->substr(__pos1, __n1)
316 .compare(basic_string_view(__str, __n2));
320 find(basic_string_view __str, size_type __pos = 0)
const noexcept
321 {
return this->find(__str._M_str, __pos, __str._M_len); }
324 find(_CharT __c, size_type __pos=0)
const noexcept;
327 find(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
330 find(
const _CharT* __str, size_type __pos=0)
const noexcept
331 {
return this->find(__str, __pos, traits_type::length(__str)); }
334 rfind(basic_string_view __str, size_type __pos = npos)
const noexcept
335 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
338 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
341 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
344 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
345 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
348 find_first_of(basic_string_view __str, size_type __pos = 0)
const noexcept
349 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
352 find_first_of(_CharT __c, size_type __pos = 0)
const noexcept
353 {
return this->find(__c, __pos); }
356 find_first_of(
const _CharT* __str, size_type __pos, size_type __n)
const;
359 find_first_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
360 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
363 find_last_of(basic_string_view __str,
364 size_type __pos = npos)
const noexcept
365 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
368 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
369 {
return this->rfind(__c, __pos); }
372 find_last_of(
const _CharT* __str, size_type __pos, size_type __n)
const;
375 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
376 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
379 find_first_not_of(basic_string_view __str,
380 size_type __pos = 0)
const noexcept
381 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
384 find_first_not_of(_CharT __c, size_type __pos = 0)
const noexcept;
387 find_first_not_of(
const _CharT* __str,
388 size_type __pos, size_type __n)
const;
391 find_first_not_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
393 return this->find_first_not_of(__str, __pos,
394 traits_type::length(__str));
398 find_last_not_of(basic_string_view __str,
399 size_type __pos = npos)
const noexcept
400 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
403 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
406 find_last_not_of(
const _CharT* __str,
407 size_type __pos, size_type __n)
const;
410 find_last_not_of(
const _CharT* __str,
411 size_type __pos = npos)
const noexcept
413 return this->find_last_not_of(__str, __pos,
414 traits_type::length(__str));
420 _S_compare(size_type __n1, size_type __n2) noexcept
426 :
static_cast<int>(difference_type(__n1 - __n2));
430 const _CharT* _M_str;
440 template<
typename _Tp>
444 template<
typename _CharT,
typename _Traits>
448 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
450 template<
typename _CharT,
typename _Traits>
454 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
456 template<
typename _CharT,
typename _Traits>
460 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
462 template<
typename _CharT,
typename _Traits>
466 {
return !(__x == __y); }
468 template<
typename _CharT,
typename _Traits>
472 {
return !(__x == __y); }
474 template<
typename _CharT,
typename _Traits>
478 {
return !(__x == __y); }
480 template<
typename _CharT,
typename _Traits>
482 operator< (basic_string_view<_CharT, _Traits> __x,
484 {
return __x.compare(__y) < 0; }
486 template<
typename _CharT,
typename _Traits>
488 operator< (basic_string_view<_CharT, _Traits> __x,
489 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
490 {
return __x.compare(__y) < 0; }
492 template<
typename _CharT,
typename _Traits>
494 operator< (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
496 {
return __x.compare(__y) < 0; }
498 template<
typename _CharT,
typename _Traits>
502 {
return __x.compare(__y) > 0; }
504 template<
typename _CharT,
typename _Traits>
508 {
return __x.compare(__y) > 0; }
510 template<
typename _CharT,
typename _Traits>
514 {
return __x.compare(__y) > 0; }
516 template<
typename _CharT,
typename _Traits>
518 operator<=(basic_string_view<_CharT, _Traits> __x,
520 {
return __x.compare(__y) <= 0; }
522 template<
typename _CharT,
typename _Traits>
524 operator<=(basic_string_view<_CharT, _Traits> __x,
525 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
526 {
return __x.compare(__y) <= 0; }
528 template<
typename _CharT,
typename _Traits>
530 operator<=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
532 {
return __x.compare(__y) <= 0; }
534 template<
typename _CharT,
typename _Traits>
538 {
return __x.compare(__y) >= 0; }
540 template<
typename _CharT,
typename _Traits>
544 {
return __x.compare(__y) >= 0; }
546 template<
typename _CharT,
typename _Traits>
550 {
return __x.compare(__y) >= 0; }
553 template<
typename _CharT,
typename _Traits>
555 operator<<(basic_ostream<_CharT, _Traits>& __os,
557 {
return __ostream_insert(__os, __str.data(), __str.size()); }
563 #ifdef _GLIBCXX_USE_WCHAR_T
566 #ifdef _GLIBCXX_USE_CHAR8_T
576 template<
typename _Tp>
581 :
public __hash_base<size_t, experimental::string_view>
584 operator()(
const experimental::string_view& __str)
const noexcept
585 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
592 #ifdef _GLIBCXX_USE_WCHAR_T
594 struct hash<experimental::wstring_view>
595 :
public __hash_base<size_t, wstring>
598 operator()(
const experimental::wstring_view& __s)
const noexcept
599 {
return std::_Hash_impl::hash(__s.data(),
600 __s.length() *
sizeof(wchar_t)); }
604 struct __is_fast_hash<hash<experimental::wstring_view>> :
std::false_type
608 #ifdef _GLIBCXX_USE_CHAR8_T
610 struct hash<experimental::u8string_view>
611 :
public __hash_base<size_t, experimental::u8string_view>
614 operator()(
const experimental::u8string_view& __s)
const noexcept
615 {
return std::_Hash_impl::hash(__s.data(), __s.length()); }
619 struct __is_fast_hash<hash<experimental::u8string_view>> :
std::false_type
624 struct hash<experimental::u16string_view>
625 :
public __hash_base<size_t, experimental::u16string_view>
628 operator()(
const experimental::u16string_view& __s)
const noexcept
629 {
return std::_Hash_impl::hash(__s.data(),
630 __s.length() *
sizeof(char16_t)); }
634 struct __is_fast_hash<hash<experimental::u16string_view>> :
std::false_type
638 struct hash<experimental::u32string_view>
639 :
public __hash_base<size_t, experimental::u32string_view>
642 operator()(
const experimental::u32string_view& __s)
const noexcept
643 {
return std::_Hash_impl::hash(__s.data(),
644 __s.length() *
sizeof(char32_t)); }
648 struct __is_fast_hash<hash<experimental::u32string_view>> :
std::false_type
651 namespace experimental
654 inline namespace literals
656 inline namespace string_view_literals
658 #pragma GCC diagnostic push
659 #pragma GCC diagnostic ignored "-Wliteral-suffix"
660 inline constexpr basic_string_view<char>
661 operator""sv(
const char* __str,
size_t __len) noexcept
662 {
return basic_string_view<char>{__str, __len}; }
664 #ifdef _GLIBCXX_USE_WCHAR_T
665 inline constexpr basic_string_view<wchar_t>
666 operator""sv(
const wchar_t* __str,
size_t __len) noexcept
667 {
return basic_string_view<wchar_t>{__str, __len}; }
670 #ifdef _GLIBCXX_USE_CHAR8_T
671 inline constexpr basic_string_view<char8_t>
672 operator""sv(
const char8_t* __str,
size_t __len) noexcept
673 {
return basic_string_view<char8_t>{__str, __len}; }
676 inline constexpr basic_string_view<char16_t>
677 operator""sv(
const char16_t* __str,
size_t __len) noexcept
678 {
return basic_string_view<char16_t>{__str, __len}; }
680 inline constexpr basic_string_view<char32_t>
681 operator""sv(
const char32_t* __str,
size_t __len) noexcept
682 {
return basic_string_view<char32_t>{__str, __len}; }
683 #pragma GCC diagnostic pop
688 _GLIBCXX_END_NAMESPACE_VERSION
693 #endif // __cplusplus <= 201103L
695 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW
Managing sequences of characters and character-like objects.
A non-owning reference to a string.
typename common_type< _Tp...>::type common_type_t
Alias template for common_type.
_GLIBCXX14_CONSTEXPR const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
static constexpr _Tp max() noexcept
Template class basic_ostream.
static constexpr _Tp min() noexcept
Primary class template hash.