1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997-2017 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 > 201402L
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
;
111 #if __cplusplus > 201402L
112 // A helper type for avoiding boiler-plate.
113 typedef basic_string_view
<_CharT
, _Traits
> __sv_type
;
115 template<typename _Tp
, typename _Res
>
116 using _If_sv
= enable_if_t
<
117 __and_
<is_convertible
<const _Tp
&, __sv_type
>,
118 __not_
<is_convertible
<const _Tp
&, const _CharT
*>>>::value
,
122 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
123 struct _Alloc_hider
: allocator_type
// TODO check __is_final
125 #if __cplusplus < 201103L
126 _Alloc_hider(pointer __dat
, const _Alloc
& __a
= _Alloc())
127 : allocator_type(__a
), _M_p(__dat
) { }
129 _Alloc_hider(pointer __dat
, const _Alloc
& __a
)
130 : allocator_type(__a
), _M_p(__dat
) { }
132 _Alloc_hider(pointer __dat
, _Alloc
&& __a
= _Alloc())
133 : allocator_type(std::move(__a
)), _M_p(__dat
) { }
136 pointer _M_p
; // The actual data.
139 _Alloc_hider _M_dataplus
;
140 size_type _M_string_length
;
142 enum { _S_local_capacity
= 15 / sizeof(_CharT
) };
146 _CharT _M_local_buf
[_S_local_capacity
+ 1];
147 size_type _M_allocated_capacity
;
152 { _M_dataplus
._M_p
= __p
; }
155 _M_length(size_type __length
)
156 { _M_string_length
= __length
; }
160 { return _M_dataplus
._M_p
; }
165 #if __cplusplus >= 201103L
166 return std::pointer_traits
<pointer
>::pointer_to(*_M_local_buf
);
168 return pointer(_M_local_buf
);
173 _M_local_data() const
175 #if __cplusplus >= 201103L
176 return std::pointer_traits
<const_pointer
>::pointer_to(*_M_local_buf
);
178 return const_pointer(_M_local_buf
);
183 _M_capacity(size_type __capacity
)
184 { _M_allocated_capacity
= __capacity
; }
187 _M_set_length(size_type __n
)
190 traits_type::assign(_M_data()[__n
], _CharT());
195 { return _M_data() == _M_local_data(); }
199 _M_create(size_type
&, size_type
);
205 _M_destroy(_M_allocated_capacity
);
209 _M_destroy(size_type __size
) throw()
210 { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size
+ 1); }
212 // _M_construct_aux is used to implement the 21.3.1 para 15 which
213 // requires special behaviour if _InIterator is an integral type
214 template<typename _InIterator
>
216 _M_construct_aux(_InIterator __beg
, _InIterator __end
,
219 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
220 _M_construct(__beg
, __end
, _Tag());
223 // _GLIBCXX_RESOLVE_LIB_DEFECTS
224 // 438. Ambiguity in the "do the right thing" clause
225 template<typename _Integer
>
227 _M_construct_aux(_Integer __beg
, _Integer __end
, std::__true_type
)
228 { _M_construct_aux_2(static_cast<size_type
>(__beg
), __end
); }
231 _M_construct_aux_2(size_type __req
, _CharT __c
)
232 { _M_construct(__req
, __c
); }
234 template<typename _InIterator
>
236 _M_construct(_InIterator __beg
, _InIterator __end
)
238 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
239 _M_construct_aux(__beg
, __end
, _Integral());
242 // For Input Iterators, used in istreambuf_iterators, etc.
243 template<typename _InIterator
>
245 _M_construct(_InIterator __beg
, _InIterator __end
,
246 std::input_iterator_tag
);
248 // For forward_iterators up to random_access_iterators, used for
249 // string::iterator, _CharT*, etc.
250 template<typename _FwdIterator
>
252 _M_construct(_FwdIterator __beg
, _FwdIterator __end
,
253 std::forward_iterator_tag
);
256 _M_construct(size_type __req
, _CharT __c
);
260 { return _M_dataplus
; }
262 const allocator_type
&
263 _M_get_allocator() const
264 { return _M_dataplus
; }
268 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
269 // The explicit instantiations in misc-inst.cc require this due to
270 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
271 template<typename _Tp
, bool _Requires
=
272 !__are_same
<_Tp
, _CharT
*>::__value
273 && !__are_same
<_Tp
, const _CharT
*>::__value
274 && !__are_same
<_Tp
, iterator
>::__value
275 && !__are_same
<_Tp
, const_iterator
>::__value
>
276 struct __enable_if_not_native_iterator
277 { typedef basic_string
& __type
; };
278 template<typename _Tp
>
279 struct __enable_if_not_native_iterator
<_Tp
, false> { };
283 _M_check(size_type __pos
, const char* __s
) const
285 if (__pos
> this->size())
286 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
287 "this->size() (which is %zu)"),
288 __s
, __pos
, this->size());
293 _M_check_length(size_type __n1
, size_type __n2
, const char* __s
) const
295 if (this->max_size() - (this->size() - __n1
) < __n2
)
296 __throw_length_error(__N(__s
));
300 // NB: _M_limit doesn't check for a bad __pos value.
302 _M_limit(size_type __pos
, size_type __off
) const _GLIBCXX_NOEXCEPT
304 const bool __testoff
= __off
< this->size() - __pos
;
305 return __testoff
? __off
: this->size() - __pos
;
308 // True if _Rep and source do not overlap.
310 _M_disjunct(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
312 return (less
<const _CharT
*>()(__s
, _M_data())
313 || less
<const _CharT
*>()(_M_data() + this->size(), __s
));
316 // When __n = 1 way faster than the general multichar
317 // traits_type::copy/move/assign.
319 _S_copy(_CharT
* __d
, const _CharT
* __s
, size_type __n
)
322 traits_type::assign(*__d
, *__s
);
324 traits_type::copy(__d
, __s
, __n
);
328 _S_move(_CharT
* __d
, const _CharT
* __s
, size_type __n
)
331 traits_type::assign(*__d
, *__s
);
333 traits_type::move(__d
, __s
, __n
);
337 _S_assign(_CharT
* __d
, size_type __n
, _CharT __c
)
340 traits_type::assign(*__d
, __c
);
342 traits_type::assign(__d
, __n
, __c
);
345 // _S_copy_chars is a separate template to permit specialization
346 // to optimize for the common case of pointers as iterators.
347 template<class _Iterator
>
349 _S_copy_chars(_CharT
* __p
, _Iterator __k1
, _Iterator __k2
)
351 for (; __k1
!= __k2
; ++__k1
, (void)++__p
)
352 traits_type::assign(*__p
, *__k1
); // These types are off.
356 _S_copy_chars(_CharT
* __p
, iterator __k1
, iterator __k2
) _GLIBCXX_NOEXCEPT
357 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
360 _S_copy_chars(_CharT
* __p
, const_iterator __k1
, const_iterator __k2
)
362 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
365 _S_copy_chars(_CharT
* __p
, _CharT
* __k1
, _CharT
* __k2
) _GLIBCXX_NOEXCEPT
366 { _S_copy(__p
, __k1
, __k2
- __k1
); }
369 _S_copy_chars(_CharT
* __p
, const _CharT
* __k1
, const _CharT
* __k2
)
371 { _S_copy(__p
, __k1
, __k2
- __k1
); }
374 _S_compare(size_type __n1
, size_type __n2
) _GLIBCXX_NOEXCEPT
376 const difference_type __d
= difference_type(__n1
- __n2
);
378 if (__d
> __gnu_cxx::__numeric_traits
<int>::__max
)
379 return __gnu_cxx::__numeric_traits
<int>::__max
;
380 else if (__d
< __gnu_cxx::__numeric_traits
<int>::__min
)
381 return __gnu_cxx::__numeric_traits
<int>::__min
;
387 _M_assign(const basic_string
&);
390 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
394 _M_erase(size_type __pos
, size_type __n
);
397 // Construct/copy/destroy:
398 // NB: We overload ctors in some cases instead of using default
399 // arguments, per 17.4.4.4 para. 2 item 2.
402 * @brief Default constructor creates an empty string.
405 _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible
<_Alloc
>::value
)
406 : _M_dataplus(_M_local_data())
407 { _M_set_length(0); }
410 * @brief Construct an empty string using allocator @a a.
413 basic_string(const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
414 : _M_dataplus(_M_local_data(), __a
)
415 { _M_set_length(0); }
418 * @brief Construct string with copy of value of @a __str.
419 * @param __str Source string.
421 basic_string(const basic_string
& __str
)
422 : _M_dataplus(_M_local_data(),
423 _Alloc_traits::_S_select_on_copy(__str
._M_get_allocator()))
424 { _M_construct(__str
._M_data(), __str
._M_data() + __str
.length()); }
426 // _GLIBCXX_RESOLVE_LIB_DEFECTS
427 // 2583. no way to supply an allocator for basic_string(str, pos)
429 * @brief Construct string as copy of a substring.
430 * @param __str Source string.
431 * @param __pos Index of first character to copy from.
432 * @param __a Allocator to use.
434 basic_string(const basic_string
& __str
, size_type __pos
,
435 const _Alloc
& __a
= _Alloc())
436 : _M_dataplus(_M_local_data(), __a
)
438 const _CharT
* __start
= __str
._M_data()
439 + __str
._M_check(__pos
, "basic_string::basic_string");
440 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, npos
));
444 * @brief Construct string as copy of a substring.
445 * @param __str Source string.
446 * @param __pos Index of first character to copy from.
447 * @param __n Number of characters to copy.
449 basic_string(const basic_string
& __str
, size_type __pos
,
451 : _M_dataplus(_M_local_data())
453 const _CharT
* __start
= __str
._M_data()
454 + __str
._M_check(__pos
, "basic_string::basic_string");
455 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, __n
));
459 * @brief Construct string as copy of a substring.
460 * @param __str Source string.
461 * @param __pos Index of first character to copy from.
462 * @param __n Number of characters to copy.
463 * @param __a Allocator to use.
465 basic_string(const basic_string
& __str
, size_type __pos
,
466 size_type __n
, const _Alloc
& __a
)
467 : _M_dataplus(_M_local_data(), __a
)
469 const _CharT
* __start
470 = __str
._M_data() + __str
._M_check(__pos
, "string::string");
471 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, __n
));
475 * @brief Construct string initialized by a character %array.
476 * @param __s Source character %array.
477 * @param __n Number of characters to copy.
478 * @param __a Allocator to use (default is default allocator).
480 * NB: @a __s must have at least @a __n characters, '\\0'
481 * has no special meaning.
483 basic_string(const _CharT
* __s
, size_type __n
,
484 const _Alloc
& __a
= _Alloc())
485 : _M_dataplus(_M_local_data(), __a
)
486 { _M_construct(__s
, __s
+ __n
); }
489 * @brief Construct string as copy of a C string.
490 * @param __s Source C string.
491 * @param __a Allocator to use (default is default allocator).
493 basic_string(const _CharT
* __s
, const _Alloc
& __a
= _Alloc())
494 : _M_dataplus(_M_local_data(), __a
)
495 { _M_construct(__s
, __s
? __s
+ traits_type::length(__s
) : __s
+npos
); }
498 * @brief Construct string as multiple characters.
499 * @param __n Number of characters.
500 * @param __c Character to use.
501 * @param __a Allocator to use (default is default allocator).
503 basic_string(size_type __n
, _CharT __c
, const _Alloc
& __a
= _Alloc())
504 : _M_dataplus(_M_local_data(), __a
)
505 { _M_construct(__n
, __c
); }
507 #if __cplusplus >= 201103L
509 * @brief Move construct string.
510 * @param __str Source string.
512 * The newly-created string contains the exact contents of @a __str.
513 * @a __str is a valid, but unspecified string.
515 basic_string(basic_string
&& __str
) noexcept
516 : _M_dataplus(_M_local_data(), std::move(__str
._M_get_allocator()))
518 if (__str
._M_is_local())
520 traits_type::copy(_M_local_buf
, __str
._M_local_buf
,
521 _S_local_capacity
+ 1);
525 _M_data(__str
._M_data());
526 _M_capacity(__str
._M_allocated_capacity
);
529 // Must use _M_length() here not _M_set_length() because
530 // basic_stringbuf relies on writing into unallocated capacity so
531 // we mess up the contents if we put a '\0' in the string.
532 _M_length(__str
.length());
533 __str
._M_data(__str
._M_local_data());
534 __str
._M_set_length(0);
538 * @brief Construct string from an initializer %list.
539 * @param __l std::initializer_list of characters.
540 * @param __a Allocator to use (default is default allocator).
542 basic_string(initializer_list
<_CharT
> __l
, const _Alloc
& __a
= _Alloc())
543 : _M_dataplus(_M_local_data(), __a
)
544 { _M_construct(__l
.begin(), __l
.end()); }
546 basic_string(const basic_string
& __str
, const _Alloc
& __a
)
547 : _M_dataplus(_M_local_data(), __a
)
548 { _M_construct(__str
.begin(), __str
.end()); }
550 basic_string(basic_string
&& __str
, const _Alloc
& __a
)
551 noexcept(_Alloc_traits::_S_always_equal())
552 : _M_dataplus(_M_local_data(), __a
)
554 if (__str
._M_is_local())
556 traits_type::copy(_M_local_buf
, __str
._M_local_buf
,
557 _S_local_capacity
+ 1);
558 _M_length(__str
.length());
559 __str
._M_set_length(0);
561 else if (_Alloc_traits::_S_always_equal()
562 || __str
.get_allocator() == __a
)
564 _M_data(__str
._M_data());
565 _M_length(__str
.length());
566 _M_capacity(__str
._M_allocated_capacity
);
567 __str
._M_data(__str
._M_local_buf
);
568 __str
._M_set_length(0);
571 _M_construct(__str
.begin(), __str
.end());
577 * @brief Construct string as copy of a range.
578 * @param __beg Start of range.
579 * @param __end End of range.
580 * @param __a Allocator to use (default is default allocator).
582 #if __cplusplus >= 201103L
583 template<typename _InputIterator
,
584 typename
= std::_RequireInputIter
<_InputIterator
>>
586 template<typename _InputIterator
>
588 basic_string(_InputIterator __beg
, _InputIterator __end
,
589 const _Alloc
& __a
= _Alloc())
590 : _M_dataplus(_M_local_data(), __a
)
591 { _M_construct(__beg
, __end
); }
593 #if __cplusplus > 201402L
595 * @brief Construct string from a substring of a string_view.
596 * @param __t Source string view.
597 * @param __pos The index of the first character to copy from __t.
598 * @param __n The number of characters to copy from __t.
599 * @param __a Allocator to use.
601 template<typename _Tp
, typename
= _If_sv
<_Tp
, void>>
602 basic_string(const _Tp
& __t
, size_type __pos
, size_type __n
,
603 const _Alloc
& __a
= _Alloc())
604 : basic_string(__sv_type(__t
).substr(__pos
, __n
), __a
) { }
607 * @brief Construct string from a string_view.
608 * @param __sv Source string view.
609 * @param __a Allocator to use (default is default allocator).
612 basic_string(__sv_type __sv
, const _Alloc
& __a
= _Alloc())
613 : basic_string(__sv
.data(), __sv
.size(), __a
) { }
617 * @brief Destroy the string instance.
623 * @brief Assign the value of @a str to this string.
624 * @param __str Source string.
627 operator=(const basic_string
& __str
)
629 #if __cplusplus >= 201103L
630 if (_Alloc_traits::_S_propagate_on_copy_assign())
632 if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
633 && _M_get_allocator() != __str
._M_get_allocator())
635 // Propagating allocator cannot free existing storage so must
636 // deallocate it before replacing current allocator.
637 if (__str
.size() <= _S_local_capacity
)
639 _M_destroy(_M_allocated_capacity
);
640 _M_data(_M_local_data());
645 const auto __len
= __str
.size();
646 auto __alloc
= __str
._M_get_allocator();
647 // If this allocation throws there are no effects:
648 auto __ptr
= _Alloc_traits::allocate(__alloc
, __len
+ 1);
649 _M_destroy(_M_allocated_capacity
);
652 _M_set_length(__len
);
655 std::__alloc_on_copy(_M_get_allocator(), __str
._M_get_allocator());
658 return this->assign(__str
);
662 * @brief Copy contents of @a s into this string.
663 * @param __s Source null-terminated string.
666 operator=(const _CharT
* __s
)
667 { return this->assign(__s
); }
670 * @brief Set value to string of length 1.
671 * @param __c Source character.
673 * Assigning to a character makes this string length 1 and
674 * (*this)[0] == @a c.
677 operator=(_CharT __c
)
679 this->assign(1, __c
);
683 #if __cplusplus >= 201103L
685 * @brief Move assign the value of @a str to this string.
686 * @param __str Source string.
688 * The contents of @a str are moved into this string (without copying).
689 * @a str is a valid, but unspecified string.
691 // PR 58265, this should be noexcept.
692 // _GLIBCXX_RESOLVE_LIB_DEFECTS
693 // 2063. Contradictory requirements for string move assignment
695 operator=(basic_string
&& __str
)
696 noexcept(_Alloc_traits::_S_nothrow_move())
698 if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
699 && !_Alloc_traits::_S_always_equal()
700 && _M_get_allocator() != __str
._M_get_allocator())
702 // Destroy existing storage before replacing allocator.
703 _M_destroy(_M_allocated_capacity
);
704 _M_data(_M_local_data());
707 // Replace allocator if POCMA is true.
708 std::__alloc_on_move(_M_get_allocator(), __str
._M_get_allocator());
710 if (!__str
._M_is_local()
711 && (_Alloc_traits::_S_propagate_on_move_assign()
712 || _Alloc_traits::_S_always_equal()))
714 pointer __data
= nullptr;
715 size_type __capacity
;
718 if (_Alloc_traits::_S_always_equal())
721 __capacity
= _M_allocated_capacity
;
724 _M_destroy(_M_allocated_capacity
);
727 _M_data(__str
._M_data());
728 _M_length(__str
.length());
729 _M_capacity(__str
._M_allocated_capacity
);
732 __str
._M_data(__data
);
733 __str
._M_capacity(__capacity
);
736 __str
._M_data(__str
._M_local_buf
);
745 * @brief Set value to string constructed from initializer %list.
746 * @param __l std::initializer_list.
749 operator=(initializer_list
<_CharT
> __l
)
751 this->assign(__l
.begin(), __l
.size());
756 #if __cplusplus > 201402L
758 * @brief Set value to string constructed from a string_view.
759 * @param __sv A string_view.
761 template<typename _Tp
>
762 _If_sv
<_Tp
, basic_string
&>
764 { return this->assign(__sv
); }
767 * @brief Convert to a string_view.
768 * @return A string_view.
770 operator __sv_type() const noexcept
771 { return __sv_type(data(), size()); }
776 * Returns a read/write iterator that points to the first character in
780 begin() _GLIBCXX_NOEXCEPT
781 { return iterator(_M_data()); }
784 * Returns a read-only (constant) iterator that points to the first
785 * character in the %string.
788 begin() const _GLIBCXX_NOEXCEPT
789 { return const_iterator(_M_data()); }
792 * Returns a read/write iterator that points one past the last
793 * character in the %string.
796 end() _GLIBCXX_NOEXCEPT
797 { return iterator(_M_data() + this->size()); }
800 * Returns a read-only (constant) iterator that points one past the
801 * last character in the %string.
804 end() const _GLIBCXX_NOEXCEPT
805 { return const_iterator(_M_data() + this->size()); }
808 * Returns a read/write reverse iterator that points to the last
809 * character in the %string. Iteration is done in reverse element
813 rbegin() _GLIBCXX_NOEXCEPT
814 { return reverse_iterator(this->end()); }
817 * Returns a read-only (constant) reverse iterator that points
818 * to the last character in the %string. Iteration is done in
819 * reverse element order.
821 const_reverse_iterator
822 rbegin() const _GLIBCXX_NOEXCEPT
823 { return const_reverse_iterator(this->end()); }
826 * Returns a read/write reverse iterator that points to one before the
827 * first character in the %string. Iteration is done in reverse
831 rend() _GLIBCXX_NOEXCEPT
832 { return reverse_iterator(this->begin()); }
835 * Returns a read-only (constant) reverse iterator that points
836 * to one before the first character in the %string. Iteration
837 * is done in reverse element order.
839 const_reverse_iterator
840 rend() const _GLIBCXX_NOEXCEPT
841 { return const_reverse_iterator(this->begin()); }
843 #if __cplusplus >= 201103L
845 * Returns a read-only (constant) iterator that points to the first
846 * character in the %string.
849 cbegin() const noexcept
850 { return const_iterator(this->_M_data()); }
853 * Returns a read-only (constant) iterator that points one past the
854 * last character in the %string.
857 cend() const noexcept
858 { return const_iterator(this->_M_data() + this->size()); }
861 * Returns a read-only (constant) reverse iterator that points
862 * to the last character in the %string. Iteration is done in
863 * reverse element order.
865 const_reverse_iterator
866 crbegin() const noexcept
867 { return const_reverse_iterator(this->end()); }
870 * Returns a read-only (constant) reverse iterator that points
871 * to one before the first character in the %string. Iteration
872 * is done in reverse element order.
874 const_reverse_iterator
875 crend() const noexcept
876 { return const_reverse_iterator(this->begin()); }
881 /// Returns the number of characters in the string, not including any
882 /// null-termination.
884 size() const _GLIBCXX_NOEXCEPT
885 { return _M_string_length
; }
887 /// Returns the number of characters in the string, not including any
888 /// null-termination.
890 length() const _GLIBCXX_NOEXCEPT
891 { return _M_string_length
; }
893 /// Returns the size() of the largest possible %string.
895 max_size() const _GLIBCXX_NOEXCEPT
896 { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
899 * @brief Resizes the %string to the specified number of characters.
900 * @param __n Number of characters the %string should contain.
901 * @param __c Character to fill any new elements.
903 * This function will %resize the %string to the specified
904 * number of characters. If the number is smaller than the
905 * %string's current size the %string is truncated, otherwise
906 * the %string is extended and new elements are %set to @a __c.
909 resize(size_type __n
, _CharT __c
);
912 * @brief Resizes the %string to the specified number of characters.
913 * @param __n Number of characters the %string should contain.
915 * This function will resize the %string to the specified length. If
916 * the new size is smaller than the %string's current size the %string
917 * is truncated, otherwise the %string is extended and new characters
918 * are default-constructed. For basic types such as char, this means
922 resize(size_type __n
)
923 { this->resize(__n
, _CharT()); }
925 #if __cplusplus >= 201103L
926 /// A non-binding request to reduce capacity() to size().
928 shrink_to_fit() noexcept
931 if (capacity() > size())
943 * Returns the total number of characters that the %string can hold
944 * before needing to allocate more memory.
947 capacity() const _GLIBCXX_NOEXCEPT
949 return _M_is_local() ? size_type(_S_local_capacity
)
950 : _M_allocated_capacity
;
954 * @brief Attempt to preallocate enough memory for specified number of
956 * @param __res_arg Number of characters required.
957 * @throw std::length_error If @a __res_arg exceeds @c max_size().
959 * This function attempts to reserve enough memory for the
960 * %string to hold the specified number of characters. If the
961 * number requested is more than max_size(), length_error is
964 * The advantage of this function is that if optimal code is a
965 * necessity and the user can determine the string length that will be
966 * required, the user can reserve the memory in %advance, and thus
967 * prevent a possible reallocation of memory and copying of %string
971 reserve(size_type __res_arg
= 0);
974 * Erases the string, making it empty.
977 clear() _GLIBCXX_NOEXCEPT
978 { _M_set_length(0); }
981 * Returns true if the %string is empty. Equivalent to
982 * <code>*this == ""</code>.
985 empty() const _GLIBCXX_NOEXCEPT
986 { return this->size() == 0; }
990 * @brief Subscript access to the data contained in the %string.
991 * @param __pos The index of the character to access.
992 * @return Read-only (constant) reference to the character.
994 * This operator allows for easy, array-style, data access.
995 * Note that data access with this operator is unchecked and
996 * out_of_range lookups are not defined. (For checked lookups
1000 operator[] (size_type __pos
) const _GLIBCXX_NOEXCEPT
1002 __glibcxx_assert(__pos
<= size());
1003 return _M_data()[__pos
];
1007 * @brief Subscript access to the data contained in the %string.
1008 * @param __pos The index of the character to access.
1009 * @return Read/write reference to the character.
1011 * This operator allows for easy, array-style, data access.
1012 * Note that data access with this operator is unchecked and
1013 * out_of_range lookups are not defined. (For checked lookups
1017 operator[](size_type __pos
)
1019 // Allow pos == size() both in C++98 mode, as v3 extension,
1020 // and in C++11 mode.
1021 __glibcxx_assert(__pos
<= size());
1022 // In pedantic mode be strict in C++98 mode.
1023 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus
>= 201103L || __pos
< size());
1024 return _M_data()[__pos
];
1028 * @brief Provides access to the data contained in the %string.
1029 * @param __n The index of the character to access.
1030 * @return Read-only (const) reference to the character.
1031 * @throw std::out_of_range If @a n is an invalid index.
1033 * This function provides for safer data access. The parameter is
1034 * first checked that it is in the range of the string. The function
1035 * throws out_of_range if the check fails.
1038 at(size_type __n
) const
1040 if (__n
>= this->size())
1041 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1042 "(which is %zu) >= this->size() "
1045 return _M_data()[__n
];
1049 * @brief Provides access to the data contained in the %string.
1050 * @param __n The index of the character to access.
1051 * @return Read/write reference to the character.
1052 * @throw std::out_of_range If @a n is an invalid index.
1054 * This function provides for safer data access. The parameter is
1055 * first checked that it is in the range of the string. The function
1056 * throws out_of_range if the check fails.
1062 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1063 "(which is %zu) >= this->size() "
1066 return _M_data()[__n
];
1069 #if __cplusplus >= 201103L
1071 * Returns a read/write reference to the data at the first
1072 * element of the %string.
1077 __glibcxx_assert(!empty());
1078 return operator[](0);
1082 * Returns a read-only (constant) reference to the data at the first
1083 * element of the %string.
1086 front() const noexcept
1088 __glibcxx_assert(!empty());
1089 return operator[](0);
1093 * Returns a read/write reference to the data at the last
1094 * element of the %string.
1099 __glibcxx_assert(!empty());
1100 return operator[](this->size() - 1);
1104 * Returns a read-only (constant) reference to the data at the
1105 * last element of the %string.
1108 back() const noexcept
1110 __glibcxx_assert(!empty());
1111 return operator[](this->size() - 1);
1117 * @brief Append a string to this string.
1118 * @param __str The string to append.
1119 * @return Reference to this string.
1122 operator+=(const basic_string
& __str
)
1123 { return this->append(__str
); }
1126 * @brief Append a C string.
1127 * @param __s The C string to append.
1128 * @return Reference to this string.
1131 operator+=(const _CharT
* __s
)
1132 { return this->append(__s
); }
1135 * @brief Append a character.
1136 * @param __c The character to append.
1137 * @return Reference to this string.
1140 operator+=(_CharT __c
)
1142 this->push_back(__c
);
1146 #if __cplusplus >= 201103L
1148 * @brief Append an initializer_list of characters.
1149 * @param __l The initializer_list of characters to be appended.
1150 * @return Reference to this string.
1153 operator+=(initializer_list
<_CharT
> __l
)
1154 { return this->append(__l
.begin(), __l
.size()); }
1157 #if __cplusplus > 201402L
1159 * @brief Append a string_view.
1160 * @param __sv The string_view to be appended.
1161 * @return Reference to this string.
1164 operator+=(__sv_type __sv
)
1165 { return this->append(__sv
); }
1169 * @brief Append a string to this string.
1170 * @param __str The string to append.
1171 * @return Reference to this string.
1174 append(const basic_string
& __str
)
1175 { return _M_append(__str
._M_data(), __str
.size()); }
1178 * @brief Append a substring.
1179 * @param __str The string to append.
1180 * @param __pos Index of the first character of str to append.
1181 * @param __n The number of characters to append.
1182 * @return Reference to this string.
1183 * @throw std::out_of_range if @a __pos is not a valid index.
1185 * This function appends @a __n characters from @a __str
1186 * starting at @a __pos to this string. If @a __n is is larger
1187 * than the number of available characters in @a __str, the
1188 * remainder of @a __str is appended.
1191 append(const basic_string
& __str
, size_type __pos
, size_type __n
)
1192 { return _M_append(__str
._M_data()
1193 + __str
._M_check(__pos
, "basic_string::append"),
1194 __str
._M_limit(__pos
, __n
)); }
1197 * @brief Append a C substring.
1198 * @param __s The C string to append.
1199 * @param __n The number of characters to append.
1200 * @return Reference to this string.
1203 append(const _CharT
* __s
, size_type __n
)
1205 __glibcxx_requires_string_len(__s
, __n
);
1206 _M_check_length(size_type(0), __n
, "basic_string::append");
1207 return _M_append(__s
, __n
);
1211 * @brief Append a C string.
1212 * @param __s The C string to append.
1213 * @return Reference to this string.
1216 append(const _CharT
* __s
)
1218 __glibcxx_requires_string(__s
);
1219 const size_type __n
= traits_type::length(__s
);
1220 _M_check_length(size_type(0), __n
, "basic_string::append");
1221 return _M_append(__s
, __n
);
1225 * @brief Append multiple characters.
1226 * @param __n The number of characters to append.
1227 * @param __c The character to use.
1228 * @return Reference to this string.
1230 * Appends __n copies of __c to this string.
1233 append(size_type __n
, _CharT __c
)
1234 { return _M_replace_aux(this->size(), size_type(0), __n
, __c
); }
1236 #if __cplusplus >= 201103L
1238 * @brief Append an initializer_list of characters.
1239 * @param __l The initializer_list of characters to append.
1240 * @return Reference to this string.
1243 append(initializer_list
<_CharT
> __l
)
1244 { return this->append(__l
.begin(), __l
.size()); }
1248 * @brief Append a range of characters.
1249 * @param __first Iterator referencing the first character to append.
1250 * @param __last Iterator marking the end of the range.
1251 * @return Reference to this string.
1253 * Appends characters in the range [__first,__last) to this string.
1255 #if __cplusplus >= 201103L
1256 template<class _InputIterator
,
1257 typename
= std::_RequireInputIter
<_InputIterator
>>
1259 template<class _InputIterator
>
1262 append(_InputIterator __first
, _InputIterator __last
)
1263 { return this->replace(end(), end(), __first
, __last
); }
1265 #if __cplusplus > 201402L
1267 * @brief Append a string_view.
1268 * @param __sv The string_view to be appended.
1269 * @return Reference to this string.
1272 append(__sv_type __sv
)
1273 { return this->append(__sv
.data(), __sv
.size()); }
1276 * @brief Append a range of characters from a string_view.
1277 * @param __sv The string_view to be appended from.
1278 * @param __pos The position in the string_view to append from.
1279 * @param __n The number of characters to append from the string_view.
1280 * @return Reference to this string.
1282 template <typename _Tp
>
1283 _If_sv
<_Tp
, basic_string
&>
1284 append(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
1286 __sv_type __sv
= __svt
;
1287 return _M_append(__sv
.data()
1288 + __sv
._M_check(__pos
, "basic_string::append"),
1289 __sv
._M_limit(__pos
, __n
));
1294 * @brief Append a single character.
1295 * @param __c Character to append.
1298 push_back(_CharT __c
)
1300 const size_type __size
= this->size();
1301 if (__size
+ 1 > this->capacity())
1302 this->_M_mutate(__size
, size_type(0), 0, size_type(1));
1303 traits_type::assign(this->_M_data()[__size
], __c
);
1304 this->_M_set_length(__size
+ 1);
1308 * @brief Set value to contents of another string.
1309 * @param __str Source string to use.
1310 * @return Reference to this string.
1313 assign(const basic_string
& __str
)
1315 this->_M_assign(__str
);
1319 #if __cplusplus >= 201103L
1321 * @brief Set value to contents of another string.
1322 * @param __str Source string to use.
1323 * @return Reference to this string.
1325 * This function sets this string to the exact contents of @a __str.
1326 * @a __str is a valid, but unspecified string.
1329 assign(basic_string
&& __str
)
1330 noexcept(_Alloc_traits::_S_nothrow_move())
1332 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1333 // 2063. Contradictory requirements for string move assignment
1334 return *this = std::move(__str
);
1339 * @brief Set value to a substring of a string.
1340 * @param __str The string to use.
1341 * @param __pos Index of the first character of str.
1342 * @param __n Number of characters to use.
1343 * @return Reference to this string.
1344 * @throw std::out_of_range if @a pos is not a valid index.
1346 * This function sets this string to the substring of @a __str
1347 * consisting of @a __n characters at @a __pos. If @a __n is
1348 * is larger than the number of available characters in @a
1349 * __str, the remainder of @a __str is used.
1352 assign(const basic_string
& __str
, size_type __pos
, size_type __n
)
1353 { return _M_replace(size_type(0), this->size(), __str
._M_data()
1354 + __str
._M_check(__pos
, "basic_string::assign"),
1355 __str
._M_limit(__pos
, __n
)); }
1358 * @brief Set value to a C substring.
1359 * @param __s The C string to use.
1360 * @param __n Number of characters to use.
1361 * @return Reference to this string.
1363 * This function sets the value of this string to the first @a __n
1364 * characters of @a __s. If @a __n is is larger than the number of
1365 * available characters in @a __s, the remainder of @a __s is used.
1368 assign(const _CharT
* __s
, size_type __n
)
1370 __glibcxx_requires_string_len(__s
, __n
);
1371 return _M_replace(size_type(0), this->size(), __s
, __n
);
1375 * @brief Set value to contents of a C string.
1376 * @param __s The C string to use.
1377 * @return Reference to this string.
1379 * This function sets the value of this string to the value of @a __s.
1380 * The data is copied, so there is no dependence on @a __s once the
1384 assign(const _CharT
* __s
)
1386 __glibcxx_requires_string(__s
);
1387 return _M_replace(size_type(0), this->size(), __s
,
1388 traits_type::length(__s
));
1392 * @brief Set value to multiple characters.
1393 * @param __n Length of the resulting string.
1394 * @param __c The character to use.
1395 * @return Reference to this string.
1397 * This function sets the value of this string to @a __n copies of
1401 assign(size_type __n
, _CharT __c
)
1402 { return _M_replace_aux(size_type(0), this->size(), __n
, __c
); }
1405 * @brief Set value to a range of characters.
1406 * @param __first Iterator referencing the first character to append.
1407 * @param __last Iterator marking the end of the range.
1408 * @return Reference to this string.
1410 * Sets value of string to characters in the range [__first,__last).
1412 #if __cplusplus >= 201103L
1413 template<class _InputIterator
,
1414 typename
= std::_RequireInputIter
<_InputIterator
>>
1416 template<class _InputIterator
>
1419 assign(_InputIterator __first
, _InputIterator __last
)
1420 { return this->replace(begin(), end(), __first
, __last
); }
1422 #if __cplusplus >= 201103L
1424 * @brief Set value to an initializer_list of characters.
1425 * @param __l The initializer_list of characters to assign.
1426 * @return Reference to this string.
1429 assign(initializer_list
<_CharT
> __l
)
1430 { return this->assign(__l
.begin(), __l
.size()); }
1433 #if __cplusplus > 201402L
1435 * @brief Set value from a string_view.
1436 * @param __sv The source string_view.
1437 * @return Reference to this string.
1440 assign(__sv_type __sv
)
1441 { return this->assign(__sv
.data(), __sv
.size()); }
1444 * @brief Set value from a range of characters in a string_view.
1445 * @param __sv The source string_view.
1446 * @param __pos The position in the string_view to assign from.
1447 * @param __n The number of characters to assign.
1448 * @return Reference to this string.
1450 template <typename _Tp
>
1451 _If_sv
<_Tp
, basic_string
&>
1452 assign(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
1454 __sv_type __sv
= __svt
;
1455 return _M_replace(size_type(0), this->size(), __sv
.data()
1456 + __sv
._M_check(__pos
, "basic_string::assign"),
1457 __sv
._M_limit(__pos
, __n
));
1461 #if __cplusplus >= 201103L
1463 * @brief Insert multiple characters.
1464 * @param __p Const_iterator referencing location in string to
1466 * @param __n Number of characters to insert
1467 * @param __c The character to insert.
1468 * @return Iterator referencing the first inserted char.
1469 * @throw std::length_error If new length exceeds @c max_size().
1471 * Inserts @a __n copies of character @a __c starting at the
1472 * position referenced by iterator @a __p. If adding
1473 * characters causes the length to exceed max_size(),
1474 * length_error is thrown. The value of the string doesn't
1475 * change if an error is thrown.
1478 insert(const_iterator __p
, size_type __n
, _CharT __c
)
1480 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1481 const size_type __pos
= __p
- begin();
1482 this->replace(__p
, __p
, __n
, __c
);
1483 return iterator(this->_M_data() + __pos
);
1487 * @brief Insert multiple characters.
1488 * @param __p Iterator referencing location in string to insert at.
1489 * @param __n Number of characters to insert
1490 * @param __c The character to insert.
1491 * @throw std::length_error If new length exceeds @c max_size().
1493 * Inserts @a __n copies of character @a __c starting at the
1494 * position referenced by iterator @a __p. If adding
1495 * characters causes the length to exceed max_size(),
1496 * length_error is thrown. The value of the string doesn't
1497 * change if an error is thrown.
1500 insert(iterator __p
, size_type __n
, _CharT __c
)
1501 { this->replace(__p
, __p
, __n
, __c
); }
1504 #if __cplusplus >= 201103L
1506 * @brief Insert a range of characters.
1507 * @param __p Const_iterator referencing location in string to
1509 * @param __beg Start of range.
1510 * @param __end End of range.
1511 * @return Iterator referencing the first inserted char.
1512 * @throw std::length_error If new length exceeds @c max_size().
1514 * Inserts characters in range [beg,end). If adding characters
1515 * causes the length to exceed max_size(), length_error is
1516 * thrown. The value of the string doesn't change if an error
1519 template<class _InputIterator
,
1520 typename
= std::_RequireInputIter
<_InputIterator
>>
1522 insert(const_iterator __p
, _InputIterator __beg
, _InputIterator __end
)
1524 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1525 const size_type __pos
= __p
- begin();
1526 this->replace(__p
, __p
, __beg
, __end
);
1527 return iterator(this->_M_data() + __pos
);
1531 * @brief Insert a range of characters.
1532 * @param __p Iterator referencing location in string to insert at.
1533 * @param __beg Start of range.
1534 * @param __end End of range.
1535 * @throw std::length_error If new length exceeds @c max_size().
1537 * Inserts characters in range [__beg,__end). If adding
1538 * characters causes the length to exceed max_size(),
1539 * length_error is thrown. The value of the string doesn't
1540 * change if an error is thrown.
1542 template<class _InputIterator
>
1544 insert(iterator __p
, _InputIterator __beg
, _InputIterator __end
)
1545 { this->replace(__p
, __p
, __beg
, __end
); }
1548 #if __cplusplus >= 201103L
1550 * @brief Insert an initializer_list of characters.
1551 * @param __p Iterator referencing location in string to insert at.
1552 * @param __l The initializer_list of characters to insert.
1553 * @throw std::length_error If new length exceeds @c max_size().
1556 insert(iterator __p
, initializer_list
<_CharT
> __l
)
1558 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1559 this->insert(__p
- begin(), __l
.begin(), __l
.size());
1564 * @brief Insert value of a string.
1565 * @param __pos1 Iterator referencing location in string to insert at.
1566 * @param __str The string to insert.
1567 * @return Reference to this string.
1568 * @throw std::length_error If new length exceeds @c max_size().
1570 * Inserts value of @a __str starting at @a __pos1. If adding
1571 * characters causes the length to exceed max_size(),
1572 * length_error is thrown. The value of the string doesn't
1573 * change if an error is thrown.
1576 insert(size_type __pos1
, const basic_string
& __str
)
1577 { return this->replace(__pos1
, size_type(0),
1578 __str
._M_data(), __str
.size()); }
1581 * @brief Insert a substring.
1582 * @param __pos1 Iterator referencing location in string to insert at.
1583 * @param __str The string to insert.
1584 * @param __pos2 Start of characters in str to insert.
1585 * @param __n Number of characters to insert.
1586 * @return Reference to this string.
1587 * @throw std::length_error If new length exceeds @c max_size().
1588 * @throw std::out_of_range If @a pos1 > size() or
1589 * @a __pos2 > @a str.size().
1591 * Starting at @a pos1, insert @a __n character of @a __str
1592 * beginning with @a __pos2. If adding characters causes the
1593 * length to exceed max_size(), length_error is thrown. If @a
1594 * __pos1 is beyond the end of this string or @a __pos2 is
1595 * beyond the end of @a __str, out_of_range is thrown. The
1596 * value of the string doesn't change if an error is thrown.
1599 insert(size_type __pos1
, const basic_string
& __str
,
1600 size_type __pos2
, size_type __n
)
1601 { return this->replace(__pos1
, size_type(0), __str
._M_data()
1602 + __str
._M_check(__pos2
, "basic_string::insert"),
1603 __str
._M_limit(__pos2
, __n
)); }
1606 * @brief Insert a C substring.
1607 * @param __pos Iterator referencing location in string to insert at.
1608 * @param __s The C string to insert.
1609 * @param __n The number of characters to insert.
1610 * @return Reference to this string.
1611 * @throw std::length_error If new length exceeds @c max_size().
1612 * @throw std::out_of_range If @a __pos is beyond the end of this
1615 * Inserts the first @a __n characters of @a __s starting at @a
1616 * __pos. If adding characters causes the length to exceed
1617 * max_size(), length_error is thrown. If @a __pos is beyond
1618 * end(), out_of_range is thrown. The value of the string
1619 * doesn't change if an error is thrown.
1622 insert(size_type __pos
, const _CharT
* __s
, size_type __n
)
1623 { return this->replace(__pos
, size_type(0), __s
, __n
); }
1626 * @brief Insert a C string.
1627 * @param __pos Iterator referencing location in string to insert at.
1628 * @param __s The C string to insert.
1629 * @return Reference to this string.
1630 * @throw std::length_error If new length exceeds @c max_size().
1631 * @throw std::out_of_range If @a pos is beyond the end of this
1634 * Inserts the first @a n characters of @a __s starting at @a __pos. If
1635 * adding characters causes the length to exceed max_size(),
1636 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
1637 * thrown. The value of the string doesn't change if an error is
1641 insert(size_type __pos
, const _CharT
* __s
)
1643 __glibcxx_requires_string(__s
);
1644 return this->replace(__pos
, size_type(0), __s
,
1645 traits_type::length(__s
));
1649 * @brief Insert multiple characters.
1650 * @param __pos Index in string to insert at.
1651 * @param __n Number of characters to insert
1652 * @param __c The character to insert.
1653 * @return Reference to this string.
1654 * @throw std::length_error If new length exceeds @c max_size().
1655 * @throw std::out_of_range If @a __pos is beyond the end of this
1658 * Inserts @a __n copies of character @a __c starting at index
1659 * @a __pos. If adding characters causes the length to exceed
1660 * max_size(), length_error is thrown. If @a __pos > length(),
1661 * out_of_range is thrown. The value of the string doesn't
1662 * change if an error is thrown.
1665 insert(size_type __pos
, size_type __n
, _CharT __c
)
1666 { return _M_replace_aux(_M_check(__pos
, "basic_string::insert"),
1667 size_type(0), __n
, __c
); }
1670 * @brief Insert one character.
1671 * @param __p Iterator referencing position in string to insert at.
1672 * @param __c The character to insert.
1673 * @return Iterator referencing newly inserted char.
1674 * @throw std::length_error If new length exceeds @c max_size().
1676 * Inserts character @a __c at position referenced by @a __p.
1677 * If adding character causes the length to exceed max_size(),
1678 * length_error is thrown. If @a __p is beyond end of string,
1679 * out_of_range is thrown. The value of the string doesn't
1680 * change if an error is thrown.
1683 insert(__const_iterator __p
, _CharT __c
)
1685 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1686 const size_type __pos
= __p
- begin();
1687 _M_replace_aux(__pos
, size_type(0), size_type(1), __c
);
1688 return iterator(_M_data() + __pos
);
1691 #if __cplusplus > 201402L
1693 * @brief Insert a string_view.
1694 * @param __pos Iterator referencing position in string to insert at.
1695 * @param __sv The string_view to insert.
1696 * @return Reference to this string.
1699 insert(size_type __pos
, __sv_type __sv
)
1700 { return this->insert(__pos
, __sv
.data(), __sv
.size()); }
1703 * @brief Insert a string_view.
1704 * @param __pos Iterator referencing position in string to insert at.
1705 * @param __sv The string_view to insert from.
1706 * @param __pos Iterator referencing position in string_view to insert
1708 * @param __n The number of characters to insert.
1709 * @return Reference to this string.
1711 template <typename _Tp
>
1712 _If_sv
<_Tp
, basic_string
&>
1713 insert(size_type __pos1
, const _Tp
& __svt
,
1714 size_type __pos2
, size_type __n
= npos
)
1716 __sv_type __sv
= __svt
;
1717 return this->replace(__pos1
, size_type(0), __sv
.data()
1718 + __sv
._M_check(__pos2
, "basic_string::insert"),
1719 __sv
._M_limit(__pos2
, __n
));
1724 * @brief Remove characters.
1725 * @param __pos Index of first character to remove (default 0).
1726 * @param __n Number of characters to remove (default remainder).
1727 * @return Reference to this string.
1728 * @throw std::out_of_range If @a pos is beyond the end of this
1731 * Removes @a __n characters from this string starting at @a
1732 * __pos. The length of the string is reduced by @a __n. If
1733 * there are < @a __n characters to remove, the remainder of
1734 * the string is truncated. If @a __p is beyond end of string,
1735 * out_of_range is thrown. The value of the string doesn't
1736 * change if an error is thrown.
1739 erase(size_type __pos
= 0, size_type __n
= npos
)
1741 _M_check(__pos
, "basic_string::erase");
1743 this->_M_set_length(__pos
);
1745 this->_M_erase(__pos
, _M_limit(__pos
, __n
));
1750 * @brief Remove one character.
1751 * @param __position Iterator referencing the character to remove.
1752 * @return iterator referencing same location after removal.
1754 * Removes the character at @a __position from this string. The value
1755 * of the string doesn't change if an error is thrown.
1758 erase(__const_iterator __position
)
1760 _GLIBCXX_DEBUG_PEDASSERT(__position
>= begin()
1761 && __position
< end());
1762 const size_type __pos
= __position
- begin();
1763 this->_M_erase(__pos
, size_type(1));
1764 return iterator(_M_data() + __pos
);
1768 * @brief Remove a range of characters.
1769 * @param __first Iterator referencing the first character to remove.
1770 * @param __last Iterator referencing the end of the range.
1771 * @return Iterator referencing location of first after removal.
1773 * Removes the characters in the range [first,last) from this string.
1774 * The value of the string doesn't change if an error is thrown.
1777 erase(__const_iterator __first
, __const_iterator __last
)
1779 _GLIBCXX_DEBUG_PEDASSERT(__first
>= begin() && __first
<= __last
1780 && __last
<= end());
1781 const size_type __pos
= __first
- begin();
1782 if (__last
== end())
1783 this->_M_set_length(__pos
);
1785 this->_M_erase(__pos
, __last
- __first
);
1786 return iterator(this->_M_data() + __pos
);
1789 #if __cplusplus >= 201103L
1791 * @brief Remove the last character.
1793 * The string must be non-empty.
1798 __glibcxx_assert(!empty());
1799 _M_erase(size() - 1, 1);
1804 * @brief Replace characters with value from another string.
1805 * @param __pos Index of first character to replace.
1806 * @param __n Number of characters to be replaced.
1807 * @param __str String to insert.
1808 * @return Reference to this string.
1809 * @throw std::out_of_range If @a pos is beyond the end of this
1811 * @throw std::length_error If new length exceeds @c max_size().
1813 * Removes the characters in the range [__pos,__pos+__n) from
1814 * this string. In place, the value of @a __str is inserted.
1815 * If @a __pos is beyond end of string, out_of_range is thrown.
1816 * If the length of the result exceeds max_size(), length_error
1817 * is thrown. The value of the string doesn't change if an
1821 replace(size_type __pos
, size_type __n
, const basic_string
& __str
)
1822 { return this->replace(__pos
, __n
, __str
._M_data(), __str
.size()); }
1825 * @brief Replace characters with value from another string.
1826 * @param __pos1 Index of first character to replace.
1827 * @param __n1 Number of characters to be replaced.
1828 * @param __str String to insert.
1829 * @param __pos2 Index of first character of str to use.
1830 * @param __n2 Number of characters from str to use.
1831 * @return Reference to this string.
1832 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
1834 * @throw std::length_error If new length exceeds @c max_size().
1836 * Removes the characters in the range [__pos1,__pos1 + n) from this
1837 * string. In place, the value of @a __str is inserted. If @a __pos is
1838 * beyond end of string, out_of_range is thrown. If the length of the
1839 * result exceeds max_size(), length_error is thrown. The value of the
1840 * string doesn't change if an error is thrown.
1843 replace(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
1844 size_type __pos2
, size_type __n2
)
1845 { return this->replace(__pos1
, __n1
, __str
._M_data()
1846 + __str
._M_check(__pos2
, "basic_string::replace"),
1847 __str
._M_limit(__pos2
, __n2
)); }
1850 * @brief Replace characters with value of a C substring.
1851 * @param __pos Index of first character to replace.
1852 * @param __n1 Number of characters to be replaced.
1853 * @param __s C string to insert.
1854 * @param __n2 Number of characters from @a s to use.
1855 * @return Reference to this string.
1856 * @throw std::out_of_range If @a pos1 > size().
1857 * @throw std::length_error If new length exceeds @c max_size().
1859 * Removes the characters in the range [__pos,__pos + __n1)
1860 * from this string. In place, the first @a __n2 characters of
1861 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
1862 * @a __pos is beyond end of string, out_of_range is thrown. If
1863 * the length of result exceeds max_size(), length_error is
1864 * thrown. The value of the string doesn't change if an error
1868 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
1871 __glibcxx_requires_string_len(__s
, __n2
);
1872 return _M_replace(_M_check(__pos
, "basic_string::replace"),
1873 _M_limit(__pos
, __n1
), __s
, __n2
);
1877 * @brief Replace characters with value of a C string.
1878 * @param __pos Index of first character to replace.
1879 * @param __n1 Number of characters to be replaced.
1880 * @param __s C string to insert.
1881 * @return Reference to this string.
1882 * @throw std::out_of_range If @a pos > size().
1883 * @throw std::length_error If new length exceeds @c max_size().
1885 * Removes the characters in the range [__pos,__pos + __n1)
1886 * from this string. In place, the characters of @a __s are
1887 * inserted. If @a __pos is beyond end of string, out_of_range
1888 * is thrown. If the length of result exceeds max_size(),
1889 * length_error is thrown. The value of the string doesn't
1890 * change if an error is thrown.
1893 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
)
1895 __glibcxx_requires_string(__s
);
1896 return this->replace(__pos
, __n1
, __s
, traits_type::length(__s
));
1900 * @brief Replace characters with multiple characters.
1901 * @param __pos Index of first character to replace.
1902 * @param __n1 Number of characters to be replaced.
1903 * @param __n2 Number of characters to insert.
1904 * @param __c Character to insert.
1905 * @return Reference to this string.
1906 * @throw std::out_of_range If @a __pos > size().
1907 * @throw std::length_error If new length exceeds @c max_size().
1909 * Removes the characters in the range [pos,pos + n1) from this
1910 * string. In place, @a __n2 copies of @a __c are inserted.
1911 * If @a __pos is beyond end of string, out_of_range is thrown.
1912 * If the length of result exceeds max_size(), length_error is
1913 * thrown. The value of the string doesn't change if an error
1917 replace(size_type __pos
, size_type __n1
, size_type __n2
, _CharT __c
)
1918 { return _M_replace_aux(_M_check(__pos
, "basic_string::replace"),
1919 _M_limit(__pos
, __n1
), __n2
, __c
); }
1922 * @brief Replace range of characters with string.
1923 * @param __i1 Iterator referencing start of range to replace.
1924 * @param __i2 Iterator referencing end of range to replace.
1925 * @param __str String value to insert.
1926 * @return Reference to this string.
1927 * @throw std::length_error If new length exceeds @c max_size().
1929 * Removes the characters in the range [__i1,__i2). In place,
1930 * the value of @a __str is inserted. If the length of result
1931 * exceeds max_size(), length_error is thrown. The value of
1932 * the string doesn't change if an error is thrown.
1935 replace(__const_iterator __i1
, __const_iterator __i2
,
1936 const basic_string
& __str
)
1937 { return this->replace(__i1
, __i2
, __str
._M_data(), __str
.size()); }
1940 * @brief Replace range of characters with C substring.
1941 * @param __i1 Iterator referencing start of range to replace.
1942 * @param __i2 Iterator referencing end of range to replace.
1943 * @param __s C string value to insert.
1944 * @param __n Number of characters from s to insert.
1945 * @return Reference to this string.
1946 * @throw std::length_error If new length exceeds @c max_size().
1948 * Removes the characters in the range [__i1,__i2). In place,
1949 * the first @a __n characters of @a __s are inserted. If the
1950 * length of result exceeds max_size(), length_error is thrown.
1951 * The value of the string doesn't change if an error is
1955 replace(__const_iterator __i1
, __const_iterator __i2
,
1956 const _CharT
* __s
, size_type __n
)
1958 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
1960 return this->replace(__i1
- begin(), __i2
- __i1
, __s
, __n
);
1964 * @brief Replace range of characters with C string.
1965 * @param __i1 Iterator referencing start of range to replace.
1966 * @param __i2 Iterator referencing end of range to replace.
1967 * @param __s C string value to insert.
1968 * @return Reference to this string.
1969 * @throw std::length_error If new length exceeds @c max_size().
1971 * Removes the characters in the range [__i1,__i2). In place,
1972 * the characters of @a __s are inserted. If the length of
1973 * result exceeds max_size(), length_error is thrown. The
1974 * value of the string doesn't change if an error is thrown.
1977 replace(__const_iterator __i1
, __const_iterator __i2
, const _CharT
* __s
)
1979 __glibcxx_requires_string(__s
);
1980 return this->replace(__i1
, __i2
, __s
, traits_type::length(__s
));
1984 * @brief Replace range of characters with multiple characters
1985 * @param __i1 Iterator referencing start of range to replace.
1986 * @param __i2 Iterator referencing end of range to replace.
1987 * @param __n Number of characters to insert.
1988 * @param __c Character to insert.
1989 * @return Reference to this string.
1990 * @throw std::length_error If new length exceeds @c max_size().
1992 * Removes the characters in the range [__i1,__i2). In place,
1993 * @a __n copies of @a __c are inserted. If the length of
1994 * result exceeds max_size(), length_error is thrown. The
1995 * value of the string doesn't change if an error is thrown.
1998 replace(__const_iterator __i1
, __const_iterator __i2
, size_type __n
,
2001 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2003 return _M_replace_aux(__i1
- begin(), __i2
- __i1
, __n
, __c
);
2007 * @brief Replace range of characters with range.
2008 * @param __i1 Iterator referencing start of range to replace.
2009 * @param __i2 Iterator referencing end of range to replace.
2010 * @param __k1 Iterator referencing start of range to insert.
2011 * @param __k2 Iterator referencing end of range to insert.
2012 * @return Reference to this string.
2013 * @throw std::length_error If new length exceeds @c max_size().
2015 * Removes the characters in the range [__i1,__i2). In place,
2016 * characters in the range [__k1,__k2) are inserted. If the
2017 * length of result exceeds max_size(), length_error is thrown.
2018 * The value of the string doesn't change if an error is
2021 #if __cplusplus >= 201103L
2022 template<class _InputIterator
,
2023 typename
= std::_RequireInputIter
<_InputIterator
>>
2025 replace(const_iterator __i1
, const_iterator __i2
,
2026 _InputIterator __k1
, _InputIterator __k2
)
2028 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2030 __glibcxx_requires_valid_range(__k1
, __k2
);
2031 return this->_M_replace_dispatch(__i1
, __i2
, __k1
, __k2
,
2032 std::__false_type());
2035 template<class _InputIterator
>
2036 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
2037 typename __enable_if_not_native_iterator
<_InputIterator
>::__type
2041 replace(iterator __i1
, iterator __i2
,
2042 _InputIterator __k1
, _InputIterator __k2
)
2044 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2046 __glibcxx_requires_valid_range(__k1
, __k2
);
2047 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
2048 return _M_replace_dispatch(__i1
, __i2
, __k1
, __k2
, _Integral());
2052 // Specializations for the common case of pointer and iterator:
2053 // useful to avoid the overhead of temporary buffering in _M_replace.
2055 replace(__const_iterator __i1
, __const_iterator __i2
,
2056 _CharT
* __k1
, _CharT
* __k2
)
2058 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2060 __glibcxx_requires_valid_range(__k1
, __k2
);
2061 return this->replace(__i1
- begin(), __i2
- __i1
,
2066 replace(__const_iterator __i1
, __const_iterator __i2
,
2067 const _CharT
* __k1
, const _CharT
* __k2
)
2069 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2071 __glibcxx_requires_valid_range(__k1
, __k2
);
2072 return this->replace(__i1
- begin(), __i2
- __i1
,
2077 replace(__const_iterator __i1
, __const_iterator __i2
,
2078 iterator __k1
, iterator __k2
)
2080 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2082 __glibcxx_requires_valid_range(__k1
, __k2
);
2083 return this->replace(__i1
- begin(), __i2
- __i1
,
2084 __k1
.base(), __k2
- __k1
);
2088 replace(__const_iterator __i1
, __const_iterator __i2
,
2089 const_iterator __k1
, const_iterator __k2
)
2091 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2093 __glibcxx_requires_valid_range(__k1
, __k2
);
2094 return this->replace(__i1
- begin(), __i2
- __i1
,
2095 __k1
.base(), __k2
- __k1
);
2098 #if __cplusplus >= 201103L
2100 * @brief Replace range of characters with initializer_list.
2101 * @param __i1 Iterator referencing start of range to replace.
2102 * @param __i2 Iterator referencing end of range to replace.
2103 * @param __l The initializer_list of characters to insert.
2104 * @return Reference to this string.
2105 * @throw std::length_error If new length exceeds @c max_size().
2107 * Removes the characters in the range [__i1,__i2). In place,
2108 * characters in the range [__k1,__k2) are inserted. If the
2109 * length of result exceeds max_size(), length_error is thrown.
2110 * The value of the string doesn't change if an error is
2113 basic_string
& replace(const_iterator __i1
, const_iterator __i2
,
2114 initializer_list
<_CharT
> __l
)
2115 { return this->replace(__i1
, __i2
, __l
.begin(), __l
.size()); }
2118 #if __cplusplus > 201402L
2120 * @brief Replace range of characters with string_view.
2121 * @param __pos The position to replace at.
2122 * @param __n The number of characters to replace.
2123 * @param __sv The string_view to insert.
2124 * @return Reference to this string.
2127 replace(size_type __pos
, size_type __n
, __sv_type __sv
)
2128 { return this->replace(__pos
, __n
, __sv
.data(), __sv
.size()); }
2131 * @brief Replace range of characters with string_view.
2132 * @param __pos1 The position to replace at.
2133 * @param __n1 The number of characters to replace.
2134 * @param __sv The string_view to insert from.
2135 * @param __pos2 The position in the string_view to insert from.
2136 * @param __n2 The number of characters to insert.
2137 * @return Reference to this string.
2139 template <typename _Tp
>
2140 _If_sv
<_Tp
, basic_string
&>
2141 replace(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
2142 size_type __pos2
, size_type __n2
= npos
)
2144 __sv_type __sv
= __svt
;
2145 return this->replace(__pos1
, __n1
, __sv
.data()
2146 + __sv
._M_check(__pos2
, "basic_string::replace"),
2147 __sv
._M_limit(__pos2
, __n2
));
2151 * @brief Replace range of characters with string_view.
2152 * @param __i1 An iterator referencing the start position
2154 * @param __i2 An iterator referencing the end position
2156 * @param __sv The string_view to insert from.
2157 * @return Reference to this string.
2160 replace(const_iterator __i1
, const_iterator __i2
, __sv_type __sv
)
2161 { return this->replace(__i1
- begin(), __i2
- __i1
, __sv
); }
2165 template<class _Integer
>
2167 _M_replace_dispatch(const_iterator __i1
, const_iterator __i2
,
2168 _Integer __n
, _Integer __val
, __true_type
)
2169 { return _M_replace_aux(__i1
- begin(), __i2
- __i1
, __n
, __val
); }
2171 template<class _InputIterator
>
2173 _M_replace_dispatch(const_iterator __i1
, const_iterator __i2
,
2174 _InputIterator __k1
, _InputIterator __k2
,
2178 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
2182 _M_replace(size_type __pos
, size_type __len1
, const _CharT
* __s
,
2183 const size_type __len2
);
2186 _M_append(const _CharT
* __s
, size_type __n
);
2191 * @brief Copy substring into C string.
2192 * @param __s C string to copy value into.
2193 * @param __n Number of characters to copy.
2194 * @param __pos Index of first character to copy.
2195 * @return Number of characters actually copied
2196 * @throw std::out_of_range If __pos > size().
2198 * Copies up to @a __n characters starting at @a __pos into the
2199 * C string @a __s. If @a __pos is %greater than size(),
2200 * out_of_range is thrown.
2203 copy(_CharT
* __s
, size_type __n
, size_type __pos
= 0) const;
2206 * @brief Swap contents with another string.
2207 * @param __s String to swap with.
2209 * Exchanges the contents of this string with that of @a __s in constant
2213 swap(basic_string
& __s
) _GLIBCXX_NOEXCEPT
;
2215 // String operations:
2217 * @brief Return const pointer to null-terminated contents.
2219 * This is a handle to internal data. Do not modify or dire things may
2223 c_str() const _GLIBCXX_NOEXCEPT
2224 { return _M_data(); }
2227 * @brief Return const pointer to contents.
2229 * This is a pointer to internal data. It is undefined to modify
2230 * the contents through the returned pointer. To get a pointer that
2231 * allows modifying the contents use @c &str[0] instead,
2232 * (or in C++17 the non-const @c str.data() overload).
2235 data() const _GLIBCXX_NOEXCEPT
2236 { return _M_data(); }
2238 #if __cplusplus > 201402L
2240 * @brief Return non-const pointer to contents.
2242 * This is a pointer to the character sequence held by the string.
2243 * Modifying the characters in the sequence is allowed.
2247 { return _M_data(); }
2251 * @brief Return copy of allocator used to construct this string.
2254 get_allocator() const _GLIBCXX_NOEXCEPT
2255 { return _M_get_allocator(); }
2258 * @brief Find position of a C substring.
2259 * @param __s C string to locate.
2260 * @param __pos Index of character to search from.
2261 * @param __n Number of characters from @a s to search for.
2262 * @return Index of start of first occurrence.
2264 * Starting from @a __pos, searches forward for the first @a
2265 * __n characters in @a __s within this string. If found,
2266 * returns the index where it begins. If not found, returns
2270 find(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2274 * @brief Find position of a string.
2275 * @param __str String to locate.
2276 * @param __pos Index of character to search from (default 0).
2277 * @return Index of start of first occurrence.
2279 * Starting from @a __pos, searches forward for value of @a __str within
2280 * this string. If found, returns the index where it begins. If not
2281 * found, returns npos.
2284 find(const basic_string
& __str
, size_type __pos
= 0) const
2286 { return this->find(__str
.data(), __pos
, __str
.size()); }
2288 #if __cplusplus > 201402L
2290 * @brief Find position of a string_view.
2291 * @param __sv The string_view to locate.
2292 * @param __pos Index of character to search from (default 0).
2293 * @return Index of start of first occurrence.
2296 find(__sv_type __sv
, size_type __pos
= 0) const noexcept
2297 { return this->find(__sv
.data(), __pos
, __sv
.size()); }
2301 * @brief Find position of a C string.
2302 * @param __s C string to locate.
2303 * @param __pos Index of character to search from (default 0).
2304 * @return Index of start of first occurrence.
2306 * Starting from @a __pos, searches forward for the value of @a
2307 * __s within this string. If found, returns the index where
2308 * it begins. If not found, returns npos.
2311 find(const _CharT
* __s
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
2313 __glibcxx_requires_string(__s
);
2314 return this->find(__s
, __pos
, traits_type::length(__s
));
2318 * @brief Find position of a character.
2319 * @param __c Character to locate.
2320 * @param __pos Index of character to search from (default 0).
2321 * @return Index of first occurrence.
2323 * Starting from @a __pos, searches forward for @a __c within
2324 * this string. If found, returns the index where it was
2325 * found. If not found, returns npos.
2328 find(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
;
2331 * @brief Find last position of a string.
2332 * @param __str String to locate.
2333 * @param __pos Index of character to search back from (default end).
2334 * @return Index of start of last occurrence.
2336 * Starting from @a __pos, searches backward for value of @a
2337 * __str within this string. If found, returns the index where
2338 * it begins. If not found, returns npos.
2341 rfind(const basic_string
& __str
, size_type __pos
= npos
) const
2343 { return this->rfind(__str
.data(), __pos
, __str
.size()); }
2345 #if __cplusplus > 201402L
2347 * @brief Find last position of a string_view.
2348 * @param __sv The string_view to locate.
2349 * @param __pos Index of character to search back from (default end).
2350 * @return Index of start of last occurrence.
2353 rfind(__sv_type __sv
, size_type __pos
= npos
) const noexcept
2354 { return this->rfind(__sv
.data(), __pos
, __sv
.size()); }
2358 * @brief Find last position of a C substring.
2359 * @param __s C string to locate.
2360 * @param __pos Index of character to search back from.
2361 * @param __n Number of characters from s to search for.
2362 * @return Index of start of last occurrence.
2364 * Starting from @a __pos, searches backward for the first @a
2365 * __n characters in @a __s within this string. If found,
2366 * returns the index where it begins. If not found, returns
2370 rfind(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2374 * @brief Find last position of a C string.
2375 * @param __s C string to locate.
2376 * @param __pos Index of character to start search at (default end).
2377 * @return Index of start of last occurrence.
2379 * Starting from @a __pos, searches backward for the value of
2380 * @a __s within this string. If found, returns the index
2381 * where it begins. If not found, returns npos.
2384 rfind(const _CharT
* __s
, size_type __pos
= npos
) const
2386 __glibcxx_requires_string(__s
);
2387 return this->rfind(__s
, __pos
, traits_type::length(__s
));
2391 * @brief Find last position of a character.
2392 * @param __c Character to locate.
2393 * @param __pos Index of character to search back from (default end).
2394 * @return Index of last occurrence.
2396 * Starting from @a __pos, searches backward for @a __c within
2397 * this string. If found, returns the index where it was
2398 * found. If not found, returns npos.
2401 rfind(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
;
2404 * @brief Find position of a character of string.
2405 * @param __str String containing characters to locate.
2406 * @param __pos Index of character to search from (default 0).
2407 * @return Index of first occurrence.
2409 * Starting from @a __pos, searches forward for one of the
2410 * characters of @a __str within this string. If found,
2411 * returns the index where it was found. If not found, returns
2415 find_first_of(const basic_string
& __str
, size_type __pos
= 0) const
2417 { return this->find_first_of(__str
.data(), __pos
, __str
.size()); }
2419 #if __cplusplus > 201402L
2421 * @brief Find position of a character of a string_view.
2422 * @param __sv A string_view containing characters to locate.
2423 * @param __pos Index of character to search from (default 0).
2424 * @return Index of first occurrence.
2427 find_first_of(__sv_type __sv
, size_type __pos
= 0) const noexcept
2428 { return this->find_first_of(__sv
.data(), __pos
, __sv
.size()); }
2432 * @brief Find position of a character of C substring.
2433 * @param __s String containing characters to locate.
2434 * @param __pos Index of character to search from.
2435 * @param __n Number of characters from s to search for.
2436 * @return Index of first occurrence.
2438 * Starting from @a __pos, searches forward for one of the
2439 * first @a __n characters of @a __s within this string. If
2440 * found, returns the index where it was found. If not found,
2444 find_first_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2448 * @brief Find position of a character of C string.
2449 * @param __s String containing characters to locate.
2450 * @param __pos Index of character to search from (default 0).
2451 * @return Index of first occurrence.
2453 * Starting from @a __pos, searches forward for one of the
2454 * characters of @a __s within this string. If found, returns
2455 * the index where it was found. If not found, returns npos.
2458 find_first_of(const _CharT
* __s
, size_type __pos
= 0) const
2461 __glibcxx_requires_string(__s
);
2462 return this->find_first_of(__s
, __pos
, traits_type::length(__s
));
2466 * @brief Find position of a character.
2467 * @param __c Character to locate.
2468 * @param __pos Index of character to search from (default 0).
2469 * @return Index of first occurrence.
2471 * Starting from @a __pos, searches forward for the character
2472 * @a __c within this string. If found, returns the index
2473 * where it was found. If not found, returns npos.
2475 * Note: equivalent to find(__c, __pos).
2478 find_first_of(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
2479 { return this->find(__c
, __pos
); }
2482 * @brief Find last position of a character of string.
2483 * @param __str String containing characters to locate.
2484 * @param __pos Index of character to search back from (default end).
2485 * @return Index of last occurrence.
2487 * Starting from @a __pos, searches backward for one of the
2488 * characters of @a __str within this string. If found,
2489 * returns the index where it was found. If not found, returns
2493 find_last_of(const basic_string
& __str
, size_type __pos
= npos
) const
2495 { return this->find_last_of(__str
.data(), __pos
, __str
.size()); }
2497 #if __cplusplus > 201402L
2499 * @brief Find last position of a character of string.
2500 * @param __sv A string_view containing characters to locate.
2501 * @param __pos Index of character to search back from (default end).
2502 * @return Index of last occurrence.
2505 find_last_of(__sv_type __sv
, size_type __pos
= npos
) const noexcept
2506 { return this->find_last_of(__sv
.data(), __pos
, __sv
.size()); }
2510 * @brief Find last position of a character of C substring.
2511 * @param __s C string containing characters to locate.
2512 * @param __pos Index of character to search back from.
2513 * @param __n Number of characters from s to search for.
2514 * @return Index of last occurrence.
2516 * Starting from @a __pos, searches backward for one of the
2517 * first @a __n characters of @a __s within this string. If
2518 * found, returns the index where it was found. If not found,
2522 find_last_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2526 * @brief Find last position of a character of C string.
2527 * @param __s C string containing characters to locate.
2528 * @param __pos Index of character to search back from (default end).
2529 * @return Index of last occurrence.
2531 * Starting from @a __pos, searches backward for one of the
2532 * characters of @a __s within this string. If found, returns
2533 * the index where it was found. If not found, returns npos.
2536 find_last_of(const _CharT
* __s
, size_type __pos
= npos
) const
2539 __glibcxx_requires_string(__s
);
2540 return this->find_last_of(__s
, __pos
, traits_type::length(__s
));
2544 * @brief Find last position of a character.
2545 * @param __c Character to locate.
2546 * @param __pos Index of character to search back from (default end).
2547 * @return Index of last occurrence.
2549 * Starting from @a __pos, searches backward for @a __c within
2550 * this string. If found, returns the index where it was
2551 * found. If not found, returns npos.
2553 * Note: equivalent to rfind(__c, __pos).
2556 find_last_of(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
2557 { return this->rfind(__c
, __pos
); }
2560 * @brief Find position of a character not in string.
2561 * @param __str String containing characters to avoid.
2562 * @param __pos Index of character to search from (default 0).
2563 * @return Index of first occurrence.
2565 * Starting from @a __pos, searches forward for a character not contained
2566 * in @a __str within this string. If found, returns the index where it
2567 * was found. If not found, returns npos.
2570 find_first_not_of(const basic_string
& __str
, size_type __pos
= 0) const
2572 { return this->find_first_not_of(__str
.data(), __pos
, __str
.size()); }
2574 #if __cplusplus > 201402L
2576 * @brief Find position of a character not in a string_view.
2577 * @param __sv A string_view containing characters to avoid.
2578 * @param __pos Index of character to search from (default 0).
2579 * @return Index of first occurrence.
2582 find_first_not_of(__sv_type __sv
, size_type __pos
= 0) const noexcept
2583 { return this->find_first_not_of(__sv
.data(), __pos
, __sv
.size()); }
2587 * @brief Find position of a character not in C substring.
2588 * @param __s C string containing characters to avoid.
2589 * @param __pos Index of character to search from.
2590 * @param __n Number of characters from __s to consider.
2591 * @return Index of first occurrence.
2593 * Starting from @a __pos, searches forward for a character not
2594 * contained in the first @a __n characters of @a __s within
2595 * this string. If found, returns the index where it was
2596 * found. If not found, returns npos.
2599 find_first_not_of(const _CharT
* __s
, size_type __pos
,
2600 size_type __n
) const _GLIBCXX_NOEXCEPT
;
2603 * @brief Find position of a character not in C string.
2604 * @param __s C string containing characters to avoid.
2605 * @param __pos Index of character to search from (default 0).
2606 * @return Index of first occurrence.
2608 * Starting from @a __pos, searches forward for a character not
2609 * contained in @a __s within this string. If found, returns
2610 * the index where it was found. If not found, returns npos.
2613 find_first_not_of(const _CharT
* __s
, size_type __pos
= 0) const
2616 __glibcxx_requires_string(__s
);
2617 return this->find_first_not_of(__s
, __pos
, traits_type::length(__s
));
2621 * @brief Find position of a different character.
2622 * @param __c Character to avoid.
2623 * @param __pos Index of character to search from (default 0).
2624 * @return Index of first occurrence.
2626 * Starting from @a __pos, searches forward for a character
2627 * other than @a __c within this string. If found, returns the
2628 * index where it was found. If not found, returns npos.
2631 find_first_not_of(_CharT __c
, size_type __pos
= 0) const
2635 * @brief Find last position of a character not in string.
2636 * @param __str String containing characters to avoid.
2637 * @param __pos Index of character to search back from (default end).
2638 * @return Index of last occurrence.
2640 * Starting from @a __pos, searches backward for a character
2641 * not contained in @a __str within this string. If found,
2642 * returns the index where it was found. If not found, returns
2646 find_last_not_of(const basic_string
& __str
, size_type __pos
= npos
) const
2648 { return this->find_last_not_of(__str
.data(), __pos
, __str
.size()); }
2650 #if __cplusplus > 201402L
2652 * @brief Find last position of a character not in a string_view.
2653 * @param __sv A string_view containing characters to avoid.
2654 * @param __pos Index of character to search back from (default end).
2655 * @return Index of last occurrence.
2658 find_last_not_of(__sv_type __sv
, size_type __pos
= npos
) const noexcept
2659 { return this->find_last_not_of(__sv
.data(), __pos
, __sv
.size()); }
2663 * @brief Find last position of a character not in C substring.
2664 * @param __s C string containing characters to avoid.
2665 * @param __pos Index of character to search back from.
2666 * @param __n Number of characters from s to consider.
2667 * @return Index of last occurrence.
2669 * Starting from @a __pos, searches backward for a character not
2670 * contained in the first @a __n characters of @a __s within this string.
2671 * If found, returns the index where it was found. If not found,
2675 find_last_not_of(const _CharT
* __s
, size_type __pos
,
2676 size_type __n
) const _GLIBCXX_NOEXCEPT
;
2678 * @brief Find last position of a character not in C string.
2679 * @param __s C string containing characters to avoid.
2680 * @param __pos Index of character to search back from (default end).
2681 * @return Index of last occurrence.
2683 * Starting from @a __pos, searches backward for a character
2684 * not contained in @a __s within this string. If found,
2685 * returns the index where it was found. If not found, returns
2689 find_last_not_of(const _CharT
* __s
, size_type __pos
= npos
) const
2692 __glibcxx_requires_string(__s
);
2693 return this->find_last_not_of(__s
, __pos
, traits_type::length(__s
));
2697 * @brief Find last position of a different character.
2698 * @param __c Character to avoid.
2699 * @param __pos Index of character to search back from (default end).
2700 * @return Index of last occurrence.
2702 * Starting from @a __pos, searches backward for a character other than
2703 * @a __c within this string. If found, returns the index where it was
2704 * found. If not found, returns npos.
2707 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const
2711 * @brief Get a substring.
2712 * @param __pos Index of first character (default 0).
2713 * @param __n Number of characters in substring (default remainder).
2714 * @return The new string.
2715 * @throw std::out_of_range If __pos > size().
2717 * Construct and return a new string using the @a __n
2718 * characters starting at @a __pos. If the string is too
2719 * short, use the remainder of the characters. If @a __pos is
2720 * beyond the end of the string, out_of_range is thrown.
2723 substr(size_type __pos
= 0, size_type __n
= npos
) const
2724 { return basic_string(*this,
2725 _M_check(__pos
, "basic_string::substr"), __n
); }
2728 * @brief Compare to a string.
2729 * @param __str String to compare against.
2730 * @return Integer < 0, 0, or > 0.
2732 * Returns an integer < 0 if this string is ordered before @a
2733 * __str, 0 if their values are equivalent, or > 0 if this
2734 * string is ordered after @a __str. Determines the effective
2735 * length rlen of the strings to compare as the smallest of
2736 * size() and str.size(). The function then compares the two
2737 * strings by calling traits::compare(data(), str.data(),rlen).
2738 * If the result of the comparison is nonzero returns it,
2739 * otherwise the shorter one is ordered first.
2742 compare(const basic_string
& __str
) const
2744 const size_type __size
= this->size();
2745 const size_type __osize
= __str
.size();
2746 const size_type __len
= std::min(__size
, __osize
);
2748 int __r
= traits_type::compare(_M_data(), __str
.data(), __len
);
2750 __r
= _S_compare(__size
, __osize
);
2754 #if __cplusplus > 201402L
2756 * @brief Compare to a string_view.
2757 * @param __sv A string_view to compare against.
2758 * @return Integer < 0, 0, or > 0.
2761 compare(__sv_type __sv
) const
2763 const size_type __size
= this->size();
2764 const size_type __osize
= __sv
.size();
2765 const size_type __len
= std::min(__size
, __osize
);
2767 int __r
= traits_type::compare(_M_data(), __sv
.data(), __len
);
2769 __r
= _S_compare(__size
, __osize
);
2774 * @brief Compare to a string_view.
2775 * @param __pos A position in the string to start comparing from.
2776 * @param __n The number of characters to compare.
2777 * @param __sv A string_view to compare against.
2778 * @return Integer < 0, 0, or > 0.
2781 compare(size_type __pos
, size_type __n
, __sv_type __sv
) const
2782 { return __sv_type(*this).substr(__pos
, __n
).compare(__sv
); }
2785 * @brief Compare to a string_view.
2786 * @param __pos1 A position in the string to start comparing from.
2787 * @param __n1 The number of characters to compare.
2788 * @param __sv A string_view to compare against.
2789 * @param __pos2 A position in the string_view to start comparing from.
2790 * @param __n2 The number of characters to compare.
2791 * @return Integer < 0, 0, or > 0.
2793 template <typename _Tp
>
2795 compare(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
2796 size_type __pos2
, size_type __n2
= npos
) const
2798 __sv_type __sv
= __svt
;
2799 return __sv_type(*this)
2800 .substr(__pos1
, __n1
).compare(__sv
.substr(__pos2
, __n2
));
2805 * @brief Compare substring to a string.
2806 * @param __pos Index of first character of substring.
2807 * @param __n Number of characters in substring.
2808 * @param __str String to compare against.
2809 * @return Integer < 0, 0, or > 0.
2811 * Form the substring of this string from the @a __n characters
2812 * starting at @a __pos. Returns an integer < 0 if the
2813 * substring is ordered before @a __str, 0 if their values are
2814 * equivalent, or > 0 if the substring is ordered after @a
2815 * __str. Determines the effective length rlen of the strings
2816 * to compare as the smallest of the length of the substring
2817 * and @a __str.size(). The function then compares the two
2818 * strings by calling
2819 * traits::compare(substring.data(),str.data(),rlen). If the
2820 * result of the comparison is nonzero returns it, otherwise
2821 * the shorter one is ordered first.
2824 compare(size_type __pos
, size_type __n
, const basic_string
& __str
) const;
2827 * @brief Compare substring to a substring.
2828 * @param __pos1 Index of first character of substring.
2829 * @param __n1 Number of characters in substring.
2830 * @param __str String to compare against.
2831 * @param __pos2 Index of first character of substring of str.
2832 * @param __n2 Number of characters in substring of str.
2833 * @return Integer < 0, 0, or > 0.
2835 * Form the substring of this string from the @a __n1
2836 * characters starting at @a __pos1. Form the substring of @a
2837 * __str from the @a __n2 characters starting at @a __pos2.
2838 * Returns an integer < 0 if this substring is ordered before
2839 * the substring of @a __str, 0 if their values are equivalent,
2840 * or > 0 if this substring is ordered after the substring of
2841 * @a __str. Determines the effective length rlen of the
2842 * strings to compare as the smallest of the lengths of the
2843 * substrings. The function then compares the two strings by
2845 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2846 * If the result of the comparison is nonzero returns it,
2847 * otherwise the shorter one is ordered first.
2850 compare(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
2851 size_type __pos2
, size_type __n2
) const;
2854 * @brief Compare to a C string.
2855 * @param __s C string to compare against.
2856 * @return Integer < 0, 0, or > 0.
2858 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
2859 * their values are equivalent, or > 0 if this string is ordered after
2860 * @a __s. Determines the effective length rlen of the strings to
2861 * compare as the smallest of size() and the length of a string
2862 * constructed from @a __s. The function then compares the two strings
2863 * by calling traits::compare(data(),s,rlen). If the result of the
2864 * comparison is nonzero returns it, otherwise the shorter one is
2868 compare(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
;
2870 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2871 // 5 String::compare specification questionable
2873 * @brief Compare substring to a C string.
2874 * @param __pos Index of first character of substring.
2875 * @param __n1 Number of characters in substring.
2876 * @param __s C string to compare against.
2877 * @return Integer < 0, 0, or > 0.
2879 * Form the substring of this string from the @a __n1
2880 * characters starting at @a pos. Returns an integer < 0 if
2881 * the substring is ordered before @a __s, 0 if their values
2882 * are equivalent, or > 0 if the substring is ordered after @a
2883 * __s. Determines the effective length rlen of the strings to
2884 * compare as the smallest of the length of the substring and
2885 * the length of a string constructed from @a __s. The
2886 * function then compares the two string by calling
2887 * traits::compare(substring.data(),__s,rlen). If the result of
2888 * the comparison is nonzero returns it, otherwise the shorter
2889 * one is ordered first.
2892 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
) const;
2895 * @brief Compare substring against a character %array.
2896 * @param __pos Index of first character of substring.
2897 * @param __n1 Number of characters in substring.
2898 * @param __s character %array to compare against.
2899 * @param __n2 Number of characters of s.
2900 * @return Integer < 0, 0, or > 0.
2902 * Form the substring of this string from the @a __n1
2903 * characters starting at @a __pos. Form a string from the
2904 * first @a __n2 characters of @a __s. Returns an integer < 0
2905 * if this substring is ordered before the string from @a __s,
2906 * 0 if their values are equivalent, or > 0 if this substring
2907 * is ordered after the string from @a __s. Determines the
2908 * effective length rlen of the strings to compare as the
2909 * smallest of the length of the substring and @a __n2. The
2910 * function then compares the two strings by calling
2911 * traits::compare(substring.data(),s,rlen). If the result of
2912 * the comparison is nonzero returns it, otherwise the shorter
2913 * one is ordered first.
2915 * NB: s must have at least n2 characters, '\\0' has
2916 * no special meaning.
2919 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
,
2920 size_type __n2
) const;
2922 _GLIBCXX_END_NAMESPACE_CXX11
2923 #else // !_GLIBCXX_USE_CXX11_ABI
2924 // Reference-counted COW string implentation
2927 * @class basic_string basic_string.h <string>
2928 * @brief Managing sequences of characters and character-like objects.
2931 * @ingroup sequences
2933 * @tparam _CharT Type of character
2934 * @tparam _Traits Traits for character type, defaults to
2935 * char_traits<_CharT>.
2936 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
2938 * Meets the requirements of a <a href="tables.html#65">container</a>, a
2939 * <a href="tables.html#66">reversible container</a>, and a
2940 * <a href="tables.html#67">sequence</a>. Of the
2941 * <a href="tables.html#68">optional sequence requirements</a>, only
2942 * @c push_back, @c at, and @c %array access are supported.
2947 * Documentation? What's that?
2948 * Nathan Myers <ncm@cantrip.org>.
2950 * A string looks like this:
2955 * [basic_string<char_type>] _M_capacity
2956 * _M_dataplus _M_refcount
2957 * _M_p ----------------> unnamed array of char_type
2960 * Where the _M_p points to the first character in the string, and
2961 * you cast it to a pointer-to-_Rep and subtract 1 to get a
2962 * pointer to the header.
2964 * This approach has the enormous advantage that a string object
2965 * requires only one allocation. All the ugliness is confined
2966 * within a single %pair of inline functions, which each compile to
2967 * a single @a add instruction: _Rep::_M_data(), and
2968 * string::_M_rep(); and the allocation function which gets a
2969 * block of raw bytes and with room enough and constructs a _Rep
2970 * object at the front.
2972 * The reason you want _M_data pointing to the character %array and
2973 * not the _Rep is so that the debugger can see the string
2974 * contents. (Probably we should add a non-inline member to get
2975 * the _Rep for the debugger to use, so users can check the actual
2978 * Note that the _Rep object is a POD so that you can have a
2979 * static <em>empty string</em> _Rep object already @a constructed before
2980 * static constructors have run. The reference-count encoding is
2981 * chosen so that a 0 indicates one reference, so you never try to
2982 * destroy the empty-string _Rep object.
2984 * All but the last paragraph is considered pretty conventional
2985 * for a C++ string implementation.
2987 // 21.3 Template class basic_string
2988 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2991 typedef typename
_Alloc::template rebind
<_CharT
>::other _CharT_alloc_type
;
2995 typedef _Traits traits_type
;
2996 typedef typename
_Traits::char_type value_type
;
2997 typedef _Alloc allocator_type
;
2998 typedef typename
_CharT_alloc_type::size_type size_type
;
2999 typedef typename
_CharT_alloc_type::difference_type difference_type
;
3000 typedef typename
_CharT_alloc_type::reference reference
;
3001 typedef typename
_CharT_alloc_type::const_reference const_reference
;
3002 typedef typename
_CharT_alloc_type::pointer pointer
;
3003 typedef typename
_CharT_alloc_type::const_pointer const_pointer
;
3004 typedef __gnu_cxx::__normal_iterator
<pointer
, basic_string
> iterator
;
3005 typedef __gnu_cxx::__normal_iterator
<const_pointer
, basic_string
>
3007 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
3008 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
3011 // _Rep: string representation
3013 // 1. String really contains _M_length + 1 characters: due to 21.3.4
3014 // must be kept null-terminated.
3015 // 2. _M_capacity >= _M_length
3016 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
3017 // 3. _M_refcount has three states:
3018 // -1: leaked, one reference, no ref-copies allowed, non-const.
3019 // 0: one reference, non-const.
3020 // n>0: n + 1 references, operations require a lock, const.
3021 // 4. All fields==0 is an empty string, given the extra storage
3022 // beyond-the-end for a null terminator; thus, the shared
3023 // empty string representation needs no constructor.
3027 size_type _M_length
;
3028 size_type _M_capacity
;
3029 _Atomic_word _M_refcount
;
3032 struct _Rep
: _Rep_base
3035 typedef typename
_Alloc::template rebind
<char>::other _Raw_bytes_alloc
;
3037 // (Public) Data members:
3039 // The maximum number of individual char_type elements of an
3040 // individual string is determined by _S_max_size. This is the
3041 // value that will be returned by max_size(). (Whereas npos
3042 // is the maximum number of bytes the allocator can allocate.)
3043 // If one was to divvy up the theoretical largest size string,
3044 // with a terminating character and m _CharT elements, it'd
3046 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
3048 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
3049 // In addition, this implementation quarters this amount.
3050 static const size_type _S_max_size
;
3051 static const _CharT _S_terminal
;
3053 // The following storage is init'd to 0 by the linker, resulting
3054 // (carefully) in an empty string with one reference.
3055 static size_type _S_empty_rep_storage
[];
3058 _S_empty_rep() _GLIBCXX_NOEXCEPT
3060 // NB: Mild hack to avoid strict-aliasing warnings. Note that
3061 // _S_empty_rep_storage is never modified and the punning should
3062 // be reasonably safe in this case.
3063 void* __p
= reinterpret_cast<void*>(&_S_empty_rep_storage
);
3064 return *reinterpret_cast<_Rep
*>(__p
);
3068 _M_is_leaked() const _GLIBCXX_NOEXCEPT
3070 #if defined(__GTHREADS)
3071 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3072 // so we need to use an atomic load. However, _M_is_leaked
3073 // predicate does not change concurrently (i.e. the string is either
3074 // leaked or not), so a relaxed load is enough.
3075 return __atomic_load_n(&this->_M_refcount
, __ATOMIC_RELAXED
) < 0;
3077 return this->_M_refcount
< 0;
3082 _M_is_shared() const _GLIBCXX_NOEXCEPT
3084 #if defined(__GTHREADS)
3085 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3086 // so we need to use an atomic load. Another thread can drop last
3087 // but one reference concurrently with this check, so we need this
3088 // load to be acquire to synchronize with release fetch_and_add in
3090 return __atomic_load_n(&this->_M_refcount
, __ATOMIC_ACQUIRE
) > 0;
3092 return this->_M_refcount
> 0;
3097 _M_set_leaked() _GLIBCXX_NOEXCEPT
3098 { this->_M_refcount
= -1; }
3101 _M_set_sharable() _GLIBCXX_NOEXCEPT
3102 { this->_M_refcount
= 0; }
3105 _M_set_length_and_sharable(size_type __n
) _GLIBCXX_NOEXCEPT
3107 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3108 if (__builtin_expect(this != &_S_empty_rep(), false))
3111 this->_M_set_sharable(); // One reference.
3112 this->_M_length
= __n
;
3113 traits_type::assign(this->_M_refdata()[__n
], _S_terminal
);
3114 // grrr. (per 21.3.4)
3115 // You cannot leave those LWG people alone for a second.
3120 _M_refdata() throw()
3121 { return reinterpret_cast<_CharT
*>(this + 1); }
3124 _M_grab(const _Alloc
& __alloc1
, const _Alloc
& __alloc2
)
3126 return (!_M_is_leaked() && __alloc1
== __alloc2
)
3127 ? _M_refcopy() : _M_clone(__alloc1
);
3132 _S_create(size_type
, size_type
, const _Alloc
&);
3135 _M_dispose(const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
3137 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3138 if (__builtin_expect(this != &_S_empty_rep(), false))
3141 // Be race-detector-friendly. For more info see bits/c++config.
3142 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount
);
3143 // Decrement of _M_refcount is acq_rel, because:
3144 // - all but last decrements need to release to synchronize with
3145 // the last decrement that will delete the object.
3146 // - the last decrement needs to acquire to synchronize with
3147 // all the previous decrements.
3148 // - last but one decrement needs to release to synchronize with
3149 // the acquire load in _M_is_shared that will conclude that
3150 // the object is not shared anymore.
3151 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount
,
3154 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount
);
3161 _M_destroy(const _Alloc
&) throw();
3164 _M_refcopy() throw()
3166 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3167 if (__builtin_expect(this != &_S_empty_rep(), false))
3169 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount
, 1);
3170 return _M_refdata();
3174 _M_clone(const _Alloc
&, size_type __res
= 0);
3177 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
3178 struct _Alloc_hider
: _Alloc
3180 _Alloc_hider(_CharT
* __dat
, const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
3181 : _Alloc(__a
), _M_p(__dat
) { }
3183 _CharT
* _M_p
; // The actual data.
3187 // Data Members (public):
3188 // NB: This is an unsigned type, and thus represents the maximum
3189 // size that the allocator can hold.
3190 /// Value returned by various member functions when they fail.
3191 static const size_type npos
= static_cast<size_type
>(-1);
3194 // Data Members (private):
3195 mutable _Alloc_hider _M_dataplus
;
3198 _M_data() const _GLIBCXX_NOEXCEPT
3199 { return _M_dataplus
._M_p
; }
3202 _M_data(_CharT
* __p
) _GLIBCXX_NOEXCEPT
3203 { return (_M_dataplus
._M_p
= __p
); }
3206 _M_rep() const _GLIBCXX_NOEXCEPT
3207 { return &((reinterpret_cast<_Rep
*> (_M_data()))[-1]); }
3209 // For the internal use we have functions similar to `begin'/`end'
3210 // but they do not call _M_leak.
3212 _M_ibegin() const _GLIBCXX_NOEXCEPT
3213 { return iterator(_M_data()); }
3216 _M_iend() const _GLIBCXX_NOEXCEPT
3217 { return iterator(_M_data() + this->size()); }
3220 _M_leak() // for use in begin() & non-const op[]
3222 if (!_M_rep()->_M_is_leaked())
3227 _M_check(size_type __pos
, const char* __s
) const
3229 if (__pos
> this->size())
3230 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
3231 "this->size() (which is %zu)"),
3232 __s
, __pos
, this->size());
3237 _M_check_length(size_type __n1
, size_type __n2
, const char* __s
) const
3239 if (this->max_size() - (this->size() - __n1
) < __n2
)
3240 __throw_length_error(__N(__s
));
3243 // NB: _M_limit doesn't check for a bad __pos value.
3245 _M_limit(size_type __pos
, size_type __off
) const _GLIBCXX_NOEXCEPT
3247 const bool __testoff
= __off
< this->size() - __pos
;
3248 return __testoff
? __off
: this->size() - __pos
;
3251 // True if _Rep and source do not overlap.
3253 _M_disjunct(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
3255 return (less
<const _CharT
*>()(__s
, _M_data())
3256 || less
<const _CharT
*>()(_M_data() + this->size(), __s
));
3259 // When __n = 1 way faster than the general multichar
3260 // traits_type::copy/move/assign.
3262 _M_copy(_CharT
* __d
, const _CharT
* __s
, size_type __n
) _GLIBCXX_NOEXCEPT
3265 traits_type::assign(*__d
, *__s
);
3267 traits_type::copy(__d
, __s
, __n
);
3271 _M_move(_CharT
* __d
, const _CharT
* __s
, size_type __n
) _GLIBCXX_NOEXCEPT
3274 traits_type::assign(*__d
, *__s
);
3276 traits_type::move(__d
, __s
, __n
);
3280 _M_assign(_CharT
* __d
, size_type __n
, _CharT __c
) _GLIBCXX_NOEXCEPT
3283 traits_type::assign(*__d
, __c
);
3285 traits_type::assign(__d
, __n
, __c
);
3288 // _S_copy_chars is a separate template to permit specialization
3289 // to optimize for the common case of pointers as iterators.
3290 template<class _Iterator
>
3292 _S_copy_chars(_CharT
* __p
, _Iterator __k1
, _Iterator __k2
)
3294 for (; __k1
!= __k2
; ++__k1
, (void)++__p
)
3295 traits_type::assign(*__p
, *__k1
); // These types are off.
3299 _S_copy_chars(_CharT
* __p
, iterator __k1
, iterator __k2
) _GLIBCXX_NOEXCEPT
3300 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
3303 _S_copy_chars(_CharT
* __p
, const_iterator __k1
, const_iterator __k2
)
3305 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
3308 _S_copy_chars(_CharT
* __p
, _CharT
* __k1
, _CharT
* __k2
) _GLIBCXX_NOEXCEPT
3309 { _M_copy(__p
, __k1
, __k2
- __k1
); }
3312 _S_copy_chars(_CharT
* __p
, const _CharT
* __k1
, const _CharT
* __k2
)
3314 { _M_copy(__p
, __k1
, __k2
- __k1
); }
3317 _S_compare(size_type __n1
, size_type __n2
) _GLIBCXX_NOEXCEPT
3319 const difference_type __d
= difference_type(__n1
- __n2
);
3321 if (__d
> __gnu_cxx::__numeric_traits
<int>::__max
)
3322 return __gnu_cxx::__numeric_traits
<int>::__max
;
3323 else if (__d
< __gnu_cxx::__numeric_traits
<int>::__min
)
3324 return __gnu_cxx::__numeric_traits
<int>::__min
;
3330 _M_mutate(size_type __pos
, size_type __len1
, size_type __len2
);
3336 _S_empty_rep() _GLIBCXX_NOEXCEPT
3337 { return _Rep::_S_empty_rep(); }
3339 #if __cplusplus > 201402L
3340 // A helper type for avoiding boiler-plate.
3341 typedef basic_string_view
<_CharT
, _Traits
> __sv_type
;
3343 template<typename _Tp
, typename _Res
>
3344 using _If_sv
= enable_if_t
<
3345 __and_
<is_convertible
<const _Tp
&, __sv_type
>,
3346 __not_
<is_convertible
<const _Tp
&, const _CharT
*>>>::value
,
3351 // Construct/copy/destroy:
3352 // NB: We overload ctors in some cases instead of using default
3353 // arguments, per 17.4.4.4 para. 2 item 2.
3356 * @brief Default constructor creates an empty string.
3359 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3360 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
3362 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ }
3366 * @brief Construct an empty string using allocator @a a.
3369 basic_string(const _Alloc
& __a
);
3371 // NB: per LWG issue 42, semantics different from IS:
3373 * @brief Construct string with copy of value of @a str.
3374 * @param __str Source string.
3376 basic_string(const basic_string
& __str
);
3378 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3379 // 2583. no way to supply an allocator for basic_string(str, pos)
3381 * @brief Construct string as copy of a substring.
3382 * @param __str Source string.
3383 * @param __pos Index of first character to copy from.
3384 * @param __a Allocator to use.
3386 basic_string(const basic_string
& __str
, size_type __pos
,
3387 const _Alloc
& __a
= _Alloc());
3390 * @brief Construct string as copy of a substring.
3391 * @param __str Source string.
3392 * @param __pos Index of first character to copy from.
3393 * @param __n Number of characters to copy.
3395 basic_string(const basic_string
& __str
, size_type __pos
,
3398 * @brief Construct string as copy of a substring.
3399 * @param __str Source string.
3400 * @param __pos Index of first character to copy from.
3401 * @param __n Number of characters to copy.
3402 * @param __a Allocator to use.
3404 basic_string(const basic_string
& __str
, size_type __pos
,
3405 size_type __n
, const _Alloc
& __a
);
3408 * @brief Construct string initialized by a character %array.
3409 * @param __s Source character %array.
3410 * @param __n Number of characters to copy.
3411 * @param __a Allocator to use (default is default allocator).
3413 * NB: @a __s must have at least @a __n characters, '\\0'
3414 * has no special meaning.
3416 basic_string(const _CharT
* __s
, size_type __n
,
3417 const _Alloc
& __a
= _Alloc());
3419 * @brief Construct string as copy of a C string.
3420 * @param __s Source C string.
3421 * @param __a Allocator to use (default is default allocator).
3423 basic_string(const _CharT
* __s
, const _Alloc
& __a
= _Alloc());
3425 * @brief Construct string as multiple characters.
3426 * @param __n Number of characters.
3427 * @param __c Character to use.
3428 * @param __a Allocator to use (default is default allocator).
3430 basic_string(size_type __n
, _CharT __c
, const _Alloc
& __a
= _Alloc());
3432 #if __cplusplus >= 201103L
3434 * @brief Move construct string.
3435 * @param __str Source string.
3437 * The newly-created string contains the exact contents of @a __str.
3438 * @a __str is a valid, but unspecified string.
3440 basic_string(basic_string
&& __str
)
3441 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3442 noexcept
// FIXME C++11: should always be noexcept.
3444 : _M_dataplus(__str
._M_dataplus
)
3446 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3447 __str
._M_data(_S_empty_rep()._M_refdata());
3449 __str
._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
3454 * @brief Construct string from an initializer %list.
3455 * @param __l std::initializer_list of characters.
3456 * @param __a Allocator to use (default is default allocator).
3458 basic_string(initializer_list
<_CharT
> __l
, const _Alloc
& __a
= _Alloc());
3462 * @brief Construct string as copy of a range.
3463 * @param __beg Start of range.
3464 * @param __end End of range.
3465 * @param __a Allocator to use (default is default allocator).
3467 template<class _InputIterator
>
3468 basic_string(_InputIterator __beg
, _InputIterator __end
,
3469 const _Alloc
& __a
= _Alloc());
3471 #if __cplusplus > 201402L
3473 * @brief Construct string from a substring of a string_view.
3474 * @param __t Source string view.
3475 * @param __pos The index of the first character to copy from __t.
3476 * @param __n The number of characters to copy from __t.
3477 * @param __a Allocator to use.
3479 template<typename _Tp
, typename
= _If_sv
<_Tp
, void>>
3480 basic_string(const _Tp
& __t
, size_type __pos
, size_type __n
,
3481 const _Alloc
& __a
= _Alloc())
3482 : basic_string(__sv_type(__t
).substr(__pos
, __n
), __a
) { }
3485 * @brief Construct string from a string_view.
3486 * @param __sv Source string view.
3487 * @param __a Allocator to use (default is default allocator).
3490 basic_string(__sv_type __sv
, const _Alloc
& __a
= _Alloc())
3491 : basic_string(__sv
.data(), __sv
.size(), __a
) { }
3495 * @brief Destroy the string instance.
3497 ~basic_string() _GLIBCXX_NOEXCEPT
3498 { _M_rep()->_M_dispose(this->get_allocator()); }
3501 * @brief Assign the value of @a str to this string.
3502 * @param __str Source string.
3505 operator=(const basic_string
& __str
)
3506 { return this->assign(__str
); }
3509 * @brief Copy contents of @a s into this string.
3510 * @param __s Source null-terminated string.
3513 operator=(const _CharT
* __s
)
3514 { return this->assign(__s
); }
3517 * @brief Set value to string of length 1.
3518 * @param __c Source character.
3520 * Assigning to a character makes this string length 1 and
3521 * (*this)[0] == @a c.
3524 operator=(_CharT __c
)
3526 this->assign(1, __c
);
3530 #if __cplusplus >= 201103L
3532 * @brief Move assign the value of @a str to this string.
3533 * @param __str Source string.
3535 * The contents of @a str are moved into this string (without copying).
3536 * @a str is a valid, but unspecified string.
3538 // PR 58265, this should be noexcept.
3540 operator=(basic_string
&& __str
)
3548 * @brief Set value to string constructed from initializer %list.
3549 * @param __l std::initializer_list.
3552 operator=(initializer_list
<_CharT
> __l
)
3554 this->assign(__l
.begin(), __l
.size());
3559 #if __cplusplus > 201402L
3561 * @brief Set value to string constructed from a string_view.
3562 * @param __sv A string_view.
3564 template<typename _Tp
>
3565 _If_sv
<_Tp
, basic_string
&>
3567 { return this->assign(__sv
); }
3570 * @brief Convert to a string_view.
3571 * @return A string_view.
3573 operator __sv_type() const noexcept
3574 { return __sv_type(data(), size()); }
3579 * Returns a read/write iterator that points to the first character in
3580 * the %string. Unshares the string.
3583 begin() // FIXME C++11: should be noexcept.
3586 return iterator(_M_data());
3590 * Returns a read-only (constant) iterator that points to the first
3591 * character in the %string.
3594 begin() const _GLIBCXX_NOEXCEPT
3595 { return const_iterator(_M_data()); }
3598 * Returns a read/write iterator that points one past the last
3599 * character in the %string. Unshares the string.
3602 end() // FIXME C++11: should be noexcept.
3605 return iterator(_M_data() + this->size());
3609 * Returns a read-only (constant) iterator that points one past the
3610 * last character in the %string.
3613 end() const _GLIBCXX_NOEXCEPT
3614 { return const_iterator(_M_data() + this->size()); }
3617 * Returns a read/write reverse iterator that points to the last
3618 * character in the %string. Iteration is done in reverse element
3619 * order. Unshares the string.
3622 rbegin() // FIXME C++11: should be noexcept.
3623 { return reverse_iterator(this->end()); }
3626 * Returns a read-only (constant) reverse iterator that points
3627 * to the last character in the %string. Iteration is done in
3628 * reverse element order.
3630 const_reverse_iterator
3631 rbegin() const _GLIBCXX_NOEXCEPT
3632 { return const_reverse_iterator(this->end()); }
3635 * Returns a read/write reverse iterator that points to one before the
3636 * first character in the %string. Iteration is done in reverse
3637 * element order. Unshares the string.
3640 rend() // FIXME C++11: should be noexcept.
3641 { return reverse_iterator(this->begin()); }
3644 * Returns a read-only (constant) reverse iterator that points
3645 * to one before the first character in the %string. Iteration
3646 * is done in reverse element order.
3648 const_reverse_iterator
3649 rend() const _GLIBCXX_NOEXCEPT
3650 { return const_reverse_iterator(this->begin()); }
3652 #if __cplusplus >= 201103L
3654 * Returns a read-only (constant) iterator that points to the first
3655 * character in the %string.
3658 cbegin() const noexcept
3659 { return const_iterator(this->_M_data()); }
3662 * Returns a read-only (constant) iterator that points one past the
3663 * last character in the %string.
3666 cend() const noexcept
3667 { return const_iterator(this->_M_data() + this->size()); }
3670 * Returns a read-only (constant) reverse iterator that points
3671 * to the last character in the %string. Iteration is done in
3672 * reverse element order.
3674 const_reverse_iterator
3675 crbegin() const noexcept
3676 { return const_reverse_iterator(this->end()); }
3679 * Returns a read-only (constant) reverse iterator that points
3680 * to one before the first character in the %string. Iteration
3681 * is done in reverse element order.
3683 const_reverse_iterator
3684 crend() const noexcept
3685 { return const_reverse_iterator(this->begin()); }
3690 /// Returns the number of characters in the string, not including any
3691 /// null-termination.
3693 size() const _GLIBCXX_NOEXCEPT
3694 { return _M_rep()->_M_length
; }
3696 /// Returns the number of characters in the string, not including any
3697 /// null-termination.
3699 length() const _GLIBCXX_NOEXCEPT
3700 { return _M_rep()->_M_length
; }
3702 /// Returns the size() of the largest possible %string.
3704 max_size() const _GLIBCXX_NOEXCEPT
3705 { return _Rep::_S_max_size
; }
3708 * @brief Resizes the %string to the specified number of characters.
3709 * @param __n Number of characters the %string should contain.
3710 * @param __c Character to fill any new elements.
3712 * This function will %resize the %string to the specified
3713 * number of characters. If the number is smaller than the
3714 * %string's current size the %string is truncated, otherwise
3715 * the %string is extended and new elements are %set to @a __c.
3718 resize(size_type __n
, _CharT __c
);
3721 * @brief Resizes the %string to the specified number of characters.
3722 * @param __n Number of characters the %string should contain.
3724 * This function will resize the %string to the specified length. If
3725 * the new size is smaller than the %string's current size the %string
3726 * is truncated, otherwise the %string is extended and new characters
3727 * are default-constructed. For basic types such as char, this means
3728 * setting them to 0.
3731 resize(size_type __n
)
3732 { this->resize(__n
, _CharT()); }
3734 #if __cplusplus >= 201103L
3735 /// A non-binding request to reduce capacity() to size().
3737 shrink_to_fit() _GLIBCXX_NOEXCEPT
3739 #if __cpp_exceptions
3740 if (capacity() > size())
3752 * Returns the total number of characters that the %string can hold
3753 * before needing to allocate more memory.
3756 capacity() const _GLIBCXX_NOEXCEPT
3757 { return _M_rep()->_M_capacity
; }
3760 * @brief Attempt to preallocate enough memory for specified number of
3762 * @param __res_arg Number of characters required.
3763 * @throw std::length_error If @a __res_arg exceeds @c max_size().
3765 * This function attempts to reserve enough memory for the
3766 * %string to hold the specified number of characters. If the
3767 * number requested is more than max_size(), length_error is
3770 * The advantage of this function is that if optimal code is a
3771 * necessity and the user can determine the string length that will be
3772 * required, the user can reserve the memory in %advance, and thus
3773 * prevent a possible reallocation of memory and copying of %string
3777 reserve(size_type __res_arg
= 0);
3780 * Erases the string, making it empty.
3782 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3784 clear() _GLIBCXX_NOEXCEPT
3786 if (_M_rep()->_M_is_shared())
3788 _M_rep()->_M_dispose(this->get_allocator());
3789 _M_data(_S_empty_rep()._M_refdata());
3792 _M_rep()->_M_set_length_and_sharable(0);
3795 // PR 56166: this should not throw.
3798 { _M_mutate(0, this->size(), 0); }
3802 * Returns true if the %string is empty. Equivalent to
3803 * <code>*this == ""</code>.
3806 empty() const _GLIBCXX_NOEXCEPT
3807 { return this->size() == 0; }
3811 * @brief Subscript access to the data contained in the %string.
3812 * @param __pos The index of the character to access.
3813 * @return Read-only (constant) reference to the character.
3815 * This operator allows for easy, array-style, data access.
3816 * Note that data access with this operator is unchecked and
3817 * out_of_range lookups are not defined. (For checked lookups
3821 operator[] (size_type __pos
) const _GLIBCXX_NOEXCEPT
3823 __glibcxx_assert(__pos
<= size());
3824 return _M_data()[__pos
];
3828 * @brief Subscript access to the data contained in the %string.
3829 * @param __pos The index of the character to access.
3830 * @return Read/write reference to the character.
3832 * This operator allows for easy, array-style, data access.
3833 * Note that data access with this operator is unchecked and
3834 * out_of_range lookups are not defined. (For checked lookups
3835 * see at().) Unshares the string.
3838 operator[](size_type __pos
)
3840 // Allow pos == size() both in C++98 mode, as v3 extension,
3841 // and in C++11 mode.
3842 __glibcxx_assert(__pos
<= size());
3843 // In pedantic mode be strict in C++98 mode.
3844 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus
>= 201103L || __pos
< size());
3846 return _M_data()[__pos
];
3850 * @brief Provides access to the data contained in the %string.
3851 * @param __n The index of the character to access.
3852 * @return Read-only (const) reference to the character.
3853 * @throw std::out_of_range If @a n is an invalid index.
3855 * This function provides for safer data access. The parameter is
3856 * first checked that it is in the range of the string. The function
3857 * throws out_of_range if the check fails.
3860 at(size_type __n
) const
3862 if (__n
>= this->size())
3863 __throw_out_of_range_fmt(__N("basic_string::at: __n "
3864 "(which is %zu) >= this->size() "
3867 return _M_data()[__n
];
3871 * @brief Provides access to the data contained in the %string.
3872 * @param __n The index of the character to access.
3873 * @return Read/write reference to the character.
3874 * @throw std::out_of_range If @a n is an invalid index.
3876 * This function provides for safer data access. The parameter is
3877 * first checked that it is in the range of the string. The function
3878 * throws out_of_range if the check fails. Success results in
3879 * unsharing the string.
3885 __throw_out_of_range_fmt(__N("basic_string::at: __n "
3886 "(which is %zu) >= this->size() "
3890 return _M_data()[__n
];
3893 #if __cplusplus >= 201103L
3895 * Returns a read/write reference to the data at the first
3896 * element of the %string.
3901 __glibcxx_assert(!empty());
3902 return operator[](0);
3906 * Returns a read-only (constant) reference to the data at the first
3907 * element of the %string.
3910 front() const noexcept
3912 __glibcxx_assert(!empty());
3913 return operator[](0);
3917 * Returns a read/write reference to the data at the last
3918 * element of the %string.
3923 __glibcxx_assert(!empty());
3924 return operator[](this->size() - 1);
3928 * Returns a read-only (constant) reference to the data at the
3929 * last element of the %string.
3932 back() const noexcept
3934 __glibcxx_assert(!empty());
3935 return operator[](this->size() - 1);
3941 * @brief Append a string to this string.
3942 * @param __str The string to append.
3943 * @return Reference to this string.
3946 operator+=(const basic_string
& __str
)
3947 { return this->append(__str
); }
3950 * @brief Append a C string.
3951 * @param __s The C string to append.
3952 * @return Reference to this string.
3955 operator+=(const _CharT
* __s
)
3956 { return this->append(__s
); }
3959 * @brief Append a character.
3960 * @param __c The character to append.
3961 * @return Reference to this string.
3964 operator+=(_CharT __c
)
3966 this->push_back(__c
);
3970 #if __cplusplus >= 201103L
3972 * @brief Append an initializer_list of characters.
3973 * @param __l The initializer_list of characters to be appended.
3974 * @return Reference to this string.
3977 operator+=(initializer_list
<_CharT
> __l
)
3978 { return this->append(__l
.begin(), __l
.size()); }
3981 #if __cplusplus > 201402L
3983 * @brief Append a string_view.
3984 * @param __sv The string_view to be appended.
3985 * @return Reference to this string.
3988 operator+=(__sv_type __sv
)
3989 { return this->append(__sv
); }
3993 * @brief Append a string to this string.
3994 * @param __str The string to append.
3995 * @return Reference to this string.
3998 append(const basic_string
& __str
);
4001 * @brief Append a substring.
4002 * @param __str The string to append.
4003 * @param __pos Index of the first character of str to append.
4004 * @param __n The number of characters to append.
4005 * @return Reference to this string.
4006 * @throw std::out_of_range if @a __pos is not a valid index.
4008 * This function appends @a __n characters from @a __str
4009 * starting at @a __pos to this string. If @a __n is is larger
4010 * than the number of available characters in @a __str, the
4011 * remainder of @a __str is appended.
4014 append(const basic_string
& __str
, size_type __pos
, size_type __n
);
4017 * @brief Append a C substring.
4018 * @param __s The C string to append.
4019 * @param __n The number of characters to append.
4020 * @return Reference to this string.
4023 append(const _CharT
* __s
, size_type __n
);
4026 * @brief Append a C string.
4027 * @param __s The C string to append.
4028 * @return Reference to this string.
4031 append(const _CharT
* __s
)
4033 __glibcxx_requires_string(__s
);
4034 return this->append(__s
, traits_type::length(__s
));
4038 * @brief Append multiple characters.
4039 * @param __n The number of characters to append.
4040 * @param __c The character to use.
4041 * @return Reference to this string.
4043 * Appends __n copies of __c to this string.
4046 append(size_type __n
, _CharT __c
);
4048 #if __cplusplus >= 201103L
4050 * @brief Append an initializer_list of characters.
4051 * @param __l The initializer_list of characters to append.
4052 * @return Reference to this string.
4055 append(initializer_list
<_CharT
> __l
)
4056 { return this->append(__l
.begin(), __l
.size()); }
4060 * @brief Append a range of characters.
4061 * @param __first Iterator referencing the first character to append.
4062 * @param __last Iterator marking the end of the range.
4063 * @return Reference to this string.
4065 * Appends characters in the range [__first,__last) to this string.
4067 template<class _InputIterator
>
4069 append(_InputIterator __first
, _InputIterator __last
)
4070 { return this->replace(_M_iend(), _M_iend(), __first
, __last
); }
4072 #if __cplusplus > 201402L
4074 * @brief Append a string_view.
4075 * @param __sv The string_view to be appended.
4076 * @return Reference to this string.
4079 append(__sv_type __sv
)
4080 { return this->append(__sv
.data(), __sv
.size()); }
4083 * @brief Append a range of characters from a string_view.
4084 * @param __sv The string_view to be appended from.
4085 * @param __pos The position in the string_view to append from.
4086 * @param __n The number of characters to append from the string_view.
4087 * @return Reference to this string.
4089 template <typename _Tp
>
4090 _If_sv
<_Tp
, basic_string
&>
4091 append(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
4093 __sv_type __sv
= __svt
;
4094 return append(__sv
.data()
4095 + __sv
._M_check(__pos
, "basic_string::append"),
4096 __sv
._M_limit(__pos
, __n
));
4101 * @brief Append a single character.
4102 * @param __c Character to append.
4105 push_back(_CharT __c
)
4107 const size_type __len
= 1 + this->size();
4108 if (__len
> this->capacity() || _M_rep()->_M_is_shared())
4109 this->reserve(__len
);
4110 traits_type::assign(_M_data()[this->size()], __c
);
4111 _M_rep()->_M_set_length_and_sharable(__len
);
4115 * @brief Set value to contents of another string.
4116 * @param __str Source string to use.
4117 * @return Reference to this string.
4120 assign(const basic_string
& __str
);
4122 #if __cplusplus >= 201103L
4124 * @brief Set value to contents of another string.
4125 * @param __str Source string to use.
4126 * @return Reference to this string.
4128 * This function sets this string to the exact contents of @a __str.
4129 * @a __str is a valid, but unspecified string.
4131 // PR 58265, this should be noexcept.
4133 assign(basic_string
&& __str
)
4141 * @brief Set value to a substring of a string.
4142 * @param __str The string to use.
4143 * @param __pos Index of the first character of str.
4144 * @param __n Number of characters to use.
4145 * @return Reference to this string.
4146 * @throw std::out_of_range if @a pos is not a valid index.
4148 * This function sets this string to the substring of @a __str
4149 * consisting of @a __n characters at @a __pos. If @a __n is
4150 * is larger than the number of available characters in @a
4151 * __str, the remainder of @a __str is used.
4154 assign(const basic_string
& __str
, size_type __pos
, size_type __n
)
4155 { return this->assign(__str
._M_data()
4156 + __str
._M_check(__pos
, "basic_string::assign"),
4157 __str
._M_limit(__pos
, __n
)); }
4160 * @brief Set value to a C substring.
4161 * @param __s The C string to use.
4162 * @param __n Number of characters to use.
4163 * @return Reference to this string.
4165 * This function sets the value of this string to the first @a __n
4166 * characters of @a __s. If @a __n is is larger than the number of
4167 * available characters in @a __s, the remainder of @a __s is used.
4170 assign(const _CharT
* __s
, size_type __n
);
4173 * @brief Set value to contents of a C string.
4174 * @param __s The C string to use.
4175 * @return Reference to this string.
4177 * This function sets the value of this string to the value of @a __s.
4178 * The data is copied, so there is no dependence on @a __s once the
4182 assign(const _CharT
* __s
)
4184 __glibcxx_requires_string(__s
);
4185 return this->assign(__s
, traits_type::length(__s
));
4189 * @brief Set value to multiple characters.
4190 * @param __n Length of the resulting string.
4191 * @param __c The character to use.
4192 * @return Reference to this string.
4194 * This function sets the value of this string to @a __n copies of
4198 assign(size_type __n
, _CharT __c
)
4199 { return _M_replace_aux(size_type(0), this->size(), __n
, __c
); }
4202 * @brief Set value to a range of characters.
4203 * @param __first Iterator referencing the first character to append.
4204 * @param __last Iterator marking the end of the range.
4205 * @return Reference to this string.
4207 * Sets value of string to characters in the range [__first,__last).
4209 template<class _InputIterator
>
4211 assign(_InputIterator __first
, _InputIterator __last
)
4212 { return this->replace(_M_ibegin(), _M_iend(), __first
, __last
); }
4214 #if __cplusplus >= 201103L
4216 * @brief Set value to an initializer_list of characters.
4217 * @param __l The initializer_list of characters to assign.
4218 * @return Reference to this string.
4221 assign(initializer_list
<_CharT
> __l
)
4222 { return this->assign(__l
.begin(), __l
.size()); }
4225 #if __cplusplus > 201402L
4227 * @brief Set value from a string_view.
4228 * @param __sv The source string_view.
4229 * @return Reference to this string.
4232 assign(__sv_type __sv
)
4233 { return this->assign(__sv
.data(), __sv
.size()); }
4236 * @brief Set value from a range of characters in a string_view.
4237 * @param __sv The source string_view.
4238 * @param __pos The position in the string_view to assign from.
4239 * @param __n The number of characters to assign.
4240 * @return Reference to this string.
4242 template <typename _Tp
>
4243 _If_sv
<_Tp
, basic_string
&>
4244 assign(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
4246 __sv_type __sv
= __svt
;
4247 return assign(__sv
.data()
4248 + __sv
._M_check(__pos
, "basic_string::assign"),
4249 __sv
._M_limit(__pos
, __n
));
4254 * @brief Insert multiple characters.
4255 * @param __p Iterator referencing location in string to insert at.
4256 * @param __n Number of characters to insert
4257 * @param __c The character to insert.
4258 * @throw std::length_error If new length exceeds @c max_size().
4260 * Inserts @a __n copies of character @a __c starting at the
4261 * position referenced by iterator @a __p. If adding
4262 * characters causes the length to exceed max_size(),
4263 * length_error is thrown. The value of the string doesn't
4264 * change if an error is thrown.
4267 insert(iterator __p
, size_type __n
, _CharT __c
)
4268 { this->replace(__p
, __p
, __n
, __c
); }
4271 * @brief Insert a range of characters.
4272 * @param __p Iterator referencing location in string to insert at.
4273 * @param __beg Start of range.
4274 * @param __end End of range.
4275 * @throw std::length_error If new length exceeds @c max_size().
4277 * Inserts characters in range [__beg,__end). If adding
4278 * characters causes the length to exceed max_size(),
4279 * length_error is thrown. The value of the string doesn't
4280 * change if an error is thrown.
4282 template<class _InputIterator
>
4284 insert(iterator __p
, _InputIterator __beg
, _InputIterator __end
)
4285 { this->replace(__p
, __p
, __beg
, __end
); }
4287 #if __cplusplus >= 201103L
4289 * @brief Insert an initializer_list of characters.
4290 * @param __p Iterator referencing location in string to insert at.
4291 * @param __l The initializer_list of characters to insert.
4292 * @throw std::length_error If new length exceeds @c max_size().
4295 insert(iterator __p
, initializer_list
<_CharT
> __l
)
4297 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
4298 this->insert(__p
- _M_ibegin(), __l
.begin(), __l
.size());
4303 * @brief Insert value of a string.
4304 * @param __pos1 Iterator referencing location in string to insert at.
4305 * @param __str The string to insert.
4306 * @return Reference to this string.
4307 * @throw std::length_error If new length exceeds @c max_size().
4309 * Inserts value of @a __str starting at @a __pos1. If adding
4310 * characters causes the length to exceed max_size(),
4311 * length_error is thrown. The value of the string doesn't
4312 * change if an error is thrown.
4315 insert(size_type __pos1
, const basic_string
& __str
)
4316 { return this->insert(__pos1
, __str
, size_type(0), __str
.size()); }
4319 * @brief Insert a substring.
4320 * @param __pos1 Iterator referencing location in string to insert at.
4321 * @param __str The string to insert.
4322 * @param __pos2 Start of characters in str to insert.
4323 * @param __n Number of characters to insert.
4324 * @return Reference to this string.
4325 * @throw std::length_error If new length exceeds @c max_size().
4326 * @throw std::out_of_range If @a pos1 > size() or
4327 * @a __pos2 > @a str.size().
4329 * Starting at @a pos1, insert @a __n character of @a __str
4330 * beginning with @a __pos2. If adding characters causes the
4331 * length to exceed max_size(), length_error is thrown. If @a
4332 * __pos1 is beyond the end of this string or @a __pos2 is
4333 * beyond the end of @a __str, out_of_range is thrown. The
4334 * value of the string doesn't change if an error is thrown.
4337 insert(size_type __pos1
, const basic_string
& __str
,
4338 size_type __pos2
, size_type __n
)
4339 { return this->insert(__pos1
, __str
._M_data()
4340 + __str
._M_check(__pos2
, "basic_string::insert"),
4341 __str
._M_limit(__pos2
, __n
)); }
4344 * @brief Insert a C substring.
4345 * @param __pos Iterator referencing location in string to insert at.
4346 * @param __s The C string to insert.
4347 * @param __n The number of characters to insert.
4348 * @return Reference to this string.
4349 * @throw std::length_error If new length exceeds @c max_size().
4350 * @throw std::out_of_range If @a __pos is beyond the end of this
4353 * Inserts the first @a __n characters of @a __s starting at @a
4354 * __pos. If adding characters causes the length to exceed
4355 * max_size(), length_error is thrown. If @a __pos is beyond
4356 * end(), out_of_range is thrown. The value of the string
4357 * doesn't change if an error is thrown.
4360 insert(size_type __pos
, const _CharT
* __s
, size_type __n
);
4363 * @brief Insert a C string.
4364 * @param __pos Iterator referencing location in string to insert at.
4365 * @param __s The C string to insert.
4366 * @return Reference to this string.
4367 * @throw std::length_error If new length exceeds @c max_size().
4368 * @throw std::out_of_range If @a pos is beyond the end of this
4371 * Inserts the first @a n characters of @a __s starting at @a __pos. If
4372 * adding characters causes the length to exceed max_size(),
4373 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
4374 * thrown. The value of the string doesn't change if an error is
4378 insert(size_type __pos
, const _CharT
* __s
)
4380 __glibcxx_requires_string(__s
);
4381 return this->insert(__pos
, __s
, traits_type::length(__s
));
4385 * @brief Insert multiple characters.
4386 * @param __pos Index in string to insert at.
4387 * @param __n Number of characters to insert
4388 * @param __c The character to insert.
4389 * @return Reference to this string.
4390 * @throw std::length_error If new length exceeds @c max_size().
4391 * @throw std::out_of_range If @a __pos is beyond the end of this
4394 * Inserts @a __n copies of character @a __c starting at index
4395 * @a __pos. If adding characters causes the length to exceed
4396 * max_size(), length_error is thrown. If @a __pos > length(),
4397 * out_of_range is thrown. The value of the string doesn't
4398 * change if an error is thrown.
4401 insert(size_type __pos
, size_type __n
, _CharT __c
)
4402 { return _M_replace_aux(_M_check(__pos
, "basic_string::insert"),
4403 size_type(0), __n
, __c
); }
4406 * @brief Insert one character.
4407 * @param __p Iterator referencing position in string to insert at.
4408 * @param __c The character to insert.
4409 * @return Iterator referencing newly inserted char.
4410 * @throw std::length_error If new length exceeds @c max_size().
4412 * Inserts character @a __c at position referenced by @a __p.
4413 * If adding character causes the length to exceed max_size(),
4414 * length_error is thrown. If @a __p is beyond end of string,
4415 * out_of_range is thrown. The value of the string doesn't
4416 * change if an error is thrown.
4419 insert(iterator __p
, _CharT __c
)
4421 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
4422 const size_type __pos
= __p
- _M_ibegin();
4423 _M_replace_aux(__pos
, size_type(0), size_type(1), __c
);
4424 _M_rep()->_M_set_leaked();
4425 return iterator(_M_data() + __pos
);
4428 #if __cplusplus > 201402L
4430 * @brief Insert a string_view.
4431 * @param __pos Iterator referencing position in string to insert at.
4432 * @param __sv The string_view to insert.
4433 * @return Reference to this string.
4436 insert(size_type __pos
, __sv_type __sv
)
4437 { return this->insert(__pos
, __sv
.data(), __sv
.size()); }
4440 * @brief Insert a string_view.
4441 * @param __pos Iterator referencing position in string to insert at.
4442 * @param __sv The string_view to insert from.
4443 * @param __pos Iterator referencing position in string_view to insert
4445 * @param __n The number of characters to insert.
4446 * @return Reference to this string.
4448 template <typename _Tp
>
4449 _If_sv
<_Tp
, basic_string
&>
4450 insert(size_type __pos1
, const _Tp
& __svt
,
4451 size_type __pos2
, size_type __n
= npos
)
4453 __sv_type __sv
= __svt
;
4454 return this->replace(__pos1
, size_type(0), __sv
.data()
4455 + __sv
._M_check(__pos2
, "basic_string::insert"),
4456 __sv
._M_limit(__pos2
, __n
));
4461 * @brief Remove characters.
4462 * @param __pos Index of first character to remove (default 0).
4463 * @param __n Number of characters to remove (default remainder).
4464 * @return Reference to this string.
4465 * @throw std::out_of_range If @a pos is beyond the end of this
4468 * Removes @a __n characters from this string starting at @a
4469 * __pos. The length of the string is reduced by @a __n. If
4470 * there are < @a __n characters to remove, the remainder of
4471 * the string is truncated. If @a __p is beyond end of string,
4472 * out_of_range is thrown. The value of the string doesn't
4473 * change if an error is thrown.
4476 erase(size_type __pos
= 0, size_type __n
= npos
)
4478 _M_mutate(_M_check(__pos
, "basic_string::erase"),
4479 _M_limit(__pos
, __n
), size_type(0));
4484 * @brief Remove one character.
4485 * @param __position Iterator referencing the character to remove.
4486 * @return iterator referencing same location after removal.
4488 * Removes the character at @a __position from this string. The value
4489 * of the string doesn't change if an error is thrown.
4492 erase(iterator __position
)
4494 _GLIBCXX_DEBUG_PEDASSERT(__position
>= _M_ibegin()
4495 && __position
< _M_iend());
4496 const size_type __pos
= __position
- _M_ibegin();
4497 _M_mutate(__pos
, size_type(1), size_type(0));
4498 _M_rep()->_M_set_leaked();
4499 return iterator(_M_data() + __pos
);
4503 * @brief Remove a range of characters.
4504 * @param __first Iterator referencing the first character to remove.
4505 * @param __last Iterator referencing the end of the range.
4506 * @return Iterator referencing location of first after removal.
4508 * Removes the characters in the range [first,last) from this string.
4509 * The value of the string doesn't change if an error is thrown.
4512 erase(iterator __first
, iterator __last
);
4514 #if __cplusplus >= 201103L
4516 * @brief Remove the last character.
4518 * The string must be non-empty.
4521 pop_back() // FIXME C++11: should be noexcept.
4523 __glibcxx_assert(!empty());
4524 erase(size() - 1, 1);
4529 * @brief Replace characters with value from another string.
4530 * @param __pos Index of first character to replace.
4531 * @param __n Number of characters to be replaced.
4532 * @param __str String to insert.
4533 * @return Reference to this string.
4534 * @throw std::out_of_range If @a pos is beyond the end of this
4536 * @throw std::length_error If new length exceeds @c max_size().
4538 * Removes the characters in the range [__pos,__pos+__n) from
4539 * this string. In place, the value of @a __str is inserted.
4540 * If @a __pos is beyond end of string, out_of_range is thrown.
4541 * If the length of the result exceeds max_size(), length_error
4542 * is thrown. The value of the string doesn't change if an
4546 replace(size_type __pos
, size_type __n
, const basic_string
& __str
)
4547 { return this->replace(__pos
, __n
, __str
._M_data(), __str
.size()); }
4550 * @brief Replace characters with value from another string.
4551 * @param __pos1 Index of first character to replace.
4552 * @param __n1 Number of characters to be replaced.
4553 * @param __str String to insert.
4554 * @param __pos2 Index of first character of str to use.
4555 * @param __n2 Number of characters from str to use.
4556 * @return Reference to this string.
4557 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
4559 * @throw std::length_error If new length exceeds @c max_size().
4561 * Removes the characters in the range [__pos1,__pos1 + n) from this
4562 * string. In place, the value of @a __str is inserted. If @a __pos is
4563 * beyond end of string, out_of_range is thrown. If the length of the
4564 * result exceeds max_size(), length_error is thrown. The value of the
4565 * string doesn't change if an error is thrown.
4568 replace(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
4569 size_type __pos2
, size_type __n2
)
4570 { return this->replace(__pos1
, __n1
, __str
._M_data()
4571 + __str
._M_check(__pos2
, "basic_string::replace"),
4572 __str
._M_limit(__pos2
, __n2
)); }
4575 * @brief Replace characters with value of a C substring.
4576 * @param __pos Index of first character to replace.
4577 * @param __n1 Number of characters to be replaced.
4578 * @param __s C string to insert.
4579 * @param __n2 Number of characters from @a s to use.
4580 * @return Reference to this string.
4581 * @throw std::out_of_range If @a pos1 > size().
4582 * @throw std::length_error If new length exceeds @c max_size().
4584 * Removes the characters in the range [__pos,__pos + __n1)
4585 * from this string. In place, the first @a __n2 characters of
4586 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
4587 * @a __pos is beyond end of string, out_of_range is thrown. If
4588 * the length of result exceeds max_size(), length_error is
4589 * thrown. The value of the string doesn't change if an error
4593 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
4597 * @brief Replace characters with value of a C string.
4598 * @param __pos Index of first character to replace.
4599 * @param __n1 Number of characters to be replaced.
4600 * @param __s C string to insert.
4601 * @return Reference to this string.
4602 * @throw std::out_of_range If @a pos > size().
4603 * @throw std::length_error If new length exceeds @c max_size().
4605 * Removes the characters in the range [__pos,__pos + __n1)
4606 * from this string. In place, the characters of @a __s are
4607 * inserted. If @a __pos is beyond end of string, out_of_range
4608 * is thrown. If the length of result exceeds max_size(),
4609 * length_error is thrown. The value of the string doesn't
4610 * change if an error is thrown.
4613 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
)
4615 __glibcxx_requires_string(__s
);
4616 return this->replace(__pos
, __n1
, __s
, traits_type::length(__s
));
4620 * @brief Replace characters with multiple characters.
4621 * @param __pos Index of first character to replace.
4622 * @param __n1 Number of characters to be replaced.
4623 * @param __n2 Number of characters to insert.
4624 * @param __c Character to insert.
4625 * @return Reference to this string.
4626 * @throw std::out_of_range If @a __pos > size().
4627 * @throw std::length_error If new length exceeds @c max_size().
4629 * Removes the characters in the range [pos,pos + n1) from this
4630 * string. In place, @a __n2 copies of @a __c are inserted.
4631 * If @a __pos is beyond end of string, out_of_range is thrown.
4632 * If the length of result exceeds max_size(), length_error is
4633 * thrown. The value of the string doesn't change if an error
4637 replace(size_type __pos
, size_type __n1
, size_type __n2
, _CharT __c
)
4638 { return _M_replace_aux(_M_check(__pos
, "basic_string::replace"),
4639 _M_limit(__pos
, __n1
), __n2
, __c
); }
4642 * @brief Replace range of characters with string.
4643 * @param __i1 Iterator referencing start of range to replace.
4644 * @param __i2 Iterator referencing end of range to replace.
4645 * @param __str String value to insert.
4646 * @return Reference to this string.
4647 * @throw std::length_error If new length exceeds @c max_size().
4649 * Removes the characters in the range [__i1,__i2). In place,
4650 * the value of @a __str is inserted. If the length of result
4651 * exceeds max_size(), length_error is thrown. The value of
4652 * the string doesn't change if an error is thrown.
4655 replace(iterator __i1
, iterator __i2
, const basic_string
& __str
)
4656 { return this->replace(__i1
, __i2
, __str
._M_data(), __str
.size()); }
4659 * @brief Replace range of characters with C substring.
4660 * @param __i1 Iterator referencing start of range to replace.
4661 * @param __i2 Iterator referencing end of range to replace.
4662 * @param __s C string value to insert.
4663 * @param __n Number of characters from s to insert.
4664 * @return Reference to this string.
4665 * @throw std::length_error If new length exceeds @c max_size().
4667 * Removes the characters in the range [__i1,__i2). In place,
4668 * the first @a __n characters of @a __s are inserted. If the
4669 * length of result exceeds max_size(), length_error is thrown.
4670 * The value of the string doesn't change if an error is
4674 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
, size_type __n
)
4676 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4677 && __i2
<= _M_iend());
4678 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
, __s
, __n
);
4682 * @brief Replace range of characters with C string.
4683 * @param __i1 Iterator referencing start of range to replace.
4684 * @param __i2 Iterator referencing end of range to replace.
4685 * @param __s C string value to insert.
4686 * @return Reference to this string.
4687 * @throw std::length_error If new length exceeds @c max_size().
4689 * Removes the characters in the range [__i1,__i2). In place,
4690 * the characters of @a __s are inserted. If the length of
4691 * result exceeds max_size(), length_error is thrown. The
4692 * value of the string doesn't change if an error is thrown.
4695 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
)
4697 __glibcxx_requires_string(__s
);
4698 return this->replace(__i1
, __i2
, __s
, traits_type::length(__s
));
4702 * @brief Replace range of characters with multiple characters
4703 * @param __i1 Iterator referencing start of range to replace.
4704 * @param __i2 Iterator referencing end of range to replace.
4705 * @param __n Number of characters to insert.
4706 * @param __c Character to insert.
4707 * @return Reference to this string.
4708 * @throw std::length_error If new length exceeds @c max_size().
4710 * Removes the characters in the range [__i1,__i2). In place,
4711 * @a __n copies of @a __c are inserted. If the length of
4712 * result exceeds max_size(), length_error is thrown. The
4713 * value of the string doesn't change if an error is thrown.
4716 replace(iterator __i1
, iterator __i2
, size_type __n
, _CharT __c
)
4718 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4719 && __i2
<= _M_iend());
4720 return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __c
);
4724 * @brief Replace range of characters with range.
4725 * @param __i1 Iterator referencing start of range to replace.
4726 * @param __i2 Iterator referencing end of range to replace.
4727 * @param __k1 Iterator referencing start of range to insert.
4728 * @param __k2 Iterator referencing end of range to insert.
4729 * @return Reference to this string.
4730 * @throw std::length_error If new length exceeds @c max_size().
4732 * Removes the characters in the range [__i1,__i2). In place,
4733 * characters in the range [__k1,__k2) are inserted. If the
4734 * length of result exceeds max_size(), length_error is thrown.
4735 * The value of the string doesn't change if an error is
4738 template<class _InputIterator
>
4740 replace(iterator __i1
, iterator __i2
,
4741 _InputIterator __k1
, _InputIterator __k2
)
4743 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4744 && __i2
<= _M_iend());
4745 __glibcxx_requires_valid_range(__k1
, __k2
);
4746 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
4747 return _M_replace_dispatch(__i1
, __i2
, __k1
, __k2
, _Integral());
4750 // Specializations for the common case of pointer and iterator:
4751 // useful to avoid the overhead of temporary buffering in _M_replace.
4753 replace(iterator __i1
, iterator __i2
, _CharT
* __k1
, _CharT
* __k2
)
4755 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4756 && __i2
<= _M_iend());
4757 __glibcxx_requires_valid_range(__k1
, __k2
);
4758 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4763 replace(iterator __i1
, iterator __i2
,
4764 const _CharT
* __k1
, const _CharT
* __k2
)
4766 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4767 && __i2
<= _M_iend());
4768 __glibcxx_requires_valid_range(__k1
, __k2
);
4769 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4774 replace(iterator __i1
, iterator __i2
, iterator __k1
, iterator __k2
)
4776 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4777 && __i2
<= _M_iend());
4778 __glibcxx_requires_valid_range(__k1
, __k2
);
4779 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4780 __k1
.base(), __k2
- __k1
);
4784 replace(iterator __i1
, iterator __i2
,
4785 const_iterator __k1
, const_iterator __k2
)
4787 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4788 && __i2
<= _M_iend());
4789 __glibcxx_requires_valid_range(__k1
, __k2
);
4790 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4791 __k1
.base(), __k2
- __k1
);
4794 #if __cplusplus >= 201103L
4796 * @brief Replace range of characters with initializer_list.
4797 * @param __i1 Iterator referencing start of range to replace.
4798 * @param __i2 Iterator referencing end of range to replace.
4799 * @param __l The initializer_list of characters to insert.
4800 * @return Reference to this string.
4801 * @throw std::length_error If new length exceeds @c max_size().
4803 * Removes the characters in the range [__i1,__i2). In place,
4804 * characters in the range [__k1,__k2) are inserted. If the
4805 * length of result exceeds max_size(), length_error is thrown.
4806 * The value of the string doesn't change if an error is
4809 basic_string
& replace(iterator __i1
, iterator __i2
,
4810 initializer_list
<_CharT
> __l
)
4811 { return this->replace(__i1
, __i2
, __l
.begin(), __l
.end()); }
4814 #if __cplusplus > 201402L
4816 * @brief Replace range of characters with string_view.
4817 * @param __pos The position to replace at.
4818 * @param __n The number of characters to replace.
4819 * @param __sv The string_view to insert.
4820 * @return Reference to this string.
4823 replace(size_type __pos
, size_type __n
, __sv_type __sv
)
4824 { return this->replace(__pos
, __n
, __sv
.data(), __sv
.size()); }
4827 * @brief Replace range of characters with string_view.
4828 * @param __pos1 The position to replace at.
4829 * @param __n1 The number of characters to replace.
4830 * @param __sv The string_view to insert from.
4831 * @param __pos2 The position in the string_view to insert from.
4832 * @param __n2 The number of characters to insert.
4833 * @return Reference to this string.
4835 template <typename _Tp
>
4836 _If_sv
<_Tp
, basic_string
&>
4837 replace(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
4838 size_type __pos2
, size_type __n2
= npos
)
4840 __sv_type __sv
= __svt
;
4841 return this->replace(__pos1
, __n1
, __sv
.data()
4842 + __sv
._M_check(__pos2
, "basic_string::replace"),
4843 __sv
._M_limit(__pos2
, __n2
));
4847 * @brief Replace range of characters with string_view.
4848 * @param __i1 An iterator referencing the start position
4850 * @param __i2 An iterator referencing the end position
4852 * @param __sv The string_view to insert from.
4853 * @return Reference to this string.
4856 replace(const_iterator __i1
, const_iterator __i2
, __sv_type __sv
)
4857 { return this->replace(__i1
- begin(), __i2
- __i1
, __sv
); }
4861 template<class _Integer
>
4863 _M_replace_dispatch(iterator __i1
, iterator __i2
, _Integer __n
,
4864 _Integer __val
, __true_type
)
4865 { return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __val
); }
4867 template<class _InputIterator
>
4869 _M_replace_dispatch(iterator __i1
, iterator __i2
, _InputIterator __k1
,
4870 _InputIterator __k2
, __false_type
);
4873 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
4877 _M_replace_safe(size_type __pos1
, size_type __n1
, const _CharT
* __s
,
4880 // _S_construct_aux is used to implement the 21.3.1 para 15 which
4881 // requires special behaviour if _InIter is an integral type
4882 template<class _InIterator
>
4884 _S_construct_aux(_InIterator __beg
, _InIterator __end
,
4885 const _Alloc
& __a
, __false_type
)
4887 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
4888 return _S_construct(__beg
, __end
, __a
, _Tag());
4891 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4892 // 438. Ambiguity in the "do the right thing" clause
4893 template<class _Integer
>
4895 _S_construct_aux(_Integer __beg
, _Integer __end
,
4896 const _Alloc
& __a
, __true_type
)
4897 { return _S_construct_aux_2(static_cast<size_type
>(__beg
),
4901 _S_construct_aux_2(size_type __req
, _CharT __c
, const _Alloc
& __a
)
4902 { return _S_construct(__req
, __c
, __a
); }
4904 template<class _InIterator
>
4906 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
)
4908 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
4909 return _S_construct_aux(__beg
, __end
, __a
, _Integral());
4912 // For Input Iterators, used in istreambuf_iterators, etc.
4913 template<class _InIterator
>
4915 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
,
4916 input_iterator_tag
);
4918 // For forward_iterators up to random_access_iterators, used for
4919 // string::iterator, _CharT*, etc.
4920 template<class _FwdIterator
>
4922 _S_construct(_FwdIterator __beg
, _FwdIterator __end
, const _Alloc
& __a
,
4923 forward_iterator_tag
);
4926 _S_construct(size_type __req
, _CharT __c
, const _Alloc
& __a
);
4931 * @brief Copy substring into C string.
4932 * @param __s C string to copy value into.
4933 * @param __n Number of characters to copy.
4934 * @param __pos Index of first character to copy.
4935 * @return Number of characters actually copied
4936 * @throw std::out_of_range If __pos > size().
4938 * Copies up to @a __n characters starting at @a __pos into the
4939 * C string @a __s. If @a __pos is %greater than size(),
4940 * out_of_range is thrown.
4943 copy(_CharT
* __s
, size_type __n
, size_type __pos
= 0) const;
4946 * @brief Swap contents with another string.
4947 * @param __s String to swap with.
4949 * Exchanges the contents of this string with that of @a __s in constant
4952 // PR 58265, this should be noexcept.
4954 swap(basic_string
& __s
);
4956 // String operations:
4958 * @brief Return const pointer to null-terminated contents.
4960 * This is a handle to internal data. Do not modify or dire things may
4964 c_str() const _GLIBCXX_NOEXCEPT
4965 { return _M_data(); }
4968 * @brief Return const pointer to contents.
4970 * This is a pointer to internal data. It is undefined to modify
4971 * the contents through the returned pointer. To get a pointer that
4972 * allows modifying the contents use @c &str[0] instead,
4973 * (or in C++17 the non-const @c str.data() overload).
4976 data() const _GLIBCXX_NOEXCEPT
4977 { return _M_data(); }
4979 #if __cplusplus > 201402L
4981 * @brief Return non-const pointer to contents.
4983 * This is a pointer to the character sequence held by the string.
4984 * Modifying the characters in the sequence is allowed.
4988 { return _M_data(); }
4992 * @brief Return copy of allocator used to construct this string.
4995 get_allocator() const _GLIBCXX_NOEXCEPT
4996 { return _M_dataplus
; }
4999 * @brief Find position of a C substring.
5000 * @param __s C string to locate.
5001 * @param __pos Index of character to search from.
5002 * @param __n Number of characters from @a s to search for.
5003 * @return Index of start of first occurrence.
5005 * Starting from @a __pos, searches forward for the first @a
5006 * __n characters in @a __s within this string. If found,
5007 * returns the index where it begins. If not found, returns
5011 find(const _CharT
* __s
, size_type __pos
, size_type __n
) const
5015 * @brief Find position of a string.
5016 * @param __str String to locate.
5017 * @param __pos Index of character to search from (default 0).
5018 * @return Index of start of first occurrence.
5020 * Starting from @a __pos, searches forward for value of @a __str within
5021 * this string. If found, returns the index where it begins. If not
5022 * found, returns npos.
5025 find(const basic_string
& __str
, size_type __pos
= 0) const
5027 { return this->find(__str
.data(), __pos
, __str
.size()); }
5030 * @brief Find position of a C string.
5031 * @param __s C string to locate.
5032 * @param __pos Index of character to search from (default 0).
5033 * @return Index of start of first occurrence.
5035 * Starting from @a __pos, searches forward for the value of @a
5036 * __s within this string. If found, returns the index where
5037 * it begins. If not found, returns npos.
5040 find(const _CharT
* __s
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
5042 __glibcxx_requires_string(__s
);
5043 return this->find(__s
, __pos
, traits_type::length(__s
));
5047 * @brief Find position of a character.
5048 * @param __c Character to locate.
5049 * @param __pos Index of character to search from (default 0).
5050 * @return Index of first occurrence.
5052 * Starting from @a __pos, searches forward for @a __c within
5053 * this string. If found, returns the index where it was
5054 * found. If not found, returns npos.
5057 find(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
;
5059 #if __cplusplus > 201402L
5061 * @brief Find position of a string_view.
5062 * @param __sv The string_view to locate.
5063 * @param __pos Index of character to search from (default 0).
5064 * @return Index of start of first occurrence.
5067 find(__sv_type __sv
, size_type __pos
= 0) const noexcept
5068 { return this->find(__sv
.data(), __pos
, __sv
.size()); }
5072 * @brief Find last position of a string.
5073 * @param __str String to locate.
5074 * @param __pos Index of character to search back from (default end).
5075 * @return Index of start of last occurrence.
5077 * Starting from @a __pos, searches backward for value of @a
5078 * __str within this string. If found, returns the index where
5079 * it begins. If not found, returns npos.
5082 rfind(const basic_string
& __str
, size_type __pos
= npos
) const
5084 { return this->rfind(__str
.data(), __pos
, __str
.size()); }
5087 * @brief Find last position of a C substring.
5088 * @param __s C string to locate.
5089 * @param __pos Index of character to search back from.
5090 * @param __n Number of characters from s to search for.
5091 * @return Index of start of last occurrence.
5093 * Starting from @a __pos, searches backward for the first @a
5094 * __n characters in @a __s within this string. If found,
5095 * returns the index where it begins. If not found, returns
5099 rfind(const _CharT
* __s
, size_type __pos
, size_type __n
) const
5103 * @brief Find last position of a C string.
5104 * @param __s C string to locate.
5105 * @param __pos Index of character to start search at (default end).
5106 * @return Index of start of last occurrence.
5108 * Starting from @a __pos, searches backward for the value of
5109 * @a __s within this string. If found, returns the index
5110 * where it begins. If not found, returns npos.
5113 rfind(const _CharT
* __s
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
5115 __glibcxx_requires_string(__s
);
5116 return this->rfind(__s
, __pos
, traits_type::length(__s
));
5120 * @brief Find last position of a character.
5121 * @param __c Character to locate.
5122 * @param __pos Index of character to search back from (default end).
5123 * @return Index of last occurrence.
5125 * Starting from @a __pos, searches backward for @a __c within
5126 * this string. If found, returns the index where it was
5127 * found. If not found, returns npos.
5130 rfind(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
;
5132 #if __cplusplus > 201402L
5134 * @brief Find last position of a string_view.
5135 * @param __sv The string_view to locate.
5136 * @param __pos Index of character to search back from (default end).
5137 * @return Index of start of last occurrence.
5140 rfind(__sv_type __sv
, size_type __pos
= npos
) const noexcept
5141 { return this->rfind(__sv
.data(), __pos
, __sv
.size()); }
5145 * @brief Find position of a character of string.
5146 * @param __str String containing characters to locate.
5147 * @param __pos Index of character to search from (default 0).
5148 * @return Index of first occurrence.
5150 * Starting from @a __pos, searches forward for one of the
5151 * characters of @a __str within this string. If found,
5152 * returns the index where it was found. If not found, returns
5156 find_first_of(const basic_string
& __str
, size_type __pos
= 0) const
5158 { return this->find_first_of(__str
.data(), __pos
, __str
.size()); }
5161 * @brief Find position of a character of C substring.
5162 * @param __s String containing characters to locate.
5163 * @param __pos Index of character to search from.
5164 * @param __n Number of characters from s to search for.
5165 * @return Index of first occurrence.
5167 * Starting from @a __pos, searches forward for one of the
5168 * first @a __n characters of @a __s within this string. If
5169 * found, returns the index where it was found. If not found,
5173 find_first_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
5177 * @brief Find position of a character of C string.
5178 * @param __s String containing characters to locate.
5179 * @param __pos Index of character to search from (default 0).
5180 * @return Index of first occurrence.
5182 * Starting from @a __pos, searches forward for one of the
5183 * characters of @a __s within this string. If found, returns
5184 * the index where it was found. If not found, returns npos.
5187 find_first_of(const _CharT
* __s
, size_type __pos
= 0) const
5190 __glibcxx_requires_string(__s
);
5191 return this->find_first_of(__s
, __pos
, traits_type::length(__s
));
5195 * @brief Find position of a character.
5196 * @param __c Character to locate.
5197 * @param __pos Index of character to search from (default 0).
5198 * @return Index of first occurrence.
5200 * Starting from @a __pos, searches forward for the character
5201 * @a __c within this string. If found, returns the index
5202 * where it was found. If not found, returns npos.
5204 * Note: equivalent to find(__c, __pos).
5207 find_first_of(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
5208 { return this->find(__c
, __pos
); }
5210 #if __cplusplus > 201402L
5212 * @brief Find position of a character of a string_view.
5213 * @param __sv A string_view containing characters to locate.
5214 * @param __pos Index of character to search from (default 0).
5215 * @return Index of first occurrence.
5218 find_first_of(__sv_type __sv
, size_type __pos
= 0) const noexcept
5219 { return this->find_first_of(__sv
.data(), __pos
, __sv
.size()); }
5223 * @brief Find last position of a character of string.
5224 * @param __str String containing characters to locate.
5225 * @param __pos Index of character to search back from (default end).
5226 * @return Index of last occurrence.
5228 * Starting from @a __pos, searches backward for one of the
5229 * characters of @a __str within this string. If found,
5230 * returns the index where it was found. If not found, returns
5234 find_last_of(const basic_string
& __str
, size_type __pos
= npos
) const
5236 { return this->find_last_of(__str
.data(), __pos
, __str
.size()); }
5239 * @brief Find last position of a character of C substring.
5240 * @param __s C string containing characters to locate.
5241 * @param __pos Index of character to search back from.
5242 * @param __n Number of characters from s to search for.
5243 * @return Index of last occurrence.
5245 * Starting from @a __pos, searches backward for one of the
5246 * first @a __n characters of @a __s within this string. If
5247 * found, returns the index where it was found. If not found,
5251 find_last_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
5255 * @brief Find last position of a character of C string.
5256 * @param __s C string containing characters to locate.
5257 * @param __pos Index of character to search back from (default end).
5258 * @return Index of last occurrence.
5260 * Starting from @a __pos, searches backward for one of the
5261 * characters of @a __s within this string. If found, returns
5262 * the index where it was found. If not found, returns npos.
5265 find_last_of(const _CharT
* __s
, size_type __pos
= npos
) const
5268 __glibcxx_requires_string(__s
);
5269 return this->find_last_of(__s
, __pos
, traits_type::length(__s
));
5273 * @brief Find last position of a character.
5274 * @param __c Character to locate.
5275 * @param __pos Index of character to search back from (default end).
5276 * @return Index of last occurrence.
5278 * Starting from @a __pos, searches backward for @a __c within
5279 * this string. If found, returns the index where it was
5280 * found. If not found, returns npos.
5282 * Note: equivalent to rfind(__c, __pos).
5285 find_last_of(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
5286 { return this->rfind(__c
, __pos
); }
5288 #if __cplusplus > 201402L
5290 * @brief Find last position of a character of string.
5291 * @param __sv A string_view containing characters to locate.
5292 * @param __pos Index of character to search back from (default end).
5293 * @return Index of last occurrence.
5296 find_last_of(__sv_type __sv
, size_type __pos
= npos
) const noexcept
5297 { return this->find_last_of(__sv
.data(), __pos
, __sv
.size()); }
5301 * @brief Find position of a character not in string.
5302 * @param __str String containing characters to avoid.
5303 * @param __pos Index of character to search from (default 0).
5304 * @return Index of first occurrence.
5306 * Starting from @a __pos, searches forward for a character not contained
5307 * in @a __str within this string. If found, returns the index where it
5308 * was found. If not found, returns npos.
5311 find_first_not_of(const basic_string
& __str
, size_type __pos
= 0) const
5313 { return this->find_first_not_of(__str
.data(), __pos
, __str
.size()); }
5316 * @brief Find position of a character not in C substring.
5317 * @param __s C string containing characters to avoid.
5318 * @param __pos Index of character to search from.
5319 * @param __n Number of characters from __s to consider.
5320 * @return Index of first occurrence.
5322 * Starting from @a __pos, searches forward for a character not
5323 * contained in the first @a __n characters of @a __s within
5324 * this string. If found, returns the index where it was
5325 * found. If not found, returns npos.
5328 find_first_not_of(const _CharT
* __s
, size_type __pos
,
5329 size_type __n
) const _GLIBCXX_NOEXCEPT
;
5332 * @brief Find position of a character not in C string.
5333 * @param __s C string containing characters to avoid.
5334 * @param __pos Index of character to search from (default 0).
5335 * @return Index of first occurrence.
5337 * Starting from @a __pos, searches forward for a character not
5338 * contained in @a __s within this string. If found, returns
5339 * the index where it was found. If not found, returns npos.
5342 find_first_not_of(const _CharT
* __s
, size_type __pos
= 0) const
5345 __glibcxx_requires_string(__s
);
5346 return this->find_first_not_of(__s
, __pos
, traits_type::length(__s
));
5350 * @brief Find position of a different character.
5351 * @param __c Character to avoid.
5352 * @param __pos Index of character to search from (default 0).
5353 * @return Index of first occurrence.
5355 * Starting from @a __pos, searches forward for a character
5356 * other than @a __c within this string. If found, returns the
5357 * index where it was found. If not found, returns npos.
5360 find_first_not_of(_CharT __c
, size_type __pos
= 0) const
5363 #if __cplusplus > 201402L
5365 * @brief Find position of a character not in a string_view.
5366 * @param __sv A string_view containing characters to avoid.
5367 * @param __pos Index of character to search from (default 0).
5368 * @return Index of first occurrence.
5371 find_first_not_of(__sv_type __sv
, size_type __pos
= 0) const noexcept
5372 { return this->find_first_not_of(__sv
.data(), __pos
, __sv
.size()); }
5376 * @brief Find last position of a character not in string.
5377 * @param __str String containing characters to avoid.
5378 * @param __pos Index of character to search back from (default end).
5379 * @return Index of last occurrence.
5381 * Starting from @a __pos, searches backward for a character
5382 * not contained in @a __str within this string. If found,
5383 * returns the index where it was found. If not found, returns
5387 find_last_not_of(const basic_string
& __str
, size_type __pos
= npos
) const
5389 { return this->find_last_not_of(__str
.data(), __pos
, __str
.size()); }
5392 * @brief Find last position of a character not in C substring.
5393 * @param __s C string containing characters to avoid.
5394 * @param __pos Index of character to search back from.
5395 * @param __n Number of characters from s to consider.
5396 * @return Index of last occurrence.
5398 * Starting from @a __pos, searches backward for a character not
5399 * contained in the first @a __n characters of @a __s within this string.
5400 * If found, returns the index where it was found. If not found,
5404 find_last_not_of(const _CharT
* __s
, size_type __pos
,
5405 size_type __n
) const _GLIBCXX_NOEXCEPT
;
5407 * @brief Find last position of a character not in C string.
5408 * @param __s C string containing characters to avoid.
5409 * @param __pos Index of character to search back from (default end).
5410 * @return Index of last occurrence.
5412 * Starting from @a __pos, searches backward for a character
5413 * not contained in @a __s within this string. If found,
5414 * returns the index where it was found. If not found, returns
5418 find_last_not_of(const _CharT
* __s
, size_type __pos
= npos
) const
5421 __glibcxx_requires_string(__s
);
5422 return this->find_last_not_of(__s
, __pos
, traits_type::length(__s
));
5426 * @brief Find last position of a different character.
5427 * @param __c Character to avoid.
5428 * @param __pos Index of character to search back from (default end).
5429 * @return Index of last occurrence.
5431 * Starting from @a __pos, searches backward for a character other than
5432 * @a __c within this string. If found, returns the index where it was
5433 * found. If not found, returns npos.
5436 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const
5439 #if __cplusplus > 201402L
5441 * @brief Find last position of a character not in a string_view.
5442 * @param __sv A string_view containing characters to avoid.
5443 * @param __pos Index of character to search back from (default end).
5444 * @return Index of last occurrence.
5447 find_last_not_of(__sv_type __sv
, size_type __pos
= npos
) const noexcept
5448 { return this->find_last_not_of(__sv
.data(), __pos
, __sv
.size()); }
5452 * @brief Get a substring.
5453 * @param __pos Index of first character (default 0).
5454 * @param __n Number of characters in substring (default remainder).
5455 * @return The new string.
5456 * @throw std::out_of_range If __pos > size().
5458 * Construct and return a new string using the @a __n
5459 * characters starting at @a __pos. If the string is too
5460 * short, use the remainder of the characters. If @a __pos is
5461 * beyond the end of the string, out_of_range is thrown.
5464 substr(size_type __pos
= 0, size_type __n
= npos
) const
5465 { return basic_string(*this,
5466 _M_check(__pos
, "basic_string::substr"), __n
); }
5469 * @brief Compare to a string.
5470 * @param __str String to compare against.
5471 * @return Integer < 0, 0, or > 0.
5473 * Returns an integer < 0 if this string is ordered before @a
5474 * __str, 0 if their values are equivalent, or > 0 if this
5475 * string is ordered after @a __str. Determines the effective
5476 * length rlen of the strings to compare as the smallest of
5477 * size() and str.size(). The function then compares the two
5478 * strings by calling traits::compare(data(), str.data(),rlen).
5479 * If the result of the comparison is nonzero returns it,
5480 * otherwise the shorter one is ordered first.
5483 compare(const basic_string
& __str
) const
5485 const size_type __size
= this->size();
5486 const size_type __osize
= __str
.size();
5487 const size_type __len
= std::min(__size
, __osize
);
5489 int __r
= traits_type::compare(_M_data(), __str
.data(), __len
);
5491 __r
= _S_compare(__size
, __osize
);
5495 #if __cplusplus > 201402L
5497 * @brief Compare to a string_view.
5498 * @param __sv A string_view to compare against.
5499 * @return Integer < 0, 0, or > 0.
5502 compare(__sv_type __sv
) const
5504 const size_type __size
= this->size();
5505 const size_type __osize
= __sv
.size();
5506 const size_type __len
= std::min(__size
, __osize
);
5508 int __r
= traits_type::compare(_M_data(), __sv
.data(), __len
);
5510 __r
= _S_compare(__size
, __osize
);
5515 * @brief Compare to a string_view.
5516 * @param __pos A position in the string to start comparing from.
5517 * @param __n The number of characters to compare.
5518 * @param __sv A string_view to compare against.
5519 * @return Integer < 0, 0, or > 0.
5522 compare(size_type __pos
, size_type __n
, __sv_type __sv
) const
5523 { return __sv_type(*this).substr(__pos
, __n
).compare(__sv
); }
5526 * @brief Compare to a string_view.
5527 * @param __pos1 A position in the string to start comparing from.
5528 * @param __n1 The number of characters to compare.
5529 * @param __sv A string_view to compare against.
5530 * @param __pos2 A position in the string_view to start comparing from.
5531 * @param __n2 The number of characters to compare.
5532 * @return Integer < 0, 0, or > 0.
5534 template <typename _Tp
>
5536 compare(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
5537 size_type __pos2
, size_type __n2
= npos
) const
5539 __sv_type __sv
= __svt
;
5540 return __sv_type(*this)
5541 .substr(__pos1
, __n1
).compare(__sv
.substr(__pos2
, __n2
));
5546 * @brief Compare substring to a string.
5547 * @param __pos Index of first character of substring.
5548 * @param __n Number of characters in substring.
5549 * @param __str String to compare against.
5550 * @return Integer < 0, 0, or > 0.
5552 * Form the substring of this string from the @a __n characters
5553 * starting at @a __pos. Returns an integer < 0 if the
5554 * substring is ordered before @a __str, 0 if their values are
5555 * equivalent, or > 0 if the substring is ordered after @a
5556 * __str. Determines the effective length rlen of the strings
5557 * to compare as the smallest of the length of the substring
5558 * and @a __str.size(). The function then compares the two
5559 * strings by calling
5560 * traits::compare(substring.data(),str.data(),rlen). If the
5561 * result of the comparison is nonzero returns it, otherwise
5562 * the shorter one is ordered first.
5565 compare(size_type __pos
, size_type __n
, const basic_string
& __str
) const;
5568 * @brief Compare substring to a substring.
5569 * @param __pos1 Index of first character of substring.
5570 * @param __n1 Number of characters in substring.
5571 * @param __str String to compare against.
5572 * @param __pos2 Index of first character of substring of str.
5573 * @param __n2 Number of characters in substring of str.
5574 * @return Integer < 0, 0, or > 0.
5576 * Form the substring of this string from the @a __n1
5577 * characters starting at @a __pos1. Form the substring of @a
5578 * __str from the @a __n2 characters starting at @a __pos2.
5579 * Returns an integer < 0 if this substring is ordered before
5580 * the substring of @a __str, 0 if their values are equivalent,
5581 * or > 0 if this substring is ordered after the substring of
5582 * @a __str. Determines the effective length rlen of the
5583 * strings to compare as the smallest of the lengths of the
5584 * substrings. The function then compares the two strings by
5586 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
5587 * If the result of the comparison is nonzero returns it,
5588 * otherwise the shorter one is ordered first.
5591 compare(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
5592 size_type __pos2
, size_type __n2
) const;
5595 * @brief Compare to a C string.
5596 * @param __s C string to compare against.
5597 * @return Integer < 0, 0, or > 0.
5599 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
5600 * their values are equivalent, or > 0 if this string is ordered after
5601 * @a __s. Determines the effective length rlen of the strings to
5602 * compare as the smallest of size() and the length of a string
5603 * constructed from @a __s. The function then compares the two strings
5604 * by calling traits::compare(data(),s,rlen). If the result of the
5605 * comparison is nonzero returns it, otherwise the shorter one is
5609 compare(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
;
5611 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5612 // 5 String::compare specification questionable
5614 * @brief Compare substring to a C string.
5615 * @param __pos Index of first character of substring.
5616 * @param __n1 Number of characters in substring.
5617 * @param __s C string to compare against.
5618 * @return Integer < 0, 0, or > 0.
5620 * Form the substring of this string from the @a __n1
5621 * characters starting at @a pos. Returns an integer < 0 if
5622 * the substring is ordered before @a __s, 0 if their values
5623 * are equivalent, or > 0 if the substring is ordered after @a
5624 * __s. Determines the effective length rlen of the strings to
5625 * compare as the smallest of the length of the substring and
5626 * the length of a string constructed from @a __s. The
5627 * function then compares the two string by calling
5628 * traits::compare(substring.data(),__s,rlen). If the result of
5629 * the comparison is nonzero returns it, otherwise the shorter
5630 * one is ordered first.
5633 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
) const;
5636 * @brief Compare substring against a character %array.
5637 * @param __pos Index of first character of substring.
5638 * @param __n1 Number of characters in substring.
5639 * @param __s character %array to compare against.
5640 * @param __n2 Number of characters of s.
5641 * @return Integer < 0, 0, or > 0.
5643 * Form the substring of this string from the @a __n1
5644 * characters starting at @a __pos. Form a string from the
5645 * first @a __n2 characters of @a __s. Returns an integer < 0
5646 * if this substring is ordered before the string from @a __s,
5647 * 0 if their values are equivalent, or > 0 if this substring
5648 * is ordered after the string from @a __s. Determines the
5649 * effective length rlen of the strings to compare as the
5650 * smallest of the length of the substring and @a __n2. The
5651 * function then compares the two strings by calling
5652 * traits::compare(substring.data(),s,rlen). If the result of
5653 * the comparison is nonzero returns it, otherwise the shorter
5654 * one is ordered first.
5656 * NB: s must have at least n2 characters, '\\0' has
5657 * no special meaning.
5660 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
,
5661 size_type __n2
) const;
5663 # ifdef _GLIBCXX_TM_TS_INTERNAL
5665 ::_txnal_cow_string_C1_for_exceptions(void* that
, const char* s
,
5668 ::_txnal_cow_string_c_str(const void *that
);
5670 ::_txnal_cow_string_D1(void *that
);
5672 ::_txnal_cow_string_D1_commit(void *that
);
5675 #endif // !_GLIBCXX_USE_CXX11_ABI
5679 * @brief Concatenate two strings.
5680 * @param __lhs First string.
5681 * @param __rhs Last string.
5682 * @return New string with value of @a __lhs followed by @a __rhs.
5684 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5685 basic_string
<_CharT
, _Traits
, _Alloc
>
5686 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5687 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5689 basic_string
<_CharT
, _Traits
, _Alloc
> __str(__lhs
);
5690 __str
.append(__rhs
);
5695 * @brief Concatenate C string and string.
5696 * @param __lhs First string.
5697 * @param __rhs Last string.
5698 * @return New string with value of @a __lhs followed by @a __rhs.
5700 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5701 basic_string
<_CharT
,_Traits
,_Alloc
>
5702 operator+(const _CharT
* __lhs
,
5703 const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
);
5706 * @brief Concatenate character and string.
5707 * @param __lhs First string.
5708 * @param __rhs Last string.
5709 * @return New string with @a __lhs followed by @a __rhs.
5711 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5712 basic_string
<_CharT
,_Traits
,_Alloc
>
5713 operator+(_CharT __lhs
, const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
);
5716 * @brief Concatenate string and C string.
5717 * @param __lhs First string.
5718 * @param __rhs Last string.
5719 * @return New string with @a __lhs followed by @a __rhs.
5721 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5722 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5723 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5724 const _CharT
* __rhs
)
5726 basic_string
<_CharT
, _Traits
, _Alloc
> __str(__lhs
);
5727 __str
.append(__rhs
);
5732 * @brief Concatenate string and character.
5733 * @param __lhs First string.
5734 * @param __rhs Last string.
5735 * @return New string with @a __lhs followed by @a __rhs.
5737 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5738 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5739 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
, _CharT __rhs
)
5741 typedef basic_string
<_CharT
, _Traits
, _Alloc
> __string_type
;
5742 typedef typename
__string_type::size_type __size_type
;
5743 __string_type
__str(__lhs
);
5744 __str
.append(__size_type(1), __rhs
);
5748 #if __cplusplus >= 201103L
5749 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5750 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5751 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
5752 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5753 { return std::move(__lhs
.append(__rhs
)); }
5755 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5756 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5757 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5758 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
5759 { return std::move(__rhs
.insert(0, __lhs
)); }
5761 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5762 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5763 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
5764 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
5766 const auto __size
= __lhs
.size() + __rhs
.size();
5767 const bool __cond
= (__size
> __lhs
.capacity()
5768 && __size
<= __rhs
.capacity());
5769 return __cond
? std::move(__rhs
.insert(0, __lhs
))
5770 : std::move(__lhs
.append(__rhs
));
5773 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5774 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5775 operator+(const _CharT
* __lhs
,
5776 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
5777 { return std::move(__rhs
.insert(0, __lhs
)); }
5779 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5780 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5781 operator+(_CharT __lhs
,
5782 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
5783 { return std::move(__rhs
.insert(0, 1, __lhs
)); }
5785 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5786 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5787 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
5788 const _CharT
* __rhs
)
5789 { return std::move(__lhs
.append(__rhs
)); }
5791 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5792 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5793 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
5795 { return std::move(__lhs
.append(1, __rhs
)); }
5800 * @brief Test equivalence of two strings.
5801 * @param __lhs First string.
5802 * @param __rhs Second string.
5803 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
5805 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5807 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5808 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5810 { return __lhs
.compare(__rhs
) == 0; }
5812 template<typename _CharT
>
5814 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
, bool>::__type
5815 operator==(const basic_string
<_CharT
>& __lhs
,
5816 const basic_string
<_CharT
>& __rhs
) _GLIBCXX_NOEXCEPT
5817 { return (__lhs
.size() == __rhs
.size()
5818 && !std::char_traits
<_CharT
>::compare(__lhs
.data(), __rhs
.data(),
5822 * @brief Test equivalence of C string and string.
5823 * @param __lhs C string.
5824 * @param __rhs String.
5825 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
5827 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5829 operator==(const _CharT
* __lhs
,
5830 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5831 { return __rhs
.compare(__lhs
) == 0; }
5834 * @brief Test equivalence of string and C string.
5835 * @param __lhs String.
5836 * @param __rhs C string.
5837 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
5839 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5841 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5842 const _CharT
* __rhs
)
5843 { return __lhs
.compare(__rhs
) == 0; }
5847 * @brief Test difference of two strings.
5848 * @param __lhs First string.
5849 * @param __rhs Second string.
5850 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
5852 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5854 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5855 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5857 { return !(__lhs
== __rhs
); }
5860 * @brief Test difference of C string and string.
5861 * @param __lhs C string.
5862 * @param __rhs String.
5863 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
5865 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5867 operator!=(const _CharT
* __lhs
,
5868 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5869 { return !(__lhs
== __rhs
); }
5872 * @brief Test difference of string and C string.
5873 * @param __lhs String.
5874 * @param __rhs C string.
5875 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
5877 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5879 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5880 const _CharT
* __rhs
)
5881 { return !(__lhs
== __rhs
); }
5885 * @brief Test if string precedes string.
5886 * @param __lhs First string.
5887 * @param __rhs Second string.
5888 * @return True if @a __lhs precedes @a __rhs. False otherwise.
5890 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5892 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5893 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5895 { return __lhs
.compare(__rhs
) < 0; }
5898 * @brief Test if string precedes C string.
5899 * @param __lhs String.
5900 * @param __rhs C string.
5901 * @return True if @a __lhs precedes @a __rhs. False otherwise.
5903 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5905 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5906 const _CharT
* __rhs
)
5907 { return __lhs
.compare(__rhs
) < 0; }
5910 * @brief Test if C string precedes string.
5911 * @param __lhs C string.
5912 * @param __rhs String.
5913 * @return True if @a __lhs precedes @a __rhs. False otherwise.
5915 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5917 operator<(const _CharT
* __lhs
,
5918 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5919 { return __rhs
.compare(__lhs
) > 0; }
5923 * @brief Test if string follows string.
5924 * @param __lhs First string.
5925 * @param __rhs Second string.
5926 * @return True if @a __lhs follows @a __rhs. False otherwise.
5928 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5930 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5931 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5933 { return __lhs
.compare(__rhs
) > 0; }
5936 * @brief Test if string follows C string.
5937 * @param __lhs String.
5938 * @param __rhs C string.
5939 * @return True if @a __lhs follows @a __rhs. False otherwise.
5941 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5943 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5944 const _CharT
* __rhs
)
5945 { return __lhs
.compare(__rhs
) > 0; }
5948 * @brief Test if C string follows string.
5949 * @param __lhs C string.
5950 * @param __rhs String.
5951 * @return True if @a __lhs follows @a __rhs. False otherwise.
5953 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5955 operator>(const _CharT
* __lhs
,
5956 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5957 { return __rhs
.compare(__lhs
) < 0; }
5961 * @brief Test if string doesn't follow string.
5962 * @param __lhs First string.
5963 * @param __rhs Second string.
5964 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5966 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5968 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5969 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5971 { return __lhs
.compare(__rhs
) <= 0; }
5974 * @brief Test if string doesn't follow C string.
5975 * @param __lhs String.
5976 * @param __rhs C string.
5977 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5979 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5981 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5982 const _CharT
* __rhs
)
5983 { return __lhs
.compare(__rhs
) <= 0; }
5986 * @brief Test if C string doesn't follow string.
5987 * @param __lhs C string.
5988 * @param __rhs String.
5989 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5991 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5993 operator<=(const _CharT
* __lhs
,
5994 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5995 { return __rhs
.compare(__lhs
) >= 0; }
5999 * @brief Test if string doesn't precede string.
6000 * @param __lhs First string.
6001 * @param __rhs Second string.
6002 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6004 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6006 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6007 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6009 { return __lhs
.compare(__rhs
) >= 0; }
6012 * @brief Test if string doesn't precede C string.
6013 * @param __lhs String.
6014 * @param __rhs C string.
6015 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6017 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6019 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6020 const _CharT
* __rhs
)
6021 { return __lhs
.compare(__rhs
) >= 0; }
6024 * @brief Test if C string doesn't precede string.
6025 * @param __lhs C string.
6026 * @param __rhs String.
6027 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6029 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6031 operator>=(const _CharT
* __lhs
,
6032 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6033 { return __rhs
.compare(__lhs
) <= 0; }
6036 * @brief Swap contents of two strings.
6037 * @param __lhs First string.
6038 * @param __rhs Second string.
6040 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
6042 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6044 swap(basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6045 basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6046 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs
.swap(__rhs
)))
6047 { __lhs
.swap(__rhs
); }
6051 * @brief Read stream into a string.
6052 * @param __is Input stream.
6053 * @param __str Buffer to store into.
6054 * @return Reference to the input stream.
6056 * Stores characters from @a __is into @a __str until whitespace is
6057 * found, the end of the stream is encountered, or str.max_size()
6058 * is reached. If is.width() is non-zero, that is the limit on the
6059 * number of characters stored into @a __str. Any previous
6060 * contents of @a __str are erased.
6062 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6063 basic_istream
<_CharT
, _Traits
>&
6064 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
6065 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
);
6068 basic_istream
<char>&
6069 operator>>(basic_istream
<char>& __is
, basic_string
<char>& __str
);
6072 * @brief Write string to a stream.
6073 * @param __os Output stream.
6074 * @param __str String to write out.
6075 * @return Reference to the output stream.
6077 * Output characters of @a __str into os following the same rules as for
6078 * writing a C string.
6080 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6081 inline basic_ostream
<_CharT
, _Traits
>&
6082 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
6083 const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
6085 // _GLIBCXX_RESOLVE_LIB_DEFECTS
6086 // 586. string inserter not a formatted function
6087 return __ostream_insert(__os
, __str
.data(), __str
.size());
6091 * @brief Read a line from stream into a string.
6092 * @param __is Input stream.
6093 * @param __str Buffer to store into.
6094 * @param __delim Character marking end of line.
6095 * @return Reference to the input stream.
6097 * Stores characters from @a __is into @a __str until @a __delim is
6098 * found, the end of the stream is encountered, or str.max_size()
6099 * is reached. Any previous contents of @a __str are erased. If
6100 * @a __delim is encountered, it is extracted but not stored into
6103 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6104 basic_istream
<_CharT
, _Traits
>&
6105 getline(basic_istream
<_CharT
, _Traits
>& __is
,
6106 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
);
6109 * @brief Read a line from stream into a string.
6110 * @param __is Input stream.
6111 * @param __str Buffer to store into.
6112 * @return Reference to the input stream.
6114 * Stores characters from is into @a __str until '\n' is
6115 * found, the end of the stream is encountered, or str.max_size()
6116 * is reached. Any previous contents of @a __str are erased. If
6117 * end of line is encountered, it is extracted but not stored into
6120 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6121 inline basic_istream
<_CharT
, _Traits
>&
6122 getline(basic_istream
<_CharT
, _Traits
>& __is
,
6123 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
6124 { return std::getline(__is
, __str
, __is
.widen('\n')); }
6126 #if __cplusplus >= 201103L
6127 /// Read a line from an rvalue stream into a string.
6128 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6129 inline basic_istream
<_CharT
, _Traits
>&
6130 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
6131 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
)
6132 { return std::getline(__is
, __str
, __delim
); }
6134 /// Read a line from an rvalue stream into a string.
6135 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6136 inline basic_istream
<_CharT
, _Traits
>&
6137 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
6138 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
6139 { return std::getline(__is
, __str
); }
6143 basic_istream
<char>&
6144 getline(basic_istream
<char>& __in
, basic_string
<char>& __str
,
6147 #ifdef _GLIBCXX_USE_WCHAR_T
6149 basic_istream
<wchar_t>&
6150 getline(basic_istream
<wchar_t>& __in
, basic_string
<wchar_t>& __str
,
6154 _GLIBCXX_END_NAMESPACE_VERSION
6157 #if __cplusplus >= 201103L
6159 #include <ext/string_conversions.h>
6161 namespace std
_GLIBCXX_VISIBILITY(default)
6163 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6164 _GLIBCXX_BEGIN_NAMESPACE_CXX11
6166 #if _GLIBCXX_USE_C99_STDLIB
6167 // 21.4 Numeric Conversions [string.conversions].
6169 stoi(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6170 { return __gnu_cxx::__stoa
<long, int>(&std::strtol
, "stoi", __str
.c_str(),
6174 stol(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6175 { return __gnu_cxx::__stoa(&std::strtol
, "stol", __str
.c_str(),
6178 inline unsigned long
6179 stoul(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6180 { return __gnu_cxx::__stoa(&std::strtoul
, "stoul", __str
.c_str(),
6184 stoll(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6185 { return __gnu_cxx::__stoa(&std::strtoll
, "stoll", __str
.c_str(),
6188 inline unsigned long long
6189 stoull(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6190 { return __gnu_cxx::__stoa(&std::strtoull
, "stoull", __str
.c_str(),
6193 // NB: strtof vs strtod.
6195 stof(const string
& __str
, size_t* __idx
= 0)
6196 { return __gnu_cxx::__stoa(&std::strtof
, "stof", __str
.c_str(), __idx
); }
6199 stod(const string
& __str
, size_t* __idx
= 0)
6200 { return __gnu_cxx::__stoa(&std::strtod
, "stod", __str
.c_str(), __idx
); }
6203 stold(const string
& __str
, size_t* __idx
= 0)
6204 { return __gnu_cxx::__stoa(&std::strtold
, "stold", __str
.c_str(), __idx
); }
6205 #endif // _GLIBCXX_USE_C99_STDLIB
6207 #if _GLIBCXX_USE_C99_STDIO
6208 // NB: (v)snprintf vs sprintf.
6212 to_string(int __val
)
6213 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, 4 * sizeof(int),
6217 to_string(unsigned __val
)
6218 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
6219 4 * sizeof(unsigned),
6223 to_string(long __val
)
6224 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, 4 * sizeof(long),
6228 to_string(unsigned long __val
)
6229 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
6230 4 * sizeof(unsigned long),
6234 to_string(long long __val
)
6235 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
6236 4 * sizeof(long long),
6240 to_string(unsigned long long __val
)
6241 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
6242 4 * sizeof(unsigned long long),
6246 to_string(float __val
)
6249 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
6250 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
6255 to_string(double __val
)
6258 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
6259 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
6264 to_string(long double __val
)
6267 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
6268 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
6271 #endif // _GLIBCXX_USE_C99_STDIO
6273 #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
6275 stoi(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6276 { return __gnu_cxx::__stoa
<long, int>(&std::wcstol
, "stoi", __str
.c_str(),
6280 stol(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6281 { return __gnu_cxx::__stoa(&std::wcstol
, "stol", __str
.c_str(),
6284 inline unsigned long
6285 stoul(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6286 { return __gnu_cxx::__stoa(&std::wcstoul
, "stoul", __str
.c_str(),
6290 stoll(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6291 { return __gnu_cxx::__stoa(&std::wcstoll
, "stoll", __str
.c_str(),
6294 inline unsigned long long
6295 stoull(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6296 { return __gnu_cxx::__stoa(&std::wcstoull
, "stoull", __str
.c_str(),
6299 // NB: wcstof vs wcstod.
6301 stof(const wstring
& __str
, size_t* __idx
= 0)
6302 { return __gnu_cxx::__stoa(&std::wcstof
, "stof", __str
.c_str(), __idx
); }
6305 stod(const wstring
& __str
, size_t* __idx
= 0)
6306 { return __gnu_cxx::__stoa(&std::wcstod
, "stod", __str
.c_str(), __idx
); }
6309 stold(const wstring
& __str
, size_t* __idx
= 0)
6310 { return __gnu_cxx::__stoa(&std::wcstold
, "stold", __str
.c_str(), __idx
); }
6312 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6315 to_wstring(int __val
)
6316 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(int),
6320 to_wstring(unsigned __val
)
6321 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
6322 4 * sizeof(unsigned),
6326 to_wstring(long __val
)
6327 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(long),
6331 to_wstring(unsigned long __val
)
6332 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
6333 4 * sizeof(unsigned long),
6337 to_wstring(long long __val
)
6338 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
6339 4 * sizeof(long long),
6343 to_wstring(unsigned long long __val
)
6344 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
6345 4 * sizeof(unsigned long long),
6349 to_wstring(float __val
)
6352 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
6353 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
6358 to_wstring(double __val
)
6361 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
6362 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
6367 to_wstring(long double __val
)
6370 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
6371 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
6374 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6375 #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
6377 _GLIBCXX_END_NAMESPACE_CXX11
6378 _GLIBCXX_END_NAMESPACE_VERSION
6383 #if __cplusplus >= 201103L
6385 #include <bits/functional_hash.h>
6387 namespace std
_GLIBCXX_VISIBILITY(default)
6389 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6393 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
6394 /// std::hash specialization for string.
6397 : public __hash_base
<size_t, string
>
6400 operator()(const string
& __s
) const noexcept
6401 { return std::_Hash_impl::hash(__s
.data(), __s
.length()); }
6405 struct __is_fast_hash
<hash
<string
>> : std::false_type
6408 #ifdef _GLIBCXX_USE_WCHAR_T
6409 /// std::hash specialization for wstring.
6411 struct hash
<wstring
>
6412 : public __hash_base
<size_t, wstring
>
6415 operator()(const wstring
& __s
) const noexcept
6416 { return std::_Hash_impl::hash(__s
.data(),
6417 __s
.length() * sizeof(wchar_t)); }
6421 struct __is_fast_hash
<hash
<wstring
>> : std::false_type
6424 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
6426 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
6427 /// std::hash specialization for u16string.
6429 struct hash
<u16string
>
6430 : public __hash_base
<size_t, u16string
>
6433 operator()(const u16string
& __s
) const noexcept
6434 { return std::_Hash_impl::hash(__s
.data(),
6435 __s
.length() * sizeof(char16_t
)); }
6439 struct __is_fast_hash
<hash
<u16string
>> : std::false_type
6442 /// std::hash specialization for u32string.
6444 struct hash
<u32string
>
6445 : public __hash_base
<size_t, u32string
>
6448 operator()(const u32string
& __s
) const noexcept
6449 { return std::_Hash_impl::hash(__s
.data(),
6450 __s
.length() * sizeof(char32_t
)); }
6454 struct __is_fast_hash
<hash
<u32string
>> : std::false_type
6458 _GLIBCXX_END_NAMESPACE_VERSION
6460 #if __cplusplus > 201103L
6462 #define __cpp_lib_string_udls 201304
6464 inline namespace literals
6466 inline namespace string_literals
6468 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6470 _GLIBCXX_DEFAULT_ABI_TAG
6471 inline basic_string
<char>
6472 operator""s(const char* __str
, size_t __len
)
6473 { return basic_string
<char>{__str
, __len
}; }
6475 #ifdef _GLIBCXX_USE_WCHAR_T
6476 _GLIBCXX_DEFAULT_ABI_TAG
6477 inline basic_string
<wchar_t>
6478 operator""s(const wchar_t* __str
, size_t __len
)
6479 { return basic_string
<wchar_t>{__str
, __len
}; }
6482 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
6483 _GLIBCXX_DEFAULT_ABI_TAG
6484 inline basic_string
<char16_t
>
6485 operator""s(const char16_t
* __str
, size_t __len
)
6486 { return basic_string
<char16_t
>{__str
, __len
}; }
6488 _GLIBCXX_DEFAULT_ABI_TAG
6489 inline basic_string
<char32_t
>
6490 operator""s(const char32_t
* __str
, size_t __len
)
6491 { return basic_string
<char32_t
>{__str
, __len
}; }
6494 _GLIBCXX_END_NAMESPACE_VERSION
6495 } // inline namespace string_literals
6496 } // inline namespace literals
6498 #endif // __cplusplus > 201103L
6504 #endif /* _BASIC_STRING_H */