1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997-2021 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
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/atomicity.h>
40 #include <ext/alloc_traits.h>
41 #include <debug/debug.h>
43 #if __cplusplus >= 201103L
44 #include <initializer_list>
47 #if __cplusplus >= 201703L
48 # include <string_view>
52 namespace std
_GLIBCXX_VISIBILITY(default)
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
56 #if _GLIBCXX_USE_CXX11_ABI
57 _GLIBCXX_BEGIN_NAMESPACE_CXX11
59 * @class basic_string basic_string.h <string>
60 * @brief Managing sequences of characters and character-like objects.
65 * @tparam _CharT Type of character
66 * @tparam _Traits Traits for character type, defaults to
67 * char_traits<_CharT>.
68 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
70 * Meets the requirements of a <a href="tables.html#65">container</a>, a
71 * <a href="tables.html#66">reversible container</a>, and a
72 * <a href="tables.html#67">sequence</a>. Of the
73 * <a href="tables.html#68">optional sequence requirements</a>, only
74 * @c push_back, @c at, and @c %array access are supported.
76 template<typename _CharT
, typename _Traits
, typename _Alloc
>
79 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
80 rebind
<_CharT
>::other _Char_alloc_type
;
81 typedef __gnu_cxx::__alloc_traits
<_Char_alloc_type
> _Alloc_traits
;
85 typedef _Traits traits_type
;
86 typedef typename
_Traits::char_type value_type
;
87 typedef _Char_alloc_type allocator_type
;
88 typedef typename
_Alloc_traits::size_type size_type
;
89 typedef typename
_Alloc_traits::difference_type difference_type
;
90 typedef typename
_Alloc_traits::reference reference
;
91 typedef typename
_Alloc_traits::const_reference const_reference
;
92 typedef typename
_Alloc_traits::pointer pointer
;
93 typedef typename
_Alloc_traits::const_pointer const_pointer
;
94 typedef __gnu_cxx::__normal_iterator
<pointer
, basic_string
> iterator
;
95 typedef __gnu_cxx::__normal_iterator
<const_pointer
, basic_string
>
97 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
98 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
100 /// Value returned by various member functions when they fail.
101 static const size_type npos
= static_cast<size_type
>(-1);
104 // type used for positions in insert, erase etc.
105 #if __cplusplus < 201103L
106 typedef iterator __const_iterator
;
108 typedef const_iterator __const_iterator
;
112 #if __cplusplus >= 201703L
113 // A helper type for avoiding boiler-plate.
114 typedef basic_string_view
<_CharT
, _Traits
> __sv_type
;
116 template<typename _Tp
, typename _Res
>
117 using _If_sv
= enable_if_t
<
118 __and_
<is_convertible
<const _Tp
&, __sv_type
>,
119 __not_
<is_convertible
<const _Tp
*, const basic_string
*>>,
120 __not_
<is_convertible
<const _Tp
&, const _CharT
*>>>::value
,
123 // Allows an implicit conversion to __sv_type.
125 _S_to_string_view(__sv_type __svt
) noexcept
128 // Wraps a string_view by explicit conversion and thus
129 // allows to add an internal constructor that does not
130 // participate in overload resolution when a string_view
134 explicit __sv_wrapper(__sv_type __sv
) noexcept
: _M_sv(__sv
) { }
139 * @brief Only internally used: Construct string from a string view
141 * @param __svw string view wrapper.
142 * @param __a Allocator to use.
145 basic_string(__sv_wrapper __svw
, const _Alloc
& __a
)
146 : basic_string(__svw
._M_sv
.data(), __svw
._M_sv
.size(), __a
) { }
149 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
150 struct _Alloc_hider
: allocator_type
// TODO check __is_final
152 #if __cplusplus < 201103L
153 _Alloc_hider(pointer __dat
, const _Alloc
& __a
= _Alloc())
154 : allocator_type(__a
), _M_p(__dat
) { }
156 _Alloc_hider(pointer __dat
, const _Alloc
& __a
)
157 : allocator_type(__a
), _M_p(__dat
) { }
159 _Alloc_hider(pointer __dat
, _Alloc
&& __a
= _Alloc())
160 : allocator_type(std::move(__a
)), _M_p(__dat
) { }
163 pointer _M_p
; // The actual data.
166 _Alloc_hider _M_dataplus
;
167 size_type _M_string_length
;
169 enum { _S_local_capacity
= 15 / sizeof(_CharT
) };
173 _CharT _M_local_buf
[_S_local_capacity
+ 1];
174 size_type _M_allocated_capacity
;
179 { _M_dataplus
._M_p
= __p
; }
182 _M_length(size_type __length
)
183 { _M_string_length
= __length
; }
187 { return _M_dataplus
._M_p
; }
192 #if __cplusplus >= 201103L
193 return std::pointer_traits
<pointer
>::pointer_to(*_M_local_buf
);
195 return pointer(_M_local_buf
);
200 _M_local_data() const
202 #if __cplusplus >= 201103L
203 return std::pointer_traits
<const_pointer
>::pointer_to(*_M_local_buf
);
205 return const_pointer(_M_local_buf
);
210 _M_capacity(size_type __capacity
)
211 { _M_allocated_capacity
= __capacity
; }
214 _M_set_length(size_type __n
)
217 traits_type::assign(_M_data()[__n
], _CharT());
222 { return _M_data() == _M_local_data(); }
226 _M_create(size_type
&, size_type
);
232 _M_destroy(_M_allocated_capacity
);
236 _M_destroy(size_type __size
) throw()
237 { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size
+ 1); }
239 // _M_construct_aux is used to implement the 21.3.1 para 15 which
240 // requires special behaviour if _InIterator is an integral type
241 template<typename _InIterator
>
243 _M_construct_aux(_InIterator __beg
, _InIterator __end
,
246 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
247 _M_construct(__beg
, __end
, _Tag());
250 // _GLIBCXX_RESOLVE_LIB_DEFECTS
251 // 438. Ambiguity in the "do the right thing" clause
252 template<typename _Integer
>
254 _M_construct_aux(_Integer __beg
, _Integer __end
, std::__true_type
)
255 { _M_construct_aux_2(static_cast<size_type
>(__beg
), __end
); }
258 _M_construct_aux_2(size_type __req
, _CharT __c
)
259 { _M_construct(__req
, __c
); }
261 template<typename _InIterator
>
263 _M_construct(_InIterator __beg
, _InIterator __end
)
265 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
266 _M_construct_aux(__beg
, __end
, _Integral());
269 // For Input Iterators, used in istreambuf_iterators, etc.
270 template<typename _InIterator
>
272 _M_construct(_InIterator __beg
, _InIterator __end
,
273 std::input_iterator_tag
);
275 // For forward_iterators up to random_access_iterators, used for
276 // string::iterator, _CharT*, etc.
277 template<typename _FwdIterator
>
279 _M_construct(_FwdIterator __beg
, _FwdIterator __end
,
280 std::forward_iterator_tag
);
283 _M_construct(size_type __req
, _CharT __c
);
287 { return _M_dataplus
; }
289 const allocator_type
&
290 _M_get_allocator() const
291 { return _M_dataplus
; }
295 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
296 // The explicit instantiations in misc-inst.cc require this due to
297 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
298 template<typename _Tp
, bool _Requires
=
299 !__are_same
<_Tp
, _CharT
*>::__value
300 && !__are_same
<_Tp
, const _CharT
*>::__value
301 && !__are_same
<_Tp
, iterator
>::__value
302 && !__are_same
<_Tp
, const_iterator
>::__value
>
303 struct __enable_if_not_native_iterator
304 { typedef basic_string
& __type
; };
305 template<typename _Tp
>
306 struct __enable_if_not_native_iterator
<_Tp
, false> { };
310 _M_check(size_type __pos
, const char* __s
) const
312 if (__pos
> this->size())
313 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
314 "this->size() (which is %zu)"),
315 __s
, __pos
, this->size());
320 _M_check_length(size_type __n1
, size_type __n2
, const char* __s
) const
322 if (this->max_size() - (this->size() - __n1
) < __n2
)
323 __throw_length_error(__N(__s
));
327 // NB: _M_limit doesn't check for a bad __pos value.
329 _M_limit(size_type __pos
, size_type __off
) const _GLIBCXX_NOEXCEPT
331 const bool __testoff
= __off
< this->size() - __pos
;
332 return __testoff
? __off
: this->size() - __pos
;
335 // True if _Rep and source do not overlap.
337 _M_disjunct(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
339 return (less
<const _CharT
*>()(__s
, _M_data())
340 || less
<const _CharT
*>()(_M_data() + this->size(), __s
));
343 // When __n = 1 way faster than the general multichar
344 // traits_type::copy/move/assign.
346 _S_copy(_CharT
* __d
, const _CharT
* __s
, size_type __n
)
349 traits_type::assign(*__d
, *__s
);
351 traits_type::copy(__d
, __s
, __n
);
355 _S_move(_CharT
* __d
, const _CharT
* __s
, size_type __n
)
358 traits_type::assign(*__d
, *__s
);
360 traits_type::move(__d
, __s
, __n
);
364 _S_assign(_CharT
* __d
, size_type __n
, _CharT __c
)
367 traits_type::assign(*__d
, __c
);
369 traits_type::assign(__d
, __n
, __c
);
372 // _S_copy_chars is a separate template to permit specialization
373 // to optimize for the common case of pointers as iterators.
374 template<class _Iterator
>
376 _S_copy_chars(_CharT
* __p
, _Iterator __k1
, _Iterator __k2
)
378 for (; __k1
!= __k2
; ++__k1
, (void)++__p
)
379 traits_type::assign(*__p
, *__k1
); // These types are off.
383 _S_copy_chars(_CharT
* __p
, iterator __k1
, iterator __k2
) _GLIBCXX_NOEXCEPT
384 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
387 _S_copy_chars(_CharT
* __p
, const_iterator __k1
, const_iterator __k2
)
389 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
392 _S_copy_chars(_CharT
* __p
, _CharT
* __k1
, _CharT
* __k2
) _GLIBCXX_NOEXCEPT
393 { _S_copy(__p
, __k1
, __k2
- __k1
); }
396 _S_copy_chars(_CharT
* __p
, const _CharT
* __k1
, const _CharT
* __k2
)
398 { _S_copy(__p
, __k1
, __k2
- __k1
); }
401 _S_compare(size_type __n1
, size_type __n2
) _GLIBCXX_NOEXCEPT
403 const difference_type __d
= difference_type(__n1
- __n2
);
405 if (__d
> __gnu_cxx::__numeric_traits
<int>::__max
)
406 return __gnu_cxx::__numeric_traits
<int>::__max
;
407 else if (__d
< __gnu_cxx::__numeric_traits
<int>::__min
)
408 return __gnu_cxx::__numeric_traits
<int>::__min
;
414 _M_assign(const basic_string
&);
417 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
421 _M_erase(size_type __pos
, size_type __n
);
424 // Construct/copy/destroy:
425 // NB: We overload ctors in some cases instead of using default
426 // arguments, per 17.4.4.4 para. 2 item 2.
429 * @brief Default constructor creates an empty string.
432 _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible
<_Alloc
>::value
)
433 : _M_dataplus(_M_local_data())
434 { _M_set_length(0); }
437 * @brief Construct an empty string using allocator @a a.
440 basic_string(const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
441 : _M_dataplus(_M_local_data(), __a
)
442 { _M_set_length(0); }
445 * @brief Construct string with copy of value of @a __str.
446 * @param __str Source string.
448 basic_string(const basic_string
& __str
)
449 : _M_dataplus(_M_local_data(),
450 _Alloc_traits::_S_select_on_copy(__str
._M_get_allocator()))
451 { _M_construct(__str
._M_data(), __str
._M_data() + __str
.length()); }
453 // _GLIBCXX_RESOLVE_LIB_DEFECTS
454 // 2583. no way to supply an allocator for basic_string(str, pos)
456 * @brief Construct string as copy of a substring.
457 * @param __str Source string.
458 * @param __pos Index of first character to copy from.
459 * @param __a Allocator to use.
461 basic_string(const basic_string
& __str
, size_type __pos
,
462 const _Alloc
& __a
= _Alloc())
463 : _M_dataplus(_M_local_data(), __a
)
465 const _CharT
* __start
= __str
._M_data()
466 + __str
._M_check(__pos
, "basic_string::basic_string");
467 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, npos
));
471 * @brief Construct string as copy of a substring.
472 * @param __str Source string.
473 * @param __pos Index of first character to copy from.
474 * @param __n Number of characters to copy.
476 basic_string(const basic_string
& __str
, size_type __pos
,
478 : _M_dataplus(_M_local_data())
480 const _CharT
* __start
= __str
._M_data()
481 + __str
._M_check(__pos
, "basic_string::basic_string");
482 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, __n
));
486 * @brief Construct string as copy of a substring.
487 * @param __str Source string.
488 * @param __pos Index of first character to copy from.
489 * @param __n Number of characters to copy.
490 * @param __a Allocator to use.
492 basic_string(const basic_string
& __str
, size_type __pos
,
493 size_type __n
, const _Alloc
& __a
)
494 : _M_dataplus(_M_local_data(), __a
)
496 const _CharT
* __start
497 = __str
._M_data() + __str
._M_check(__pos
, "string::string");
498 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, __n
));
502 * @brief Construct string initialized by a character %array.
503 * @param __s Source character %array.
504 * @param __n Number of characters to copy.
505 * @param __a Allocator to use (default is default allocator).
507 * NB: @a __s must have at least @a __n characters, '\\0'
508 * has no special meaning.
510 basic_string(const _CharT
* __s
, size_type __n
,
511 const _Alloc
& __a
= _Alloc())
512 : _M_dataplus(_M_local_data(), __a
)
513 { _M_construct(__s
, __s
+ __n
); }
516 * @brief Construct string as copy of a C string.
517 * @param __s Source C string.
518 * @param __a Allocator to use (default is default allocator).
520 #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
521 // _GLIBCXX_RESOLVE_LIB_DEFECTS
522 // 3076. basic_string CTAD ambiguity
523 template<typename
= _RequireAllocator
<_Alloc
>>
525 basic_string(const _CharT
* __s
, const _Alloc
& __a
= _Alloc())
526 : _M_dataplus(_M_local_data(), __a
)
527 { _M_construct(__s
, __s
? __s
+ traits_type::length(__s
) : __s
+npos
); }
530 * @brief Construct string as multiple characters.
531 * @param __n Number of characters.
532 * @param __c Character to use.
533 * @param __a Allocator to use (default is default allocator).
535 #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
536 // _GLIBCXX_RESOLVE_LIB_DEFECTS
537 // 3076. basic_string CTAD ambiguity
538 template<typename
= _RequireAllocator
<_Alloc
>>
540 basic_string(size_type __n
, _CharT __c
, const _Alloc
& __a
= _Alloc())
541 : _M_dataplus(_M_local_data(), __a
)
542 { _M_construct(__n
, __c
); }
544 #if __cplusplus >= 201103L
546 * @brief Move construct string.
547 * @param __str Source string.
549 * The newly-created string contains the exact contents of @a __str.
550 * @a __str is a valid, but unspecified string.
552 basic_string(basic_string
&& __str
) noexcept
553 : _M_dataplus(_M_local_data(), std::move(__str
._M_get_allocator()))
555 if (__str
._M_is_local())
557 traits_type::copy(_M_local_buf
, __str
._M_local_buf
,
558 _S_local_capacity
+ 1);
562 _M_data(__str
._M_data());
563 _M_capacity(__str
._M_allocated_capacity
);
566 // Must use _M_length() here not _M_set_length() because
567 // basic_stringbuf relies on writing into unallocated capacity so
568 // we mess up the contents if we put a '\0' in the string.
569 _M_length(__str
.length());
570 __str
._M_data(__str
._M_local_data());
571 __str
._M_set_length(0);
575 * @brief Construct string from an initializer %list.
576 * @param __l std::initializer_list of characters.
577 * @param __a Allocator to use (default is default allocator).
579 basic_string(initializer_list
<_CharT
> __l
, const _Alloc
& __a
= _Alloc())
580 : _M_dataplus(_M_local_data(), __a
)
581 { _M_construct(__l
.begin(), __l
.end()); }
583 basic_string(const basic_string
& __str
, const _Alloc
& __a
)
584 : _M_dataplus(_M_local_data(), __a
)
585 { _M_construct(__str
.begin(), __str
.end()); }
587 basic_string(basic_string
&& __str
, const _Alloc
& __a
)
588 noexcept(_Alloc_traits::_S_always_equal())
589 : _M_dataplus(_M_local_data(), __a
)
591 if (__str
._M_is_local())
593 traits_type::copy(_M_local_buf
, __str
._M_local_buf
,
594 _S_local_capacity
+ 1);
595 _M_length(__str
.length());
596 __str
._M_set_length(0);
598 else if (_Alloc_traits::_S_always_equal()
599 || __str
.get_allocator() == __a
)
601 _M_data(__str
._M_data());
602 _M_length(__str
.length());
603 _M_capacity(__str
._M_allocated_capacity
);
604 __str
._M_data(__str
._M_local_buf
);
605 __str
._M_set_length(0);
608 _M_construct(__str
.begin(), __str
.end());
614 * @brief Construct string as copy of a range.
615 * @param __beg Start of range.
616 * @param __end End of range.
617 * @param __a Allocator to use (default is default allocator).
619 #if __cplusplus >= 201103L
620 template<typename _InputIterator
,
621 typename
= std::_RequireInputIter
<_InputIterator
>>
623 template<typename _InputIterator
>
625 basic_string(_InputIterator __beg
, _InputIterator __end
,
626 const _Alloc
& __a
= _Alloc())
627 : _M_dataplus(_M_local_data(), __a
)
628 { _M_construct(__beg
, __end
); }
630 #if __cplusplus >= 201703L
632 * @brief Construct string from a substring of a string_view.
633 * @param __t Source object convertible to string view.
634 * @param __pos The index of the first character to copy from __t.
635 * @param __n The number of characters to copy from __t.
636 * @param __a Allocator to use.
638 template<typename _Tp
, typename
= _If_sv
<_Tp
, void>>
639 basic_string(const _Tp
& __t
, size_type __pos
, size_type __n
,
640 const _Alloc
& __a
= _Alloc())
641 : basic_string(_S_to_string_view(__t
).substr(__pos
, __n
), __a
) { }
644 * @brief Construct string from a string_view.
645 * @param __t Source object convertible to string view.
646 * @param __a Allocator to use (default is default allocator).
648 template<typename _Tp
, typename
= _If_sv
<_Tp
, void>>
650 basic_string(const _Tp
& __t
, const _Alloc
& __a
= _Alloc())
651 : basic_string(__sv_wrapper(_S_to_string_view(__t
)), __a
) { }
655 * @brief Destroy the string instance.
661 * @brief Assign the value of @a str to this string.
662 * @param __str Source string.
665 operator=(const basic_string
& __str
)
667 return this->assign(__str
);
671 * @brief Copy contents of @a s into this string.
672 * @param __s Source null-terminated string.
675 operator=(const _CharT
* __s
)
676 { return this->assign(__s
); }
679 * @brief Set value to string of length 1.
680 * @param __c Source character.
682 * Assigning to a character makes this string length 1 and
683 * (*this)[0] == @a c.
686 operator=(_CharT __c
)
688 this->assign(1, __c
);
692 #if __cplusplus >= 201103L
694 * @brief Move assign the value of @a str to this string.
695 * @param __str Source string.
697 * The contents of @a str are moved into this string (without copying).
698 * @a str is a valid, but unspecified string.
700 // _GLIBCXX_RESOLVE_LIB_DEFECTS
701 // 2063. Contradictory requirements for string move assignment
703 operator=(basic_string
&& __str
)
704 noexcept(_Alloc_traits::_S_nothrow_move())
706 if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
707 && !_Alloc_traits::_S_always_equal()
708 && _M_get_allocator() != __str
._M_get_allocator())
710 // Destroy existing storage before replacing allocator.
711 _M_destroy(_M_allocated_capacity
);
712 _M_data(_M_local_data());
715 // Replace allocator if POCMA is true.
716 std::__alloc_on_move(_M_get_allocator(), __str
._M_get_allocator());
718 if (__str
._M_is_local())
720 // We've always got room for a short string, just copy it
721 // (unless this is a self-move, because that would violate the
722 // char_traits::copy precondition that the ranges don't overlap).
723 if (__builtin_expect(std::__addressof(__str
) != this, true))
726 this->_S_copy(_M_data(), __str
._M_data(), __str
.size());
727 _M_set_length(__str
.size());
730 else if (_Alloc_traits::_S_propagate_on_move_assign()
731 || _Alloc_traits::_S_always_equal()
732 || _M_get_allocator() == __str
._M_get_allocator())
734 // Just move the allocated pointer, our allocator can free it.
735 pointer __data
= nullptr;
736 size_type __capacity
;
739 if (_Alloc_traits::_S_always_equal())
741 // __str can reuse our existing storage.
743 __capacity
= _M_allocated_capacity
;
745 else // __str can't use it, so free it.
746 _M_destroy(_M_allocated_capacity
);
749 _M_data(__str
._M_data());
750 _M_length(__str
.length());
751 _M_capacity(__str
._M_allocated_capacity
);
754 __str
._M_data(__data
);
755 __str
._M_capacity(__capacity
);
758 __str
._M_data(__str
._M_local_buf
);
760 else // Need to do a deep copy
767 * @brief Set value to string constructed from initializer %list.
768 * @param __l std::initializer_list.
771 operator=(initializer_list
<_CharT
> __l
)
773 this->assign(__l
.begin(), __l
.size());
778 #if __cplusplus >= 201703L
780 * @brief Set value to string constructed from a string_view.
781 * @param __svt An object convertible to string_view.
783 template<typename _Tp
>
784 _If_sv
<_Tp
, basic_string
&>
785 operator=(const _Tp
& __svt
)
786 { return this->assign(__svt
); }
789 * @brief Convert to a string_view.
790 * @return A string_view.
792 operator __sv_type() const noexcept
793 { return __sv_type(data(), size()); }
798 * Returns a read/write iterator that points to the first character in
802 begin() _GLIBCXX_NOEXCEPT
803 { return iterator(_M_data()); }
806 * Returns a read-only (constant) iterator that points to the first
807 * character in the %string.
810 begin() const _GLIBCXX_NOEXCEPT
811 { return const_iterator(_M_data()); }
814 * Returns a read/write iterator that points one past the last
815 * character in the %string.
818 end() _GLIBCXX_NOEXCEPT
819 { return iterator(_M_data() + this->size()); }
822 * Returns a read-only (constant) iterator that points one past the
823 * last character in the %string.
826 end() const _GLIBCXX_NOEXCEPT
827 { return const_iterator(_M_data() + this->size()); }
830 * Returns a read/write reverse iterator that points to the last
831 * character in the %string. Iteration is done in reverse element
835 rbegin() _GLIBCXX_NOEXCEPT
836 { return reverse_iterator(this->end()); }
839 * Returns a read-only (constant) reverse iterator that points
840 * to the last character in the %string. Iteration is done in
841 * reverse element order.
843 const_reverse_iterator
844 rbegin() const _GLIBCXX_NOEXCEPT
845 { return const_reverse_iterator(this->end()); }
848 * Returns a read/write reverse iterator that points to one before the
849 * first character in the %string. Iteration is done in reverse
853 rend() _GLIBCXX_NOEXCEPT
854 { return reverse_iterator(this->begin()); }
857 * Returns a read-only (constant) reverse iterator that points
858 * to one before the first character in the %string. Iteration
859 * is done in reverse element order.
861 const_reverse_iterator
862 rend() const _GLIBCXX_NOEXCEPT
863 { return const_reverse_iterator(this->begin()); }
865 #if __cplusplus >= 201103L
867 * Returns a read-only (constant) iterator that points to the first
868 * character in the %string.
871 cbegin() const noexcept
872 { return const_iterator(this->_M_data()); }
875 * Returns a read-only (constant) iterator that points one past the
876 * last character in the %string.
879 cend() const noexcept
880 { return const_iterator(this->_M_data() + this->size()); }
883 * Returns a read-only (constant) reverse iterator that points
884 * to the last character in the %string. Iteration is done in
885 * reverse element order.
887 const_reverse_iterator
888 crbegin() const noexcept
889 { return const_reverse_iterator(this->end()); }
892 * Returns a read-only (constant) reverse iterator that points
893 * to one before the first character in the %string. Iteration
894 * is done in reverse element order.
896 const_reverse_iterator
897 crend() const noexcept
898 { return const_reverse_iterator(this->begin()); }
903 /// Returns the number of characters in the string, not including any
904 /// null-termination.
906 size() const _GLIBCXX_NOEXCEPT
907 { return _M_string_length
; }
909 /// Returns the number of characters in the string, not including any
910 /// null-termination.
912 length() const _GLIBCXX_NOEXCEPT
913 { return _M_string_length
; }
915 /// Returns the size() of the largest possible %string.
917 max_size() const _GLIBCXX_NOEXCEPT
918 { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
921 * @brief Resizes the %string to the specified number of characters.
922 * @param __n Number of characters the %string should contain.
923 * @param __c Character to fill any new elements.
925 * This function will %resize the %string to the specified
926 * number of characters. If the number is smaller than the
927 * %string's current size the %string is truncated, otherwise
928 * the %string is extended and new elements are %set to @a __c.
931 resize(size_type __n
, _CharT __c
);
934 * @brief Resizes the %string to the specified number of characters.
935 * @param __n Number of characters the %string should contain.
937 * This function will resize the %string to the specified length. If
938 * the new size is smaller than the %string's current size the %string
939 * is truncated, otherwise the %string is extended and new characters
940 * are default-constructed. For basic types such as char, this means
944 resize(size_type __n
)
945 { this->resize(__n
, _CharT()); }
947 #if __cplusplus >= 201103L
948 #pragma GCC diagnostic push
949 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
950 /// A non-binding request to reduce capacity() to size().
952 shrink_to_fit() noexcept
954 #pragma GCC diagnostic pop
958 * Returns the total number of characters that the %string can hold
959 * before needing to allocate more memory.
962 capacity() const _GLIBCXX_NOEXCEPT
964 return _M_is_local() ? size_type(_S_local_capacity
)
965 : _M_allocated_capacity
;
969 * @brief Attempt to preallocate enough memory for specified number of
971 * @param __res_arg Number of characters required.
972 * @throw std::length_error If @a __res_arg exceeds @c max_size().
974 * This function attempts to reserve enough memory for the
975 * %string to hold the specified number of characters. If the
976 * number requested is more than max_size(), length_error is
979 * The advantage of this function is that if optimal code is a
980 * necessity and the user can determine the string length that will be
981 * required, the user can reserve the memory in %advance, and thus
982 * prevent a possible reallocation of memory and copying of %string
986 reserve(size_type __res_arg
);
989 * Equivalent to shrink_to_fit().
991 #if __cplusplus > 201703L
992 [[deprecated("use shrink_to_fit() instead")]]
998 * Erases the string, making it empty.
1001 clear() _GLIBCXX_NOEXCEPT
1002 { _M_set_length(0); }
1005 * Returns true if the %string is empty. Equivalent to
1006 * <code>*this == ""</code>.
1008 _GLIBCXX_NODISCARD
bool
1009 empty() const _GLIBCXX_NOEXCEPT
1010 { return this->size() == 0; }
1014 * @brief Subscript access to the data contained in the %string.
1015 * @param __pos The index of the character to access.
1016 * @return Read-only (constant) reference to the character.
1018 * This operator allows for easy, array-style, data access.
1019 * Note that data access with this operator is unchecked and
1020 * out_of_range lookups are not defined. (For checked lookups
1024 operator[] (size_type __pos
) const _GLIBCXX_NOEXCEPT
1026 __glibcxx_assert(__pos
<= size());
1027 return _M_data()[__pos
];
1031 * @brief Subscript access to the data contained in the %string.
1032 * @param __pos The index of the character to access.
1033 * @return Read/write reference to the character.
1035 * This operator allows for easy, array-style, data access.
1036 * Note that data access with this operator is unchecked and
1037 * out_of_range lookups are not defined. (For checked lookups
1041 operator[](size_type __pos
)
1043 // Allow pos == size() both in C++98 mode, as v3 extension,
1044 // and in C++11 mode.
1045 __glibcxx_assert(__pos
<= size());
1046 // In pedantic mode be strict in C++98 mode.
1047 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus
>= 201103L || __pos
< size());
1048 return _M_data()[__pos
];
1052 * @brief Provides access to the data contained in the %string.
1053 * @param __n The index of the character to access.
1054 * @return Read-only (const) reference to the character.
1055 * @throw std::out_of_range If @a n is an invalid index.
1057 * This function provides for safer data access. The parameter is
1058 * first checked that it is in the range of the string. The function
1059 * throws out_of_range if the check fails.
1062 at(size_type __n
) const
1064 if (__n
>= this->size())
1065 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1066 "(which is %zu) >= this->size() "
1069 return _M_data()[__n
];
1073 * @brief Provides access to the data contained in the %string.
1074 * @param __n The index of the character to access.
1075 * @return Read/write reference to the character.
1076 * @throw std::out_of_range If @a n is an invalid index.
1078 * This function provides for safer data access. The parameter is
1079 * first checked that it is in the range of the string. The function
1080 * throws out_of_range if the check fails.
1086 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1087 "(which is %zu) >= this->size() "
1090 return _M_data()[__n
];
1093 #if __cplusplus >= 201103L
1095 * Returns a read/write reference to the data at the first
1096 * element of the %string.
1101 __glibcxx_assert(!empty());
1102 return operator[](0);
1106 * Returns a read-only (constant) reference to the data at the first
1107 * element of the %string.
1110 front() const noexcept
1112 __glibcxx_assert(!empty());
1113 return operator[](0);
1117 * Returns a read/write reference to the data at the last
1118 * element of the %string.
1123 __glibcxx_assert(!empty());
1124 return operator[](this->size() - 1);
1128 * Returns a read-only (constant) reference to the data at the
1129 * last element of the %string.
1132 back() const noexcept
1134 __glibcxx_assert(!empty());
1135 return operator[](this->size() - 1);
1141 * @brief Append a string to this string.
1142 * @param __str The string to append.
1143 * @return Reference to this string.
1146 operator+=(const basic_string
& __str
)
1147 { return this->append(__str
); }
1150 * @brief Append a C string.
1151 * @param __s The C string to append.
1152 * @return Reference to this string.
1155 operator+=(const _CharT
* __s
)
1156 { return this->append(__s
); }
1159 * @brief Append a character.
1160 * @param __c The character to append.
1161 * @return Reference to this string.
1164 operator+=(_CharT __c
)
1166 this->push_back(__c
);
1170 #if __cplusplus >= 201103L
1172 * @brief Append an initializer_list of characters.
1173 * @param __l The initializer_list of characters to be appended.
1174 * @return Reference to this string.
1177 operator+=(initializer_list
<_CharT
> __l
)
1178 { return this->append(__l
.begin(), __l
.size()); }
1181 #if __cplusplus >= 201703L
1183 * @brief Append a string_view.
1184 * @param __svt An object convertible to string_view to be appended.
1185 * @return Reference to this string.
1187 template<typename _Tp
>
1188 _If_sv
<_Tp
, basic_string
&>
1189 operator+=(const _Tp
& __svt
)
1190 { return this->append(__svt
); }
1194 * @brief Append a string to this string.
1195 * @param __str The string to append.
1196 * @return Reference to this string.
1199 append(const basic_string
& __str
)
1200 { return _M_append(__str
._M_data(), __str
.size()); }
1203 * @brief Append a substring.
1204 * @param __str The string to append.
1205 * @param __pos Index of the first character of str to append.
1206 * @param __n The number of characters to append.
1207 * @return Reference to this string.
1208 * @throw std::out_of_range if @a __pos is not a valid index.
1210 * This function appends @a __n characters from @a __str
1211 * starting at @a __pos to this string. If @a __n is is larger
1212 * than the number of available characters in @a __str, the
1213 * remainder of @a __str is appended.
1216 append(const basic_string
& __str
, size_type __pos
, size_type __n
= npos
)
1217 { return _M_append(__str
._M_data()
1218 + __str
._M_check(__pos
, "basic_string::append"),
1219 __str
._M_limit(__pos
, __n
)); }
1222 * @brief Append a C substring.
1223 * @param __s The C string to append.
1224 * @param __n The number of characters to append.
1225 * @return Reference to this string.
1228 append(const _CharT
* __s
, size_type __n
)
1230 __glibcxx_requires_string_len(__s
, __n
);
1231 _M_check_length(size_type(0), __n
, "basic_string::append");
1232 return _M_append(__s
, __n
);
1236 * @brief Append a C string.
1237 * @param __s The C string to append.
1238 * @return Reference to this string.
1241 append(const _CharT
* __s
)
1243 __glibcxx_requires_string(__s
);
1244 const size_type __n
= traits_type::length(__s
);
1245 _M_check_length(size_type(0), __n
, "basic_string::append");
1246 return _M_append(__s
, __n
);
1250 * @brief Append multiple characters.
1251 * @param __n The number of characters to append.
1252 * @param __c The character to use.
1253 * @return Reference to this string.
1255 * Appends __n copies of __c to this string.
1258 append(size_type __n
, _CharT __c
)
1259 { return _M_replace_aux(this->size(), size_type(0), __n
, __c
); }
1261 #if __cplusplus >= 201103L
1263 * @brief Append an initializer_list of characters.
1264 * @param __l The initializer_list of characters to append.
1265 * @return Reference to this string.
1268 append(initializer_list
<_CharT
> __l
)
1269 { return this->append(__l
.begin(), __l
.size()); }
1273 * @brief Append a range of characters.
1274 * @param __first Iterator referencing the first character to append.
1275 * @param __last Iterator marking the end of the range.
1276 * @return Reference to this string.
1278 * Appends characters in the range [__first,__last) to this string.
1280 #if __cplusplus >= 201103L
1281 template<class _InputIterator
,
1282 typename
= std::_RequireInputIter
<_InputIterator
>>
1284 template<class _InputIterator
>
1287 append(_InputIterator __first
, _InputIterator __last
)
1288 { return this->replace(end(), end(), __first
, __last
); }
1290 #if __cplusplus >= 201703L
1292 * @brief Append a string_view.
1293 * @param __svt An object convertible to string_view to be appended.
1294 * @return Reference to this string.
1296 template<typename _Tp
>
1297 _If_sv
<_Tp
, basic_string
&>
1298 append(const _Tp
& __svt
)
1300 __sv_type __sv
= __svt
;
1301 return this->append(__sv
.data(), __sv
.size());
1305 * @brief Append a range of characters from a string_view.
1306 * @param __svt An object convertible to string_view to be appended from.
1307 * @param __pos The position in the string_view to append from.
1308 * @param __n The number of characters to append from the string_view.
1309 * @return Reference to this string.
1311 template<typename _Tp
>
1312 _If_sv
<_Tp
, basic_string
&>
1313 append(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
1315 __sv_type __sv
= __svt
;
1316 return _M_append(__sv
.data()
1317 + std::__sv_check(__sv
.size(), __pos
, "basic_string::append"),
1318 std::__sv_limit(__sv
.size(), __pos
, __n
));
1323 * @brief Append a single character.
1324 * @param __c Character to append.
1327 push_back(_CharT __c
)
1329 const size_type __size
= this->size();
1330 if (__size
+ 1 > this->capacity())
1331 this->_M_mutate(__size
, size_type(0), 0, size_type(1));
1332 traits_type::assign(this->_M_data()[__size
], __c
);
1333 this->_M_set_length(__size
+ 1);
1337 * @brief Set value to contents of another string.
1338 * @param __str Source string to use.
1339 * @return Reference to this string.
1342 assign(const basic_string
& __str
)
1344 #if __cplusplus >= 201103L
1345 if (_Alloc_traits::_S_propagate_on_copy_assign())
1347 if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
1348 && _M_get_allocator() != __str
._M_get_allocator())
1350 // Propagating allocator cannot free existing storage so must
1351 // deallocate it before replacing current allocator.
1352 if (__str
.size() <= _S_local_capacity
)
1354 _M_destroy(_M_allocated_capacity
);
1355 _M_data(_M_local_data());
1360 const auto __len
= __str
.size();
1361 auto __alloc
= __str
._M_get_allocator();
1362 // If this allocation throws there are no effects:
1363 auto __ptr
= _Alloc_traits::allocate(__alloc
, __len
+ 1);
1364 _M_destroy(_M_allocated_capacity
);
1367 _M_set_length(__len
);
1370 std::__alloc_on_copy(_M_get_allocator(), __str
._M_get_allocator());
1373 this->_M_assign(__str
);
1377 #if __cplusplus >= 201103L
1379 * @brief Set value to contents of another string.
1380 * @param __str Source string to use.
1381 * @return Reference to this string.
1383 * This function sets this string to the exact contents of @a __str.
1384 * @a __str is a valid, but unspecified string.
1387 assign(basic_string
&& __str
)
1388 noexcept(_Alloc_traits::_S_nothrow_move())
1390 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1391 // 2063. Contradictory requirements for string move assignment
1392 return *this = std::move(__str
);
1397 * @brief Set value to a substring of a string.
1398 * @param __str The string to use.
1399 * @param __pos Index of the first character of str.
1400 * @param __n Number of characters to use.
1401 * @return Reference to this string.
1402 * @throw std::out_of_range if @a pos is not a valid index.
1404 * This function sets this string to the substring of @a __str
1405 * consisting of @a __n characters at @a __pos. If @a __n is
1406 * is larger than the number of available characters in @a
1407 * __str, the remainder of @a __str is used.
1410 assign(const basic_string
& __str
, size_type __pos
, size_type __n
= npos
)
1411 { return _M_replace(size_type(0), this->size(), __str
._M_data()
1412 + __str
._M_check(__pos
, "basic_string::assign"),
1413 __str
._M_limit(__pos
, __n
)); }
1416 * @brief Set value to a C substring.
1417 * @param __s The C string to use.
1418 * @param __n Number of characters to use.
1419 * @return Reference to this string.
1421 * This function sets the value of this string to the first @a __n
1422 * characters of @a __s. If @a __n is is larger than the number of
1423 * available characters in @a __s, the remainder of @a __s is used.
1426 assign(const _CharT
* __s
, size_type __n
)
1428 __glibcxx_requires_string_len(__s
, __n
);
1429 return _M_replace(size_type(0), this->size(), __s
, __n
);
1433 * @brief Set value to contents of a C string.
1434 * @param __s The C string to use.
1435 * @return Reference to this string.
1437 * This function sets the value of this string to the value of @a __s.
1438 * The data is copied, so there is no dependence on @a __s once the
1442 assign(const _CharT
* __s
)
1444 __glibcxx_requires_string(__s
);
1445 return _M_replace(size_type(0), this->size(), __s
,
1446 traits_type::length(__s
));
1450 * @brief Set value to multiple characters.
1451 * @param __n Length of the resulting string.
1452 * @param __c The character to use.
1453 * @return Reference to this string.
1455 * This function sets the value of this string to @a __n copies of
1459 assign(size_type __n
, _CharT __c
)
1460 { return _M_replace_aux(size_type(0), this->size(), __n
, __c
); }
1463 * @brief Set value to a range of characters.
1464 * @param __first Iterator referencing the first character to append.
1465 * @param __last Iterator marking the end of the range.
1466 * @return Reference to this string.
1468 * Sets value of string to characters in the range [__first,__last).
1470 #if __cplusplus >= 201103L
1471 template<class _InputIterator
,
1472 typename
= std::_RequireInputIter
<_InputIterator
>>
1474 template<class _InputIterator
>
1477 assign(_InputIterator __first
, _InputIterator __last
)
1478 { return this->replace(begin(), end(), __first
, __last
); }
1480 #if __cplusplus >= 201103L
1482 * @brief Set value to an initializer_list of characters.
1483 * @param __l The initializer_list of characters to assign.
1484 * @return Reference to this string.
1487 assign(initializer_list
<_CharT
> __l
)
1488 { return this->assign(__l
.begin(), __l
.size()); }
1491 #if __cplusplus >= 201703L
1493 * @brief Set value from a string_view.
1494 * @param __svt The source object convertible to string_view.
1495 * @return Reference to this string.
1497 template<typename _Tp
>
1498 _If_sv
<_Tp
, basic_string
&>
1499 assign(const _Tp
& __svt
)
1501 __sv_type __sv
= __svt
;
1502 return this->assign(__sv
.data(), __sv
.size());
1506 * @brief Set value from a range of characters in a string_view.
1507 * @param __svt The source object convertible to string_view.
1508 * @param __pos The position in the string_view to assign from.
1509 * @param __n The number of characters to assign.
1510 * @return Reference to this string.
1512 template<typename _Tp
>
1513 _If_sv
<_Tp
, basic_string
&>
1514 assign(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
1516 __sv_type __sv
= __svt
;
1517 return _M_replace(size_type(0), this->size(),
1519 + std::__sv_check(__sv
.size(), __pos
, "basic_string::assign"),
1520 std::__sv_limit(__sv
.size(), __pos
, __n
));
1524 #if __cplusplus >= 201103L
1526 * @brief Insert multiple characters.
1527 * @param __p Const_iterator referencing location in string to
1529 * @param __n Number of characters to insert
1530 * @param __c The character to insert.
1531 * @return Iterator referencing the first inserted char.
1532 * @throw std::length_error If new length exceeds @c max_size().
1534 * Inserts @a __n copies of character @a __c starting at the
1535 * position referenced by iterator @a __p. If adding
1536 * characters causes the length to exceed max_size(),
1537 * length_error is thrown. The value of the string doesn't
1538 * change if an error is thrown.
1541 insert(const_iterator __p
, size_type __n
, _CharT __c
)
1543 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1544 const size_type __pos
= __p
- begin();
1545 this->replace(__p
, __p
, __n
, __c
);
1546 return iterator(this->_M_data() + __pos
);
1550 * @brief Insert multiple characters.
1551 * @param __p Iterator referencing location in string to insert at.
1552 * @param __n Number of characters to insert
1553 * @param __c The character to insert.
1554 * @throw std::length_error If new length exceeds @c max_size().
1556 * Inserts @a __n copies of character @a __c starting at the
1557 * position referenced by iterator @a __p. If adding
1558 * characters causes the length to exceed max_size(),
1559 * length_error is thrown. The value of the string doesn't
1560 * change if an error is thrown.
1563 insert(iterator __p
, size_type __n
, _CharT __c
)
1564 { this->replace(__p
, __p
, __n
, __c
); }
1567 #if __cplusplus >= 201103L
1569 * @brief Insert a range of characters.
1570 * @param __p Const_iterator referencing location in string to
1572 * @param __beg Start of range.
1573 * @param __end End of range.
1574 * @return Iterator referencing the first inserted char.
1575 * @throw std::length_error If new length exceeds @c max_size().
1577 * Inserts characters in range [beg,end). If adding characters
1578 * causes the length to exceed max_size(), length_error is
1579 * thrown. The value of the string doesn't change if an error
1582 template<class _InputIterator
,
1583 typename
= std::_RequireInputIter
<_InputIterator
>>
1585 insert(const_iterator __p
, _InputIterator __beg
, _InputIterator __end
)
1587 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1588 const size_type __pos
= __p
- begin();
1589 this->replace(__p
, __p
, __beg
, __end
);
1590 return iterator(this->_M_data() + __pos
);
1594 * @brief Insert a range of characters.
1595 * @param __p Iterator referencing location in string to insert at.
1596 * @param __beg Start of range.
1597 * @param __end End of range.
1598 * @throw std::length_error If new length exceeds @c max_size().
1600 * Inserts characters in range [__beg,__end). If adding
1601 * characters causes the length to exceed max_size(),
1602 * length_error is thrown. The value of the string doesn't
1603 * change if an error is thrown.
1605 template<class _InputIterator
>
1607 insert(iterator __p
, _InputIterator __beg
, _InputIterator __end
)
1608 { this->replace(__p
, __p
, __beg
, __end
); }
1611 #if __cplusplus >= 201103L
1613 * @brief Insert an initializer_list of characters.
1614 * @param __p Iterator referencing location in string to insert at.
1615 * @param __l The initializer_list of characters to insert.
1616 * @throw std::length_error If new length exceeds @c max_size().
1619 insert(const_iterator __p
, initializer_list
<_CharT
> __l
)
1620 { return this->insert(__p
, __l
.begin(), __l
.end()); }
1622 #ifdef _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
1623 // See PR libstdc++/83328
1625 insert(iterator __p
, initializer_list
<_CharT
> __l
)
1627 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1628 this->insert(__p
- begin(), __l
.begin(), __l
.size());
1634 * @brief Insert value of a string.
1635 * @param __pos1 Position in string to insert at.
1636 * @param __str The string to insert.
1637 * @return Reference to this string.
1638 * @throw std::length_error If new length exceeds @c max_size().
1640 * Inserts value of @a __str starting at @a __pos1. If adding
1641 * characters causes the length to exceed max_size(),
1642 * length_error is thrown. The value of the string doesn't
1643 * change if an error is thrown.
1646 insert(size_type __pos1
, const basic_string
& __str
)
1647 { return this->replace(__pos1
, size_type(0),
1648 __str
._M_data(), __str
.size()); }
1651 * @brief Insert a substring.
1652 * @param __pos1 Position in string to insert at.
1653 * @param __str The string to insert.
1654 * @param __pos2 Start of characters in str to insert.
1655 * @param __n Number of characters to insert.
1656 * @return Reference to this string.
1657 * @throw std::length_error If new length exceeds @c max_size().
1658 * @throw std::out_of_range If @a pos1 > size() or
1659 * @a __pos2 > @a str.size().
1661 * Starting at @a pos1, insert @a __n character of @a __str
1662 * beginning with @a __pos2. If adding characters causes the
1663 * length to exceed max_size(), length_error is thrown. If @a
1664 * __pos1 is beyond the end of this string or @a __pos2 is
1665 * beyond the end of @a __str, out_of_range is thrown. The
1666 * value of the string doesn't change if an error is thrown.
1669 insert(size_type __pos1
, const basic_string
& __str
,
1670 size_type __pos2
, size_type __n
= npos
)
1671 { return this->replace(__pos1
, size_type(0), __str
._M_data()
1672 + __str
._M_check(__pos2
, "basic_string::insert"),
1673 __str
._M_limit(__pos2
, __n
)); }
1676 * @brief Insert a C substring.
1677 * @param __pos Position in string to insert at.
1678 * @param __s The C string to insert.
1679 * @param __n The number of characters to insert.
1680 * @return Reference to this string.
1681 * @throw std::length_error If new length exceeds @c max_size().
1682 * @throw std::out_of_range If @a __pos is beyond the end of this
1685 * Inserts the first @a __n characters of @a __s starting at @a
1686 * __pos. If adding characters causes the length to exceed
1687 * max_size(), length_error is thrown. If @a __pos is beyond
1688 * end(), out_of_range is thrown. The value of the string
1689 * doesn't change if an error is thrown.
1692 insert(size_type __pos
, const _CharT
* __s
, size_type __n
)
1693 { return this->replace(__pos
, size_type(0), __s
, __n
); }
1696 * @brief Insert a C string.
1697 * @param __pos Position in string to insert at.
1698 * @param __s The C string to insert.
1699 * @return Reference to this string.
1700 * @throw std::length_error If new length exceeds @c max_size().
1701 * @throw std::out_of_range If @a pos is beyond the end of this
1704 * Inserts the first @a n characters of @a __s starting at @a __pos. If
1705 * adding characters causes the length to exceed max_size(),
1706 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
1707 * thrown. The value of the string doesn't change if an error is
1711 insert(size_type __pos
, const _CharT
* __s
)
1713 __glibcxx_requires_string(__s
);
1714 return this->replace(__pos
, size_type(0), __s
,
1715 traits_type::length(__s
));
1719 * @brief Insert multiple characters.
1720 * @param __pos Index in string to insert at.
1721 * @param __n Number of characters to insert
1722 * @param __c The character to insert.
1723 * @return Reference to this string.
1724 * @throw std::length_error If new length exceeds @c max_size().
1725 * @throw std::out_of_range If @a __pos is beyond the end of this
1728 * Inserts @a __n copies of character @a __c starting at index
1729 * @a __pos. If adding characters causes the length to exceed
1730 * max_size(), length_error is thrown. If @a __pos > length(),
1731 * out_of_range is thrown. The value of the string doesn't
1732 * change if an error is thrown.
1735 insert(size_type __pos
, size_type __n
, _CharT __c
)
1736 { return _M_replace_aux(_M_check(__pos
, "basic_string::insert"),
1737 size_type(0), __n
, __c
); }
1740 * @brief Insert one character.
1741 * @param __p Iterator referencing position in string to insert at.
1742 * @param __c The character to insert.
1743 * @return Iterator referencing newly inserted char.
1744 * @throw std::length_error If new length exceeds @c max_size().
1746 * Inserts character @a __c at position referenced by @a __p.
1747 * If adding character causes the length to exceed max_size(),
1748 * length_error is thrown. If @a __p is beyond end of string,
1749 * out_of_range is thrown. The value of the string doesn't
1750 * change if an error is thrown.
1753 insert(__const_iterator __p
, _CharT __c
)
1755 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1756 const size_type __pos
= __p
- begin();
1757 _M_replace_aux(__pos
, size_type(0), size_type(1), __c
);
1758 return iterator(_M_data() + __pos
);
1761 #if __cplusplus >= 201703L
1763 * @brief Insert a string_view.
1764 * @param __pos Position in string to insert at.
1765 * @param __svt The object convertible to string_view to insert.
1766 * @return Reference to this string.
1768 template<typename _Tp
>
1769 _If_sv
<_Tp
, basic_string
&>
1770 insert(size_type __pos
, const _Tp
& __svt
)
1772 __sv_type __sv
= __svt
;
1773 return this->insert(__pos
, __sv
.data(), __sv
.size());
1777 * @brief Insert a string_view.
1778 * @param __pos1 Position in string to insert at.
1779 * @param __svt The object convertible to string_view to insert from.
1780 * @param __pos2 Start of characters in str to insert.
1781 * @param __n The number of characters to insert.
1782 * @return Reference to this string.
1784 template<typename _Tp
>
1785 _If_sv
<_Tp
, basic_string
&>
1786 insert(size_type __pos1
, const _Tp
& __svt
,
1787 size_type __pos2
, size_type __n
= npos
)
1789 __sv_type __sv
= __svt
;
1790 return this->replace(__pos1
, size_type(0),
1792 + std::__sv_check(__sv
.size(), __pos2
, "basic_string::insert"),
1793 std::__sv_limit(__sv
.size(), __pos2
, __n
));
1798 * @brief Remove characters.
1799 * @param __pos Index of first character to remove (default 0).
1800 * @param __n Number of characters to remove (default remainder).
1801 * @return Reference to this string.
1802 * @throw std::out_of_range If @a pos is beyond the end of this
1805 * Removes @a __n characters from this string starting at @a
1806 * __pos. The length of the string is reduced by @a __n. If
1807 * there are < @a __n characters to remove, the remainder of
1808 * the string is truncated. If @a __p is beyond end of string,
1809 * out_of_range is thrown. The value of the string doesn't
1810 * change if an error is thrown.
1813 erase(size_type __pos
= 0, size_type __n
= npos
)
1815 _M_check(__pos
, "basic_string::erase");
1817 this->_M_set_length(__pos
);
1819 this->_M_erase(__pos
, _M_limit(__pos
, __n
));
1824 * @brief Remove one character.
1825 * @param __position Iterator referencing the character to remove.
1826 * @return iterator referencing same location after removal.
1828 * Removes the character at @a __position from this string. The value
1829 * of the string doesn't change if an error is thrown.
1832 erase(__const_iterator __position
)
1834 _GLIBCXX_DEBUG_PEDASSERT(__position
>= begin()
1835 && __position
< end());
1836 const size_type __pos
= __position
- begin();
1837 this->_M_erase(__pos
, size_type(1));
1838 return iterator(_M_data() + __pos
);
1842 * @brief Remove a range of characters.
1843 * @param __first Iterator referencing the first character to remove.
1844 * @param __last Iterator referencing the end of the range.
1845 * @return Iterator referencing location of first after removal.
1847 * Removes the characters in the range [first,last) from this string.
1848 * The value of the string doesn't change if an error is thrown.
1851 erase(__const_iterator __first
, __const_iterator __last
)
1853 _GLIBCXX_DEBUG_PEDASSERT(__first
>= begin() && __first
<= __last
1854 && __last
<= end());
1855 const size_type __pos
= __first
- begin();
1856 if (__last
== end())
1857 this->_M_set_length(__pos
);
1859 this->_M_erase(__pos
, __last
- __first
);
1860 return iterator(this->_M_data() + __pos
);
1863 #if __cplusplus >= 201103L
1865 * @brief Remove the last character.
1867 * The string must be non-empty.
1872 __glibcxx_assert(!empty());
1873 _M_erase(size() - 1, 1);
1878 * @brief Replace characters with value from another string.
1879 * @param __pos Index of first character to replace.
1880 * @param __n Number of characters to be replaced.
1881 * @param __str String to insert.
1882 * @return Reference to this string.
1883 * @throw std::out_of_range If @a pos is beyond the end of this
1885 * @throw std::length_error If new length exceeds @c max_size().
1887 * Removes the characters in the range [__pos,__pos+__n) from
1888 * this string. In place, the value of @a __str is inserted.
1889 * If @a __pos is beyond end of string, out_of_range is thrown.
1890 * If the length of the result exceeds max_size(), length_error
1891 * is thrown. The value of the string doesn't change if an
1895 replace(size_type __pos
, size_type __n
, const basic_string
& __str
)
1896 { return this->replace(__pos
, __n
, __str
._M_data(), __str
.size()); }
1899 * @brief Replace characters with value from another string.
1900 * @param __pos1 Index of first character to replace.
1901 * @param __n1 Number of characters to be replaced.
1902 * @param __str String to insert.
1903 * @param __pos2 Index of first character of str to use.
1904 * @param __n2 Number of characters from str to use.
1905 * @return Reference to this string.
1906 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
1908 * @throw std::length_error If new length exceeds @c max_size().
1910 * Removes the characters in the range [__pos1,__pos1 + n) from this
1911 * string. In place, the value of @a __str is inserted. If @a __pos is
1912 * beyond end of string, out_of_range is thrown. If the length of the
1913 * result exceeds max_size(), length_error is thrown. The value of the
1914 * string doesn't change if an error is thrown.
1917 replace(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
1918 size_type __pos2
, size_type __n2
= npos
)
1919 { return this->replace(__pos1
, __n1
, __str
._M_data()
1920 + __str
._M_check(__pos2
, "basic_string::replace"),
1921 __str
._M_limit(__pos2
, __n2
)); }
1924 * @brief Replace characters with value of a C substring.
1925 * @param __pos Index of first character to replace.
1926 * @param __n1 Number of characters to be replaced.
1927 * @param __s C string to insert.
1928 * @param __n2 Number of characters from @a s to use.
1929 * @return Reference to this string.
1930 * @throw std::out_of_range If @a pos1 > size().
1931 * @throw std::length_error If new length exceeds @c max_size().
1933 * Removes the characters in the range [__pos,__pos + __n1)
1934 * from this string. In place, the first @a __n2 characters of
1935 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
1936 * @a __pos is beyond end of string, out_of_range is thrown. If
1937 * the length of result exceeds max_size(), length_error is
1938 * thrown. The value of the string doesn't change if an error
1942 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
1945 __glibcxx_requires_string_len(__s
, __n2
);
1946 return _M_replace(_M_check(__pos
, "basic_string::replace"),
1947 _M_limit(__pos
, __n1
), __s
, __n2
);
1951 * @brief Replace characters with value of a C string.
1952 * @param __pos Index of first character to replace.
1953 * @param __n1 Number of characters to be replaced.
1954 * @param __s C string to insert.
1955 * @return Reference to this string.
1956 * @throw std::out_of_range If @a pos > size().
1957 * @throw std::length_error If new length exceeds @c max_size().
1959 * Removes the characters in the range [__pos,__pos + __n1)
1960 * from this string. In place, the characters of @a __s are
1961 * inserted. If @a __pos is beyond end of string, out_of_range
1962 * is thrown. If the length of result exceeds max_size(),
1963 * length_error is thrown. The value of the string doesn't
1964 * change if an error is thrown.
1967 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
)
1969 __glibcxx_requires_string(__s
);
1970 return this->replace(__pos
, __n1
, __s
, traits_type::length(__s
));
1974 * @brief Replace characters with multiple characters.
1975 * @param __pos Index of first character to replace.
1976 * @param __n1 Number of characters to be replaced.
1977 * @param __n2 Number of characters to insert.
1978 * @param __c Character to insert.
1979 * @return Reference to this string.
1980 * @throw std::out_of_range If @a __pos > size().
1981 * @throw std::length_error If new length exceeds @c max_size().
1983 * Removes the characters in the range [pos,pos + n1) from this
1984 * string. In place, @a __n2 copies of @a __c are inserted.
1985 * If @a __pos is beyond end of string, out_of_range is thrown.
1986 * If the length of result exceeds max_size(), length_error is
1987 * thrown. The value of the string doesn't change if an error
1991 replace(size_type __pos
, size_type __n1
, size_type __n2
, _CharT __c
)
1992 { return _M_replace_aux(_M_check(__pos
, "basic_string::replace"),
1993 _M_limit(__pos
, __n1
), __n2
, __c
); }
1996 * @brief Replace range of characters with string.
1997 * @param __i1 Iterator referencing start of range to replace.
1998 * @param __i2 Iterator referencing end of range to replace.
1999 * @param __str String value to insert.
2000 * @return Reference to this string.
2001 * @throw std::length_error If new length exceeds @c max_size().
2003 * Removes the characters in the range [__i1,__i2). In place,
2004 * the value of @a __str is inserted. If the length of result
2005 * exceeds max_size(), length_error is thrown. The value of
2006 * the string doesn't change if an error is thrown.
2009 replace(__const_iterator __i1
, __const_iterator __i2
,
2010 const basic_string
& __str
)
2011 { return this->replace(__i1
, __i2
, __str
._M_data(), __str
.size()); }
2014 * @brief Replace range of characters with C substring.
2015 * @param __i1 Iterator referencing start of range to replace.
2016 * @param __i2 Iterator referencing end of range to replace.
2017 * @param __s C string value to insert.
2018 * @param __n Number of characters from s to insert.
2019 * @return Reference to this string.
2020 * @throw std::length_error If new length exceeds @c max_size().
2022 * Removes the characters in the range [__i1,__i2). In place,
2023 * the first @a __n characters of @a __s are inserted. If the
2024 * length of result exceeds max_size(), length_error is thrown.
2025 * The value of the string doesn't change if an error is
2029 replace(__const_iterator __i1
, __const_iterator __i2
,
2030 const _CharT
* __s
, size_type __n
)
2032 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2034 return this->replace(__i1
- begin(), __i2
- __i1
, __s
, __n
);
2038 * @brief Replace range of characters with C string.
2039 * @param __i1 Iterator referencing start of range to replace.
2040 * @param __i2 Iterator referencing end of range to replace.
2041 * @param __s C string value to insert.
2042 * @return Reference to this string.
2043 * @throw std::length_error If new length exceeds @c max_size().
2045 * Removes the characters in the range [__i1,__i2). In place,
2046 * the characters of @a __s are inserted. If the length of
2047 * result exceeds max_size(), length_error is thrown. The
2048 * value of the string doesn't change if an error is thrown.
2051 replace(__const_iterator __i1
, __const_iterator __i2
, const _CharT
* __s
)
2053 __glibcxx_requires_string(__s
);
2054 return this->replace(__i1
, __i2
, __s
, traits_type::length(__s
));
2058 * @brief Replace range of characters with multiple characters
2059 * @param __i1 Iterator referencing start of range to replace.
2060 * @param __i2 Iterator referencing end of range to replace.
2061 * @param __n Number of characters to insert.
2062 * @param __c Character to insert.
2063 * @return Reference to this string.
2064 * @throw std::length_error If new length exceeds @c max_size().
2066 * Removes the characters in the range [__i1,__i2). In place,
2067 * @a __n copies of @a __c are inserted. If the length of
2068 * result exceeds max_size(), length_error is thrown. The
2069 * value of the string doesn't change if an error is thrown.
2072 replace(__const_iterator __i1
, __const_iterator __i2
, size_type __n
,
2075 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2077 return _M_replace_aux(__i1
- begin(), __i2
- __i1
, __n
, __c
);
2081 * @brief Replace range of characters with range.
2082 * @param __i1 Iterator referencing start of range to replace.
2083 * @param __i2 Iterator referencing end of range to replace.
2084 * @param __k1 Iterator referencing start of range to insert.
2085 * @param __k2 Iterator referencing end of range to insert.
2086 * @return Reference to this string.
2087 * @throw std::length_error If new length exceeds @c max_size().
2089 * Removes the characters in the range [__i1,__i2). In place,
2090 * characters in the range [__k1,__k2) are inserted. If the
2091 * length of result exceeds max_size(), length_error is thrown.
2092 * The value of the string doesn't change if an error is
2095 #if __cplusplus >= 201103L
2096 template<class _InputIterator
,
2097 typename
= std::_RequireInputIter
<_InputIterator
>>
2099 replace(const_iterator __i1
, const_iterator __i2
,
2100 _InputIterator __k1
, _InputIterator __k2
)
2102 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2104 __glibcxx_requires_valid_range(__k1
, __k2
);
2105 return this->_M_replace_dispatch(__i1
, __i2
, __k1
, __k2
,
2106 std::__false_type());
2109 template<class _InputIterator
>
2110 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
2111 typename __enable_if_not_native_iterator
<_InputIterator
>::__type
2115 replace(iterator __i1
, iterator __i2
,
2116 _InputIterator __k1
, _InputIterator __k2
)
2118 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2120 __glibcxx_requires_valid_range(__k1
, __k2
);
2121 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
2122 return _M_replace_dispatch(__i1
, __i2
, __k1
, __k2
, _Integral());
2126 // Specializations for the common case of pointer and iterator:
2127 // useful to avoid the overhead of temporary buffering in _M_replace.
2129 replace(__const_iterator __i1
, __const_iterator __i2
,
2130 _CharT
* __k1
, _CharT
* __k2
)
2132 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2134 __glibcxx_requires_valid_range(__k1
, __k2
);
2135 return this->replace(__i1
- begin(), __i2
- __i1
,
2140 replace(__const_iterator __i1
, __const_iterator __i2
,
2141 const _CharT
* __k1
, const _CharT
* __k2
)
2143 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2145 __glibcxx_requires_valid_range(__k1
, __k2
);
2146 return this->replace(__i1
- begin(), __i2
- __i1
,
2151 replace(__const_iterator __i1
, __const_iterator __i2
,
2152 iterator __k1
, iterator __k2
)
2154 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2156 __glibcxx_requires_valid_range(__k1
, __k2
);
2157 return this->replace(__i1
- begin(), __i2
- __i1
,
2158 __k1
.base(), __k2
- __k1
);
2162 replace(__const_iterator __i1
, __const_iterator __i2
,
2163 const_iterator __k1
, const_iterator __k2
)
2165 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2167 __glibcxx_requires_valid_range(__k1
, __k2
);
2168 return this->replace(__i1
- begin(), __i2
- __i1
,
2169 __k1
.base(), __k2
- __k1
);
2172 #if __cplusplus >= 201103L
2174 * @brief Replace range of characters with initializer_list.
2175 * @param __i1 Iterator referencing start of range to replace.
2176 * @param __i2 Iterator referencing end of range to replace.
2177 * @param __l The initializer_list of characters to insert.
2178 * @return Reference to this string.
2179 * @throw std::length_error If new length exceeds @c max_size().
2181 * Removes the characters in the range [__i1,__i2). In place,
2182 * characters in the range [__k1,__k2) are inserted. If the
2183 * length of result exceeds max_size(), length_error is thrown.
2184 * The value of the string doesn't change if an error is
2187 basic_string
& replace(const_iterator __i1
, const_iterator __i2
,
2188 initializer_list
<_CharT
> __l
)
2189 { return this->replace(__i1
, __i2
, __l
.begin(), __l
.size()); }
2192 #if __cplusplus >= 201703L
2194 * @brief Replace range of characters with string_view.
2195 * @param __pos The position to replace at.
2196 * @param __n The number of characters to replace.
2197 * @param __svt The object convertible to string_view to insert.
2198 * @return Reference to this string.
2200 template<typename _Tp
>
2201 _If_sv
<_Tp
, basic_string
&>
2202 replace(size_type __pos
, size_type __n
, const _Tp
& __svt
)
2204 __sv_type __sv
= __svt
;
2205 return this->replace(__pos
, __n
, __sv
.data(), __sv
.size());
2209 * @brief Replace range of characters with string_view.
2210 * @param __pos1 The position to replace at.
2211 * @param __n1 The number of characters to replace.
2212 * @param __svt The object convertible to string_view to insert from.
2213 * @param __pos2 The position in the string_view to insert from.
2214 * @param __n2 The number of characters to insert.
2215 * @return Reference to this string.
2217 template<typename _Tp
>
2218 _If_sv
<_Tp
, basic_string
&>
2219 replace(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
2220 size_type __pos2
, size_type __n2
= npos
)
2222 __sv_type __sv
= __svt
;
2223 return this->replace(__pos1
, __n1
,
2225 + std::__sv_check(__sv
.size(), __pos2
, "basic_string::replace"),
2226 std::__sv_limit(__sv
.size(), __pos2
, __n2
));
2230 * @brief Replace range of characters with string_view.
2231 * @param __i1 An iterator referencing the start position
2233 * @param __i2 An iterator referencing the end position
2235 * @param __svt The object convertible to string_view to insert from.
2236 * @return Reference to this string.
2238 template<typename _Tp
>
2239 _If_sv
<_Tp
, basic_string
&>
2240 replace(const_iterator __i1
, const_iterator __i2
, const _Tp
& __svt
)
2242 __sv_type __sv
= __svt
;
2243 return this->replace(__i1
- begin(), __i2
- __i1
, __sv
);
2248 template<class _Integer
>
2250 _M_replace_dispatch(const_iterator __i1
, const_iterator __i2
,
2251 _Integer __n
, _Integer __val
, __true_type
)
2252 { return _M_replace_aux(__i1
- begin(), __i2
- __i1
, __n
, __val
); }
2254 template<class _InputIterator
>
2256 _M_replace_dispatch(const_iterator __i1
, const_iterator __i2
,
2257 _InputIterator __k1
, _InputIterator __k2
,
2261 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
2265 _M_replace(size_type __pos
, size_type __len1
, const _CharT
* __s
,
2266 const size_type __len2
);
2269 _M_append(const _CharT
* __s
, size_type __n
);
2274 * @brief Copy substring into C string.
2275 * @param __s C string to copy value into.
2276 * @param __n Number of characters to copy.
2277 * @param __pos Index of first character to copy.
2278 * @return Number of characters actually copied
2279 * @throw std::out_of_range If __pos > size().
2281 * Copies up to @a __n characters starting at @a __pos into the
2282 * C string @a __s. If @a __pos is %greater than size(),
2283 * out_of_range is thrown.
2286 copy(_CharT
* __s
, size_type __n
, size_type __pos
= 0) const;
2289 * @brief Swap contents with another string.
2290 * @param __s String to swap with.
2292 * Exchanges the contents of this string with that of @a __s in constant
2296 swap(basic_string
& __s
) _GLIBCXX_NOEXCEPT
;
2298 // String operations:
2300 * @brief Return const pointer to null-terminated contents.
2302 * This is a handle to internal data. Do not modify or dire things may
2306 c_str() const _GLIBCXX_NOEXCEPT
2307 { return _M_data(); }
2310 * @brief Return const pointer to contents.
2312 * This is a pointer to internal data. It is undefined to modify
2313 * the contents through the returned pointer. To get a pointer that
2314 * allows modifying the contents use @c &str[0] instead,
2315 * (or in C++17 the non-const @c str.data() overload).
2318 data() const _GLIBCXX_NOEXCEPT
2319 { return _M_data(); }
2321 #if __cplusplus >= 201703L
2323 * @brief Return non-const pointer to contents.
2325 * This is a pointer to the character sequence held by the string.
2326 * Modifying the characters in the sequence is allowed.
2330 { return _M_data(); }
2334 * @brief Return copy of allocator used to construct this string.
2337 get_allocator() const _GLIBCXX_NOEXCEPT
2338 { return _M_get_allocator(); }
2341 * @brief Find position of a C substring.
2342 * @param __s C string to locate.
2343 * @param __pos Index of character to search from.
2344 * @param __n Number of characters from @a s to search for.
2345 * @return Index of start of first occurrence.
2347 * Starting from @a __pos, searches forward for the first @a
2348 * __n characters in @a __s within this string. If found,
2349 * returns the index where it begins. If not found, returns
2353 find(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2357 * @brief Find position of a string.
2358 * @param __str String to locate.
2359 * @param __pos Index of character to search from (default 0).
2360 * @return Index of start of first occurrence.
2362 * Starting from @a __pos, searches forward for value of @a __str within
2363 * this string. If found, returns the index where it begins. If not
2364 * found, returns npos.
2367 find(const basic_string
& __str
, size_type __pos
= 0) const
2369 { return this->find(__str
.data(), __pos
, __str
.size()); }
2371 #if __cplusplus >= 201703L
2373 * @brief Find position of a string_view.
2374 * @param __svt The object convertible to string_view to locate.
2375 * @param __pos Index of character to search from (default 0).
2376 * @return Index of start of first occurrence.
2378 template<typename _Tp
>
2379 _If_sv
<_Tp
, size_type
>
2380 find(const _Tp
& __svt
, size_type __pos
= 0) const
2381 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2383 __sv_type __sv
= __svt
;
2384 return this->find(__sv
.data(), __pos
, __sv
.size());
2389 * @brief Find position of a C string.
2390 * @param __s C string to locate.
2391 * @param __pos Index of character to search from (default 0).
2392 * @return Index of start of first occurrence.
2394 * Starting from @a __pos, searches forward for the value of @a
2395 * __s within this string. If found, returns the index where
2396 * it begins. If not found, returns npos.
2399 find(const _CharT
* __s
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
2401 __glibcxx_requires_string(__s
);
2402 return this->find(__s
, __pos
, traits_type::length(__s
));
2406 * @brief Find position of a character.
2407 * @param __c Character to locate.
2408 * @param __pos Index of character to search from (default 0).
2409 * @return Index of first occurrence.
2411 * Starting from @a __pos, searches forward for @a __c within
2412 * this string. If found, returns the index where it was
2413 * found. If not found, returns npos.
2416 find(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
;
2419 * @brief Find last position of a string.
2420 * @param __str String to locate.
2421 * @param __pos Index of character to search back from (default end).
2422 * @return Index of start of last occurrence.
2424 * Starting from @a __pos, searches backward for value of @a
2425 * __str within this string. If found, returns the index where
2426 * it begins. If not found, returns npos.
2429 rfind(const basic_string
& __str
, size_type __pos
= npos
) const
2431 { return this->rfind(__str
.data(), __pos
, __str
.size()); }
2433 #if __cplusplus >= 201703L
2435 * @brief Find last position of a string_view.
2436 * @param __svt The object convertible to string_view to locate.
2437 * @param __pos Index of character to search back from (default end).
2438 * @return Index of start of last occurrence.
2440 template<typename _Tp
>
2441 _If_sv
<_Tp
, size_type
>
2442 rfind(const _Tp
& __svt
, size_type __pos
= npos
) const
2443 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2445 __sv_type __sv
= __svt
;
2446 return this->rfind(__sv
.data(), __pos
, __sv
.size());
2451 * @brief Find last position of a C substring.
2452 * @param __s C string to locate.
2453 * @param __pos Index of character to search back from.
2454 * @param __n Number of characters from s to search for.
2455 * @return Index of start of last occurrence.
2457 * Starting from @a __pos, searches backward for the first @a
2458 * __n characters in @a __s within this string. If found,
2459 * returns the index where it begins. If not found, returns
2463 rfind(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2467 * @brief Find last position of a C string.
2468 * @param __s C string to locate.
2469 * @param __pos Index of character to start search at (default end).
2470 * @return Index of start of last occurrence.
2472 * Starting from @a __pos, searches backward for the value of
2473 * @a __s within this string. If found, returns the index
2474 * where it begins. If not found, returns npos.
2477 rfind(const _CharT
* __s
, size_type __pos
= npos
) const
2479 __glibcxx_requires_string(__s
);
2480 return this->rfind(__s
, __pos
, traits_type::length(__s
));
2484 * @brief Find last position of a character.
2485 * @param __c Character to locate.
2486 * @param __pos Index of character to search back from (default end).
2487 * @return Index of last occurrence.
2489 * Starting from @a __pos, searches backward for @a __c within
2490 * this string. If found, returns the index where it was
2491 * found. If not found, returns npos.
2494 rfind(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
;
2497 * @brief Find position of a character of string.
2498 * @param __str String containing characters to locate.
2499 * @param __pos Index of character to search from (default 0).
2500 * @return Index of first occurrence.
2502 * Starting from @a __pos, searches forward for one of the
2503 * characters of @a __str within this string. If found,
2504 * returns the index where it was found. If not found, returns
2508 find_first_of(const basic_string
& __str
, size_type __pos
= 0) const
2510 { return this->find_first_of(__str
.data(), __pos
, __str
.size()); }
2512 #if __cplusplus >= 201703L
2514 * @brief Find position of a character of a string_view.
2515 * @param __svt An object convertible to string_view containing
2516 * characters to locate.
2517 * @param __pos Index of character to search from (default 0).
2518 * @return Index of first occurrence.
2520 template<typename _Tp
>
2521 _If_sv
<_Tp
, size_type
>
2522 find_first_of(const _Tp
& __svt
, size_type __pos
= 0) const
2523 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2525 __sv_type __sv
= __svt
;
2526 return this->find_first_of(__sv
.data(), __pos
, __sv
.size());
2531 * @brief Find position of a character of C substring.
2532 * @param __s String containing characters to locate.
2533 * @param __pos Index of character to search from.
2534 * @param __n Number of characters from s to search for.
2535 * @return Index of first occurrence.
2537 * Starting from @a __pos, searches forward for one of the
2538 * first @a __n characters of @a __s within this string. If
2539 * found, returns the index where it was found. If not found,
2543 find_first_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2547 * @brief Find position of a character of C string.
2548 * @param __s String containing characters to locate.
2549 * @param __pos Index of character to search from (default 0).
2550 * @return Index of first occurrence.
2552 * Starting from @a __pos, searches forward for one of the
2553 * characters of @a __s within this string. If found, returns
2554 * the index where it was found. If not found, returns npos.
2557 find_first_of(const _CharT
* __s
, size_type __pos
= 0) const
2560 __glibcxx_requires_string(__s
);
2561 return this->find_first_of(__s
, __pos
, traits_type::length(__s
));
2565 * @brief Find position of a character.
2566 * @param __c Character to locate.
2567 * @param __pos Index of character to search from (default 0).
2568 * @return Index of first occurrence.
2570 * Starting from @a __pos, searches forward for the character
2571 * @a __c within this string. If found, returns the index
2572 * where it was found. If not found, returns npos.
2574 * Note: equivalent to find(__c, __pos).
2577 find_first_of(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
2578 { return this->find(__c
, __pos
); }
2581 * @brief Find last position of a character of string.
2582 * @param __str String containing characters to locate.
2583 * @param __pos Index of character to search back from (default end).
2584 * @return Index of last occurrence.
2586 * Starting from @a __pos, searches backward for one of the
2587 * characters of @a __str within this string. If found,
2588 * returns the index where it was found. If not found, returns
2592 find_last_of(const basic_string
& __str
, size_type __pos
= npos
) const
2594 { return this->find_last_of(__str
.data(), __pos
, __str
.size()); }
2596 #if __cplusplus >= 201703L
2598 * @brief Find last position of a character of string.
2599 * @param __svt An object convertible to string_view containing
2600 * characters to locate.
2601 * @param __pos Index of character to search back from (default end).
2602 * @return Index of last occurrence.
2604 template<typename _Tp
>
2605 _If_sv
<_Tp
, size_type
>
2606 find_last_of(const _Tp
& __svt
, size_type __pos
= npos
) const
2607 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2609 __sv_type __sv
= __svt
;
2610 return this->find_last_of(__sv
.data(), __pos
, __sv
.size());
2615 * @brief Find last position of a character of C substring.
2616 * @param __s C string containing characters to locate.
2617 * @param __pos Index of character to search back from.
2618 * @param __n Number of characters from s to search for.
2619 * @return Index of last occurrence.
2621 * Starting from @a __pos, searches backward for one of the
2622 * first @a __n characters of @a __s within this string. If
2623 * found, returns the index where it was found. If not found,
2627 find_last_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2631 * @brief Find last position of a character of C string.
2632 * @param __s C string containing characters to locate.
2633 * @param __pos Index of character to search back from (default end).
2634 * @return Index of last occurrence.
2636 * Starting from @a __pos, searches backward for one of the
2637 * characters of @a __s within this string. If found, returns
2638 * the index where it was found. If not found, returns npos.
2641 find_last_of(const _CharT
* __s
, size_type __pos
= npos
) const
2644 __glibcxx_requires_string(__s
);
2645 return this->find_last_of(__s
, __pos
, traits_type::length(__s
));
2649 * @brief Find last position of a character.
2650 * @param __c Character to locate.
2651 * @param __pos Index of character to search back from (default end).
2652 * @return Index of last occurrence.
2654 * Starting from @a __pos, searches backward for @a __c within
2655 * this string. If found, returns the index where it was
2656 * found. If not found, returns npos.
2658 * Note: equivalent to rfind(__c, __pos).
2661 find_last_of(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
2662 { return this->rfind(__c
, __pos
); }
2665 * @brief Find position of a character not in string.
2666 * @param __str String containing characters to avoid.
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 character not contained
2671 * in @a __str within this string. If found, returns the index where it
2672 * was found. If not found, returns npos.
2675 find_first_not_of(const basic_string
& __str
, size_type __pos
= 0) const
2677 { return this->find_first_not_of(__str
.data(), __pos
, __str
.size()); }
2679 #if __cplusplus >= 201703L
2681 * @brief Find position of a character not in a string_view.
2682 * @param __svt A object convertible to string_view containing
2683 * characters to avoid.
2684 * @param __pos Index of character to search from (default 0).
2685 * @return Index of first occurrence.
2687 template<typename _Tp
>
2688 _If_sv
<_Tp
, size_type
>
2689 find_first_not_of(const _Tp
& __svt
, size_type __pos
= 0) const
2690 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2692 __sv_type __sv
= __svt
;
2693 return this->find_first_not_of(__sv
.data(), __pos
, __sv
.size());
2698 * @brief Find position of a character not in C substring.
2699 * @param __s C string containing characters to avoid.
2700 * @param __pos Index of character to search from.
2701 * @param __n Number of characters from __s to consider.
2702 * @return Index of first occurrence.
2704 * Starting from @a __pos, searches forward for a character not
2705 * contained in the first @a __n characters of @a __s within
2706 * this string. If found, returns the index where it was
2707 * found. If not found, returns npos.
2710 find_first_not_of(const _CharT
* __s
, size_type __pos
,
2711 size_type __n
) const _GLIBCXX_NOEXCEPT
;
2714 * @brief Find position of a character not in C string.
2715 * @param __s C string containing characters to avoid.
2716 * @param __pos Index of character to search from (default 0).
2717 * @return Index of first occurrence.
2719 * Starting from @a __pos, searches forward for a character not
2720 * contained in @a __s within this string. If found, returns
2721 * the index where it was found. If not found, returns npos.
2724 find_first_not_of(const _CharT
* __s
, size_type __pos
= 0) const
2727 __glibcxx_requires_string(__s
);
2728 return this->find_first_not_of(__s
, __pos
, traits_type::length(__s
));
2732 * @brief Find position of a different character.
2733 * @param __c Character to avoid.
2734 * @param __pos Index of character to search from (default 0).
2735 * @return Index of first occurrence.
2737 * Starting from @a __pos, searches forward for a character
2738 * other than @a __c within this string. If found, returns the
2739 * index where it was found. If not found, returns npos.
2742 find_first_not_of(_CharT __c
, size_type __pos
= 0) const
2746 * @brief Find last position of a character not in string.
2747 * @param __str String containing characters to avoid.
2748 * @param __pos Index of character to search back from (default end).
2749 * @return Index of last occurrence.
2751 * Starting from @a __pos, searches backward for a character
2752 * not contained in @a __str within this string. If found,
2753 * returns the index where it was found. If not found, returns
2757 find_last_not_of(const basic_string
& __str
, size_type __pos
= npos
) const
2759 { return this->find_last_not_of(__str
.data(), __pos
, __str
.size()); }
2761 #if __cplusplus >= 201703L
2763 * @brief Find last position of a character not in a string_view.
2764 * @param __svt An object convertible to string_view containing
2765 * characters to avoid.
2766 * @param __pos Index of character to search back from (default end).
2767 * @return Index of last occurrence.
2769 template<typename _Tp
>
2770 _If_sv
<_Tp
, size_type
>
2771 find_last_not_of(const _Tp
& __svt
, size_type __pos
= npos
) const
2772 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2774 __sv_type __sv
= __svt
;
2775 return this->find_last_not_of(__sv
.data(), __pos
, __sv
.size());
2780 * @brief Find last position of a character not in C substring.
2781 * @param __s C string containing characters to avoid.
2782 * @param __pos Index of character to search back from.
2783 * @param __n Number of characters from s to consider.
2784 * @return Index of last occurrence.
2786 * Starting from @a __pos, searches backward for a character not
2787 * contained in the first @a __n characters of @a __s within this string.
2788 * If found, returns the index where it was found. If not found,
2792 find_last_not_of(const _CharT
* __s
, size_type __pos
,
2793 size_type __n
) const _GLIBCXX_NOEXCEPT
;
2795 * @brief Find last position of a character not in C string.
2796 * @param __s C string containing characters to avoid.
2797 * @param __pos Index of character to search back from (default end).
2798 * @return Index of last occurrence.
2800 * Starting from @a __pos, searches backward for a character
2801 * not contained in @a __s within this string. If found,
2802 * returns the index where it was found. If not found, returns
2806 find_last_not_of(const _CharT
* __s
, size_type __pos
= npos
) const
2809 __glibcxx_requires_string(__s
);
2810 return this->find_last_not_of(__s
, __pos
, traits_type::length(__s
));
2814 * @brief Find last position of a different character.
2815 * @param __c Character to avoid.
2816 * @param __pos Index of character to search back from (default end).
2817 * @return Index of last occurrence.
2819 * Starting from @a __pos, searches backward for a character other than
2820 * @a __c within this string. If found, returns the index where it was
2821 * found. If not found, returns npos.
2824 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const
2828 * @brief Get a substring.
2829 * @param __pos Index of first character (default 0).
2830 * @param __n Number of characters in substring (default remainder).
2831 * @return The new string.
2832 * @throw std::out_of_range If __pos > size().
2834 * Construct and return a new string using the @a __n
2835 * characters starting at @a __pos. If the string is too
2836 * short, use the remainder of the characters. If @a __pos is
2837 * beyond the end of the string, out_of_range is thrown.
2840 substr(size_type __pos
= 0, size_type __n
= npos
) const
2841 { return basic_string(*this,
2842 _M_check(__pos
, "basic_string::substr"), __n
); }
2845 * @brief Compare to a string.
2846 * @param __str String to compare against.
2847 * @return Integer < 0, 0, or > 0.
2849 * Returns an integer < 0 if this string is ordered before @a
2850 * __str, 0 if their values are equivalent, or > 0 if this
2851 * string is ordered after @a __str. Determines the effective
2852 * length rlen of the strings to compare as the smallest of
2853 * size() and str.size(). The function then compares the two
2854 * strings by calling traits::compare(data(), str.data(),rlen).
2855 * If the result of the comparison is nonzero returns it,
2856 * otherwise the shorter one is ordered first.
2859 compare(const basic_string
& __str
) const
2861 const size_type __size
= this->size();
2862 const size_type __osize
= __str
.size();
2863 const size_type __len
= std::min(__size
, __osize
);
2865 int __r
= traits_type::compare(_M_data(), __str
.data(), __len
);
2867 __r
= _S_compare(__size
, __osize
);
2871 #if __cplusplus >= 201703L
2873 * @brief Compare to a string_view.
2874 * @param __svt An object convertible to string_view to compare against.
2875 * @return Integer < 0, 0, or > 0.
2877 template<typename _Tp
>
2879 compare(const _Tp
& __svt
) const
2880 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2882 __sv_type __sv
= __svt
;
2883 const size_type __size
= this->size();
2884 const size_type __osize
= __sv
.size();
2885 const size_type __len
= std::min(__size
, __osize
);
2887 int __r
= traits_type::compare(_M_data(), __sv
.data(), __len
);
2889 __r
= _S_compare(__size
, __osize
);
2894 * @brief Compare to a string_view.
2895 * @param __pos A position in the string to start comparing from.
2896 * @param __n The number of characters to compare.
2897 * @param __svt An object convertible to string_view to compare
2899 * @return Integer < 0, 0, or > 0.
2901 template<typename _Tp
>
2903 compare(size_type __pos
, size_type __n
, const _Tp
& __svt
) const
2904 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2906 __sv_type __sv
= __svt
;
2907 return __sv_type(*this).substr(__pos
, __n
).compare(__sv
);
2911 * @brief Compare to a string_view.
2912 * @param __pos1 A position in the string to start comparing from.
2913 * @param __n1 The number of characters to compare.
2914 * @param __svt An object convertible to string_view to compare
2916 * @param __pos2 A position in the string_view to start comparing from.
2917 * @param __n2 The number of characters to compare.
2918 * @return Integer < 0, 0, or > 0.
2920 template<typename _Tp
>
2922 compare(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
2923 size_type __pos2
, size_type __n2
= npos
) const
2924 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2926 __sv_type __sv
= __svt
;
2927 return __sv_type(*this)
2928 .substr(__pos1
, __n1
).compare(__sv
.substr(__pos2
, __n2
));
2933 * @brief Compare substring to a string.
2934 * @param __pos Index of first character of substring.
2935 * @param __n Number of characters in substring.
2936 * @param __str String to compare against.
2937 * @return Integer < 0, 0, or > 0.
2939 * Form the substring of this string from the @a __n characters
2940 * starting at @a __pos. Returns an integer < 0 if the
2941 * substring is ordered before @a __str, 0 if their values are
2942 * equivalent, or > 0 if the substring is ordered after @a
2943 * __str. Determines the effective length rlen of the strings
2944 * to compare as the smallest of the length of the substring
2945 * and @a __str.size(). The function then compares the two
2946 * strings by calling
2947 * traits::compare(substring.data(),str.data(),rlen). If the
2948 * result of the comparison is nonzero returns it, otherwise
2949 * the shorter one is ordered first.
2952 compare(size_type __pos
, size_type __n
, const basic_string
& __str
) const;
2955 * @brief Compare substring to a substring.
2956 * @param __pos1 Index of first character of substring.
2957 * @param __n1 Number of characters in substring.
2958 * @param __str String to compare against.
2959 * @param __pos2 Index of first character of substring of str.
2960 * @param __n2 Number of characters in substring of str.
2961 * @return Integer < 0, 0, or > 0.
2963 * Form the substring of this string from the @a __n1
2964 * characters starting at @a __pos1. Form the substring of @a
2965 * __str from the @a __n2 characters starting at @a __pos2.
2966 * Returns an integer < 0 if this substring is ordered before
2967 * the substring of @a __str, 0 if their values are equivalent,
2968 * or > 0 if this substring is ordered after the substring of
2969 * @a __str. Determines the effective length rlen of the
2970 * strings to compare as the smallest of the lengths of the
2971 * substrings. The function then compares the two strings by
2973 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2974 * If the result of the comparison is nonzero returns it,
2975 * otherwise the shorter one is ordered first.
2978 compare(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
2979 size_type __pos2
, size_type __n2
= npos
) const;
2982 * @brief Compare to a C string.
2983 * @param __s C string to compare against.
2984 * @return Integer < 0, 0, or > 0.
2986 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
2987 * their values are equivalent, or > 0 if this string is ordered after
2988 * @a __s. Determines the effective length rlen of the strings to
2989 * compare as the smallest of size() and the length of a string
2990 * constructed from @a __s. The function then compares the two strings
2991 * by calling traits::compare(data(),s,rlen). If the result of the
2992 * comparison is nonzero returns it, otherwise the shorter one is
2996 compare(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
;
2998 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2999 // 5 String::compare specification questionable
3001 * @brief Compare substring to a C string.
3002 * @param __pos Index of first character of substring.
3003 * @param __n1 Number of characters in substring.
3004 * @param __s C string to compare against.
3005 * @return Integer < 0, 0, or > 0.
3007 * Form the substring of this string from the @a __n1
3008 * characters starting at @a pos. Returns an integer < 0 if
3009 * the substring is ordered before @a __s, 0 if their values
3010 * are equivalent, or > 0 if the substring is ordered after @a
3011 * __s. Determines the effective length rlen of the strings to
3012 * compare as the smallest of the length of the substring and
3013 * the length of a string constructed from @a __s. The
3014 * function then compares the two string by calling
3015 * traits::compare(substring.data(),__s,rlen). If the result of
3016 * the comparison is nonzero returns it, otherwise the shorter
3017 * one is ordered first.
3020 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
) const;
3023 * @brief Compare substring against a character %array.
3024 * @param __pos Index of first character of substring.
3025 * @param __n1 Number of characters in substring.
3026 * @param __s character %array to compare against.
3027 * @param __n2 Number of characters of s.
3028 * @return Integer < 0, 0, or > 0.
3030 * Form the substring of this string from the @a __n1
3031 * characters starting at @a __pos. Form a string from the
3032 * first @a __n2 characters of @a __s. Returns an integer < 0
3033 * if this substring is ordered before the string from @a __s,
3034 * 0 if their values are equivalent, or > 0 if this substring
3035 * is ordered after the string from @a __s. Determines the
3036 * effective length rlen of the strings to compare as the
3037 * smallest of the length of the substring and @a __n2. The
3038 * function then compares the two strings by calling
3039 * traits::compare(substring.data(),s,rlen). If the result of
3040 * the comparison is nonzero returns it, otherwise the shorter
3041 * one is ordered first.
3043 * NB: s must have at least n2 characters, '\\0' has
3044 * no special meaning.
3047 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
,
3048 size_type __n2
) const;
3050 #if __cplusplus > 201703L
3052 starts_with(basic_string_view
<_CharT
, _Traits
> __x
) const noexcept
3053 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
3056 starts_with(_CharT __x
) const noexcept
3057 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
3060 starts_with(const _CharT
* __x
) const noexcept
3061 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
3064 ends_with(basic_string_view
<_CharT
, _Traits
> __x
) const noexcept
3065 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
3068 ends_with(_CharT __x
) const noexcept
3069 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
3072 ends_with(const _CharT
* __x
) const noexcept
3073 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
3076 // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
3077 template<typename
, typename
, typename
> friend class basic_stringbuf
;
3079 _GLIBCXX_END_NAMESPACE_CXX11
3080 #else // !_GLIBCXX_USE_CXX11_ABI
3081 // Reference-counted COW string implentation
3084 * @class basic_string basic_string.h <string>
3085 * @brief Managing sequences of characters and character-like objects.
3088 * @ingroup sequences
3090 * @tparam _CharT Type of character
3091 * @tparam _Traits Traits for character type, defaults to
3092 * char_traits<_CharT>.
3093 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
3095 * Meets the requirements of a <a href="tables.html#65">container</a>, a
3096 * <a href="tables.html#66">reversible container</a>, and a
3097 * <a href="tables.html#67">sequence</a>. Of the
3098 * <a href="tables.html#68">optional sequence requirements</a>, only
3099 * @c push_back, @c at, and @c %array access are supported.
3104 * Documentation? What's that?
3105 * Nathan Myers <ncm@cantrip.org>.
3107 * A string looks like this:
3112 * [basic_string<char_type>] _M_capacity
3113 * _M_dataplus _M_refcount
3114 * _M_p ----------------> unnamed array of char_type
3117 * Where the _M_p points to the first character in the string, and
3118 * you cast it to a pointer-to-_Rep and subtract 1 to get a
3119 * pointer to the header.
3121 * This approach has the enormous advantage that a string object
3122 * requires only one allocation. All the ugliness is confined
3123 * within a single %pair of inline functions, which each compile to
3124 * a single @a add instruction: _Rep::_M_data(), and
3125 * string::_M_rep(); and the allocation function which gets a
3126 * block of raw bytes and with room enough and constructs a _Rep
3127 * object at the front.
3129 * The reason you want _M_data pointing to the character %array and
3130 * not the _Rep is so that the debugger can see the string
3131 * contents. (Probably we should add a non-inline member to get
3132 * the _Rep for the debugger to use, so users can check the actual
3135 * Note that the _Rep object is a POD so that you can have a
3136 * static <em>empty string</em> _Rep object already @a constructed before
3137 * static constructors have run. The reference-count encoding is
3138 * chosen so that a 0 indicates one reference, so you never try to
3139 * destroy the empty-string _Rep object.
3141 * All but the last paragraph is considered pretty conventional
3142 * for a C++ string implementation.
3144 // 21.3 Template class basic_string
3145 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3148 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
3149 rebind
<_CharT
>::other _CharT_alloc_type
;
3150 typedef __gnu_cxx::__alloc_traits
<_CharT_alloc_type
> _CharT_alloc_traits
;
3154 typedef _Traits traits_type
;
3155 typedef typename
_Traits::char_type value_type
;
3156 typedef _Alloc allocator_type
;
3157 typedef typename
_CharT_alloc_traits::size_type size_type
;
3158 typedef typename
_CharT_alloc_traits::difference_type difference_type
;
3159 #if __cplusplus < 201103L
3160 typedef typename
_CharT_alloc_type::reference reference
;
3161 typedef typename
_CharT_alloc_type::const_reference const_reference
;
3163 typedef value_type
& reference
;
3164 typedef const value_type
& const_reference
;
3166 typedef typename
_CharT_alloc_traits::pointer pointer
;
3167 typedef typename
_CharT_alloc_traits::const_pointer const_pointer
;
3168 typedef __gnu_cxx::__normal_iterator
<pointer
, basic_string
> iterator
;
3169 typedef __gnu_cxx::__normal_iterator
<const_pointer
, basic_string
>
3171 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
3172 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
3175 // type used for positions in insert, erase etc.
3176 typedef iterator __const_iterator
;
3179 // _Rep: string representation
3181 // 1. String really contains _M_length + 1 characters: due to 21.3.4
3182 // must be kept null-terminated.
3183 // 2. _M_capacity >= _M_length
3184 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
3185 // 3. _M_refcount has three states:
3186 // -1: leaked, one reference, no ref-copies allowed, non-const.
3187 // 0: one reference, non-const.
3188 // n>0: n + 1 references, operations require a lock, const.
3189 // 4. All fields==0 is an empty string, given the extra storage
3190 // beyond-the-end for a null terminator; thus, the shared
3191 // empty string representation needs no constructor.
3195 size_type _M_length
;
3196 size_type _M_capacity
;
3197 _Atomic_word _M_refcount
;
3200 struct _Rep
: _Rep_base
3203 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
3204 rebind
<char>::other _Raw_bytes_alloc
;
3206 // (Public) Data members:
3208 // The maximum number of individual char_type elements of an
3209 // individual string is determined by _S_max_size. This is the
3210 // value that will be returned by max_size(). (Whereas npos
3211 // is the maximum number of bytes the allocator can allocate.)
3212 // If one was to divvy up the theoretical largest size string,
3213 // with a terminating character and m _CharT elements, it'd
3215 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
3217 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
3218 // In addition, this implementation quarters this amount.
3219 static const size_type _S_max_size
;
3220 static const _CharT _S_terminal
;
3222 // The following storage is init'd to 0 by the linker, resulting
3223 // (carefully) in an empty string with one reference.
3224 static size_type _S_empty_rep_storage
[];
3227 _S_empty_rep() _GLIBCXX_NOEXCEPT
3229 // NB: Mild hack to avoid strict-aliasing warnings. Note that
3230 // _S_empty_rep_storage is never modified and the punning should
3231 // be reasonably safe in this case.
3232 void* __p
= reinterpret_cast<void*>(&_S_empty_rep_storage
);
3233 return *reinterpret_cast<_Rep
*>(__p
);
3237 _M_is_leaked() const _GLIBCXX_NOEXCEPT
3239 #if defined(__GTHREADS)
3240 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3241 // so we need to use an atomic load. However, _M_is_leaked
3242 // predicate does not change concurrently (i.e. the string is either
3243 // leaked or not), so a relaxed load is enough.
3244 return __atomic_load_n(&this->_M_refcount
, __ATOMIC_RELAXED
) < 0;
3246 return this->_M_refcount
< 0;
3251 _M_is_shared() const _GLIBCXX_NOEXCEPT
3253 #if defined(__GTHREADS)
3254 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3255 // so we need to use an atomic load. Another thread can drop last
3256 // but one reference concurrently with this check, so we need this
3257 // load to be acquire to synchronize with release fetch_and_add in
3259 return __atomic_load_n(&this->_M_refcount
, __ATOMIC_ACQUIRE
) > 0;
3261 return this->_M_refcount
> 0;
3266 _M_set_leaked() _GLIBCXX_NOEXCEPT
3267 { this->_M_refcount
= -1; }
3270 _M_set_sharable() _GLIBCXX_NOEXCEPT
3271 { this->_M_refcount
= 0; }
3274 _M_set_length_and_sharable(size_type __n
) _GLIBCXX_NOEXCEPT
3276 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3277 if (__builtin_expect(this != &_S_empty_rep(), false))
3280 this->_M_set_sharable(); // One reference.
3281 this->_M_length
= __n
;
3282 traits_type::assign(this->_M_refdata()[__n
], _S_terminal
);
3283 // grrr. (per 21.3.4)
3284 // You cannot leave those LWG people alone for a second.
3289 _M_refdata() throw()
3290 { return reinterpret_cast<_CharT
*>(this + 1); }
3293 _M_grab(const _Alloc
& __alloc1
, const _Alloc
& __alloc2
)
3295 return (!_M_is_leaked() && __alloc1
== __alloc2
)
3296 ? _M_refcopy() : _M_clone(__alloc1
);
3301 _S_create(size_type
, size_type
, const _Alloc
&);
3304 _M_dispose(const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
3306 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3307 if (__builtin_expect(this != &_S_empty_rep(), false))
3310 // Be race-detector-friendly. For more info see bits/c++config.
3311 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount
);
3312 // Decrement of _M_refcount is acq_rel, because:
3313 // - all but last decrements need to release to synchronize with
3314 // the last decrement that will delete the object.
3315 // - the last decrement needs to acquire to synchronize with
3316 // all the previous decrements.
3317 // - last but one decrement needs to release to synchronize with
3318 // the acquire load in _M_is_shared that will conclude that
3319 // the object is not shared anymore.
3320 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount
,
3323 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount
);
3330 _M_destroy(const _Alloc
&) throw();
3333 _M_refcopy() throw()
3335 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3336 if (__builtin_expect(this != &_S_empty_rep(), false))
3338 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount
, 1);
3339 return _M_refdata();
3343 _M_clone(const _Alloc
&, size_type __res
= 0);
3346 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
3347 struct _Alloc_hider
: _Alloc
3349 _Alloc_hider(_CharT
* __dat
, const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
3350 : _Alloc(__a
), _M_p(__dat
) { }
3352 _CharT
* _M_p
; // The actual data.
3356 // Data Members (public):
3357 // NB: This is an unsigned type, and thus represents the maximum
3358 // size that the allocator can hold.
3359 /// Value returned by various member functions when they fail.
3360 static const size_type npos
= static_cast<size_type
>(-1);
3363 // Data Members (private):
3364 mutable _Alloc_hider _M_dataplus
;
3367 _M_data() const _GLIBCXX_NOEXCEPT
3368 { return _M_dataplus
._M_p
; }
3371 _M_data(_CharT
* __p
) _GLIBCXX_NOEXCEPT
3372 { return (_M_dataplus
._M_p
= __p
); }
3375 _M_rep() const _GLIBCXX_NOEXCEPT
3376 { return &((reinterpret_cast<_Rep
*> (_M_data()))[-1]); }
3378 // For the internal use we have functions similar to `begin'/`end'
3379 // but they do not call _M_leak.
3381 _M_ibegin() const _GLIBCXX_NOEXCEPT
3382 { return iterator(_M_data()); }
3385 _M_iend() const _GLIBCXX_NOEXCEPT
3386 { return iterator(_M_data() + this->size()); }
3389 _M_leak() // for use in begin() & non-const op[]
3391 if (!_M_rep()->_M_is_leaked())
3396 _M_check(size_type __pos
, const char* __s
) const
3398 if (__pos
> this->size())
3399 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
3400 "this->size() (which is %zu)"),
3401 __s
, __pos
, this->size());
3406 _M_check_length(size_type __n1
, size_type __n2
, const char* __s
) const
3408 if (this->max_size() - (this->size() - __n1
) < __n2
)
3409 __throw_length_error(__N(__s
));
3412 // NB: _M_limit doesn't check for a bad __pos value.
3414 _M_limit(size_type __pos
, size_type __off
) const _GLIBCXX_NOEXCEPT
3416 const bool __testoff
= __off
< this->size() - __pos
;
3417 return __testoff
? __off
: this->size() - __pos
;
3420 // True if _Rep and source do not overlap.
3422 _M_disjunct(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
3424 return (less
<const _CharT
*>()(__s
, _M_data())
3425 || less
<const _CharT
*>()(_M_data() + this->size(), __s
));
3428 // When __n = 1 way faster than the general multichar
3429 // traits_type::copy/move/assign.
3431 _M_copy(_CharT
* __d
, const _CharT
* __s
, size_type __n
) _GLIBCXX_NOEXCEPT
3434 traits_type::assign(*__d
, *__s
);
3436 traits_type::copy(__d
, __s
, __n
);
3440 _M_move(_CharT
* __d
, const _CharT
* __s
, size_type __n
) _GLIBCXX_NOEXCEPT
3443 traits_type::assign(*__d
, *__s
);
3445 traits_type::move(__d
, __s
, __n
);
3449 _M_assign(_CharT
* __d
, size_type __n
, _CharT __c
) _GLIBCXX_NOEXCEPT
3452 traits_type::assign(*__d
, __c
);
3454 traits_type::assign(__d
, __n
, __c
);
3457 // _S_copy_chars is a separate template to permit specialization
3458 // to optimize for the common case of pointers as iterators.
3459 template<class _Iterator
>
3461 _S_copy_chars(_CharT
* __p
, _Iterator __k1
, _Iterator __k2
)
3463 for (; __k1
!= __k2
; ++__k1
, (void)++__p
)
3464 traits_type::assign(*__p
, *__k1
); // These types are off.
3468 _S_copy_chars(_CharT
* __p
, iterator __k1
, iterator __k2
) _GLIBCXX_NOEXCEPT
3469 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
3472 _S_copy_chars(_CharT
* __p
, const_iterator __k1
, const_iterator __k2
)
3474 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
3477 _S_copy_chars(_CharT
* __p
, _CharT
* __k1
, _CharT
* __k2
) _GLIBCXX_NOEXCEPT
3478 { _M_copy(__p
, __k1
, __k2
- __k1
); }
3481 _S_copy_chars(_CharT
* __p
, const _CharT
* __k1
, const _CharT
* __k2
)
3483 { _M_copy(__p
, __k1
, __k2
- __k1
); }
3486 _S_compare(size_type __n1
, size_type __n2
) _GLIBCXX_NOEXCEPT
3488 const difference_type __d
= difference_type(__n1
- __n2
);
3490 if (__d
> __gnu_cxx::__numeric_traits
<int>::__max
)
3491 return __gnu_cxx::__numeric_traits
<int>::__max
;
3492 else if (__d
< __gnu_cxx::__numeric_traits
<int>::__min
)
3493 return __gnu_cxx::__numeric_traits
<int>::__min
;
3499 _M_mutate(size_type __pos
, size_type __len1
, size_type __len2
);
3505 _S_empty_rep() _GLIBCXX_NOEXCEPT
3506 { return _Rep::_S_empty_rep(); }
3508 #if __cplusplus >= 201703L
3509 // A helper type for avoiding boiler-plate.
3510 typedef basic_string_view
<_CharT
, _Traits
> __sv_type
;
3512 template<typename _Tp
, typename _Res
>
3513 using _If_sv
= enable_if_t
<
3514 __and_
<is_convertible
<const _Tp
&, __sv_type
>,
3515 __not_
<is_convertible
<const _Tp
*, const basic_string
*>>,
3516 __not_
<is_convertible
<const _Tp
&, const _CharT
*>>>::value
,
3519 // Allows an implicit conversion to __sv_type.
3521 _S_to_string_view(__sv_type __svt
) noexcept
3524 // Wraps a string_view by explicit conversion and thus
3525 // allows to add an internal constructor that does not
3526 // participate in overload resolution when a string_view
3530 explicit __sv_wrapper(__sv_type __sv
) noexcept
: _M_sv(__sv
) { }
3535 * @brief Only internally used: Construct string from a string view
3537 * @param __svw string view wrapper.
3538 * @param __a Allocator to use.
3541 basic_string(__sv_wrapper __svw
, const _Alloc
& __a
)
3542 : basic_string(__svw
._M_sv
.data(), __svw
._M_sv
.size(), __a
) { }
3546 // Construct/copy/destroy:
3547 // NB: We overload ctors in some cases instead of using default
3548 // arguments, per 17.4.4.4 para. 2 item 2.
3551 * @brief Default constructor creates an empty string.
3554 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3556 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc())
3558 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc())
3563 * @brief Construct an empty string using allocator @a a.
3566 basic_string(const _Alloc
& __a
)
3567 : _M_dataplus(_S_construct(size_type(), _CharT(), __a
), __a
)
3570 // NB: per LWG issue 42, semantics different from IS:
3572 * @brief Construct string with copy of value of @a str.
3573 * @param __str Source string.
3575 basic_string(const basic_string
& __str
)
3576 : _M_dataplus(__str
._M_rep()->_M_grab(_Alloc(__str
.get_allocator()),
3577 __str
.get_allocator()),
3578 __str
.get_allocator())
3581 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3582 // 2583. no way to supply an allocator for basic_string(str, pos)
3584 * @brief Construct string as copy of a substring.
3585 * @param __str Source string.
3586 * @param __pos Index of first character to copy from.
3587 * @param __a Allocator to use.
3589 basic_string(const basic_string
& __str
, size_type __pos
,
3590 const _Alloc
& __a
= _Alloc());
3593 * @brief Construct string as copy of a substring.
3594 * @param __str Source string.
3595 * @param __pos Index of first character to copy from.
3596 * @param __n Number of characters to copy.
3598 basic_string(const basic_string
& __str
, size_type __pos
,
3601 * @brief Construct string as copy of a substring.
3602 * @param __str Source string.
3603 * @param __pos Index of first character to copy from.
3604 * @param __n Number of characters to copy.
3605 * @param __a Allocator to use.
3607 basic_string(const basic_string
& __str
, size_type __pos
,
3608 size_type __n
, const _Alloc
& __a
);
3611 * @brief Construct string initialized by a character %array.
3612 * @param __s Source character %array.
3613 * @param __n Number of characters to copy.
3614 * @param __a Allocator to use (default is default allocator).
3616 * NB: @a __s must have at least @a __n characters, '\\0'
3617 * has no special meaning.
3619 basic_string(const _CharT
* __s
, size_type __n
,
3620 const _Alloc
& __a
= _Alloc())
3621 : _M_dataplus(_S_construct(__s
, __s
+ __n
, __a
), __a
)
3625 * @brief Construct string as copy of a C string.
3626 * @param __s Source C string.
3627 * @param __a Allocator to use (default is default allocator).
3629 #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
3630 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3631 // 3076. basic_string CTAD ambiguity
3632 template<typename
= _RequireAllocator
<_Alloc
>>
3634 basic_string(const _CharT
* __s
, const _Alloc
& __a
= _Alloc())
3635 : _M_dataplus(_S_construct(__s
, __s
? __s
+ traits_type::length(__s
) :
3636 __s
+ npos
, __a
), __a
)
3640 * @brief Construct string as multiple characters.
3641 * @param __n Number of characters.
3642 * @param __c Character to use.
3643 * @param __a Allocator to use (default is default allocator).
3645 basic_string(size_type __n
, _CharT __c
, const _Alloc
& __a
= _Alloc())
3646 : _M_dataplus(_S_construct(__n
, __c
, __a
), __a
)
3649 #if __cplusplus >= 201103L
3651 * @brief Move construct string.
3652 * @param __str Source string.
3654 * The newly-created string contains the exact contents of @a __str.
3655 * @a __str is a valid, but unspecified string.
3657 basic_string(basic_string
&& __str
)
3658 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3659 noexcept
// FIXME C++11: should always be noexcept.
3661 : _M_dataplus(std::move(__str
._M_dataplus
))
3663 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3664 __str
._M_data(_S_empty_rep()._M_refdata());
3666 __str
._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
3671 * @brief Construct string from an initializer %list.
3672 * @param __l std::initializer_list of characters.
3673 * @param __a Allocator to use (default is default allocator).
3675 basic_string(initializer_list
<_CharT
> __l
, const _Alloc
& __a
= _Alloc())
3676 : _M_dataplus(_S_construct(__l
.begin(), __l
.end(), __a
), __a
)
3679 basic_string(const basic_string
& __str
, const _Alloc
& __a
)
3680 : _M_dataplus(__str
._M_rep()->_M_grab(__a
, __str
.get_allocator()), __a
)
3683 basic_string(basic_string
&& __str
, const _Alloc
& __a
)
3684 : _M_dataplus(__str
._M_data(), __a
)
3686 if (__a
== __str
.get_allocator())
3688 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3689 __str
._M_data(_S_empty_rep()._M_refdata());
3691 __str
._M_data(_S_construct(size_type(), _CharT(), __a
));
3695 _M_dataplus
._M_p
= _S_construct(__str
.begin(), __str
.end(), __a
);
3700 * @brief Construct string as copy of a range.
3701 * @param __beg Start of range.
3702 * @param __end End of range.
3703 * @param __a Allocator to use (default is default allocator).
3705 template<class _InputIterator
>
3706 basic_string(_InputIterator __beg
, _InputIterator __end
,
3707 const _Alloc
& __a
= _Alloc())
3708 : _M_dataplus(_S_construct(__beg
, __end
, __a
), __a
)
3711 #if __cplusplus >= 201703L
3713 * @brief Construct string from a substring of a string_view.
3714 * @param __t Source object convertible to string view.
3715 * @param __pos The index of the first character to copy from __t.
3716 * @param __n The number of characters to copy from __t.
3717 * @param __a Allocator to use.
3719 template<typename _Tp
, typename
= _If_sv
<_Tp
, void>>
3720 basic_string(const _Tp
& __t
, size_type __pos
, size_type __n
,
3721 const _Alloc
& __a
= _Alloc())
3722 : basic_string(_S_to_string_view(__t
).substr(__pos
, __n
), __a
) { }
3725 * @brief Construct string from a string_view.
3726 * @param __t Source object convertible to string view.
3727 * @param __a Allocator to use (default is default allocator).
3729 template<typename _Tp
, typename
= _If_sv
<_Tp
, void>>
3731 basic_string(const _Tp
& __t
, const _Alloc
& __a
= _Alloc())
3732 : basic_string(__sv_wrapper(_S_to_string_view(__t
)), __a
) { }
3736 * @brief Destroy the string instance.
3738 ~basic_string() _GLIBCXX_NOEXCEPT
3739 { _M_rep()->_M_dispose(this->get_allocator()); }
3742 * @brief Assign the value of @a str to this string.
3743 * @param __str Source string.
3746 operator=(const basic_string
& __str
)
3747 { return this->assign(__str
); }
3750 * @brief Copy contents of @a s into this string.
3751 * @param __s Source null-terminated string.
3754 operator=(const _CharT
* __s
)
3755 { return this->assign(__s
); }
3758 * @brief Set value to string of length 1.
3759 * @param __c Source character.
3761 * Assigning to a character makes this string length 1 and
3762 * (*this)[0] == @a c.
3765 operator=(_CharT __c
)
3767 this->assign(1, __c
);
3771 #if __cplusplus >= 201103L
3773 * @brief Move assign the value of @a str to this string.
3774 * @param __str Source string.
3776 * The contents of @a str are moved into this string (without copying).
3777 * @a str is a valid, but unspecified string.
3780 operator=(basic_string
&& __str
)
3781 _GLIBCXX_NOEXCEPT_IF(allocator_traits
<_Alloc
>::is_always_equal::value
)
3789 * @brief Set value to string constructed from initializer %list.
3790 * @param __l std::initializer_list.
3793 operator=(initializer_list
<_CharT
> __l
)
3795 this->assign(__l
.begin(), __l
.size());
3800 #if __cplusplus >= 201703L
3802 * @brief Set value to string constructed from a string_view.
3803 * @param __svt An object convertible to string_view.
3805 template<typename _Tp
>
3806 _If_sv
<_Tp
, basic_string
&>
3807 operator=(const _Tp
& __svt
)
3808 { return this->assign(__svt
); }
3811 * @brief Convert to a string_view.
3812 * @return A string_view.
3814 operator __sv_type() const noexcept
3815 { return __sv_type(data(), size()); }
3820 * Returns a read/write iterator that points to the first character in
3821 * the %string. Unshares the string.
3824 begin() // FIXME C++11: should be noexcept.
3827 return iterator(_M_data());
3831 * Returns a read-only (constant) iterator that points to the first
3832 * character in the %string.
3835 begin() const _GLIBCXX_NOEXCEPT
3836 { return const_iterator(_M_data()); }
3839 * Returns a read/write iterator that points one past the last
3840 * character in the %string. Unshares the string.
3843 end() // FIXME C++11: should be noexcept.
3846 return iterator(_M_data() + this->size());
3850 * Returns a read-only (constant) iterator that points one past the
3851 * last character in the %string.
3854 end() const _GLIBCXX_NOEXCEPT
3855 { return const_iterator(_M_data() + this->size()); }
3858 * Returns a read/write reverse iterator that points to the last
3859 * character in the %string. Iteration is done in reverse element
3860 * order. Unshares the string.
3863 rbegin() // FIXME C++11: should be noexcept.
3864 { return reverse_iterator(this->end()); }
3867 * Returns a read-only (constant) reverse iterator that points
3868 * to the last character in the %string. Iteration is done in
3869 * reverse element order.
3871 const_reverse_iterator
3872 rbegin() const _GLIBCXX_NOEXCEPT
3873 { return const_reverse_iterator(this->end()); }
3876 * Returns a read/write reverse iterator that points to one before the
3877 * first character in the %string. Iteration is done in reverse
3878 * element order. Unshares the string.
3881 rend() // FIXME C++11: should be noexcept.
3882 { return reverse_iterator(this->begin()); }
3885 * Returns a read-only (constant) reverse iterator that points
3886 * to one before the first character in the %string. Iteration
3887 * is done in reverse element order.
3889 const_reverse_iterator
3890 rend() const _GLIBCXX_NOEXCEPT
3891 { return const_reverse_iterator(this->begin()); }
3893 #if __cplusplus >= 201103L
3895 * Returns a read-only (constant) iterator that points to the first
3896 * character in the %string.
3899 cbegin() const noexcept
3900 { return const_iterator(this->_M_data()); }
3903 * Returns a read-only (constant) iterator that points one past the
3904 * last character in the %string.
3907 cend() const noexcept
3908 { return const_iterator(this->_M_data() + this->size()); }
3911 * Returns a read-only (constant) reverse iterator that points
3912 * to the last character in the %string. Iteration is done in
3913 * reverse element order.
3915 const_reverse_iterator
3916 crbegin() const noexcept
3917 { return const_reverse_iterator(this->end()); }
3920 * Returns a read-only (constant) reverse iterator that points
3921 * to one before the first character in the %string. Iteration
3922 * is done in reverse element order.
3924 const_reverse_iterator
3925 crend() const noexcept
3926 { return const_reverse_iterator(this->begin()); }
3931 /// Returns the number of characters in the string, not including any
3932 /// null-termination.
3934 size() const _GLIBCXX_NOEXCEPT
3935 { return _M_rep()->_M_length
; }
3937 /// Returns the number of characters in the string, not including any
3938 /// null-termination.
3940 length() const _GLIBCXX_NOEXCEPT
3941 { return _M_rep()->_M_length
; }
3943 /// Returns the size() of the largest possible %string.
3945 max_size() const _GLIBCXX_NOEXCEPT
3946 { return _Rep::_S_max_size
; }
3949 * @brief Resizes the %string to the specified number of characters.
3950 * @param __n Number of characters the %string should contain.
3951 * @param __c Character to fill any new elements.
3953 * This function will %resize the %string to the specified
3954 * number of characters. If the number is smaller than the
3955 * %string's current size the %string is truncated, otherwise
3956 * the %string is extended and new elements are %set to @a __c.
3959 resize(size_type __n
, _CharT __c
);
3962 * @brief Resizes the %string to the specified number of characters.
3963 * @param __n Number of characters the %string should contain.
3965 * This function will resize the %string to the specified length. If
3966 * the new size is smaller than the %string's current size the %string
3967 * is truncated, otherwise the %string is extended and new characters
3968 * are default-constructed. For basic types such as char, this means
3969 * setting them to 0.
3972 resize(size_type __n
)
3973 { this->resize(__n
, _CharT()); }
3975 #if __cplusplus >= 201103L
3976 #pragma GCC diagnostic push
3977 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
3978 /// A non-binding request to reduce capacity() to size().
3980 shrink_to_fit() noexcept
3982 #pragma GCC diagnostic pop
3986 * Returns the total number of characters that the %string can hold
3987 * before needing to allocate more memory.
3990 capacity() const _GLIBCXX_NOEXCEPT
3991 { return _M_rep()->_M_capacity
; }
3994 * @brief Attempt to preallocate enough memory for specified number of
3996 * @param __res_arg Number of characters required.
3997 * @throw std::length_error If @a __res_arg exceeds @c max_size().
3999 * This function attempts to reserve enough memory for the
4000 * %string to hold the specified number of characters. If the
4001 * number requested is more than max_size(), length_error is
4004 * The advantage of this function is that if optimal code is a
4005 * necessity and the user can determine the string length that will be
4006 * required, the user can reserve the memory in %advance, and thus
4007 * prevent a possible reallocation of memory and copying of %string
4011 reserve(size_type __res_arg
);
4013 /// Equivalent to shrink_to_fit().
4014 #if __cplusplus > 201703L
4015 [[deprecated("use shrink_to_fit() instead")]]
4021 * Erases the string, making it empty.
4023 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
4025 clear() _GLIBCXX_NOEXCEPT
4027 if (_M_rep()->_M_is_shared())
4029 _M_rep()->_M_dispose(this->get_allocator());
4030 _M_data(_S_empty_rep()._M_refdata());
4033 _M_rep()->_M_set_length_and_sharable(0);
4036 // PR 56166: this should not throw.
4039 { _M_mutate(0, this->size(), 0); }
4043 * Returns true if the %string is empty. Equivalent to
4044 * <code>*this == ""</code>.
4046 _GLIBCXX_NODISCARD
bool
4047 empty() const _GLIBCXX_NOEXCEPT
4048 { return this->size() == 0; }
4052 * @brief Subscript access to the data contained in the %string.
4053 * @param __pos The index of the character to access.
4054 * @return Read-only (constant) reference to the character.
4056 * This operator allows for easy, array-style, data access.
4057 * Note that data access with this operator is unchecked and
4058 * out_of_range lookups are not defined. (For checked lookups
4062 operator[] (size_type __pos
) const _GLIBCXX_NOEXCEPT
4064 __glibcxx_assert(__pos
<= size());
4065 return _M_data()[__pos
];
4069 * @brief Subscript access to the data contained in the %string.
4070 * @param __pos The index of the character to access.
4071 * @return Read/write reference to the character.
4073 * This operator allows for easy, array-style, data access.
4074 * Note that data access with this operator is unchecked and
4075 * out_of_range lookups are not defined. (For checked lookups
4076 * see at().) Unshares the string.
4079 operator[](size_type __pos
)
4081 // Allow pos == size() both in C++98 mode, as v3 extension,
4082 // and in C++11 mode.
4083 __glibcxx_assert(__pos
<= size());
4084 // In pedantic mode be strict in C++98 mode.
4085 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus
>= 201103L || __pos
< size());
4087 return _M_data()[__pos
];
4091 * @brief Provides access to the data contained in the %string.
4092 * @param __n The index of the character to access.
4093 * @return Read-only (const) reference to the character.
4094 * @throw std::out_of_range If @a n is an invalid index.
4096 * This function provides for safer data access. The parameter is
4097 * first checked that it is in the range of the string. The function
4098 * throws out_of_range if the check fails.
4101 at(size_type __n
) const
4103 if (__n
>= this->size())
4104 __throw_out_of_range_fmt(__N("basic_string::at: __n "
4105 "(which is %zu) >= this->size() "
4108 return _M_data()[__n
];
4112 * @brief Provides access to the data contained in the %string.
4113 * @param __n The index of the character to access.
4114 * @return Read/write reference to the character.
4115 * @throw std::out_of_range If @a n is an invalid index.
4117 * This function provides for safer data access. The parameter is
4118 * first checked that it is in the range of the string. The function
4119 * throws out_of_range if the check fails. Success results in
4120 * unsharing the string.
4126 __throw_out_of_range_fmt(__N("basic_string::at: __n "
4127 "(which is %zu) >= this->size() "
4131 return _M_data()[__n
];
4134 #if __cplusplus >= 201103L
4136 * Returns a read/write reference to the data at the first
4137 * element of the %string.
4142 __glibcxx_assert(!empty());
4143 return operator[](0);
4147 * Returns a read-only (constant) reference to the data at the first
4148 * element of the %string.
4151 front() const noexcept
4153 __glibcxx_assert(!empty());
4154 return operator[](0);
4158 * Returns a read/write reference to the data at the last
4159 * element of the %string.
4164 __glibcxx_assert(!empty());
4165 return operator[](this->size() - 1);
4169 * Returns a read-only (constant) reference to the data at the
4170 * last element of the %string.
4173 back() const noexcept
4175 __glibcxx_assert(!empty());
4176 return operator[](this->size() - 1);
4182 * @brief Append a string to this string.
4183 * @param __str The string to append.
4184 * @return Reference to this string.
4187 operator+=(const basic_string
& __str
)
4188 { return this->append(__str
); }
4191 * @brief Append a C string.
4192 * @param __s The C string to append.
4193 * @return Reference to this string.
4196 operator+=(const _CharT
* __s
)
4197 { return this->append(__s
); }
4200 * @brief Append a character.
4201 * @param __c The character to append.
4202 * @return Reference to this string.
4205 operator+=(_CharT __c
)
4207 this->push_back(__c
);
4211 #if __cplusplus >= 201103L
4213 * @brief Append an initializer_list of characters.
4214 * @param __l The initializer_list of characters to be appended.
4215 * @return Reference to this string.
4218 operator+=(initializer_list
<_CharT
> __l
)
4219 { return this->append(__l
.begin(), __l
.size()); }
4222 #if __cplusplus >= 201703L
4224 * @brief Append a string_view.
4225 * @param __svt The object convertible to string_view to be appended.
4226 * @return Reference to this string.
4228 template<typename _Tp
>
4229 _If_sv
<_Tp
, basic_string
&>
4230 operator+=(const _Tp
& __svt
)
4231 { return this->append(__svt
); }
4235 * @brief Append a string to this string.
4236 * @param __str The string to append.
4237 * @return Reference to this string.
4240 append(const basic_string
& __str
);
4243 * @brief Append a substring.
4244 * @param __str The string to append.
4245 * @param __pos Index of the first character of str to append.
4246 * @param __n The number of characters to append.
4247 * @return Reference to this string.
4248 * @throw std::out_of_range if @a __pos is not a valid index.
4250 * This function appends @a __n characters from @a __str
4251 * starting at @a __pos to this string. If @a __n is is larger
4252 * than the number of available characters in @a __str, the
4253 * remainder of @a __str is appended.
4256 append(const basic_string
& __str
, size_type __pos
, size_type __n
= npos
);
4259 * @brief Append a C substring.
4260 * @param __s The C string to append.
4261 * @param __n The number of characters to append.
4262 * @return Reference to this string.
4265 append(const _CharT
* __s
, size_type __n
);
4268 * @brief Append a C string.
4269 * @param __s The C string to append.
4270 * @return Reference to this string.
4273 append(const _CharT
* __s
)
4275 __glibcxx_requires_string(__s
);
4276 return this->append(__s
, traits_type::length(__s
));
4280 * @brief Append multiple characters.
4281 * @param __n The number of characters to append.
4282 * @param __c The character to use.
4283 * @return Reference to this string.
4285 * Appends __n copies of __c to this string.
4288 append(size_type __n
, _CharT __c
);
4290 #if __cplusplus >= 201103L
4292 * @brief Append an initializer_list of characters.
4293 * @param __l The initializer_list of characters to append.
4294 * @return Reference to this string.
4297 append(initializer_list
<_CharT
> __l
)
4298 { return this->append(__l
.begin(), __l
.size()); }
4302 * @brief Append a range of characters.
4303 * @param __first Iterator referencing the first character to append.
4304 * @param __last Iterator marking the end of the range.
4305 * @return Reference to this string.
4307 * Appends characters in the range [__first,__last) to this string.
4309 template<class _InputIterator
>
4311 append(_InputIterator __first
, _InputIterator __last
)
4312 { return this->replace(_M_iend(), _M_iend(), __first
, __last
); }
4314 #if __cplusplus >= 201703L
4316 * @brief Append a string_view.
4317 * @param __svt The object convertible to string_view to be appended.
4318 * @return Reference to this string.
4320 template<typename _Tp
>
4321 _If_sv
<_Tp
, basic_string
&>
4322 append(const _Tp
& __svt
)
4324 __sv_type __sv
= __svt
;
4325 return this->append(__sv
.data(), __sv
.size());
4329 * @brief Append a range of characters from a string_view.
4330 * @param __svt The object convertible to string_view to be appended
4332 * @param __pos The position in the string_view to append from.
4333 * @param __n The number of characters to append from the string_view.
4334 * @return Reference to this string.
4336 template<typename _Tp
>
4337 _If_sv
<_Tp
, basic_string
&>
4338 append(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
4340 __sv_type __sv
= __svt
;
4341 return append(__sv
.data()
4342 + std::__sv_check(__sv
.size(), __pos
, "basic_string::append"),
4343 std::__sv_limit(__sv
.size(), __pos
, __n
));
4348 * @brief Append a single character.
4349 * @param __c Character to append.
4352 push_back(_CharT __c
)
4354 const size_type __len
= 1 + this->size();
4355 if (__len
> this->capacity() || _M_rep()->_M_is_shared())
4356 this->reserve(__len
);
4357 traits_type::assign(_M_data()[this->size()], __c
);
4358 _M_rep()->_M_set_length_and_sharable(__len
);
4362 * @brief Set value to contents of another string.
4363 * @param __str Source string to use.
4364 * @return Reference to this string.
4367 assign(const basic_string
& __str
);
4369 #if __cplusplus >= 201103L
4371 * @brief Set value to contents of another string.
4372 * @param __str Source string to use.
4373 * @return Reference to this string.
4375 * This function sets this string to the exact contents of @a __str.
4376 * @a __str is a valid, but unspecified string.
4379 assign(basic_string
&& __str
)
4380 noexcept(allocator_traits
<_Alloc
>::is_always_equal::value
)
4388 * @brief Set value to a substring of a string.
4389 * @param __str The string to use.
4390 * @param __pos Index of the first character of str.
4391 * @param __n Number of characters to use.
4392 * @return Reference to this string.
4393 * @throw std::out_of_range if @a pos is not a valid index.
4395 * This function sets this string to the substring of @a __str
4396 * consisting of @a __n characters at @a __pos. If @a __n is
4397 * is larger than the number of available characters in @a
4398 * __str, the remainder of @a __str is used.
4401 assign(const basic_string
& __str
, size_type __pos
, size_type __n
= npos
)
4402 { return this->assign(__str
._M_data()
4403 + __str
._M_check(__pos
, "basic_string::assign"),
4404 __str
._M_limit(__pos
, __n
)); }
4407 * @brief Set value to a C substring.
4408 * @param __s The C string to use.
4409 * @param __n Number of characters to use.
4410 * @return Reference to this string.
4412 * This function sets the value of this string to the first @a __n
4413 * characters of @a __s. If @a __n is is larger than the number of
4414 * available characters in @a __s, the remainder of @a __s is used.
4417 assign(const _CharT
* __s
, size_type __n
);
4420 * @brief Set value to contents of a C string.
4421 * @param __s The C string to use.
4422 * @return Reference to this string.
4424 * This function sets the value of this string to the value of @a __s.
4425 * The data is copied, so there is no dependence on @a __s once the
4429 assign(const _CharT
* __s
)
4431 __glibcxx_requires_string(__s
);
4432 return this->assign(__s
, traits_type::length(__s
));
4436 * @brief Set value to multiple characters.
4437 * @param __n Length of the resulting string.
4438 * @param __c The character to use.
4439 * @return Reference to this string.
4441 * This function sets the value of this string to @a __n copies of
4445 assign(size_type __n
, _CharT __c
)
4446 { return _M_replace_aux(size_type(0), this->size(), __n
, __c
); }
4449 * @brief Set value to a range of characters.
4450 * @param __first Iterator referencing the first character to append.
4451 * @param __last Iterator marking the end of the range.
4452 * @return Reference to this string.
4454 * Sets value of string to characters in the range [__first,__last).
4456 template<class _InputIterator
>
4458 assign(_InputIterator __first
, _InputIterator __last
)
4459 { return this->replace(_M_ibegin(), _M_iend(), __first
, __last
); }
4461 #if __cplusplus >= 201103L
4463 * @brief Set value to an initializer_list of characters.
4464 * @param __l The initializer_list of characters to assign.
4465 * @return Reference to this string.
4468 assign(initializer_list
<_CharT
> __l
)
4469 { return this->assign(__l
.begin(), __l
.size()); }
4472 #if __cplusplus >= 201703L
4474 * @brief Set value from a string_view.
4475 * @param __svt The source object convertible to string_view.
4476 * @return Reference to this string.
4478 template<typename _Tp
>
4479 _If_sv
<_Tp
, basic_string
&>
4480 assign(const _Tp
& __svt
)
4482 __sv_type __sv
= __svt
;
4483 return this->assign(__sv
.data(), __sv
.size());
4487 * @brief Set value from a range of characters in a string_view.
4488 * @param __svt The source object convertible to string_view.
4489 * @param __pos The position in the string_view to assign from.
4490 * @param __n The number of characters to assign.
4491 * @return Reference to this string.
4493 template<typename _Tp
>
4494 _If_sv
<_Tp
, basic_string
&>
4495 assign(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
4497 __sv_type __sv
= __svt
;
4498 return assign(__sv
.data()
4499 + std::__sv_check(__sv
.size(), __pos
, "basic_string::assign"),
4500 std::__sv_limit(__sv
.size(), __pos
, __n
));
4505 * @brief Insert multiple characters.
4506 * @param __p Iterator referencing location in string to insert at.
4507 * @param __n Number of characters to insert
4508 * @param __c The character to insert.
4509 * @throw std::length_error If new length exceeds @c max_size().
4511 * Inserts @a __n copies of character @a __c starting at the
4512 * position referenced by iterator @a __p. If adding
4513 * characters causes the length to exceed max_size(),
4514 * length_error is thrown. The value of the string doesn't
4515 * change if an error is thrown.
4518 insert(iterator __p
, size_type __n
, _CharT __c
)
4519 { this->replace(__p
, __p
, __n
, __c
); }
4522 * @brief Insert a range of characters.
4523 * @param __p Iterator referencing location in string to insert at.
4524 * @param __beg Start of range.
4525 * @param __end End of range.
4526 * @throw std::length_error If new length exceeds @c max_size().
4528 * Inserts characters in range [__beg,__end). If adding
4529 * characters causes the length to exceed max_size(),
4530 * length_error is thrown. The value of the string doesn't
4531 * change if an error is thrown.
4533 template<class _InputIterator
>
4535 insert(iterator __p
, _InputIterator __beg
, _InputIterator __end
)
4536 { this->replace(__p
, __p
, __beg
, __end
); }
4538 #if __cplusplus >= 201103L
4540 * @brief Insert an initializer_list of characters.
4541 * @param __p Iterator referencing location in string to insert at.
4542 * @param __l The initializer_list of characters to insert.
4543 * @throw std::length_error If new length exceeds @c max_size().
4546 insert(iterator __p
, initializer_list
<_CharT
> __l
)
4548 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
4549 this->insert(__p
- _M_ibegin(), __l
.begin(), __l
.size());
4554 * @brief Insert value of a string.
4555 * @param __pos1 Position in string to insert at.
4556 * @param __str The string to insert.
4557 * @return Reference to this string.
4558 * @throw std::length_error If new length exceeds @c max_size().
4560 * Inserts value of @a __str starting at @a __pos1. If adding
4561 * characters causes the length to exceed max_size(),
4562 * length_error is thrown. The value of the string doesn't
4563 * change if an error is thrown.
4566 insert(size_type __pos1
, const basic_string
& __str
)
4567 { return this->insert(__pos1
, __str
, size_type(0), __str
.size()); }
4570 * @brief Insert a substring.
4571 * @param __pos1 Position in string to insert at.
4572 * @param __str The string to insert.
4573 * @param __pos2 Start of characters in str to insert.
4574 * @param __n Number of characters to insert.
4575 * @return Reference to this string.
4576 * @throw std::length_error If new length exceeds @c max_size().
4577 * @throw std::out_of_range If @a pos1 > size() or
4578 * @a __pos2 > @a str.size().
4580 * Starting at @a pos1, insert @a __n character of @a __str
4581 * beginning with @a __pos2. If adding characters causes the
4582 * length to exceed max_size(), length_error is thrown. If @a
4583 * __pos1 is beyond the end of this string or @a __pos2 is
4584 * beyond the end of @a __str, out_of_range is thrown. The
4585 * value of the string doesn't change if an error is thrown.
4588 insert(size_type __pos1
, const basic_string
& __str
,
4589 size_type __pos2
, size_type __n
= npos
)
4590 { return this->insert(__pos1
, __str
._M_data()
4591 + __str
._M_check(__pos2
, "basic_string::insert"),
4592 __str
._M_limit(__pos2
, __n
)); }
4595 * @brief Insert a C substring.
4596 * @param __pos Position in string to insert at.
4597 * @param __s The C string to insert.
4598 * @param __n The number of characters to insert.
4599 * @return Reference to this string.
4600 * @throw std::length_error If new length exceeds @c max_size().
4601 * @throw std::out_of_range If @a __pos is beyond the end of this
4604 * Inserts the first @a __n characters of @a __s starting at @a
4605 * __pos. If adding characters causes the length to exceed
4606 * max_size(), length_error is thrown. If @a __pos is beyond
4607 * end(), out_of_range is thrown. The value of the string
4608 * doesn't change if an error is thrown.
4611 insert(size_type __pos
, const _CharT
* __s
, size_type __n
);
4614 * @brief Insert a C string.
4615 * @param __pos Position in string to insert at.
4616 * @param __s The C string to insert.
4617 * @return Reference to this string.
4618 * @throw std::length_error If new length exceeds @c max_size().
4619 * @throw std::out_of_range If @a pos is beyond the end of this
4622 * Inserts the first @a n characters of @a __s starting at @a __pos. If
4623 * adding characters causes the length to exceed max_size(),
4624 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
4625 * thrown. The value of the string doesn't change if an error is
4629 insert(size_type __pos
, const _CharT
* __s
)
4631 __glibcxx_requires_string(__s
);
4632 return this->insert(__pos
, __s
, traits_type::length(__s
));
4636 * @brief Insert multiple characters.
4637 * @param __pos Index in string to insert at.
4638 * @param __n Number of characters to insert
4639 * @param __c The character to insert.
4640 * @return Reference to this string.
4641 * @throw std::length_error If new length exceeds @c max_size().
4642 * @throw std::out_of_range If @a __pos is beyond the end of this
4645 * Inserts @a __n copies of character @a __c starting at index
4646 * @a __pos. If adding characters causes the length to exceed
4647 * max_size(), length_error is thrown. If @a __pos > length(),
4648 * out_of_range is thrown. The value of the string doesn't
4649 * change if an error is thrown.
4652 insert(size_type __pos
, size_type __n
, _CharT __c
)
4653 { return _M_replace_aux(_M_check(__pos
, "basic_string::insert"),
4654 size_type(0), __n
, __c
); }
4657 * @brief Insert one character.
4658 * @param __p Iterator referencing position in string to insert at.
4659 * @param __c The character to insert.
4660 * @return Iterator referencing newly inserted char.
4661 * @throw std::length_error If new length exceeds @c max_size().
4663 * Inserts character @a __c at position referenced by @a __p.
4664 * If adding character causes the length to exceed max_size(),
4665 * length_error is thrown. If @a __p is beyond end of string,
4666 * out_of_range is thrown. The value of the string doesn't
4667 * change if an error is thrown.
4670 insert(iterator __p
, _CharT __c
)
4672 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
4673 const size_type __pos
= __p
- _M_ibegin();
4674 _M_replace_aux(__pos
, size_type(0), size_type(1), __c
);
4675 _M_rep()->_M_set_leaked();
4676 return iterator(_M_data() + __pos
);
4679 #if __cplusplus >= 201703L
4681 * @brief Insert a string_view.
4682 * @param __pos Position in string to insert at.
4683 * @param __svt The object convertible to string_view to insert.
4684 * @return Reference to this string.
4686 template<typename _Tp
>
4687 _If_sv
<_Tp
, basic_string
&>
4688 insert(size_type __pos
, const _Tp
& __svt
)
4690 __sv_type __sv
= __svt
;
4691 return this->insert(__pos
, __sv
.data(), __sv
.size());
4695 * @brief Insert a string_view.
4696 * @param __pos Position in string to insert at.
4697 * @param __svt The object convertible to string_view to insert from.
4698 * @param __pos Position in string_view to insert
4700 * @param __n The number of characters to insert.
4701 * @return Reference to this string.
4703 template<typename _Tp
>
4704 _If_sv
<_Tp
, basic_string
&>
4705 insert(size_type __pos1
, const _Tp
& __svt
,
4706 size_type __pos2
, size_type __n
= npos
)
4708 __sv_type __sv
= __svt
;
4709 return this->replace(__pos1
, size_type(0), __sv
.data()
4710 + std::__sv_check(__sv
.size(), __pos2
, "basic_string::insert"),
4711 std::__sv_limit(__sv
.size(), __pos2
, __n
));
4716 * @brief Remove characters.
4717 * @param __pos Index of first character to remove (default 0).
4718 * @param __n Number of characters to remove (default remainder).
4719 * @return Reference to this string.
4720 * @throw std::out_of_range If @a pos is beyond the end of this
4723 * Removes @a __n characters from this string starting at @a
4724 * __pos. The length of the string is reduced by @a __n. If
4725 * there are < @a __n characters to remove, the remainder of
4726 * the string is truncated. If @a __p is beyond end of string,
4727 * out_of_range is thrown. The value of the string doesn't
4728 * change if an error is thrown.
4731 erase(size_type __pos
= 0, size_type __n
= npos
)
4733 _M_mutate(_M_check(__pos
, "basic_string::erase"),
4734 _M_limit(__pos
, __n
), size_type(0));
4739 * @brief Remove one character.
4740 * @param __position Iterator referencing the character to remove.
4741 * @return iterator referencing same location after removal.
4743 * Removes the character at @a __position from this string. The value
4744 * of the string doesn't change if an error is thrown.
4747 erase(iterator __position
)
4749 _GLIBCXX_DEBUG_PEDASSERT(__position
>= _M_ibegin()
4750 && __position
< _M_iend());
4751 const size_type __pos
= __position
- _M_ibegin();
4752 _M_mutate(__pos
, size_type(1), size_type(0));
4753 _M_rep()->_M_set_leaked();
4754 return iterator(_M_data() + __pos
);
4758 * @brief Remove a range of characters.
4759 * @param __first Iterator referencing the first character to remove.
4760 * @param __last Iterator referencing the end of the range.
4761 * @return Iterator referencing location of first after removal.
4763 * Removes the characters in the range [first,last) from this string.
4764 * The value of the string doesn't change if an error is thrown.
4767 erase(iterator __first
, iterator __last
);
4769 #if __cplusplus >= 201103L
4771 * @brief Remove the last character.
4773 * The string must be non-empty.
4776 pop_back() // FIXME C++11: should be noexcept.
4778 __glibcxx_assert(!empty());
4779 erase(size() - 1, 1);
4784 * @brief Replace characters with value from another string.
4785 * @param __pos Index of first character to replace.
4786 * @param __n Number of characters to be replaced.
4787 * @param __str String to insert.
4788 * @return Reference to this string.
4789 * @throw std::out_of_range If @a pos is beyond the end of this
4791 * @throw std::length_error If new length exceeds @c max_size().
4793 * Removes the characters in the range [__pos,__pos+__n) from
4794 * this string. In place, the value of @a __str is inserted.
4795 * If @a __pos is beyond end of string, out_of_range is thrown.
4796 * If the length of the result exceeds max_size(), length_error
4797 * is thrown. The value of the string doesn't change if an
4801 replace(size_type __pos
, size_type __n
, const basic_string
& __str
)
4802 { return this->replace(__pos
, __n
, __str
._M_data(), __str
.size()); }
4805 * @brief Replace characters with value from another string.
4806 * @param __pos1 Index of first character to replace.
4807 * @param __n1 Number of characters to be replaced.
4808 * @param __str String to insert.
4809 * @param __pos2 Index of first character of str to use.
4810 * @param __n2 Number of characters from str to use.
4811 * @return Reference to this string.
4812 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
4814 * @throw std::length_error If new length exceeds @c max_size().
4816 * Removes the characters in the range [__pos1,__pos1 + n) from this
4817 * string. In place, the value of @a __str is inserted. If @a __pos is
4818 * beyond end of string, out_of_range is thrown. If the length of the
4819 * result exceeds max_size(), length_error is thrown. The value of the
4820 * string doesn't change if an error is thrown.
4823 replace(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
4824 size_type __pos2
, size_type __n2
= npos
)
4825 { return this->replace(__pos1
, __n1
, __str
._M_data()
4826 + __str
._M_check(__pos2
, "basic_string::replace"),
4827 __str
._M_limit(__pos2
, __n2
)); }
4830 * @brief Replace characters with value of a C substring.
4831 * @param __pos Index of first character to replace.
4832 * @param __n1 Number of characters to be replaced.
4833 * @param __s C string to insert.
4834 * @param __n2 Number of characters from @a s to use.
4835 * @return Reference to this string.
4836 * @throw std::out_of_range If @a pos1 > size().
4837 * @throw std::length_error If new length exceeds @c max_size().
4839 * Removes the characters in the range [__pos,__pos + __n1)
4840 * from this string. In place, the first @a __n2 characters of
4841 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
4842 * @a __pos is beyond end of string, out_of_range is thrown. If
4843 * the length of result exceeds max_size(), length_error is
4844 * thrown. The value of the string doesn't change if an error
4848 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
4852 * @brief Replace characters with value of a C string.
4853 * @param __pos Index of first character to replace.
4854 * @param __n1 Number of characters to be replaced.
4855 * @param __s C string to insert.
4856 * @return Reference to this string.
4857 * @throw std::out_of_range If @a pos > size().
4858 * @throw std::length_error If new length exceeds @c max_size().
4860 * Removes the characters in the range [__pos,__pos + __n1)
4861 * from this string. In place, the characters of @a __s are
4862 * inserted. If @a __pos is beyond end of string, out_of_range
4863 * is thrown. If the length of result exceeds max_size(),
4864 * length_error is thrown. The value of the string doesn't
4865 * change if an error is thrown.
4868 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
)
4870 __glibcxx_requires_string(__s
);
4871 return this->replace(__pos
, __n1
, __s
, traits_type::length(__s
));
4875 * @brief Replace characters with multiple characters.
4876 * @param __pos Index of first character to replace.
4877 * @param __n1 Number of characters to be replaced.
4878 * @param __n2 Number of characters to insert.
4879 * @param __c Character to insert.
4880 * @return Reference to this string.
4881 * @throw std::out_of_range If @a __pos > size().
4882 * @throw std::length_error If new length exceeds @c max_size().
4884 * Removes the characters in the range [pos,pos + n1) from this
4885 * string. In place, @a __n2 copies of @a __c are inserted.
4886 * If @a __pos is beyond end of string, out_of_range is thrown.
4887 * If the length of result exceeds max_size(), length_error is
4888 * thrown. The value of the string doesn't change if an error
4892 replace(size_type __pos
, size_type __n1
, size_type __n2
, _CharT __c
)
4893 { return _M_replace_aux(_M_check(__pos
, "basic_string::replace"),
4894 _M_limit(__pos
, __n1
), __n2
, __c
); }
4897 * @brief Replace range of characters with string.
4898 * @param __i1 Iterator referencing start of range to replace.
4899 * @param __i2 Iterator referencing end of range to replace.
4900 * @param __str String value to insert.
4901 * @return Reference to this string.
4902 * @throw std::length_error If new length exceeds @c max_size().
4904 * Removes the characters in the range [__i1,__i2). In place,
4905 * the value of @a __str is inserted. If the length of result
4906 * exceeds max_size(), length_error is thrown. The value of
4907 * the string doesn't change if an error is thrown.
4910 replace(iterator __i1
, iterator __i2
, const basic_string
& __str
)
4911 { return this->replace(__i1
, __i2
, __str
._M_data(), __str
.size()); }
4914 * @brief Replace range of characters with C substring.
4915 * @param __i1 Iterator referencing start of range to replace.
4916 * @param __i2 Iterator referencing end of range to replace.
4917 * @param __s C string value to insert.
4918 * @param __n Number of characters from s to insert.
4919 * @return Reference to this string.
4920 * @throw std::length_error If new length exceeds @c max_size().
4922 * Removes the characters in the range [__i1,__i2). In place,
4923 * the first @a __n characters of @a __s are inserted. If the
4924 * length of result exceeds max_size(), length_error is thrown.
4925 * The value of the string doesn't change if an error is
4929 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
, size_type __n
)
4931 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4932 && __i2
<= _M_iend());
4933 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
, __s
, __n
);
4937 * @brief Replace range of characters with C string.
4938 * @param __i1 Iterator referencing start of range to replace.
4939 * @param __i2 Iterator referencing end of range to replace.
4940 * @param __s C string value to insert.
4941 * @return Reference to this string.
4942 * @throw std::length_error If new length exceeds @c max_size().
4944 * Removes the characters in the range [__i1,__i2). In place,
4945 * the characters of @a __s are inserted. If the length of
4946 * result exceeds max_size(), length_error is thrown. The
4947 * value of the string doesn't change if an error is thrown.
4950 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
)
4952 __glibcxx_requires_string(__s
);
4953 return this->replace(__i1
, __i2
, __s
, traits_type::length(__s
));
4957 * @brief Replace range of characters with multiple characters
4958 * @param __i1 Iterator referencing start of range to replace.
4959 * @param __i2 Iterator referencing end of range to replace.
4960 * @param __n Number of characters to insert.
4961 * @param __c Character to insert.
4962 * @return Reference to this string.
4963 * @throw std::length_error If new length exceeds @c max_size().
4965 * Removes the characters in the range [__i1,__i2). In place,
4966 * @a __n copies of @a __c are inserted. If the length of
4967 * result exceeds max_size(), length_error is thrown. The
4968 * value of the string doesn't change if an error is thrown.
4971 replace(iterator __i1
, iterator __i2
, size_type __n
, _CharT __c
)
4973 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4974 && __i2
<= _M_iend());
4975 return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __c
);
4979 * @brief Replace range of characters with range.
4980 * @param __i1 Iterator referencing start of range to replace.
4981 * @param __i2 Iterator referencing end of range to replace.
4982 * @param __k1 Iterator referencing start of range to insert.
4983 * @param __k2 Iterator referencing end of range to insert.
4984 * @return Reference to this string.
4985 * @throw std::length_error If new length exceeds @c max_size().
4987 * Removes the characters in the range [__i1,__i2). In place,
4988 * characters in the range [__k1,__k2) are inserted. If the
4989 * length of result exceeds max_size(), length_error is thrown.
4990 * The value of the string doesn't change if an error is
4993 template<class _InputIterator
>
4995 replace(iterator __i1
, iterator __i2
,
4996 _InputIterator __k1
, _InputIterator __k2
)
4998 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4999 && __i2
<= _M_iend());
5000 __glibcxx_requires_valid_range(__k1
, __k2
);
5001 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
5002 return _M_replace_dispatch(__i1
, __i2
, __k1
, __k2
, _Integral());
5005 // Specializations for the common case of pointer and iterator:
5006 // useful to avoid the overhead of temporary buffering in _M_replace.
5008 replace(iterator __i1
, iterator __i2
, _CharT
* __k1
, _CharT
* __k2
)
5010 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
5011 && __i2
<= _M_iend());
5012 __glibcxx_requires_valid_range(__k1
, __k2
);
5013 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
5018 replace(iterator __i1
, iterator __i2
,
5019 const _CharT
* __k1
, const _CharT
* __k2
)
5021 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
5022 && __i2
<= _M_iend());
5023 __glibcxx_requires_valid_range(__k1
, __k2
);
5024 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
5029 replace(iterator __i1
, iterator __i2
, iterator __k1
, iterator __k2
)
5031 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
5032 && __i2
<= _M_iend());
5033 __glibcxx_requires_valid_range(__k1
, __k2
);
5034 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
5035 __k1
.base(), __k2
- __k1
);
5039 replace(iterator __i1
, iterator __i2
,
5040 const_iterator __k1
, const_iterator __k2
)
5042 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
5043 && __i2
<= _M_iend());
5044 __glibcxx_requires_valid_range(__k1
, __k2
);
5045 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
5046 __k1
.base(), __k2
- __k1
);
5049 #if __cplusplus >= 201103L
5051 * @brief Replace range of characters with initializer_list.
5052 * @param __i1 Iterator referencing start of range to replace.
5053 * @param __i2 Iterator referencing end of range to replace.
5054 * @param __l The initializer_list of characters to insert.
5055 * @return Reference to this string.
5056 * @throw std::length_error If new length exceeds @c max_size().
5058 * Removes the characters in the range [__i1,__i2). In place,
5059 * characters in the range [__k1,__k2) are inserted. If the
5060 * length of result exceeds max_size(), length_error is thrown.
5061 * The value of the string doesn't change if an error is
5064 basic_string
& replace(iterator __i1
, iterator __i2
,
5065 initializer_list
<_CharT
> __l
)
5066 { return this->replace(__i1
, __i2
, __l
.begin(), __l
.end()); }
5069 #if __cplusplus >= 201703L
5071 * @brief Replace range of characters with string_view.
5072 * @param __pos The position to replace at.
5073 * @param __n The number of characters to replace.
5074 * @param __svt The object convertible to string_view to insert.
5075 * @return Reference to this string.
5077 template<typename _Tp
>
5078 _If_sv
<_Tp
, basic_string
&>
5079 replace(size_type __pos
, size_type __n
, const _Tp
& __svt
)
5081 __sv_type __sv
= __svt
;
5082 return this->replace(__pos
, __n
, __sv
.data(), __sv
.size());
5086 * @brief Replace range of characters with string_view.
5087 * @param __pos1 The position to replace at.
5088 * @param __n1 The number of characters to replace.
5089 * @param __svt The object convertible to string_view to insert from.
5090 * @param __pos2 The position in the string_view to insert from.
5091 * @param __n2 The number of characters to insert.
5092 * @return Reference to this string.
5094 template<typename _Tp
>
5095 _If_sv
<_Tp
, basic_string
&>
5096 replace(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
5097 size_type __pos2
, size_type __n2
= npos
)
5099 __sv_type __sv
= __svt
;
5100 return this->replace(__pos1
, __n1
,
5102 + std::__sv_check(__sv
.size(), __pos2
, "basic_string::replace"),
5103 std::__sv_limit(__sv
.size(), __pos2
, __n2
));
5107 * @brief Replace range of characters with string_view.
5108 * @param __i1 An iterator referencing the start position
5110 * @param __i2 An iterator referencing the end position
5112 * @param __svt The object convertible to string_view to insert from.
5113 * @return Reference to this string.
5115 template<typename _Tp
>
5116 _If_sv
<_Tp
, basic_string
&>
5117 replace(const_iterator __i1
, const_iterator __i2
, const _Tp
& __svt
)
5119 __sv_type __sv
= __svt
;
5120 return this->replace(__i1
- begin(), __i2
- __i1
, __sv
);
5125 template<class _Integer
>
5127 _M_replace_dispatch(iterator __i1
, iterator __i2
, _Integer __n
,
5128 _Integer __val
, __true_type
)
5129 { return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __val
); }
5131 template<class _InputIterator
>
5133 _M_replace_dispatch(iterator __i1
, iterator __i2
, _InputIterator __k1
,
5134 _InputIterator __k2
, __false_type
);
5137 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
5141 _M_replace_safe(size_type __pos1
, size_type __n1
, const _CharT
* __s
,
5144 // _S_construct_aux is used to implement the 21.3.1 para 15 which
5145 // requires special behaviour if _InIter is an integral type
5146 template<class _InIterator
>
5148 _S_construct_aux(_InIterator __beg
, _InIterator __end
,
5149 const _Alloc
& __a
, __false_type
)
5151 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
5152 return _S_construct(__beg
, __end
, __a
, _Tag());
5155 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5156 // 438. Ambiguity in the "do the right thing" clause
5157 template<class _Integer
>
5159 _S_construct_aux(_Integer __beg
, _Integer __end
,
5160 const _Alloc
& __a
, __true_type
)
5161 { return _S_construct_aux_2(static_cast<size_type
>(__beg
),
5165 _S_construct_aux_2(size_type __req
, _CharT __c
, const _Alloc
& __a
)
5166 { return _S_construct(__req
, __c
, __a
); }
5168 template<class _InIterator
>
5170 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
)
5172 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
5173 return _S_construct_aux(__beg
, __end
, __a
, _Integral());
5176 // For Input Iterators, used in istreambuf_iterators, etc.
5177 template<class _InIterator
>
5179 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
,
5180 input_iterator_tag
);
5182 // For forward_iterators up to random_access_iterators, used for
5183 // string::iterator, _CharT*, etc.
5184 template<class _FwdIterator
>
5186 _S_construct(_FwdIterator __beg
, _FwdIterator __end
, const _Alloc
& __a
,
5187 forward_iterator_tag
);
5190 _S_construct(size_type __req
, _CharT __c
, const _Alloc
& __a
);
5195 * @brief Copy substring into C string.
5196 * @param __s C string to copy value into.
5197 * @param __n Number of characters to copy.
5198 * @param __pos Index of first character to copy.
5199 * @return Number of characters actually copied
5200 * @throw std::out_of_range If __pos > size().
5202 * Copies up to @a __n characters starting at @a __pos into the
5203 * C string @a __s. If @a __pos is %greater than size(),
5204 * out_of_range is thrown.
5207 copy(_CharT
* __s
, size_type __n
, size_type __pos
= 0) const;
5210 * @brief Swap contents with another string.
5211 * @param __s String to swap with.
5213 * Exchanges the contents of this string with that of @a __s in constant
5217 swap(basic_string
& __s
)
5218 _GLIBCXX_NOEXCEPT_IF(allocator_traits
<_Alloc
>::is_always_equal::value
);
5220 // String operations:
5222 * @brief Return const pointer to null-terminated contents.
5224 * This is a handle to internal data. Do not modify or dire things may
5228 c_str() const _GLIBCXX_NOEXCEPT
5229 { return _M_data(); }
5232 * @brief Return const pointer to contents.
5234 * This is a pointer to internal data. It is undefined to modify
5235 * the contents through the returned pointer. To get a pointer that
5236 * allows modifying the contents use @c &str[0] instead,
5237 * (or in C++17 the non-const @c str.data() overload).
5240 data() const _GLIBCXX_NOEXCEPT
5241 { return _M_data(); }
5243 #if __cplusplus >= 201703L
5245 * @brief Return non-const pointer to contents.
5247 * This is a pointer to the character sequence held by the string.
5248 * Modifying the characters in the sequence is allowed.
5259 * @brief Return copy of allocator used to construct this string.
5262 get_allocator() const _GLIBCXX_NOEXCEPT
5263 { return _M_dataplus
; }
5266 * @brief Find position of a C substring.
5267 * @param __s C string to locate.
5268 * @param __pos Index of character to search from.
5269 * @param __n Number of characters from @a s to search for.
5270 * @return Index of start of first occurrence.
5272 * Starting from @a __pos, searches forward for the first @a
5273 * __n characters in @a __s within this string. If found,
5274 * returns the index where it begins. If not found, returns
5278 find(const _CharT
* __s
, size_type __pos
, size_type __n
) const
5282 * @brief Find position of a string.
5283 * @param __str String to locate.
5284 * @param __pos Index of character to search from (default 0).
5285 * @return Index of start of first occurrence.
5287 * Starting from @a __pos, searches forward for value of @a __str within
5288 * this string. If found, returns the index where it begins. If not
5289 * found, returns npos.
5292 find(const basic_string
& __str
, size_type __pos
= 0) const
5294 { return this->find(__str
.data(), __pos
, __str
.size()); }
5297 * @brief Find position of a C string.
5298 * @param __s C string to locate.
5299 * @param __pos Index of character to search from (default 0).
5300 * @return Index of start of first occurrence.
5302 * Starting from @a __pos, searches forward for the value of @a
5303 * __s within this string. If found, returns the index where
5304 * it begins. If not found, returns npos.
5307 find(const _CharT
* __s
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
5309 __glibcxx_requires_string(__s
);
5310 return this->find(__s
, __pos
, traits_type::length(__s
));
5314 * @brief Find position of a character.
5315 * @param __c Character to locate.
5316 * @param __pos Index of character to search from (default 0).
5317 * @return Index of first occurrence.
5319 * Starting from @a __pos, searches forward for @a __c within
5320 * this string. If found, returns the index where it was
5321 * found. If not found, returns npos.
5324 find(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
;
5326 #if __cplusplus >= 201703L
5328 * @brief Find position of a string_view.
5329 * @param __svt The object convertible to string_view to locate.
5330 * @param __pos Index of character to search from (default 0).
5331 * @return Index of start of first occurrence.
5333 template<typename _Tp
>
5334 _If_sv
<_Tp
, size_type
>
5335 find(const _Tp
& __svt
, size_type __pos
= 0) const
5336 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5338 __sv_type __sv
= __svt
;
5339 return this->find(__sv
.data(), __pos
, __sv
.size());
5344 * @brief Find last position of a string.
5345 * @param __str String to locate.
5346 * @param __pos Index of character to search back from (default end).
5347 * @return Index of start of last occurrence.
5349 * Starting from @a __pos, searches backward for value of @a
5350 * __str within this string. If found, returns the index where
5351 * it begins. If not found, returns npos.
5354 rfind(const basic_string
& __str
, size_type __pos
= npos
) const
5356 { return this->rfind(__str
.data(), __pos
, __str
.size()); }
5359 * @brief Find last position of a C substring.
5360 * @param __s C string to locate.
5361 * @param __pos Index of character to search back from.
5362 * @param __n Number of characters from s to search for.
5363 * @return Index of start of last occurrence.
5365 * Starting from @a __pos, searches backward for the first @a
5366 * __n characters in @a __s within this string. If found,
5367 * returns the index where it begins. If not found, returns
5371 rfind(const _CharT
* __s
, size_type __pos
, size_type __n
) const
5375 * @brief Find last position of a C string.
5376 * @param __s C string to locate.
5377 * @param __pos Index of character to start search at (default end).
5378 * @return Index of start of last occurrence.
5380 * Starting from @a __pos, searches backward for the value of
5381 * @a __s within this string. If found, returns the index
5382 * where it begins. If not found, returns npos.
5385 rfind(const _CharT
* __s
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
5387 __glibcxx_requires_string(__s
);
5388 return this->rfind(__s
, __pos
, traits_type::length(__s
));
5392 * @brief Find last position of a character.
5393 * @param __c Character to locate.
5394 * @param __pos Index of character to search back from (default end).
5395 * @return Index of last occurrence.
5397 * Starting from @a __pos, searches backward for @a __c within
5398 * this string. If found, returns the index where it was
5399 * found. If not found, returns npos.
5402 rfind(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
;
5404 #if __cplusplus >= 201703L
5406 * @brief Find last position of a string_view.
5407 * @param __svt The object convertible to string_view to locate.
5408 * @param __pos Index of character to search back from (default end).
5409 * @return Index of start of last occurrence.
5411 template<typename _Tp
>
5412 _If_sv
<_Tp
, size_type
>
5413 rfind(const _Tp
& __svt
, size_type __pos
= npos
) const
5414 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5416 __sv_type __sv
= __svt
;
5417 return this->rfind(__sv
.data(), __pos
, __sv
.size());
5422 * @brief Find position of a character of string.
5423 * @param __str String containing characters to locate.
5424 * @param __pos Index of character to search from (default 0).
5425 * @return Index of first occurrence.
5427 * Starting from @a __pos, searches forward for one of the
5428 * characters of @a __str within this string. If found,
5429 * returns the index where it was found. If not found, returns
5433 find_first_of(const basic_string
& __str
, size_type __pos
= 0) const
5435 { return this->find_first_of(__str
.data(), __pos
, __str
.size()); }
5438 * @brief Find position of a character of C substring.
5439 * @param __s String containing characters to locate.
5440 * @param __pos Index of character to search from.
5441 * @param __n Number of characters from s to search for.
5442 * @return Index of first occurrence.
5444 * Starting from @a __pos, searches forward for one of the
5445 * first @a __n characters of @a __s within this string. If
5446 * found, returns the index where it was found. If not found,
5450 find_first_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
5454 * @brief Find position of a character of C string.
5455 * @param __s String containing characters to locate.
5456 * @param __pos Index of character to search from (default 0).
5457 * @return Index of first occurrence.
5459 * Starting from @a __pos, searches forward for one of the
5460 * characters of @a __s within this string. If found, returns
5461 * the index where it was found. If not found, returns npos.
5464 find_first_of(const _CharT
* __s
, size_type __pos
= 0) const
5467 __glibcxx_requires_string(__s
);
5468 return this->find_first_of(__s
, __pos
, traits_type::length(__s
));
5472 * @brief Find position of a character.
5473 * @param __c Character to locate.
5474 * @param __pos Index of character to search from (default 0).
5475 * @return Index of first occurrence.
5477 * Starting from @a __pos, searches forward for the character
5478 * @a __c within this string. If found, returns the index
5479 * where it was found. If not found, returns npos.
5481 * Note: equivalent to find(__c, __pos).
5484 find_first_of(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
5485 { return this->find(__c
, __pos
); }
5487 #if __cplusplus >= 201703L
5489 * @brief Find position of a character of a string_view.
5490 * @param __svt An object convertible to string_view containing
5491 * characters to locate.
5492 * @param __pos Index of character to search from (default 0).
5493 * @return Index of first occurrence.
5495 template<typename _Tp
>
5496 _If_sv
<_Tp
, size_type
>
5497 find_first_of(const _Tp
& __svt
, size_type __pos
= 0) const
5498 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5500 __sv_type __sv
= __svt
;
5501 return this->find_first_of(__sv
.data(), __pos
, __sv
.size());
5506 * @brief Find last position of a character of string.
5507 * @param __str String containing characters to locate.
5508 * @param __pos Index of character to search back from (default end).
5509 * @return Index of last occurrence.
5511 * Starting from @a __pos, searches backward for one of the
5512 * characters of @a __str within this string. If found,
5513 * returns the index where it was found. If not found, returns
5517 find_last_of(const basic_string
& __str
, size_type __pos
= npos
) const
5519 { return this->find_last_of(__str
.data(), __pos
, __str
.size()); }
5522 * @brief Find last position of a character of C substring.
5523 * @param __s C string containing characters to locate.
5524 * @param __pos Index of character to search back from.
5525 * @param __n Number of characters from s to search for.
5526 * @return Index of last occurrence.
5528 * Starting from @a __pos, searches backward for one of the
5529 * first @a __n characters of @a __s within this string. If
5530 * found, returns the index where it was found. If not found,
5534 find_last_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
5538 * @brief Find last position of a character of C string.
5539 * @param __s C string containing characters to locate.
5540 * @param __pos Index of character to search back from (default end).
5541 * @return Index of last occurrence.
5543 * Starting from @a __pos, searches backward for one of the
5544 * characters of @a __s within this string. If found, returns
5545 * the index where it was found. If not found, returns npos.
5548 find_last_of(const _CharT
* __s
, size_type __pos
= npos
) const
5551 __glibcxx_requires_string(__s
);
5552 return this->find_last_of(__s
, __pos
, traits_type::length(__s
));
5556 * @brief Find last position of a character.
5557 * @param __c Character to locate.
5558 * @param __pos Index of character to search back from (default end).
5559 * @return Index of last occurrence.
5561 * Starting from @a __pos, searches backward for @a __c within
5562 * this string. If found, returns the index where it was
5563 * found. If not found, returns npos.
5565 * Note: equivalent to rfind(__c, __pos).
5568 find_last_of(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
5569 { return this->rfind(__c
, __pos
); }
5571 #if __cplusplus >= 201703L
5573 * @brief Find last position of a character of string.
5574 * @param __svt An object convertible to string_view containing
5575 * characters to locate.
5576 * @param __pos Index of character to search back from (default end).
5577 * @return Index of last occurrence.
5579 template<typename _Tp
>
5580 _If_sv
<_Tp
, size_type
>
5581 find_last_of(const _Tp
& __svt
, size_type __pos
= npos
) const
5582 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5584 __sv_type __sv
= __svt
;
5585 return this->find_last_of(__sv
.data(), __pos
, __sv
.size());
5590 * @brief Find position of a character not in string.
5591 * @param __str String containing characters to avoid.
5592 * @param __pos Index of character to search from (default 0).
5593 * @return Index of first occurrence.
5595 * Starting from @a __pos, searches forward for a character not contained
5596 * in @a __str within this string. If found, returns the index where it
5597 * was found. If not found, returns npos.
5600 find_first_not_of(const basic_string
& __str
, size_type __pos
= 0) const
5602 { return this->find_first_not_of(__str
.data(), __pos
, __str
.size()); }
5605 * @brief Find position of a character not in C substring.
5606 * @param __s C string containing characters to avoid.
5607 * @param __pos Index of character to search from.
5608 * @param __n Number of characters from __s to consider.
5609 * @return Index of first occurrence.
5611 * Starting from @a __pos, searches forward for a character not
5612 * contained in the first @a __n characters of @a __s within
5613 * this string. If found, returns the index where it was
5614 * found. If not found, returns npos.
5617 find_first_not_of(const _CharT
* __s
, size_type __pos
,
5618 size_type __n
) const _GLIBCXX_NOEXCEPT
;
5621 * @brief Find position of a character not in C string.
5622 * @param __s C string containing characters to avoid.
5623 * @param __pos Index of character to search from (default 0).
5624 * @return Index of first occurrence.
5626 * Starting from @a __pos, searches forward for a character not
5627 * contained in @a __s within this string. If found, returns
5628 * the index where it was found. If not found, returns npos.
5631 find_first_not_of(const _CharT
* __s
, size_type __pos
= 0) const
5634 __glibcxx_requires_string(__s
);
5635 return this->find_first_not_of(__s
, __pos
, traits_type::length(__s
));
5639 * @brief Find position of a different character.
5640 * @param __c Character to avoid.
5641 * @param __pos Index of character to search from (default 0).
5642 * @return Index of first occurrence.
5644 * Starting from @a __pos, searches forward for a character
5645 * other than @a __c within this string. If found, returns the
5646 * index where it was found. If not found, returns npos.
5649 find_first_not_of(_CharT __c
, size_type __pos
= 0) const
5652 #if __cplusplus >= 201703L
5654 * @brief Find position of a character not in a string_view.
5655 * @param __svt An object convertible to string_view containing
5656 * characters to avoid.
5657 * @param __pos Index of character to search from (default 0).
5658 * @return Index of first occurrence.
5660 template<typename _Tp
>
5661 _If_sv
<_Tp
, size_type
>
5662 find_first_not_of(const _Tp
& __svt
, size_type __pos
= 0) const
5663 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5665 __sv_type __sv
= __svt
;
5666 return this->find_first_not_of(__sv
.data(), __pos
, __sv
.size());
5671 * @brief Find last position of a character not in string.
5672 * @param __str String containing characters to avoid.
5673 * @param __pos Index of character to search back from (default end).
5674 * @return Index of last occurrence.
5676 * Starting from @a __pos, searches backward for a character
5677 * not contained in @a __str within this string. If found,
5678 * returns the index where it was found. If not found, returns
5682 find_last_not_of(const basic_string
& __str
, size_type __pos
= npos
) const
5684 { return this->find_last_not_of(__str
.data(), __pos
, __str
.size()); }
5687 * @brief Find last position of a character not in C substring.
5688 * @param __s C string containing characters to avoid.
5689 * @param __pos Index of character to search back from.
5690 * @param __n Number of characters from s to consider.
5691 * @return Index of last occurrence.
5693 * Starting from @a __pos, searches backward for a character not
5694 * contained in the first @a __n characters of @a __s within this string.
5695 * If found, returns the index where it was found. If not found,
5699 find_last_not_of(const _CharT
* __s
, size_type __pos
,
5700 size_type __n
) const _GLIBCXX_NOEXCEPT
;
5702 * @brief Find last position of a character not in C string.
5703 * @param __s C string containing characters to avoid.
5704 * @param __pos Index of character to search back from (default end).
5705 * @return Index of last occurrence.
5707 * Starting from @a __pos, searches backward for a character
5708 * not contained in @a __s within this string. If found,
5709 * returns the index where it was found. If not found, returns
5713 find_last_not_of(const _CharT
* __s
, size_type __pos
= npos
) const
5716 __glibcxx_requires_string(__s
);
5717 return this->find_last_not_of(__s
, __pos
, traits_type::length(__s
));
5721 * @brief Find last position of a different character.
5722 * @param __c Character to avoid.
5723 * @param __pos Index of character to search back from (default end).
5724 * @return Index of last occurrence.
5726 * Starting from @a __pos, searches backward for a character other than
5727 * @a __c within this string. If found, returns the index where it was
5728 * found. If not found, returns npos.
5731 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const
5734 #if __cplusplus >= 201703L
5736 * @brief Find last position of a character not in a string_view.
5737 * @param __svt An object convertible to string_view containing
5738 * characters to avoid.
5739 * @param __pos Index of character to search back from (default end).
5740 * @return Index of last occurrence.
5742 template<typename _Tp
>
5743 _If_sv
<_Tp
, size_type
>
5744 find_last_not_of(const _Tp
& __svt
, size_type __pos
= npos
) const
5745 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5747 __sv_type __sv
= __svt
;
5748 return this->find_last_not_of(__sv
.data(), __pos
, __sv
.size());
5753 * @brief Get a substring.
5754 * @param __pos Index of first character (default 0).
5755 * @param __n Number of characters in substring (default remainder).
5756 * @return The new string.
5757 * @throw std::out_of_range If __pos > size().
5759 * Construct and return a new string using the @a __n
5760 * characters starting at @a __pos. If the string is too
5761 * short, use the remainder of the characters. If @a __pos is
5762 * beyond the end of the string, out_of_range is thrown.
5765 substr(size_type __pos
= 0, size_type __n
= npos
) const
5766 { return basic_string(*this,
5767 _M_check(__pos
, "basic_string::substr"), __n
); }
5770 * @brief Compare to a string.
5771 * @param __str String to compare against.
5772 * @return Integer < 0, 0, or > 0.
5774 * Returns an integer < 0 if this string is ordered before @a
5775 * __str, 0 if their values are equivalent, or > 0 if this
5776 * string is ordered after @a __str. Determines the effective
5777 * length rlen of the strings to compare as the smallest of
5778 * size() and str.size(). The function then compares the two
5779 * strings by calling traits::compare(data(), str.data(),rlen).
5780 * If the result of the comparison is nonzero returns it,
5781 * otherwise the shorter one is ordered first.
5784 compare(const basic_string
& __str
) const
5786 const size_type __size
= this->size();
5787 const size_type __osize
= __str
.size();
5788 const size_type __len
= std::min(__size
, __osize
);
5790 int __r
= traits_type::compare(_M_data(), __str
.data(), __len
);
5792 __r
= _S_compare(__size
, __osize
);
5796 #if __cplusplus >= 201703L
5798 * @brief Compare to a string_view.
5799 * @param __svt An object convertible to string_view to compare against.
5800 * @return Integer < 0, 0, or > 0.
5802 template<typename _Tp
>
5804 compare(const _Tp
& __svt
) const
5805 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5807 __sv_type __sv
= __svt
;
5808 const size_type __size
= this->size();
5809 const size_type __osize
= __sv
.size();
5810 const size_type __len
= std::min(__size
, __osize
);
5812 int __r
= traits_type::compare(_M_data(), __sv
.data(), __len
);
5814 __r
= _S_compare(__size
, __osize
);
5819 * @brief Compare to a string_view.
5820 * @param __pos A position in the string to start comparing from.
5821 * @param __n The number of characters to compare.
5822 * @param __svt An object convertible to string_view to compare
5824 * @return Integer < 0, 0, or > 0.
5826 template<typename _Tp
>
5828 compare(size_type __pos
, size_type __n
, const _Tp
& __svt
) const
5829 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5831 __sv_type __sv
= __svt
;
5832 return __sv_type(*this).substr(__pos
, __n
).compare(__sv
);
5836 * @brief Compare to a string_view.
5837 * @param __pos1 A position in the string to start comparing from.
5838 * @param __n1 The number of characters to compare.
5839 * @param __svt An object convertible to string_view to compare
5841 * @param __pos2 A position in the string_view to start comparing from.
5842 * @param __n2 The number of characters to compare.
5843 * @return Integer < 0, 0, or > 0.
5845 template<typename _Tp
>
5847 compare(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
5848 size_type __pos2
, size_type __n2
= npos
) const
5849 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5851 __sv_type __sv
= __svt
;
5852 return __sv_type(*this)
5853 .substr(__pos1
, __n1
).compare(__sv
.substr(__pos2
, __n2
));
5858 * @brief Compare substring to a string.
5859 * @param __pos Index of first character of substring.
5860 * @param __n Number of characters in substring.
5861 * @param __str String to compare against.
5862 * @return Integer < 0, 0, or > 0.
5864 * Form the substring of this string from the @a __n characters
5865 * starting at @a __pos. Returns an integer < 0 if the
5866 * substring is ordered before @a __str, 0 if their values are
5867 * equivalent, or > 0 if the substring is ordered after @a
5868 * __str. Determines the effective length rlen of the strings
5869 * to compare as the smallest of the length of the substring
5870 * and @a __str.size(). The function then compares the two
5871 * strings by calling
5872 * traits::compare(substring.data(),str.data(),rlen). If the
5873 * result of the comparison is nonzero returns it, otherwise
5874 * the shorter one is ordered first.
5877 compare(size_type __pos
, size_type __n
, const basic_string
& __str
) const;
5880 * @brief Compare substring to a substring.
5881 * @param __pos1 Index of first character of substring.
5882 * @param __n1 Number of characters in substring.
5883 * @param __str String to compare against.
5884 * @param __pos2 Index of first character of substring of str.
5885 * @param __n2 Number of characters in substring of str.
5886 * @return Integer < 0, 0, or > 0.
5888 * Form the substring of this string from the @a __n1
5889 * characters starting at @a __pos1. Form the substring of @a
5890 * __str from the @a __n2 characters starting at @a __pos2.
5891 * Returns an integer < 0 if this substring is ordered before
5892 * the substring of @a __str, 0 if their values are equivalent,
5893 * or > 0 if this substring is ordered after the substring of
5894 * @a __str. Determines the effective length rlen of the
5895 * strings to compare as the smallest of the lengths of the
5896 * substrings. The function then compares the two strings by
5898 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
5899 * If the result of the comparison is nonzero returns it,
5900 * otherwise the shorter one is ordered first.
5903 compare(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
5904 size_type __pos2
, size_type __n2
= npos
) const;
5907 * @brief Compare to a C string.
5908 * @param __s C string to compare against.
5909 * @return Integer < 0, 0, or > 0.
5911 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
5912 * their values are equivalent, or > 0 if this string is ordered after
5913 * @a __s. Determines the effective length rlen of the strings to
5914 * compare as the smallest of size() and the length of a string
5915 * constructed from @a __s. The function then compares the two strings
5916 * by calling traits::compare(data(),s,rlen). If the result of the
5917 * comparison is nonzero returns it, otherwise the shorter one is
5921 compare(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
;
5923 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5924 // 5 String::compare specification questionable
5926 * @brief Compare substring to a C string.
5927 * @param __pos Index of first character of substring.
5928 * @param __n1 Number of characters in substring.
5929 * @param __s C string to compare against.
5930 * @return Integer < 0, 0, or > 0.
5932 * Form the substring of this string from the @a __n1
5933 * characters starting at @a pos. Returns an integer < 0 if
5934 * the substring is ordered before @a __s, 0 if their values
5935 * are equivalent, or > 0 if the substring is ordered after @a
5936 * __s. Determines the effective length rlen of the strings to
5937 * compare as the smallest of the length of the substring and
5938 * the length of a string constructed from @a __s. The
5939 * function then compares the two string by calling
5940 * traits::compare(substring.data(),__s,rlen). If the result of
5941 * the comparison is nonzero returns it, otherwise the shorter
5942 * one is ordered first.
5945 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
) const;
5948 * @brief Compare substring against a character %array.
5949 * @param __pos Index of first character of substring.
5950 * @param __n1 Number of characters in substring.
5951 * @param __s character %array to compare against.
5952 * @param __n2 Number of characters of s.
5953 * @return Integer < 0, 0, or > 0.
5955 * Form the substring of this string from the @a __n1
5956 * characters starting at @a __pos. Form a string from the
5957 * first @a __n2 characters of @a __s. Returns an integer < 0
5958 * if this substring is ordered before the string from @a __s,
5959 * 0 if their values are equivalent, or > 0 if this substring
5960 * is ordered after the string from @a __s. Determines the
5961 * effective length rlen of the strings to compare as the
5962 * smallest of the length of the substring and @a __n2. The
5963 * function then compares the two strings by calling
5964 * traits::compare(substring.data(),s,rlen). If the result of
5965 * the comparison is nonzero returns it, otherwise the shorter
5966 * one is ordered first.
5968 * NB: s must have at least n2 characters, '\\0' has
5969 * no special meaning.
5972 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
,
5973 size_type __n2
) const;
5975 #if __cplusplus > 201703L
5977 starts_with(basic_string_view
<_CharT
, _Traits
> __x
) const noexcept
5978 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
5981 starts_with(_CharT __x
) const noexcept
5982 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
5985 starts_with(const _CharT
* __x
) const noexcept
5986 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
5989 ends_with(basic_string_view
<_CharT
, _Traits
> __x
) const noexcept
5990 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
5993 ends_with(_CharT __x
) const noexcept
5994 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
5997 ends_with(const _CharT
* __x
) const noexcept
5998 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
6001 # ifdef _GLIBCXX_TM_TS_INTERNAL
6003 ::_txnal_cow_string_C1_for_exceptions(void* that
, const char* s
,
6006 ::_txnal_cow_string_c_str(const void *that
);
6008 ::_txnal_cow_string_D1(void *that
);
6010 ::_txnal_cow_string_D1_commit(void *that
);
6013 #endif // !_GLIBCXX_USE_CXX11_ABI
6015 #if __cpp_deduction_guides >= 201606
6016 _GLIBCXX_BEGIN_NAMESPACE_CXX11
6017 template<typename _InputIterator
, typename _CharT
6018 = typename iterator_traits
<_InputIterator
>::value_type
,
6019 typename _Allocator
= allocator
<_CharT
>,
6020 typename
= _RequireInputIter
<_InputIterator
>,
6021 typename
= _RequireAllocator
<_Allocator
>>
6022 basic_string(_InputIterator
, _InputIterator
, _Allocator
= _Allocator())
6023 -> basic_string
<_CharT
, char_traits
<_CharT
>, _Allocator
>;
6025 // _GLIBCXX_RESOLVE_LIB_DEFECTS
6026 // 3075. basic_string needs deduction guides from basic_string_view
6027 template<typename _CharT
, typename _Traits
,
6028 typename _Allocator
= allocator
<_CharT
>,
6029 typename
= _RequireAllocator
<_Allocator
>>
6030 basic_string(basic_string_view
<_CharT
, _Traits
>, const _Allocator
& = _Allocator())
6031 -> basic_string
<_CharT
, _Traits
, _Allocator
>;
6033 template<typename _CharT
, typename _Traits
,
6034 typename _Allocator
= allocator
<_CharT
>,
6035 typename
= _RequireAllocator
<_Allocator
>>
6036 basic_string(basic_string_view
<_CharT
, _Traits
>,
6037 typename basic_string
<_CharT
, _Traits
, _Allocator
>::size_type
,
6038 typename basic_string
<_CharT
, _Traits
, _Allocator
>::size_type
,
6039 const _Allocator
& = _Allocator())
6040 -> basic_string
<_CharT
, _Traits
, _Allocator
>;
6041 _GLIBCXX_END_NAMESPACE_CXX11
6046 * @brief Concatenate two strings.
6047 * @param __lhs First string.
6048 * @param __rhs Last string.
6049 * @return New string with value of @a __lhs followed by @a __rhs.
6051 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6052 basic_string
<_CharT
, _Traits
, _Alloc
>
6053 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6054 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6056 basic_string
<_CharT
, _Traits
, _Alloc
> __str(__lhs
);
6057 __str
.append(__rhs
);
6062 * @brief Concatenate C string and string.
6063 * @param __lhs First string.
6064 * @param __rhs Last string.
6065 * @return New string with value of @a __lhs followed by @a __rhs.
6067 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6068 basic_string
<_CharT
,_Traits
,_Alloc
>
6069 operator+(const _CharT
* __lhs
,
6070 const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
);
6073 * @brief Concatenate character and string.
6074 * @param __lhs First string.
6075 * @param __rhs Last string.
6076 * @return New string with @a __lhs followed by @a __rhs.
6078 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6079 basic_string
<_CharT
,_Traits
,_Alloc
>
6080 operator+(_CharT __lhs
, const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
);
6083 * @brief Concatenate string and C string.
6084 * @param __lhs First string.
6085 * @param __rhs Last string.
6086 * @return New string with @a __lhs followed by @a __rhs.
6088 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6089 inline basic_string
<_CharT
, _Traits
, _Alloc
>
6090 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6091 const _CharT
* __rhs
)
6093 basic_string
<_CharT
, _Traits
, _Alloc
> __str(__lhs
);
6094 __str
.append(__rhs
);
6099 * @brief Concatenate string and character.
6100 * @param __lhs First string.
6101 * @param __rhs Last string.
6102 * @return New string with @a __lhs followed by @a __rhs.
6104 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6105 inline basic_string
<_CharT
, _Traits
, _Alloc
>
6106 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
, _CharT __rhs
)
6108 typedef basic_string
<_CharT
, _Traits
, _Alloc
> __string_type
;
6109 typedef typename
__string_type::size_type __size_type
;
6110 __string_type
__str(__lhs
);
6111 __str
.append(__size_type(1), __rhs
);
6115 #if __cplusplus >= 201103L
6116 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6117 inline basic_string
<_CharT
, _Traits
, _Alloc
>
6118 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
6119 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6120 { return std::move(__lhs
.append(__rhs
)); }
6122 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6123 inline basic_string
<_CharT
, _Traits
, _Alloc
>
6124 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6125 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
6126 { return std::move(__rhs
.insert(0, __lhs
)); }
6128 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6129 inline basic_string
<_CharT
, _Traits
, _Alloc
>
6130 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
6131 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
6133 #if _GLIBCXX_USE_CXX11_ABI
6134 using _Alloc_traits
= allocator_traits
<_Alloc
>;
6135 bool __use_rhs
= false;
6136 if _GLIBCXX17_CONSTEXPR (typename
_Alloc_traits::is_always_equal
{})
6138 else if (__lhs
.get_allocator() == __rhs
.get_allocator())
6143 const auto __size
= __lhs
.size() + __rhs
.size();
6144 if (__size
> __lhs
.capacity() && __size
<= __rhs
.capacity())
6145 return std::move(__rhs
.insert(0, __lhs
));
6147 return std::move(__lhs
.append(__rhs
));
6150 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6151 inline basic_string
<_CharT
, _Traits
, _Alloc
>
6152 operator+(const _CharT
* __lhs
,
6153 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
6154 { return std::move(__rhs
.insert(0, __lhs
)); }
6156 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6157 inline basic_string
<_CharT
, _Traits
, _Alloc
>
6158 operator+(_CharT __lhs
,
6159 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
6160 { return std::move(__rhs
.insert(0, 1, __lhs
)); }
6162 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6163 inline basic_string
<_CharT
, _Traits
, _Alloc
>
6164 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
6165 const _CharT
* __rhs
)
6166 { return std::move(__lhs
.append(__rhs
)); }
6168 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6169 inline basic_string
<_CharT
, _Traits
, _Alloc
>
6170 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
6172 { return std::move(__lhs
.append(1, __rhs
)); }
6177 * @brief Test equivalence of two strings.
6178 * @param __lhs First string.
6179 * @param __rhs Second string.
6180 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
6182 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6184 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6185 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6187 { return __lhs
.compare(__rhs
) == 0; }
6189 template<typename _CharT
>
6191 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
, bool>::__type
6192 operator==(const basic_string
<_CharT
>& __lhs
,
6193 const basic_string
<_CharT
>& __rhs
) _GLIBCXX_NOEXCEPT
6194 { return (__lhs
.size() == __rhs
.size()
6195 && !std::char_traits
<_CharT
>::compare(__lhs
.data(), __rhs
.data(),
6199 * @brief Test equivalence of string and C string.
6200 * @param __lhs String.
6201 * @param __rhs C string.
6202 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
6204 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6206 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6207 const _CharT
* __rhs
)
6208 { return __lhs
.compare(__rhs
) == 0; }
6210 #if __cpp_lib_three_way_comparison
6212 * @brief Three-way comparison of a string and a C string.
6213 * @param __lhs A string.
6214 * @param __rhs A null-terminated string.
6215 * @return A value indicating whether `__lhs` is less than, equal to,
6216 * greater than, or incomparable with `__rhs`.
6218 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6220 operator<=>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6221 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
) noexcept
6222 -> decltype(__detail::__char_traits_cmp_cat
<_Traits
>(0))
6223 { return __detail::__char_traits_cmp_cat
<_Traits
>(__lhs
.compare(__rhs
)); }
6226 * @brief Three-way comparison of a string and a C string.
6227 * @param __lhs A string.
6228 * @param __rhs A null-terminated string.
6229 * @return A value indicating whether `__lhs` is less than, equal to,
6230 * greater than, or incomparable with `__rhs`.
6232 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6234 operator<=>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6235 const _CharT
* __rhs
) noexcept
6236 -> decltype(__detail::__char_traits_cmp_cat
<_Traits
>(0))
6237 { return __detail::__char_traits_cmp_cat
<_Traits
>(__lhs
.compare(__rhs
)); }
6240 * @brief Test equivalence of C string and string.
6241 * @param __lhs C string.
6242 * @param __rhs String.
6243 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
6245 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6247 operator==(const _CharT
* __lhs
,
6248 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6249 { return __rhs
.compare(__lhs
) == 0; }
6253 * @brief Test difference of two strings.
6254 * @param __lhs First string.
6255 * @param __rhs Second string.
6256 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
6258 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6260 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6261 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6263 { return !(__lhs
== __rhs
); }
6266 * @brief Test difference of C string and string.
6267 * @param __lhs C string.
6268 * @param __rhs String.
6269 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
6271 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6273 operator!=(const _CharT
* __lhs
,
6274 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6275 { return !(__lhs
== __rhs
); }
6278 * @brief Test difference of string and C string.
6279 * @param __lhs String.
6280 * @param __rhs C string.
6281 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
6283 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6285 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6286 const _CharT
* __rhs
)
6287 { return !(__lhs
== __rhs
); }
6291 * @brief Test if string precedes string.
6292 * @param __lhs First string.
6293 * @param __rhs Second string.
6294 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6296 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6298 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6299 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6301 { return __lhs
.compare(__rhs
) < 0; }
6304 * @brief Test if string precedes C string.
6305 * @param __lhs String.
6306 * @param __rhs C string.
6307 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6309 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6311 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6312 const _CharT
* __rhs
)
6313 { return __lhs
.compare(__rhs
) < 0; }
6316 * @brief Test if C string precedes string.
6317 * @param __lhs C string.
6318 * @param __rhs String.
6319 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6321 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6323 operator<(const _CharT
* __lhs
,
6324 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6325 { return __rhs
.compare(__lhs
) > 0; }
6329 * @brief Test if string follows string.
6330 * @param __lhs First string.
6331 * @param __rhs Second string.
6332 * @return True if @a __lhs follows @a __rhs. False otherwise.
6334 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6336 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6337 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6339 { return __lhs
.compare(__rhs
) > 0; }
6342 * @brief Test if string follows C string.
6343 * @param __lhs String.
6344 * @param __rhs C string.
6345 * @return True if @a __lhs follows @a __rhs. False otherwise.
6347 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6349 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6350 const _CharT
* __rhs
)
6351 { return __lhs
.compare(__rhs
) > 0; }
6354 * @brief Test if C string follows string.
6355 * @param __lhs C string.
6356 * @param __rhs String.
6357 * @return True if @a __lhs follows @a __rhs. False otherwise.
6359 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6361 operator>(const _CharT
* __lhs
,
6362 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6363 { return __rhs
.compare(__lhs
) < 0; }
6367 * @brief Test if string doesn't follow string.
6368 * @param __lhs First string.
6369 * @param __rhs Second string.
6370 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6372 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6374 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6375 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6377 { return __lhs
.compare(__rhs
) <= 0; }
6380 * @brief Test if string doesn't follow C string.
6381 * @param __lhs String.
6382 * @param __rhs C string.
6383 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6385 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6387 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6388 const _CharT
* __rhs
)
6389 { return __lhs
.compare(__rhs
) <= 0; }
6392 * @brief Test if C string doesn't follow string.
6393 * @param __lhs C string.
6394 * @param __rhs String.
6395 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6397 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6399 operator<=(const _CharT
* __lhs
,
6400 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6401 { return __rhs
.compare(__lhs
) >= 0; }
6405 * @brief Test if string doesn't precede string.
6406 * @param __lhs First string.
6407 * @param __rhs Second string.
6408 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6410 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6412 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6413 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6415 { return __lhs
.compare(__rhs
) >= 0; }
6418 * @brief Test if string doesn't precede C string.
6419 * @param __lhs String.
6420 * @param __rhs C string.
6421 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6423 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6425 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6426 const _CharT
* __rhs
)
6427 { return __lhs
.compare(__rhs
) >= 0; }
6430 * @brief Test if C string doesn't precede string.
6431 * @param __lhs C string.
6432 * @param __rhs String.
6433 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6435 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6437 operator>=(const _CharT
* __lhs
,
6438 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6439 { return __rhs
.compare(__lhs
) <= 0; }
6440 #endif // three-way comparison
6443 * @brief Swap contents of two strings.
6444 * @param __lhs First string.
6445 * @param __rhs Second string.
6447 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
6449 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6451 swap(basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6452 basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6453 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs
.swap(__rhs
)))
6454 { __lhs
.swap(__rhs
); }
6458 * @brief Read stream into a string.
6459 * @param __is Input stream.
6460 * @param __str Buffer to store into.
6461 * @return Reference to the input stream.
6463 * Stores characters from @a __is into @a __str until whitespace is
6464 * found, the end of the stream is encountered, or str.max_size()
6465 * is reached. If is.width() is non-zero, that is the limit on the
6466 * number of characters stored into @a __str. Any previous
6467 * contents of @a __str are erased.
6469 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6470 basic_istream
<_CharT
, _Traits
>&
6471 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
6472 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
);
6475 basic_istream
<char>&
6476 operator>>(basic_istream
<char>& __is
, basic_string
<char>& __str
);
6479 * @brief Write string to a stream.
6480 * @param __os Output stream.
6481 * @param __str String to write out.
6482 * @return Reference to the output stream.
6484 * Output characters of @a __str into os following the same rules as for
6485 * writing a C string.
6487 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6488 inline basic_ostream
<_CharT
, _Traits
>&
6489 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
6490 const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
6492 // _GLIBCXX_RESOLVE_LIB_DEFECTS
6493 // 586. string inserter not a formatted function
6494 return __ostream_insert(__os
, __str
.data(), __str
.size());
6498 * @brief Read a line from stream into a string.
6499 * @param __is Input stream.
6500 * @param __str Buffer to store into.
6501 * @param __delim Character marking end of line.
6502 * @return Reference to the input stream.
6504 * Stores characters from @a __is into @a __str until @a __delim is
6505 * found, the end of the stream is encountered, or str.max_size()
6506 * is reached. Any previous contents of @a __str are erased. If
6507 * @a __delim is encountered, it is extracted but not stored into
6510 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6511 basic_istream
<_CharT
, _Traits
>&
6512 getline(basic_istream
<_CharT
, _Traits
>& __is
,
6513 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
);
6516 * @brief Read a line from stream into a string.
6517 * @param __is Input stream.
6518 * @param __str Buffer to store into.
6519 * @return Reference to the input stream.
6521 * Stores characters from is into @a __str until '\n' is
6522 * found, the end of the stream is encountered, or str.max_size()
6523 * is reached. Any previous contents of @a __str are erased. If
6524 * end of line is encountered, it is extracted but not stored into
6527 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6528 inline basic_istream
<_CharT
, _Traits
>&
6529 getline(basic_istream
<_CharT
, _Traits
>& __is
,
6530 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
6531 { return std::getline(__is
, __str
, __is
.widen('\n')); }
6533 #if __cplusplus >= 201103L
6534 /// Read a line from an rvalue stream into a string.
6535 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6536 inline basic_istream
<_CharT
, _Traits
>&
6537 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
6538 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
)
6539 { return std::getline(__is
, __str
, __delim
); }
6541 /// Read a line from an rvalue stream into a string.
6542 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6543 inline basic_istream
<_CharT
, _Traits
>&
6544 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
6545 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
6546 { return std::getline(__is
, __str
); }
6550 basic_istream
<char>&
6551 getline(basic_istream
<char>& __in
, basic_string
<char>& __str
,
6554 #ifdef _GLIBCXX_USE_WCHAR_T
6556 basic_istream
<wchar_t>&
6557 getline(basic_istream
<wchar_t>& __in
, basic_string
<wchar_t>& __str
,
6561 _GLIBCXX_END_NAMESPACE_VERSION
6564 #if __cplusplus >= 201103L
6566 #include <ext/string_conversions.h>
6567 #include <bits/charconv.h>
6569 namespace std
_GLIBCXX_VISIBILITY(default)
6571 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6572 _GLIBCXX_BEGIN_NAMESPACE_CXX11
6574 #if _GLIBCXX_USE_C99_STDLIB
6575 // 21.4 Numeric Conversions [string.conversions].
6577 stoi(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6578 { return __gnu_cxx::__stoa
<long, int>(&std::strtol
, "stoi", __str
.c_str(),
6582 stol(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6583 { return __gnu_cxx::__stoa(&std::strtol
, "stol", __str
.c_str(),
6586 inline unsigned long
6587 stoul(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6588 { return __gnu_cxx::__stoa(&std::strtoul
, "stoul", __str
.c_str(),
6592 stoll(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6593 { return __gnu_cxx::__stoa(&std::strtoll
, "stoll", __str
.c_str(),
6596 inline unsigned long long
6597 stoull(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6598 { return __gnu_cxx::__stoa(&std::strtoull
, "stoull", __str
.c_str(),
6601 // NB: strtof vs strtod.
6603 stof(const string
& __str
, size_t* __idx
= 0)
6604 { return __gnu_cxx::__stoa(&std::strtof
, "stof", __str
.c_str(), __idx
); }
6607 stod(const string
& __str
, size_t* __idx
= 0)
6608 { return __gnu_cxx::__stoa(&std::strtod
, "stod", __str
.c_str(), __idx
); }
6611 stold(const string
& __str
, size_t* __idx
= 0)
6612 { return __gnu_cxx::__stoa(&std::strtold
, "stold", __str
.c_str(), __idx
); }
6613 #endif // _GLIBCXX_USE_C99_STDLIB
6615 // DR 1261. Insufficent overloads for to_string / to_wstring
6618 to_string(int __val
)
6620 const bool __neg
= __val
< 0;
6621 const unsigned __uval
= __neg
? (unsigned)~__val
+ 1u : __val
;
6622 const auto __len
= __detail::__to_chars_len(__uval
);
6623 string
__str(__neg
+ __len
, '-');
6624 __detail::__to_chars_10_impl(&__str
[__neg
], __len
, __uval
);
6629 to_string(unsigned __val
)
6631 string
__str(__detail::__to_chars_len(__val
), '\0');
6632 __detail::__to_chars_10_impl(&__str
[0], __str
.size(), __val
);
6637 to_string(long __val
)
6639 const bool __neg
= __val
< 0;
6640 const unsigned long __uval
= __neg
? (unsigned long)~__val
+ 1ul : __val
;
6641 const auto __len
= __detail::__to_chars_len(__uval
);
6642 string
__str(__neg
+ __len
, '-');
6643 __detail::__to_chars_10_impl(&__str
[__neg
], __len
, __uval
);
6648 to_string(unsigned long __val
)
6650 string
__str(__detail::__to_chars_len(__val
), '\0');
6651 __detail::__to_chars_10_impl(&__str
[0], __str
.size(), __val
);
6656 to_string(long long __val
)
6658 const bool __neg
= __val
< 0;
6659 const unsigned long long __uval
6660 = __neg
? (unsigned long long)~__val
+ 1ull : __val
;
6661 const auto __len
= __detail::__to_chars_len(__uval
);
6662 string
__str(__neg
+ __len
, '-');
6663 __detail::__to_chars_10_impl(&__str
[__neg
], __len
, __uval
);
6668 to_string(unsigned long long __val
)
6670 string
__str(__detail::__to_chars_len(__val
), '\0');
6671 __detail::__to_chars_10_impl(&__str
[0], __str
.size(), __val
);
6675 #if _GLIBCXX_USE_C99_STDIO
6676 // NB: (v)snprintf vs sprintf.
6679 to_string(float __val
)
6682 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
6683 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
6688 to_string(double __val
)
6691 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
6692 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
6697 to_string(long double __val
)
6700 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
6701 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
6704 #endif // _GLIBCXX_USE_C99_STDIO
6706 #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
6708 stoi(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6709 { return __gnu_cxx::__stoa
<long, int>(&std::wcstol
, "stoi", __str
.c_str(),
6713 stol(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6714 { return __gnu_cxx::__stoa(&std::wcstol
, "stol", __str
.c_str(),
6717 inline unsigned long
6718 stoul(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6719 { return __gnu_cxx::__stoa(&std::wcstoul
, "stoul", __str
.c_str(),
6723 stoll(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6724 { return __gnu_cxx::__stoa(&std::wcstoll
, "stoll", __str
.c_str(),
6727 inline unsigned long long
6728 stoull(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6729 { return __gnu_cxx::__stoa(&std::wcstoull
, "stoull", __str
.c_str(),
6732 // NB: wcstof vs wcstod.
6734 stof(const wstring
& __str
, size_t* __idx
= 0)
6735 { return __gnu_cxx::__stoa(&std::wcstof
, "stof", __str
.c_str(), __idx
); }
6738 stod(const wstring
& __str
, size_t* __idx
= 0)
6739 { return __gnu_cxx::__stoa(&std::wcstod
, "stod", __str
.c_str(), __idx
); }
6742 stold(const wstring
& __str
, size_t* __idx
= 0)
6743 { return __gnu_cxx::__stoa(&std::wcstold
, "stold", __str
.c_str(), __idx
); }
6745 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6748 to_wstring(int __val
)
6749 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(int),
6753 to_wstring(unsigned __val
)
6754 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
6755 4 * sizeof(unsigned),
6759 to_wstring(long __val
)
6760 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(long),
6764 to_wstring(unsigned long __val
)
6765 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
6766 4 * sizeof(unsigned long),
6770 to_wstring(long long __val
)
6771 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
6772 4 * sizeof(long long),
6776 to_wstring(unsigned long long __val
)
6777 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
6778 4 * sizeof(unsigned long long),
6782 to_wstring(float __val
)
6785 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
6786 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
6791 to_wstring(double __val
)
6794 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
6795 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
6800 to_wstring(long double __val
)
6803 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
6804 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
6807 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6808 #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
6810 _GLIBCXX_END_NAMESPACE_CXX11
6811 _GLIBCXX_END_NAMESPACE_VERSION
6816 #if __cplusplus >= 201103L
6818 #include <bits/functional_hash.h>
6820 namespace std
_GLIBCXX_VISIBILITY(default)
6822 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6826 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
6827 /// std::hash specialization for string.
6830 : public __hash_base
<size_t, string
>
6833 operator()(const string
& __s
) const noexcept
6834 { return std::_Hash_impl::hash(__s
.data(), __s
.length()); }
6838 struct __is_fast_hash
<hash
<string
>> : std::false_type
6841 #ifdef _GLIBCXX_USE_WCHAR_T
6842 /// std::hash specialization for wstring.
6844 struct hash
<wstring
>
6845 : public __hash_base
<size_t, wstring
>
6848 operator()(const wstring
& __s
) const noexcept
6849 { return std::_Hash_impl::hash(__s
.data(),
6850 __s
.length() * sizeof(wchar_t)); }
6854 struct __is_fast_hash
<hash
<wstring
>> : std::false_type
6857 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
6859 #ifdef _GLIBCXX_USE_CHAR8_T
6860 /// std::hash specialization for u8string.
6862 struct hash
<u8string
>
6863 : public __hash_base
<size_t, u8string
>
6866 operator()(const u8string
& __s
) const noexcept
6867 { return std::_Hash_impl::hash(__s
.data(),
6868 __s
.length() * sizeof(char8_t
)); }
6872 struct __is_fast_hash
<hash
<u8string
>> : std::false_type
6876 /// std::hash specialization for u16string.
6878 struct hash
<u16string
>
6879 : public __hash_base
<size_t, u16string
>
6882 operator()(const u16string
& __s
) const noexcept
6883 { return std::_Hash_impl::hash(__s
.data(),
6884 __s
.length() * sizeof(char16_t
)); }
6888 struct __is_fast_hash
<hash
<u16string
>> : std::false_type
6891 /// std::hash specialization for u32string.
6893 struct hash
<u32string
>
6894 : public __hash_base
<size_t, u32string
>
6897 operator()(const u32string
& __s
) const noexcept
6898 { return std::_Hash_impl::hash(__s
.data(),
6899 __s
.length() * sizeof(char32_t
)); }
6903 struct __is_fast_hash
<hash
<u32string
>> : std::false_type
6906 #if __cplusplus >= 201402L
6908 #define __cpp_lib_string_udls 201304
6910 inline namespace literals
6912 inline namespace string_literals
6914 #pragma GCC diagnostic push
6915 #pragma GCC diagnostic ignored "-Wliteral-suffix"
6916 _GLIBCXX_DEFAULT_ABI_TAG
6917 inline basic_string
<char>
6918 operator""s(const char* __str
, size_t __len
)
6919 { return basic_string
<char>{__str
, __len
}; }
6921 #ifdef _GLIBCXX_USE_WCHAR_T
6922 _GLIBCXX_DEFAULT_ABI_TAG
6923 inline basic_string
<wchar_t>
6924 operator""s(const wchar_t* __str
, size_t __len
)
6925 { return basic_string
<wchar_t>{__str
, __len
}; }
6928 #ifdef _GLIBCXX_USE_CHAR8_T
6929 _GLIBCXX_DEFAULT_ABI_TAG
6930 inline basic_string
<char8_t
>
6931 operator""s(const char8_t
* __str
, size_t __len
)
6932 { return basic_string
<char8_t
>{__str
, __len
}; }
6935 _GLIBCXX_DEFAULT_ABI_TAG
6936 inline basic_string
<char16_t
>
6937 operator""s(const char16_t
* __str
, size_t __len
)
6938 { return basic_string
<char16_t
>{__str
, __len
}; }
6940 _GLIBCXX_DEFAULT_ABI_TAG
6941 inline basic_string
<char32_t
>
6942 operator""s(const char32_t
* __str
, size_t __len
)
6943 { return basic_string
<char32_t
>{__str
, __len
}; }
6945 #pragma GCC diagnostic pop
6946 } // inline namespace string_literals
6947 } // inline namespace literals
6949 #if __cplusplus >= 201703L
6950 namespace __detail::__variant
6952 template<typename
> struct _Never_valueless_alt
; // see <variant>
6954 // Provide the strong exception-safety guarantee when emplacing a
6955 // basic_string into a variant, but only if moving the string cannot throw.
6956 template<typename _Tp
, typename _Traits
, typename _Alloc
>
6957 struct _Never_valueless_alt
<std::basic_string
<_Tp
, _Traits
, _Alloc
>>
6959 is_nothrow_move_constructible
<std::basic_string
<_Tp
, _Traits
, _Alloc
>>,
6960 is_nothrow_move_assignable
<std::basic_string
<_Tp
, _Traits
, _Alloc
>>
6963 } // namespace __detail::__variant
6967 _GLIBCXX_END_NAMESPACE_VERSION
6972 #endif /* _BASIC_STRING_H */