33 #ifndef _GLIBCXX_EXPERIMENTAL_STRING_VIEW
34 #define _GLIBCXX_EXPERIMENTAL_STRING_VIEW 1
36 #pragma GCC system_header
38 #if __cplusplus <= 201103L
45 namespace std _GLIBCXX_VISIBILITY(default)
47 namespace experimental
49 inline namespace fundamentals_v1
51 _GLIBCXX_BEGIN_NAMESPACE_VERSION
53 #define __cpp_lib_experimental_string_view 201411
74 template<
typename _CharT,
typename _Traits = std::
char_traits<_CharT>>
75 class basic_string_view
80 using traits_type = _Traits;
81 using value_type = _CharT;
82 using pointer =
const _CharT*;
83 using const_pointer =
const _CharT*;
84 using reference =
const _CharT&;
85 using const_reference =
const _CharT&;
86 using const_iterator =
const _CharT*;
87 using iterator = const_iterator;
89 using reverse_iterator = const_reverse_iterator;
90 using size_type = size_t;
91 using difference_type = ptrdiff_t;
92 static constexpr size_type npos = size_type(-1);
97 basic_string_view() noexcept
98 : _M_len{0}, _M_str{
nullptr}
101 constexpr basic_string_view(
const basic_string_view&) noexcept = default;
103 template<typename _Allocator>
104 basic_string_view(const basic_string<_CharT, _Traits,
105 _Allocator>& __str) noexcept
106 : _M_len{__str.length()}, _M_str{__str.data()}
109 constexpr basic_string_view(
const _CharT* __str)
110 : _M_len{__str ==
nullptr ? 0 : traits_type::length(__str)},
114 constexpr basic_string_view(
const _CharT* __str, size_type __len)
120 operator=(
const basic_string_view&) noexcept = default;
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;
174 empty() const noexcept
175 {
return this->_M_len == 0; }
179 constexpr
const _CharT&
184 return *(this->_M_str + __pos);
187 constexpr
const _CharT&
188 at(size_type __pos)
const
190 return __pos < this->_M_len
191 ? *(this->_M_str + __pos)
192 : (__throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos "
193 "(which is %zu) >= this->size() "
195 __pos, this->
size()),
199 constexpr
const _CharT&
204 return *this->_M_str;
207 constexpr
const _CharT&
212 return *(this->_M_str + this->_M_len - 1);
215 constexpr
const _CharT*
216 data() const noexcept
217 {
return this->_M_str; }
222 remove_prefix(size_type __n)
224 _GLIBCXX_DEBUG_ASSERT(this->_M_len >= __n);
230 remove_suffix(size_type __n)
231 { this->_M_len -= __n; }
234 swap(basic_string_view& __sv) noexcept
243 template<
typename _Allocator>
244 explicit operator basic_string<_CharT, _Traits, _Allocator>()
const
246 return { this->_M_str, this->_M_len };
249 template<
typename _Allocator = std::allocator<_CharT>>
250 basic_string<_CharT, _Traits, _Allocator>
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, 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));
422 static constexpr
const int
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 =
void>
445 {
typedef _Tp type; };
448 struct __identity<void>;
450 template<
typename _Tp>
451 using __idt =
typename __identity<_Tp>::type;
454 template<
typename _CharT,
typename _Traits>
456 operator==(basic_string_view<_CharT, _Traits> __x,
457 basic_string_view<_CharT, _Traits> __y) noexcept
458 {
return __x.compare(__y) == 0; }
460 template<
typename _CharT,
typename _Traits>
462 operator==(basic_string_view<_CharT, _Traits> __x,
463 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
464 {
return __x.compare(__y) == 0; }
466 template<
typename _CharT,
typename _Traits>
468 operator==(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
469 basic_string_view<_CharT, _Traits> __y) noexcept
470 {
return __x.compare(__y) == 0; }
472 template<
typename _CharT,
typename _Traits>
474 operator!=(basic_string_view<_CharT, _Traits> __x,
475 basic_string_view<_CharT, _Traits> __y) noexcept
476 {
return !(__x == __y); }
478 template<
typename _CharT,
typename _Traits>
480 operator!=(basic_string_view<_CharT, _Traits> __x,
481 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
482 {
return !(__x == __y); }
484 template<
typename _CharT,
typename _Traits>
486 operator!=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
487 basic_string_view<_CharT, _Traits> __y) noexcept
488 {
return !(__x == __y); }
490 template<
typename _CharT,
typename _Traits>
492 operator< (basic_string_view<_CharT, _Traits> __x,
493 basic_string_view<_CharT, _Traits> __y) noexcept
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,
505 basic_string_view<_CharT, _Traits> __y) noexcept
506 {
return __x.compare(__y) < 0; }
508 template<
typename _CharT,
typename _Traits>
510 operator> (basic_string_view<_CharT, _Traits> __x,
511 basic_string_view<_CharT, _Traits> __y) noexcept
512 {
return __x.compare(__y) > 0; }
514 template<
typename _CharT,
typename _Traits>
516 operator> (basic_string_view<_CharT, _Traits> __x,
517 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
518 {
return __x.compare(__y) > 0; }
520 template<
typename _CharT,
typename _Traits>
522 operator> (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
523 basic_string_view<_CharT, _Traits> __y) noexcept
524 {
return __x.compare(__y) > 0; }
526 template<
typename _CharT,
typename _Traits>
528 operator<=(basic_string_view<_CharT, _Traits> __x,
529 basic_string_view<_CharT, _Traits> __y) noexcept
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,
541 basic_string_view<_CharT, _Traits> __y) noexcept
542 {
return __x.compare(__y) <= 0; }
544 template<
typename _CharT,
typename _Traits>
546 operator>=(basic_string_view<_CharT, _Traits> __x,
547 basic_string_view<_CharT, _Traits> __y) noexcept
548 {
return __x.compare(__y) >= 0; }
550 template<
typename _CharT,
typename _Traits>
552 operator>=(basic_string_view<_CharT, _Traits> __x,
553 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
554 {
return __x.compare(__y) >= 0; }
556 template<
typename _CharT,
typename _Traits>
558 operator>=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
559 basic_string_view<_CharT, _Traits> __y) noexcept
560 {
return __x.compare(__y) >= 0; }
563 template<
typename _CharT,
typename _Traits>
564 inline basic_ostream<_CharT, _Traits>&
565 operator<<(basic_ostream<_CharT, _Traits>& __os,
566 basic_string_view<_CharT,_Traits> __str)
567 {
return __ostream_insert(__os, __str.data(), __str.size()); }
572 using string_view = basic_string_view<char>;
573 #ifdef _GLIBCXX_USE_WCHAR_T
574 using wstring_view = basic_string_view<wchar_t>;
576 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
577 using u16string_view = basic_string_view<char16_t>;
578 using u32string_view = basic_string_view<char32_t>;
581 _GLIBCXX_END_NAMESPACE_VERSION
588 _GLIBCXX_BEGIN_NAMESPACE_VERSION
589 template<
typename _Tp>
593 struct hash<experimental::string_view>
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()); }
602 struct __is_fast_hash<hash<experimental::string_view>> :
std::false_type
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
654 _GLIBCXX_BEGIN_NAMESPACE_VERSION
657 inline namespace literals
659 inline namespace string_view_literals
662 inline constexpr basic_string_view<char>
663 operator""sv(
const char* __str,
size_t __len)
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)
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)
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)
679 {
return basic_string_view<char32_t>{__str, __len}; }
685 _GLIBCXX_END_NAMESPACE_VERSION
689 #include <experimental/string_view.tcc>
691 #endif // __cplusplus <= 201103L
693 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW
_GLIBCXX14_CONSTEXPR const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
std::basic_string< _CharT, _Traits, _Alloc > to_string() const
Returns a character interpretation of the bitset.
reference operator[](size_t __position)
Array-indexing support.
static constexpr _Tp max() noexcept
constexpr const _Tp * end(initializer_list< _Tp > __ils) noexcept
Return an iterator pointing to one past the last element of the initializer_list. ...
constexpr const _Tp * begin(initializer_list< _Tp > __ils) noexcept
Return an iterator pointing to the first element of the initializer_list.
static constexpr _Tp min() noexcept
void swap(basic_filebuf< _CharT, _Traits > &__x, basic_filebuf< _CharT, _Traits > &__y)
Swap specialization for filebufs.
constexpr size_t size() const noexcept
Returns the total number of bits.