1 // Components for manipulating non-owning sequences of characters -*- C++ -*-
3 // Copyright (C) 2013-2021 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
26 * This is a Standard C++ Library header.
30 // N3762 basic_string_view library
33 #ifndef _GLIBCXX_STRING_VIEW
34 #define _GLIBCXX_STRING_VIEW 1
36 #pragma GCC system_header
38 #if __cplusplus >= 201703L
41 #include <bits/char_traits.h>
42 #include <bits/functional_hash.h>
43 #include <bits/range_access.h>
44 #include <bits/ranges_base.h>
45 #include <bits/ostream_insert.h>
46 #include <ext/numeric_traits.h>
48 namespace std _GLIBCXX_VISIBILITY(default)
50 _GLIBCXX_BEGIN_NAMESPACE_VERSION
52 # define __cpp_lib_string_view 201803L
53 #if __cplusplus > 201703L
54 # define __cpp_lib_constexpr_string_view 201811L
57 // Helper for basic_string and basic_string_view members.
59 __sv_check(size_t __size, size_t __pos, const char* __s)
62 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > __size "
63 "(which is %zu)"), __s, __pos, __size);
67 // Helper for basic_string members.
68 // NB: __sv_limit doesn't check for a bad __pos value.
70 __sv_limit(size_t __size, size_t __pos, size_t __off) noexcept
72 const bool __testoff = __off < __size - __pos;
73 return __testoff ? __off : __size - __pos;
77 * @class basic_string_view <string_view>
78 * @brief A non-owning reference to a string.
83 * @tparam _CharT Type of character
84 * @tparam _Traits Traits for character type, defaults to
85 * char_traits<_CharT>.
87 * A basic_string_view looks like this:
94 template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
95 class basic_string_view
97 static_assert(!is_array_v<_CharT>);
98 static_assert(is_trivial_v<_CharT> && is_standard_layout_v<_CharT>);
99 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
104 using traits_type = _Traits;
105 using value_type = _CharT;
106 using pointer = value_type*;
107 using const_pointer = const value_type*;
108 using reference = value_type&;
109 using const_reference = const value_type&;
110 using const_iterator = const value_type*;
111 using iterator = const_iterator;
112 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
113 using reverse_iterator = const_reverse_iterator;
114 using size_type = size_t;
115 using difference_type = ptrdiff_t;
116 static constexpr size_type npos = size_type(-1);
118 // [string.view.cons], construction and assignment
121 basic_string_view() noexcept
122 : _M_len{0}, _M_str{nullptr}
125 constexpr basic_string_view(const basic_string_view&) noexcept = default;
127 __attribute__((__nonnull__)) constexpr
128 basic_string_view(const _CharT* __str) noexcept
129 : _M_len{traits_type::length(__str)},
134 basic_string_view(const _CharT* __str, size_type __len) noexcept
135 : _M_len{__len}, _M_str{__str}
138 #if __cplusplus > 201703L && __cpp_lib_concepts
139 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
140 requires same_as<iter_value_t<_It>, _CharT>
141 && (!convertible_to<_End, size_type>)
143 basic_string_view(_It __first, _End __last)
144 : _M_len(__last - __first), _M_str(std::to_address(__first))
148 constexpr basic_string_view&
149 operator=(const basic_string_view&) noexcept = default;
151 // [string.view.iterators], iterator support
153 constexpr const_iterator
154 begin() const noexcept
155 { return this->_M_str; }
157 constexpr const_iterator
159 { return this->_M_str + this->_M_len; }
161 constexpr const_iterator
162 cbegin() const noexcept
163 { return this->_M_str; }
165 constexpr const_iterator
166 cend() const noexcept
167 { return this->_M_str + this->_M_len; }
169 constexpr const_reverse_iterator
170 rbegin() const noexcept
171 { return const_reverse_iterator(this->end()); }
173 constexpr const_reverse_iterator
174 rend() const noexcept
175 { return const_reverse_iterator(this->begin()); }
177 constexpr const_reverse_iterator
178 crbegin() const noexcept
179 { return const_reverse_iterator(this->end()); }
181 constexpr const_reverse_iterator
182 crend() const noexcept
183 { return const_reverse_iterator(this->begin()); }
185 // [string.view.capacity], capacity
188 size() const noexcept
189 { return this->_M_len; }
192 length() const noexcept
196 max_size() const noexcept
198 return (npos - sizeof(size_type) - sizeof(void*))
199 / sizeof(value_type) / 4;
202 [[nodiscard]] constexpr bool
203 empty() const noexcept
204 { return this->_M_len == 0; }
206 // [string.view.access], element access
208 constexpr const_reference
209 operator[](size_type __pos) const noexcept
211 __glibcxx_assert(__pos < this->_M_len);
212 return *(this->_M_str + __pos);
215 constexpr const_reference
216 at(size_type __pos) const
219 __throw_out_of_range_fmt(__N("basic_string_view::at: __pos "
220 "(which is %zu) >= this->size() "
221 "(which is %zu)"), __pos, this->size());
222 return *(this->_M_str + __pos);
225 constexpr const_reference
226 front() const noexcept
228 __glibcxx_assert(this->_M_len > 0);
229 return *this->_M_str;
232 constexpr const_reference
233 back() const noexcept
235 __glibcxx_assert(this->_M_len > 0);
236 return *(this->_M_str + this->_M_len - 1);
239 constexpr const_pointer
240 data() const noexcept
241 { return this->_M_str; }
243 // [string.view.modifiers], modifiers:
246 remove_prefix(size_type __n) noexcept
248 __glibcxx_assert(this->_M_len >= __n);
254 remove_suffix(size_type __n) noexcept
255 { this->_M_len -= __n; }
258 swap(basic_string_view& __sv) noexcept
265 // [string.view.ops], string operations:
269 copy(_CharT* __str, size_type __n, size_type __pos = 0) const
271 __glibcxx_requires_string_len(__str, __n);
272 __pos = std::__sv_check(size(), __pos, "basic_string_view::copy");
273 const size_type __rlen = std::min(__n, _M_len - __pos);
274 // _GLIBCXX_RESOLVE_LIB_DEFECTS
275 // 2777. basic_string_view::copy should use char_traits::copy
276 traits_type::copy(__str, data() + __pos, __rlen);
280 constexpr basic_string_view
281 substr(size_type __pos = 0, size_type __n = npos) const noexcept(false)
283 __pos = std::__sv_check(size(), __pos, "basic_string_view::substr");
284 const size_type __rlen = std::min(__n, _M_len - __pos);
285 return basic_string_view{_M_str + __pos, __rlen};
289 compare(basic_string_view __str) const noexcept
291 const size_type __rlen = std::min(this->_M_len, __str._M_len);
292 int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
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
306 return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
309 __attribute__((__nonnull__)) constexpr int
310 compare(const _CharT* __str) const noexcept
311 { return this->compare(basic_string_view{__str}); }
313 __attribute__((__nonnull__)) constexpr int
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 noexcept(false)
321 return this->substr(__pos1, __n1)
322 .compare(basic_string_view(__str, __n2));
325 #if __cplusplus > 201703L
326 #define __cpp_lib_starts_ends_with 201711L
328 starts_with(basic_string_view __x) const noexcept
329 { return this->substr(0, __x.size()) == __x; }
332 starts_with(_CharT __x) const noexcept
333 { return !this->empty() && traits_type::eq(this->front(), __x); }
336 starts_with(const _CharT* __x) const noexcept
337 { return this->starts_with(basic_string_view(__x)); }
340 ends_with(basic_string_view __x) const noexcept
342 return this->size() >= __x.size()
343 && this->compare(this->size() - __x.size(), npos, __x) == 0;
347 ends_with(_CharT __x) const noexcept
348 { return !this->empty() && traits_type::eq(this->back(), __x); }
351 ends_with(const _CharT* __x) const noexcept
352 { return this->ends_with(basic_string_view(__x)); }
355 // [string.view.find], searching
358 find(basic_string_view __str, size_type __pos = 0) const noexcept
359 { return this->find(__str._M_str, __pos, __str._M_len); }
362 find(_CharT __c, size_type __pos = 0) const noexcept;
365 find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
367 __attribute__((__nonnull__)) constexpr size_type
368 find(const _CharT* __str, size_type __pos = 0) const noexcept
369 { return this->find(__str, __pos, traits_type::length(__str)); }
372 rfind(basic_string_view __str, size_type __pos = npos) const noexcept
373 { return this->rfind(__str._M_str, __pos, __str._M_len); }
376 rfind(_CharT __c, size_type __pos = npos) const noexcept;
379 rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
381 __attribute__((__nonnull__)) constexpr size_type
382 rfind(const _CharT* __str, size_type __pos = npos) const noexcept
383 { return this->rfind(__str, __pos, traits_type::length(__str)); }
386 find_first_of(basic_string_view __str, size_type __pos = 0) const noexcept
387 { return this->find_first_of(__str._M_str, __pos, __str._M_len); }
390 find_first_of(_CharT __c, size_type __pos = 0) const noexcept
391 { return this->find(__c, __pos); }
394 find_first_of(const _CharT* __str, size_type __pos,
395 size_type __n) const noexcept;
397 __attribute__((__nonnull__)) constexpr size_type
398 find_first_of(const _CharT* __str, size_type __pos = 0) const noexcept
399 { return this->find_first_of(__str, __pos, traits_type::length(__str)); }
402 find_last_of(basic_string_view __str,
403 size_type __pos = npos) const noexcept
404 { return this->find_last_of(__str._M_str, __pos, __str._M_len); }
407 find_last_of(_CharT __c, size_type __pos=npos) const noexcept
408 { return this->rfind(__c, __pos); }
411 find_last_of(const _CharT* __str, size_type __pos,
412 size_type __n) const noexcept;
414 __attribute__((__nonnull__)) constexpr size_type
415 find_last_of(const _CharT* __str, size_type __pos = npos) const noexcept
416 { return this->find_last_of(__str, __pos, traits_type::length(__str)); }
419 find_first_not_of(basic_string_view __str,
420 size_type __pos = 0) const noexcept
421 { return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
424 find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
427 find_first_not_of(const _CharT* __str,
428 size_type __pos, size_type __n) const noexcept;
430 __attribute__((__nonnull__)) constexpr size_type
431 find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
433 return this->find_first_not_of(__str, __pos,
434 traits_type::length(__str));
438 find_last_not_of(basic_string_view __str,
439 size_type __pos = npos) const noexcept
440 { return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
443 find_last_not_of(_CharT __c, size_type __pos = npos) const noexcept;
446 find_last_not_of(const _CharT* __str,
447 size_type __pos, size_type __n) const noexcept;
449 __attribute__((__nonnull__)) constexpr size_type
450 find_last_not_of(const _CharT* __str,
451 size_type __pos = npos) const noexcept
453 return this->find_last_not_of(__str, __pos,
454 traits_type::length(__str));
460 _S_compare(size_type __n1, size_type __n2) noexcept
462 using __limits = __gnu_cxx::__int_traits<int>;
463 const difference_type __diff = __n1 - __n2;
464 if (__diff > __limits::__max)
465 return __limits::__max;
466 if (__diff < __limits::__min)
467 return __limits::__min;
468 return static_cast<int>(__diff);
472 const _CharT* _M_str;
475 #if __cplusplus > 201703L && __cpp_lib_concepts && __cpp_deduction_guides
476 template<contiguous_iterator _It, sized_sentinel_for<_It> _End>
477 basic_string_view(_It, _End) -> basic_string_view<iter_value_t<_It>>;
480 // [string.view.comparison], non-member basic_string_view comparison function
482 // Several of these functions use type_identity_t to create a non-deduced
483 // context, so that only one argument participates in template argument
484 // deduction and the other argument gets implicitly converted to the deduced
487 template<typename _CharT, typename _Traits>
489 operator==(basic_string_view<_CharT, _Traits> __x,
490 basic_string_view<_CharT, _Traits> __y) noexcept
491 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
493 template<typename _CharT, typename _Traits>
495 operator==(basic_string_view<_CharT, _Traits> __x,
496 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
498 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
500 #if __cpp_lib_three_way_comparison
501 template<typename _CharT, typename _Traits>
503 operator<=>(basic_string_view<_CharT, _Traits> __x,
504 basic_string_view<_CharT, _Traits> __y) noexcept
505 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
506 { return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
508 template<typename _CharT, typename _Traits>
510 operator<=>(basic_string_view<_CharT, _Traits> __x,
511 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
513 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
514 { return __detail::__char_traits_cmp_cat<_Traits>(__x.compare(__y)); }
516 template<typename _CharT, typename _Traits>
518 operator==(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
519 basic_string_view<_CharT, _Traits> __y) noexcept
520 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
522 template<typename _CharT, typename _Traits>
524 operator!=(basic_string_view<_CharT, _Traits> __x,
525 basic_string_view<_CharT, _Traits> __y) noexcept
526 { return !(__x == __y); }
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 == __y); }
535 template<typename _CharT, typename _Traits>
537 operator!=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
538 basic_string_view<_CharT, _Traits> __y) noexcept
539 { return !(__x == __y); }
541 template<typename _CharT, typename _Traits>
543 operator< (basic_string_view<_CharT, _Traits> __x,
544 basic_string_view<_CharT, _Traits> __y) noexcept
545 { return __x.compare(__y) < 0; }
547 template<typename _CharT, typename _Traits>
549 operator< (basic_string_view<_CharT, _Traits> __x,
550 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
552 { return __x.compare(__y) < 0; }
554 template<typename _CharT, typename _Traits>
556 operator< (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
557 basic_string_view<_CharT, _Traits> __y) noexcept
558 { return __x.compare(__y) < 0; }
560 template<typename _CharT, typename _Traits>
562 operator> (basic_string_view<_CharT, _Traits> __x,
563 basic_string_view<_CharT, _Traits> __y) noexcept
564 { return __x.compare(__y) > 0; }
566 template<typename _CharT, typename _Traits>
568 operator> (basic_string_view<_CharT, _Traits> __x,
569 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
571 { return __x.compare(__y) > 0; }
573 template<typename _CharT, typename _Traits>
575 operator> (__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
576 basic_string_view<_CharT, _Traits> __y) noexcept
577 { return __x.compare(__y) > 0; }
579 template<typename _CharT, typename _Traits>
581 operator<=(basic_string_view<_CharT, _Traits> __x,
582 basic_string_view<_CharT, _Traits> __y) noexcept
583 { return __x.compare(__y) <= 0; }
585 template<typename _CharT, typename _Traits>
587 operator<=(basic_string_view<_CharT, _Traits> __x,
588 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
590 { return __x.compare(__y) <= 0; }
592 template<typename _CharT, typename _Traits>
594 operator<=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
595 basic_string_view<_CharT, _Traits> __y) noexcept
596 { return __x.compare(__y) <= 0; }
598 template<typename _CharT, typename _Traits>
600 operator>=(basic_string_view<_CharT, _Traits> __x,
601 basic_string_view<_CharT, _Traits> __y) noexcept
602 { return __x.compare(__y) >= 0; }
604 template<typename _CharT, typename _Traits>
606 operator>=(basic_string_view<_CharT, _Traits> __x,
607 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
609 { return __x.compare(__y) >= 0; }
611 template<typename _CharT, typename _Traits>
613 operator>=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
614 basic_string_view<_CharT, _Traits> __y) noexcept
615 { return __x.compare(__y) >= 0; }
616 #endif // three-way comparison
618 // [string.view.io], Inserters and extractors
619 template<typename _CharT, typename _Traits>
620 inline basic_ostream<_CharT, _Traits>&
621 operator<<(basic_ostream<_CharT, _Traits>& __os,
622 basic_string_view<_CharT,_Traits> __str)
623 { return __ostream_insert(__os, __str.data(), __str.size()); }
626 // basic_string_view typedef names
628 using string_view = basic_string_view<char>;
629 #ifdef _GLIBCXX_USE_WCHAR_T
630 using wstring_view = basic_string_view<wchar_t>;
632 #ifdef _GLIBCXX_USE_CHAR8_T
633 using u8string_view = basic_string_view<char8_t>;
635 using u16string_view = basic_string_view<char16_t>;
636 using u32string_view = basic_string_view<char32_t>;
638 // [string.view.hash], hash support:
640 template<typename _Tp>
644 struct hash<string_view>
645 : public __hash_base<size_t, string_view>
648 operator()(const string_view& __str) const noexcept
649 { return std::_Hash_impl::hash(__str.data(), __str.length()); }
653 struct __is_fast_hash<hash<string_view>> : std::false_type
656 #ifdef _GLIBCXX_USE_WCHAR_T
658 struct hash<wstring_view>
659 : public __hash_base<size_t, wstring_view>
662 operator()(const wstring_view& __s) const noexcept
663 { return std::_Hash_impl::hash(__s.data(),
664 __s.length() * sizeof(wchar_t)); }
668 struct __is_fast_hash<hash<wstring_view>> : std::false_type
672 #ifdef _GLIBCXX_USE_CHAR8_T
674 struct hash<u8string_view>
675 : public __hash_base<size_t, u8string_view>
678 operator()(const u8string_view& __str) const noexcept
679 { return std::_Hash_impl::hash(__str.data(), __str.length()); }
683 struct __is_fast_hash<hash<u8string_view>> : std::false_type
688 struct hash<u16string_view>
689 : public __hash_base<size_t, u16string_view>
692 operator()(const u16string_view& __s) const noexcept
693 { return std::_Hash_impl::hash(__s.data(),
694 __s.length() * sizeof(char16_t)); }
698 struct __is_fast_hash<hash<u16string_view>> : std::false_type
702 struct hash<u32string_view>
703 : public __hash_base<size_t, u32string_view>
706 operator()(const u32string_view& __s) const noexcept
707 { return std::_Hash_impl::hash(__s.data(),
708 __s.length() * sizeof(char32_t)); }
712 struct __is_fast_hash<hash<u32string_view>> : std::false_type
715 inline namespace literals
717 inline namespace string_view_literals
719 #pragma GCC diagnostic push
720 #pragma GCC diagnostic ignored "-Wliteral-suffix"
721 inline constexpr basic_string_view<char>
722 operator""sv(const char* __str, size_t __len) noexcept
723 { return basic_string_view<char>{__str, __len}; }
725 #ifdef _GLIBCXX_USE_WCHAR_T
726 inline constexpr basic_string_view<wchar_t>
727 operator""sv(const wchar_t* __str, size_t __len) noexcept
728 { return basic_string_view<wchar_t>{__str, __len}; }
731 #ifdef _GLIBCXX_USE_CHAR8_T
732 inline constexpr basic_string_view<char8_t>
733 operator""sv(const char8_t* __str, size_t __len) noexcept
734 { return basic_string_view<char8_t>{__str, __len}; }
737 inline constexpr basic_string_view<char16_t>
738 operator""sv(const char16_t* __str, size_t __len) noexcept
739 { return basic_string_view<char16_t>{__str, __len}; }
741 inline constexpr basic_string_view<char32_t>
742 operator""sv(const char32_t* __str, size_t __len) noexcept
743 { return basic_string_view<char32_t>{__str, __len}; }
745 #pragma GCC diagnostic pop
746 } // namespace string_literals
747 } // namespace literals
749 #if __cpp_lib_concepts
752 // Opt-in to borrowed_range concept
753 template<typename _CharT, typename _Traits>
754 inline constexpr bool
755 enable_borrowed_range<basic_string_view<_CharT, _Traits>> = true;
757 // Opt-in to view concept
758 template<typename _CharT, typename _Traits>
759 inline constexpr bool
760 enable_view<basic_string_view<_CharT, _Traits>> = true;
763 _GLIBCXX_END_NAMESPACE_VERSION
766 #include <bits/string_view.tcc>
768 #endif // __cplusplus <= 201402L
770 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW