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
237 std::swap(this->_M_len, __sv._M_len);
238 std::swap(this->_M_str, __sv._M_str);
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++;
276 constexpr basic_string_view
277 substr(size_type __pos, size_type __n=npos)
const
279 return __pos <= this->_M_len
280 ? basic_string_view{this->_M_str + __pos,
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() "
285 __pos, this->size()), basic_string_view{});
289 compare(basic_string_view __str)
const noexcept
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);
299 compare(size_type __pos1, size_type __n1, basic_string_view __str)
const
300 {
return this->substr(__pos1, __n1).compare(__str); }
303 compare(size_type __pos1, size_type __n1,
304 basic_string_view __str, size_type __pos2, size_type __n2)
const
305 {
return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2)); }
308 compare(
const _CharT* __str)
const noexcept
309 {
return this->compare(basic_string_view{__str}); }
312 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const
313 {
return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
316 compare(size_type __pos1, size_type __n1,
317 const _CharT* __str, size_type __n2)
const
319 return this->substr(__pos1, __n1)
320 .compare(basic_string_view(__str, __n2));
324 find(basic_string_view __str, size_type __pos = 0)
const noexcept
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)); }
338 rfind(basic_string_view __str, size_type __pos = npos)
const noexcept
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)); }
352 find_first_of(basic_string_view __str, size_type __pos = 0)
const noexcept
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)); }
367 find_last_of(basic_string_view __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)); }
383 find_first_not_of(basic_string_view __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));
402 find_last_not_of(basic_string_view __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;
437 _GLIBCXX_END_NAMESPACE_VERSION
443 _GLIBCXX_BEGIN_NAMESPACE_VERSION
447 template<
typename _Tp>
448 using __idt = common_type_t<_Tp>;
449 _GLIBCXX_END_NAMESPACE_VERSION
452 _GLIBCXX_BEGIN_NAMESPACE_VERSION
454 template<
typename _CharT,
typename _Traits>
458 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
460 template<
typename _CharT,
typename _Traits>
464 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
466 template<
typename _CharT,
typename _Traits>
470 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
472 template<
typename _CharT,
typename _Traits>
476 {
return !(__x == __y); }
478 template<
typename _CharT,
typename _Traits>
482 {
return !(__x == __y); }
484 template<
typename _CharT,
typename _Traits>
488 {
return !(__x == __y); }
490 template<
typename _CharT,
typename _Traits>
492 operator< (basic_string_view<_CharT, _Traits> __x,
494 {
return __x.compare(__y) < 0; }
496 template<
typename _CharT,
typename _Traits>
498 operator< (basic_string_view<_CharT, _Traits> __x,
499 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
500 {
return __x.compare(__y) < 0; }
502 template<
typename _CharT,
typename _Traits>
504 operator< (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
506 {
return __x.compare(__y) < 0; }
508 template<
typename _CharT,
typename _Traits>
512 {
return __x.compare(__y) > 0; }
514 template<
typename _CharT,
typename _Traits>
518 {
return __x.compare(__y) > 0; }
520 template<
typename _CharT,
typename _Traits>
524 {
return __x.compare(__y) > 0; }
526 template<
typename _CharT,
typename _Traits>
528 operator<=(basic_string_view<_CharT, _Traits> __x,
530 {
return __x.compare(__y) <= 0; }
532 template<
typename _CharT,
typename _Traits>
534 operator<=(basic_string_view<_CharT, _Traits> __x,
535 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
536 {
return __x.compare(__y) <= 0; }
538 template<
typename _CharT,
typename _Traits>
540 operator<=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
542 {
return __x.compare(__y) <= 0; }
544 template<
typename _CharT,
typename _Traits>
548 {
return __x.compare(__y) >= 0; }
550 template<
typename _CharT,
typename _Traits>
554 {
return __x.compare(__y) >= 0; }
556 template<
typename _CharT,
typename _Traits>
560 {
return __x.compare(__y) >= 0; }
563 template<
typename _CharT,
typename _Traits>
565 operator<<(basic_ostream<_CharT, _Traits>& __os,
567 {
return __ostream_insert(__os, __str.data(), __str.size()); }
573 #ifdef _GLIBCXX_USE_WCHAR_T
576 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
581 _GLIBCXX_END_NAMESPACE_VERSION
588 _GLIBCXX_BEGIN_NAMESPACE_VERSION
589 template<
typename _Tp>
594 :
public __hash_base<size_t, experimental::string_view>
597 operator()(
const experimental::string_view& __str)
const noexcept
598 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
605 #ifdef _GLIBCXX_USE_WCHAR_T
607 struct hash<experimental::wstring_view>
608 :
public __hash_base<size_t, wstring>
611 operator()(
const experimental::wstring_view& __s)
const noexcept
612 {
return std::_Hash_impl::hash(__s.data(),
613 __s.length() *
sizeof(wchar_t)); }
617 struct __is_fast_hash<hash<experimental::wstring_view>> :
std::false_type
621 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
623 struct hash<experimental::u16string_view>
624 :
public __hash_base<size_t, experimental::u16string_view>
627 operator()(
const experimental::u16string_view& __s)
const noexcept
628 {
return std::_Hash_impl::hash(__s.data(),
629 __s.length() *
sizeof(char16_t)); }
633 struct __is_fast_hash<hash<experimental::u16string_view>> :
std::false_type
637 struct hash<experimental::u32string_view>
638 :
public __hash_base<size_t, experimental::u32string_view>
641 operator()(
const experimental::u32string_view& __s)
const noexcept
642 {
return std::_Hash_impl::hash(__s.data(),
643 __s.length() *
sizeof(char32_t)); }
647 struct __is_fast_hash<hash<experimental::u32string_view>> :
std::false_type
650 _GLIBCXX_END_NAMESPACE_VERSION
652 namespace experimental
655 inline namespace literals
657 inline namespace string_view_literals
659 _GLIBCXX_BEGIN_NAMESPACE_VERSION
661 inline constexpr basic_string_view<char>
662 operator""sv(
const char* __str,
size_t __len)
663 {
return basic_string_view<char>{__str, __len}; }
665 #ifdef _GLIBCXX_USE_WCHAR_T
666 inline constexpr basic_string_view<wchar_t>
667 operator""sv(
const wchar_t* __str,
size_t __len)
668 {
return basic_string_view<wchar_t>{__str, __len}; }
671 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
672 inline constexpr basic_string_view<char16_t>
673 operator""sv(
const char16_t* __str,
size_t __len)
674 {
return basic_string_view<char16_t>{__str, __len}; }
676 inline constexpr basic_string_view<char32_t>
677 operator""sv(
const char32_t* __str,
size_t __len)
678 {
return basic_string_view<char32_t>{__str, __len}; }
681 _GLIBCXX_END_NAMESPACE_VERSION
689 #endif // __cplusplus <= 201103L
691 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW
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
_GLIBCXX14_CONSTEXPR const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
static constexpr _Tp max() noexcept