34 #ifndef _GLIBCXX_EXPERIMENTAL_STRING_VIEW 35 #define _GLIBCXX_EXPERIMENTAL_STRING_VIEW 1 37 #ifdef _GLIBCXX_SYSHDR 38 #pragma GCC system_header 43 #if __cplusplus >= 201402L 50 namespace std _GLIBCXX_VISIBILITY(default)
52 _GLIBCXX_BEGIN_NAMESPACE_VERSION
54 namespace experimental
56 inline namespace fundamentals_v1
58 #define __cpp_lib_experimental_string_view 201411 79 template<
typename _CharT,
typename _Traits = std::
char_traits<_CharT>>
85 using traits_type = _Traits;
86 using value_type = _CharT;
87 using pointer = _CharT*;
88 using const_pointer =
const _CharT*;
89 using reference = _CharT&;
90 using const_reference =
const _CharT&;
91 using const_iterator =
const _CharT*;
92 using iterator = const_iterator;
95 using size_type = size_t;
96 using difference_type = ptrdiff_t;
97 static constexpr size_type npos = size_type(-1);
103 : _M_len{0}, _M_str{
nullptr}
108 template<
typename _Allocator>
110 _Allocator>& __str) noexcept
111 : _M_len{__str.length()}, _M_str{__str.data()}
115 : _M_len{__str ==
nullptr ? 0 : traits_type::length(__str)},
119 constexpr basic_string_view(
const _CharT* __str, size_type __len)
125 operator=(
const basic_string_view&) noexcept =
default;
129 constexpr const_iterator
130 begin()
const noexcept
131 {
return this->_M_str; }
133 constexpr const_iterator
135 {
return this->_M_str + this->_M_len; }
137 constexpr const_iterator
138 cbegin()
const noexcept
139 {
return this->_M_str; }
141 constexpr const_iterator
142 cend()
const noexcept
143 {
return this->_M_str + this->_M_len; }
146 rbegin()
const noexcept
150 rend()
const noexcept
154 crbegin()
const noexcept
158 crend()
const noexcept
164 size()
const noexcept
165 {
return this->_M_len; }
168 length()
const noexcept
172 max_size()
const noexcept
174 return (npos -
sizeof(size_type) -
sizeof(
void*))
175 /
sizeof(value_type) / 4;
178 _GLIBCXX_NODISCARD constexpr
bool 179 empty()
const noexcept
180 {
return this->_M_len == 0; }
184 constexpr
const _CharT&
185 operator[](size_type __pos)
const 187 __glibcxx_assert(__pos < this->_M_len);
188 return *(this->_M_str + __pos);
191 constexpr
const _CharT&
192 at(size_type __pos)
const 194 return __pos < this->_M_len
195 ? *(this->_M_str + __pos)
196 : (__throw_out_of_range_fmt(__N(
"basic_string_view::at: __pos " 197 "(which is %zu) >= this->size() " 199 __pos, this->size()),
203 constexpr
const _CharT&
206 __glibcxx_assert(this->_M_len > 0);
207 return *this->_M_str;
210 constexpr
const _CharT&
213 __glibcxx_assert(this->_M_len > 0);
214 return *(this->_M_str + this->_M_len - 1);
217 constexpr
const _CharT*
218 data()
const noexcept
219 {
return this->_M_str; }
224 remove_prefix(size_type __n)
226 __glibcxx_assert(this->_M_len >= __n);
232 remove_suffix(size_type __n)
233 { this->_M_len -= __n; }
236 swap(basic_string_view& __sv) noexcept
246 template<
typename _Allocator>
249 return { this->_M_str, this->_M_len };
252 template<
typename _Allocator = std::allocator<_CharT>>
254 to_string(
const _Allocator& __alloc = _Allocator())
const 256 return { this->_M_str, this->_M_len, __alloc };
260 copy(_CharT* __str, size_type __n, size_type __pos = 0)
const 262 __glibcxx_requires_string_len(__str, __n);
263 if (__pos > this->_M_len)
264 __throw_out_of_range_fmt(__N(
"basic_string_view::copy: __pos " 265 "(which is %zu) > this->size() " 267 __pos, this->size());
268 size_type __rlen{
std::min(__n, size_type{this->_M_len - __pos})};
269 for (
auto __begin = this->_M_str + __pos,
270 __end = __begin + __rlen; __begin != __end;)
271 *__str++ = *__begin++;
278 constexpr basic_string_view
279 substr(size_type __pos = 0, size_type __n = npos)
const 281 return __pos <= this->_M_len
282 ? basic_string_view{this->_M_str + __pos,
283 std::min(__n, size_type{this->_M_len - __pos})}
284 : (__throw_out_of_range_fmt(__N(
"basic_string_view::substr: __pos " 285 "(which is %zu) > this->size() " 287 __pos, this->size()), basic_string_view{});
291 compare(basic_string_view __str)
const noexcept
293 int __ret = traits_type::compare(this->_M_str, __str._M_str,
294 std::min(this->_M_len, __str._M_len));
296 __ret = _S_compare(this->_M_len, __str._M_len);
301 compare(size_type __pos1, size_type __n1, basic_string_view __str)
const 302 {
return this->substr(__pos1, __n1).compare(__str); }
305 compare(size_type __pos1, size_type __n1,
306 basic_string_view __str, size_type __pos2, size_type __n2)
const 307 {
return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2)); }
310 compare(
const _CharT* __str)
const noexcept
311 {
return this->compare(basic_string_view{__str}); }
314 compare(size_type __pos1, size_type __n1,
const _CharT* __str)
const 315 {
return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
318 compare(size_type __pos1, size_type __n1,
319 const _CharT* __str, size_type __n2)
const 321 return this->substr(__pos1, __n1)
322 .compare(basic_string_view(__str, __n2));
326 find(basic_string_view __str, size_type __pos = 0)
const noexcept
327 {
return this->find(__str._M_str, __pos, __str._M_len); }
330 find(_CharT __c, size_type __pos=0)
const noexcept;
333 find(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
336 find(
const _CharT* __str, size_type __pos=0)
const noexcept
337 {
return this->find(__str, __pos, traits_type::length(__str)); }
340 rfind(basic_string_view __str, size_type __pos = npos)
const noexcept
341 {
return this->rfind(__str._M_str, __pos, __str._M_len); }
344 rfind(_CharT __c, size_type __pos = npos)
const noexcept;
347 rfind(
const _CharT* __str, size_type __pos, size_type __n)
const noexcept;
350 rfind(
const _CharT* __str, size_type __pos = npos)
const noexcept
351 {
return this->rfind(__str, __pos, traits_type::length(__str)); }
354 find_first_of(basic_string_view __str, size_type __pos = 0)
const noexcept
355 {
return this->find_first_of(__str._M_str, __pos, __str._M_len); }
358 find_first_of(_CharT __c, size_type __pos = 0)
const noexcept
359 {
return this->find(__c, __pos); }
362 find_first_of(
const _CharT* __str, size_type __pos, size_type __n)
const;
365 find_first_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
366 {
return this->find_first_of(__str, __pos, traits_type::length(__str)); }
369 find_last_of(basic_string_view __str,
370 size_type __pos = npos)
const noexcept
371 {
return this->find_last_of(__str._M_str, __pos, __str._M_len); }
374 find_last_of(_CharT __c, size_type __pos=npos)
const noexcept
375 {
return this->rfind(__c, __pos); }
378 find_last_of(
const _CharT* __str, size_type __pos, size_type __n)
const;
381 find_last_of(
const _CharT* __str, size_type __pos = npos)
const noexcept
382 {
return this->find_last_of(__str, __pos, traits_type::length(__str)); }
385 find_first_not_of(basic_string_view __str,
386 size_type __pos = 0)
const noexcept
387 {
return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
390 find_first_not_of(_CharT __c, size_type __pos = 0)
const noexcept;
393 find_first_not_of(
const _CharT* __str,
394 size_type __pos, size_type __n)
const;
397 find_first_not_of(
const _CharT* __str, size_type __pos = 0)
const noexcept
399 return this->find_first_not_of(__str, __pos,
400 traits_type::length(__str));
404 find_last_not_of(basic_string_view __str,
405 size_type __pos = npos)
const noexcept
406 {
return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
409 find_last_not_of(_CharT __c, size_type __pos = npos)
const noexcept;
412 find_last_not_of(
const _CharT* __str,
413 size_type __pos, size_type __n)
const;
416 find_last_not_of(
const _CharT* __str,
417 size_type __pos = npos)
const noexcept
419 return this->find_last_not_of(__str, __pos,
420 traits_type::length(__str));
426 _S_compare(size_type __n1, size_type __n2) noexcept
432 :
static_cast<int>(difference_type(__n1 - __n2));
436 const _CharT* _M_str;
446 template<
typename _CharT,
typename _Traits>
450 {
return __x.size() == __y.size() && __x.compare(__y) == 0; }
452 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>
476 {
return !(__x == __y); }
478 template<
typename _CharT,
typename _Traits>
482 {
return !(__x == __y); }
484 template<
typename _CharT,
typename _Traits>
486 operator< (basic_string_view<_CharT, _Traits> __x,
488 {
return __x.compare(__y) < 0; }
490 template<
typename _CharT,
typename _Traits>
492 operator< (basic_string_view<_CharT, _Traits> __x,
493 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
495 {
return __x.compare(__y) < 0; }
497 template<
typename _CharT,
typename _Traits>
499 operator< (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
501 {
return __x.compare(__y) < 0; }
503 template<
typename _CharT,
typename _Traits>
507 {
return __x.compare(__y) > 0; }
509 template<
typename _CharT,
typename _Traits>
514 {
return __x.compare(__y) > 0; }
516 template<
typename _CharT,
typename _Traits>
520 {
return __x.compare(__y) > 0; }
522 template<
typename _CharT,
typename _Traits>
524 operator<=(basic_string_view<_CharT, _Traits> __x,
526 {
return __x.compare(__y) <= 0; }
528 template<
typename _CharT,
typename _Traits>
530 operator<=(basic_string_view<_CharT, _Traits> __x,
531 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
533 {
return __x.compare(__y) <= 0; }
535 template<
typename _CharT,
typename _Traits>
537 operator<=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
539 {
return __x.compare(__y) <= 0; }
541 template<
typename _CharT,
typename _Traits>
545 {
return __x.compare(__y) >= 0; }
547 template<
typename _CharT,
typename _Traits>
552 {
return __x.compare(__y) >= 0; }
554 template<
typename _CharT,
typename _Traits>
558 {
return __x.compare(__y) >= 0; }
561 template<
typename _CharT,
typename _Traits>
563 operator<<(basic_ostream<_CharT, _Traits>& __os,
565 {
return __ostream_insert(__os, __str.data(), __str.size()); }
572 #ifdef _GLIBCXX_USE_CHAR8_T 582 template<
typename _Tp>
587 :
public __hash_base<size_t, experimental::string_view>
590 operator()(
const experimental::string_view& __str)
const noexcept
591 {
return std::_Hash_impl::hash(__str.data(), __str.length()); }
600 :
public __hash_base<size_t, wstring>
603 operator()(
const experimental::wstring_view& __s)
const noexcept
604 {
return std::_Hash_impl::hash(__s.data(),
605 __s.length() *
sizeof(wchar_t)); }
612 #ifdef _GLIBCXX_USE_CHAR8_T 614 struct hash<experimental::u8string_view>
615 :
public __hash_base<size_t, experimental::u8string_view>
618 operator()(
const experimental::u8string_view& __s)
const noexcept
619 {
return std::_Hash_impl::hash(__s.data(), __s.length()); }
629 :
public __hash_base<size_t, experimental::u16string_view>
632 operator()(
const experimental::u16string_view& __s)
const noexcept
633 {
return std::_Hash_impl::hash(__s.data(),
634 __s.length() *
sizeof(char16_t)); }
643 :
public __hash_base<size_t, experimental::u32string_view>
646 operator()(
const experimental::u32string_view& __s)
const noexcept
647 {
return std::_Hash_impl::hash(__s.data(),
648 __s.length() *
sizeof(char32_t)); }
655 namespace experimental
658 inline namespace literals
660 inline namespace string_view_literals
662 #pragma GCC diagnostic push 663 #pragma GCC diagnostic ignored "-Wliteral-suffix" 665 operator""sv(
const char* __str,
size_t __len) noexcept
669 operator""sv(
const wchar_t* __str,
size_t __len) noexcept
672 #ifdef _GLIBCXX_USE_CHAR8_T 674 operator""sv(
const char8_t* __str,
size_t __len) noexcept
679 operator""sv(
const char16_t* __str,
size_t __len) noexcept
683 operator""sv(
const char32_t* __str,
size_t __len) noexcept
685 #pragma GCC diagnostic pop 690 #if __cpp_lib_concepts 694 template<
typename _CharT,
typename _Traits>
695 inline constexpr
bool 696 enable_borrowed_range<experimental::basic_string_view<_CharT, _Traits>>
700 template<
typename _CharT,
typename _Traits>
701 inline constexpr
bool 702 enable_view<experimental::basic_string_view<_CharT, _Traits>> =
true;
706 _GLIBCXX_END_NAMESPACE_VERSION
711 #endif // __cplusplus <= 201103L 713 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW
Template class basic_ostream.
ISO C++ entities toplevel namespace is std.
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
A non-owning reference to a string.
static constexpr _Tp max() noexcept
static constexpr _Tp min() noexcept
Primary class template hash.
Managing sequences of characters and character-like objects.
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.