1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997-2023 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 bits/basic_string.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{string}
31 // ISO C++ 14882: 21 Strings library
34 #ifndef _BASIC_STRING_H
35 #define _BASIC_STRING_H 1
37 #pragma GCC system_header
39 #include <ext/alloc_traits.h>
40 #include <debug/debug.h>
42 #if __cplusplus >= 201103L
43 #include <initializer_list>
46 #if __cplusplus >= 201703L
47 # include <string_view>
50 #if ! _GLIBCXX_USE_CXX11_ABI
51 # include "cow_string.h"
53 namespace std
_GLIBCXX_VISIBILITY(default)
55 _GLIBCXX_BEGIN_NAMESPACE_VERSION
56 _GLIBCXX_BEGIN_NAMESPACE_CXX11
58 #ifdef __cpp_lib_is_constant_evaluated
59 // Support P0980R1 in C++20.
60 # define __cpp_lib_constexpr_string 201907L
61 #elif __cplusplus >= 201703L && _GLIBCXX_HAVE_IS_CONSTANT_EVALUATED
62 // Support P0426R1 changes to char_traits in C++17.
63 # define __cpp_lib_constexpr_string 201611L
67 * @class basic_string basic_string.h <string>
68 * @brief Managing sequences of characters and character-like objects.
75 * @tparam _CharT Type of character
76 * @tparam _Traits Traits for character type, defaults to
77 * char_traits<_CharT>.
78 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
80 * Meets the requirements of a <a href="tables.html#65">container</a>, a
81 * <a href="tables.html#66">reversible container</a>, and a
82 * <a href="tables.html#67">sequence</a>. Of the
83 * <a href="tables.html#68">optional sequence requirements</a>, only
84 * @c push_back, @c at, and @c %array access are supported.
86 template<typename _CharT
, typename _Traits
, typename _Alloc
>
89 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
90 rebind
<_CharT
>::other _Char_alloc_type
;
92 #if __cpp_lib_constexpr_string < 201907L
93 typedef __gnu_cxx::__alloc_traits
<_Char_alloc_type
> _Alloc_traits
;
95 template<typename _Traits2
, typename _Dummy_for_PR85282
>
96 struct _Alloc_traits_impl
: __gnu_cxx::__alloc_traits
<_Char_alloc_type
>
98 typedef __gnu_cxx::__alloc_traits
<_Char_alloc_type
> _Base
;
100 [[__gnu__::__always_inline__
]]
101 static constexpr typename
_Base::pointer
102 allocate(_Char_alloc_type
& __a
, typename
_Base::size_type __n
)
104 pointer __p
= _Base::allocate(__a
, __n
);
105 if (std::is_constant_evaluated())
106 // Begin the lifetime of characters in allocated storage.
107 for (size_type __i
= 0; __i
< __n
; ++__i
)
108 std::construct_at(__builtin_addressof(__p
[__i
]));
113 template<typename _Dummy_for_PR85282
>
114 struct _Alloc_traits_impl
<char_traits
<_CharT
>, _Dummy_for_PR85282
>
115 : __gnu_cxx::__alloc_traits
<_Char_alloc_type
>
117 // std::char_traits begins the lifetime of characters.
120 using _Alloc_traits
= _Alloc_traits_impl
<_Traits
, void>;
125 typedef _Traits traits_type
;
126 typedef typename
_Traits::char_type value_type
;
127 typedef _Char_alloc_type allocator_type
;
128 typedef typename
_Alloc_traits::size_type size_type
;
129 typedef typename
_Alloc_traits::difference_type difference_type
;
130 typedef typename
_Alloc_traits::reference reference
;
131 typedef typename
_Alloc_traits::const_reference const_reference
;
132 typedef typename
_Alloc_traits::pointer pointer
;
133 typedef typename
_Alloc_traits::const_pointer const_pointer
;
134 typedef __gnu_cxx::__normal_iterator
<pointer
, basic_string
> iterator
;
135 typedef __gnu_cxx::__normal_iterator
<const_pointer
, basic_string
>
137 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
138 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
140 /// Value returned by various member functions when they fail.
141 static const size_type npos
= static_cast<size_type
>(-1);
144 // type used for positions in insert, erase etc.
145 #if __cplusplus < 201103L
146 typedef iterator __const_iterator
;
148 typedef const_iterator __const_iterator
;
152 #if __cplusplus >= 201703L
153 // A helper type for avoiding boiler-plate.
154 typedef basic_string_view
<_CharT
, _Traits
> __sv_type
;
156 template<typename _Tp
, typename _Res
>
157 using _If_sv
= enable_if_t
<
158 __and_
<is_convertible
<const _Tp
&, __sv_type
>,
159 __not_
<is_convertible
<const _Tp
*, const basic_string
*>>,
160 __not_
<is_convertible
<const _Tp
&, const _CharT
*>>>::value
,
163 // Allows an implicit conversion to __sv_type.
166 _S_to_string_view(__sv_type __svt
) noexcept
169 // Wraps a string_view by explicit conversion and thus
170 // allows to add an internal constructor that does not
171 // participate in overload resolution when a string_view
175 _GLIBCXX20_CONSTEXPR
explicit
176 __sv_wrapper(__sv_type __sv
) noexcept
: _M_sv(__sv
) { }
182 * @brief Only internally used: Construct string from a string view
184 * @param __svw string view wrapper.
185 * @param __a Allocator to use.
189 basic_string(__sv_wrapper __svw
, const _Alloc
& __a
)
190 : basic_string(__svw
._M_sv
.data(), __svw
._M_sv
.size(), __a
) { }
193 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
194 struct _Alloc_hider
: allocator_type
// TODO check __is_final
196 #if __cplusplus < 201103L
197 _Alloc_hider(pointer __dat
, const _Alloc
& __a
= _Alloc())
198 : allocator_type(__a
), _M_p(__dat
) { }
201 _Alloc_hider(pointer __dat
, const _Alloc
& __a
)
202 : allocator_type(__a
), _M_p(__dat
) { }
205 _Alloc_hider(pointer __dat
, _Alloc
&& __a
= _Alloc())
206 : allocator_type(std::move(__a
)), _M_p(__dat
) { }
209 pointer _M_p
; // The actual data.
212 _Alloc_hider _M_dataplus
;
213 size_type _M_string_length
;
215 enum { _S_local_capacity
= 15 / sizeof(_CharT
) };
219 _CharT _M_local_buf
[_S_local_capacity
+ 1];
220 size_type _M_allocated_capacity
;
226 { _M_dataplus
._M_p
= __p
; }
230 _M_length(size_type __length
)
231 { _M_string_length
= __length
; }
236 { return _M_dataplus
._M_p
; }
242 #if __cplusplus >= 201103L
243 return std::pointer_traits
<pointer
>::pointer_to(*_M_local_buf
);
245 return pointer(_M_local_buf
);
251 _M_local_data() const
253 #if __cplusplus >= 201103L
254 return std::pointer_traits
<const_pointer
>::pointer_to(*_M_local_buf
);
256 return const_pointer(_M_local_buf
);
262 _M_capacity(size_type __capacity
)
263 { _M_allocated_capacity
= __capacity
; }
267 _M_set_length(size_type __n
)
270 traits_type::assign(_M_data()[__n
], _CharT());
277 if (_M_data() == _M_local_data())
279 if (_M_string_length
> _S_local_capacity
)
280 __builtin_unreachable();
289 _M_create(size_type
&, size_type
);
296 _M_destroy(_M_allocated_capacity
);
301 _M_destroy(size_type __size
) throw()
302 { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size
+ 1); }
304 #if __cplusplus < 201103L || defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
305 // _M_construct_aux is used to implement the 21.3.1 para 15 which
306 // requires special behaviour if _InIterator is an integral type
307 template<typename _InIterator
>
309 _M_construct_aux(_InIterator __beg
, _InIterator __end
,
312 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
313 _M_construct(__beg
, __end
, _Tag());
316 // _GLIBCXX_RESOLVE_LIB_DEFECTS
317 // 438. Ambiguity in the "do the right thing" clause
318 template<typename _Integer
>
320 _M_construct_aux(_Integer __beg
, _Integer __end
, std::__true_type
)
321 { _M_construct_aux_2(static_cast<size_type
>(__beg
), __end
); }
324 _M_construct_aux_2(size_type __req
, _CharT __c
)
325 { _M_construct(__req
, __c
); }
328 // For Input Iterators, used in istreambuf_iterators, etc.
329 template<typename _InIterator
>
332 _M_construct(_InIterator __beg
, _InIterator __end
,
333 std::input_iterator_tag
);
335 // For forward_iterators up to random_access_iterators, used for
336 // string::iterator, _CharT*, etc.
337 template<typename _FwdIterator
>
340 _M_construct(_FwdIterator __beg
, _FwdIterator __end
,
341 std::forward_iterator_tag
);
345 _M_construct(size_type __req
, _CharT __c
);
350 { return _M_dataplus
; }
353 const allocator_type
&
354 _M_get_allocator() const
355 { return _M_dataplus
; }
357 // Ensure that _M_local_buf is the active member of the union.
358 __attribute__((__always_inline__
))
361 _M_use_local_data() _GLIBCXX_NOEXCEPT
363 #if __cpp_lib_is_constant_evaluated
364 if (std::is_constant_evaluated())
365 for (size_type __i
= 0; __i
<= _S_local_capacity
; ++__i
)
366 _M_local_buf
[__i
] = _CharT();
368 return _M_local_data();
373 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
374 // The explicit instantiations in misc-inst.cc require this due to
375 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
376 template<typename _Tp
, bool _Requires
=
377 !__are_same
<_Tp
, _CharT
*>::__value
378 && !__are_same
<_Tp
, const _CharT
*>::__value
379 && !__are_same
<_Tp
, iterator
>::__value
380 && !__are_same
<_Tp
, const_iterator
>::__value
>
381 struct __enable_if_not_native_iterator
382 { typedef basic_string
& __type
; };
383 template<typename _Tp
>
384 struct __enable_if_not_native_iterator
<_Tp
, false> { };
389 _M_check(size_type __pos
, const char* __s
) const
391 if (__pos
> this->size())
392 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
393 "this->size() (which is %zu)"),
394 __s
, __pos
, this->size());
400 _M_check_length(size_type __n1
, size_type __n2
, const char* __s
) const
402 if (this->max_size() - (this->size() - __n1
) < __n2
)
403 __throw_length_error(__N(__s
));
407 // NB: _M_limit doesn't check for a bad __pos value.
410 _M_limit(size_type __pos
, size_type __off
) const _GLIBCXX_NOEXCEPT
412 const bool __testoff
= __off
< this->size() - __pos
;
413 return __testoff
? __off
: this->size() - __pos
;
416 // True if _Rep and source do not overlap.
418 _M_disjunct(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
420 return (less
<const _CharT
*>()(__s
, _M_data())
421 || less
<const _CharT
*>()(_M_data() + this->size(), __s
));
424 // When __n = 1 way faster than the general multichar
425 // traits_type::copy/move/assign.
428 _S_copy(_CharT
* __d
, const _CharT
* __s
, size_type __n
)
431 traits_type::assign(*__d
, *__s
);
433 traits_type::copy(__d
, __s
, __n
);
438 _S_move(_CharT
* __d
, const _CharT
* __s
, size_type __n
)
441 traits_type::assign(*__d
, *__s
);
443 traits_type::move(__d
, __s
, __n
);
448 _S_assign(_CharT
* __d
, size_type __n
, _CharT __c
)
451 traits_type::assign(*__d
, __c
);
453 traits_type::assign(__d
, __n
, __c
);
456 // _S_copy_chars is a separate template to permit specialization
457 // to optimize for the common case of pointers as iterators.
458 template<class _Iterator
>
461 _S_copy_chars(_CharT
* __p
, _Iterator __k1
, _Iterator __k2
)
463 for (; __k1
!= __k2
; ++__k1
, (void)++__p
)
464 traits_type::assign(*__p
, *__k1
); // These types are off.
469 _S_copy_chars(_CharT
* __p
, iterator __k1
, iterator __k2
) _GLIBCXX_NOEXCEPT
470 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
474 _S_copy_chars(_CharT
* __p
, const_iterator __k1
, const_iterator __k2
)
476 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
480 _S_copy_chars(_CharT
* __p
, _CharT
* __k1
, _CharT
* __k2
) _GLIBCXX_NOEXCEPT
481 { _S_copy(__p
, __k1
, __k2
- __k1
); }
485 _S_copy_chars(_CharT
* __p
, const _CharT
* __k1
, const _CharT
* __k2
)
487 { _S_copy(__p
, __k1
, __k2
- __k1
); }
491 _S_compare(size_type __n1
, size_type __n2
) _GLIBCXX_NOEXCEPT
493 const difference_type __d
= difference_type(__n1
- __n2
);
495 if (__d
> __gnu_cxx::__numeric_traits
<int>::__max
)
496 return __gnu_cxx::__numeric_traits
<int>::__max
;
497 else if (__d
< __gnu_cxx::__numeric_traits
<int>::__min
)
498 return __gnu_cxx::__numeric_traits
<int>::__min
;
505 _M_assign(const basic_string
&);
509 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
514 _M_erase(size_type __pos
, size_type __n
);
517 // Construct/copy/destroy:
518 // NB: We overload ctors in some cases instead of using default
519 // arguments, per 17.4.4.4 para. 2 item 2.
522 * @brief Default constructor creates an empty string.
526 _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible
<_Alloc
>::value
)
527 : _M_dataplus(_M_local_data())
534 * @brief Construct an empty string using allocator @a a.
538 basic_string(const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
539 : _M_dataplus(_M_local_data(), __a
)
546 * @brief Construct string with copy of value of @a __str.
547 * @param __str Source string.
550 basic_string(const basic_string
& __str
)
551 : _M_dataplus(_M_local_data(),
552 _Alloc_traits::_S_select_on_copy(__str
._M_get_allocator()))
554 _M_construct(__str
._M_data(), __str
._M_data() + __str
.length(),
555 std::forward_iterator_tag());
558 // _GLIBCXX_RESOLVE_LIB_DEFECTS
559 // 2583. no way to supply an allocator for basic_string(str, pos)
561 * @brief Construct string as copy of a substring.
562 * @param __str Source string.
563 * @param __pos Index of first character to copy from.
564 * @param __a Allocator to use.
567 basic_string(const basic_string
& __str
, size_type __pos
,
568 const _Alloc
& __a
= _Alloc())
569 : _M_dataplus(_M_local_data(), __a
)
571 const _CharT
* __start
= __str
._M_data()
572 + __str
._M_check(__pos
, "basic_string::basic_string");
573 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, npos
),
574 std::forward_iterator_tag());
578 * @brief Construct string as copy of a substring.
579 * @param __str Source string.
580 * @param __pos Index of first character to copy from.
581 * @param __n Number of characters to copy.
584 basic_string(const basic_string
& __str
, size_type __pos
,
586 : _M_dataplus(_M_local_data())
588 const _CharT
* __start
= __str
._M_data()
589 + __str
._M_check(__pos
, "basic_string::basic_string");
590 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, __n
),
591 std::forward_iterator_tag());
595 * @brief Construct string as copy of a substring.
596 * @param __str Source string.
597 * @param __pos Index of first character to copy from.
598 * @param __n Number of characters to copy.
599 * @param __a Allocator to use.
602 basic_string(const basic_string
& __str
, size_type __pos
,
603 size_type __n
, const _Alloc
& __a
)
604 : _M_dataplus(_M_local_data(), __a
)
606 const _CharT
* __start
607 = __str
._M_data() + __str
._M_check(__pos
, "string::string");
608 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, __n
),
609 std::forward_iterator_tag());
613 * @brief Construct string initialized by a character %array.
614 * @param __s Source character %array.
615 * @param __n Number of characters to copy.
616 * @param __a Allocator to use (default is default allocator).
618 * NB: @a __s must have at least @a __n characters, '\\0'
619 * has no special meaning.
622 basic_string(const _CharT
* __s
, size_type __n
,
623 const _Alloc
& __a
= _Alloc())
624 : _M_dataplus(_M_local_data(), __a
)
626 // NB: Not required, but considered best practice.
627 if (__s
== 0 && __n
> 0)
628 std::__throw_logic_error(__N("basic_string: "
629 "construction from null is not valid"));
630 _M_construct(__s
, __s
+ __n
, std::forward_iterator_tag());
634 * @brief Construct string as copy of a C string.
635 * @param __s Source C string.
636 * @param __a Allocator to use (default is default allocator).
638 #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
639 // _GLIBCXX_RESOLVE_LIB_DEFECTS
640 // 3076. basic_string CTAD ambiguity
641 template<typename
= _RequireAllocator
<_Alloc
>>
644 basic_string(const _CharT
* __s
, const _Alloc
& __a
= _Alloc())
645 : _M_dataplus(_M_local_data(), __a
)
647 // NB: Not required, but considered best practice.
649 std::__throw_logic_error(__N("basic_string: "
650 "construction from null is not valid"));
651 const _CharT
* __end
= __s
+ traits_type::length(__s
);
652 _M_construct(__s
, __end
, forward_iterator_tag());
656 * @brief Construct string as multiple characters.
657 * @param __n Number of characters.
658 * @param __c Character to use.
659 * @param __a Allocator to use (default is default allocator).
661 #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
662 // _GLIBCXX_RESOLVE_LIB_DEFECTS
663 // 3076. basic_string CTAD ambiguity
664 template<typename
= _RequireAllocator
<_Alloc
>>
667 basic_string(size_type __n
, _CharT __c
, const _Alloc
& __a
= _Alloc())
668 : _M_dataplus(_M_local_data(), __a
)
669 { _M_construct(__n
, __c
); }
671 #if __cplusplus >= 201103L
673 * @brief Move construct string.
674 * @param __str Source string.
676 * The newly-created string contains the exact contents of @a __str.
677 * @a __str is a valid, but unspecified string.
680 basic_string(basic_string
&& __str
) noexcept
681 : _M_dataplus(_M_local_data(), std::move(__str
._M_get_allocator()))
683 if (__str
._M_is_local())
685 traits_type::copy(_M_local_buf
, __str
._M_local_buf
,
690 _M_data(__str
._M_data());
691 _M_capacity(__str
._M_allocated_capacity
);
694 // Must use _M_length() here not _M_set_length() because
695 // basic_stringbuf relies on writing into unallocated capacity so
696 // we mess up the contents if we put a '\0' in the string.
697 _M_length(__str
.length());
698 __str
._M_data(__str
._M_local_data());
699 __str
._M_set_length(0);
703 * @brief Construct string from an initializer %list.
704 * @param __l std::initializer_list of characters.
705 * @param __a Allocator to use (default is default allocator).
708 basic_string(initializer_list
<_CharT
> __l
, const _Alloc
& __a
= _Alloc())
709 : _M_dataplus(_M_local_data(), __a
)
710 { _M_construct(__l
.begin(), __l
.end(), std::forward_iterator_tag()); }
713 basic_string(const basic_string
& __str
, const _Alloc
& __a
)
714 : _M_dataplus(_M_local_data(), __a
)
715 { _M_construct(__str
.begin(), __str
.end(), std::forward_iterator_tag()); }
718 basic_string(basic_string
&& __str
, const _Alloc
& __a
)
719 noexcept(_Alloc_traits::_S_always_equal())
720 : _M_dataplus(_M_local_data(), __a
)
722 if (__str
._M_is_local())
724 traits_type::copy(_M_local_buf
, __str
._M_local_buf
,
726 _M_length(__str
.length());
727 __str
._M_set_length(0);
729 else if (_Alloc_traits::_S_always_equal()
730 || __str
.get_allocator() == __a
)
732 _M_data(__str
._M_data());
733 _M_length(__str
.length());
734 _M_capacity(__str
._M_allocated_capacity
);
735 __str
._M_data(__str
._M_local_buf
);
736 __str
._M_set_length(0);
739 _M_construct(__str
.begin(), __str
.end(), std::forward_iterator_tag());
743 #if __cplusplus >= 202100L
744 basic_string(nullptr_t
) = delete;
745 basic_string
& operator=(nullptr_t
) = delete;
749 * @brief Construct string as copy of a range.
750 * @param __beg Start of range.
751 * @param __end End of range.
752 * @param __a Allocator to use (default is default allocator).
754 #if __cplusplus >= 201103L
755 template<typename _InputIterator
,
756 typename
= std::_RequireInputIter
<_InputIterator
>>
758 template<typename _InputIterator
>
761 basic_string(_InputIterator __beg
, _InputIterator __end
,
762 const _Alloc
& __a
= _Alloc())
763 : _M_dataplus(_M_local_data(), __a
)
765 #if __cplusplus >= 201103L
766 _M_construct(__beg
, __end
, std::__iterator_category(__beg
));
768 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
769 _M_construct_aux(__beg
, __end
, _Integral());
773 #if __cplusplus >= 201703L
775 * @brief Construct string from a substring of a string_view.
776 * @param __t Source object convertible to string view.
777 * @param __pos The index of the first character to copy from __t.
778 * @param __n The number of characters to copy from __t.
779 * @param __a Allocator to use.
781 template<typename _Tp
,
782 typename
= enable_if_t
<is_convertible_v
<const _Tp
&, __sv_type
>>>
784 basic_string(const _Tp
& __t
, size_type __pos
, size_type __n
,
785 const _Alloc
& __a
= _Alloc())
786 : basic_string(_S_to_string_view(__t
).substr(__pos
, __n
), __a
) { }
789 * @brief Construct string from a string_view.
790 * @param __t Source object convertible to string view.
791 * @param __a Allocator to use (default is default allocator).
793 template<typename _Tp
, typename
= _If_sv
<_Tp
, void>>
796 basic_string(const _Tp
& __t
, const _Alloc
& __a
= _Alloc())
797 : basic_string(__sv_wrapper(_S_to_string_view(__t
)), __a
) { }
801 * @brief Destroy the string instance.
808 * @brief Assign the value of @a str to this string.
809 * @param __str Source string.
813 operator=(const basic_string
& __str
)
815 return this->assign(__str
);
819 * @brief Copy contents of @a s into this string.
820 * @param __s Source null-terminated string.
824 operator=(const _CharT
* __s
)
825 { return this->assign(__s
); }
828 * @brief Set value to string of length 1.
829 * @param __c Source character.
831 * Assigning to a character makes this string length 1 and
832 * (*this)[0] == @a c.
836 operator=(_CharT __c
)
838 this->assign(1, __c
);
842 #if __cplusplus >= 201103L
844 * @brief Move assign the value of @a str to this string.
845 * @param __str Source string.
847 * The contents of @a str are moved into this string (without copying).
848 * @a str is a valid, but unspecified string.
850 // _GLIBCXX_RESOLVE_LIB_DEFECTS
851 // 2063. Contradictory requirements for string move assignment
854 operator=(basic_string
&& __str
)
855 noexcept(_Alloc_traits::_S_nothrow_move())
857 const bool __equal_allocs
= _Alloc_traits::_S_always_equal()
858 || _M_get_allocator() == __str
._M_get_allocator();
859 if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
862 // Destroy existing storage before replacing allocator.
863 _M_destroy(_M_allocated_capacity
);
864 _M_data(_M_local_data());
867 // Replace allocator if POCMA is true.
868 std::__alloc_on_move(_M_get_allocator(), __str
._M_get_allocator());
870 if (__str
._M_is_local())
872 // We've always got room for a short string, just copy it
873 // (unless this is a self-move, because that would violate the
874 // char_traits::copy precondition that the ranges don't overlap).
875 if (__builtin_expect(std::__addressof(__str
) != this, true))
878 this->_S_copy(_M_data(), __str
._M_data(), __str
.size());
879 _M_set_length(__str
.size());
882 else if (_Alloc_traits::_S_propagate_on_move_assign() || __equal_allocs
)
884 // Just move the allocated pointer, our allocator can free it.
885 pointer __data
= nullptr;
886 size_type __capacity
;
891 // __str can reuse our existing storage.
893 __capacity
= _M_allocated_capacity
;
895 else // __str can't use it, so free it.
896 _M_destroy(_M_allocated_capacity
);
899 _M_data(__str
._M_data());
900 _M_length(__str
.length());
901 _M_capacity(__str
._M_allocated_capacity
);
904 __str
._M_data(__data
);
905 __str
._M_capacity(__capacity
);
908 __str
._M_data(__str
._M_local_buf
);
910 else // Need to do a deep copy
917 * @brief Set value to string constructed from initializer %list.
918 * @param __l std::initializer_list.
922 operator=(initializer_list
<_CharT
> __l
)
924 this->assign(__l
.begin(), __l
.size());
929 #if __cplusplus >= 201703L
931 * @brief Set value to string constructed from a string_view.
932 * @param __svt An object convertible to string_view.
934 template<typename _Tp
>
936 _If_sv
<_Tp
, basic_string
&>
937 operator=(const _Tp
& __svt
)
938 { return this->assign(__svt
); }
941 * @brief Convert to a string_view.
942 * @return A string_view.
945 operator __sv_type() const noexcept
946 { return __sv_type(data(), size()); }
951 * Returns a read/write iterator that points to the first character in
954 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
956 begin() _GLIBCXX_NOEXCEPT
957 { return iterator(_M_data()); }
960 * Returns a read-only (constant) iterator that points to the first
961 * character in the %string.
963 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
965 begin() const _GLIBCXX_NOEXCEPT
966 { return const_iterator(_M_data()); }
969 * Returns a read/write iterator that points one past the last
970 * character in the %string.
972 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
974 end() _GLIBCXX_NOEXCEPT
975 { return iterator(_M_data() + this->size()); }
978 * Returns a read-only (constant) iterator that points one past the
979 * last character in the %string.
981 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
983 end() const _GLIBCXX_NOEXCEPT
984 { return const_iterator(_M_data() + this->size()); }
987 * Returns a read/write reverse iterator that points to the last
988 * character in the %string. Iteration is done in reverse element
991 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
993 rbegin() _GLIBCXX_NOEXCEPT
994 { return reverse_iterator(this->end()); }
997 * Returns a read-only (constant) reverse iterator that points
998 * to the last character in the %string. Iteration is done in
999 * reverse element order.
1001 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1002 const_reverse_iterator
1003 rbegin() const _GLIBCXX_NOEXCEPT
1004 { return const_reverse_iterator(this->end()); }
1007 * Returns a read/write reverse iterator that points to one before the
1008 * first character in the %string. Iteration is done in reverse
1011 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1013 rend() _GLIBCXX_NOEXCEPT
1014 { return reverse_iterator(this->begin()); }
1017 * Returns a read-only (constant) reverse iterator that points
1018 * to one before the first character in the %string. Iteration
1019 * is done in reverse element order.
1021 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1022 const_reverse_iterator
1023 rend() const _GLIBCXX_NOEXCEPT
1024 { return const_reverse_iterator(this->begin()); }
1026 #if __cplusplus >= 201103L
1028 * Returns a read-only (constant) iterator that points to the first
1029 * character in the %string.
1031 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1033 cbegin() const noexcept
1034 { return const_iterator(this->_M_data()); }
1037 * Returns a read-only (constant) iterator that points one past the
1038 * last character in the %string.
1040 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1042 cend() const noexcept
1043 { return const_iterator(this->_M_data() + this->size()); }
1046 * Returns a read-only (constant) reverse iterator that points
1047 * to the last character in the %string. Iteration is done in
1048 * reverse element order.
1050 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1051 const_reverse_iterator
1052 crbegin() const noexcept
1053 { return const_reverse_iterator(this->end()); }
1056 * Returns a read-only (constant) reverse iterator that points
1057 * to one before the first character in the %string. Iteration
1058 * is done in reverse element order.
1060 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1061 const_reverse_iterator
1062 crend() const noexcept
1063 { return const_reverse_iterator(this->begin()); }
1068 /// Returns the number of characters in the string, not including any
1069 /// null-termination.
1070 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1072 size() const _GLIBCXX_NOEXCEPT
1073 { return _M_string_length
; }
1075 /// Returns the number of characters in the string, not including any
1076 /// null-termination.
1077 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1079 length() const _GLIBCXX_NOEXCEPT
1080 { return _M_string_length
; }
1082 /// Returns the size() of the largest possible %string.
1083 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1085 max_size() const _GLIBCXX_NOEXCEPT
1086 { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
1089 * @brief Resizes the %string to the specified number of characters.
1090 * @param __n Number of characters the %string should contain.
1091 * @param __c Character to fill any new elements.
1093 * This function will %resize the %string to the specified
1094 * number of characters. If the number is smaller than the
1095 * %string's current size the %string is truncated, otherwise
1096 * the %string is extended and new elements are %set to @a __c.
1098 _GLIBCXX20_CONSTEXPR
1100 resize(size_type __n
, _CharT __c
);
1103 * @brief Resizes the %string to the specified number of characters.
1104 * @param __n Number of characters the %string should contain.
1106 * This function will resize the %string to the specified length. If
1107 * the new size is smaller than the %string's current size the %string
1108 * is truncated, otherwise the %string is extended and new characters
1109 * are default-constructed. For basic types such as char, this means
1110 * setting them to 0.
1112 _GLIBCXX20_CONSTEXPR
1114 resize(size_type __n
)
1115 { this->resize(__n
, _CharT()); }
1117 #if __cplusplus >= 201103L
1118 #pragma GCC diagnostic push
1119 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1120 /// A non-binding request to reduce capacity() to size().
1121 _GLIBCXX20_CONSTEXPR
1123 shrink_to_fit() noexcept
1125 #pragma GCC diagnostic pop
1128 #if __cplusplus > 202002L
1129 #define __cpp_lib_string_resize_and_overwrite 202110L
1130 /** Resize the string and call a function to fill it.
1132 * @param __n The maximum size requested.
1133 * @param __op A callable object that writes characters to the string.
1135 * This is a low-level function that is easy to misuse, be careful.
1137 * Calling `str.resize_and_overwrite(n, op)` will reserve at least `n`
1138 * characters in `str`, evaluate `n2 = std::move(op)(str.data(), n)`,
1139 * and finally set the string length to `n2` (adding a null terminator
1140 * at the end). The function object `op` is allowed to write to the
1141 * extra capacity added by the initial reserve operation, which is not
1142 * allowed if you just call `str.reserve(n)` yourself.
1144 * This can be used to efficiently fill a `string` buffer without the
1145 * overhead of zero-initializing characters that will be overwritten
1148 * The callable `op` must not access the string directly (only through
1149 * the pointer passed as its first argument), must not write more than
1150 * `n` characters to the string, must return a value no greater than `n`,
1151 * and must ensure that all characters up to the returned length are
1152 * valid after it returns (i.e. there must be no uninitialized values
1153 * left in the string after the call, because accessing them would
1154 * have undefined behaviour). If `op` exits by throwing an exception
1155 * the behaviour is undefined.
1159 template<typename _Operation
>
1161 resize_and_overwrite(size_type __n
, _Operation __op
);
1165 * Returns the total number of characters that the %string can hold
1166 * before needing to allocate more memory.
1168 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1170 capacity() const _GLIBCXX_NOEXCEPT
1172 return _M_is_local() ? size_type(_S_local_capacity
)
1173 : _M_allocated_capacity
;
1177 * @brief Attempt to preallocate enough memory for specified number of
1179 * @param __res_arg Number of characters required.
1180 * @throw std::length_error If @a __res_arg exceeds @c max_size().
1182 * This function attempts to reserve enough memory for the
1183 * %string to hold the specified number of characters. If the
1184 * number requested is more than max_size(), length_error is
1187 * The advantage of this function is that if optimal code is a
1188 * necessity and the user can determine the string length that will be
1189 * required, the user can reserve the memory in %advance, and thus
1190 * prevent a possible reallocation of memory and copying of %string
1193 _GLIBCXX20_CONSTEXPR
1195 reserve(size_type __res_arg
);
1198 * Equivalent to shrink_to_fit().
1200 #if __cplusplus > 201703L
1201 [[deprecated("use shrink_to_fit() instead")]]
1203 _GLIBCXX20_CONSTEXPR
1208 * Erases the string, making it empty.
1210 _GLIBCXX20_CONSTEXPR
1212 clear() _GLIBCXX_NOEXCEPT
1213 { _M_set_length(0); }
1216 * Returns true if the %string is empty. Equivalent to
1217 * <code>*this == ""</code>.
1219 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1221 empty() const _GLIBCXX_NOEXCEPT
1222 { return this->size() == 0; }
1226 * @brief Subscript access to the data contained in the %string.
1227 * @param __pos The index of the character to access.
1228 * @return Read-only (constant) reference to the character.
1230 * This operator allows for easy, array-style, data access.
1231 * Note that data access with this operator is unchecked and
1232 * out_of_range lookups are not defined. (For checked lookups
1235 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1237 operator[] (size_type __pos
) const _GLIBCXX_NOEXCEPT
1239 __glibcxx_assert(__pos
<= size());
1240 return _M_data()[__pos
];
1244 * @brief Subscript access to the data contained in the %string.
1245 * @param __pos The index of the character to access.
1246 * @return Read/write reference to the character.
1248 * This operator allows for easy, array-style, data access.
1249 * Note that data access with this operator is unchecked and
1250 * out_of_range lookups are not defined. (For checked lookups
1253 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1255 operator[](size_type __pos
)
1257 // Allow pos == size() both in C++98 mode, as v3 extension,
1258 // and in C++11 mode.
1259 __glibcxx_assert(__pos
<= size());
1260 // In pedantic mode be strict in C++98 mode.
1261 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus
>= 201103L || __pos
< size());
1262 return _M_data()[__pos
];
1266 * @brief Provides access to the data contained in the %string.
1267 * @param __n The index of the character to access.
1268 * @return Read-only (const) reference to the character.
1269 * @throw std::out_of_range If @a n is an invalid index.
1271 * This function provides for safer data access. The parameter is
1272 * first checked that it is in the range of the string. The function
1273 * throws out_of_range if the check fails.
1275 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1277 at(size_type __n
) const
1279 if (__n
>= this->size())
1280 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1281 "(which is %zu) >= this->size() "
1284 return _M_data()[__n
];
1288 * @brief Provides access to the data contained in the %string.
1289 * @param __n The index of the character to access.
1290 * @return Read/write reference to the character.
1291 * @throw std::out_of_range If @a n is an invalid index.
1293 * This function provides for safer data access. The parameter is
1294 * first checked that it is in the range of the string. The function
1295 * throws out_of_range if the check fails.
1297 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1302 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1303 "(which is %zu) >= this->size() "
1306 return _M_data()[__n
];
1309 #if __cplusplus >= 201103L
1311 * Returns a read/write reference to the data at the first
1312 * element of the %string.
1314 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1318 __glibcxx_assert(!empty());
1319 return operator[](0);
1323 * Returns a read-only (constant) reference to the data at the first
1324 * element of the %string.
1326 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1328 front() const noexcept
1330 __glibcxx_assert(!empty());
1331 return operator[](0);
1335 * Returns a read/write reference to the data at the last
1336 * element of the %string.
1338 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1342 __glibcxx_assert(!empty());
1343 return operator[](this->size() - 1);
1347 * Returns a read-only (constant) reference to the data at the
1348 * last element of the %string.
1350 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1352 back() const noexcept
1354 __glibcxx_assert(!empty());
1355 return operator[](this->size() - 1);
1361 * @brief Append a string to this string.
1362 * @param __str The string to append.
1363 * @return Reference to this string.
1365 _GLIBCXX20_CONSTEXPR
1367 operator+=(const basic_string
& __str
)
1368 { return this->append(__str
); }
1371 * @brief Append a C string.
1372 * @param __s The C string to append.
1373 * @return Reference to this string.
1375 _GLIBCXX20_CONSTEXPR
1377 operator+=(const _CharT
* __s
)
1378 { return this->append(__s
); }
1381 * @brief Append a character.
1382 * @param __c The character to append.
1383 * @return Reference to this string.
1385 _GLIBCXX20_CONSTEXPR
1387 operator+=(_CharT __c
)
1389 this->push_back(__c
);
1393 #if __cplusplus >= 201103L
1395 * @brief Append an initializer_list of characters.
1396 * @param __l The initializer_list of characters to be appended.
1397 * @return Reference to this string.
1399 _GLIBCXX20_CONSTEXPR
1401 operator+=(initializer_list
<_CharT
> __l
)
1402 { return this->append(__l
.begin(), __l
.size()); }
1405 #if __cplusplus >= 201703L
1407 * @brief Append a string_view.
1408 * @param __svt An object convertible to string_view to be appended.
1409 * @return Reference to this string.
1411 template<typename _Tp
>
1412 _GLIBCXX20_CONSTEXPR
1413 _If_sv
<_Tp
, basic_string
&>
1414 operator+=(const _Tp
& __svt
)
1415 { return this->append(__svt
); }
1419 * @brief Append a string to this string.
1420 * @param __str The string to append.
1421 * @return Reference to this string.
1423 _GLIBCXX20_CONSTEXPR
1425 append(const basic_string
& __str
)
1426 { return this->append(__str
._M_data(), __str
.size()); }
1429 * @brief Append a substring.
1430 * @param __str The string to append.
1431 * @param __pos Index of the first character of str to append.
1432 * @param __n The number of characters to append.
1433 * @return Reference to this string.
1434 * @throw std::out_of_range if @a __pos is not a valid index.
1436 * This function appends @a __n characters from @a __str
1437 * starting at @a __pos to this string. If @a __n is is larger
1438 * than the number of available characters in @a __str, the
1439 * remainder of @a __str is appended.
1441 _GLIBCXX20_CONSTEXPR
1443 append(const basic_string
& __str
, size_type __pos
, size_type __n
= npos
)
1444 { return this->append(__str
._M_data()
1445 + __str
._M_check(__pos
, "basic_string::append"),
1446 __str
._M_limit(__pos
, __n
)); }
1449 * @brief Append a C substring.
1450 * @param __s The C string to append.
1451 * @param __n The number of characters to append.
1452 * @return Reference to this string.
1454 _GLIBCXX20_CONSTEXPR
1456 append(const _CharT
* __s
, size_type __n
)
1458 __glibcxx_requires_string_len(__s
, __n
);
1459 _M_check_length(size_type(0), __n
, "basic_string::append");
1460 return _M_append(__s
, __n
);
1464 * @brief Append a C string.
1465 * @param __s The C string to append.
1466 * @return Reference to this string.
1468 _GLIBCXX20_CONSTEXPR
1470 append(const _CharT
* __s
)
1472 __glibcxx_requires_string(__s
);
1473 const size_type __n
= traits_type::length(__s
);
1474 _M_check_length(size_type(0), __n
, "basic_string::append");
1475 return _M_append(__s
, __n
);
1479 * @brief Append multiple characters.
1480 * @param __n The number of characters to append.
1481 * @param __c The character to use.
1482 * @return Reference to this string.
1484 * Appends __n copies of __c to this string.
1486 _GLIBCXX20_CONSTEXPR
1488 append(size_type __n
, _CharT __c
)
1489 { return _M_replace_aux(this->size(), size_type(0), __n
, __c
); }
1491 #if __cplusplus >= 201103L
1493 * @brief Append an initializer_list of characters.
1494 * @param __l The initializer_list of characters to append.
1495 * @return Reference to this string.
1497 _GLIBCXX20_CONSTEXPR
1499 append(initializer_list
<_CharT
> __l
)
1500 { return this->append(__l
.begin(), __l
.size()); }
1504 * @brief Append a range of characters.
1505 * @param __first Iterator referencing the first character to append.
1506 * @param __last Iterator marking the end of the range.
1507 * @return Reference to this string.
1509 * Appends characters in the range [__first,__last) to this string.
1511 #if __cplusplus >= 201103L
1512 template<class _InputIterator
,
1513 typename
= std::_RequireInputIter
<_InputIterator
>>
1514 _GLIBCXX20_CONSTEXPR
1516 template<class _InputIterator
>
1519 append(_InputIterator __first
, _InputIterator __last
)
1520 { return this->replace(end(), end(), __first
, __last
); }
1522 #if __cplusplus >= 201703L
1524 * @brief Append a string_view.
1525 * @param __svt An object convertible to string_view to be appended.
1526 * @return Reference to this string.
1528 template<typename _Tp
>
1529 _GLIBCXX20_CONSTEXPR
1530 _If_sv
<_Tp
, basic_string
&>
1531 append(const _Tp
& __svt
)
1533 __sv_type __sv
= __svt
;
1534 return this->append(__sv
.data(), __sv
.size());
1538 * @brief Append a range of characters from a string_view.
1539 * @param __svt An object convertible to string_view to be appended from.
1540 * @param __pos The position in the string_view to append from.
1541 * @param __n The number of characters to append from the string_view.
1542 * @return Reference to this string.
1544 template<typename _Tp
>
1545 _GLIBCXX20_CONSTEXPR
1546 _If_sv
<_Tp
, basic_string
&>
1547 append(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
1549 __sv_type __sv
= __svt
;
1550 return _M_append(__sv
.data()
1551 + std::__sv_check(__sv
.size(), __pos
, "basic_string::append"),
1552 std::__sv_limit(__sv
.size(), __pos
, __n
));
1557 * @brief Append a single character.
1558 * @param __c Character to append.
1560 _GLIBCXX20_CONSTEXPR
1562 push_back(_CharT __c
)
1564 const size_type __size
= this->size();
1565 if (__size
+ 1 > this->capacity())
1566 this->_M_mutate(__size
, size_type(0), 0, size_type(1));
1567 traits_type::assign(this->_M_data()[__size
], __c
);
1568 this->_M_set_length(__size
+ 1);
1572 * @brief Set value to contents of another string.
1573 * @param __str Source string to use.
1574 * @return Reference to this string.
1576 _GLIBCXX20_CONSTEXPR
1578 assign(const basic_string
& __str
)
1580 #if __cplusplus >= 201103L
1581 if (_Alloc_traits::_S_propagate_on_copy_assign())
1583 if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
1584 && _M_get_allocator() != __str
._M_get_allocator())
1586 // Propagating allocator cannot free existing storage so must
1587 // deallocate it before replacing current allocator.
1588 if (__str
.size() <= _S_local_capacity
)
1590 _M_destroy(_M_allocated_capacity
);
1591 _M_data(_M_use_local_data());
1596 const auto __len
= __str
.size();
1597 auto __alloc
= __str
._M_get_allocator();
1598 // If this allocation throws there are no effects:
1599 auto __ptr
= _Alloc_traits::allocate(__alloc
, __len
+ 1);
1600 _M_destroy(_M_allocated_capacity
);
1603 _M_set_length(__len
);
1606 std::__alloc_on_copy(_M_get_allocator(), __str
._M_get_allocator());
1609 this->_M_assign(__str
);
1613 #if __cplusplus >= 201103L
1615 * @brief Set value to contents of another string.
1616 * @param __str Source string to use.
1617 * @return Reference to this string.
1619 * This function sets this string to the exact contents of @a __str.
1620 * @a __str is a valid, but unspecified string.
1622 _GLIBCXX20_CONSTEXPR
1624 assign(basic_string
&& __str
)
1625 noexcept(_Alloc_traits::_S_nothrow_move())
1627 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1628 // 2063. Contradictory requirements for string move assignment
1629 return *this = std::move(__str
);
1634 * @brief Set value to a substring of a string.
1635 * @param __str The string to use.
1636 * @param __pos Index of the first character of str.
1637 * @param __n Number of characters to use.
1638 * @return Reference to this string.
1639 * @throw std::out_of_range if @a pos is not a valid index.
1641 * This function sets this string to the substring of @a __str
1642 * consisting of @a __n characters at @a __pos. If @a __n is
1643 * is larger than the number of available characters in @a
1644 * __str, the remainder of @a __str is used.
1646 _GLIBCXX20_CONSTEXPR
1648 assign(const basic_string
& __str
, size_type __pos
, size_type __n
= npos
)
1649 { return _M_replace(size_type(0), this->size(), __str
._M_data()
1650 + __str
._M_check(__pos
, "basic_string::assign"),
1651 __str
._M_limit(__pos
, __n
)); }
1654 * @brief Set value to a C substring.
1655 * @param __s The C string to use.
1656 * @param __n Number of characters to use.
1657 * @return Reference to this string.
1659 * This function sets the value of this string to the first @a __n
1660 * characters of @a __s. If @a __n is is larger than the number of
1661 * available characters in @a __s, the remainder of @a __s is used.
1663 _GLIBCXX20_CONSTEXPR
1665 assign(const _CharT
* __s
, size_type __n
)
1667 __glibcxx_requires_string_len(__s
, __n
);
1668 return _M_replace(size_type(0), this->size(), __s
, __n
);
1672 * @brief Set value to contents of a C string.
1673 * @param __s The C string to use.
1674 * @return Reference to this string.
1676 * This function sets the value of this string to the value of @a __s.
1677 * The data is copied, so there is no dependence on @a __s once the
1680 _GLIBCXX20_CONSTEXPR
1682 assign(const _CharT
* __s
)
1684 __glibcxx_requires_string(__s
);
1685 return _M_replace(size_type(0), this->size(), __s
,
1686 traits_type::length(__s
));
1690 * @brief Set value to multiple characters.
1691 * @param __n Length of the resulting string.
1692 * @param __c The character to use.
1693 * @return Reference to this string.
1695 * This function sets the value of this string to @a __n copies of
1698 _GLIBCXX20_CONSTEXPR
1700 assign(size_type __n
, _CharT __c
)
1701 { return _M_replace_aux(size_type(0), this->size(), __n
, __c
); }
1704 * @brief Set value to a range of characters.
1705 * @param __first Iterator referencing the first character to append.
1706 * @param __last Iterator marking the end of the range.
1707 * @return Reference to this string.
1709 * Sets value of string to characters in the range [__first,__last).
1711 #if __cplusplus >= 201103L
1712 template<class _InputIterator
,
1713 typename
= std::_RequireInputIter
<_InputIterator
>>
1714 _GLIBCXX20_CONSTEXPR
1716 template<class _InputIterator
>
1719 assign(_InputIterator __first
, _InputIterator __last
)
1720 { return this->replace(begin(), end(), __first
, __last
); }
1722 #if __cplusplus >= 201103L
1724 * @brief Set value to an initializer_list of characters.
1725 * @param __l The initializer_list of characters to assign.
1726 * @return Reference to this string.
1728 _GLIBCXX20_CONSTEXPR
1730 assign(initializer_list
<_CharT
> __l
)
1731 { return this->assign(__l
.begin(), __l
.size()); }
1734 #if __cplusplus >= 201703L
1736 * @brief Set value from a string_view.
1737 * @param __svt The source object convertible to string_view.
1738 * @return Reference to this string.
1740 template<typename _Tp
>
1741 _GLIBCXX20_CONSTEXPR
1742 _If_sv
<_Tp
, basic_string
&>
1743 assign(const _Tp
& __svt
)
1745 __sv_type __sv
= __svt
;
1746 return this->assign(__sv
.data(), __sv
.size());
1750 * @brief Set value from a range of characters in a string_view.
1751 * @param __svt The source object convertible to string_view.
1752 * @param __pos The position in the string_view to assign from.
1753 * @param __n The number of characters to assign.
1754 * @return Reference to this string.
1756 template<typename _Tp
>
1757 _GLIBCXX20_CONSTEXPR
1758 _If_sv
<_Tp
, basic_string
&>
1759 assign(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
1761 __sv_type __sv
= __svt
;
1762 return _M_replace(size_type(0), this->size(),
1764 + std::__sv_check(__sv
.size(), __pos
, "basic_string::assign"),
1765 std::__sv_limit(__sv
.size(), __pos
, __n
));
1769 #if __cplusplus >= 201103L
1771 * @brief Insert multiple characters.
1772 * @param __p Const_iterator referencing location in string to
1774 * @param __n Number of characters to insert
1775 * @param __c The character to insert.
1776 * @return Iterator referencing the first inserted char.
1777 * @throw std::length_error If new length exceeds @c max_size().
1779 * Inserts @a __n copies of character @a __c starting at the
1780 * position referenced by iterator @a __p. If adding
1781 * characters causes the length to exceed max_size(),
1782 * length_error is thrown. The value of the string doesn't
1783 * change if an error is thrown.
1785 _GLIBCXX20_CONSTEXPR
1787 insert(const_iterator __p
, size_type __n
, _CharT __c
)
1789 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1790 const size_type __pos
= __p
- begin();
1791 this->replace(__p
, __p
, __n
, __c
);
1792 return iterator(this->_M_data() + __pos
);
1796 * @brief Insert multiple characters.
1797 * @param __p Iterator referencing location in string to insert at.
1798 * @param __n Number of characters to insert
1799 * @param __c The character to insert.
1800 * @throw std::length_error If new length exceeds @c max_size().
1802 * Inserts @a __n copies of character @a __c starting at the
1803 * position referenced by iterator @a __p. If adding
1804 * characters causes the length to exceed max_size(),
1805 * length_error is thrown. The value of the string doesn't
1806 * change if an error is thrown.
1809 insert(iterator __p
, size_type __n
, _CharT __c
)
1810 { this->replace(__p
, __p
, __n
, __c
); }
1813 #if __cplusplus >= 201103L
1815 * @brief Insert a range of characters.
1816 * @param __p Const_iterator referencing location in string to
1818 * @param __beg Start of range.
1819 * @param __end End of range.
1820 * @return Iterator referencing the first inserted char.
1821 * @throw std::length_error If new length exceeds @c max_size().
1823 * Inserts characters in range [beg,end). If adding characters
1824 * causes the length to exceed max_size(), length_error is
1825 * thrown. The value of the string doesn't change if an error
1828 template<class _InputIterator
,
1829 typename
= std::_RequireInputIter
<_InputIterator
>>
1830 _GLIBCXX20_CONSTEXPR
1832 insert(const_iterator __p
, _InputIterator __beg
, _InputIterator __end
)
1834 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1835 const size_type __pos
= __p
- begin();
1836 this->replace(__p
, __p
, __beg
, __end
);
1837 return iterator(this->_M_data() + __pos
);
1841 * @brief Insert a range of characters.
1842 * @param __p Iterator referencing location in string to insert at.
1843 * @param __beg Start of range.
1844 * @param __end End of range.
1845 * @throw std::length_error If new length exceeds @c max_size().
1847 * Inserts characters in range [__beg,__end). If adding
1848 * characters causes the length to exceed max_size(),
1849 * length_error is thrown. The value of the string doesn't
1850 * change if an error is thrown.
1852 template<class _InputIterator
>
1854 insert(iterator __p
, _InputIterator __beg
, _InputIterator __end
)
1855 { this->replace(__p
, __p
, __beg
, __end
); }
1858 #if __cplusplus >= 201103L
1860 * @brief Insert an initializer_list of characters.
1861 * @param __p Iterator referencing location in string to insert at.
1862 * @param __l The initializer_list of characters to insert.
1863 * @throw std::length_error If new length exceeds @c max_size().
1865 _GLIBCXX20_CONSTEXPR
1867 insert(const_iterator __p
, initializer_list
<_CharT
> __l
)
1868 { return this->insert(__p
, __l
.begin(), __l
.end()); }
1870 #ifdef _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
1871 // See PR libstdc++/83328
1873 insert(iterator __p
, initializer_list
<_CharT
> __l
)
1875 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1876 this->insert(__p
- begin(), __l
.begin(), __l
.size());
1882 * @brief Insert value of a string.
1883 * @param __pos1 Position in string to insert at.
1884 * @param __str The string to insert.
1885 * @return Reference to this string.
1886 * @throw std::length_error If new length exceeds @c max_size().
1888 * Inserts value of @a __str starting at @a __pos1. If adding
1889 * characters causes the length to exceed max_size(),
1890 * length_error is thrown. The value of the string doesn't
1891 * change if an error is thrown.
1893 _GLIBCXX20_CONSTEXPR
1895 insert(size_type __pos1
, const basic_string
& __str
)
1896 { return this->replace(__pos1
, size_type(0),
1897 __str
._M_data(), __str
.size()); }
1900 * @brief Insert a substring.
1901 * @param __pos1 Position in string to insert at.
1902 * @param __str The string to insert.
1903 * @param __pos2 Start of characters in str to insert.
1904 * @param __n Number of characters to insert.
1905 * @return Reference to this string.
1906 * @throw std::length_error If new length exceeds @c max_size().
1907 * @throw std::out_of_range If @a pos1 > size() or
1908 * @a __pos2 > @a str.size().
1910 * Starting at @a pos1, insert @a __n character of @a __str
1911 * beginning with @a __pos2. If adding characters causes the
1912 * length to exceed max_size(), length_error is thrown. If @a
1913 * __pos1 is beyond the end of this string or @a __pos2 is
1914 * beyond the end of @a __str, out_of_range is thrown. The
1915 * value of the string doesn't change if an error is thrown.
1917 _GLIBCXX20_CONSTEXPR
1919 insert(size_type __pos1
, const basic_string
& __str
,
1920 size_type __pos2
, size_type __n
= npos
)
1921 { return this->replace(__pos1
, size_type(0), __str
._M_data()
1922 + __str
._M_check(__pos2
, "basic_string::insert"),
1923 __str
._M_limit(__pos2
, __n
)); }
1926 * @brief Insert a C substring.
1927 * @param __pos Position in string to insert at.
1928 * @param __s The C string to insert.
1929 * @param __n The number of characters to insert.
1930 * @return Reference to this string.
1931 * @throw std::length_error If new length exceeds @c max_size().
1932 * @throw std::out_of_range If @a __pos is beyond the end of this
1935 * Inserts the first @a __n characters of @a __s starting at @a
1936 * __pos. If adding characters causes the length to exceed
1937 * max_size(), length_error is thrown. If @a __pos is beyond
1938 * end(), out_of_range is thrown. The value of the string
1939 * doesn't change if an error is thrown.
1941 _GLIBCXX20_CONSTEXPR
1943 insert(size_type __pos
, const _CharT
* __s
, size_type __n
)
1944 { return this->replace(__pos
, size_type(0), __s
, __n
); }
1947 * @brief Insert a C string.
1948 * @param __pos Position in string to insert at.
1949 * @param __s The C string to insert.
1950 * @return Reference to this string.
1951 * @throw std::length_error If new length exceeds @c max_size().
1952 * @throw std::out_of_range If @a pos is beyond the end of this
1955 * Inserts the first @a n characters of @a __s starting at @a __pos. If
1956 * adding characters causes the length to exceed max_size(),
1957 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
1958 * thrown. The value of the string doesn't change if an error is
1961 _GLIBCXX20_CONSTEXPR
1963 insert(size_type __pos
, const _CharT
* __s
)
1965 __glibcxx_requires_string(__s
);
1966 return this->replace(__pos
, size_type(0), __s
,
1967 traits_type::length(__s
));
1971 * @brief Insert multiple characters.
1972 * @param __pos Index in string to insert at.
1973 * @param __n Number of characters to insert
1974 * @param __c The character to insert.
1975 * @return Reference to this string.
1976 * @throw std::length_error If new length exceeds @c max_size().
1977 * @throw std::out_of_range If @a __pos is beyond the end of this
1980 * Inserts @a __n copies of character @a __c starting at index
1981 * @a __pos. If adding characters causes the length to exceed
1982 * max_size(), length_error is thrown. If @a __pos > length(),
1983 * out_of_range is thrown. The value of the string doesn't
1984 * change if an error is thrown.
1986 _GLIBCXX20_CONSTEXPR
1988 insert(size_type __pos
, size_type __n
, _CharT __c
)
1989 { return _M_replace_aux(_M_check(__pos
, "basic_string::insert"),
1990 size_type(0), __n
, __c
); }
1993 * @brief Insert one character.
1994 * @param __p Iterator referencing position in string to insert at.
1995 * @param __c The character to insert.
1996 * @return Iterator referencing newly inserted char.
1997 * @throw std::length_error If new length exceeds @c max_size().
1999 * Inserts character @a __c at position referenced by @a __p.
2000 * If adding character causes the length to exceed max_size(),
2001 * length_error is thrown. If @a __p is beyond end of string,
2002 * out_of_range is thrown. The value of the string doesn't
2003 * change if an error is thrown.
2005 _GLIBCXX20_CONSTEXPR
2007 insert(__const_iterator __p
, _CharT __c
)
2009 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
2010 const size_type __pos
= __p
- begin();
2011 _M_replace_aux(__pos
, size_type(0), size_type(1), __c
);
2012 return iterator(_M_data() + __pos
);
2015 #if __cplusplus >= 201703L
2017 * @brief Insert a string_view.
2018 * @param __pos Position in string to insert at.
2019 * @param __svt The object convertible to string_view to insert.
2020 * @return Reference to this string.
2022 template<typename _Tp
>
2023 _GLIBCXX20_CONSTEXPR
2024 _If_sv
<_Tp
, basic_string
&>
2025 insert(size_type __pos
, const _Tp
& __svt
)
2027 __sv_type __sv
= __svt
;
2028 return this->insert(__pos
, __sv
.data(), __sv
.size());
2032 * @brief Insert a string_view.
2033 * @param __pos1 Position in string to insert at.
2034 * @param __svt The object convertible to string_view to insert from.
2035 * @param __pos2 Start of characters in str to insert.
2036 * @param __n The number of characters to insert.
2037 * @return Reference to this string.
2039 template<typename _Tp
>
2040 _GLIBCXX20_CONSTEXPR
2041 _If_sv
<_Tp
, basic_string
&>
2042 insert(size_type __pos1
, const _Tp
& __svt
,
2043 size_type __pos2
, size_type __n
= npos
)
2045 __sv_type __sv
= __svt
;
2046 return this->replace(__pos1
, size_type(0),
2048 + std::__sv_check(__sv
.size(), __pos2
, "basic_string::insert"),
2049 std::__sv_limit(__sv
.size(), __pos2
, __n
));
2054 * @brief Remove characters.
2055 * @param __pos Index of first character to remove (default 0).
2056 * @param __n Number of characters to remove (default remainder).
2057 * @return Reference to this string.
2058 * @throw std::out_of_range If @a pos is beyond the end of this
2061 * Removes @a __n characters from this string starting at @a
2062 * __pos. The length of the string is reduced by @a __n. If
2063 * there are < @a __n characters to remove, the remainder of
2064 * the string is truncated. If @a __p is beyond end of string,
2065 * out_of_range is thrown. The value of the string doesn't
2066 * change if an error is thrown.
2068 _GLIBCXX20_CONSTEXPR
2070 erase(size_type __pos
= 0, size_type __n
= npos
)
2072 _M_check(__pos
, "basic_string::erase");
2074 this->_M_set_length(__pos
);
2076 this->_M_erase(__pos
, _M_limit(__pos
, __n
));
2081 * @brief Remove one character.
2082 * @param __position Iterator referencing the character to remove.
2083 * @return iterator referencing same location after removal.
2085 * Removes the character at @a __position from this string. The value
2086 * of the string doesn't change if an error is thrown.
2088 _GLIBCXX20_CONSTEXPR
2090 erase(__const_iterator __position
)
2092 _GLIBCXX_DEBUG_PEDASSERT(__position
>= begin()
2093 && __position
< end());
2094 const size_type __pos
= __position
- begin();
2095 this->_M_erase(__pos
, size_type(1));
2096 return iterator(_M_data() + __pos
);
2100 * @brief Remove a range of characters.
2101 * @param __first Iterator referencing the first character to remove.
2102 * @param __last Iterator referencing the end of the range.
2103 * @return Iterator referencing location of first after removal.
2105 * Removes the characters in the range [first,last) from this string.
2106 * The value of the string doesn't change if an error is thrown.
2108 _GLIBCXX20_CONSTEXPR
2110 erase(__const_iterator __first
, __const_iterator __last
)
2112 _GLIBCXX_DEBUG_PEDASSERT(__first
>= begin() && __first
<= __last
2113 && __last
<= end());
2114 const size_type __pos
= __first
- begin();
2115 if (__last
== end())
2116 this->_M_set_length(__pos
);
2118 this->_M_erase(__pos
, __last
- __first
);
2119 return iterator(this->_M_data() + __pos
);
2122 #if __cplusplus >= 201103L
2124 * @brief Remove the last character.
2126 * The string must be non-empty.
2128 _GLIBCXX20_CONSTEXPR
2132 __glibcxx_assert(!empty());
2133 _M_erase(size() - 1, 1);
2138 * @brief Replace characters with value from another string.
2139 * @param __pos Index of first character to replace.
2140 * @param __n Number of characters to be replaced.
2141 * @param __str String to insert.
2142 * @return Reference to this string.
2143 * @throw std::out_of_range If @a pos is beyond the end of this
2145 * @throw std::length_error If new length exceeds @c max_size().
2147 * Removes the characters in the range [__pos,__pos+__n) from
2148 * this string. In place, the value of @a __str is inserted.
2149 * If @a __pos is beyond end of string, out_of_range is thrown.
2150 * If the length of the result exceeds max_size(), length_error
2151 * is thrown. The value of the string doesn't change if an
2154 _GLIBCXX20_CONSTEXPR
2156 replace(size_type __pos
, size_type __n
, const basic_string
& __str
)
2157 { return this->replace(__pos
, __n
, __str
._M_data(), __str
.size()); }
2160 * @brief Replace characters with value from another string.
2161 * @param __pos1 Index of first character to replace.
2162 * @param __n1 Number of characters to be replaced.
2163 * @param __str String to insert.
2164 * @param __pos2 Index of first character of str to use.
2165 * @param __n2 Number of characters from str to use.
2166 * @return Reference to this string.
2167 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
2169 * @throw std::length_error If new length exceeds @c max_size().
2171 * Removes the characters in the range [__pos1,__pos1 + n) from this
2172 * string. In place, the value of @a __str is inserted. If @a __pos is
2173 * beyond end of string, out_of_range is thrown. If the length of the
2174 * result exceeds max_size(), length_error is thrown. The value of the
2175 * string doesn't change if an error is thrown.
2177 _GLIBCXX20_CONSTEXPR
2179 replace(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
2180 size_type __pos2
, size_type __n2
= npos
)
2181 { return this->replace(__pos1
, __n1
, __str
._M_data()
2182 + __str
._M_check(__pos2
, "basic_string::replace"),
2183 __str
._M_limit(__pos2
, __n2
)); }
2186 * @brief Replace characters with value of a C substring.
2187 * @param __pos Index of first character to replace.
2188 * @param __n1 Number of characters to be replaced.
2189 * @param __s C string to insert.
2190 * @param __n2 Number of characters from @a s to use.
2191 * @return Reference to this string.
2192 * @throw std::out_of_range If @a pos1 > size().
2193 * @throw std::length_error If new length exceeds @c max_size().
2195 * Removes the characters in the range [__pos,__pos + __n1)
2196 * from this string. In place, the first @a __n2 characters of
2197 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
2198 * @a __pos is beyond end of string, out_of_range is thrown. If
2199 * the length of result exceeds max_size(), length_error is
2200 * thrown. The value of the string doesn't change if an error
2203 _GLIBCXX20_CONSTEXPR
2205 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
2208 __glibcxx_requires_string_len(__s
, __n2
);
2209 return _M_replace(_M_check(__pos
, "basic_string::replace"),
2210 _M_limit(__pos
, __n1
), __s
, __n2
);
2214 * @brief Replace characters with value of a C string.
2215 * @param __pos Index of first character to replace.
2216 * @param __n1 Number of characters to be replaced.
2217 * @param __s C string to insert.
2218 * @return Reference to this string.
2219 * @throw std::out_of_range If @a pos > size().
2220 * @throw std::length_error If new length exceeds @c max_size().
2222 * Removes the characters in the range [__pos,__pos + __n1)
2223 * from this string. In place, the characters of @a __s are
2224 * inserted. If @a __pos is beyond end of string, out_of_range
2225 * is thrown. If the length of result exceeds max_size(),
2226 * length_error is thrown. The value of the string doesn't
2227 * change if an error is thrown.
2229 _GLIBCXX20_CONSTEXPR
2231 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
)
2233 __glibcxx_requires_string(__s
);
2234 return this->replace(__pos
, __n1
, __s
, traits_type::length(__s
));
2238 * @brief Replace characters with multiple characters.
2239 * @param __pos Index of first character to replace.
2240 * @param __n1 Number of characters to be replaced.
2241 * @param __n2 Number of characters to insert.
2242 * @param __c Character to insert.
2243 * @return Reference to this string.
2244 * @throw std::out_of_range If @a __pos > size().
2245 * @throw std::length_error If new length exceeds @c max_size().
2247 * Removes the characters in the range [pos,pos + n1) from this
2248 * string. In place, @a __n2 copies of @a __c are inserted.
2249 * If @a __pos is beyond end of string, out_of_range is thrown.
2250 * If the length of result exceeds max_size(), length_error is
2251 * thrown. The value of the string doesn't change if an error
2254 _GLIBCXX20_CONSTEXPR
2256 replace(size_type __pos
, size_type __n1
, size_type __n2
, _CharT __c
)
2257 { return _M_replace_aux(_M_check(__pos
, "basic_string::replace"),
2258 _M_limit(__pos
, __n1
), __n2
, __c
); }
2261 * @brief Replace range of characters with string.
2262 * @param __i1 Iterator referencing start of range to replace.
2263 * @param __i2 Iterator referencing end of range to replace.
2264 * @param __str String value to insert.
2265 * @return Reference to this string.
2266 * @throw std::length_error If new length exceeds @c max_size().
2268 * Removes the characters in the range [__i1,__i2). In place,
2269 * the value of @a __str is inserted. If the length of result
2270 * exceeds max_size(), length_error is thrown. The value of
2271 * the string doesn't change if an error is thrown.
2273 _GLIBCXX20_CONSTEXPR
2275 replace(__const_iterator __i1
, __const_iterator __i2
,
2276 const basic_string
& __str
)
2277 { return this->replace(__i1
, __i2
, __str
._M_data(), __str
.size()); }
2280 * @brief Replace range of characters with C substring.
2281 * @param __i1 Iterator referencing start of range to replace.
2282 * @param __i2 Iterator referencing end of range to replace.
2283 * @param __s C string value to insert.
2284 * @param __n Number of characters from s to insert.
2285 * @return Reference to this string.
2286 * @throw std::length_error If new length exceeds @c max_size().
2288 * Removes the characters in the range [__i1,__i2). In place,
2289 * the first @a __n characters of @a __s are inserted. If the
2290 * length of result exceeds max_size(), length_error is thrown.
2291 * The value of the string doesn't change if an error is
2294 _GLIBCXX20_CONSTEXPR
2296 replace(__const_iterator __i1
, __const_iterator __i2
,
2297 const _CharT
* __s
, size_type __n
)
2299 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2301 return this->replace(__i1
- begin(), __i2
- __i1
, __s
, __n
);
2305 * @brief Replace range of characters with C string.
2306 * @param __i1 Iterator referencing start of range to replace.
2307 * @param __i2 Iterator referencing end of range to replace.
2308 * @param __s C string value to insert.
2309 * @return Reference to this string.
2310 * @throw std::length_error If new length exceeds @c max_size().
2312 * Removes the characters in the range [__i1,__i2). In place,
2313 * the characters of @a __s are inserted. If the length of
2314 * result exceeds max_size(), length_error is thrown. The
2315 * value of the string doesn't change if an error is thrown.
2317 _GLIBCXX20_CONSTEXPR
2319 replace(__const_iterator __i1
, __const_iterator __i2
, const _CharT
* __s
)
2321 __glibcxx_requires_string(__s
);
2322 return this->replace(__i1
, __i2
, __s
, traits_type::length(__s
));
2326 * @brief Replace range of characters with multiple characters
2327 * @param __i1 Iterator referencing start of range to replace.
2328 * @param __i2 Iterator referencing end of range to replace.
2329 * @param __n Number of characters to insert.
2330 * @param __c Character to insert.
2331 * @return Reference to this string.
2332 * @throw std::length_error If new length exceeds @c max_size().
2334 * Removes the characters in the range [__i1,__i2). In place,
2335 * @a __n copies of @a __c are inserted. If the length of
2336 * result exceeds max_size(), length_error is thrown. The
2337 * value of the string doesn't change if an error is thrown.
2339 _GLIBCXX20_CONSTEXPR
2341 replace(__const_iterator __i1
, __const_iterator __i2
, size_type __n
,
2344 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2346 return _M_replace_aux(__i1
- begin(), __i2
- __i1
, __n
, __c
);
2350 * @brief Replace range of characters with range.
2351 * @param __i1 Iterator referencing start of range to replace.
2352 * @param __i2 Iterator referencing end of range to replace.
2353 * @param __k1 Iterator referencing start of range to insert.
2354 * @param __k2 Iterator referencing end of range to insert.
2355 * @return Reference to this string.
2356 * @throw std::length_error If new length exceeds @c max_size().
2358 * Removes the characters in the range [__i1,__i2). In place,
2359 * characters in the range [__k1,__k2) are inserted. If the
2360 * length of result exceeds max_size(), length_error is thrown.
2361 * The value of the string doesn't change if an error is
2364 #if __cplusplus >= 201103L
2365 template<class _InputIterator
,
2366 typename
= std::_RequireInputIter
<_InputIterator
>>
2367 _GLIBCXX20_CONSTEXPR
2369 replace(const_iterator __i1
, const_iterator __i2
,
2370 _InputIterator __k1
, _InputIterator __k2
)
2372 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2374 __glibcxx_requires_valid_range(__k1
, __k2
);
2375 return this->_M_replace_dispatch(__i1
, __i2
, __k1
, __k2
,
2376 std::__false_type());
2379 template<class _InputIterator
>
2380 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
2381 typename __enable_if_not_native_iterator
<_InputIterator
>::__type
2385 replace(iterator __i1
, iterator __i2
,
2386 _InputIterator __k1
, _InputIterator __k2
)
2388 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2390 __glibcxx_requires_valid_range(__k1
, __k2
);
2391 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
2392 return _M_replace_dispatch(__i1
, __i2
, __k1
, __k2
, _Integral());
2396 // Specializations for the common case of pointer and iterator:
2397 // useful to avoid the overhead of temporary buffering in _M_replace.
2398 _GLIBCXX20_CONSTEXPR
2400 replace(__const_iterator __i1
, __const_iterator __i2
,
2401 _CharT
* __k1
, _CharT
* __k2
)
2403 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2405 __glibcxx_requires_valid_range(__k1
, __k2
);
2406 return this->replace(__i1
- begin(), __i2
- __i1
,
2410 _GLIBCXX20_CONSTEXPR
2412 replace(__const_iterator __i1
, __const_iterator __i2
,
2413 const _CharT
* __k1
, const _CharT
* __k2
)
2415 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2417 __glibcxx_requires_valid_range(__k1
, __k2
);
2418 return this->replace(__i1
- begin(), __i2
- __i1
,
2422 _GLIBCXX20_CONSTEXPR
2424 replace(__const_iterator __i1
, __const_iterator __i2
,
2425 iterator __k1
, iterator __k2
)
2427 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2429 __glibcxx_requires_valid_range(__k1
, __k2
);
2430 return this->replace(__i1
- begin(), __i2
- __i1
,
2431 __k1
.base(), __k2
- __k1
);
2434 _GLIBCXX20_CONSTEXPR
2436 replace(__const_iterator __i1
, __const_iterator __i2
,
2437 const_iterator __k1
, const_iterator __k2
)
2439 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2441 __glibcxx_requires_valid_range(__k1
, __k2
);
2442 return this->replace(__i1
- begin(), __i2
- __i1
,
2443 __k1
.base(), __k2
- __k1
);
2446 #if __cplusplus >= 201103L
2448 * @brief Replace range of characters with initializer_list.
2449 * @param __i1 Iterator referencing start of range to replace.
2450 * @param __i2 Iterator referencing end of range to replace.
2451 * @param __l The initializer_list of characters to insert.
2452 * @return Reference to this string.
2453 * @throw std::length_error If new length exceeds @c max_size().
2455 * Removes the characters in the range [__i1,__i2). In place,
2456 * characters in the range [__k1,__k2) are inserted. If the
2457 * length of result exceeds max_size(), length_error is thrown.
2458 * The value of the string doesn't change if an error is
2461 _GLIBCXX20_CONSTEXPR
2462 basic_string
& replace(const_iterator __i1
, const_iterator __i2
,
2463 initializer_list
<_CharT
> __l
)
2464 { return this->replace(__i1
, __i2
, __l
.begin(), __l
.size()); }
2467 #if __cplusplus >= 201703L
2469 * @brief Replace range of characters with string_view.
2470 * @param __pos The position to replace at.
2471 * @param __n The number of characters to replace.
2472 * @param __svt The object convertible to string_view to insert.
2473 * @return Reference to this string.
2475 template<typename _Tp
>
2476 _GLIBCXX20_CONSTEXPR
2477 _If_sv
<_Tp
, basic_string
&>
2478 replace(size_type __pos
, size_type __n
, const _Tp
& __svt
)
2480 __sv_type __sv
= __svt
;
2481 return this->replace(__pos
, __n
, __sv
.data(), __sv
.size());
2485 * @brief Replace range of characters with string_view.
2486 * @param __pos1 The position to replace at.
2487 * @param __n1 The number of characters to replace.
2488 * @param __svt The object convertible to string_view to insert from.
2489 * @param __pos2 The position in the string_view to insert from.
2490 * @param __n2 The number of characters to insert.
2491 * @return Reference to this string.
2493 template<typename _Tp
>
2494 _GLIBCXX20_CONSTEXPR
2495 _If_sv
<_Tp
, basic_string
&>
2496 replace(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
2497 size_type __pos2
, size_type __n2
= npos
)
2499 __sv_type __sv
= __svt
;
2500 return this->replace(__pos1
, __n1
,
2502 + std::__sv_check(__sv
.size(), __pos2
, "basic_string::replace"),
2503 std::__sv_limit(__sv
.size(), __pos2
, __n2
));
2507 * @brief Replace range of characters with string_view.
2508 * @param __i1 An iterator referencing the start position
2510 * @param __i2 An iterator referencing the end position
2512 * @param __svt The object convertible to string_view to insert from.
2513 * @return Reference to this string.
2515 template<typename _Tp
>
2516 _GLIBCXX20_CONSTEXPR
2517 _If_sv
<_Tp
, basic_string
&>
2518 replace(const_iterator __i1
, const_iterator __i2
, const _Tp
& __svt
)
2520 __sv_type __sv
= __svt
;
2521 return this->replace(__i1
- begin(), __i2
- __i1
, __sv
);
2526 template<class _Integer
>
2527 _GLIBCXX20_CONSTEXPR
2529 _M_replace_dispatch(const_iterator __i1
, const_iterator __i2
,
2530 _Integer __n
, _Integer __val
, __true_type
)
2531 { return _M_replace_aux(__i1
- begin(), __i2
- __i1
, __n
, __val
); }
2533 template<class _InputIterator
>
2534 _GLIBCXX20_CONSTEXPR
2536 _M_replace_dispatch(const_iterator __i1
, const_iterator __i2
,
2537 _InputIterator __k1
, _InputIterator __k2
,
2540 _GLIBCXX20_CONSTEXPR
2542 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
2545 __attribute__((__noinline__
, __noclone__
, __cold__
)) void
2546 _M_replace_cold(pointer __p
, size_type __len1
, const _CharT
* __s
,
2547 const size_type __len2
, const size_type __how_much
);
2549 _GLIBCXX20_CONSTEXPR
2551 _M_replace(size_type __pos
, size_type __len1
, const _CharT
* __s
,
2552 const size_type __len2
);
2554 _GLIBCXX20_CONSTEXPR
2556 _M_append(const _CharT
* __s
, size_type __n
);
2561 * @brief Copy substring into C string.
2562 * @param __s C string to copy value into.
2563 * @param __n Number of characters to copy.
2564 * @param __pos Index of first character to copy.
2565 * @return Number of characters actually copied
2566 * @throw std::out_of_range If __pos > size().
2568 * Copies up to @a __n characters starting at @a __pos into the
2569 * C string @a __s. If @a __pos is %greater than size(),
2570 * out_of_range is thrown.
2572 _GLIBCXX20_CONSTEXPR
2574 copy(_CharT
* __s
, size_type __n
, size_type __pos
= 0) const;
2577 * @brief Swap contents with another string.
2578 * @param __s String to swap with.
2580 * Exchanges the contents of this string with that of @a __s in constant
2583 _GLIBCXX20_CONSTEXPR
2585 swap(basic_string
& __s
) _GLIBCXX_NOEXCEPT
;
2587 // String operations:
2589 * @brief Return const pointer to null-terminated contents.
2591 * This is a handle to internal data. Do not modify or dire things may
2594 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2596 c_str() const _GLIBCXX_NOEXCEPT
2597 { return _M_data(); }
2600 * @brief Return const pointer to contents.
2602 * This is a pointer to internal data. It is undefined to modify
2603 * the contents through the returned pointer. To get a pointer that
2604 * allows modifying the contents use @c &str[0] instead,
2605 * (or in C++17 the non-const @c str.data() overload).
2607 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2609 data() const _GLIBCXX_NOEXCEPT
2610 { return _M_data(); }
2612 #if __cplusplus >= 201703L
2614 * @brief Return non-const pointer to contents.
2616 * This is a pointer to the character sequence held by the string.
2617 * Modifying the characters in the sequence is allowed.
2619 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2622 { return _M_data(); }
2626 * @brief Return copy of allocator used to construct this string.
2628 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2630 get_allocator() const _GLIBCXX_NOEXCEPT
2631 { return _M_get_allocator(); }
2634 * @brief Find position of a C substring.
2635 * @param __s C string to locate.
2636 * @param __pos Index of character to search from.
2637 * @param __n Number of characters from @a s to search for.
2638 * @return Index of start of first occurrence.
2640 * Starting from @a __pos, searches forward for the first @a
2641 * __n characters in @a __s within this string. If found,
2642 * returns the index where it begins. If not found, returns
2645 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2647 find(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2651 * @brief Find position of a string.
2652 * @param __str String to locate.
2653 * @param __pos Index of character to search from (default 0).
2654 * @return Index of start of first occurrence.
2656 * Starting from @a __pos, searches forward for value of @a __str within
2657 * this string. If found, returns the index where it begins. If not
2658 * found, returns npos.
2660 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2662 find(const basic_string
& __str
, size_type __pos
= 0) const
2664 { return this->find(__str
.data(), __pos
, __str
.size()); }
2666 #if __cplusplus >= 201703L
2668 * @brief Find position of a string_view.
2669 * @param __svt The object convertible to string_view to locate.
2670 * @param __pos Index of character to search from (default 0).
2671 * @return Index of start of first occurrence.
2673 template<typename _Tp
>
2674 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2675 _If_sv
<_Tp
, size_type
>
2676 find(const _Tp
& __svt
, size_type __pos
= 0) const
2677 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2679 __sv_type __sv
= __svt
;
2680 return this->find(__sv
.data(), __pos
, __sv
.size());
2685 * @brief Find position of a C string.
2686 * @param __s C string to locate.
2687 * @param __pos Index of character to search from (default 0).
2688 * @return Index of start of first occurrence.
2690 * Starting from @a __pos, searches forward for the value of @a
2691 * __s within this string. If found, returns the index where
2692 * it begins. If not found, returns npos.
2694 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2696 find(const _CharT
* __s
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
2698 __glibcxx_requires_string(__s
);
2699 return this->find(__s
, __pos
, traits_type::length(__s
));
2703 * @brief Find position of a character.
2704 * @param __c Character to locate.
2705 * @param __pos Index of character to search from (default 0).
2706 * @return Index of first occurrence.
2708 * Starting from @a __pos, searches forward for @a __c within
2709 * this string. If found, returns the index where it was
2710 * found. If not found, returns npos.
2712 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2714 find(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
;
2717 * @brief Find last position of a string.
2718 * @param __str String to locate.
2719 * @param __pos Index of character to search back from (default end).
2720 * @return Index of start of last occurrence.
2722 * Starting from @a __pos, searches backward for value of @a
2723 * __str within this string. If found, returns the index where
2724 * it begins. If not found, returns npos.
2726 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2728 rfind(const basic_string
& __str
, size_type __pos
= npos
) const
2730 { return this->rfind(__str
.data(), __pos
, __str
.size()); }
2732 #if __cplusplus >= 201703L
2734 * @brief Find last position of a string_view.
2735 * @param __svt The object convertible to string_view to locate.
2736 * @param __pos Index of character to search back from (default end).
2737 * @return Index of start of last occurrence.
2739 template<typename _Tp
>
2740 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2741 _If_sv
<_Tp
, size_type
>
2742 rfind(const _Tp
& __svt
, size_type __pos
= npos
) const
2743 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2745 __sv_type __sv
= __svt
;
2746 return this->rfind(__sv
.data(), __pos
, __sv
.size());
2751 * @brief Find last position of a C substring.
2752 * @param __s C string to locate.
2753 * @param __pos Index of character to search back from.
2754 * @param __n Number of characters from s to search for.
2755 * @return Index of start of last occurrence.
2757 * Starting from @a __pos, searches backward for the first @a
2758 * __n characters in @a __s within this string. If found,
2759 * returns the index where it begins. If not found, returns
2762 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2764 rfind(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2768 * @brief Find last position of a C string.
2769 * @param __s C string to locate.
2770 * @param __pos Index of character to start search at (default end).
2771 * @return Index of start of last occurrence.
2773 * Starting from @a __pos, searches backward for the value of
2774 * @a __s within this string. If found, returns the index
2775 * where it begins. If not found, returns npos.
2777 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2779 rfind(const _CharT
* __s
, size_type __pos
= npos
) const
2781 __glibcxx_requires_string(__s
);
2782 return this->rfind(__s
, __pos
, traits_type::length(__s
));
2786 * @brief Find last position of a character.
2787 * @param __c Character to locate.
2788 * @param __pos Index of character to search back from (default end).
2789 * @return Index of last occurrence.
2791 * Starting from @a __pos, searches backward for @a __c within
2792 * this string. If found, returns the index where it was
2793 * found. If not found, returns npos.
2795 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2797 rfind(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
;
2800 * @brief Find position of a character of string.
2801 * @param __str String containing characters to locate.
2802 * @param __pos Index of character to search from (default 0).
2803 * @return Index of first occurrence.
2805 * Starting from @a __pos, searches forward for one of the
2806 * characters of @a __str within this string. If found,
2807 * returns the index where it was found. If not found, returns
2810 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2812 find_first_of(const basic_string
& __str
, size_type __pos
= 0) const
2814 { return this->find_first_of(__str
.data(), __pos
, __str
.size()); }
2816 #if __cplusplus >= 201703L
2818 * @brief Find position of a character of a string_view.
2819 * @param __svt An object convertible to string_view containing
2820 * characters to locate.
2821 * @param __pos Index of character to search from (default 0).
2822 * @return Index of first occurrence.
2824 template<typename _Tp
>
2825 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2826 _If_sv
<_Tp
, size_type
>
2827 find_first_of(const _Tp
& __svt
, size_type __pos
= 0) const
2828 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2830 __sv_type __sv
= __svt
;
2831 return this->find_first_of(__sv
.data(), __pos
, __sv
.size());
2836 * @brief Find position of a character of C substring.
2837 * @param __s String containing characters to locate.
2838 * @param __pos Index of character to search from.
2839 * @param __n Number of characters from s to search for.
2840 * @return Index of first occurrence.
2842 * Starting from @a __pos, searches forward for one of the
2843 * first @a __n characters of @a __s within this string. If
2844 * found, returns the index where it was found. If not found,
2847 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2849 find_first_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2853 * @brief Find position of a character of C string.
2854 * @param __s String containing characters to locate.
2855 * @param __pos Index of character to search from (default 0).
2856 * @return Index of first occurrence.
2858 * Starting from @a __pos, searches forward for one of the
2859 * characters of @a __s within this string. If found, returns
2860 * the index where it was found. If not found, returns npos.
2862 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2864 find_first_of(const _CharT
* __s
, size_type __pos
= 0) const
2867 __glibcxx_requires_string(__s
);
2868 return this->find_first_of(__s
, __pos
, traits_type::length(__s
));
2872 * @brief Find position of a character.
2873 * @param __c Character to locate.
2874 * @param __pos Index of character to search from (default 0).
2875 * @return Index of first occurrence.
2877 * Starting from @a __pos, searches forward for the character
2878 * @a __c within this string. If found, returns the index
2879 * where it was found. If not found, returns npos.
2881 * Note: equivalent to find(__c, __pos).
2883 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2885 find_first_of(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
2886 { return this->find(__c
, __pos
); }
2889 * @brief Find last position of a character of string.
2890 * @param __str String containing characters to locate.
2891 * @param __pos Index of character to search back from (default end).
2892 * @return Index of last occurrence.
2894 * Starting from @a __pos, searches backward for one of the
2895 * characters of @a __str within this string. If found,
2896 * returns the index where it was found. If not found, returns
2899 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2901 find_last_of(const basic_string
& __str
, size_type __pos
= npos
) const
2903 { return this->find_last_of(__str
.data(), __pos
, __str
.size()); }
2905 #if __cplusplus >= 201703L
2907 * @brief Find last position of a character of string.
2908 * @param __svt An object convertible to string_view containing
2909 * characters to locate.
2910 * @param __pos Index of character to search back from (default end).
2911 * @return Index of last occurrence.
2913 template<typename _Tp
>
2914 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2915 _If_sv
<_Tp
, size_type
>
2916 find_last_of(const _Tp
& __svt
, size_type __pos
= npos
) const
2917 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2919 __sv_type __sv
= __svt
;
2920 return this->find_last_of(__sv
.data(), __pos
, __sv
.size());
2925 * @brief Find last position of a character of C substring.
2926 * @param __s C string containing characters to locate.
2927 * @param __pos Index of character to search back from.
2928 * @param __n Number of characters from s to search for.
2929 * @return Index of last occurrence.
2931 * Starting from @a __pos, searches backward for one of the
2932 * first @a __n characters of @a __s within this string. If
2933 * found, returns the index where it was found. If not found,
2936 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2938 find_last_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2942 * @brief Find last position of a character of C string.
2943 * @param __s C string containing characters to locate.
2944 * @param __pos Index of character to search back from (default end).
2945 * @return Index of last occurrence.
2947 * Starting from @a __pos, searches backward for one of the
2948 * characters of @a __s within this string. If found, returns
2949 * the index where it was found. If not found, returns npos.
2951 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2953 find_last_of(const _CharT
* __s
, size_type __pos
= npos
) const
2956 __glibcxx_requires_string(__s
);
2957 return this->find_last_of(__s
, __pos
, traits_type::length(__s
));
2961 * @brief Find last position of a character.
2962 * @param __c Character to locate.
2963 * @param __pos Index of character to search back from (default end).
2964 * @return Index of last occurrence.
2966 * Starting from @a __pos, searches backward for @a __c within
2967 * this string. If found, returns the index where it was
2968 * found. If not found, returns npos.
2970 * Note: equivalent to rfind(__c, __pos).
2972 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2974 find_last_of(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
2975 { return this->rfind(__c
, __pos
); }
2978 * @brief Find position of a character not in string.
2979 * @param __str String containing characters to avoid.
2980 * @param __pos Index of character to search from (default 0).
2981 * @return Index of first occurrence.
2983 * Starting from @a __pos, searches forward for a character not contained
2984 * in @a __str within this string. If found, returns the index where it
2985 * was found. If not found, returns npos.
2987 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2989 find_first_not_of(const basic_string
& __str
, size_type __pos
= 0) const
2991 { return this->find_first_not_of(__str
.data(), __pos
, __str
.size()); }
2993 #if __cplusplus >= 201703L
2995 * @brief Find position of a character not in a string_view.
2996 * @param __svt A object convertible to string_view containing
2997 * characters to avoid.
2998 * @param __pos Index of character to search from (default 0).
2999 * @return Index of first occurrence.
3001 template<typename _Tp
>
3002 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3003 _If_sv
<_Tp
, size_type
>
3004 find_first_not_of(const _Tp
& __svt
, size_type __pos
= 0) const
3005 noexcept(is_same
<_Tp
, __sv_type
>::value
)
3007 __sv_type __sv
= __svt
;
3008 return this->find_first_not_of(__sv
.data(), __pos
, __sv
.size());
3013 * @brief Find position of a character not in C substring.
3014 * @param __s C string containing characters to avoid.
3015 * @param __pos Index of character to search from.
3016 * @param __n Number of characters from __s to consider.
3017 * @return Index of first occurrence.
3019 * Starting from @a __pos, searches forward for a character not
3020 * contained in the first @a __n characters of @a __s within
3021 * this string. If found, returns the index where it was
3022 * found. If not found, returns npos.
3024 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3026 find_first_not_of(const _CharT
* __s
, size_type __pos
,
3027 size_type __n
) const _GLIBCXX_NOEXCEPT
;
3030 * @brief Find position of a character not in C string.
3031 * @param __s C string containing characters to avoid.
3032 * @param __pos Index of character to search from (default 0).
3033 * @return Index of first occurrence.
3035 * Starting from @a __pos, searches forward for a character not
3036 * contained in @a __s within this string. If found, returns
3037 * the index where it was found. If not found, returns npos.
3039 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3041 find_first_not_of(const _CharT
* __s
, size_type __pos
= 0) const
3044 __glibcxx_requires_string(__s
);
3045 return this->find_first_not_of(__s
, __pos
, traits_type::length(__s
));
3049 * @brief Find position of a different character.
3050 * @param __c Character to avoid.
3051 * @param __pos Index of character to search from (default 0).
3052 * @return Index of first occurrence.
3054 * Starting from @a __pos, searches forward for a character
3055 * other than @a __c within this string. If found, returns the
3056 * index where it was found. If not found, returns npos.
3058 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3060 find_first_not_of(_CharT __c
, size_type __pos
= 0) const
3064 * @brief Find last position of a character not in string.
3065 * @param __str String containing characters to avoid.
3066 * @param __pos Index of character to search back from (default end).
3067 * @return Index of last occurrence.
3069 * Starting from @a __pos, searches backward for a character
3070 * not contained in @a __str within this string. If found,
3071 * returns the index where it was found. If not found, returns
3074 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3076 find_last_not_of(const basic_string
& __str
, size_type __pos
= npos
) const
3078 { return this->find_last_not_of(__str
.data(), __pos
, __str
.size()); }
3080 #if __cplusplus >= 201703L
3082 * @brief Find last position of a character not in a string_view.
3083 * @param __svt An object convertible to string_view containing
3084 * characters to avoid.
3085 * @param __pos Index of character to search back from (default end).
3086 * @return Index of last occurrence.
3088 template<typename _Tp
>
3089 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3090 _If_sv
<_Tp
, size_type
>
3091 find_last_not_of(const _Tp
& __svt
, size_type __pos
= npos
) const
3092 noexcept(is_same
<_Tp
, __sv_type
>::value
)
3094 __sv_type __sv
= __svt
;
3095 return this->find_last_not_of(__sv
.data(), __pos
, __sv
.size());
3100 * @brief Find last position of a character not in C substring.
3101 * @param __s C string containing characters to avoid.
3102 * @param __pos Index of character to search back from.
3103 * @param __n Number of characters from s to consider.
3104 * @return Index of last occurrence.
3106 * Starting from @a __pos, searches backward for a character not
3107 * contained in the first @a __n characters of @a __s within this string.
3108 * If found, returns the index where it was found. If not found,
3111 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3113 find_last_not_of(const _CharT
* __s
, size_type __pos
,
3114 size_type __n
) const _GLIBCXX_NOEXCEPT
;
3116 * @brief Find last position of a character not in C string.
3117 * @param __s C string containing characters to avoid.
3118 * @param __pos Index of character to search back from (default end).
3119 * @return Index of last occurrence.
3121 * Starting from @a __pos, searches backward for a character
3122 * not contained in @a __s within this string. If found,
3123 * returns the index where it was found. If not found, returns
3126 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3128 find_last_not_of(const _CharT
* __s
, size_type __pos
= npos
) const
3131 __glibcxx_requires_string(__s
);
3132 return this->find_last_not_of(__s
, __pos
, traits_type::length(__s
));
3136 * @brief Find last position of a different character.
3137 * @param __c Character to avoid.
3138 * @param __pos Index of character to search back from (default end).
3139 * @return Index of last occurrence.
3141 * Starting from @a __pos, searches backward for a character other than
3142 * @a __c within this string. If found, returns the index where it was
3143 * found. If not found, returns npos.
3145 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3147 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const
3151 * @brief Get a substring.
3152 * @param __pos Index of first character (default 0).
3153 * @param __n Number of characters in substring (default remainder).
3154 * @return The new string.
3155 * @throw std::out_of_range If __pos > size().
3157 * Construct and return a new string using the @a __n
3158 * characters starting at @a __pos. If the string is too
3159 * short, use the remainder of the characters. If @a __pos is
3160 * beyond the end of the string, out_of_range is thrown.
3162 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3164 substr(size_type __pos
= 0, size_type __n
= npos
) const
3165 { return basic_string(*this,
3166 _M_check(__pos
, "basic_string::substr"), __n
); }
3169 * @brief Compare to a string.
3170 * @param __str String to compare against.
3171 * @return Integer < 0, 0, or > 0.
3173 * Returns an integer < 0 if this string is ordered before @a
3174 * __str, 0 if their values are equivalent, or > 0 if this
3175 * string is ordered after @a __str. Determines the effective
3176 * length rlen of the strings to compare as the smallest of
3177 * size() and str.size(). The function then compares the two
3178 * strings by calling traits::compare(data(), str.data(),rlen).
3179 * If the result of the comparison is nonzero returns it,
3180 * otherwise the shorter one is ordered first.
3182 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3184 compare(const basic_string
& __str
) const
3186 const size_type __size
= this->size();
3187 const size_type __osize
= __str
.size();
3188 const size_type __len
= std::min(__size
, __osize
);
3190 int __r
= traits_type::compare(_M_data(), __str
.data(), __len
);
3192 __r
= _S_compare(__size
, __osize
);
3196 #if __cplusplus >= 201703L
3198 * @brief Compare to a string_view.
3199 * @param __svt An object convertible to string_view to compare against.
3200 * @return Integer < 0, 0, or > 0.
3202 template<typename _Tp
>
3203 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3205 compare(const _Tp
& __svt
) const
3206 noexcept(is_same
<_Tp
, __sv_type
>::value
)
3208 __sv_type __sv
= __svt
;
3209 const size_type __size
= this->size();
3210 const size_type __osize
= __sv
.size();
3211 const size_type __len
= std::min(__size
, __osize
);
3213 int __r
= traits_type::compare(_M_data(), __sv
.data(), __len
);
3215 __r
= _S_compare(__size
, __osize
);
3220 * @brief Compare to a string_view.
3221 * @param __pos A position in the string to start comparing from.
3222 * @param __n The number of characters to compare.
3223 * @param __svt An object convertible to string_view to compare
3225 * @return Integer < 0, 0, or > 0.
3227 template<typename _Tp
>
3228 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3230 compare(size_type __pos
, size_type __n
, const _Tp
& __svt
) const
3231 noexcept(is_same
<_Tp
, __sv_type
>::value
)
3233 __sv_type __sv
= __svt
;
3234 return __sv_type(*this).substr(__pos
, __n
).compare(__sv
);
3238 * @brief Compare to a string_view.
3239 * @param __pos1 A position in the string to start comparing from.
3240 * @param __n1 The number of characters to compare.
3241 * @param __svt An object convertible to string_view to compare
3243 * @param __pos2 A position in the string_view to start comparing from.
3244 * @param __n2 The number of characters to compare.
3245 * @return Integer < 0, 0, or > 0.
3247 template<typename _Tp
>
3248 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3250 compare(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
3251 size_type __pos2
, size_type __n2
= npos
) const
3252 noexcept(is_same
<_Tp
, __sv_type
>::value
)
3254 __sv_type __sv
= __svt
;
3255 return __sv_type(*this)
3256 .substr(__pos1
, __n1
).compare(__sv
.substr(__pos2
, __n2
));
3261 * @brief Compare substring to a string.
3262 * @param __pos Index of first character of substring.
3263 * @param __n Number of characters in substring.
3264 * @param __str String to compare against.
3265 * @return Integer < 0, 0, or > 0.
3267 * Form the substring of this string from the @a __n characters
3268 * starting at @a __pos. Returns an integer < 0 if the
3269 * substring is ordered before @a __str, 0 if their values are
3270 * equivalent, or > 0 if the substring is ordered after @a
3271 * __str. Determines the effective length rlen of the strings
3272 * to compare as the smallest of the length of the substring
3273 * and @a __str.size(). The function then compares the two
3274 * strings by calling
3275 * traits::compare(substring.data(),str.data(),rlen). If the
3276 * result of the comparison is nonzero returns it, otherwise
3277 * the shorter one is ordered first.
3279 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3281 compare(size_type __pos
, size_type __n
, const basic_string
& __str
) const
3283 _M_check(__pos
, "basic_string::compare");
3284 __n
= _M_limit(__pos
, __n
);
3285 const size_type __osize
= __str
.size();
3286 const size_type __len
= std::min(__n
, __osize
);
3287 int __r
= traits_type::compare(_M_data() + __pos
, __str
.data(), __len
);
3289 __r
= _S_compare(__n
, __osize
);
3294 * @brief Compare substring to a substring.
3295 * @param __pos1 Index of first character of substring.
3296 * @param __n1 Number of characters in substring.
3297 * @param __str String to compare against.
3298 * @param __pos2 Index of first character of substring of str.
3299 * @param __n2 Number of characters in substring of str.
3300 * @return Integer < 0, 0, or > 0.
3302 * Form the substring of this string from the @a __n1
3303 * characters starting at @a __pos1. Form the substring of @a
3304 * __str from the @a __n2 characters starting at @a __pos2.
3305 * Returns an integer < 0 if this substring is ordered before
3306 * the substring of @a __str, 0 if their values are equivalent,
3307 * or > 0 if this substring is ordered after the substring of
3308 * @a __str. Determines the effective length rlen of the
3309 * strings to compare as the smallest of the lengths of the
3310 * substrings. The function then compares the two strings by
3312 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
3313 * If the result of the comparison is nonzero returns it,
3314 * otherwise the shorter one is ordered first.
3316 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3318 compare(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
3319 size_type __pos2
, size_type __n2
= npos
) const
3321 _M_check(__pos1
, "basic_string::compare");
3322 __str
._M_check(__pos2
, "basic_string::compare");
3323 __n1
= _M_limit(__pos1
, __n1
);
3324 __n2
= __str
._M_limit(__pos2
, __n2
);
3325 const size_type __len
= std::min(__n1
, __n2
);
3326 int __r
= traits_type::compare(_M_data() + __pos1
,
3327 __str
.data() + __pos2
, __len
);
3329 __r
= _S_compare(__n1
, __n2
);
3334 * @brief Compare to a C string.
3335 * @param __s C string to compare against.
3336 * @return Integer < 0, 0, or > 0.
3338 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
3339 * their values are equivalent, or > 0 if this string is ordered after
3340 * @a __s. Determines the effective length rlen of the strings to
3341 * compare as the smallest of size() and the length of a string
3342 * constructed from @a __s. The function then compares the two strings
3343 * by calling traits::compare(data(),s,rlen). If the result of the
3344 * comparison is nonzero returns it, otherwise the shorter one is
3347 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3349 compare(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
3351 __glibcxx_requires_string(__s
);
3352 const size_type __size
= this->size();
3353 const size_type __osize
= traits_type::length(__s
);
3354 const size_type __len
= std::min(__size
, __osize
);
3355 int __r
= traits_type::compare(_M_data(), __s
, __len
);
3357 __r
= _S_compare(__size
, __osize
);
3361 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3362 // 5 String::compare specification questionable
3364 * @brief Compare substring to a C string.
3365 * @param __pos Index of first character of substring.
3366 * @param __n1 Number of characters in substring.
3367 * @param __s C string to compare against.
3368 * @return Integer < 0, 0, or > 0.
3370 * Form the substring of this string from the @a __n1
3371 * characters starting at @a pos. Returns an integer < 0 if
3372 * the substring is ordered before @a __s, 0 if their values
3373 * are equivalent, or > 0 if the substring is ordered after @a
3374 * __s. Determines the effective length rlen of the strings to
3375 * compare as the smallest of the length of the substring and
3376 * the length of a string constructed from @a __s. The
3377 * function then compares the two string by calling
3378 * traits::compare(substring.data(),__s,rlen). If the result of
3379 * the comparison is nonzero returns it, otherwise the shorter
3380 * one is ordered first.
3382 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3384 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
) const
3386 __glibcxx_requires_string(__s
);
3387 _M_check(__pos
, "basic_string::compare");
3388 __n1
= _M_limit(__pos
, __n1
);
3389 const size_type __osize
= traits_type::length(__s
);
3390 const size_type __len
= std::min(__n1
, __osize
);
3391 int __r
= traits_type::compare(_M_data() + __pos
, __s
, __len
);
3393 __r
= _S_compare(__n1
, __osize
);
3398 * @brief Compare substring against a character %array.
3399 * @param __pos Index of first character of substring.
3400 * @param __n1 Number of characters in substring.
3401 * @param __s character %array to compare against.
3402 * @param __n2 Number of characters of s.
3403 * @return Integer < 0, 0, or > 0.
3405 * Form the substring of this string from the @a __n1
3406 * characters starting at @a __pos. Form a string from the
3407 * first @a __n2 characters of @a __s. Returns an integer < 0
3408 * if this substring is ordered before the string from @a __s,
3409 * 0 if their values are equivalent, or > 0 if this substring
3410 * is ordered after the string from @a __s. Determines the
3411 * effective length rlen of the strings to compare as the
3412 * smallest of the length of the substring and @a __n2. The
3413 * function then compares the two strings by calling
3414 * traits::compare(substring.data(),s,rlen). If the result of
3415 * the comparison is nonzero returns it, otherwise the shorter
3416 * one is ordered first.
3418 * NB: s must have at least n2 characters, '\\0' has
3419 * no special meaning.
3421 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3423 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
,
3424 size_type __n2
) const
3426 __glibcxx_requires_string_len(__s
, __n2
);
3427 _M_check(__pos
, "basic_string::compare");
3428 __n1
= _M_limit(__pos
, __n1
);
3429 const size_type __len
= std::min(__n1
, __n2
);
3430 int __r
= traits_type::compare(_M_data() + __pos
, __s
, __len
);
3432 __r
= _S_compare(__n1
, __n2
);
3436 #if __cplusplus >= 202002L
3439 starts_with(basic_string_view
<_CharT
, _Traits
> __x
) const noexcept
3440 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
3444 starts_with(_CharT __x
) const noexcept
3445 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
3447 [[nodiscard
, __gnu__::__nonnull__
]]
3449 starts_with(const _CharT
* __x
) const noexcept
3450 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
3454 ends_with(basic_string_view
<_CharT
, _Traits
> __x
) const noexcept
3455 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
3459 ends_with(_CharT __x
) const noexcept
3460 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
3462 [[nodiscard
, __gnu__::__nonnull__
]]
3464 ends_with(const _CharT
* __x
) const noexcept
3465 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
3468 #if __cplusplus > 202002L
3471 contains(basic_string_view
<_CharT
, _Traits
> __x
) const noexcept
3472 { return __sv_type(this->data(), this->size()).contains(__x
); }
3476 contains(_CharT __x
) const noexcept
3477 { return __sv_type(this->data(), this->size()).contains(__x
); }
3479 [[nodiscard
, __gnu__::__nonnull__
]]
3481 contains(const _CharT
* __x
) const noexcept
3482 { return __sv_type(this->data(), this->size()).contains(__x
); }
3485 // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
3486 template<typename
, typename
, typename
> friend class basic_stringbuf
;
3488 _GLIBCXX_END_NAMESPACE_CXX11
3489 _GLIBCXX_END_NAMESPACE_VERSION
3491 #endif // _GLIBCXX_USE_CXX11_ABI
3493 namespace std
_GLIBCXX_VISIBILITY(default)
3495 _GLIBCXX_BEGIN_NAMESPACE_VERSION
3497 #if __cpp_deduction_guides >= 201606
3498 _GLIBCXX_BEGIN_NAMESPACE_CXX11
3499 template<typename _InputIterator
, typename _CharT
3500 = typename iterator_traits
<_InputIterator
>::value_type
,
3501 typename _Allocator
= allocator
<_CharT
>,
3502 typename
= _RequireInputIter
<_InputIterator
>,
3503 typename
= _RequireAllocator
<_Allocator
>>
3504 basic_string(_InputIterator
, _InputIterator
, _Allocator
= _Allocator())
3505 -> basic_string
<_CharT
, char_traits
<_CharT
>, _Allocator
>;
3507 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3508 // 3075. basic_string needs deduction guides from basic_string_view
3509 template<typename _CharT
, typename _Traits
,
3510 typename _Allocator
= allocator
<_CharT
>,
3511 typename
= _RequireAllocator
<_Allocator
>>
3512 basic_string(basic_string_view
<_CharT
, _Traits
>, const _Allocator
& = _Allocator())
3513 -> basic_string
<_CharT
, _Traits
, _Allocator
>;
3515 template<typename _CharT
, typename _Traits
,
3516 typename _Allocator
= allocator
<_CharT
>,
3517 typename
= _RequireAllocator
<_Allocator
>>
3518 basic_string(basic_string_view
<_CharT
, _Traits
>,
3519 typename basic_string
<_CharT
, _Traits
, _Allocator
>::size_type
,
3520 typename basic_string
<_CharT
, _Traits
, _Allocator
>::size_type
,
3521 const _Allocator
& = _Allocator())
3522 -> basic_string
<_CharT
, _Traits
, _Allocator
>;
3523 _GLIBCXX_END_NAMESPACE_CXX11
3526 template<typename _Str
>
3527 _GLIBCXX20_CONSTEXPR
3529 __str_concat(typename
_Str::value_type
const* __lhs
,
3530 typename
_Str::size_type __lhs_len
,
3531 typename
_Str::value_type
const* __rhs
,
3532 typename
_Str::size_type __rhs_len
,
3533 typename
_Str::allocator_type
const& __a
)
3535 typedef typename
_Str::allocator_type allocator_type
;
3536 typedef __gnu_cxx::__alloc_traits
<allocator_type
> _Alloc_traits
;
3537 _Str
__str(_Alloc_traits::_S_select_on_copy(__a
));
3538 __str
.reserve(__lhs_len
+ __rhs_len
);
3539 __str
.append(__lhs
, __lhs_len
);
3540 __str
.append(__rhs
, __rhs_len
);
3546 * @brief Concatenate two strings.
3547 * @param __lhs First string.
3548 * @param __rhs Last string.
3549 * @return New string with value of @a __lhs followed by @a __rhs.
3551 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3552 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3553 inline basic_string
<_CharT
, _Traits
, _Alloc
>
3554 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3555 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3557 typedef basic_string
<_CharT
, _Traits
, _Alloc
> _Str
;
3558 return std::__str_concat
<_Str
>(__lhs
.c_str(), __lhs
.size(),
3559 __rhs
.c_str(), __rhs
.size(),
3560 __lhs
.get_allocator());
3564 * @brief Concatenate C string and string.
3565 * @param __lhs First string.
3566 * @param __rhs Last string.
3567 * @return New string with value of @a __lhs followed by @a __rhs.
3569 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3570 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3571 inline basic_string
<_CharT
,_Traits
,_Alloc
>
3572 operator+(const _CharT
* __lhs
,
3573 const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
)
3575 __glibcxx_requires_string(__lhs
);
3576 typedef basic_string
<_CharT
, _Traits
, _Alloc
> _Str
;
3577 return std::__str_concat
<_Str
>(__lhs
, _Traits::length(__lhs
),
3578 __rhs
.c_str(), __rhs
.size(),
3579 __rhs
.get_allocator());
3583 * @brief Concatenate character and string.
3584 * @param __lhs First string.
3585 * @param __rhs Last string.
3586 * @return New string with @a __lhs followed by @a __rhs.
3588 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3589 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3590 inline basic_string
<_CharT
,_Traits
,_Alloc
>
3591 operator+(_CharT __lhs
, const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
)
3593 typedef basic_string
<_CharT
, _Traits
, _Alloc
> _Str
;
3594 return std::__str_concat
<_Str
>(__builtin_addressof(__lhs
), 1,
3595 __rhs
.c_str(), __rhs
.size(),
3596 __rhs
.get_allocator());
3600 * @brief Concatenate string and C string.
3601 * @param __lhs First string.
3602 * @param __rhs Last string.
3603 * @return New string with @a __lhs followed by @a __rhs.
3605 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3606 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3607 inline basic_string
<_CharT
, _Traits
, _Alloc
>
3608 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3609 const _CharT
* __rhs
)
3611 __glibcxx_requires_string(__rhs
);
3612 typedef basic_string
<_CharT
, _Traits
, _Alloc
> _Str
;
3613 return std::__str_concat
<_Str
>(__lhs
.c_str(), __lhs
.size(),
3614 __rhs
, _Traits::length(__rhs
),
3615 __lhs
.get_allocator());
3618 * @brief Concatenate string and character.
3619 * @param __lhs First string.
3620 * @param __rhs Last string.
3621 * @return New string with @a __lhs followed by @a __rhs.
3623 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3624 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3625 inline basic_string
<_CharT
, _Traits
, _Alloc
>
3626 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
, _CharT __rhs
)
3628 typedef basic_string
<_CharT
, _Traits
, _Alloc
> _Str
;
3629 return std::__str_concat
<_Str
>(__lhs
.c_str(), __lhs
.size(),
3630 __builtin_addressof(__rhs
), 1,
3631 __lhs
.get_allocator());
3634 #if __cplusplus >= 201103L
3635 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3636 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3637 inline basic_string
<_CharT
, _Traits
, _Alloc
>
3638 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
3639 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3640 { return std::move(__lhs
.append(__rhs
)); }
3642 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3643 _GLIBCXX20_CONSTEXPR
3644 inline basic_string
<_CharT
, _Traits
, _Alloc
>
3645 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3646 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
3647 { return std::move(__rhs
.insert(0, __lhs
)); }
3649 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3650 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3651 inline basic_string
<_CharT
, _Traits
, _Alloc
>
3652 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
3653 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
3655 #if _GLIBCXX_USE_CXX11_ABI
3656 using _Alloc_traits
= allocator_traits
<_Alloc
>;
3657 bool __use_rhs
= false;
3658 if _GLIBCXX17_CONSTEXPR (typename
_Alloc_traits::is_always_equal
{})
3660 else if (__lhs
.get_allocator() == __rhs
.get_allocator())
3665 const auto __size
= __lhs
.size() + __rhs
.size();
3666 if (__size
> __lhs
.capacity() && __size
<= __rhs
.capacity())
3667 return std::move(__rhs
.insert(0, __lhs
));
3669 return std::move(__lhs
.append(__rhs
));
3672 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3673 _GLIBCXX_NODISCARD _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3674 inline basic_string
<_CharT
, _Traits
, _Alloc
>
3675 operator+(const _CharT
* __lhs
,
3676 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
3677 { return std::move(__rhs
.insert(0, __lhs
)); }
3679 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3680 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3681 inline basic_string
<_CharT
, _Traits
, _Alloc
>
3682 operator+(_CharT __lhs
,
3683 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
3684 { return std::move(__rhs
.insert(0, 1, __lhs
)); }
3686 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3687 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3688 inline basic_string
<_CharT
, _Traits
, _Alloc
>
3689 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
3690 const _CharT
* __rhs
)
3691 { return std::move(__lhs
.append(__rhs
)); }
3693 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3694 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3695 inline basic_string
<_CharT
, _Traits
, _Alloc
>
3696 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
3698 { return std::move(__lhs
.append(1, __rhs
)); }
3703 * @brief Test equivalence of two strings.
3704 * @param __lhs First string.
3705 * @param __rhs Second string.
3706 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
3708 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3709 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3711 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3712 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3715 return __lhs
.size() == __rhs
.size()
3716 && !_Traits::compare(__lhs
.data(), __rhs
.data(), __lhs
.size());
3720 * @brief Test equivalence of string and C string.
3721 * @param __lhs String.
3722 * @param __rhs C string.
3723 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
3725 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3726 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3728 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3729 const _CharT
* __rhs
)
3731 return __lhs
.size() == _Traits::length(__rhs
)
3732 && !_Traits::compare(__lhs
.data(), __rhs
, __lhs
.size());
3735 #if __cpp_lib_three_way_comparison
3737 * @brief Three-way comparison of a string and a C string.
3738 * @param __lhs A string.
3739 * @param __rhs A null-terminated string.
3740 * @return A value indicating whether `__lhs` is less than, equal to,
3741 * greater than, or incomparable with `__rhs`.
3743 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3746 operator<=>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3747 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
) noexcept
3748 -> decltype(__detail::__char_traits_cmp_cat
<_Traits
>(0))
3749 { return __detail::__char_traits_cmp_cat
<_Traits
>(__lhs
.compare(__rhs
)); }
3752 * @brief Three-way comparison of a string and a C string.
3753 * @param __lhs A string.
3754 * @param __rhs A null-terminated string.
3755 * @return A value indicating whether `__lhs` is less than, equal to,
3756 * greater than, or incomparable with `__rhs`.
3758 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3761 operator<=>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3762 const _CharT
* __rhs
) noexcept
3763 -> decltype(__detail::__char_traits_cmp_cat
<_Traits
>(0))
3764 { return __detail::__char_traits_cmp_cat
<_Traits
>(__lhs
.compare(__rhs
)); }
3767 * @brief Test equivalence of C string and string.
3768 * @param __lhs C string.
3769 * @param __rhs String.
3770 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
3772 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3775 operator==(const _CharT
* __lhs
,
3776 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3777 { return __rhs
== __lhs
; }
3781 * @brief Test difference of two strings.
3782 * @param __lhs First string.
3783 * @param __rhs Second string.
3784 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
3786 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3789 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3790 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3792 { return !(__lhs
== __rhs
); }
3795 * @brief Test difference of C string and string.
3796 * @param __lhs C string.
3797 * @param __rhs String.
3798 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
3800 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3803 operator!=(const _CharT
* __lhs
,
3804 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3805 { return !(__rhs
== __lhs
); }
3808 * @brief Test difference of string and C string.
3809 * @param __lhs String.
3810 * @param __rhs C string.
3811 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
3813 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3816 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3817 const _CharT
* __rhs
)
3818 { return !(__lhs
== __rhs
); }
3822 * @brief Test if string precedes string.
3823 * @param __lhs First string.
3824 * @param __rhs Second string.
3825 * @return True if @a __lhs precedes @a __rhs. False otherwise.
3827 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3830 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3831 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3833 { return __lhs
.compare(__rhs
) < 0; }
3836 * @brief Test if string precedes C string.
3837 * @param __lhs String.
3838 * @param __rhs C string.
3839 * @return True if @a __lhs precedes @a __rhs. False otherwise.
3841 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3844 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3845 const _CharT
* __rhs
)
3846 { return __lhs
.compare(__rhs
) < 0; }
3849 * @brief Test if C string precedes string.
3850 * @param __lhs C string.
3851 * @param __rhs String.
3852 * @return True if @a __lhs precedes @a __rhs. False otherwise.
3854 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3857 operator<(const _CharT
* __lhs
,
3858 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3859 { return __rhs
.compare(__lhs
) > 0; }
3863 * @brief Test if string follows string.
3864 * @param __lhs First string.
3865 * @param __rhs Second string.
3866 * @return True if @a __lhs follows @a __rhs. False otherwise.
3868 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3871 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3872 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3874 { return __lhs
.compare(__rhs
) > 0; }
3877 * @brief Test if string follows C string.
3878 * @param __lhs String.
3879 * @param __rhs C string.
3880 * @return True if @a __lhs follows @a __rhs. False otherwise.
3882 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3885 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3886 const _CharT
* __rhs
)
3887 { return __lhs
.compare(__rhs
) > 0; }
3890 * @brief Test if C string follows string.
3891 * @param __lhs C string.
3892 * @param __rhs String.
3893 * @return True if @a __lhs follows @a __rhs. False otherwise.
3895 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3898 operator>(const _CharT
* __lhs
,
3899 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3900 { return __rhs
.compare(__lhs
) < 0; }
3904 * @brief Test if string doesn't follow string.
3905 * @param __lhs First string.
3906 * @param __rhs Second string.
3907 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
3909 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3912 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3913 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3915 { return __lhs
.compare(__rhs
) <= 0; }
3918 * @brief Test if string doesn't follow C string.
3919 * @param __lhs String.
3920 * @param __rhs C string.
3921 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
3923 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3926 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3927 const _CharT
* __rhs
)
3928 { return __lhs
.compare(__rhs
) <= 0; }
3931 * @brief Test if C string doesn't follow string.
3932 * @param __lhs C string.
3933 * @param __rhs String.
3934 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
3936 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3939 operator<=(const _CharT
* __lhs
,
3940 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3941 { return __rhs
.compare(__lhs
) >= 0; }
3945 * @brief Test if string doesn't precede string.
3946 * @param __lhs First string.
3947 * @param __rhs Second string.
3948 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
3950 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3953 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3954 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3956 { return __lhs
.compare(__rhs
) >= 0; }
3959 * @brief Test if string doesn't precede C string.
3960 * @param __lhs String.
3961 * @param __rhs C string.
3962 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
3964 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3967 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3968 const _CharT
* __rhs
)
3969 { return __lhs
.compare(__rhs
) >= 0; }
3972 * @brief Test if C string doesn't precede string.
3973 * @param __lhs C string.
3974 * @param __rhs String.
3975 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
3977 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3980 operator>=(const _CharT
* __lhs
,
3981 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3982 { return __rhs
.compare(__lhs
) <= 0; }
3983 #endif // three-way comparison
3986 * @brief Swap contents of two strings.
3987 * @param __lhs First string.
3988 * @param __rhs Second string.
3990 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
3992 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3993 _GLIBCXX20_CONSTEXPR
3995 swap(basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3996 basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3997 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs
.swap(__rhs
)))
3998 { __lhs
.swap(__rhs
); }
4002 * @brief Read stream into a string.
4003 * @param __is Input stream.
4004 * @param __str Buffer to store into.
4005 * @return Reference to the input stream.
4007 * Stores characters from @a __is into @a __str until whitespace is
4008 * found, the end of the stream is encountered, or str.max_size()
4009 * is reached. If is.width() is non-zero, that is the limit on the
4010 * number of characters stored into @a __str. Any previous
4011 * contents of @a __str are erased.
4013 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4014 basic_istream
<_CharT
, _Traits
>&
4015 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
4016 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
);
4019 basic_istream
<char>&
4020 operator>>(basic_istream
<char>& __is
, basic_string
<char>& __str
);
4023 * @brief Write string to a stream.
4024 * @param __os Output stream.
4025 * @param __str String to write out.
4026 * @return Reference to the output stream.
4028 * Output characters of @a __str into os following the same rules as for
4029 * writing a C string.
4031 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4032 inline basic_ostream
<_CharT
, _Traits
>&
4033 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
4034 const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
4036 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4037 // 586. string inserter not a formatted function
4038 return __ostream_insert(__os
, __str
.data(), __str
.size());
4042 * @brief Read a line from stream into a string.
4043 * @param __is Input stream.
4044 * @param __str Buffer to store into.
4045 * @param __delim Character marking end of line.
4046 * @return Reference to the input stream.
4048 * Stores characters from @a __is into @a __str until @a __delim is
4049 * found, the end of the stream is encountered, or str.max_size()
4050 * is reached. Any previous contents of @a __str are erased. If
4051 * @a __delim is encountered, it is extracted but not stored into
4054 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4055 basic_istream
<_CharT
, _Traits
>&
4056 getline(basic_istream
<_CharT
, _Traits
>& __is
,
4057 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
);
4060 * @brief Read a line from stream into a string.
4061 * @param __is Input stream.
4062 * @param __str Buffer to store into.
4063 * @return Reference to the input stream.
4065 * Stores characters from is into @a __str until '\n' is
4066 * found, the end of the stream is encountered, or str.max_size()
4067 * is reached. Any previous contents of @a __str are erased. If
4068 * end of line is encountered, it is extracted but not stored into
4071 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4072 inline basic_istream
<_CharT
, _Traits
>&
4073 getline(basic_istream
<_CharT
, _Traits
>& __is
,
4074 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
4075 { return std::getline(__is
, __str
, __is
.widen('\n')); }
4077 #if __cplusplus >= 201103L
4078 /// Read a line from an rvalue stream into a string.
4079 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4080 inline basic_istream
<_CharT
, _Traits
>&
4081 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
4082 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
)
4083 { return std::getline(__is
, __str
, __delim
); }
4085 /// Read a line from an rvalue stream into a string.
4086 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4087 inline basic_istream
<_CharT
, _Traits
>&
4088 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
4089 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
4090 { return std::getline(__is
, __str
); }
4094 basic_istream
<char>&
4095 getline(basic_istream
<char>& __in
, basic_string
<char>& __str
,
4098 #ifdef _GLIBCXX_USE_WCHAR_T
4100 basic_istream
<wchar_t>&
4101 getline(basic_istream
<wchar_t>& __in
, basic_string
<wchar_t>& __str
,
4105 _GLIBCXX_END_NAMESPACE_VERSION
4108 #if __cplusplus >= 201103L
4110 #include <ext/string_conversions.h>
4111 #include <bits/charconv.h>
4113 namespace std
_GLIBCXX_VISIBILITY(default)
4115 _GLIBCXX_BEGIN_NAMESPACE_VERSION
4116 _GLIBCXX_BEGIN_NAMESPACE_CXX11
4118 #if _GLIBCXX_USE_C99_STDLIB
4119 // 21.4 Numeric Conversions [string.conversions].
4121 stoi(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
4122 { return __gnu_cxx::__stoa
<long, int>(&std::strtol
, "stoi", __str
.c_str(),
4126 stol(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
4127 { return __gnu_cxx::__stoa(&std::strtol
, "stol", __str
.c_str(),
4130 inline unsigned long
4131 stoul(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
4132 { return __gnu_cxx::__stoa(&std::strtoul
, "stoul", __str
.c_str(),
4136 stoll(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
4137 { return __gnu_cxx::__stoa(&std::strtoll
, "stoll", __str
.c_str(),
4140 inline unsigned long long
4141 stoull(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
4142 { return __gnu_cxx::__stoa(&std::strtoull
, "stoull", __str
.c_str(),
4145 // NB: strtof vs strtod.
4147 stof(const string
& __str
, size_t* __idx
= 0)
4148 { return __gnu_cxx::__stoa(&std::strtof
, "stof", __str
.c_str(), __idx
); }
4151 stod(const string
& __str
, size_t* __idx
= 0)
4152 { return __gnu_cxx::__stoa(&std::strtod
, "stod", __str
.c_str(), __idx
); }
4155 stold(const string
& __str
, size_t* __idx
= 0)
4156 { return __gnu_cxx::__stoa(&std::strtold
, "stold", __str
.c_str(), __idx
); }
4157 #endif // _GLIBCXX_USE_C99_STDLIB
4159 // DR 1261. Insufficent overloads for to_string / to_wstring
4163 to_string(int __val
)
4164 #if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32
4165 noexcept
// any 32-bit value fits in the SSO buffer
4168 const bool __neg
= __val
< 0;
4169 const unsigned __uval
= __neg
? (unsigned)~__val
+ 1u : __val
;
4170 const auto __len
= __detail::__to_chars_len(__uval
);
4171 string
__str(__neg
+ __len
, '-');
4172 __detail::__to_chars_10_impl(&__str
[__neg
], __len
, __uval
);
4178 to_string(unsigned __val
)
4179 #if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32
4180 noexcept
// any 32-bit value fits in the SSO buffer
4183 string
__str(__detail::__to_chars_len(__val
), '\0');
4184 __detail::__to_chars_10_impl(&__str
[0], __str
.size(), __val
);
4190 to_string(long __val
)
4191 #if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32
4192 noexcept
// any 32-bit value fits in the SSO buffer
4195 const bool __neg
= __val
< 0;
4196 const unsigned long __uval
= __neg
? (unsigned long)~__val
+ 1ul : __val
;
4197 const auto __len
= __detail::__to_chars_len(__uval
);
4198 string
__str(__neg
+ __len
, '-');
4199 __detail::__to_chars_10_impl(&__str
[__neg
], __len
, __uval
);
4205 to_string(unsigned long __val
)
4206 #if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32
4207 noexcept
// any 32-bit value fits in the SSO buffer
4210 string
__str(__detail::__to_chars_len(__val
), '\0');
4211 __detail::__to_chars_10_impl(&__str
[0], __str
.size(), __val
);
4217 to_string(long long __val
)
4219 const bool __neg
= __val
< 0;
4220 const unsigned long long __uval
4221 = __neg
? (unsigned long long)~__val
+ 1ull : __val
;
4222 const auto __len
= __detail::__to_chars_len(__uval
);
4223 string
__str(__neg
+ __len
, '-');
4224 __detail::__to_chars_10_impl(&__str
[__neg
], __len
, __uval
);
4230 to_string(unsigned long long __val
)
4232 string
__str(__detail::__to_chars_len(__val
), '\0');
4233 __detail::__to_chars_10_impl(&__str
[0], __str
.size(), __val
);
4237 #if _GLIBCXX_USE_C99_STDIO
4238 // NB: (v)snprintf vs sprintf.
4242 to_string(float __val
)
4245 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
4246 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
4252 to_string(double __val
)
4255 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
4256 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
4262 to_string(long double __val
)
4265 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
4266 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
4269 #endif // _GLIBCXX_USE_C99_STDIO
4271 #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
4273 stoi(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
4274 { return __gnu_cxx::__stoa
<long, int>(&std::wcstol
, "stoi", __str
.c_str(),
4278 stol(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
4279 { return __gnu_cxx::__stoa(&std::wcstol
, "stol", __str
.c_str(),
4282 inline unsigned long
4283 stoul(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
4284 { return __gnu_cxx::__stoa(&std::wcstoul
, "stoul", __str
.c_str(),
4288 stoll(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
4289 { return __gnu_cxx::__stoa(&std::wcstoll
, "stoll", __str
.c_str(),
4292 inline unsigned long long
4293 stoull(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
4294 { return __gnu_cxx::__stoa(&std::wcstoull
, "stoull", __str
.c_str(),
4297 // NB: wcstof vs wcstod.
4299 stof(const wstring
& __str
, size_t* __idx
= 0)
4300 { return __gnu_cxx::__stoa(&std::wcstof
, "stof", __str
.c_str(), __idx
); }
4303 stod(const wstring
& __str
, size_t* __idx
= 0)
4304 { return __gnu_cxx::__stoa(&std::wcstod
, "stod", __str
.c_str(), __idx
); }
4307 stold(const wstring
& __str
, size_t* __idx
= 0)
4308 { return __gnu_cxx::__stoa(&std::wcstold
, "stold", __str
.c_str(), __idx
); }
4310 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
4314 to_wstring(int __val
)
4315 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(int),
4320 to_wstring(unsigned __val
)
4321 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
4322 4 * sizeof(unsigned),
4327 to_wstring(long __val
)
4328 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(long),
4333 to_wstring(unsigned long __val
)
4334 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
4335 4 * sizeof(unsigned long),
4340 to_wstring(long long __val
)
4341 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
4342 4 * sizeof(long long),
4347 to_wstring(unsigned long long __val
)
4348 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
4349 4 * sizeof(unsigned long long),
4354 to_wstring(float __val
)
4357 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
4358 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
4364 to_wstring(double __val
)
4367 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
4368 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
4374 to_wstring(long double __val
)
4377 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
4378 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
4381 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
4382 #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
4384 _GLIBCXX_END_NAMESPACE_CXX11
4385 _GLIBCXX_END_NAMESPACE_VERSION
4390 #if __cplusplus >= 201103L
4392 #include <bits/functional_hash.h>
4394 namespace std
_GLIBCXX_VISIBILITY(default)
4396 _GLIBCXX_BEGIN_NAMESPACE_VERSION
4398 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4399 // 3705. Hashability shouldn't depend on basic_string's allocator
4401 template<typename _CharT
, typename _Alloc
,
4402 typename _StrT
= basic_string
<_CharT
, char_traits
<_CharT
>, _Alloc
>>
4403 struct __str_hash_base
4404 : public __hash_base
<size_t, _StrT
>
4408 operator()(const _StrT
& __s
) const noexcept
4409 { return _Hash_impl::hash(__s
.data(), __s
.length() * sizeof(_CharT
)); }
4412 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
4413 /// std::hash specialization for string.
4414 template<typename _Alloc
>
4415 struct hash
<basic_string
<char, char_traits
<char>, _Alloc
>>
4416 : public __str_hash_base
<char, _Alloc
>
4419 /// std::hash specialization for wstring.
4420 template<typename _Alloc
>
4421 struct hash
<basic_string
<wchar_t, char_traits
<wchar_t>, _Alloc
>>
4422 : public __str_hash_base
<wchar_t, _Alloc
>
4425 template<typename _Alloc
>
4426 struct __is_fast_hash
<hash
<basic_string
<wchar_t, char_traits
<wchar_t>,
4430 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
4432 #ifdef _GLIBCXX_USE_CHAR8_T
4433 /// std::hash specialization for u8string.
4434 template<typename _Alloc
>
4435 struct hash
<basic_string
<char8_t
, char_traits
<char8_t
>, _Alloc
>>
4436 : public __str_hash_base
<char8_t
, _Alloc
>
4440 /// std::hash specialization for u16string.
4441 template<typename _Alloc
>
4442 struct hash
<basic_string
<char16_t
, char_traits
<char16_t
>, _Alloc
>>
4443 : public __str_hash_base
<char16_t
, _Alloc
>
4446 /// std::hash specialization for u32string.
4447 template<typename _Alloc
>
4448 struct hash
<basic_string
<char32_t
, char_traits
<char32_t
>, _Alloc
>>
4449 : public __str_hash_base
<char32_t
, _Alloc
>
4452 #if ! _GLIBCXX_INLINE_VERSION
4453 // PR libstdc++/105907 - __is_fast_hash affects unordered container ABI.
4454 template<> struct __is_fast_hash
<hash
<string
>> : std::false_type
{ };
4455 template<> struct __is_fast_hash
<hash
<wstring
>> : std::false_type
{ };
4456 template<> struct __is_fast_hash
<hash
<u16string
>> : std::false_type
{ };
4457 template<> struct __is_fast_hash
<hash
<u32string
>> : std::false_type
{ };
4458 #ifdef _GLIBCXX_USE_CHAR8_T
4459 template<> struct __is_fast_hash
<hash
<u8string
>> : std::false_type
{ };
4462 // For versioned namespace, assume every std::hash<basic_string<>> is slow.
4463 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4464 struct __is_fast_hash
<hash
<basic_string
<_CharT
, _Traits
, _Alloc
>>>
4469 #if __cplusplus >= 201402L
4471 #define __cpp_lib_string_udls 201304L
4473 inline namespace literals
4475 inline namespace string_literals
4477 #pragma GCC diagnostic push
4478 #pragma GCC diagnostic ignored "-Wliteral-suffix"
4480 #if __cpp_lib_constexpr_string >= 201907L
4481 # define _GLIBCXX_STRING_CONSTEXPR constexpr
4483 # define _GLIBCXX_STRING_CONSTEXPR
4486 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
4487 inline basic_string
<char>
4488 operator""s(const char* __str
, size_t __len
)
4489 { return basic_string
<char>{__str
, __len
}; }
4491 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
4492 inline basic_string
<wchar_t>
4493 operator""s(const wchar_t* __str
, size_t __len
)
4494 { return basic_string
<wchar_t>{__str
, __len
}; }
4496 #ifdef _GLIBCXX_USE_CHAR8_T
4497 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
4498 inline basic_string
<char8_t
>
4499 operator""s(const char8_t
* __str
, size_t __len
)
4500 { return basic_string
<char8_t
>{__str
, __len
}; }
4503 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
4504 inline basic_string
<char16_t
>
4505 operator""s(const char16_t
* __str
, size_t __len
)
4506 { return basic_string
<char16_t
>{__str
, __len
}; }
4508 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
4509 inline basic_string
<char32_t
>
4510 operator""s(const char32_t
* __str
, size_t __len
)
4511 { return basic_string
<char32_t
>{__str
, __len
}; }
4513 #undef _GLIBCXX_STRING_CONSTEXPR
4514 #pragma GCC diagnostic pop
4515 } // inline namespace string_literals
4516 } // inline namespace literals
4518 #if __cplusplus >= 201703L
4519 namespace __detail::__variant
4521 template<typename
> struct _Never_valueless_alt
; // see <variant>
4523 // Provide the strong exception-safety guarantee when emplacing a
4524 // basic_string into a variant, but only if moving the string cannot throw.
4525 template<typename _Tp
, typename _Traits
, typename _Alloc
>
4526 struct _Never_valueless_alt
<std::basic_string
<_Tp
, _Traits
, _Alloc
>>
4528 is_nothrow_move_constructible
<std::basic_string
<_Tp
, _Traits
, _Alloc
>>,
4529 is_nothrow_move_assignable
<std::basic_string
<_Tp
, _Traits
, _Alloc
>>
4532 } // namespace __detail::__variant
4536 _GLIBCXX_END_NAMESPACE_VERSION
4541 #endif /* _BASIC_STRING_H */