1 // Debugging string implementation -*- C++ -*-
3 // Copyright (C) 2003-2026 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 debug/string
26 * This file is a GNU debug extension to the Standard C++ Library.
29 #ifndef _GLIBCXX_DEBUG_STRING
30 #define _GLIBCXX_DEBUG_STRING 1
32 #ifdef _GLIBCXX_SYSHDR
33 #pragma GCC system_header
37 #include <debug/safe_sequence.h>
38 #include <debug/safe_container.h>
39 #include <debug/safe_iterator.h>
41 #define _GLIBCXX_DEBUG_VERIFY_STR_COND_AT(_Cond,_File,_Line,_Func) \
43 __gnu_debug::_Error_formatter::_S_at(_File, _Line, _Func) \
44 ._M_message(#_Cond)._M_error()
46 #if _GLIBCXX_USE_CXX11_ABI && __cplusplus >= 201103
47 # define _GLIBCXX_INSERT_RETURNS_ITERATOR 1
48 # define _GLIBCXX_INSERT_RETURNS_ITERATOR_ONLY(expr) expr
50 # define _GLIBCXX_INSERT_RETURNS_ITERATOR 0
51 # define _GLIBCXX_INSERT_RETURNS_ITERATOR_ONLY(expr)
54 #ifdef _GLIBCXX_DEBUG_PEDANTIC
55 # if __cplusplus < 201103L
56 # define __glibcxx_check_string(_String) \
57 _GLIBCXX_DEBUG_VERIFY_STR_COND_AT(_String != 0, \
60 # define __glibcxx_check_string_len(_String,_Len) \
61 _GLIBCXX_DEBUG_VERIFY_STR_COND_AT(_String != 0 || _Len == 0, \
65 # define __glibcxx_check_string(_String) \
66 _GLIBCXX_DEBUG_VERIFY_STR_COND_AT(_String != nullptr, \
69 # define __glibcxx_check_string_len(_String,_Len) \
70 _GLIBCXX_DEBUG_VERIFY_STR_COND_AT(_String != nullptr || _Len == 0, \
75 # define __glibcxx_check_string(_String)
76 # define __glibcxx_check_string_len(_String,_Len)
81 /** Checks that __s is non-NULL or __n == 0, and then returns __s. */
82 template<typename _CharT, typename _Integer>
84 __check_string(const _CharT* __s,
85 _Integer __n __attribute__((__unused__)),
86 const char* __file __attribute__((__unused__)),
87 unsigned int __line __attribute__((__unused__)),
88 const char* __function __attribute__((__unused__)))
90 #ifdef _GLIBCXX_DEBUG_PEDANTIC
91 # if __cplusplus < 201103L
92 _GLIBCXX_DEBUG_VERIFY_STR_COND_AT(__s != 0 || __n == 0,
93 __file, __line, __function);
95 _GLIBCXX_DEBUG_VERIFY_STR_COND_AT(__s != nullptr || __n == 0,
96 __file, __line, __function);
102 /** Checks that __s is non-NULL and then returns __s. */
103 template<typename _CharT>
105 __check_string(const _CharT* __s,
106 const char* __file __attribute__((__unused__)),
107 unsigned int __line __attribute__((__unused__)),
108 const char* __function __attribute__((__unused__)))
110 #ifdef _GLIBCXX_DEBUG_PEDANTIC
111 # if __cplusplus < 201103L
112 _GLIBCXX_DEBUG_VERIFY_STR_COND_AT(__s != 0,
113 __file, __line, __function);
115 _GLIBCXX_DEBUG_VERIFY_STR_COND_AT(__s != nullptr,
116 __file, __line, __function);
122 #define __glibcxx_check_string_n_constructor(_Str, _Size) \
123 __check_string(_Str, _Size, __FILE__, __LINE__, __PRETTY_FUNCTION__)
125 #define __glibcxx_check_string_constructor(_Str) \
126 __check_string(_Str, __FILE__, __LINE__, __PRETTY_FUNCTION__)
128 /// Class std::basic_string with safety/checking/debug instrumentation.
129 template<typename _CharT, typename _Traits = std::char_traits<_CharT>,
130 typename _Allocator = std::allocator<_CharT> >
132 : public __gnu_debug::_Safe_container<
133 basic_string<_CharT, _Traits, _Allocator>,
134 _Allocator, _Safe_sequence, bool(_GLIBCXX_USE_CXX11_ABI)>,
135 public std::basic_string<_CharT, _Traits, _Allocator>
137 typedef std::basic_string<_CharT, _Traits, _Allocator> _Base;
138 typedef __gnu_debug::_Safe_container<
139 basic_string, _Allocator, _Safe_sequence, bool(_GLIBCXX_USE_CXX11_ABI)>
142 template<typename _ItT, typename _SeqT, typename _CatT>
143 friend class ::__gnu_debug::_Safe_iterator;
145 // type used for positions in insert, erase etc.
146 typedef __gnu_debug::_Safe_iterator<
147 typename _Base::__const_iterator, basic_string> __const_iterator;
151 typedef _Traits traits_type;
152 typedef typename _Traits::char_type value_type;
153 typedef _Allocator allocator_type;
154 typedef typename _Base::size_type size_type;
155 typedef typename _Base::difference_type difference_type;
156 typedef typename _Base::reference reference;
157 typedef typename _Base::const_reference const_reference;
158 typedef typename _Base::pointer pointer;
159 typedef typename _Base::const_pointer const_pointer;
161 typedef __gnu_debug::_Safe_iterator<
162 typename _Base::iterator, basic_string> iterator;
163 typedef __gnu_debug::_Safe_iterator<
164 typename _Base::const_iterator, basic_string> const_iterator;
166 typedef std::reverse_iterator<iterator> reverse_iterator;
167 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
171 // 21.3.1 construct/copy/destroy:
174 basic_string(const _Allocator& __a) _GLIBCXX_NOEXCEPT
177 #if __cplusplus < 201103L
178 basic_string() : _Base() { }
180 basic_string(const basic_string& __str)
185 basic_string() = default;
186 basic_string(const basic_string&) = default;
187 basic_string(basic_string&&) = default;
189 basic_string(std::initializer_list<_CharT> __l,
190 const _Allocator& __a = _Allocator())
194 basic_string(const basic_string& __s, const _Allocator& __a)
195 : _Base(__s, __a) { }
197 basic_string(basic_string&& __s, const _Allocator& __a)
199 std::is_nothrow_constructible<_Base, _Base, const _Allocator&>::value )
200 : _Safe(std::move(__s), __a),
201 _Base(std::move(__s), __a)
204 ~basic_string() = default;
206 // Provides conversion from a normal-mode string to a debug-mode string
207 basic_string(_Base&& __base) noexcept
208 : _Base(std::move(__base)) { }
211 // Provides conversion from a normal-mode string to a debug-mode string
212 basic_string(const _Base& __base)
215 // _GLIBCXX_RESOLVE_LIB_DEFECTS
216 // 42. string ctors specify wrong default allocator
217 basic_string(const basic_string& __str, size_type __pos,
218 size_type __n = _Base::npos,
219 const _Allocator& __a = _Allocator())
220 : _Base(__str, __pos, __n, __a) { }
222 basic_string(const _CharT* __s, size_type __n,
223 const _Allocator& __a = _Allocator())
224 : _Base(__glibcxx_check_string_n_constructor(__s, __n), __n, __a) { }
226 basic_string(const _CharT* __s, const _Allocator& __a = _Allocator())
227 : _Base(__glibcxx_check_string_constructor(__s), __a)
230 basic_string(size_type __n, _CharT __c,
231 const _Allocator& __a = _Allocator())
232 : _Base(__n, __c, __a) { }
234 #if __cplusplus >= 201103L
235 template<typename _InputIterator,
236 typename = std::_RequireInputIter<_InputIterator>>
238 template<typename _InputIterator>
240 basic_string(_InputIterator __begin, _InputIterator __end,
241 const _Allocator& __a = _Allocator())
242 : _Base(__gnu_debug::__base(
243 __glibcxx_check_valid_constructor_range(__begin, __end)),
244 __gnu_debug::__base(__end), __a) { }
246 #if __cplusplus >= 201103L
248 operator=(const basic_string&) = default;
251 operator=(basic_string&&) = default;
255 operator=(const _CharT* __s)
257 __glibcxx_check_string(__s);
258 _Base::operator=(__s);
259 this->_M_invalidate_all();
264 operator=(_CharT __c)
266 _Base::operator=(__c);
267 this->_M_invalidate_all();
271 #if __cplusplus >= 201103L
273 operator=(std::initializer_list<_CharT> __l)
275 _Base::operator=(__l);
276 this->_M_invalidate_all();
283 begin() // _GLIBCXX_NOEXCEPT
284 { return iterator(_Base::begin(), this); }
287 begin() const _GLIBCXX_NOEXCEPT
288 { return const_iterator(_Base::begin(), this); }
291 end() // _GLIBCXX_NOEXCEPT
292 { return iterator(_Base::end(), this); }
295 end() const _GLIBCXX_NOEXCEPT
296 { return const_iterator(_Base::end(), this); }
299 rbegin() // _GLIBCXX_NOEXCEPT
300 { return reverse_iterator(end()); }
302 const_reverse_iterator
303 rbegin() const _GLIBCXX_NOEXCEPT
304 { return const_reverse_iterator(end()); }
307 rend() // _GLIBCXX_NOEXCEPT
308 { return reverse_iterator(begin()); }
310 const_reverse_iterator
311 rend() const _GLIBCXX_NOEXCEPT
312 { return const_reverse_iterator(begin()); }
314 #if __cplusplus >= 201103L
316 cbegin() const noexcept
317 { return const_iterator(_Base::begin(), this); }
320 cend() const noexcept
321 { return const_iterator(_Base::end(), this); }
323 const_reverse_iterator
324 crbegin() const noexcept
325 { return const_reverse_iterator(end()); }
327 const_reverse_iterator
328 crend() const noexcept
329 { return const_reverse_iterator(begin()); }
335 using _Base::max_size;
338 resize(size_type __n, _CharT __c)
340 _Base::resize(__n, __c);
341 this->_M_invalidate_all();
345 resize(size_type __n)
346 { this->resize(__n, _CharT()); }
348 #if __cplusplus >= 201103L
350 shrink_to_fit() noexcept
352 if (capacity() > size())
357 this->_M_invalidate_all();
365 using _Base::capacity;
366 using _Base::reserve;
369 clear() // _GLIBCXX_NOEXCEPT
372 this->_M_invalidate_all();
377 // 21.3.4 element access:
379 operator[](size_type __pos) const _GLIBCXX_NOEXCEPT
381 _GLIBCXX_DEBUG_VERIFY(__pos <= this->size(),
382 _M_message(__gnu_debug::__msg_subscript_oob)
383 ._M_sequence(*this, "this")
384 ._M_integer(__pos, "__pos")
385 ._M_integer(this->size(), "size"));
386 return _Base::operator[](__pos);
390 operator[](size_type __pos) // _GLIBCXX_NOEXCEPT
392 #if __cplusplus < 201103L && defined(_GLIBCXX_DEBUG_PEDANTIC)
393 __glibcxx_check_subscript(__pos);
395 // as an extension v3 allows s[s.size()] when s is non-const.
396 _GLIBCXX_DEBUG_VERIFY(__pos <= this->size(),
397 _M_message(__gnu_debug::__msg_subscript_oob)
398 ._M_sequence(*this, "this")
399 ._M_integer(__pos, "__pos")
400 ._M_integer(this->size(), "size"));
402 return _Base::operator[](__pos);
407 #if __cplusplus >= 201103L
414 operator+=(const basic_string& __str)
416 _Base::operator+=(__str);
417 this->_M_invalidate_all();
422 operator+=(const _CharT* __s)
424 __glibcxx_check_string(__s);
425 _Base::operator+=(__s);
426 this->_M_invalidate_all();
431 operator+=(_CharT __c)
433 _Base::operator+=(__c);
434 this->_M_invalidate_all();
438 #if __cplusplus >= 201103L
440 operator+=(std::initializer_list<_CharT> __l)
442 _Base::operator+=(__l);
443 this->_M_invalidate_all();
449 append(const basic_string& __str)
451 _Base::append(__str);
452 this->_M_invalidate_all();
457 append(const basic_string& __str, size_type __pos, size_type __n)
459 _Base::append(__str, __pos, __n);
460 this->_M_invalidate_all();
465 append(const _CharT* __s, size_type __n)
467 __glibcxx_check_string_len(__s, __n);
468 _Base::append(__s, __n);
469 this->_M_invalidate_all();
474 append(const _CharT* __s)
476 __glibcxx_check_string(__s);
478 this->_M_invalidate_all();
483 append(size_type __n, _CharT __c)
485 _Base::append(__n, __c);
486 this->_M_invalidate_all();
490 template<typename _InputIterator>
492 append(_InputIterator __first, _InputIterator __last)
494 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
495 __glibcxx_check_valid_range2(__first, __last, __dist);
497 if (__dist.second >= __dp_sign)
498 _Base::append(__gnu_debug::__unsafe(__first),
499 __gnu_debug::__unsafe(__last));
501 _Base::append(__first, __last);
503 this->_M_invalidate_all();
507 // _GLIBCXX_RESOLVE_LIB_DEFECTS
508 // 7. string clause minor problems
510 push_back(_CharT __c)
512 _Base::push_back(__c);
513 this->_M_invalidate_all();
517 assign(const basic_string& __x)
520 this->_M_invalidate_all();
524 #if __cplusplus >= 201103L
526 assign(basic_string&& __x)
527 noexcept(noexcept(std::declval<_Base&>().assign(std::move(__x))))
529 _Base::assign(std::move(__x));
530 this->_M_invalidate_all();
536 assign(const basic_string& __str, size_type __pos, size_type __n)
538 _Base::assign(__str, __pos, __n);
539 this->_M_invalidate_all();
544 assign(const _CharT* __s, size_type __n)
546 __glibcxx_check_string_len(__s, __n);
547 _Base::assign(__s, __n);
548 this->_M_invalidate_all();
553 assign(const _CharT* __s)
555 __glibcxx_check_string(__s);
557 this->_M_invalidate_all();
562 assign(size_type __n, _CharT __c)
564 _Base::assign(__n, __c);
565 this->_M_invalidate_all();
569 template<typename _InputIterator>
571 assign(_InputIterator __first, _InputIterator __last)
573 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
574 __glibcxx_check_valid_range2(__first, __last, __dist);
576 if (__dist.second >= __dp_sign)
577 _Base::assign(__gnu_debug::__unsafe(__first),
578 __gnu_debug::__unsafe(__last));
580 _Base::assign(__first, __last);
582 this->_M_invalidate_all();
586 #if __cplusplus >= 201103L
588 assign(std::initializer_list<_CharT> __l)
591 this->_M_invalidate_all();
597 insert(size_type __pos1, const basic_string& __str)
599 _Base::insert(__pos1, __str);
600 this->_M_invalidate_all();
605 insert(size_type __pos1, const basic_string& __str,
606 size_type __pos2, size_type __n)
608 _Base::insert(__pos1, __str, __pos2, __n);
609 this->_M_invalidate_all();
614 insert(size_type __pos, const _CharT* __s, size_type __n)
616 __glibcxx_check_string(__s);
617 _Base::insert(__pos, __s, __n);
618 this->_M_invalidate_all();
623 insert(size_type __pos, const _CharT* __s)
625 __glibcxx_check_string(__s);
626 _Base::insert(__pos, __s);
627 this->_M_invalidate_all();
632 insert(size_type __pos, size_type __n, _CharT __c)
634 _Base::insert(__pos, __n, __c);
635 this->_M_invalidate_all();
640 insert(__const_iterator __p, _CharT __c)
642 __glibcxx_check_insert(__p);
643 typename _Base::iterator __res = _Base::insert(__p.base(), __c);
644 this->_M_invalidate_all();
645 return iterator(__res, this);
648 #if __cplusplus >= 201103L
650 insert(const_iterator __p, size_type __n, _CharT __c)
652 __glibcxx_check_insert(__p);
653 #if _GLIBCXX_USE_CXX11_ABI
654 typename _Base::iterator __res = _Base::insert(__p.base(), __n, __c);
656 const size_type __offset = __p.base() - _Base::cbegin();
657 _Base::insert(_Base::begin() + __offset, __n, __c);
658 typename _Base::iterator __res = _Base::begin() + __offset;
660 this->_M_invalidate_all();
661 return iterator(__res, this);
665 insert(iterator __p, size_type __n, _CharT __c)
667 __glibcxx_check_insert(__p);
668 _Base::insert(__p.base(), __n, __c);
669 this->_M_invalidate_all();
673 template<typename _InputIterator>
675 insert(__const_iterator __p,
676 _InputIterator __first, _InputIterator __last)
678 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
679 __glibcxx_check_insert_range(__p, __first, __last, __dist);
681 typename _Base::iterator __res;
682 #if ! _GLIBCXX_INSERT_RETURNS_ITERATOR
683 const size_type __offset = __p.base() - _Base::begin();
685 if (__dist.second >= __dp_sign)
687 _GLIBCXX_INSERT_RETURNS_ITERATOR_ONLY(__res =)
688 _Base::insert(__p.base(), __gnu_debug::__unsafe(__first),
689 __gnu_debug::__unsafe(__last));
693 _GLIBCXX_INSERT_RETURNS_ITERATOR_ONLY(__res =)
694 _Base::insert(__p.base(), __first, __last);
697 #if ! _GLIBCXX_INSERT_RETURNS_ITERATOR
698 __res = _Base::begin() + __offset;
700 this->_M_invalidate_all();
701 return iterator(__res, this);
704 #if __cplusplus >= 201103L
706 insert(const_iterator __p, std::initializer_list<_CharT> __l)
708 __glibcxx_check_insert(__p);
709 #if _GLIBCXX_USE_CXX11_ABI
710 const auto __res = _Base::insert(__p.base(), __l);
712 const size_type __offset = __p.base() - _Base::cbegin();
713 _Base::insert(_Base::begin() + __offset, __l);
714 auto __res = _Base::begin() + __offset;
716 this->_M_invalidate_all();
717 return iterator(__res, this);
722 erase(size_type __pos = 0, size_type __n = _Base::npos)
724 _Base::erase(__pos, __n);
725 this->_M_invalidate_all();
730 erase(__const_iterator __position)
732 __glibcxx_check_erase(__position);
733 typename _Base::iterator __res = _Base::erase(__position.base());
734 this->_M_invalidate_all();
735 return iterator(__res, this);
739 erase(__const_iterator __first, __const_iterator __last)
741 // _GLIBCXX_RESOLVE_LIB_DEFECTS
742 // 151. can't currently clear() empty container
743 __glibcxx_check_erase_range(__first, __last);
744 typename _Base::iterator __res = _Base::erase(__first.base(),
746 this->_M_invalidate_all();
747 return iterator(__res, this);
750 #if __cplusplus >= 201103L
752 pop_back() // noexcept
754 __glibcxx_check_nonempty();
756 this->_M_invalidate_all();
761 replace(size_type __pos1, size_type __n1, const basic_string& __str)
763 _Base::replace(__pos1, __n1, __str);
764 this->_M_invalidate_all();
769 replace(size_type __pos1, size_type __n1, const basic_string& __str,
770 size_type __pos2, size_type __n2)
772 _Base::replace(__pos1, __n1, __str, __pos2, __n2);
773 this->_M_invalidate_all();
778 replace(size_type __pos, size_type __n1, const _CharT* __s,
781 __glibcxx_check_string_len(__s, __n2);
782 _Base::replace(__pos, __n1, __s, __n2);
783 this->_M_invalidate_all();
788 replace(size_type __pos, size_type __n1, const _CharT* __s)
790 __glibcxx_check_string(__s);
791 _Base::replace(__pos, __n1, __s);
792 this->_M_invalidate_all();
797 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
799 _Base::replace(__pos, __n1, __n2, __c);
800 this->_M_invalidate_all();
805 replace(__const_iterator __i1, __const_iterator __i2,
806 const basic_string& __str)
808 __glibcxx_check_erase_range(__i1, __i2);
809 _Base::replace(__i1.base(), __i2.base(), __str);
810 this->_M_invalidate_all();
815 replace(__const_iterator __i1, __const_iterator __i2,
816 const _CharT* __s, size_type __n)
818 __glibcxx_check_erase_range(__i1, __i2);
819 __glibcxx_check_string_len(__s, __n);
820 _Base::replace(__i1.base(), __i2.base(), __s, __n);
821 this->_M_invalidate_all();
826 replace(__const_iterator __i1, __const_iterator __i2,
829 __glibcxx_check_erase_range(__i1, __i2);
830 __glibcxx_check_string(__s);
831 _Base::replace(__i1.base(), __i2.base(), __s);
832 this->_M_invalidate_all();
837 replace(__const_iterator __i1, __const_iterator __i2,
838 size_type __n, _CharT __c)
840 __glibcxx_check_erase_range(__i1, __i2);
841 _Base::replace(__i1.base(), __i2.base(), __n, __c);
842 this->_M_invalidate_all();
846 template<typename _InputIterator>
848 replace(__const_iterator __i1, __const_iterator __i2,
849 _InputIterator __j1, _InputIterator __j2)
851 __glibcxx_check_erase_range(__i1, __i2);
853 typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist;
854 __glibcxx_check_valid_range2(__j1, __j2, __dist);
856 if (__dist.second >= __dp_sign)
857 _Base::replace(__i1.base(), __i2.base(),
858 __gnu_debug::__unsafe(__j1),
859 __gnu_debug::__unsafe(__j2));
861 _Base::replace(__i1.base(), __i2.base(), __j1, __j2);
863 this->_M_invalidate_all();
867 #if __cplusplus >= 201103L
869 replace(__const_iterator __i1, __const_iterator __i2,
870 std::initializer_list<_CharT> __l)
872 __glibcxx_check_erase_range(__i1, __i2);
873 _Base::replace(__i1.base(), __i2.base(), __l);
874 this->_M_invalidate_all();
880 copy(_CharT* __s, size_type __n, size_type __pos = 0) const
882 __glibcxx_check_string_len(__s, __n);
883 return _Base::copy(__s, __n, __pos);
887 swap(basic_string& __x)
888 _GLIBCXX_NOEXCEPT_IF(std::__is_nothrow_swappable<_Base>::value)
894 // 21.3.6 string operations:
896 c_str() const _GLIBCXX_NOEXCEPT
898 const _CharT* __res = _Base::c_str();
899 this->_M_invalidate_all();
904 data() const _GLIBCXX_NOEXCEPT
906 const _CharT* __res = _Base::data();
907 this->_M_invalidate_all();
911 using _Base::get_allocator;
917 find(const _CharT* __s, size_type __pos, size_type __n) const
920 __glibcxx_check_string(__s);
921 return _Base::find(__s, __pos, __n);
926 find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
928 __glibcxx_check_string(__s);
929 return _Base::find(__s, __pos);
936 rfind(const _CharT* __s, size_type __pos, size_type __n) const
938 __glibcxx_check_string_len(__s, __n);
939 return _Base::rfind(__s, __pos, __n);
944 rfind(const _CharT* __s, size_type __pos = _Base::npos) const
946 __glibcxx_check_string(__s);
947 return _Base::rfind(__s, __pos);
950 using _Base::find_first_of;
954 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
957 __glibcxx_check_string(__s);
958 return _Base::find_first_of(__s, __pos, __n);
963 find_first_of(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
965 __glibcxx_check_string(__s);
966 return _Base::find_first_of(__s, __pos);
969 using _Base::find_last_of;
973 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
976 __glibcxx_check_string(__s);
977 return _Base::find_last_of(__s, __pos, __n);
982 find_last_of(const _CharT* __s, size_type __pos = _Base::npos) const
985 __glibcxx_check_string(__s);
986 return _Base::find_last_of(__s, __pos);
989 using _Base::find_first_not_of;
993 find_first_not_of(const _CharT* __s, size_type __pos, size_type __n) const
996 __glibcxx_check_string_len(__s, __n);
997 return _Base::find_first_not_of(__s, __pos, __n);
1000 _GLIBCXX20_CONSTEXPR
1002 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
1005 __glibcxx_check_string(__s);
1006 return _Base::find_first_not_of(__s, __pos);
1009 using _Base::find_last_not_of;
1011 _GLIBCXX20_CONSTEXPR
1013 find_last_not_of(const _CharT* __s, size_type __pos, size_type __n) const
1016 __glibcxx_check_string(__s);
1017 return _Base::find_last_not_of(__s, __pos, __n);
1020 _GLIBCXX20_CONSTEXPR
1022 find_last_not_of(const _CharT* __s, size_type __pos = _Base::npos) const
1025 __glibcxx_check_string(__s);
1026 return _Base::find_last_not_of(__s, __pos);
1030 substr(size_type __pos = 0, size_type __n = _Base::npos) const
1031 { return basic_string(_Base::substr(__pos, __n)); }
1033 using _Base::compare;
1035 _GLIBCXX20_CONSTEXPR
1037 compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT
1039 __glibcxx_check_string(__s);
1040 return _Base::compare(__s);
1043 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1044 // 5. string::compare specification questionable
1045 _GLIBCXX20_CONSTEXPR
1047 compare(size_type __pos1, size_type __n1, const _CharT* __s) const
1049 __glibcxx_check_string(__s);
1050 return _Base::compare(__pos1, __n1, __s);
1053 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1054 // 5. string::compare specification questionable
1055 _GLIBCXX20_CONSTEXPR
1057 compare(size_type __pos1, size_type __n1,const _CharT* __s,
1058 size_type __n2) const
1060 __glibcxx_check_string_len(__s, __n2);
1061 return _Base::compare(__pos1, __n1, __s, __n2);
1065 _M_base() _GLIBCXX_NOEXCEPT { return *this; }
1068 _M_base() const _GLIBCXX_NOEXCEPT { return *this; }
1070 using _Safe::_M_invalidate_all;
1073 template<typename _CharT, typename _Traits, typename _Allocator>
1074 inline basic_string<_CharT,_Traits,_Allocator>
1075 operator+(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
1076 const basic_string<_CharT,_Traits,_Allocator>& __rhs)
1077 { return basic_string<_CharT,_Traits,_Allocator>(__lhs) += __rhs; }
1079 template<typename _CharT, typename _Traits, typename _Allocator>
1080 inline basic_string<_CharT,_Traits,_Allocator>
1081 operator+(const _CharT* __lhs,
1082 const basic_string<_CharT,_Traits,_Allocator>& __rhs)
1084 __glibcxx_check_string(__lhs);
1085 return basic_string<_CharT,_Traits,_Allocator>(__lhs) += __rhs;
1088 template<typename _CharT, typename _Traits, typename _Allocator>
1089 inline basic_string<_CharT,_Traits,_Allocator>
1090 operator+(_CharT __lhs,
1091 const basic_string<_CharT,_Traits,_Allocator>& __rhs)
1092 { return basic_string<_CharT,_Traits,_Allocator>(1, __lhs) += __rhs; }
1094 template<typename _CharT, typename _Traits, typename _Allocator>
1095 inline basic_string<_CharT,_Traits,_Allocator>
1096 operator+(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
1097 const _CharT* __rhs)
1099 __glibcxx_check_string(__rhs);
1100 return basic_string<_CharT,_Traits,_Allocator>(__lhs) += __rhs;
1103 template<typename _CharT, typename _Traits, typename _Allocator>
1104 inline basic_string<_CharT,_Traits,_Allocator>
1105 operator+(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
1107 { return basic_string<_CharT,_Traits,_Allocator>(__lhs) += __rhs; }
1109 template<typename _CharT, typename _Traits, typename _Allocator>
1111 operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
1112 const basic_string<_CharT,_Traits,_Allocator>& __rhs)
1113 { return __lhs._M_base() == __rhs._M_base(); }
1115 template<typename _CharT, typename _Traits, typename _Allocator>
1117 operator==(const _CharT* __lhs,
1118 const basic_string<_CharT,_Traits,_Allocator>& __rhs)
1120 __glibcxx_check_string(__lhs);
1121 return __lhs == __rhs._M_base();
1124 template<typename _CharT, typename _Traits, typename _Allocator>
1126 operator==(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
1127 const _CharT* __rhs)
1129 __glibcxx_check_string(__rhs);
1130 return __lhs._M_base() == __rhs;
1133 template<typename _CharT, typename _Traits, typename _Allocator>
1135 operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
1136 const basic_string<_CharT,_Traits,_Allocator>& __rhs)
1137 { return __lhs._M_base() != __rhs._M_base(); }
1139 template<typename _CharT, typename _Traits, typename _Allocator>
1141 operator!=(const _CharT* __lhs,
1142 const basic_string<_CharT,_Traits,_Allocator>& __rhs)
1144 __glibcxx_check_string(__lhs);
1145 return __lhs != __rhs._M_base();
1148 template<typename _CharT, typename _Traits, typename _Allocator>
1150 operator!=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
1151 const _CharT* __rhs)
1153 __glibcxx_check_string(__rhs);
1154 return __lhs._M_base() != __rhs;
1157 template<typename _CharT, typename _Traits, typename _Allocator>
1159 operator<(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
1160 const basic_string<_CharT,_Traits,_Allocator>& __rhs)
1161 { return __lhs._M_base() < __rhs._M_base(); }
1163 template<typename _CharT, typename _Traits, typename _Allocator>
1165 operator<(const _CharT* __lhs,
1166 const basic_string<_CharT,_Traits,_Allocator>& __rhs)
1168 __glibcxx_check_string(__lhs);
1169 return __lhs < __rhs._M_base();
1172 template<typename _CharT, typename _Traits, typename _Allocator>
1174 operator<(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
1175 const _CharT* __rhs)
1177 __glibcxx_check_string(__rhs);
1178 return __lhs._M_base() < __rhs;
1181 template<typename _CharT, typename _Traits, typename _Allocator>
1183 operator<=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
1184 const basic_string<_CharT,_Traits,_Allocator>& __rhs)
1185 { return __lhs._M_base() <= __rhs._M_base(); }
1187 template<typename _CharT, typename _Traits, typename _Allocator>
1189 operator<=(const _CharT* __lhs,
1190 const basic_string<_CharT,_Traits,_Allocator>& __rhs)
1192 __glibcxx_check_string(__lhs);
1193 return __lhs <= __rhs._M_base();
1196 template<typename _CharT, typename _Traits, typename _Allocator>
1198 operator<=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
1199 const _CharT* __rhs)
1201 __glibcxx_check_string(__rhs);
1202 return __lhs._M_base() <= __rhs;
1205 template<typename _CharT, typename _Traits, typename _Allocator>
1207 operator>=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
1208 const basic_string<_CharT,_Traits,_Allocator>& __rhs)
1209 { return __lhs._M_base() >= __rhs._M_base(); }
1211 template<typename _CharT, typename _Traits, typename _Allocator>
1213 operator>=(const _CharT* __lhs,
1214 const basic_string<_CharT,_Traits,_Allocator>& __rhs)
1216 __glibcxx_check_string(__lhs);
1217 return __lhs >= __rhs._M_base();
1220 template<typename _CharT, typename _Traits, typename _Allocator>
1222 operator>=(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
1223 const _CharT* __rhs)
1225 __glibcxx_check_string(__rhs);
1226 return __lhs._M_base() >= __rhs;
1229 template<typename _CharT, typename _Traits, typename _Allocator>
1231 operator>(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
1232 const basic_string<_CharT,_Traits,_Allocator>& __rhs)
1233 { return __lhs._M_base() > __rhs._M_base(); }
1235 template<typename _CharT, typename _Traits, typename _Allocator>
1237 operator>(const _CharT* __lhs,
1238 const basic_string<_CharT,_Traits,_Allocator>& __rhs)
1240 __glibcxx_check_string(__lhs);
1241 return __lhs > __rhs._M_base();
1244 template<typename _CharT, typename _Traits, typename _Allocator>
1246 operator>(const basic_string<_CharT,_Traits,_Allocator>& __lhs,
1247 const _CharT* __rhs)
1249 __glibcxx_check_string(__rhs);
1250 return __lhs._M_base() > __rhs;
1254 template<typename _CharT, typename _Traits, typename _Allocator>
1256 swap(basic_string<_CharT,_Traits,_Allocator>& __lhs,
1257 basic_string<_CharT,_Traits,_Allocator>& __rhs)
1258 { __lhs.swap(__rhs); }
1260 template<typename _CharT, typename _Traits, typename _Allocator>
1261 std::basic_ostream<_CharT, _Traits>&
1262 operator<<(std::basic_ostream<_CharT, _Traits>& __os,
1263 const basic_string<_CharT, _Traits, _Allocator>& __str)
1264 { return __os << __str._M_base(); }
1266 template<typename _CharT, typename _Traits, typename _Allocator>
1267 std::basic_istream<_CharT,_Traits>&
1268 operator>>(std::basic_istream<_CharT,_Traits>& __is,
1269 basic_string<_CharT,_Traits,_Allocator>& __str)
1271 std::basic_istream<_CharT,_Traits>& __res = __is >> __str._M_base();
1272 __str._M_invalidate_all();
1276 template<typename _CharT, typename _Traits, typename _Allocator>
1277 std::basic_istream<_CharT,_Traits>&
1278 getline(std::basic_istream<_CharT,_Traits>& __is,
1279 basic_string<_CharT,_Traits,_Allocator>& __str, _CharT __delim)
1281 std::basic_istream<_CharT,_Traits>& __res = getline(__is,
1284 __str._M_invalidate_all();
1288 template<typename _CharT, typename _Traits, typename _Allocator>
1289 std::basic_istream<_CharT,_Traits>&
1290 getline(std::basic_istream<_CharT,_Traits>& __is,
1291 basic_string<_CharT,_Traits,_Allocator>& __str)
1293 std::basic_istream<_CharT,_Traits>& __res = getline(__is,
1295 __str._M_invalidate_all();
1299 typedef basic_string<char> string;
1301 typedef basic_string<wchar_t> wstring;
1303 #ifdef _GLIBCXX_USE_CHAR8_T
1304 /// A string of @c char8_t
1305 typedef basic_string<char8_t> u8string;
1308 #if __cplusplus >= 201103L
1309 /// A string of @c char16_t
1310 typedef basic_string<char16_t> u16string;
1312 /// A string of @c char32_t
1313 typedef basic_string<char32_t> u32string;
1316 template<typename _CharT, typename _Traits, typename _Allocator>
1317 struct _Insert_range_from_self_is_safe<
1318 __gnu_debug::basic_string<_CharT, _Traits, _Allocator> >
1319 { enum { __value = 1 }; };
1321 } // namespace __gnu_debug
1323 #if __cplusplus >= 201103L
1324 namespace std _GLIBCXX_VISIBILITY(default)
1326 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1328 /// std::hash specialization for __gnu_debug::basic_string.
1329 template<typename _CharT>
1330 struct hash<__gnu_debug::basic_string<_CharT>>
1331 : public hash<std::basic_string<_CharT>>
1334 template<typename _CharT>
1335 struct __is_fast_hash<hash<__gnu_debug::basic_string<_CharT>>>
1336 : __is_fast_hash<hash<std::basic_string<_CharT>>>
1339 #ifdef __glibcxx_erase_if // C++ >= 20 && HOSTED
1340 template<typename _CharT, typename _Traits, typename _Alloc,
1341 typename _Predicate>
1342 constexpr typename __gnu_debug::basic_string<_CharT,
1343 _Traits, _Alloc>::size_type
1344 erase_if(__gnu_debug::basic_string<_CharT, _Traits, _Alloc>& __cont,
1347 return __detail::__erase_if(__cont, __cont._M_base(), std::move(__pred));
1350 template<typename _CharT, typename _Traits, typename _Alloc,
1351 typename _Up _GLIBCXX26_DEF_VAL_T(_CharT)>
1352 constexpr typename __gnu_debug::basic_string<_CharT,
1353 _Traits, _Alloc>::size_type
1354 erase(__gnu_debug::basic_string<_CharT, _Traits, _Alloc>& __cont,
1356 { return std::erase_if(__cont, __gnu_cxx::__ops::__equal_to(__value)); }
1357 #endif // __glibcxx_erase_if
1359 _GLIBCXX_END_NAMESPACE_VERSION
1363 #undef _GLIBCXX_INSERT_RETURNS_ITERATOR
1364 #undef _GLIBCXX_INSERT_RETURNS_ITERATOR_ONLY