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>
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
125 {
return this->_M_str; }
127 constexpr const_iterator
129 {
return this->_M_str + this->_M_len; }
131 constexpr const_iterator
133 {
return this->_M_str; }
135 constexpr const_iterator
137 {
return this->_M_str + this->_M_len; }
159 {
return this->_M_len; }
168 return (npos -
sizeof(size_type) -
sizeof(
void*))
169 /
sizeof(value_type) / 4;
172 _GLIBCXX_NODISCARD constexpr
bool
174 {
return this->_M_len == 0; }
178 constexpr
const _CharT&
179 operator[](size_type __pos)
const
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&
203 return *this->_M_str;
206 constexpr
const _CharT&
211 return *(this->_M_str + this->_M_len - 1);
214 constexpr
const _CharT*
216 {
return this->_M_str; }
221 remove_prefix(size_type __n)
223 __glibcxx_assert(this->_M_len >= __n);
229 remove_suffix(size_type __n)
230 { this->_M_len -= __n; }
233 swap(basic_string_view& __sv)
noexcept
243 template<
typename _Allocator>
246 return { this->_M_str, this->_M_len };
249 template<
typename _Allocator = std::allocator<_CharT>>
251 to_string(
const _Allocator& __alloc = _Allocator())
const
253 return { this->_M_str, this->_M_len, __alloc };
257 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
259 __glibcxx_requires_string_len(__str, __n);
260 if (__pos > this->_M_len)
261 __throw_out_of_range_fmt(__N(
"basic_string_view::copy: __pos "
262 "(which is %zu) > this->size() "
264 __pos, this->size());
265 size_type __rlen{
std::min(__n, size_type{this->_M_len - __pos})};
266 for (
auto __begin = this->_M_str + __pos,
267 __end = __begin + __rlen; __begin != __end;)
268 *__str++ = *__begin++;
275 constexpr basic_string_view
276 substr(size_type __pos = 0, size_type __n = npos)
const
278 return __pos <= this->_M_len
279 ? basic_string_view{this->_M_str + __pos,
280 std::min(__n, size_type{this->_M_len - __pos})}
281 : (__throw_out_of_range_fmt(__N(
"basic_string_view::substr: __pos "
282 "(which is %zu) > this->size() "
284 __pos, this->size()), basic_string_view{});
288 compare(basic_string_view __str)
const noexcept
290 int __ret = traits_type::compare(this->_M_str, __str._M_str,
291 std::min(this->_M_len, __str._M_len));
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
304 {
return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2)); }
307 compare(
const _CharT* __str)
const noexcept
308 {
return this->compare(basic_string_view{__str}); }
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
318 return this->substr(__pos1, __n1)
319 .compare(basic_string_view(__str, __n2));
323 find(basic_string_view __str, size_type __pos = 0)
const noexcept
324 {
return this->find(__str._M_str, __pos, __str._M_len); }
327 find(_CharT __c, size_type __pos=0)
const noexcept;
330 find(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
333 find(
const _CharT* __str, size_type __pos=0)
const noexcept
334 {
return this->find(__str, __pos, traits_type::length(__str)); }
337 rfind(basic_string_view __str, size_type __pos = npos)
const noexcept
338 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
341 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
344 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
347 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
348 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
351 find_first_of(basic_string_view __str, size_type __pos = 0)
const noexcept
352 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
355 find_first_of(_CharT __c, size_type __pos = 0)
const noexcept
356 {
return this->find(__c, __pos); }
359 find_first_of(
const _CharT* __str, size_type __pos, size_type __n)
const;
362 find_first_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
363 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
366 find_last_of(basic_string_view __str,
367 size_type __pos = npos)
const noexcept
368 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
371 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
372 {
return this->rfind(__c, __pos); }
375 find_last_of(
const _CharT* __str, size_type __pos, size_type __n)
const;
378 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
379 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
382 find_first_not_of(basic_string_view __str,
383 size_type __pos = 0)
const noexcept
384 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
387 find_first_not_of(_CharT __c, size_type __pos = 0)
const noexcept;
390 find_first_not_of(
const _CharT* __str,
391 size_type __pos, size_type __n)
const;
394 find_first_not_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
396 return this->find_first_not_of(__str, __pos,
397 traits_type::length(__str));
401 find_last_not_of(basic_string_view __str,
402 size_type __pos = npos)
const noexcept
403 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
406 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
409 find_last_not_of(
const _CharT* __str,
410 size_type __pos, size_type __n)
const;
413 find_last_not_of(
const _CharT* __str,
414 size_type __pos = npos)
const noexcept
416 return this->find_last_not_of(__str, __pos,
417 traits_type::length(__str));
423 _S_compare(size_type __n1, size_type __n2) noexcept
429 :
static_cast<int>(difference_type(__n1 - __n2));
433 const _CharT* _M_str;
443 template<
typename _Tp>
447 template<
typename _CharT,
typename _Traits>
451 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
453 template<
typename _CharT,
typename _Traits>
457 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
459 template<
typename _CharT,
typename _Traits>
463 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
465 template<
typename _CharT,
typename _Traits>
469 {
return !(__x == __y); }
471 template<
typename _CharT,
typename _Traits>
475 {
return !(__x == __y); }
477 template<
typename _CharT,
typename _Traits>
481 {
return !(__x == __y); }
483 template<
typename _CharT,
typename _Traits>
485 operator< (basic_string_view<_CharT, _Traits> __x,
487 {
return __x.compare(__y) < 0; }
489 template<
typename _CharT,
typename _Traits>
491 operator< (basic_string_view<_CharT, _Traits> __x,
492 __detail::__idt<basic_string_view<_CharT, _Traits>> __y)
noexcept
493 {
return __x.compare(__y) < 0; }
495 template<
typename _CharT,
typename _Traits>
497 operator< (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
499 {
return __x.compare(__y) < 0; }
501 template<
typename _CharT,
typename _Traits>
505 {
return __x.compare(__y) > 0; }
507 template<
typename _CharT,
typename _Traits>
511 {
return __x.compare(__y) > 0; }
513 template<
typename _CharT,
typename _Traits>
517 {
return __x.compare(__y) > 0; }
519 template<
typename _CharT,
typename _Traits>
521 operator<=(basic_string_view<_CharT, _Traits> __x,
523 {
return __x.compare(__y) <= 0; }
525 template<
typename _CharT,
typename _Traits>
527 operator<=(basic_string_view<_CharT, _Traits> __x,
528 __detail::__idt<basic_string_view<_CharT, _Traits>> __y)
noexcept
529 {
return __x.compare(__y) <= 0; }
531 template<
typename _CharT,
typename _Traits>
533 operator<=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
535 {
return __x.compare(__y) <= 0; }
537 template<
typename _CharT,
typename _Traits>
541 {
return __x.compare(__y) >= 0; }
543 template<
typename _CharT,
typename _Traits>
547 {
return __x.compare(__y) >= 0; }
549 template<
typename _CharT,
typename _Traits>
553 {
return __x.compare(__y) >= 0; }
556 template<
typename _CharT,
typename _Traits>
558 operator<<(basic_ostream<_CharT, _Traits>& __os,
560 {
return __ostream_insert(__os, __str.data(), __str.size()); }
566 #ifdef _GLIBCXX_USE_WCHAR_T
569 #ifdef _GLIBCXX_USE_CHAR8_T
579 template<
typename _Tp>
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()); }
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 _GLIBCXX_END_NAMESPACE_VERSION
696 #endif // __cplusplus <= 201103L
698 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW
Template class basic_ostream.
enable_if< ::__array_traits< _Tp, _Nm >::_Is_swappable::value >::type noexcept(noexcept(__one.swap(__two)))
swap
typename common_type< _Tp...>::type common_type_t
Alias template for common_type.
static constexpr _Tp max() noexcept
_GLIBCXX14_CONSTEXPR const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
A non-owning reference to a string.
Managing sequences of characters and character-like objects.
static constexpr _Tp min() noexcept
Primary class template hash.