33 #ifndef _GLIBCXX_EXPERIMENTAL_STRING_VIEW
34 #define _GLIBCXX_EXPERIMENTAL_STRING_VIEW 1
36 #pragma GCC system_header
38 #if __cplusplus <= 201103L
46 namespace std _GLIBCXX_VISIBILITY(default)
48 namespace experimental
50 inline namespace fundamentals_v1
52 _GLIBCXX_BEGIN_NAMESPACE_VERSION
54 #define __cpp_lib_experimental_string_view 201411
75 template<
typename _CharT,
typename _Traits = std::
char_traits<_CharT>>
81 using traits_type = _Traits;
82 using value_type = _CharT;
83 using pointer =
const _CharT*;
84 using const_pointer =
const _CharT*;
85 using reference =
const _CharT&;
86 using const_reference =
const _CharT&;
87 using const_iterator =
const _CharT*;
88 using iterator = const_iterator;
91 using size_type = size_t;
92 using difference_type = ptrdiff_t;
93 static constexpr size_type npos = size_type(-1);
99 : _M_len{0}, _M_str{
nullptr}
104 template<
typename _Allocator>
106 _Allocator>& __str) noexcept
107 : _M_len{__str.length()}, _M_str{__str.data()}
111 : _M_len{__str ==
nullptr ? 0 : traits_type::length(__str)},
115 constexpr basic_string_view(
const _CharT* __str, size_type __len)
121 operator=(
const basic_string_view&) noexcept =
default;
125 constexpr const_iterator
126 begin()
const noexcept
127 {
return this->_M_str; }
129 constexpr const_iterator
131 {
return this->_M_str + this->_M_len; }
133 constexpr const_iterator
134 cbegin()
const noexcept
135 {
return this->_M_str; }
137 constexpr const_iterator
138 cend()
const noexcept
139 {
return this->_M_str + this->_M_len; }
142 rbegin()
const noexcept
146 rend()
const noexcept
150 crbegin()
const noexcept
154 crend()
const noexcept
160 size()
const noexcept
161 {
return this->_M_len; }
164 length()
const noexcept
168 max_size()
const noexcept
170 return (npos -
sizeof(size_type) -
sizeof(
void*))
171 /
sizeof(value_type) / 4;
175 empty()
const noexcept
176 {
return this->_M_len == 0; }
180 constexpr
const _CharT&
181 operator[](size_type __pos)
const
185 return *(this->_M_str + __pos);
188 constexpr
const _CharT&
189 at(size_type __pos)
const
191 return __pos < this->_M_len
192 ? *(this->_M_str + __pos)
193 : (__throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
194 "(which is %zu) >= this->size() "
196 __pos, this->size()),
200 constexpr
const _CharT&
205 return *this->_M_str;
208 constexpr
const _CharT&
213 return *(this->_M_str + this->_M_len - 1);
216 constexpr
const _CharT*
217 data()
const noexcept
218 {
return this->_M_str; }
223 remove_prefix(size_type __n)
225 __glibcxx_assert(this->_M_len >= __n);
231 remove_suffix(size_type __n)
232 { this->_M_len -= __n; }
235 swap(basic_string_view& __sv) noexcept
245 template<
typename _Allocator>
248 return { this->_M_str, this->_M_len };
251 template<
typename _Allocator = std::allocator<_CharT>>
253 to_string(
const _Allocator& __alloc = _Allocator())
const
255 return { this->_M_str, this->_M_len, __alloc };
259 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const
261 __glibcxx_requires_string_len(__str, __n);
262 if (__pos > this->_M_len)
263 __throw_out_of_range_fmt(__N(
"basic_string_view::copy: __pos "
264 "(which is %zu) > this->size() "
266 __pos, this->size());
267 size_type __rlen{
std::min(__n, size_type{this->_M_len - __pos})};
268 for (
auto __begin = this->_M_str + __pos,
269 __end = __begin + __rlen; __begin != __end;)
270 *__str++ = *__begin++;
277 constexpr basic_string_view
278 substr(size_type __pos, size_type __n=npos)
const
280 return __pos <= this->_M_len
281 ? basic_string_view{this->_M_str + __pos,
282 std::min(__n, size_type{this->_M_len - __pos})}
283 : (__throw_out_of_range_fmt(__N(
"basic_string_view::substr: __pos "
284 "(which is %zu) > this->size() "
286 __pos, this->size()), basic_string_view{});
290 compare(basic_string_view __str)
const noexcept
292 int __ret = traits_type::compare(this->_M_str, __str._M_str,
293 std::min(this->_M_len, __str._M_len));
295 __ret = _S_compare(this->_M_len, __str._M_len);
300 compare(size_type __pos1, size_type __n1, basic_string_view __str)
const
301 {
return this->substr(__pos1, __n1).compare(__str); }
304 compare(size_type __pos1, size_type __n1,
305 basic_string_view __str, size_type __pos2, size_type __n2)
const
306 {
return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2)); }
309 compare(
const _CharT* __str)
const noexcept
310 {
return this->compare(basic_string_view{__str}); }
313 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
314 {
return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
317 compare(size_type __pos1, size_type __n1,
318 const _CharT* __str, size_type __n2)
const
320 return this->substr(__pos1, __n1)
321 .compare(basic_string_view(__str, __n2));
325 find(basic_string_view __str, size_type __pos = 0)
const noexcept
326 {
return this->find(__str._M_str, __pos, __str._M_len); }
329 find(_CharT __c, size_type __pos=0)
const noexcept;
332 find(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
335 find(
const _CharT* __str, size_type __pos=0)
const noexcept
336 {
return this->find(__str, __pos, traits_type::length(__str)); }
339 rfind(basic_string_view __str, size_type __pos = npos)
const noexcept
340 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
343 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
346 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
349 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
350 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
353 find_first_of(basic_string_view __str, size_type __pos = 0)
const noexcept
354 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
357 find_first_of(_CharT __c, size_type __pos = 0)
const noexcept
358 {
return this->find(__c, __pos); }
361 find_first_of(
const _CharT* __str, size_type __pos, size_type __n)
const;
364 find_first_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
365 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
368 find_last_of(basic_string_view __str,
369 size_type __pos = npos)
const noexcept
370 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
373 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
374 {
return this->rfind(__c, __pos); }
377 find_last_of(
const _CharT* __str, size_type __pos, size_type __n)
const;
380 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
381 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
384 find_first_not_of(basic_string_view __str,
385 size_type __pos = 0)
const noexcept
386 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
389 find_first_not_of(_CharT __c, size_type __pos = 0)
const noexcept;
392 find_first_not_of(
const _CharT* __str,
393 size_type __pos, size_type __n)
const;
396 find_first_not_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
398 return this->find_first_not_of(__str, __pos,
399 traits_type::length(__str));
403 find_last_not_of(basic_string_view __str,
404 size_type __pos = npos)
const noexcept
405 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
408 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
411 find_last_not_of(
const _CharT* __str,
412 size_type __pos, size_type __n)
const;
415 find_last_not_of(
const _CharT* __str,
416 size_type __pos = npos)
const noexcept
418 return this->find_last_not_of(__str, __pos,
419 traits_type::length(__str));
425 _S_compare(size_type __n1, size_type __n2) noexcept
431 :
static_cast<int>(difference_type{__n1 - __n2});
435 const _CharT* _M_str;
438 _GLIBCXX_END_NAMESPACE_VERSION
444 _GLIBCXX_BEGIN_NAMESPACE_VERSION
448 template<
typename _Tp>
449 using __idt = common_type_t<_Tp>;
450 _GLIBCXX_END_NAMESPACE_VERSION
453 _GLIBCXX_BEGIN_NAMESPACE_VERSION
455 template<
typename _CharT,
typename _Traits>
459 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
461 template<
typename _CharT,
typename _Traits>
465 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
467 template<
typename _CharT,
typename _Traits>
471 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
473 template<
typename _CharT,
typename _Traits>
477 {
return !(__x == __y); }
479 template<
typename _CharT,
typename _Traits>
483 {
return !(__x == __y); }
485 template<
typename _CharT,
typename _Traits>
489 {
return !(__x == __y); }
491 template<
typename _CharT,
typename _Traits>
493 operator< (basic_string_view<_CharT, _Traits> __x,
495 {
return __x.compare(__y) < 0; }
497 template<
typename _CharT,
typename _Traits>
499 operator< (basic_string_view<_CharT, _Traits> __x,
500 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
501 {
return __x.compare(__y) < 0; }
503 template<
typename _CharT,
typename _Traits>
505 operator< (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
507 {
return __x.compare(__y) < 0; }
509 template<
typename _CharT,
typename _Traits>
513 {
return __x.compare(__y) > 0; }
515 template<
typename _CharT,
typename _Traits>
519 {
return __x.compare(__y) > 0; }
521 template<
typename _CharT,
typename _Traits>
525 {
return __x.compare(__y) > 0; }
527 template<
typename _CharT,
typename _Traits>
529 operator<=(basic_string_view<_CharT, _Traits> __x,
531 {
return __x.compare(__y) <= 0; }
533 template<
typename _CharT,
typename _Traits>
535 operator<=(basic_string_view<_CharT, _Traits> __x,
536 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
537 {
return __x.compare(__y) <= 0; }
539 template<
typename _CharT,
typename _Traits>
541 operator<=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
543 {
return __x.compare(__y) <= 0; }
545 template<
typename _CharT,
typename _Traits>
549 {
return __x.compare(__y) >= 0; }
551 template<
typename _CharT,
typename _Traits>
555 {
return __x.compare(__y) >= 0; }
557 template<
typename _CharT,
typename _Traits>
561 {
return __x.compare(__y) >= 0; }
564 template<
typename _CharT,
typename _Traits>
566 operator<<(basic_ostream<_CharT, _Traits>& __os,
568 {
return __ostream_insert(__os, __str.data(), __str.size()); }
574 #ifdef _GLIBCXX_USE_WCHAR_T
577 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
582 _GLIBCXX_END_NAMESPACE_VERSION
589 _GLIBCXX_BEGIN_NAMESPACE_VERSION
590 template<
typename _Tp>
595 :
public __hash_base<size_t, experimental::string_view>
598 operator()(
const experimental::string_view& __str)
const noexcept
599 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
606 #ifdef _GLIBCXX_USE_WCHAR_T
608 struct hash<experimental::wstring_view>
609 :
public __hash_base<size_t, wstring>
612 operator()(
const experimental::wstring_view& __s)
const noexcept
613 {
return std::_Hash_impl::hash(__s.data(),
614 __s.length() *
sizeof(wchar_t)); }
618 struct __is_fast_hash<hash<experimental::wstring_view>> :
std::false_type
622 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
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 _GLIBCXX_END_NAMESPACE_VERSION
653 namespace experimental
656 inline namespace literals
658 inline namespace string_view_literals
660 _GLIBCXX_BEGIN_NAMESPACE_VERSION
662 inline constexpr basic_string_view<char>
663 operator""sv(
const char* __str,
size_t __len) noexcept
664 {
return basic_string_view<char>{__str, __len}; }
666 #ifdef _GLIBCXX_USE_WCHAR_T
667 inline constexpr basic_string_view<wchar_t>
668 operator""sv(
const wchar_t* __str,
size_t __len) noexcept
669 {
return basic_string_view<wchar_t>{__str, __len}; }
672 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
673 inline constexpr basic_string_view<char16_t>
674 operator""sv(
const char16_t* __str,
size_t __len) noexcept
675 {
return basic_string_view<char16_t>{__str, __len}; }
677 inline constexpr basic_string_view<char32_t>
678 operator""sv(
const char32_t* __str,
size_t __len) noexcept
679 {
return basic_string_view<char32_t>{__str, __len}; }
682 _GLIBCXX_END_NAMESPACE_VERSION
690 #endif // __cplusplus <= 201103L
692 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW
_GLIBCXX14_CONSTEXPR const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Primary class template hash.
A non-owning reference to a string.
Managing sequences of characters and character-like objects.
Template class basic_ostream.
static constexpr _Tp min() noexcept
static constexpr _Tp max() noexcept