1 // Components for manipulating non-owning sequences of characters -*- C++ -*-
3 // Copyright (C) 2013-2025 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/>.
25 /** @file experimental/string_view
26 * This is a TS C++ Library header.
31 // N3762 basic_string_view library
34 #ifndef _GLIBCXX_EXPERIMENTAL_STRING_VIEW
35 #define _GLIBCXX_EXPERIMENTAL_STRING_VIEW 1
37 #ifdef _GLIBCXX_SYSHDR
38 #pragma GCC system_header
41 #include <bits/requires_hosted.h> // experimental is currently omitted
43 #if __cplusplus >= 201402L
47 #include <bits/ranges_base.h> // enable_borrowed_range, enable_view
48 #include <experimental/bits/lfts_config.h>
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
61 * @class basic_string_view <experimental/string_view>
62 * @brief A non-owning reference to a string.
68 * @tparam _CharT Type of character
69 * @tparam _Traits Traits for character type, defaults to
70 * char_traits<_CharT>.
72 * A basic_string_view looks like this:
79 template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
80 class basic_string_view
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;
93 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
94 using reverse_iterator = const_reverse_iterator;
95 using size_type = size_t;
96 using difference_type = ptrdiff_t;
97 static constexpr size_type npos = size_type(-1);
99 // [string.view.cons], construct/copy
102 basic_string_view() noexcept
103 : _M_len{0}, _M_str{nullptr}
106 constexpr basic_string_view(const basic_string_view&) noexcept = default;
108 template<typename _Allocator>
109 basic_string_view(const basic_string<_CharT, _Traits,
110 _Allocator>& __str) noexcept
111 : _M_len{__str.length()}, _M_str{__str.data()}
114 constexpr basic_string_view(const _CharT* __str)
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;
127 // [string.view.iterators], iterators
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; }
145 const_reverse_iterator
146 rbegin() const noexcept
147 { return const_reverse_iterator(this->end()); }
149 const_reverse_iterator
150 rend() const noexcept
151 { return const_reverse_iterator(this->begin()); }
153 const_reverse_iterator
154 crbegin() const noexcept
155 { return const_reverse_iterator(this->end()); }
157 const_reverse_iterator
158 crend() const noexcept
159 { return const_reverse_iterator(this->begin()); }
161 // [string.view.capacity], capacity
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; }
182 // [string.view.access], element access
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; }
221 // [string.view.modifiers], modifiers:
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
244 // [string.view.ops], string operations:
246 template<typename _Allocator>
247 explicit operator basic_string<_CharT, _Traits, _Allocator>() const
249 return { this->_M_str, this->_M_len };
252 template<typename _Allocator = std::allocator<_CharT>>
253 basic_string<_CharT, _Traits, _Allocator>
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++;
276 // [string.view.ops], string operations:
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
428 return difference_type(__n1 - __n2) > std::numeric_limits<int>::max()
429 ? std::numeric_limits<int>::max()
430 : difference_type(__n1 - __n2) < std::numeric_limits<int>::min()
431 ? std::numeric_limits<int>::min()
432 : static_cast<int>(difference_type(__n1 - __n2));
436 const _CharT* _M_str;
439 // [string.view.comparison], non-member basic_string_view comparison functions
441 // Several of these functions use type_identity_t to create a non-deduced
442 // context, so that only one argument participates in template argument
443 // deduction and the other argument gets implicitly converted to the deduced
446 template<typename _CharT, typename _Traits>
448 operator==(basic_string_view<_CharT, _Traits> __x,
449 basic_string_view<_CharT, _Traits> __y) noexcept
450 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
452 template<typename _CharT, typename _Traits>
454 operator==(basic_string_view<_CharT, _Traits> __x,
455 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
457 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
459 template<typename _CharT, typename _Traits>
461 operator==(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
462 basic_string_view<_CharT, _Traits> __y) noexcept
463 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
465 template<typename _CharT, typename _Traits>
467 operator!=(basic_string_view<_CharT, _Traits> __x,
468 basic_string_view<_CharT, _Traits> __y) noexcept
469 { return !(__x == __y); }
471 template<typename _CharT, typename _Traits>
473 operator!=(basic_string_view<_CharT, _Traits> __x,
474 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
476 { return !(__x == __y); }
478 template<typename _CharT, typename _Traits>
480 operator!=(__type_identity_t<basic_string_view<_CharT, _Traits>> __x,
481 basic_string_view<_CharT, _Traits> __y) noexcept
482 { return !(__x == __y); }
484 template<typename _CharT, typename _Traits>
486 operator< (basic_string_view<_CharT, _Traits> __x,
487 basic_string_view<_CharT, _Traits> __y) noexcept
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,
500 basic_string_view<_CharT, _Traits> __y) noexcept
501 { return __x.compare(__y) < 0; }
503 template<typename _CharT, typename _Traits>
505 operator> (basic_string_view<_CharT, _Traits> __x,
506 basic_string_view<_CharT, _Traits> __y) noexcept
507 { return __x.compare(__y) > 0; }
509 template<typename _CharT, typename _Traits>
511 operator> (basic_string_view<_CharT, _Traits> __x,
512 __type_identity_t<basic_string_view<_CharT, _Traits>> __y)
514 { return __x.compare(__y) > 0; }
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.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.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,
538 basic_string_view<_CharT, _Traits> __y) noexcept
539 { return __x.compare(__y) <= 0; }
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 // [string.view.io], Inserters and extractors
561 template<typename _CharT, typename _Traits>
562 inline basic_ostream<_CharT, _Traits>&
563 operator<<(basic_ostream<_CharT, _Traits>& __os,
564 basic_string_view<_CharT,_Traits> __str)
565 { return __ostream_insert(__os, __str.data(), __str.size()); }
568 // basic_string_view typedef names
570 using string_view = basic_string_view<char>;
571 using wstring_view = basic_string_view<wchar_t>;
572 #ifdef _GLIBCXX_USE_CHAR8_T
573 using u8string_view = basic_string_view<char8_t>;
575 using u16string_view = basic_string_view<char16_t>;
576 using u32string_view = basic_string_view<char32_t>;
577 } // namespace fundamentals_v1
578 } // namespace experimental
581 // [string.view.hash], hash support:
582 template<typename _Tp>
586 struct hash<experimental::string_view>
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()); }
595 struct __is_fast_hash<hash<experimental::string_view>> : std::false_type
599 struct hash<experimental::wstring_view>
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)); }
609 struct __is_fast_hash<hash<experimental::wstring_view>> : std::false_type
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()); }
623 struct __is_fast_hash<hash<experimental::u8string_view>> : std::false_type
628 struct hash<experimental::u16string_view>
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)); }
638 struct __is_fast_hash<hash<experimental::u16string_view>> : std::false_type
642 struct hash<experimental::u32string_view>
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)); }
652 struct __is_fast_hash<hash<experimental::u32string_view>> : std::false_type
655 namespace experimental
657 // I added these EMSR.
658 inline namespace literals
660 inline namespace string_view_literals
662 #pragma GCC diagnostic push
663 #pragma GCC diagnostic ignored "-Wliteral-suffix"
664 inline constexpr basic_string_view<char>
665 operator""sv(const char* __str, size_t __len) noexcept
666 { return basic_string_view<char>{__str, __len}; }
668 inline constexpr basic_string_view<wchar_t>
669 operator""sv(const wchar_t* __str, size_t __len) noexcept
670 { return basic_string_view<wchar_t>{__str, __len}; }
672 #ifdef _GLIBCXX_USE_CHAR8_T
673 inline constexpr basic_string_view<char8_t>
674 operator""sv(const char8_t* __str, size_t __len) noexcept
675 { return basic_string_view<char8_t>{__str, __len}; }
678 inline constexpr basic_string_view<char16_t>
679 operator""sv(const char16_t* __str, size_t __len) noexcept
680 { return basic_string_view<char16_t>{__str, __len}; }
682 inline constexpr basic_string_view<char32_t>
683 operator""sv(const char32_t* __str, size_t __len) noexcept
684 { return basic_string_view<char32_t>{__str, __len}; }
685 #pragma GCC diagnostic pop
686 } // namespace string_literals
687 } // namespace literals
688 } // namespace experimental
690 #if __cpp_lib_concepts
693 // Opt-in to borrowed_range concept
694 template<typename _CharT, typename _Traits>
695 inline constexpr bool
696 enable_borrowed_range<experimental::basic_string_view<_CharT, _Traits>>
699 // Opt-in to view concept
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
709 #include <experimental/bits/string_view.tcc>
711 #endif // __cplusplus <= 201103L
713 #endif // _GLIBCXX_EXPERIMENTAL_STRING_VIEW