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.
73 * @tparam _CharT Type of character
74 * @tparam _Traits Traits for character type, defaults to
75 * char_traits<_CharT>.
76 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
78 * Meets the requirements of a <a href="tables.html#65">container</a>, a
79 * <a href="tables.html#66">reversible container</a>, and a
80 * <a href="tables.html#67">sequence</a>. Of the
81 * <a href="tables.html#68">optional sequence requirements</a>, only
82 * @c push_back, @c at, and @c %array access are supported.
84 template<typename _CharT
, typename _Traits
, typename _Alloc
>
87 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
88 rebind
<_CharT
>::other _Char_alloc_type
;
90 #if __cpp_lib_constexpr_string < 201907L
91 typedef __gnu_cxx::__alloc_traits
<_Char_alloc_type
> _Alloc_traits
;
93 template<typename _Traits2
, typename _Dummy_for_PR85282
>
94 struct _Alloc_traits_impl
: __gnu_cxx::__alloc_traits
<_Char_alloc_type
>
96 typedef __gnu_cxx::__alloc_traits
<_Char_alloc_type
> _Base
;
98 [[__gnu__::__always_inline__
]]
99 static constexpr typename
_Base::pointer
100 allocate(_Char_alloc_type
& __a
, typename
_Base::size_type __n
)
102 pointer __p
= _Base::allocate(__a
, __n
);
103 if (std::is_constant_evaluated())
104 // Begin the lifetime of characters in allocated storage.
105 for (size_type __i
= 0; __i
< __n
; ++__i
)
106 std::construct_at(__builtin_addressof(__p
[__i
]));
111 template<typename _Dummy_for_PR85282
>
112 struct _Alloc_traits_impl
<char_traits
<_CharT
>, _Dummy_for_PR85282
>
113 : __gnu_cxx::__alloc_traits
<_Char_alloc_type
>
115 // std::char_traits begins the lifetime of characters.
118 using _Alloc_traits
= _Alloc_traits_impl
<_Traits
, void>;
123 typedef _Traits traits_type
;
124 typedef typename
_Traits::char_type value_type
;
125 typedef _Char_alloc_type allocator_type
;
126 typedef typename
_Alloc_traits::size_type size_type
;
127 typedef typename
_Alloc_traits::difference_type difference_type
;
128 typedef typename
_Alloc_traits::reference reference
;
129 typedef typename
_Alloc_traits::const_reference const_reference
;
130 typedef typename
_Alloc_traits::pointer pointer
;
131 typedef typename
_Alloc_traits::const_pointer const_pointer
;
132 typedef __gnu_cxx::__normal_iterator
<pointer
, basic_string
> iterator
;
133 typedef __gnu_cxx::__normal_iterator
<const_pointer
, basic_string
>
135 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
136 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
138 /// Value returned by various member functions when they fail.
139 static const size_type npos
= static_cast<size_type
>(-1);
142 // type used for positions in insert, erase etc.
143 #if __cplusplus < 201103L
144 typedef iterator __const_iterator
;
146 typedef const_iterator __const_iterator
;
150 #if __cplusplus >= 201703L
151 // A helper type for avoiding boiler-plate.
152 typedef basic_string_view
<_CharT
, _Traits
> __sv_type
;
154 template<typename _Tp
, typename _Res
>
155 using _If_sv
= enable_if_t
<
156 __and_
<is_convertible
<const _Tp
&, __sv_type
>,
157 __not_
<is_convertible
<const _Tp
*, const basic_string
*>>,
158 __not_
<is_convertible
<const _Tp
&, const _CharT
*>>>::value
,
161 // Allows an implicit conversion to __sv_type.
164 _S_to_string_view(__sv_type __svt
) noexcept
167 // Wraps a string_view by explicit conversion and thus
168 // allows to add an internal constructor that does not
169 // participate in overload resolution when a string_view
173 _GLIBCXX20_CONSTEXPR
explicit
174 __sv_wrapper(__sv_type __sv
) noexcept
: _M_sv(__sv
) { }
180 * @brief Only internally used: Construct string from a string view
182 * @param __svw string view wrapper.
183 * @param __a Allocator to use.
187 basic_string(__sv_wrapper __svw
, const _Alloc
& __a
)
188 : basic_string(__svw
._M_sv
.data(), __svw
._M_sv
.size(), __a
) { }
191 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
192 struct _Alloc_hider
: allocator_type
// TODO check __is_final
194 #if __cplusplus < 201103L
195 _Alloc_hider(pointer __dat
, const _Alloc
& __a
= _Alloc())
196 : allocator_type(__a
), _M_p(__dat
) { }
199 _Alloc_hider(pointer __dat
, const _Alloc
& __a
)
200 : allocator_type(__a
), _M_p(__dat
) { }
203 _Alloc_hider(pointer __dat
, _Alloc
&& __a
= _Alloc())
204 : allocator_type(std::move(__a
)), _M_p(__dat
) { }
207 pointer _M_p
; // The actual data.
210 _Alloc_hider _M_dataplus
;
211 size_type _M_string_length
;
213 enum { _S_local_capacity
= 15 / sizeof(_CharT
) };
217 _CharT _M_local_buf
[_S_local_capacity
+ 1];
218 size_type _M_allocated_capacity
;
224 { _M_dataplus
._M_p
= __p
; }
228 _M_length(size_type __length
)
229 { _M_string_length
= __length
; }
234 { return _M_dataplus
._M_p
; }
240 #if __cplusplus >= 201103L
241 return std::pointer_traits
<pointer
>::pointer_to(*_M_local_buf
);
243 return pointer(_M_local_buf
);
249 _M_local_data() const
251 #if __cplusplus >= 201103L
252 return std::pointer_traits
<const_pointer
>::pointer_to(*_M_local_buf
);
254 return const_pointer(_M_local_buf
);
260 _M_capacity(size_type __capacity
)
261 { _M_allocated_capacity
= __capacity
; }
265 _M_set_length(size_type __n
)
268 traits_type::assign(_M_data()[__n
], _CharT());
274 { return _M_data() == _M_local_data(); }
279 _M_create(size_type
&, size_type
);
286 _M_destroy(_M_allocated_capacity
);
291 _M_destroy(size_type __size
) throw()
292 { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size
+ 1); }
294 #if __cplusplus < 201103L || defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
295 // _M_construct_aux is used to implement the 21.3.1 para 15 which
296 // requires special behaviour if _InIterator is an integral type
297 template<typename _InIterator
>
299 _M_construct_aux(_InIterator __beg
, _InIterator __end
,
302 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
303 _M_construct(__beg
, __end
, _Tag());
306 // _GLIBCXX_RESOLVE_LIB_DEFECTS
307 // 438. Ambiguity in the "do the right thing" clause
308 template<typename _Integer
>
310 _M_construct_aux(_Integer __beg
, _Integer __end
, std::__true_type
)
311 { _M_construct_aux_2(static_cast<size_type
>(__beg
), __end
); }
314 _M_construct_aux_2(size_type __req
, _CharT __c
)
315 { _M_construct(__req
, __c
); }
318 // For Input Iterators, used in istreambuf_iterators, etc.
319 template<typename _InIterator
>
322 _M_construct(_InIterator __beg
, _InIterator __end
,
323 std::input_iterator_tag
);
325 // For forward_iterators up to random_access_iterators, used for
326 // string::iterator, _CharT*, etc.
327 template<typename _FwdIterator
>
330 _M_construct(_FwdIterator __beg
, _FwdIterator __end
,
331 std::forward_iterator_tag
);
335 _M_construct(size_type __req
, _CharT __c
);
340 { return _M_dataplus
; }
343 const allocator_type
&
344 _M_get_allocator() const
345 { return _M_dataplus
; }
347 // Ensure that _M_local_buf is the active member of the union.
348 __attribute__((__always_inline__
))
351 _M_use_local_data() _GLIBCXX_NOEXCEPT
353 #if __cpp_lib_is_constant_evaluated
354 if (std::is_constant_evaluated())
355 for (size_type __i
= 0; __i
<= _S_local_capacity
; ++__i
)
356 _M_local_buf
[__i
] = _CharT();
358 return _M_local_data();
363 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
364 // The explicit instantiations in misc-inst.cc require this due to
365 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
366 template<typename _Tp
, bool _Requires
=
367 !__are_same
<_Tp
, _CharT
*>::__value
368 && !__are_same
<_Tp
, const _CharT
*>::__value
369 && !__are_same
<_Tp
, iterator
>::__value
370 && !__are_same
<_Tp
, const_iterator
>::__value
>
371 struct __enable_if_not_native_iterator
372 { typedef basic_string
& __type
; };
373 template<typename _Tp
>
374 struct __enable_if_not_native_iterator
<_Tp
, false> { };
379 _M_check(size_type __pos
, const char* __s
) const
381 if (__pos
> this->size())
382 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
383 "this->size() (which is %zu)"),
384 __s
, __pos
, this->size());
390 _M_check_length(size_type __n1
, size_type __n2
, const char* __s
) const
392 if (this->max_size() - (this->size() - __n1
) < __n2
)
393 __throw_length_error(__N(__s
));
397 // NB: _M_limit doesn't check for a bad __pos value.
400 _M_limit(size_type __pos
, size_type __off
) const _GLIBCXX_NOEXCEPT
402 const bool __testoff
= __off
< this->size() - __pos
;
403 return __testoff
? __off
: this->size() - __pos
;
406 // True if _Rep and source do not overlap.
408 _M_disjunct(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
410 return (less
<const _CharT
*>()(__s
, _M_data())
411 || less
<const _CharT
*>()(_M_data() + this->size(), __s
));
414 // When __n = 1 way faster than the general multichar
415 // traits_type::copy/move/assign.
418 _S_copy(_CharT
* __d
, const _CharT
* __s
, size_type __n
)
421 traits_type::assign(*__d
, *__s
);
423 traits_type::copy(__d
, __s
, __n
);
428 _S_move(_CharT
* __d
, const _CharT
* __s
, size_type __n
)
431 traits_type::assign(*__d
, *__s
);
433 traits_type::move(__d
, __s
, __n
);
438 _S_assign(_CharT
* __d
, size_type __n
, _CharT __c
)
441 traits_type::assign(*__d
, __c
);
443 traits_type::assign(__d
, __n
, __c
);
446 // _S_copy_chars is a separate template to permit specialization
447 // to optimize for the common case of pointers as iterators.
448 template<class _Iterator
>
451 _S_copy_chars(_CharT
* __p
, _Iterator __k1
, _Iterator __k2
)
453 for (; __k1
!= __k2
; ++__k1
, (void)++__p
)
454 traits_type::assign(*__p
, *__k1
); // These types are off.
459 _S_copy_chars(_CharT
* __p
, iterator __k1
, iterator __k2
) _GLIBCXX_NOEXCEPT
460 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
464 _S_copy_chars(_CharT
* __p
, const_iterator __k1
, const_iterator __k2
)
466 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
470 _S_copy_chars(_CharT
* __p
, _CharT
* __k1
, _CharT
* __k2
) _GLIBCXX_NOEXCEPT
471 { _S_copy(__p
, __k1
, __k2
- __k1
); }
475 _S_copy_chars(_CharT
* __p
, const _CharT
* __k1
, const _CharT
* __k2
)
477 { _S_copy(__p
, __k1
, __k2
- __k1
); }
481 _S_compare(size_type __n1
, size_type __n2
) _GLIBCXX_NOEXCEPT
483 const difference_type __d
= difference_type(__n1
- __n2
);
485 if (__d
> __gnu_cxx::__numeric_traits
<int>::__max
)
486 return __gnu_cxx::__numeric_traits
<int>::__max
;
487 else if (__d
< __gnu_cxx::__numeric_traits
<int>::__min
)
488 return __gnu_cxx::__numeric_traits
<int>::__min
;
495 _M_assign(const basic_string
&);
499 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
504 _M_erase(size_type __pos
, size_type __n
);
507 // Construct/copy/destroy:
508 // NB: We overload ctors in some cases instead of using default
509 // arguments, per 17.4.4.4 para. 2 item 2.
512 * @brief Default constructor creates an empty string.
516 _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible
<_Alloc
>::value
)
517 : _M_dataplus(_M_local_data())
524 * @brief Construct an empty string using allocator @a a.
528 basic_string(const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
529 : _M_dataplus(_M_local_data(), __a
)
536 * @brief Construct string with copy of value of @a __str.
537 * @param __str Source string.
540 basic_string(const basic_string
& __str
)
541 : _M_dataplus(_M_local_data(),
542 _Alloc_traits::_S_select_on_copy(__str
._M_get_allocator()))
544 _M_construct(__str
._M_data(), __str
._M_data() + __str
.length(),
545 std::forward_iterator_tag());
548 // _GLIBCXX_RESOLVE_LIB_DEFECTS
549 // 2583. no way to supply an allocator for basic_string(str, pos)
551 * @brief Construct string as copy of a substring.
552 * @param __str Source string.
553 * @param __pos Index of first character to copy from.
554 * @param __a Allocator to use.
557 basic_string(const basic_string
& __str
, size_type __pos
,
558 const _Alloc
& __a
= _Alloc())
559 : _M_dataplus(_M_local_data(), __a
)
561 const _CharT
* __start
= __str
._M_data()
562 + __str
._M_check(__pos
, "basic_string::basic_string");
563 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, npos
),
564 std::forward_iterator_tag());
568 * @brief Construct string as copy of a substring.
569 * @param __str Source string.
570 * @param __pos Index of first character to copy from.
571 * @param __n Number of characters to copy.
574 basic_string(const basic_string
& __str
, size_type __pos
,
576 : _M_dataplus(_M_local_data())
578 const _CharT
* __start
= __str
._M_data()
579 + __str
._M_check(__pos
, "basic_string::basic_string");
580 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, __n
),
581 std::forward_iterator_tag());
585 * @brief Construct string as copy of a substring.
586 * @param __str Source string.
587 * @param __pos Index of first character to copy from.
588 * @param __n Number of characters to copy.
589 * @param __a Allocator to use.
592 basic_string(const basic_string
& __str
, size_type __pos
,
593 size_type __n
, const _Alloc
& __a
)
594 : _M_dataplus(_M_local_data(), __a
)
596 const _CharT
* __start
597 = __str
._M_data() + __str
._M_check(__pos
, "string::string");
598 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, __n
),
599 std::forward_iterator_tag());
603 * @brief Construct string initialized by a character %array.
604 * @param __s Source character %array.
605 * @param __n Number of characters to copy.
606 * @param __a Allocator to use (default is default allocator).
608 * NB: @a __s must have at least @a __n characters, '\\0'
609 * has no special meaning.
612 basic_string(const _CharT
* __s
, size_type __n
,
613 const _Alloc
& __a
= _Alloc())
614 : _M_dataplus(_M_local_data(), __a
)
616 // NB: Not required, but considered best practice.
617 if (__s
== 0 && __n
> 0)
618 std::__throw_logic_error(__N("basic_string: "
619 "construction from null is not valid"));
620 _M_construct(__s
, __s
+ __n
, std::forward_iterator_tag());
624 * @brief Construct string as copy of a C string.
625 * @param __s Source C string.
626 * @param __a Allocator to use (default is default allocator).
628 #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
629 // _GLIBCXX_RESOLVE_LIB_DEFECTS
630 // 3076. basic_string CTAD ambiguity
631 template<typename
= _RequireAllocator
<_Alloc
>>
634 basic_string(const _CharT
* __s
, const _Alloc
& __a
= _Alloc())
635 : _M_dataplus(_M_local_data(), __a
)
637 // NB: Not required, but considered best practice.
639 std::__throw_logic_error(__N("basic_string: "
640 "construction from null is not valid"));
641 const _CharT
* __end
= __s
+ traits_type::length(__s
);
642 _M_construct(__s
, __end
, forward_iterator_tag());
646 * @brief Construct string as multiple characters.
647 * @param __n Number of characters.
648 * @param __c Character to use.
649 * @param __a Allocator to use (default is default allocator).
651 #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
652 // _GLIBCXX_RESOLVE_LIB_DEFECTS
653 // 3076. basic_string CTAD ambiguity
654 template<typename
= _RequireAllocator
<_Alloc
>>
657 basic_string(size_type __n
, _CharT __c
, const _Alloc
& __a
= _Alloc())
658 : _M_dataplus(_M_local_data(), __a
)
659 { _M_construct(__n
, __c
); }
661 #if __cplusplus >= 201103L
663 * @brief Move construct string.
664 * @param __str Source string.
666 * The newly-created string contains the exact contents of @a __str.
667 * @a __str is a valid, but unspecified string.
670 basic_string(basic_string
&& __str
) noexcept
671 : _M_dataplus(_M_local_data(), std::move(__str
._M_get_allocator()))
673 if (__str
._M_is_local())
675 traits_type::copy(_M_local_buf
, __str
._M_local_buf
,
680 _M_data(__str
._M_data());
681 _M_capacity(__str
._M_allocated_capacity
);
684 // Must use _M_length() here not _M_set_length() because
685 // basic_stringbuf relies on writing into unallocated capacity so
686 // we mess up the contents if we put a '\0' in the string.
687 _M_length(__str
.length());
688 __str
._M_data(__str
._M_local_data());
689 __str
._M_set_length(0);
693 * @brief Construct string from an initializer %list.
694 * @param __l std::initializer_list of characters.
695 * @param __a Allocator to use (default is default allocator).
698 basic_string(initializer_list
<_CharT
> __l
, const _Alloc
& __a
= _Alloc())
699 : _M_dataplus(_M_local_data(), __a
)
700 { _M_construct(__l
.begin(), __l
.end(), std::forward_iterator_tag()); }
703 basic_string(const basic_string
& __str
, const _Alloc
& __a
)
704 : _M_dataplus(_M_local_data(), __a
)
705 { _M_construct(__str
.begin(), __str
.end(), std::forward_iterator_tag()); }
708 basic_string(basic_string
&& __str
, const _Alloc
& __a
)
709 noexcept(_Alloc_traits::_S_always_equal())
710 : _M_dataplus(_M_local_data(), __a
)
712 if (__str
._M_is_local())
714 traits_type::copy(_M_local_buf
, __str
._M_local_buf
,
716 _M_length(__str
.length());
717 __str
._M_set_length(0);
719 else if (_Alloc_traits::_S_always_equal()
720 || __str
.get_allocator() == __a
)
722 _M_data(__str
._M_data());
723 _M_length(__str
.length());
724 _M_capacity(__str
._M_allocated_capacity
);
725 __str
._M_data(__str
._M_local_buf
);
726 __str
._M_set_length(0);
729 _M_construct(__str
.begin(), __str
.end(), std::forward_iterator_tag());
733 #if __cplusplus >= 202100L
734 basic_string(nullptr_t
) = delete;
735 basic_string
& operator=(nullptr_t
) = delete;
739 * @brief Construct string as copy of a range.
740 * @param __beg Start of range.
741 * @param __end End of range.
742 * @param __a Allocator to use (default is default allocator).
744 #if __cplusplus >= 201103L
745 template<typename _InputIterator
,
746 typename
= std::_RequireInputIter
<_InputIterator
>>
748 template<typename _InputIterator
>
751 basic_string(_InputIterator __beg
, _InputIterator __end
,
752 const _Alloc
& __a
= _Alloc())
753 : _M_dataplus(_M_local_data(), __a
)
755 #if __cplusplus >= 201103L
756 _M_construct(__beg
, __end
, std::__iterator_category(__beg
));
758 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
759 _M_construct_aux(__beg
, __end
, _Integral());
763 #if __cplusplus >= 201703L
765 * @brief Construct string from a substring of a string_view.
766 * @param __t Source object convertible to string view.
767 * @param __pos The index of the first character to copy from __t.
768 * @param __n The number of characters to copy from __t.
769 * @param __a Allocator to use.
771 template<typename _Tp
,
772 typename
= enable_if_t
<is_convertible_v
<const _Tp
&, __sv_type
>>>
774 basic_string(const _Tp
& __t
, size_type __pos
, size_type __n
,
775 const _Alloc
& __a
= _Alloc())
776 : basic_string(_S_to_string_view(__t
).substr(__pos
, __n
), __a
) { }
779 * @brief Construct string from a string_view.
780 * @param __t Source object convertible to string view.
781 * @param __a Allocator to use (default is default allocator).
783 template<typename _Tp
, typename
= _If_sv
<_Tp
, void>>
786 basic_string(const _Tp
& __t
, const _Alloc
& __a
= _Alloc())
787 : basic_string(__sv_wrapper(_S_to_string_view(__t
)), __a
) { }
791 * @brief Destroy the string instance.
798 * @brief Assign the value of @a str to this string.
799 * @param __str Source string.
803 operator=(const basic_string
& __str
)
805 return this->assign(__str
);
809 * @brief Copy contents of @a s into this string.
810 * @param __s Source null-terminated string.
814 operator=(const _CharT
* __s
)
815 { return this->assign(__s
); }
818 * @brief Set value to string of length 1.
819 * @param __c Source character.
821 * Assigning to a character makes this string length 1 and
822 * (*this)[0] == @a c.
826 operator=(_CharT __c
)
828 this->assign(1, __c
);
832 #if __cplusplus >= 201103L
834 * @brief Move assign the value of @a str to this string.
835 * @param __str Source string.
837 * The contents of @a str are moved into this string (without copying).
838 * @a str is a valid, but unspecified string.
840 // _GLIBCXX_RESOLVE_LIB_DEFECTS
841 // 2063. Contradictory requirements for string move assignment
844 operator=(basic_string
&& __str
)
845 noexcept(_Alloc_traits::_S_nothrow_move())
847 if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
848 && !_Alloc_traits::_S_always_equal()
849 && _M_get_allocator() != __str
._M_get_allocator())
851 // Destroy existing storage before replacing allocator.
852 _M_destroy(_M_allocated_capacity
);
853 _M_data(_M_local_data());
856 // Replace allocator if POCMA is true.
857 std::__alloc_on_move(_M_get_allocator(), __str
._M_get_allocator());
859 if (__str
._M_is_local())
861 // We've always got room for a short string, just copy it
862 // (unless this is a self-move, because that would violate the
863 // char_traits::copy precondition that the ranges don't overlap).
864 if (__builtin_expect(std::__addressof(__str
) != this, true))
867 this->_S_copy(_M_data(), __str
._M_data(), __str
.size());
868 _M_set_length(__str
.size());
871 else if (_Alloc_traits::_S_propagate_on_move_assign()
872 || _Alloc_traits::_S_always_equal()
873 || _M_get_allocator() == __str
._M_get_allocator())
875 // Just move the allocated pointer, our allocator can free it.
876 pointer __data
= nullptr;
877 size_type __capacity
;
880 if (_Alloc_traits::_S_always_equal())
882 // __str can reuse our existing storage.
884 __capacity
= _M_allocated_capacity
;
886 else // __str can't use it, so free it.
887 _M_destroy(_M_allocated_capacity
);
890 _M_data(__str
._M_data());
891 _M_length(__str
.length());
892 _M_capacity(__str
._M_allocated_capacity
);
895 __str
._M_data(__data
);
896 __str
._M_capacity(__capacity
);
899 __str
._M_data(__str
._M_local_buf
);
901 else // Need to do a deep copy
908 * @brief Set value to string constructed from initializer %list.
909 * @param __l std::initializer_list.
913 operator=(initializer_list
<_CharT
> __l
)
915 this->assign(__l
.begin(), __l
.size());
920 #if __cplusplus >= 201703L
922 * @brief Set value to string constructed from a string_view.
923 * @param __svt An object convertible to string_view.
925 template<typename _Tp
>
927 _If_sv
<_Tp
, basic_string
&>
928 operator=(const _Tp
& __svt
)
929 { return this->assign(__svt
); }
932 * @brief Convert to a string_view.
933 * @return A string_view.
936 operator __sv_type() const noexcept
937 { return __sv_type(data(), size()); }
942 * Returns a read/write iterator that points to the first character in
945 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
947 begin() _GLIBCXX_NOEXCEPT
948 { return iterator(_M_data()); }
951 * Returns a read-only (constant) iterator that points to the first
952 * character in the %string.
954 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
956 begin() const _GLIBCXX_NOEXCEPT
957 { return const_iterator(_M_data()); }
960 * Returns a read/write iterator that points one past the last
961 * character in the %string.
963 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
965 end() _GLIBCXX_NOEXCEPT
966 { return iterator(_M_data() + this->size()); }
969 * Returns a read-only (constant) iterator that points one past the
970 * last character in the %string.
972 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
974 end() const _GLIBCXX_NOEXCEPT
975 { return const_iterator(_M_data() + this->size()); }
978 * Returns a read/write reverse iterator that points to the last
979 * character in the %string. Iteration is done in reverse element
982 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
984 rbegin() _GLIBCXX_NOEXCEPT
985 { return reverse_iterator(this->end()); }
988 * Returns a read-only (constant) reverse iterator that points
989 * to the last character in the %string. Iteration is done in
990 * reverse element order.
992 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
993 const_reverse_iterator
994 rbegin() const _GLIBCXX_NOEXCEPT
995 { return const_reverse_iterator(this->end()); }
998 * Returns a read/write reverse iterator that points to one before the
999 * first character in the %string. Iteration is done in reverse
1002 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1004 rend() _GLIBCXX_NOEXCEPT
1005 { return reverse_iterator(this->begin()); }
1008 * Returns a read-only (constant) reverse iterator that points
1009 * to one before the first character in the %string. Iteration
1010 * is done in reverse element order.
1012 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1013 const_reverse_iterator
1014 rend() const _GLIBCXX_NOEXCEPT
1015 { return const_reverse_iterator(this->begin()); }
1017 #if __cplusplus >= 201103L
1019 * Returns a read-only (constant) iterator that points to the first
1020 * character in the %string.
1022 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1024 cbegin() const noexcept
1025 { return const_iterator(this->_M_data()); }
1028 * Returns a read-only (constant) iterator that points one past the
1029 * last character in the %string.
1031 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1033 cend() const noexcept
1034 { return const_iterator(this->_M_data() + this->size()); }
1037 * Returns a read-only (constant) reverse iterator that points
1038 * to the last character in the %string. Iteration is done in
1039 * reverse element order.
1041 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1042 const_reverse_iterator
1043 crbegin() const noexcept
1044 { return const_reverse_iterator(this->end()); }
1047 * Returns a read-only (constant) reverse iterator that points
1048 * to one before the first character in the %string. Iteration
1049 * is done in reverse element order.
1051 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1052 const_reverse_iterator
1053 crend() const noexcept
1054 { return const_reverse_iterator(this->begin()); }
1059 /// Returns the number of characters in the string, not including any
1060 /// null-termination.
1061 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1063 size() const _GLIBCXX_NOEXCEPT
1064 { return _M_string_length
; }
1066 /// Returns the number of characters in the string, not including any
1067 /// null-termination.
1068 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1070 length() const _GLIBCXX_NOEXCEPT
1071 { return _M_string_length
; }
1073 /// Returns the size() of the largest possible %string.
1074 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1076 max_size() const _GLIBCXX_NOEXCEPT
1077 { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
1080 * @brief Resizes the %string to the specified number of characters.
1081 * @param __n Number of characters the %string should contain.
1082 * @param __c Character to fill any new elements.
1084 * This function will %resize the %string to the specified
1085 * number of characters. If the number is smaller than the
1086 * %string's current size the %string is truncated, otherwise
1087 * the %string is extended and new elements are %set to @a __c.
1089 _GLIBCXX20_CONSTEXPR
1091 resize(size_type __n
, _CharT __c
);
1094 * @brief Resizes the %string to the specified number of characters.
1095 * @param __n Number of characters the %string should contain.
1097 * This function will resize the %string to the specified length. If
1098 * the new size is smaller than the %string's current size the %string
1099 * is truncated, otherwise the %string is extended and new characters
1100 * are default-constructed. For basic types such as char, this means
1101 * setting them to 0.
1103 _GLIBCXX20_CONSTEXPR
1105 resize(size_type __n
)
1106 { this->resize(__n
, _CharT()); }
1108 #if __cplusplus >= 201103L
1109 #pragma GCC diagnostic push
1110 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1111 /// A non-binding request to reduce capacity() to size().
1112 _GLIBCXX20_CONSTEXPR
1114 shrink_to_fit() noexcept
1116 #pragma GCC diagnostic pop
1119 #if __cplusplus > 202002L
1120 #define __cpp_lib_string_resize_and_overwrite 202110L
1121 template<typename _Operation
>
1123 resize_and_overwrite(size_type __n
, _Operation __op
);
1127 * Returns the total number of characters that the %string can hold
1128 * before needing to allocate more memory.
1130 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1132 capacity() const _GLIBCXX_NOEXCEPT
1134 return _M_is_local() ? size_type(_S_local_capacity
)
1135 : _M_allocated_capacity
;
1139 * @brief Attempt to preallocate enough memory for specified number of
1141 * @param __res_arg Number of characters required.
1142 * @throw std::length_error If @a __res_arg exceeds @c max_size().
1144 * This function attempts to reserve enough memory for the
1145 * %string to hold the specified number of characters. If the
1146 * number requested is more than max_size(), length_error is
1149 * The advantage of this function is that if optimal code is a
1150 * necessity and the user can determine the string length that will be
1151 * required, the user can reserve the memory in %advance, and thus
1152 * prevent a possible reallocation of memory and copying of %string
1155 _GLIBCXX20_CONSTEXPR
1157 reserve(size_type __res_arg
);
1160 * Equivalent to shrink_to_fit().
1162 #if __cplusplus > 201703L
1163 [[deprecated("use shrink_to_fit() instead")]]
1165 _GLIBCXX20_CONSTEXPR
1170 * Erases the string, making it empty.
1172 _GLIBCXX20_CONSTEXPR
1174 clear() _GLIBCXX_NOEXCEPT
1175 { _M_set_length(0); }
1178 * Returns true if the %string is empty. Equivalent to
1179 * <code>*this == ""</code>.
1181 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1183 empty() const _GLIBCXX_NOEXCEPT
1184 { return this->size() == 0; }
1188 * @brief Subscript access to the data contained in the %string.
1189 * @param __pos The index of the character to access.
1190 * @return Read-only (constant) reference to the character.
1192 * This operator allows for easy, array-style, data access.
1193 * Note that data access with this operator is unchecked and
1194 * out_of_range lookups are not defined. (For checked lookups
1197 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1199 operator[] (size_type __pos
) const _GLIBCXX_NOEXCEPT
1201 __glibcxx_assert(__pos
<= size());
1202 return _M_data()[__pos
];
1206 * @brief Subscript access to the data contained in the %string.
1207 * @param __pos The index of the character to access.
1208 * @return Read/write reference to the character.
1210 * This operator allows for easy, array-style, data access.
1211 * Note that data access with this operator is unchecked and
1212 * out_of_range lookups are not defined. (For checked lookups
1215 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1217 operator[](size_type __pos
)
1219 // Allow pos == size() both in C++98 mode, as v3 extension,
1220 // and in C++11 mode.
1221 __glibcxx_assert(__pos
<= size());
1222 // In pedantic mode be strict in C++98 mode.
1223 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus
>= 201103L || __pos
< size());
1224 return _M_data()[__pos
];
1228 * @brief Provides access to the data contained in the %string.
1229 * @param __n The index of the character to access.
1230 * @return Read-only (const) reference to the character.
1231 * @throw std::out_of_range If @a n is an invalid index.
1233 * This function provides for safer data access. The parameter is
1234 * first checked that it is in the range of the string. The function
1235 * throws out_of_range if the check fails.
1237 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1239 at(size_type __n
) const
1241 if (__n
>= this->size())
1242 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1243 "(which is %zu) >= this->size() "
1246 return _M_data()[__n
];
1250 * @brief Provides access to the data contained in the %string.
1251 * @param __n The index of the character to access.
1252 * @return Read/write reference to the character.
1253 * @throw std::out_of_range If @a n is an invalid index.
1255 * This function provides for safer data access. The parameter is
1256 * first checked that it is in the range of the string. The function
1257 * throws out_of_range if the check fails.
1259 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1264 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1265 "(which is %zu) >= this->size() "
1268 return _M_data()[__n
];
1271 #if __cplusplus >= 201103L
1273 * Returns a read/write reference to the data at the first
1274 * element of the %string.
1276 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1280 __glibcxx_assert(!empty());
1281 return operator[](0);
1285 * Returns a read-only (constant) reference to the data at the first
1286 * element of the %string.
1288 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1290 front() const noexcept
1292 __glibcxx_assert(!empty());
1293 return operator[](0);
1297 * Returns a read/write reference to the data at the last
1298 * element of the %string.
1300 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1304 __glibcxx_assert(!empty());
1305 return operator[](this->size() - 1);
1309 * Returns a read-only (constant) reference to the data at the
1310 * last element of the %string.
1312 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1314 back() const noexcept
1316 __glibcxx_assert(!empty());
1317 return operator[](this->size() - 1);
1323 * @brief Append a string to this string.
1324 * @param __str The string to append.
1325 * @return Reference to this string.
1327 _GLIBCXX20_CONSTEXPR
1329 operator+=(const basic_string
& __str
)
1330 { return this->append(__str
); }
1333 * @brief Append a C string.
1334 * @param __s The C string to append.
1335 * @return Reference to this string.
1337 _GLIBCXX20_CONSTEXPR
1339 operator+=(const _CharT
* __s
)
1340 { return this->append(__s
); }
1343 * @brief Append a character.
1344 * @param __c The character to append.
1345 * @return Reference to this string.
1347 _GLIBCXX20_CONSTEXPR
1349 operator+=(_CharT __c
)
1351 this->push_back(__c
);
1355 #if __cplusplus >= 201103L
1357 * @brief Append an initializer_list of characters.
1358 * @param __l The initializer_list of characters to be appended.
1359 * @return Reference to this string.
1361 _GLIBCXX20_CONSTEXPR
1363 operator+=(initializer_list
<_CharT
> __l
)
1364 { return this->append(__l
.begin(), __l
.size()); }
1367 #if __cplusplus >= 201703L
1369 * @brief Append a string_view.
1370 * @param __svt An object convertible to string_view to be appended.
1371 * @return Reference to this string.
1373 template<typename _Tp
>
1374 _GLIBCXX20_CONSTEXPR
1375 _If_sv
<_Tp
, basic_string
&>
1376 operator+=(const _Tp
& __svt
)
1377 { return this->append(__svt
); }
1381 * @brief Append a string to this string.
1382 * @param __str The string to append.
1383 * @return Reference to this string.
1385 _GLIBCXX20_CONSTEXPR
1387 append(const basic_string
& __str
)
1388 { return this->append(__str
._M_data(), __str
.size()); }
1391 * @brief Append a substring.
1392 * @param __str The string to append.
1393 * @param __pos Index of the first character of str to append.
1394 * @param __n The number of characters to append.
1395 * @return Reference to this string.
1396 * @throw std::out_of_range if @a __pos is not a valid index.
1398 * This function appends @a __n characters from @a __str
1399 * starting at @a __pos to this string. If @a __n is is larger
1400 * than the number of available characters in @a __str, the
1401 * remainder of @a __str is appended.
1403 _GLIBCXX20_CONSTEXPR
1405 append(const basic_string
& __str
, size_type __pos
, size_type __n
= npos
)
1406 { return this->append(__str
._M_data()
1407 + __str
._M_check(__pos
, "basic_string::append"),
1408 __str
._M_limit(__pos
, __n
)); }
1411 * @brief Append a C substring.
1412 * @param __s The C string to append.
1413 * @param __n The number of characters to append.
1414 * @return Reference to this string.
1416 _GLIBCXX20_CONSTEXPR
1418 append(const _CharT
* __s
, size_type __n
)
1420 __glibcxx_requires_string_len(__s
, __n
);
1421 _M_check_length(size_type(0), __n
, "basic_string::append");
1422 return _M_append(__s
, __n
);
1426 * @brief Append a C string.
1427 * @param __s The C string to append.
1428 * @return Reference to this string.
1430 _GLIBCXX20_CONSTEXPR
1432 append(const _CharT
* __s
)
1434 __glibcxx_requires_string(__s
);
1435 const size_type __n
= traits_type::length(__s
);
1436 _M_check_length(size_type(0), __n
, "basic_string::append");
1437 return _M_append(__s
, __n
);
1441 * @brief Append multiple characters.
1442 * @param __n The number of characters to append.
1443 * @param __c The character to use.
1444 * @return Reference to this string.
1446 * Appends __n copies of __c to this string.
1448 _GLIBCXX20_CONSTEXPR
1450 append(size_type __n
, _CharT __c
)
1451 { return _M_replace_aux(this->size(), size_type(0), __n
, __c
); }
1453 #if __cplusplus >= 201103L
1455 * @brief Append an initializer_list of characters.
1456 * @param __l The initializer_list of characters to append.
1457 * @return Reference to this string.
1459 _GLIBCXX20_CONSTEXPR
1461 append(initializer_list
<_CharT
> __l
)
1462 { return this->append(__l
.begin(), __l
.size()); }
1466 * @brief Append a range of characters.
1467 * @param __first Iterator referencing the first character to append.
1468 * @param __last Iterator marking the end of the range.
1469 * @return Reference to this string.
1471 * Appends characters in the range [__first,__last) to this string.
1473 #if __cplusplus >= 201103L
1474 template<class _InputIterator
,
1475 typename
= std::_RequireInputIter
<_InputIterator
>>
1476 _GLIBCXX20_CONSTEXPR
1478 template<class _InputIterator
>
1481 append(_InputIterator __first
, _InputIterator __last
)
1482 { return this->replace(end(), end(), __first
, __last
); }
1484 #if __cplusplus >= 201703L
1486 * @brief Append a string_view.
1487 * @param __svt An object convertible to string_view to be appended.
1488 * @return Reference to this string.
1490 template<typename _Tp
>
1491 _GLIBCXX20_CONSTEXPR
1492 _If_sv
<_Tp
, basic_string
&>
1493 append(const _Tp
& __svt
)
1495 __sv_type __sv
= __svt
;
1496 return this->append(__sv
.data(), __sv
.size());
1500 * @brief Append a range of characters from a string_view.
1501 * @param __svt An object convertible to string_view to be appended from.
1502 * @param __pos The position in the string_view to append from.
1503 * @param __n The number of characters to append from the string_view.
1504 * @return Reference to this string.
1506 template<typename _Tp
>
1507 _GLIBCXX20_CONSTEXPR
1508 _If_sv
<_Tp
, basic_string
&>
1509 append(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
1511 __sv_type __sv
= __svt
;
1512 return _M_append(__sv
.data()
1513 + std::__sv_check(__sv
.size(), __pos
, "basic_string::append"),
1514 std::__sv_limit(__sv
.size(), __pos
, __n
));
1519 * @brief Append a single character.
1520 * @param __c Character to append.
1522 _GLIBCXX20_CONSTEXPR
1524 push_back(_CharT __c
)
1526 const size_type __size
= this->size();
1527 if (__size
+ 1 > this->capacity())
1528 this->_M_mutate(__size
, size_type(0), 0, size_type(1));
1529 traits_type::assign(this->_M_data()[__size
], __c
);
1530 this->_M_set_length(__size
+ 1);
1534 * @brief Set value to contents of another string.
1535 * @param __str Source string to use.
1536 * @return Reference to this string.
1538 _GLIBCXX20_CONSTEXPR
1540 assign(const basic_string
& __str
)
1542 #if __cplusplus >= 201103L
1543 if (_Alloc_traits::_S_propagate_on_copy_assign())
1545 if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
1546 && _M_get_allocator() != __str
._M_get_allocator())
1548 // Propagating allocator cannot free existing storage so must
1549 // deallocate it before replacing current allocator.
1550 if (__str
.size() <= _S_local_capacity
)
1552 _M_destroy(_M_allocated_capacity
);
1553 _M_data(_M_use_local_data());
1558 const auto __len
= __str
.size();
1559 auto __alloc
= __str
._M_get_allocator();
1560 // If this allocation throws there are no effects:
1561 auto __ptr
= _Alloc_traits::allocate(__alloc
, __len
+ 1);
1562 _M_destroy(_M_allocated_capacity
);
1565 _M_set_length(__len
);
1568 std::__alloc_on_copy(_M_get_allocator(), __str
._M_get_allocator());
1571 this->_M_assign(__str
);
1575 #if __cplusplus >= 201103L
1577 * @brief Set value to contents of another string.
1578 * @param __str Source string to use.
1579 * @return Reference to this string.
1581 * This function sets this string to the exact contents of @a __str.
1582 * @a __str is a valid, but unspecified string.
1584 _GLIBCXX20_CONSTEXPR
1586 assign(basic_string
&& __str
)
1587 noexcept(_Alloc_traits::_S_nothrow_move())
1589 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1590 // 2063. Contradictory requirements for string move assignment
1591 return *this = std::move(__str
);
1596 * @brief Set value to a substring of a string.
1597 * @param __str The string to use.
1598 * @param __pos Index of the first character of str.
1599 * @param __n Number of characters to use.
1600 * @return Reference to this string.
1601 * @throw std::out_of_range if @a pos is not a valid index.
1603 * This function sets this string to the substring of @a __str
1604 * consisting of @a __n characters at @a __pos. If @a __n is
1605 * is larger than the number of available characters in @a
1606 * __str, the remainder of @a __str is used.
1608 _GLIBCXX20_CONSTEXPR
1610 assign(const basic_string
& __str
, size_type __pos
, size_type __n
= npos
)
1611 { return _M_replace(size_type(0), this->size(), __str
._M_data()
1612 + __str
._M_check(__pos
, "basic_string::assign"),
1613 __str
._M_limit(__pos
, __n
)); }
1616 * @brief Set value to a C substring.
1617 * @param __s The C string to use.
1618 * @param __n Number of characters to use.
1619 * @return Reference to this string.
1621 * This function sets the value of this string to the first @a __n
1622 * characters of @a __s. If @a __n is is larger than the number of
1623 * available characters in @a __s, the remainder of @a __s is used.
1625 _GLIBCXX20_CONSTEXPR
1627 assign(const _CharT
* __s
, size_type __n
)
1629 __glibcxx_requires_string_len(__s
, __n
);
1630 return _M_replace(size_type(0), this->size(), __s
, __n
);
1634 * @brief Set value to contents of a C string.
1635 * @param __s The C string to use.
1636 * @return Reference to this string.
1638 * This function sets the value of this string to the value of @a __s.
1639 * The data is copied, so there is no dependence on @a __s once the
1642 _GLIBCXX20_CONSTEXPR
1644 assign(const _CharT
* __s
)
1646 __glibcxx_requires_string(__s
);
1647 return _M_replace(size_type(0), this->size(), __s
,
1648 traits_type::length(__s
));
1652 * @brief Set value to multiple characters.
1653 * @param __n Length of the resulting string.
1654 * @param __c The character to use.
1655 * @return Reference to this string.
1657 * This function sets the value of this string to @a __n copies of
1660 _GLIBCXX20_CONSTEXPR
1662 assign(size_type __n
, _CharT __c
)
1663 { return _M_replace_aux(size_type(0), this->size(), __n
, __c
); }
1666 * @brief Set value to a range of characters.
1667 * @param __first Iterator referencing the first character to append.
1668 * @param __last Iterator marking the end of the range.
1669 * @return Reference to this string.
1671 * Sets value of string to characters in the range [__first,__last).
1673 #if __cplusplus >= 201103L
1674 template<class _InputIterator
,
1675 typename
= std::_RequireInputIter
<_InputIterator
>>
1676 _GLIBCXX20_CONSTEXPR
1678 template<class _InputIterator
>
1681 assign(_InputIterator __first
, _InputIterator __last
)
1682 { return this->replace(begin(), end(), __first
, __last
); }
1684 #if __cplusplus >= 201103L
1686 * @brief Set value to an initializer_list of characters.
1687 * @param __l The initializer_list of characters to assign.
1688 * @return Reference to this string.
1690 _GLIBCXX20_CONSTEXPR
1692 assign(initializer_list
<_CharT
> __l
)
1693 { return this->assign(__l
.begin(), __l
.size()); }
1696 #if __cplusplus >= 201703L
1698 * @brief Set value from a string_view.
1699 * @param __svt The source object convertible to string_view.
1700 * @return Reference to this string.
1702 template<typename _Tp
>
1703 _GLIBCXX20_CONSTEXPR
1704 _If_sv
<_Tp
, basic_string
&>
1705 assign(const _Tp
& __svt
)
1707 __sv_type __sv
= __svt
;
1708 return this->assign(__sv
.data(), __sv
.size());
1712 * @brief Set value from a range of characters in a string_view.
1713 * @param __svt The source object convertible to string_view.
1714 * @param __pos The position in the string_view to assign from.
1715 * @param __n The number of characters to assign.
1716 * @return Reference to this string.
1718 template<typename _Tp
>
1719 _GLIBCXX20_CONSTEXPR
1720 _If_sv
<_Tp
, basic_string
&>
1721 assign(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
1723 __sv_type __sv
= __svt
;
1724 return _M_replace(size_type(0), this->size(),
1726 + std::__sv_check(__sv
.size(), __pos
, "basic_string::assign"),
1727 std::__sv_limit(__sv
.size(), __pos
, __n
));
1731 #if __cplusplus >= 201103L
1733 * @brief Insert multiple characters.
1734 * @param __p Const_iterator referencing location in string to
1736 * @param __n Number of characters to insert
1737 * @param __c The character to insert.
1738 * @return Iterator referencing the first inserted char.
1739 * @throw std::length_error If new length exceeds @c max_size().
1741 * Inserts @a __n copies of character @a __c starting at the
1742 * position referenced by iterator @a __p. If adding
1743 * characters causes the length to exceed max_size(),
1744 * length_error is thrown. The value of the string doesn't
1745 * change if an error is thrown.
1747 _GLIBCXX20_CONSTEXPR
1749 insert(const_iterator __p
, size_type __n
, _CharT __c
)
1751 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1752 const size_type __pos
= __p
- begin();
1753 this->replace(__p
, __p
, __n
, __c
);
1754 return iterator(this->_M_data() + __pos
);
1758 * @brief Insert multiple characters.
1759 * @param __p Iterator referencing location in string to insert at.
1760 * @param __n Number of characters to insert
1761 * @param __c The character to insert.
1762 * @throw std::length_error If new length exceeds @c max_size().
1764 * Inserts @a __n copies of character @a __c starting at the
1765 * position referenced by iterator @a __p. If adding
1766 * characters causes the length to exceed max_size(),
1767 * length_error is thrown. The value of the string doesn't
1768 * change if an error is thrown.
1771 insert(iterator __p
, size_type __n
, _CharT __c
)
1772 { this->replace(__p
, __p
, __n
, __c
); }
1775 #if __cplusplus >= 201103L
1777 * @brief Insert a range of characters.
1778 * @param __p Const_iterator referencing location in string to
1780 * @param __beg Start of range.
1781 * @param __end End of range.
1782 * @return Iterator referencing the first inserted char.
1783 * @throw std::length_error If new length exceeds @c max_size().
1785 * Inserts characters in range [beg,end). If adding characters
1786 * causes the length to exceed max_size(), length_error is
1787 * thrown. The value of the string doesn't change if an error
1790 template<class _InputIterator
,
1791 typename
= std::_RequireInputIter
<_InputIterator
>>
1792 _GLIBCXX20_CONSTEXPR
1794 insert(const_iterator __p
, _InputIterator __beg
, _InputIterator __end
)
1796 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1797 const size_type __pos
= __p
- begin();
1798 this->replace(__p
, __p
, __beg
, __end
);
1799 return iterator(this->_M_data() + __pos
);
1803 * @brief Insert a range of characters.
1804 * @param __p Iterator referencing location in string to insert at.
1805 * @param __beg Start of range.
1806 * @param __end End of range.
1807 * @throw std::length_error If new length exceeds @c max_size().
1809 * Inserts characters in range [__beg,__end). If adding
1810 * characters causes the length to exceed max_size(),
1811 * length_error is thrown. The value of the string doesn't
1812 * change if an error is thrown.
1814 template<class _InputIterator
>
1816 insert(iterator __p
, _InputIterator __beg
, _InputIterator __end
)
1817 { this->replace(__p
, __p
, __beg
, __end
); }
1820 #if __cplusplus >= 201103L
1822 * @brief Insert an initializer_list of characters.
1823 * @param __p Iterator referencing location in string to insert at.
1824 * @param __l The initializer_list of characters to insert.
1825 * @throw std::length_error If new length exceeds @c max_size().
1827 _GLIBCXX20_CONSTEXPR
1829 insert(const_iterator __p
, initializer_list
<_CharT
> __l
)
1830 { return this->insert(__p
, __l
.begin(), __l
.end()); }
1832 #ifdef _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
1833 // See PR libstdc++/83328
1835 insert(iterator __p
, initializer_list
<_CharT
> __l
)
1837 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1838 this->insert(__p
- begin(), __l
.begin(), __l
.size());
1844 * @brief Insert value of a string.
1845 * @param __pos1 Position in string to insert at.
1846 * @param __str The string to insert.
1847 * @return Reference to this string.
1848 * @throw std::length_error If new length exceeds @c max_size().
1850 * Inserts value of @a __str starting at @a __pos1. If adding
1851 * characters causes the length to exceed max_size(),
1852 * length_error is thrown. The value of the string doesn't
1853 * change if an error is thrown.
1855 _GLIBCXX20_CONSTEXPR
1857 insert(size_type __pos1
, const basic_string
& __str
)
1858 { return this->replace(__pos1
, size_type(0),
1859 __str
._M_data(), __str
.size()); }
1862 * @brief Insert a substring.
1863 * @param __pos1 Position in string to insert at.
1864 * @param __str The string to insert.
1865 * @param __pos2 Start of characters in str to insert.
1866 * @param __n Number of characters to insert.
1867 * @return Reference to this string.
1868 * @throw std::length_error If new length exceeds @c max_size().
1869 * @throw std::out_of_range If @a pos1 > size() or
1870 * @a __pos2 > @a str.size().
1872 * Starting at @a pos1, insert @a __n character of @a __str
1873 * beginning with @a __pos2. If adding characters causes the
1874 * length to exceed max_size(), length_error is thrown. If @a
1875 * __pos1 is beyond the end of this string or @a __pos2 is
1876 * beyond the end of @a __str, out_of_range is thrown. The
1877 * value of the string doesn't change if an error is thrown.
1879 _GLIBCXX20_CONSTEXPR
1881 insert(size_type __pos1
, const basic_string
& __str
,
1882 size_type __pos2
, size_type __n
= npos
)
1883 { return this->replace(__pos1
, size_type(0), __str
._M_data()
1884 + __str
._M_check(__pos2
, "basic_string::insert"),
1885 __str
._M_limit(__pos2
, __n
)); }
1888 * @brief Insert a C substring.
1889 * @param __pos Position in string to insert at.
1890 * @param __s The C string to insert.
1891 * @param __n The number of characters to insert.
1892 * @return Reference to this string.
1893 * @throw std::length_error If new length exceeds @c max_size().
1894 * @throw std::out_of_range If @a __pos is beyond the end of this
1897 * Inserts the first @a __n characters of @a __s starting at @a
1898 * __pos. If adding characters causes the length to exceed
1899 * max_size(), length_error is thrown. If @a __pos is beyond
1900 * end(), out_of_range is thrown. The value of the string
1901 * doesn't change if an error is thrown.
1903 _GLIBCXX20_CONSTEXPR
1905 insert(size_type __pos
, const _CharT
* __s
, size_type __n
)
1906 { return this->replace(__pos
, size_type(0), __s
, __n
); }
1909 * @brief Insert a C string.
1910 * @param __pos Position in string to insert at.
1911 * @param __s The C string to insert.
1912 * @return Reference to this string.
1913 * @throw std::length_error If new length exceeds @c max_size().
1914 * @throw std::out_of_range If @a pos is beyond the end of this
1917 * Inserts the first @a n characters of @a __s starting at @a __pos. If
1918 * adding characters causes the length to exceed max_size(),
1919 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
1920 * thrown. The value of the string doesn't change if an error is
1923 _GLIBCXX20_CONSTEXPR
1925 insert(size_type __pos
, const _CharT
* __s
)
1927 __glibcxx_requires_string(__s
);
1928 return this->replace(__pos
, size_type(0), __s
,
1929 traits_type::length(__s
));
1933 * @brief Insert multiple characters.
1934 * @param __pos Index in string to insert at.
1935 * @param __n Number of characters to insert
1936 * @param __c The character to insert.
1937 * @return Reference to this string.
1938 * @throw std::length_error If new length exceeds @c max_size().
1939 * @throw std::out_of_range If @a __pos is beyond the end of this
1942 * Inserts @a __n copies of character @a __c starting at index
1943 * @a __pos. If adding characters causes the length to exceed
1944 * max_size(), length_error is thrown. If @a __pos > length(),
1945 * out_of_range is thrown. The value of the string doesn't
1946 * change if an error is thrown.
1948 _GLIBCXX20_CONSTEXPR
1950 insert(size_type __pos
, size_type __n
, _CharT __c
)
1951 { return _M_replace_aux(_M_check(__pos
, "basic_string::insert"),
1952 size_type(0), __n
, __c
); }
1955 * @brief Insert one character.
1956 * @param __p Iterator referencing position in string to insert at.
1957 * @param __c The character to insert.
1958 * @return Iterator referencing newly inserted char.
1959 * @throw std::length_error If new length exceeds @c max_size().
1961 * Inserts character @a __c at position referenced by @a __p.
1962 * If adding character causes the length to exceed max_size(),
1963 * length_error is thrown. If @a __p is beyond end of string,
1964 * out_of_range is thrown. The value of the string doesn't
1965 * change if an error is thrown.
1967 _GLIBCXX20_CONSTEXPR
1969 insert(__const_iterator __p
, _CharT __c
)
1971 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1972 const size_type __pos
= __p
- begin();
1973 _M_replace_aux(__pos
, size_type(0), size_type(1), __c
);
1974 return iterator(_M_data() + __pos
);
1977 #if __cplusplus >= 201703L
1979 * @brief Insert a string_view.
1980 * @param __pos Position in string to insert at.
1981 * @param __svt The object convertible to string_view to insert.
1982 * @return Reference to this string.
1984 template<typename _Tp
>
1985 _GLIBCXX20_CONSTEXPR
1986 _If_sv
<_Tp
, basic_string
&>
1987 insert(size_type __pos
, const _Tp
& __svt
)
1989 __sv_type __sv
= __svt
;
1990 return this->insert(__pos
, __sv
.data(), __sv
.size());
1994 * @brief Insert a string_view.
1995 * @param __pos1 Position in string to insert at.
1996 * @param __svt The object convertible to string_view to insert from.
1997 * @param __pos2 Start of characters in str to insert.
1998 * @param __n The number of characters to insert.
1999 * @return Reference to this string.
2001 template<typename _Tp
>
2002 _GLIBCXX20_CONSTEXPR
2003 _If_sv
<_Tp
, basic_string
&>
2004 insert(size_type __pos1
, const _Tp
& __svt
,
2005 size_type __pos2
, size_type __n
= npos
)
2007 __sv_type __sv
= __svt
;
2008 return this->replace(__pos1
, size_type(0),
2010 + std::__sv_check(__sv
.size(), __pos2
, "basic_string::insert"),
2011 std::__sv_limit(__sv
.size(), __pos2
, __n
));
2016 * @brief Remove characters.
2017 * @param __pos Index of first character to remove (default 0).
2018 * @param __n Number of characters to remove (default remainder).
2019 * @return Reference to this string.
2020 * @throw std::out_of_range If @a pos is beyond the end of this
2023 * Removes @a __n characters from this string starting at @a
2024 * __pos. The length of the string is reduced by @a __n. If
2025 * there are < @a __n characters to remove, the remainder of
2026 * the string is truncated. If @a __p is beyond end of string,
2027 * out_of_range is thrown. The value of the string doesn't
2028 * change if an error is thrown.
2030 _GLIBCXX20_CONSTEXPR
2032 erase(size_type __pos
= 0, size_type __n
= npos
)
2034 _M_check(__pos
, "basic_string::erase");
2036 this->_M_set_length(__pos
);
2038 this->_M_erase(__pos
, _M_limit(__pos
, __n
));
2043 * @brief Remove one character.
2044 * @param __position Iterator referencing the character to remove.
2045 * @return iterator referencing same location after removal.
2047 * Removes the character at @a __position from this string. The value
2048 * of the string doesn't change if an error is thrown.
2050 _GLIBCXX20_CONSTEXPR
2052 erase(__const_iterator __position
)
2054 _GLIBCXX_DEBUG_PEDASSERT(__position
>= begin()
2055 && __position
< end());
2056 const size_type __pos
= __position
- begin();
2057 this->_M_erase(__pos
, size_type(1));
2058 return iterator(_M_data() + __pos
);
2062 * @brief Remove a range of characters.
2063 * @param __first Iterator referencing the first character to remove.
2064 * @param __last Iterator referencing the end of the range.
2065 * @return Iterator referencing location of first after removal.
2067 * Removes the characters in the range [first,last) from this string.
2068 * The value of the string doesn't change if an error is thrown.
2070 _GLIBCXX20_CONSTEXPR
2072 erase(__const_iterator __first
, __const_iterator __last
)
2074 _GLIBCXX_DEBUG_PEDASSERT(__first
>= begin() && __first
<= __last
2075 && __last
<= end());
2076 const size_type __pos
= __first
- begin();
2077 if (__last
== end())
2078 this->_M_set_length(__pos
);
2080 this->_M_erase(__pos
, __last
- __first
);
2081 return iterator(this->_M_data() + __pos
);
2084 #if __cplusplus >= 201103L
2086 * @brief Remove the last character.
2088 * The string must be non-empty.
2090 _GLIBCXX20_CONSTEXPR
2094 __glibcxx_assert(!empty());
2095 _M_erase(size() - 1, 1);
2100 * @brief Replace characters with value from another string.
2101 * @param __pos Index of first character to replace.
2102 * @param __n Number of characters to be replaced.
2103 * @param __str String to insert.
2104 * @return Reference to this string.
2105 * @throw std::out_of_range If @a pos is beyond the end of this
2107 * @throw std::length_error If new length exceeds @c max_size().
2109 * Removes the characters in the range [__pos,__pos+__n) from
2110 * this string. In place, the value of @a __str is inserted.
2111 * If @a __pos is beyond end of string, out_of_range is thrown.
2112 * If the length of the result exceeds max_size(), length_error
2113 * is thrown. The value of the string doesn't change if an
2116 _GLIBCXX20_CONSTEXPR
2118 replace(size_type __pos
, size_type __n
, const basic_string
& __str
)
2119 { return this->replace(__pos
, __n
, __str
._M_data(), __str
.size()); }
2122 * @brief Replace characters with value from another string.
2123 * @param __pos1 Index of first character to replace.
2124 * @param __n1 Number of characters to be replaced.
2125 * @param __str String to insert.
2126 * @param __pos2 Index of first character of str to use.
2127 * @param __n2 Number of characters from str to use.
2128 * @return Reference to this string.
2129 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
2131 * @throw std::length_error If new length exceeds @c max_size().
2133 * Removes the characters in the range [__pos1,__pos1 + n) from this
2134 * string. In place, the value of @a __str is inserted. If @a __pos is
2135 * beyond end of string, out_of_range is thrown. If the length of the
2136 * result exceeds max_size(), length_error is thrown. The value of the
2137 * string doesn't change if an error is thrown.
2139 _GLIBCXX20_CONSTEXPR
2141 replace(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
2142 size_type __pos2
, size_type __n2
= npos
)
2143 { return this->replace(__pos1
, __n1
, __str
._M_data()
2144 + __str
._M_check(__pos2
, "basic_string::replace"),
2145 __str
._M_limit(__pos2
, __n2
)); }
2148 * @brief Replace characters with value of a C substring.
2149 * @param __pos Index of first character to replace.
2150 * @param __n1 Number of characters to be replaced.
2151 * @param __s C string to insert.
2152 * @param __n2 Number of characters from @a s to use.
2153 * @return Reference to this string.
2154 * @throw std::out_of_range If @a pos1 > size().
2155 * @throw std::length_error If new length exceeds @c max_size().
2157 * Removes the characters in the range [__pos,__pos + __n1)
2158 * from this string. In place, the first @a __n2 characters of
2159 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
2160 * @a __pos is beyond end of string, out_of_range is thrown. If
2161 * the length of result exceeds max_size(), length_error is
2162 * thrown. The value of the string doesn't change if an error
2165 _GLIBCXX20_CONSTEXPR
2167 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
2170 __glibcxx_requires_string_len(__s
, __n2
);
2171 return _M_replace(_M_check(__pos
, "basic_string::replace"),
2172 _M_limit(__pos
, __n1
), __s
, __n2
);
2176 * @brief Replace characters with value of a C string.
2177 * @param __pos Index of first character to replace.
2178 * @param __n1 Number of characters to be replaced.
2179 * @param __s C string to insert.
2180 * @return Reference to this string.
2181 * @throw std::out_of_range If @a pos > size().
2182 * @throw std::length_error If new length exceeds @c max_size().
2184 * Removes the characters in the range [__pos,__pos + __n1)
2185 * from this string. In place, the characters of @a __s are
2186 * inserted. If @a __pos is beyond end of string, out_of_range
2187 * is thrown. If the length of result exceeds max_size(),
2188 * length_error is thrown. The value of the string doesn't
2189 * change if an error is thrown.
2191 _GLIBCXX20_CONSTEXPR
2193 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
)
2195 __glibcxx_requires_string(__s
);
2196 return this->replace(__pos
, __n1
, __s
, traits_type::length(__s
));
2200 * @brief Replace characters with multiple characters.
2201 * @param __pos Index of first character to replace.
2202 * @param __n1 Number of characters to be replaced.
2203 * @param __n2 Number of characters to insert.
2204 * @param __c Character to insert.
2205 * @return Reference to this string.
2206 * @throw std::out_of_range If @a __pos > size().
2207 * @throw std::length_error If new length exceeds @c max_size().
2209 * Removes the characters in the range [pos,pos + n1) from this
2210 * string. In place, @a __n2 copies of @a __c are inserted.
2211 * If @a __pos is beyond end of string, out_of_range is thrown.
2212 * If the length of result exceeds max_size(), length_error is
2213 * thrown. The value of the string doesn't change if an error
2216 _GLIBCXX20_CONSTEXPR
2218 replace(size_type __pos
, size_type __n1
, size_type __n2
, _CharT __c
)
2219 { return _M_replace_aux(_M_check(__pos
, "basic_string::replace"),
2220 _M_limit(__pos
, __n1
), __n2
, __c
); }
2223 * @brief Replace range of characters with string.
2224 * @param __i1 Iterator referencing start of range to replace.
2225 * @param __i2 Iterator referencing end of range to replace.
2226 * @param __str String value to insert.
2227 * @return Reference to this string.
2228 * @throw std::length_error If new length exceeds @c max_size().
2230 * Removes the characters in the range [__i1,__i2). In place,
2231 * the value of @a __str is inserted. If the length of result
2232 * exceeds max_size(), length_error is thrown. The value of
2233 * the string doesn't change if an error is thrown.
2235 _GLIBCXX20_CONSTEXPR
2237 replace(__const_iterator __i1
, __const_iterator __i2
,
2238 const basic_string
& __str
)
2239 { return this->replace(__i1
, __i2
, __str
._M_data(), __str
.size()); }
2242 * @brief Replace range of characters with C substring.
2243 * @param __i1 Iterator referencing start of range to replace.
2244 * @param __i2 Iterator referencing end of range to replace.
2245 * @param __s C string value to insert.
2246 * @param __n Number of characters from s to insert.
2247 * @return Reference to this string.
2248 * @throw std::length_error If new length exceeds @c max_size().
2250 * Removes the characters in the range [__i1,__i2). In place,
2251 * the first @a __n characters of @a __s are inserted. If the
2252 * length of result exceeds max_size(), length_error is thrown.
2253 * The value of the string doesn't change if an error is
2256 _GLIBCXX20_CONSTEXPR
2258 replace(__const_iterator __i1
, __const_iterator __i2
,
2259 const _CharT
* __s
, size_type __n
)
2261 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2263 return this->replace(__i1
- begin(), __i2
- __i1
, __s
, __n
);
2267 * @brief Replace range of characters with C string.
2268 * @param __i1 Iterator referencing start of range to replace.
2269 * @param __i2 Iterator referencing end of range to replace.
2270 * @param __s C string value to insert.
2271 * @return Reference to this string.
2272 * @throw std::length_error If new length exceeds @c max_size().
2274 * Removes the characters in the range [__i1,__i2). In place,
2275 * the characters of @a __s are inserted. If the length of
2276 * result exceeds max_size(), length_error is thrown. The
2277 * value of the string doesn't change if an error is thrown.
2279 _GLIBCXX20_CONSTEXPR
2281 replace(__const_iterator __i1
, __const_iterator __i2
, const _CharT
* __s
)
2283 __glibcxx_requires_string(__s
);
2284 return this->replace(__i1
, __i2
, __s
, traits_type::length(__s
));
2288 * @brief Replace range of characters with multiple characters
2289 * @param __i1 Iterator referencing start of range to replace.
2290 * @param __i2 Iterator referencing end of range to replace.
2291 * @param __n Number of characters to insert.
2292 * @param __c Character to insert.
2293 * @return Reference to this string.
2294 * @throw std::length_error If new length exceeds @c max_size().
2296 * Removes the characters in the range [__i1,__i2). In place,
2297 * @a __n copies of @a __c are inserted. If the length of
2298 * result exceeds max_size(), length_error is thrown. The
2299 * value of the string doesn't change if an error is thrown.
2301 _GLIBCXX20_CONSTEXPR
2303 replace(__const_iterator __i1
, __const_iterator __i2
, size_type __n
,
2306 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2308 return _M_replace_aux(__i1
- begin(), __i2
- __i1
, __n
, __c
);
2312 * @brief Replace range of characters with range.
2313 * @param __i1 Iterator referencing start of range to replace.
2314 * @param __i2 Iterator referencing end of range to replace.
2315 * @param __k1 Iterator referencing start of range to insert.
2316 * @param __k2 Iterator referencing end of range to insert.
2317 * @return Reference to this string.
2318 * @throw std::length_error If new length exceeds @c max_size().
2320 * Removes the characters in the range [__i1,__i2). In place,
2321 * characters in the range [__k1,__k2) are inserted. If the
2322 * length of result exceeds max_size(), length_error is thrown.
2323 * The value of the string doesn't change if an error is
2326 #if __cplusplus >= 201103L
2327 template<class _InputIterator
,
2328 typename
= std::_RequireInputIter
<_InputIterator
>>
2329 _GLIBCXX20_CONSTEXPR
2331 replace(const_iterator __i1
, const_iterator __i2
,
2332 _InputIterator __k1
, _InputIterator __k2
)
2334 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2336 __glibcxx_requires_valid_range(__k1
, __k2
);
2337 return this->_M_replace_dispatch(__i1
, __i2
, __k1
, __k2
,
2338 std::__false_type());
2341 template<class _InputIterator
>
2342 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
2343 typename __enable_if_not_native_iterator
<_InputIterator
>::__type
2347 replace(iterator __i1
, iterator __i2
,
2348 _InputIterator __k1
, _InputIterator __k2
)
2350 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2352 __glibcxx_requires_valid_range(__k1
, __k2
);
2353 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
2354 return _M_replace_dispatch(__i1
, __i2
, __k1
, __k2
, _Integral());
2358 // Specializations for the common case of pointer and iterator:
2359 // useful to avoid the overhead of temporary buffering in _M_replace.
2360 _GLIBCXX20_CONSTEXPR
2362 replace(__const_iterator __i1
, __const_iterator __i2
,
2363 _CharT
* __k1
, _CharT
* __k2
)
2365 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2367 __glibcxx_requires_valid_range(__k1
, __k2
);
2368 return this->replace(__i1
- begin(), __i2
- __i1
,
2372 _GLIBCXX20_CONSTEXPR
2374 replace(__const_iterator __i1
, __const_iterator __i2
,
2375 const _CharT
* __k1
, const _CharT
* __k2
)
2377 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2379 __glibcxx_requires_valid_range(__k1
, __k2
);
2380 return this->replace(__i1
- begin(), __i2
- __i1
,
2384 _GLIBCXX20_CONSTEXPR
2386 replace(__const_iterator __i1
, __const_iterator __i2
,
2387 iterator __k1
, iterator __k2
)
2389 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2391 __glibcxx_requires_valid_range(__k1
, __k2
);
2392 return this->replace(__i1
- begin(), __i2
- __i1
,
2393 __k1
.base(), __k2
- __k1
);
2396 _GLIBCXX20_CONSTEXPR
2398 replace(__const_iterator __i1
, __const_iterator __i2
,
2399 const_iterator __k1
, const_iterator __k2
)
2401 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2403 __glibcxx_requires_valid_range(__k1
, __k2
);
2404 return this->replace(__i1
- begin(), __i2
- __i1
,
2405 __k1
.base(), __k2
- __k1
);
2408 #if __cplusplus >= 201103L
2410 * @brief Replace range of characters with initializer_list.
2411 * @param __i1 Iterator referencing start of range to replace.
2412 * @param __i2 Iterator referencing end of range to replace.
2413 * @param __l The initializer_list of characters to insert.
2414 * @return Reference to this string.
2415 * @throw std::length_error If new length exceeds @c max_size().
2417 * Removes the characters in the range [__i1,__i2). In place,
2418 * characters in the range [__k1,__k2) are inserted. If the
2419 * length of result exceeds max_size(), length_error is thrown.
2420 * The value of the string doesn't change if an error is
2423 _GLIBCXX20_CONSTEXPR
2424 basic_string
& replace(const_iterator __i1
, const_iterator __i2
,
2425 initializer_list
<_CharT
> __l
)
2426 { return this->replace(__i1
, __i2
, __l
.begin(), __l
.size()); }
2429 #if __cplusplus >= 201703L
2431 * @brief Replace range of characters with string_view.
2432 * @param __pos The position to replace at.
2433 * @param __n The number of characters to replace.
2434 * @param __svt The object convertible to string_view to insert.
2435 * @return Reference to this string.
2437 template<typename _Tp
>
2438 _GLIBCXX20_CONSTEXPR
2439 _If_sv
<_Tp
, basic_string
&>
2440 replace(size_type __pos
, size_type __n
, const _Tp
& __svt
)
2442 __sv_type __sv
= __svt
;
2443 return this->replace(__pos
, __n
, __sv
.data(), __sv
.size());
2447 * @brief Replace range of characters with string_view.
2448 * @param __pos1 The position to replace at.
2449 * @param __n1 The number of characters to replace.
2450 * @param __svt The object convertible to string_view to insert from.
2451 * @param __pos2 The position in the string_view to insert from.
2452 * @param __n2 The number of characters to insert.
2453 * @return Reference to this string.
2455 template<typename _Tp
>
2456 _GLIBCXX20_CONSTEXPR
2457 _If_sv
<_Tp
, basic_string
&>
2458 replace(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
2459 size_type __pos2
, size_type __n2
= npos
)
2461 __sv_type __sv
= __svt
;
2462 return this->replace(__pos1
, __n1
,
2464 + std::__sv_check(__sv
.size(), __pos2
, "basic_string::replace"),
2465 std::__sv_limit(__sv
.size(), __pos2
, __n2
));
2469 * @brief Replace range of characters with string_view.
2470 * @param __i1 An iterator referencing the start position
2472 * @param __i2 An iterator referencing the end position
2474 * @param __svt The object convertible to string_view to insert from.
2475 * @return Reference to this string.
2477 template<typename _Tp
>
2478 _GLIBCXX20_CONSTEXPR
2479 _If_sv
<_Tp
, basic_string
&>
2480 replace(const_iterator __i1
, const_iterator __i2
, const _Tp
& __svt
)
2482 __sv_type __sv
= __svt
;
2483 return this->replace(__i1
- begin(), __i2
- __i1
, __sv
);
2488 template<class _Integer
>
2489 _GLIBCXX20_CONSTEXPR
2491 _M_replace_dispatch(const_iterator __i1
, const_iterator __i2
,
2492 _Integer __n
, _Integer __val
, __true_type
)
2493 { return _M_replace_aux(__i1
- begin(), __i2
- __i1
, __n
, __val
); }
2495 template<class _InputIterator
>
2496 _GLIBCXX20_CONSTEXPR
2498 _M_replace_dispatch(const_iterator __i1
, const_iterator __i2
,
2499 _InputIterator __k1
, _InputIterator __k2
,
2502 _GLIBCXX20_CONSTEXPR
2504 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
2507 __attribute__((__noinline__
, __noclone__
, __cold__
)) void
2508 _M_replace_cold(pointer __p
, size_type __len1
, const _CharT
* __s
,
2509 const size_type __len2
, const size_type __how_much
);
2511 _GLIBCXX20_CONSTEXPR
2513 _M_replace(size_type __pos
, size_type __len1
, const _CharT
* __s
,
2514 const size_type __len2
);
2516 _GLIBCXX20_CONSTEXPR
2518 _M_append(const _CharT
* __s
, size_type __n
);
2523 * @brief Copy substring into C string.
2524 * @param __s C string to copy value into.
2525 * @param __n Number of characters to copy.
2526 * @param __pos Index of first character to copy.
2527 * @return Number of characters actually copied
2528 * @throw std::out_of_range If __pos > size().
2530 * Copies up to @a __n characters starting at @a __pos into the
2531 * C string @a __s. If @a __pos is %greater than size(),
2532 * out_of_range is thrown.
2534 _GLIBCXX20_CONSTEXPR
2536 copy(_CharT
* __s
, size_type __n
, size_type __pos
= 0) const;
2539 * @brief Swap contents with another string.
2540 * @param __s String to swap with.
2542 * Exchanges the contents of this string with that of @a __s in constant
2545 _GLIBCXX20_CONSTEXPR
2547 swap(basic_string
& __s
) _GLIBCXX_NOEXCEPT
;
2549 // String operations:
2551 * @brief Return const pointer to null-terminated contents.
2553 * This is a handle to internal data. Do not modify or dire things may
2556 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2558 c_str() const _GLIBCXX_NOEXCEPT
2559 { return _M_data(); }
2562 * @brief Return const pointer to contents.
2564 * This is a pointer to internal data. It is undefined to modify
2565 * the contents through the returned pointer. To get a pointer that
2566 * allows modifying the contents use @c &str[0] instead,
2567 * (or in C++17 the non-const @c str.data() overload).
2569 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2571 data() const _GLIBCXX_NOEXCEPT
2572 { return _M_data(); }
2574 #if __cplusplus >= 201703L
2576 * @brief Return non-const pointer to contents.
2578 * This is a pointer to the character sequence held by the string.
2579 * Modifying the characters in the sequence is allowed.
2581 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2584 { return _M_data(); }
2588 * @brief Return copy of allocator used to construct this string.
2590 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2592 get_allocator() const _GLIBCXX_NOEXCEPT
2593 { return _M_get_allocator(); }
2596 * @brief Find position of a C substring.
2597 * @param __s C string to locate.
2598 * @param __pos Index of character to search from.
2599 * @param __n Number of characters from @a s to search for.
2600 * @return Index of start of first occurrence.
2602 * Starting from @a __pos, searches forward for the first @a
2603 * __n characters in @a __s within this string. If found,
2604 * returns the index where it begins. If not found, returns
2607 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2609 find(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2613 * @brief Find position of a string.
2614 * @param __str String to locate.
2615 * @param __pos Index of character to search from (default 0).
2616 * @return Index of start of first occurrence.
2618 * Starting from @a __pos, searches forward for value of @a __str within
2619 * this string. If found, returns the index where it begins. If not
2620 * found, returns npos.
2622 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2624 find(const basic_string
& __str
, size_type __pos
= 0) const
2626 { return this->find(__str
.data(), __pos
, __str
.size()); }
2628 #if __cplusplus >= 201703L
2630 * @brief Find position of a string_view.
2631 * @param __svt The object convertible to string_view to locate.
2632 * @param __pos Index of character to search from (default 0).
2633 * @return Index of start of first occurrence.
2635 template<typename _Tp
>
2636 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2637 _If_sv
<_Tp
, size_type
>
2638 find(const _Tp
& __svt
, size_type __pos
= 0) const
2639 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2641 __sv_type __sv
= __svt
;
2642 return this->find(__sv
.data(), __pos
, __sv
.size());
2647 * @brief Find position of a C string.
2648 * @param __s C string to locate.
2649 * @param __pos Index of character to search from (default 0).
2650 * @return Index of start of first occurrence.
2652 * Starting from @a __pos, searches forward for the value of @a
2653 * __s within this string. If found, returns the index where
2654 * it begins. If not found, returns npos.
2656 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2658 find(const _CharT
* __s
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
2660 __glibcxx_requires_string(__s
);
2661 return this->find(__s
, __pos
, traits_type::length(__s
));
2665 * @brief Find position of a character.
2666 * @param __c Character to locate.
2667 * @param __pos Index of character to search from (default 0).
2668 * @return Index of first occurrence.
2670 * Starting from @a __pos, searches forward for @a __c within
2671 * this string. If found, returns the index where it was
2672 * found. If not found, returns npos.
2674 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2676 find(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
;
2679 * @brief Find last position of a string.
2680 * @param __str String to locate.
2681 * @param __pos Index of character to search back from (default end).
2682 * @return Index of start of last occurrence.
2684 * Starting from @a __pos, searches backward for value of @a
2685 * __str within this string. If found, returns the index where
2686 * it begins. If not found, returns npos.
2688 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2690 rfind(const basic_string
& __str
, size_type __pos
= npos
) const
2692 { return this->rfind(__str
.data(), __pos
, __str
.size()); }
2694 #if __cplusplus >= 201703L
2696 * @brief Find last position of a string_view.
2697 * @param __svt The object convertible to string_view to locate.
2698 * @param __pos Index of character to search back from (default end).
2699 * @return Index of start of last occurrence.
2701 template<typename _Tp
>
2702 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2703 _If_sv
<_Tp
, size_type
>
2704 rfind(const _Tp
& __svt
, size_type __pos
= npos
) const
2705 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2707 __sv_type __sv
= __svt
;
2708 return this->rfind(__sv
.data(), __pos
, __sv
.size());
2713 * @brief Find last position of a C substring.
2714 * @param __s C string to locate.
2715 * @param __pos Index of character to search back from.
2716 * @param __n Number of characters from s to search for.
2717 * @return Index of start of last occurrence.
2719 * Starting from @a __pos, searches backward for the first @a
2720 * __n characters in @a __s within this string. If found,
2721 * returns the index where it begins. If not found, returns
2724 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2726 rfind(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2730 * @brief Find last position of a C string.
2731 * @param __s C string to locate.
2732 * @param __pos Index of character to start search at (default end).
2733 * @return Index of start of last occurrence.
2735 * Starting from @a __pos, searches backward for the value of
2736 * @a __s within this string. If found, returns the index
2737 * where it begins. If not found, returns npos.
2739 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2741 rfind(const _CharT
* __s
, size_type __pos
= npos
) const
2743 __glibcxx_requires_string(__s
);
2744 return this->rfind(__s
, __pos
, traits_type::length(__s
));
2748 * @brief Find last position of a character.
2749 * @param __c Character to locate.
2750 * @param __pos Index of character to search back from (default end).
2751 * @return Index of last occurrence.
2753 * Starting from @a __pos, searches backward for @a __c within
2754 * this string. If found, returns the index where it was
2755 * found. If not found, returns npos.
2757 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2759 rfind(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
;
2762 * @brief Find position of a character of string.
2763 * @param __str String containing characters to locate.
2764 * @param __pos Index of character to search from (default 0).
2765 * @return Index of first occurrence.
2767 * Starting from @a __pos, searches forward for one of the
2768 * characters of @a __str within this string. If found,
2769 * returns the index where it was found. If not found, returns
2772 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2774 find_first_of(const basic_string
& __str
, size_type __pos
= 0) const
2776 { return this->find_first_of(__str
.data(), __pos
, __str
.size()); }
2778 #if __cplusplus >= 201703L
2780 * @brief Find position of a character of a string_view.
2781 * @param __svt An object convertible to string_view containing
2782 * characters to locate.
2783 * @param __pos Index of character to search from (default 0).
2784 * @return Index of first occurrence.
2786 template<typename _Tp
>
2787 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2788 _If_sv
<_Tp
, size_type
>
2789 find_first_of(const _Tp
& __svt
, size_type __pos
= 0) const
2790 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2792 __sv_type __sv
= __svt
;
2793 return this->find_first_of(__sv
.data(), __pos
, __sv
.size());
2798 * @brief Find position of a character of C substring.
2799 * @param __s String containing characters to locate.
2800 * @param __pos Index of character to search from.
2801 * @param __n Number of characters from s to search for.
2802 * @return Index of first occurrence.
2804 * Starting from @a __pos, searches forward for one of the
2805 * first @a __n characters of @a __s within this string. If
2806 * found, returns the index where it was found. If not found,
2809 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2811 find_first_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2815 * @brief Find position of a character of C string.
2816 * @param __s String containing characters to locate.
2817 * @param __pos Index of character to search from (default 0).
2818 * @return Index of first occurrence.
2820 * Starting from @a __pos, searches forward for one of the
2821 * characters of @a __s within this string. If found, returns
2822 * the index where it was found. If not found, returns npos.
2824 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2826 find_first_of(const _CharT
* __s
, size_type __pos
= 0) const
2829 __glibcxx_requires_string(__s
);
2830 return this->find_first_of(__s
, __pos
, traits_type::length(__s
));
2834 * @brief Find position of a character.
2835 * @param __c Character to locate.
2836 * @param __pos Index of character to search from (default 0).
2837 * @return Index of first occurrence.
2839 * Starting from @a __pos, searches forward for the character
2840 * @a __c within this string. If found, returns the index
2841 * where it was found. If not found, returns npos.
2843 * Note: equivalent to find(__c, __pos).
2845 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2847 find_first_of(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
2848 { return this->find(__c
, __pos
); }
2851 * @brief Find last position of a character of string.
2852 * @param __str String containing characters to locate.
2853 * @param __pos Index of character to search back from (default end).
2854 * @return Index of last occurrence.
2856 * Starting from @a __pos, searches backward for one of the
2857 * characters of @a __str within this string. If found,
2858 * returns the index where it was found. If not found, returns
2861 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2863 find_last_of(const basic_string
& __str
, size_type __pos
= npos
) const
2865 { return this->find_last_of(__str
.data(), __pos
, __str
.size()); }
2867 #if __cplusplus >= 201703L
2869 * @brief Find last position of a character of string.
2870 * @param __svt An object convertible to string_view containing
2871 * characters to locate.
2872 * @param __pos Index of character to search back from (default end).
2873 * @return Index of last occurrence.
2875 template<typename _Tp
>
2876 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2877 _If_sv
<_Tp
, size_type
>
2878 find_last_of(const _Tp
& __svt
, size_type __pos
= npos
) const
2879 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2881 __sv_type __sv
= __svt
;
2882 return this->find_last_of(__sv
.data(), __pos
, __sv
.size());
2887 * @brief Find last position of a character of C substring.
2888 * @param __s C string containing characters to locate.
2889 * @param __pos Index of character to search back from.
2890 * @param __n Number of characters from s to search for.
2891 * @return Index of last occurrence.
2893 * Starting from @a __pos, searches backward for one of the
2894 * first @a __n characters of @a __s within this string. If
2895 * found, returns the index where it was found. If not found,
2898 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2900 find_last_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2904 * @brief Find last position of a character of C string.
2905 * @param __s C string containing characters to locate.
2906 * @param __pos Index of character to search back from (default end).
2907 * @return Index of last occurrence.
2909 * Starting from @a __pos, searches backward for one of the
2910 * characters of @a __s within this string. If found, returns
2911 * the index where it was found. If not found, returns npos.
2913 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2915 find_last_of(const _CharT
* __s
, size_type __pos
= npos
) const
2918 __glibcxx_requires_string(__s
);
2919 return this->find_last_of(__s
, __pos
, traits_type::length(__s
));
2923 * @brief Find last position of a character.
2924 * @param __c Character to locate.
2925 * @param __pos Index of character to search back from (default end).
2926 * @return Index of last occurrence.
2928 * Starting from @a __pos, searches backward for @a __c within
2929 * this string. If found, returns the index where it was
2930 * found. If not found, returns npos.
2932 * Note: equivalent to rfind(__c, __pos).
2934 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2936 find_last_of(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
2937 { return this->rfind(__c
, __pos
); }
2940 * @brief Find position of a character not in string.
2941 * @param __str String containing characters to avoid.
2942 * @param __pos Index of character to search from (default 0).
2943 * @return Index of first occurrence.
2945 * Starting from @a __pos, searches forward for a character not contained
2946 * in @a __str within this string. If found, returns the index where it
2947 * was found. If not found, returns npos.
2949 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2951 find_first_not_of(const basic_string
& __str
, size_type __pos
= 0) const
2953 { return this->find_first_not_of(__str
.data(), __pos
, __str
.size()); }
2955 #if __cplusplus >= 201703L
2957 * @brief Find position of a character not in a string_view.
2958 * @param __svt A object convertible to string_view containing
2959 * characters to avoid.
2960 * @param __pos Index of character to search from (default 0).
2961 * @return Index of first occurrence.
2963 template<typename _Tp
>
2964 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2965 _If_sv
<_Tp
, size_type
>
2966 find_first_not_of(const _Tp
& __svt
, size_type __pos
= 0) const
2967 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2969 __sv_type __sv
= __svt
;
2970 return this->find_first_not_of(__sv
.data(), __pos
, __sv
.size());
2975 * @brief Find position of a character not in C substring.
2976 * @param __s C string containing characters to avoid.
2977 * @param __pos Index of character to search from.
2978 * @param __n Number of characters from __s to consider.
2979 * @return Index of first occurrence.
2981 * Starting from @a __pos, searches forward for a character not
2982 * contained in the first @a __n characters of @a __s within
2983 * this string. If found, returns the index where it was
2984 * found. If not found, returns npos.
2986 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2988 find_first_not_of(const _CharT
* __s
, size_type __pos
,
2989 size_type __n
) const _GLIBCXX_NOEXCEPT
;
2992 * @brief Find position of a character not in C string.
2993 * @param __s C string containing characters to avoid.
2994 * @param __pos Index of character to search from (default 0).
2995 * @return Index of first occurrence.
2997 * Starting from @a __pos, searches forward for a character not
2998 * contained in @a __s within this string. If found, returns
2999 * the index where it was found. If not found, returns npos.
3001 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3003 find_first_not_of(const _CharT
* __s
, size_type __pos
= 0) const
3006 __glibcxx_requires_string(__s
);
3007 return this->find_first_not_of(__s
, __pos
, traits_type::length(__s
));
3011 * @brief Find position of a different character.
3012 * @param __c Character to avoid.
3013 * @param __pos Index of character to search from (default 0).
3014 * @return Index of first occurrence.
3016 * Starting from @a __pos, searches forward for a character
3017 * other than @a __c within this string. If found, returns the
3018 * index where it was found. If not found, returns npos.
3020 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3022 find_first_not_of(_CharT __c
, size_type __pos
= 0) const
3026 * @brief Find last position of a character not in string.
3027 * @param __str String containing characters to avoid.
3028 * @param __pos Index of character to search back from (default end).
3029 * @return Index of last occurrence.
3031 * Starting from @a __pos, searches backward for a character
3032 * not contained in @a __str within this string. If found,
3033 * returns the index where it was found. If not found, returns
3036 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3038 find_last_not_of(const basic_string
& __str
, size_type __pos
= npos
) const
3040 { return this->find_last_not_of(__str
.data(), __pos
, __str
.size()); }
3042 #if __cplusplus >= 201703L
3044 * @brief Find last position of a character not in a string_view.
3045 * @param __svt An object convertible to string_view containing
3046 * characters to avoid.
3047 * @param __pos Index of character to search back from (default end).
3048 * @return Index of last occurrence.
3050 template<typename _Tp
>
3051 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3052 _If_sv
<_Tp
, size_type
>
3053 find_last_not_of(const _Tp
& __svt
, size_type __pos
= npos
) const
3054 noexcept(is_same
<_Tp
, __sv_type
>::value
)
3056 __sv_type __sv
= __svt
;
3057 return this->find_last_not_of(__sv
.data(), __pos
, __sv
.size());
3062 * @brief Find last position of a character not in C substring.
3063 * @param __s C string containing characters to avoid.
3064 * @param __pos Index of character to search back from.
3065 * @param __n Number of characters from s to consider.
3066 * @return Index of last occurrence.
3068 * Starting from @a __pos, searches backward for a character not
3069 * contained in the first @a __n characters of @a __s within this string.
3070 * If found, returns the index where it was found. If not found,
3073 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3075 find_last_not_of(const _CharT
* __s
, size_type __pos
,
3076 size_type __n
) const _GLIBCXX_NOEXCEPT
;
3078 * @brief Find last position of a character not in C string.
3079 * @param __s C string containing characters to avoid.
3080 * @param __pos Index of character to search back from (default end).
3081 * @return Index of last occurrence.
3083 * Starting from @a __pos, searches backward for a character
3084 * not contained in @a __s within this string. If found,
3085 * returns the index where it was found. If not found, returns
3088 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3090 find_last_not_of(const _CharT
* __s
, size_type __pos
= npos
) const
3093 __glibcxx_requires_string(__s
);
3094 return this->find_last_not_of(__s
, __pos
, traits_type::length(__s
));
3098 * @brief Find last position of a different character.
3099 * @param __c Character to avoid.
3100 * @param __pos Index of character to search back from (default end).
3101 * @return Index of last occurrence.
3103 * Starting from @a __pos, searches backward for a character other than
3104 * @a __c within this string. If found, returns the index where it was
3105 * found. If not found, returns npos.
3107 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3109 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const
3113 * @brief Get a substring.
3114 * @param __pos Index of first character (default 0).
3115 * @param __n Number of characters in substring (default remainder).
3116 * @return The new string.
3117 * @throw std::out_of_range If __pos > size().
3119 * Construct and return a new string using the @a __n
3120 * characters starting at @a __pos. If the string is too
3121 * short, use the remainder of the characters. If @a __pos is
3122 * beyond the end of the string, out_of_range is thrown.
3124 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3126 substr(size_type __pos
= 0, size_type __n
= npos
) const
3127 { return basic_string(*this,
3128 _M_check(__pos
, "basic_string::substr"), __n
); }
3131 * @brief Compare to a string.
3132 * @param __str String to compare against.
3133 * @return Integer < 0, 0, or > 0.
3135 * Returns an integer < 0 if this string is ordered before @a
3136 * __str, 0 if their values are equivalent, or > 0 if this
3137 * string is ordered after @a __str. Determines the effective
3138 * length rlen of the strings to compare as the smallest of
3139 * size() and str.size(). The function then compares the two
3140 * strings by calling traits::compare(data(), str.data(),rlen).
3141 * If the result of the comparison is nonzero returns it,
3142 * otherwise the shorter one is ordered first.
3144 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3146 compare(const basic_string
& __str
) const
3148 const size_type __size
= this->size();
3149 const size_type __osize
= __str
.size();
3150 const size_type __len
= std::min(__size
, __osize
);
3152 int __r
= traits_type::compare(_M_data(), __str
.data(), __len
);
3154 __r
= _S_compare(__size
, __osize
);
3158 #if __cplusplus >= 201703L
3160 * @brief Compare to a string_view.
3161 * @param __svt An object convertible to string_view to compare against.
3162 * @return Integer < 0, 0, or > 0.
3164 template<typename _Tp
>
3165 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3167 compare(const _Tp
& __svt
) const
3168 noexcept(is_same
<_Tp
, __sv_type
>::value
)
3170 __sv_type __sv
= __svt
;
3171 const size_type __size
= this->size();
3172 const size_type __osize
= __sv
.size();
3173 const size_type __len
= std::min(__size
, __osize
);
3175 int __r
= traits_type::compare(_M_data(), __sv
.data(), __len
);
3177 __r
= _S_compare(__size
, __osize
);
3182 * @brief Compare to a string_view.
3183 * @param __pos A position in the string to start comparing from.
3184 * @param __n The number of characters to compare.
3185 * @param __svt An object convertible to string_view to compare
3187 * @return Integer < 0, 0, or > 0.
3189 template<typename _Tp
>
3190 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3192 compare(size_type __pos
, size_type __n
, const _Tp
& __svt
) const
3193 noexcept(is_same
<_Tp
, __sv_type
>::value
)
3195 __sv_type __sv
= __svt
;
3196 return __sv_type(*this).substr(__pos
, __n
).compare(__sv
);
3200 * @brief Compare to a string_view.
3201 * @param __pos1 A position in the string to start comparing from.
3202 * @param __n1 The number of characters to compare.
3203 * @param __svt An object convertible to string_view to compare
3205 * @param __pos2 A position in the string_view to start comparing from.
3206 * @param __n2 The number of characters to compare.
3207 * @return Integer < 0, 0, or > 0.
3209 template<typename _Tp
>
3210 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3212 compare(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
3213 size_type __pos2
, size_type __n2
= npos
) const
3214 noexcept(is_same
<_Tp
, __sv_type
>::value
)
3216 __sv_type __sv
= __svt
;
3217 return __sv_type(*this)
3218 .substr(__pos1
, __n1
).compare(__sv
.substr(__pos2
, __n2
));
3223 * @brief Compare substring to a string.
3224 * @param __pos Index of first character of substring.
3225 * @param __n Number of characters in substring.
3226 * @param __str String to compare against.
3227 * @return Integer < 0, 0, or > 0.
3229 * Form the substring of this string from the @a __n characters
3230 * starting at @a __pos. Returns an integer < 0 if the
3231 * substring is ordered before @a __str, 0 if their values are
3232 * equivalent, or > 0 if the substring is ordered after @a
3233 * __str. Determines the effective length rlen of the strings
3234 * to compare as the smallest of the length of the substring
3235 * and @a __str.size(). The function then compares the two
3236 * strings by calling
3237 * traits::compare(substring.data(),str.data(),rlen). If the
3238 * result of the comparison is nonzero returns it, otherwise
3239 * the shorter one is ordered first.
3241 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3243 compare(size_type __pos
, size_type __n
, const basic_string
& __str
) const
3245 _M_check(__pos
, "basic_string::compare");
3246 __n
= _M_limit(__pos
, __n
);
3247 const size_type __osize
= __str
.size();
3248 const size_type __len
= std::min(__n
, __osize
);
3249 int __r
= traits_type::compare(_M_data() + __pos
, __str
.data(), __len
);
3251 __r
= _S_compare(__n
, __osize
);
3256 * @brief Compare substring to a substring.
3257 * @param __pos1 Index of first character of substring.
3258 * @param __n1 Number of characters in substring.
3259 * @param __str String to compare against.
3260 * @param __pos2 Index of first character of substring of str.
3261 * @param __n2 Number of characters in substring of str.
3262 * @return Integer < 0, 0, or > 0.
3264 * Form the substring of this string from the @a __n1
3265 * characters starting at @a __pos1. Form the substring of @a
3266 * __str from the @a __n2 characters starting at @a __pos2.
3267 * Returns an integer < 0 if this substring is ordered before
3268 * the substring of @a __str, 0 if their values are equivalent,
3269 * or > 0 if this substring is ordered after the substring of
3270 * @a __str. Determines the effective length rlen of the
3271 * strings to compare as the smallest of the lengths of the
3272 * substrings. The function then compares the two strings by
3274 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
3275 * If the result of the comparison is nonzero returns it,
3276 * otherwise the shorter one is ordered first.
3278 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3280 compare(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
3281 size_type __pos2
, size_type __n2
= npos
) const
3283 _M_check(__pos1
, "basic_string::compare");
3284 __str
._M_check(__pos2
, "basic_string::compare");
3285 __n1
= _M_limit(__pos1
, __n1
);
3286 __n2
= __str
._M_limit(__pos2
, __n2
);
3287 const size_type __len
= std::min(__n1
, __n2
);
3288 int __r
= traits_type::compare(_M_data() + __pos1
,
3289 __str
.data() + __pos2
, __len
);
3291 __r
= _S_compare(__n1
, __n2
);
3296 * @brief Compare to a C string.
3297 * @param __s C string to compare against.
3298 * @return Integer < 0, 0, or > 0.
3300 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
3301 * their values are equivalent, or > 0 if this string is ordered after
3302 * @a __s. Determines the effective length rlen of the strings to
3303 * compare as the smallest of size() and the length of a string
3304 * constructed from @a __s. The function then compares the two strings
3305 * by calling traits::compare(data(),s,rlen). If the result of the
3306 * comparison is nonzero returns it, otherwise the shorter one is
3309 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3311 compare(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
3313 __glibcxx_requires_string(__s
);
3314 const size_type __size
= this->size();
3315 const size_type __osize
= traits_type::length(__s
);
3316 const size_type __len
= std::min(__size
, __osize
);
3317 int __r
= traits_type::compare(_M_data(), __s
, __len
);
3319 __r
= _S_compare(__size
, __osize
);
3323 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3324 // 5 String::compare specification questionable
3326 * @brief Compare substring to a C string.
3327 * @param __pos Index of first character of substring.
3328 * @param __n1 Number of characters in substring.
3329 * @param __s C string to compare against.
3330 * @return Integer < 0, 0, or > 0.
3332 * Form the substring of this string from the @a __n1
3333 * characters starting at @a pos. Returns an integer < 0 if
3334 * the substring is ordered before @a __s, 0 if their values
3335 * are equivalent, or > 0 if the substring is ordered after @a
3336 * __s. Determines the effective length rlen of the strings to
3337 * compare as the smallest of the length of the substring and
3338 * the length of a string constructed from @a __s. The
3339 * function then compares the two string by calling
3340 * traits::compare(substring.data(),__s,rlen). If the result of
3341 * the comparison is nonzero returns it, otherwise the shorter
3342 * one is ordered first.
3344 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3346 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
) const
3348 __glibcxx_requires_string(__s
);
3349 _M_check(__pos
, "basic_string::compare");
3350 __n1
= _M_limit(__pos
, __n1
);
3351 const size_type __osize
= traits_type::length(__s
);
3352 const size_type __len
= std::min(__n1
, __osize
);
3353 int __r
= traits_type::compare(_M_data() + __pos
, __s
, __len
);
3355 __r
= _S_compare(__n1
, __osize
);
3360 * @brief Compare substring against a character %array.
3361 * @param __pos Index of first character of substring.
3362 * @param __n1 Number of characters in substring.
3363 * @param __s character %array to compare against.
3364 * @param __n2 Number of characters of s.
3365 * @return Integer < 0, 0, or > 0.
3367 * Form the substring of this string from the @a __n1
3368 * characters starting at @a __pos. Form a string from the
3369 * first @a __n2 characters of @a __s. Returns an integer < 0
3370 * if this substring is ordered before the string from @a __s,
3371 * 0 if their values are equivalent, or > 0 if this substring
3372 * is ordered after the string from @a __s. Determines the
3373 * effective length rlen of the strings to compare as the
3374 * smallest of the length of the substring and @a __n2. The
3375 * function then compares the two strings by calling
3376 * traits::compare(substring.data(),s,rlen). If the result of
3377 * the comparison is nonzero returns it, otherwise the shorter
3378 * one is ordered first.
3380 * NB: s must have at least n2 characters, '\\0' has
3381 * no special meaning.
3383 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3385 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
,
3386 size_type __n2
) const
3388 __glibcxx_requires_string_len(__s
, __n2
);
3389 _M_check(__pos
, "basic_string::compare");
3390 __n1
= _M_limit(__pos
, __n1
);
3391 const size_type __len
= std::min(__n1
, __n2
);
3392 int __r
= traits_type::compare(_M_data() + __pos
, __s
, __len
);
3394 __r
= _S_compare(__n1
, __n2
);
3398 #if __cplusplus >= 202002L
3401 starts_with(basic_string_view
<_CharT
, _Traits
> __x
) const noexcept
3402 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
3406 starts_with(_CharT __x
) const noexcept
3407 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
3409 [[nodiscard
, __gnu__::__nonnull__
]]
3411 starts_with(const _CharT
* __x
) const noexcept
3412 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
3416 ends_with(basic_string_view
<_CharT
, _Traits
> __x
) const noexcept
3417 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
3421 ends_with(_CharT __x
) const noexcept
3422 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
3424 [[nodiscard
, __gnu__::__nonnull__
]]
3426 ends_with(const _CharT
* __x
) const noexcept
3427 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
3430 #if __cplusplus > 202002L
3433 contains(basic_string_view
<_CharT
, _Traits
> __x
) const noexcept
3434 { return __sv_type(this->data(), this->size()).contains(__x
); }
3438 contains(_CharT __x
) const noexcept
3439 { return __sv_type(this->data(), this->size()).contains(__x
); }
3441 [[nodiscard
, __gnu__::__nonnull__
]]
3443 contains(const _CharT
* __x
) const noexcept
3444 { return __sv_type(this->data(), this->size()).contains(__x
); }
3447 // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
3448 template<typename
, typename
, typename
> friend class basic_stringbuf
;
3450 _GLIBCXX_END_NAMESPACE_CXX11
3451 _GLIBCXX_END_NAMESPACE_VERSION
3453 #endif // _GLIBCXX_USE_CXX11_ABI
3455 namespace std
_GLIBCXX_VISIBILITY(default)
3457 _GLIBCXX_BEGIN_NAMESPACE_VERSION
3459 #if __cpp_deduction_guides >= 201606
3460 _GLIBCXX_BEGIN_NAMESPACE_CXX11
3461 template<typename _InputIterator
, typename _CharT
3462 = typename iterator_traits
<_InputIterator
>::value_type
,
3463 typename _Allocator
= allocator
<_CharT
>,
3464 typename
= _RequireInputIter
<_InputIterator
>,
3465 typename
= _RequireAllocator
<_Allocator
>>
3466 basic_string(_InputIterator
, _InputIterator
, _Allocator
= _Allocator())
3467 -> basic_string
<_CharT
, char_traits
<_CharT
>, _Allocator
>;
3469 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3470 // 3075. basic_string needs deduction guides from basic_string_view
3471 template<typename _CharT
, typename _Traits
,
3472 typename _Allocator
= allocator
<_CharT
>,
3473 typename
= _RequireAllocator
<_Allocator
>>
3474 basic_string(basic_string_view
<_CharT
, _Traits
>, const _Allocator
& = _Allocator())
3475 -> basic_string
<_CharT
, _Traits
, _Allocator
>;
3477 template<typename _CharT
, typename _Traits
,
3478 typename _Allocator
= allocator
<_CharT
>,
3479 typename
= _RequireAllocator
<_Allocator
>>
3480 basic_string(basic_string_view
<_CharT
, _Traits
>,
3481 typename basic_string
<_CharT
, _Traits
, _Allocator
>::size_type
,
3482 typename basic_string
<_CharT
, _Traits
, _Allocator
>::size_type
,
3483 const _Allocator
& = _Allocator())
3484 -> basic_string
<_CharT
, _Traits
, _Allocator
>;
3485 _GLIBCXX_END_NAMESPACE_CXX11
3488 template<typename _Str
>
3489 _GLIBCXX20_CONSTEXPR
3491 __str_concat(typename
_Str::value_type
const* __lhs
,
3492 typename
_Str::size_type __lhs_len
,
3493 typename
_Str::value_type
const* __rhs
,
3494 typename
_Str::size_type __rhs_len
,
3495 typename
_Str::allocator_type
const& __a
)
3497 typedef typename
_Str::allocator_type allocator_type
;
3498 typedef __gnu_cxx::__alloc_traits
<allocator_type
> _Alloc_traits
;
3499 _Str
__str(_Alloc_traits::_S_select_on_copy(__a
));
3500 __str
.reserve(__lhs_len
+ __rhs_len
);
3501 __str
.append(__lhs
, __lhs_len
);
3502 __str
.append(__rhs
, __rhs_len
);
3508 * @brief Concatenate two strings.
3509 * @param __lhs First string.
3510 * @param __rhs Last string.
3511 * @return New string with value of @a __lhs followed by @a __rhs.
3513 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3514 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3515 inline basic_string
<_CharT
, _Traits
, _Alloc
>
3516 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3517 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3519 typedef basic_string
<_CharT
, _Traits
, _Alloc
> _Str
;
3520 return std::__str_concat
<_Str
>(__lhs
.c_str(), __lhs
.size(),
3521 __rhs
.c_str(), __rhs
.size(),
3522 __lhs
.get_allocator());
3526 * @brief Concatenate C string and string.
3527 * @param __lhs First string.
3528 * @param __rhs Last string.
3529 * @return New string with value of @a __lhs followed by @a __rhs.
3531 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3532 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3533 inline basic_string
<_CharT
,_Traits
,_Alloc
>
3534 operator+(const _CharT
* __lhs
,
3535 const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
)
3537 __glibcxx_requires_string(__lhs
);
3538 typedef basic_string
<_CharT
, _Traits
, _Alloc
> _Str
;
3539 return std::__str_concat
<_Str
>(__lhs
, _Traits::length(__lhs
),
3540 __rhs
.c_str(), __rhs
.size(),
3541 __rhs
.get_allocator());
3545 * @brief Concatenate character and string.
3546 * @param __lhs First string.
3547 * @param __rhs Last string.
3548 * @return New string with @a __lhs followed by @a __rhs.
3550 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3551 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3552 inline basic_string
<_CharT
,_Traits
,_Alloc
>
3553 operator+(_CharT __lhs
, const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
)
3555 typedef basic_string
<_CharT
, _Traits
, _Alloc
> _Str
;
3556 return std::__str_concat
<_Str
>(__builtin_addressof(__lhs
), 1,
3557 __rhs
.c_str(), __rhs
.size(),
3558 __rhs
.get_allocator());
3562 * @brief Concatenate string and C string.
3563 * @param __lhs First string.
3564 * @param __rhs Last string.
3565 * @return New string with @a __lhs followed by @a __rhs.
3567 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3568 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3569 inline basic_string
<_CharT
, _Traits
, _Alloc
>
3570 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3571 const _CharT
* __rhs
)
3573 __glibcxx_requires_string(__rhs
);
3574 typedef basic_string
<_CharT
, _Traits
, _Alloc
> _Str
;
3575 return std::__str_concat
<_Str
>(__lhs
.c_str(), __lhs
.size(),
3576 __rhs
, _Traits::length(__rhs
),
3577 __lhs
.get_allocator());
3580 * @brief Concatenate string and character.
3581 * @param __lhs First string.
3582 * @param __rhs Last string.
3583 * @return New string with @a __lhs followed by @a __rhs.
3585 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3586 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3587 inline basic_string
<_CharT
, _Traits
, _Alloc
>
3588 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
, _CharT __rhs
)
3590 typedef basic_string
<_CharT
, _Traits
, _Alloc
> _Str
;
3591 return std::__str_concat
<_Str
>(__lhs
.c_str(), __lhs
.size(),
3592 __builtin_addressof(__rhs
), 1,
3593 __lhs
.get_allocator());
3596 #if __cplusplus >= 201103L
3597 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3598 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3599 inline basic_string
<_CharT
, _Traits
, _Alloc
>
3600 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
3601 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3602 { return std::move(__lhs
.append(__rhs
)); }
3604 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3605 _GLIBCXX20_CONSTEXPR
3606 inline basic_string
<_CharT
, _Traits
, _Alloc
>
3607 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3608 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
3609 { return std::move(__rhs
.insert(0, __lhs
)); }
3611 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3612 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3613 inline basic_string
<_CharT
, _Traits
, _Alloc
>
3614 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
3615 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
3617 #if _GLIBCXX_USE_CXX11_ABI
3618 using _Alloc_traits
= allocator_traits
<_Alloc
>;
3619 bool __use_rhs
= false;
3620 if _GLIBCXX17_CONSTEXPR (typename
_Alloc_traits::is_always_equal
{})
3622 else if (__lhs
.get_allocator() == __rhs
.get_allocator())
3627 const auto __size
= __lhs
.size() + __rhs
.size();
3628 if (__size
> __lhs
.capacity() && __size
<= __rhs
.capacity())
3629 return std::move(__rhs
.insert(0, __lhs
));
3631 return std::move(__lhs
.append(__rhs
));
3634 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3635 _GLIBCXX_NODISCARD _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3636 inline basic_string
<_CharT
, _Traits
, _Alloc
>
3637 operator+(const _CharT
* __lhs
,
3638 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
3639 { return std::move(__rhs
.insert(0, __lhs
)); }
3641 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3642 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3643 inline basic_string
<_CharT
, _Traits
, _Alloc
>
3644 operator+(_CharT __lhs
,
3645 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
3646 { return std::move(__rhs
.insert(0, 1, __lhs
)); }
3648 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3649 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3650 inline basic_string
<_CharT
, _Traits
, _Alloc
>
3651 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
3652 const _CharT
* __rhs
)
3653 { return std::move(__lhs
.append(__rhs
)); }
3655 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3656 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3657 inline basic_string
<_CharT
, _Traits
, _Alloc
>
3658 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
3660 { return std::move(__lhs
.append(1, __rhs
)); }
3665 * @brief Test equivalence of two strings.
3666 * @param __lhs First string.
3667 * @param __rhs Second string.
3668 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
3670 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3671 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3673 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3674 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3677 return __lhs
.size() == __rhs
.size()
3678 && !_Traits::compare(__lhs
.data(), __rhs
.data(), __lhs
.size());
3682 * @brief Test equivalence of string and C string.
3683 * @param __lhs String.
3684 * @param __rhs C string.
3685 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
3687 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3688 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3690 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3691 const _CharT
* __rhs
)
3693 return __lhs
.size() == _Traits::length(__rhs
)
3694 && !_Traits::compare(__lhs
.data(), __rhs
, __lhs
.size());
3697 #if __cpp_lib_three_way_comparison
3699 * @brief Three-way comparison of a string and a C string.
3700 * @param __lhs A string.
3701 * @param __rhs A null-terminated string.
3702 * @return A value indicating whether `__lhs` is less than, equal to,
3703 * greater than, or incomparable with `__rhs`.
3705 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3708 operator<=>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3709 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
) noexcept
3710 -> decltype(__detail::__char_traits_cmp_cat
<_Traits
>(0))
3711 { return __detail::__char_traits_cmp_cat
<_Traits
>(__lhs
.compare(__rhs
)); }
3714 * @brief Three-way comparison of a string and a C string.
3715 * @param __lhs A string.
3716 * @param __rhs A null-terminated string.
3717 * @return A value indicating whether `__lhs` is less than, equal to,
3718 * greater than, or incomparable with `__rhs`.
3720 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3723 operator<=>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3724 const _CharT
* __rhs
) noexcept
3725 -> decltype(__detail::__char_traits_cmp_cat
<_Traits
>(0))
3726 { return __detail::__char_traits_cmp_cat
<_Traits
>(__lhs
.compare(__rhs
)); }
3729 * @brief Test equivalence of C string and string.
3730 * @param __lhs C string.
3731 * @param __rhs String.
3732 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
3734 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3737 operator==(const _CharT
* __lhs
,
3738 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3739 { return __rhs
== __lhs
; }
3743 * @brief Test difference of two strings.
3744 * @param __lhs First string.
3745 * @param __rhs Second string.
3746 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
3748 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3751 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3752 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3754 { return !(__lhs
== __rhs
); }
3757 * @brief Test difference of C string and string.
3758 * @param __lhs C string.
3759 * @param __rhs String.
3760 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
3762 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3765 operator!=(const _CharT
* __lhs
,
3766 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3767 { return !(__rhs
== __lhs
); }
3770 * @brief Test difference of string and C string.
3771 * @param __lhs String.
3772 * @param __rhs C string.
3773 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
3775 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3778 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3779 const _CharT
* __rhs
)
3780 { return !(__lhs
== __rhs
); }
3784 * @brief Test if string precedes string.
3785 * @param __lhs First string.
3786 * @param __rhs Second string.
3787 * @return True if @a __lhs precedes @a __rhs. False otherwise.
3789 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3792 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3793 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3795 { return __lhs
.compare(__rhs
) < 0; }
3798 * @brief Test if string precedes C string.
3799 * @param __lhs String.
3800 * @param __rhs C string.
3801 * @return True if @a __lhs precedes @a __rhs. False otherwise.
3803 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3806 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3807 const _CharT
* __rhs
)
3808 { return __lhs
.compare(__rhs
) < 0; }
3811 * @brief Test if C string precedes string.
3812 * @param __lhs C string.
3813 * @param __rhs String.
3814 * @return True if @a __lhs precedes @a __rhs. False otherwise.
3816 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3819 operator<(const _CharT
* __lhs
,
3820 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3821 { return __rhs
.compare(__lhs
) > 0; }
3825 * @brief Test if string follows string.
3826 * @param __lhs First string.
3827 * @param __rhs Second string.
3828 * @return True if @a __lhs follows @a __rhs. False otherwise.
3830 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3833 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3834 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3836 { return __lhs
.compare(__rhs
) > 0; }
3839 * @brief Test if string follows C string.
3840 * @param __lhs String.
3841 * @param __rhs C string.
3842 * @return True if @a __lhs follows @a __rhs. False otherwise.
3844 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3847 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3848 const _CharT
* __rhs
)
3849 { return __lhs
.compare(__rhs
) > 0; }
3852 * @brief Test if C string follows string.
3853 * @param __lhs C string.
3854 * @param __rhs String.
3855 * @return True if @a __lhs follows @a __rhs. False otherwise.
3857 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3860 operator>(const _CharT
* __lhs
,
3861 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3862 { return __rhs
.compare(__lhs
) < 0; }
3866 * @brief Test if string doesn't follow string.
3867 * @param __lhs First string.
3868 * @param __rhs Second string.
3869 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
3871 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3874 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3875 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3877 { return __lhs
.compare(__rhs
) <= 0; }
3880 * @brief Test if string doesn't follow C string.
3881 * @param __lhs String.
3882 * @param __rhs C string.
3883 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
3885 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3888 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3889 const _CharT
* __rhs
)
3890 { return __lhs
.compare(__rhs
) <= 0; }
3893 * @brief Test if C string doesn't follow string.
3894 * @param __lhs C string.
3895 * @param __rhs String.
3896 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
3898 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3901 operator<=(const _CharT
* __lhs
,
3902 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3903 { return __rhs
.compare(__lhs
) >= 0; }
3907 * @brief Test if string doesn't precede string.
3908 * @param __lhs First string.
3909 * @param __rhs Second string.
3910 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
3912 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3915 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3916 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3918 { return __lhs
.compare(__rhs
) >= 0; }
3921 * @brief Test if string doesn't precede C string.
3922 * @param __lhs String.
3923 * @param __rhs C string.
3924 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
3926 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3929 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3930 const _CharT
* __rhs
)
3931 { return __lhs
.compare(__rhs
) >= 0; }
3934 * @brief Test if C string doesn't precede string.
3935 * @param __lhs C string.
3936 * @param __rhs String.
3937 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
3939 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3942 operator>=(const _CharT
* __lhs
,
3943 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3944 { return __rhs
.compare(__lhs
) <= 0; }
3945 #endif // three-way comparison
3948 * @brief Swap contents of two strings.
3949 * @param __lhs First string.
3950 * @param __rhs Second string.
3952 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
3954 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3955 _GLIBCXX20_CONSTEXPR
3957 swap(basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
3958 basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
3959 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs
.swap(__rhs
)))
3960 { __lhs
.swap(__rhs
); }
3964 * @brief Read stream into a string.
3965 * @param __is Input stream.
3966 * @param __str Buffer to store into.
3967 * @return Reference to the input stream.
3969 * Stores characters from @a __is into @a __str until whitespace is
3970 * found, the end of the stream is encountered, or str.max_size()
3971 * is reached. If is.width() is non-zero, that is the limit on the
3972 * number of characters stored into @a __str. Any previous
3973 * contents of @a __str are erased.
3975 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3976 basic_istream
<_CharT
, _Traits
>&
3977 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
3978 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
);
3981 basic_istream
<char>&
3982 operator>>(basic_istream
<char>& __is
, basic_string
<char>& __str
);
3985 * @brief Write string to a stream.
3986 * @param __os Output stream.
3987 * @param __str String to write out.
3988 * @return Reference to the output stream.
3990 * Output characters of @a __str into os following the same rules as for
3991 * writing a C string.
3993 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3994 inline basic_ostream
<_CharT
, _Traits
>&
3995 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
3996 const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
3998 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3999 // 586. string inserter not a formatted function
4000 return __ostream_insert(__os
, __str
.data(), __str
.size());
4004 * @brief Read a line from stream into a string.
4005 * @param __is Input stream.
4006 * @param __str Buffer to store into.
4007 * @param __delim Character marking end of line.
4008 * @return Reference to the input stream.
4010 * Stores characters from @a __is into @a __str until @a __delim is
4011 * found, the end of the stream is encountered, or str.max_size()
4012 * is reached. Any previous contents of @a __str are erased. If
4013 * @a __delim is encountered, it is extracted but not stored into
4016 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4017 basic_istream
<_CharT
, _Traits
>&
4018 getline(basic_istream
<_CharT
, _Traits
>& __is
,
4019 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
);
4022 * @brief Read a line from stream into a string.
4023 * @param __is Input stream.
4024 * @param __str Buffer to store into.
4025 * @return Reference to the input stream.
4027 * Stores characters from is into @a __str until '\n' is
4028 * found, the end of the stream is encountered, or str.max_size()
4029 * is reached. Any previous contents of @a __str are erased. If
4030 * end of line is encountered, it is extracted but not stored into
4033 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4034 inline basic_istream
<_CharT
, _Traits
>&
4035 getline(basic_istream
<_CharT
, _Traits
>& __is
,
4036 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
4037 { return std::getline(__is
, __str
, __is
.widen('\n')); }
4039 #if __cplusplus >= 201103L
4040 /// Read a line from an rvalue stream into a string.
4041 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4042 inline basic_istream
<_CharT
, _Traits
>&
4043 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
4044 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
)
4045 { return std::getline(__is
, __str
, __delim
); }
4047 /// Read a line from an rvalue stream into a string.
4048 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4049 inline basic_istream
<_CharT
, _Traits
>&
4050 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
4051 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
4052 { return std::getline(__is
, __str
); }
4056 basic_istream
<char>&
4057 getline(basic_istream
<char>& __in
, basic_string
<char>& __str
,
4060 #ifdef _GLIBCXX_USE_WCHAR_T
4062 basic_istream
<wchar_t>&
4063 getline(basic_istream
<wchar_t>& __in
, basic_string
<wchar_t>& __str
,
4067 _GLIBCXX_END_NAMESPACE_VERSION
4070 #if __cplusplus >= 201103L
4072 #include <ext/string_conversions.h>
4073 #include <bits/charconv.h>
4075 namespace std
_GLIBCXX_VISIBILITY(default)
4077 _GLIBCXX_BEGIN_NAMESPACE_VERSION
4078 _GLIBCXX_BEGIN_NAMESPACE_CXX11
4080 #if _GLIBCXX_USE_C99_STDLIB
4081 // 21.4 Numeric Conversions [string.conversions].
4083 stoi(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
4084 { return __gnu_cxx::__stoa
<long, int>(&std::strtol
, "stoi", __str
.c_str(),
4088 stol(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
4089 { return __gnu_cxx::__stoa(&std::strtol
, "stol", __str
.c_str(),
4092 inline unsigned long
4093 stoul(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
4094 { return __gnu_cxx::__stoa(&std::strtoul
, "stoul", __str
.c_str(),
4098 stoll(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
4099 { return __gnu_cxx::__stoa(&std::strtoll
, "stoll", __str
.c_str(),
4102 inline unsigned long long
4103 stoull(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
4104 { return __gnu_cxx::__stoa(&std::strtoull
, "stoull", __str
.c_str(),
4107 // NB: strtof vs strtod.
4109 stof(const string
& __str
, size_t* __idx
= 0)
4110 { return __gnu_cxx::__stoa(&std::strtof
, "stof", __str
.c_str(), __idx
); }
4113 stod(const string
& __str
, size_t* __idx
= 0)
4114 { return __gnu_cxx::__stoa(&std::strtod
, "stod", __str
.c_str(), __idx
); }
4117 stold(const string
& __str
, size_t* __idx
= 0)
4118 { return __gnu_cxx::__stoa(&std::strtold
, "stold", __str
.c_str(), __idx
); }
4119 #endif // _GLIBCXX_USE_C99_STDLIB
4121 // DR 1261. Insufficent overloads for to_string / to_wstring
4125 to_string(int __val
)
4126 #if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32
4127 noexcept
// any 32-bit value fits in the SSO buffer
4130 const bool __neg
= __val
< 0;
4131 const unsigned __uval
= __neg
? (unsigned)~__val
+ 1u : __val
;
4132 const auto __len
= __detail::__to_chars_len(__uval
);
4133 string
__str(__neg
+ __len
, '-');
4134 __detail::__to_chars_10_impl(&__str
[__neg
], __len
, __uval
);
4140 to_string(unsigned __val
)
4141 #if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32
4142 noexcept
// any 32-bit value fits in the SSO buffer
4145 string
__str(__detail::__to_chars_len(__val
), '\0');
4146 __detail::__to_chars_10_impl(&__str
[0], __str
.size(), __val
);
4152 to_string(long __val
)
4153 #if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32
4154 noexcept
// any 32-bit value fits in the SSO buffer
4157 const bool __neg
= __val
< 0;
4158 const unsigned long __uval
= __neg
? (unsigned long)~__val
+ 1ul : __val
;
4159 const auto __len
= __detail::__to_chars_len(__uval
);
4160 string
__str(__neg
+ __len
, '-');
4161 __detail::__to_chars_10_impl(&__str
[__neg
], __len
, __uval
);
4167 to_string(unsigned long __val
)
4168 #if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32
4169 noexcept
// any 32-bit value fits in the SSO buffer
4172 string
__str(__detail::__to_chars_len(__val
), '\0');
4173 __detail::__to_chars_10_impl(&__str
[0], __str
.size(), __val
);
4179 to_string(long long __val
)
4181 const bool __neg
= __val
< 0;
4182 const unsigned long long __uval
4183 = __neg
? (unsigned long long)~__val
+ 1ull : __val
;
4184 const auto __len
= __detail::__to_chars_len(__uval
);
4185 string
__str(__neg
+ __len
, '-');
4186 __detail::__to_chars_10_impl(&__str
[__neg
], __len
, __uval
);
4192 to_string(unsigned long long __val
)
4194 string
__str(__detail::__to_chars_len(__val
), '\0');
4195 __detail::__to_chars_10_impl(&__str
[0], __str
.size(), __val
);
4199 #if _GLIBCXX_USE_C99_STDIO
4200 // NB: (v)snprintf vs sprintf.
4204 to_string(float __val
)
4207 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
4208 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
4214 to_string(double __val
)
4217 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
4218 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
4224 to_string(long double __val
)
4227 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
4228 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
4231 #endif // _GLIBCXX_USE_C99_STDIO
4233 #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
4235 stoi(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
4236 { return __gnu_cxx::__stoa
<long, int>(&std::wcstol
, "stoi", __str
.c_str(),
4240 stol(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
4241 { return __gnu_cxx::__stoa(&std::wcstol
, "stol", __str
.c_str(),
4244 inline unsigned long
4245 stoul(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
4246 { return __gnu_cxx::__stoa(&std::wcstoul
, "stoul", __str
.c_str(),
4250 stoll(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
4251 { return __gnu_cxx::__stoa(&std::wcstoll
, "stoll", __str
.c_str(),
4254 inline unsigned long long
4255 stoull(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
4256 { return __gnu_cxx::__stoa(&std::wcstoull
, "stoull", __str
.c_str(),
4259 // NB: wcstof vs wcstod.
4261 stof(const wstring
& __str
, size_t* __idx
= 0)
4262 { return __gnu_cxx::__stoa(&std::wcstof
, "stof", __str
.c_str(), __idx
); }
4265 stod(const wstring
& __str
, size_t* __idx
= 0)
4266 { return __gnu_cxx::__stoa(&std::wcstod
, "stod", __str
.c_str(), __idx
); }
4269 stold(const wstring
& __str
, size_t* __idx
= 0)
4270 { return __gnu_cxx::__stoa(&std::wcstold
, "stold", __str
.c_str(), __idx
); }
4272 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
4276 to_wstring(int __val
)
4277 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(int),
4282 to_wstring(unsigned __val
)
4283 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
4284 4 * sizeof(unsigned),
4289 to_wstring(long __val
)
4290 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(long),
4295 to_wstring(unsigned long __val
)
4296 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
4297 4 * sizeof(unsigned long),
4302 to_wstring(long long __val
)
4303 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
4304 4 * sizeof(long long),
4309 to_wstring(unsigned long long __val
)
4310 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
4311 4 * sizeof(unsigned long long),
4316 to_wstring(float __val
)
4319 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
4320 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
4326 to_wstring(double __val
)
4329 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
4330 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
4336 to_wstring(long double __val
)
4339 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
4340 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
4343 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
4344 #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
4346 _GLIBCXX_END_NAMESPACE_CXX11
4347 _GLIBCXX_END_NAMESPACE_VERSION
4352 #if __cplusplus >= 201103L
4354 #include <bits/functional_hash.h>
4356 namespace std
_GLIBCXX_VISIBILITY(default)
4358 _GLIBCXX_BEGIN_NAMESPACE_VERSION
4360 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4361 // 3705. Hashability shouldn't depend on basic_string's allocator
4363 template<typename _CharT
, typename _Alloc
,
4364 typename _StrT
= basic_string
<_CharT
, char_traits
<_CharT
>, _Alloc
>>
4365 struct __str_hash_base
4366 : public __hash_base
<size_t, _StrT
>
4370 operator()(const _StrT
& __s
) const noexcept
4371 { return _Hash_impl::hash(__s
.data(), __s
.length() * sizeof(_CharT
)); }
4374 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
4375 /// std::hash specialization for string.
4376 template<typename _Alloc
>
4377 struct hash
<basic_string
<char, char_traits
<char>, _Alloc
>>
4378 : public __str_hash_base
<char, _Alloc
>
4381 /// std::hash specialization for wstring.
4382 template<typename _Alloc
>
4383 struct hash
<basic_string
<wchar_t, char_traits
<wchar_t>, _Alloc
>>
4384 : public __str_hash_base
<wchar_t, _Alloc
>
4387 template<typename _Alloc
>
4388 struct __is_fast_hash
<hash
<basic_string
<wchar_t, char_traits
<wchar_t>,
4392 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
4394 #ifdef _GLIBCXX_USE_CHAR8_T
4395 /// std::hash specialization for u8string.
4396 template<typename _Alloc
>
4397 struct hash
<basic_string
<char8_t
, char_traits
<char8_t
>, _Alloc
>>
4398 : public __str_hash_base
<char8_t
, _Alloc
>
4402 /// std::hash specialization for u16string.
4403 template<typename _Alloc
>
4404 struct hash
<basic_string
<char16_t
, char_traits
<char16_t
>, _Alloc
>>
4405 : public __str_hash_base
<char16_t
, _Alloc
>
4408 /// std::hash specialization for u32string.
4409 template<typename _Alloc
>
4410 struct hash
<basic_string
<char32_t
, char_traits
<char32_t
>, _Alloc
>>
4411 : public __str_hash_base
<char32_t
, _Alloc
>
4414 #if ! _GLIBCXX_INLINE_VERSION
4415 // PR libstdc++/105907 - __is_fast_hash affects unordered container ABI.
4416 template<> struct __is_fast_hash
<hash
<string
>> : std::false_type
{ };
4417 template<> struct __is_fast_hash
<hash
<wstring
>> : std::false_type
{ };
4418 template<> struct __is_fast_hash
<hash
<u16string
>> : std::false_type
{ };
4419 template<> struct __is_fast_hash
<hash
<u32string
>> : std::false_type
{ };
4420 #ifdef _GLIBCXX_USE_CHAR8_T
4421 template<> struct __is_fast_hash
<hash
<u8string
>> : std::false_type
{ };
4424 // For versioned namespace, assume every std::hash<basic_string<>> is slow.
4425 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4426 struct __is_fast_hash
<hash
<basic_string
<_CharT
, _Traits
, _Alloc
>>>
4431 #if __cplusplus >= 201402L
4433 #define __cpp_lib_string_udls 201304L
4435 inline namespace literals
4437 inline namespace string_literals
4439 #pragma GCC diagnostic push
4440 #pragma GCC diagnostic ignored "-Wliteral-suffix"
4442 #if __cpp_lib_constexpr_string >= 201907L
4443 # define _GLIBCXX_STRING_CONSTEXPR constexpr
4445 # define _GLIBCXX_STRING_CONSTEXPR
4448 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
4449 inline basic_string
<char>
4450 operator""s(const char* __str
, size_t __len
)
4451 { return basic_string
<char>{__str
, __len
}; }
4453 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
4454 inline basic_string
<wchar_t>
4455 operator""s(const wchar_t* __str
, size_t __len
)
4456 { return basic_string
<wchar_t>{__str
, __len
}; }
4458 #ifdef _GLIBCXX_USE_CHAR8_T
4459 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
4460 inline basic_string
<char8_t
>
4461 operator""s(const char8_t
* __str
, size_t __len
)
4462 { return basic_string
<char8_t
>{__str
, __len
}; }
4465 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
4466 inline basic_string
<char16_t
>
4467 operator""s(const char16_t
* __str
, size_t __len
)
4468 { return basic_string
<char16_t
>{__str
, __len
}; }
4470 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
4471 inline basic_string
<char32_t
>
4472 operator""s(const char32_t
* __str
, size_t __len
)
4473 { return basic_string
<char32_t
>{__str
, __len
}; }
4475 #undef _GLIBCXX_STRING_CONSTEXPR
4476 #pragma GCC diagnostic pop
4477 } // inline namespace string_literals
4478 } // inline namespace literals
4480 #if __cplusplus >= 201703L
4481 namespace __detail::__variant
4483 template<typename
> struct _Never_valueless_alt
; // see <variant>
4485 // Provide the strong exception-safety guarantee when emplacing a
4486 // basic_string into a variant, but only if moving the string cannot throw.
4487 template<typename _Tp
, typename _Traits
, typename _Alloc
>
4488 struct _Never_valueless_alt
<std::basic_string
<_Tp
, _Traits
, _Alloc
>>
4490 is_nothrow_move_constructible
<std::basic_string
<_Tp
, _Traits
, _Alloc
>>,
4491 is_nothrow_move_assignable
<std::basic_string
<_Tp
, _Traits
, _Alloc
>>
4494 } // namespace __detail::__variant
4498 _GLIBCXX_END_NAMESPACE_VERSION
4503 #endif /* _BASIC_STRING_H */