1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997-2018 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 basic_string
*>>,
119 __not_
<is_convertible
<const _Tp
&, const _CharT
*>>>::value
,
122 // Allows an implicit conversion to __sv_type.
124 _S_to_string_view(__sv_type __svt
) noexcept
127 // Wraps a string_view by explicit conversion and thus
128 // allows to add an internal constructor that does not
129 // participate in overload resolution when a string_view
133 explicit __sv_wrapper(__sv_type __sv
) noexcept
: _M_sv(__sv
) { }
138 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
139 struct _Alloc_hider
: allocator_type
// TODO check __is_final
141 #if __cplusplus < 201103L
142 _Alloc_hider(pointer __dat
, const _Alloc
& __a
= _Alloc())
143 : allocator_type(__a
), _M_p(__dat
) { }
145 _Alloc_hider(pointer __dat
, const _Alloc
& __a
)
146 : allocator_type(__a
), _M_p(__dat
) { }
148 _Alloc_hider(pointer __dat
, _Alloc
&& __a
= _Alloc())
149 : allocator_type(std::move(__a
)), _M_p(__dat
) { }
152 pointer _M_p
; // The actual data.
155 _Alloc_hider _M_dataplus
;
156 size_type _M_string_length
;
158 enum { _S_local_capacity
= 15 / sizeof(_CharT
) };
162 _CharT _M_local_buf
[_S_local_capacity
+ 1];
163 size_type _M_allocated_capacity
;
168 { _M_dataplus
._M_p
= __p
; }
171 _M_length(size_type __length
)
172 { _M_string_length
= __length
; }
176 { return _M_dataplus
._M_p
; }
181 #if __cplusplus >= 201103L
182 return std::pointer_traits
<pointer
>::pointer_to(*_M_local_buf
);
184 return pointer(_M_local_buf
);
189 _M_local_data() const
191 #if __cplusplus >= 201103L
192 return std::pointer_traits
<const_pointer
>::pointer_to(*_M_local_buf
);
194 return const_pointer(_M_local_buf
);
199 _M_capacity(size_type __capacity
)
200 { _M_allocated_capacity
= __capacity
; }
203 _M_set_length(size_type __n
)
206 traits_type::assign(_M_data()[__n
], _CharT());
211 { return _M_data() == _M_local_data(); }
215 _M_create(size_type
&, size_type
);
221 _M_destroy(_M_allocated_capacity
);
225 _M_destroy(size_type __size
) throw()
226 { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size
+ 1); }
228 // _M_construct_aux is used to implement the 21.3.1 para 15 which
229 // requires special behaviour if _InIterator is an integral type
230 template<typename _InIterator
>
232 _M_construct_aux(_InIterator __beg
, _InIterator __end
,
235 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
236 _M_construct(__beg
, __end
, _Tag());
239 // _GLIBCXX_RESOLVE_LIB_DEFECTS
240 // 438. Ambiguity in the "do the right thing" clause
241 template<typename _Integer
>
243 _M_construct_aux(_Integer __beg
, _Integer __end
, std::__true_type
)
244 { _M_construct_aux_2(static_cast<size_type
>(__beg
), __end
); }
247 _M_construct_aux_2(size_type __req
, _CharT __c
)
248 { _M_construct(__req
, __c
); }
250 template<typename _InIterator
>
252 _M_construct(_InIterator __beg
, _InIterator __end
)
254 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
255 _M_construct_aux(__beg
, __end
, _Integral());
258 // For Input Iterators, used in istreambuf_iterators, etc.
259 template<typename _InIterator
>
261 _M_construct(_InIterator __beg
, _InIterator __end
,
262 std::input_iterator_tag
);
264 // For forward_iterators up to random_access_iterators, used for
265 // string::iterator, _CharT*, etc.
266 template<typename _FwdIterator
>
268 _M_construct(_FwdIterator __beg
, _FwdIterator __end
,
269 std::forward_iterator_tag
);
272 _M_construct(size_type __req
, _CharT __c
);
276 { return _M_dataplus
; }
278 const allocator_type
&
279 _M_get_allocator() const
280 { return _M_dataplus
; }
284 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
285 // The explicit instantiations in misc-inst.cc require this due to
286 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
287 template<typename _Tp
, bool _Requires
=
288 !__are_same
<_Tp
, _CharT
*>::__value
289 && !__are_same
<_Tp
, const _CharT
*>::__value
290 && !__are_same
<_Tp
, iterator
>::__value
291 && !__are_same
<_Tp
, const_iterator
>::__value
>
292 struct __enable_if_not_native_iterator
293 { typedef basic_string
& __type
; };
294 template<typename _Tp
>
295 struct __enable_if_not_native_iterator
<_Tp
, false> { };
299 _M_check(size_type __pos
, const char* __s
) const
301 if (__pos
> this->size())
302 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
303 "this->size() (which is %zu)"),
304 __s
, __pos
, this->size());
309 _M_check_length(size_type __n1
, size_type __n2
, const char* __s
) const
311 if (this->max_size() - (this->size() - __n1
) < __n2
)
312 __throw_length_error(__N(__s
));
316 // NB: _M_limit doesn't check for a bad __pos value.
318 _M_limit(size_type __pos
, size_type __off
) const _GLIBCXX_NOEXCEPT
320 const bool __testoff
= __off
< this->size() - __pos
;
321 return __testoff
? __off
: this->size() - __pos
;
324 // True if _Rep and source do not overlap.
326 _M_disjunct(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
328 return (less
<const _CharT
*>()(__s
, _M_data())
329 || less
<const _CharT
*>()(_M_data() + this->size(), __s
));
332 // When __n = 1 way faster than the general multichar
333 // traits_type::copy/move/assign.
335 _S_copy(_CharT
* __d
, const _CharT
* __s
, size_type __n
)
338 traits_type::assign(*__d
, *__s
);
340 traits_type::copy(__d
, __s
, __n
);
344 _S_move(_CharT
* __d
, const _CharT
* __s
, size_type __n
)
347 traits_type::assign(*__d
, *__s
);
349 traits_type::move(__d
, __s
, __n
);
353 _S_assign(_CharT
* __d
, size_type __n
, _CharT __c
)
356 traits_type::assign(*__d
, __c
);
358 traits_type::assign(__d
, __n
, __c
);
361 // _S_copy_chars is a separate template to permit specialization
362 // to optimize for the common case of pointers as iterators.
363 template<class _Iterator
>
365 _S_copy_chars(_CharT
* __p
, _Iterator __k1
, _Iterator __k2
)
367 for (; __k1
!= __k2
; ++__k1
, (void)++__p
)
368 traits_type::assign(*__p
, *__k1
); // These types are off.
372 _S_copy_chars(_CharT
* __p
, iterator __k1
, iterator __k2
) _GLIBCXX_NOEXCEPT
373 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
376 _S_copy_chars(_CharT
* __p
, const_iterator __k1
, const_iterator __k2
)
378 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
381 _S_copy_chars(_CharT
* __p
, _CharT
* __k1
, _CharT
* __k2
) _GLIBCXX_NOEXCEPT
382 { _S_copy(__p
, __k1
, __k2
- __k1
); }
385 _S_copy_chars(_CharT
* __p
, const _CharT
* __k1
, const _CharT
* __k2
)
387 { _S_copy(__p
, __k1
, __k2
- __k1
); }
390 _S_compare(size_type __n1
, size_type __n2
) _GLIBCXX_NOEXCEPT
392 const difference_type __d
= difference_type(__n1
- __n2
);
394 if (__d
> __gnu_cxx::__numeric_traits
<int>::__max
)
395 return __gnu_cxx::__numeric_traits
<int>::__max
;
396 else if (__d
< __gnu_cxx::__numeric_traits
<int>::__min
)
397 return __gnu_cxx::__numeric_traits
<int>::__min
;
403 _M_assign(const basic_string
&);
406 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
410 _M_erase(size_type __pos
, size_type __n
);
413 // Construct/copy/destroy:
414 // NB: We overload ctors in some cases instead of using default
415 // arguments, per 17.4.4.4 para. 2 item 2.
418 * @brief Default constructor creates an empty string.
421 _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible
<_Alloc
>::value
)
422 : _M_dataplus(_M_local_data())
423 { _M_set_length(0); }
426 * @brief Construct an empty string using allocator @a a.
429 basic_string(const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
430 : _M_dataplus(_M_local_data(), __a
)
431 { _M_set_length(0); }
434 * @brief Construct string with copy of value of @a __str.
435 * @param __str Source string.
437 basic_string(const basic_string
& __str
)
438 : _M_dataplus(_M_local_data(),
439 _Alloc_traits::_S_select_on_copy(__str
._M_get_allocator()))
440 { _M_construct(__str
._M_data(), __str
._M_data() + __str
.length()); }
442 // _GLIBCXX_RESOLVE_LIB_DEFECTS
443 // 2583. no way to supply an allocator for basic_string(str, pos)
445 * @brief Construct string as copy of a substring.
446 * @param __str Source string.
447 * @param __pos Index of first character to copy from.
448 * @param __a Allocator to use.
450 basic_string(const basic_string
& __str
, size_type __pos
,
451 const _Alloc
& __a
= _Alloc())
452 : _M_dataplus(_M_local_data(), __a
)
454 const _CharT
* __start
= __str
._M_data()
455 + __str
._M_check(__pos
, "basic_string::basic_string");
456 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, npos
));
460 * @brief Construct string as copy of a substring.
461 * @param __str Source string.
462 * @param __pos Index of first character to copy from.
463 * @param __n Number of characters to copy.
465 basic_string(const basic_string
& __str
, size_type __pos
,
467 : _M_dataplus(_M_local_data())
469 const _CharT
* __start
= __str
._M_data()
470 + __str
._M_check(__pos
, "basic_string::basic_string");
471 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, __n
));
475 * @brief Construct string as copy of a substring.
476 * @param __str Source string.
477 * @param __pos Index of first character to copy from.
478 * @param __n Number of characters to copy.
479 * @param __a Allocator to use.
481 basic_string(const basic_string
& __str
, size_type __pos
,
482 size_type __n
, const _Alloc
& __a
)
483 : _M_dataplus(_M_local_data(), __a
)
485 const _CharT
* __start
486 = __str
._M_data() + __str
._M_check(__pos
, "string::string");
487 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, __n
));
491 * @brief Construct string initialized by a character %array.
492 * @param __s Source character %array.
493 * @param __n Number of characters to copy.
494 * @param __a Allocator to use (default is default allocator).
496 * NB: @a __s must have at least @a __n characters, '\\0'
497 * has no special meaning.
499 basic_string(const _CharT
* __s
, size_type __n
,
500 const _Alloc
& __a
= _Alloc())
501 : _M_dataplus(_M_local_data(), __a
)
502 { _M_construct(__s
, __s
+ __n
); }
505 * @brief Construct string as copy of a C string.
506 * @param __s Source C string.
507 * @param __a Allocator to use (default is default allocator).
509 basic_string(const _CharT
* __s
, const _Alloc
& __a
= _Alloc())
510 : _M_dataplus(_M_local_data(), __a
)
511 { _M_construct(__s
, __s
? __s
+ traits_type::length(__s
) : __s
+npos
); }
514 * @brief Construct string as multiple characters.
515 * @param __n Number of characters.
516 * @param __c Character to use.
517 * @param __a Allocator to use (default is default allocator).
519 basic_string(size_type __n
, _CharT __c
, const _Alloc
& __a
= _Alloc())
520 : _M_dataplus(_M_local_data(), __a
)
521 { _M_construct(__n
, __c
); }
523 #if __cplusplus >= 201103L
525 * @brief Move construct string.
526 * @param __str Source string.
528 * The newly-created string contains the exact contents of @a __str.
529 * @a __str is a valid, but unspecified string.
531 basic_string(basic_string
&& __str
) noexcept
532 : _M_dataplus(_M_local_data(), std::move(__str
._M_get_allocator()))
534 if (__str
._M_is_local())
536 traits_type::copy(_M_local_buf
, __str
._M_local_buf
,
537 _S_local_capacity
+ 1);
541 _M_data(__str
._M_data());
542 _M_capacity(__str
._M_allocated_capacity
);
545 // Must use _M_length() here not _M_set_length() because
546 // basic_stringbuf relies on writing into unallocated capacity so
547 // we mess up the contents if we put a '\0' in the string.
548 _M_length(__str
.length());
549 __str
._M_data(__str
._M_local_data());
550 __str
._M_set_length(0);
554 * @brief Construct string from an initializer %list.
555 * @param __l std::initializer_list of characters.
556 * @param __a Allocator to use (default is default allocator).
558 basic_string(initializer_list
<_CharT
> __l
, const _Alloc
& __a
= _Alloc())
559 : _M_dataplus(_M_local_data(), __a
)
560 { _M_construct(__l
.begin(), __l
.end()); }
562 basic_string(const basic_string
& __str
, const _Alloc
& __a
)
563 : _M_dataplus(_M_local_data(), __a
)
564 { _M_construct(__str
.begin(), __str
.end()); }
566 basic_string(basic_string
&& __str
, const _Alloc
& __a
)
567 noexcept(_Alloc_traits::_S_always_equal())
568 : _M_dataplus(_M_local_data(), __a
)
570 if (__str
._M_is_local())
572 traits_type::copy(_M_local_buf
, __str
._M_local_buf
,
573 _S_local_capacity
+ 1);
574 _M_length(__str
.length());
575 __str
._M_set_length(0);
577 else if (_Alloc_traits::_S_always_equal()
578 || __str
.get_allocator() == __a
)
580 _M_data(__str
._M_data());
581 _M_length(__str
.length());
582 _M_capacity(__str
._M_allocated_capacity
);
583 __str
._M_data(__str
._M_local_buf
);
584 __str
._M_set_length(0);
587 _M_construct(__str
.begin(), __str
.end());
593 * @brief Construct string as copy of a range.
594 * @param __beg Start of range.
595 * @param __end End of range.
596 * @param __a Allocator to use (default is default allocator).
598 #if __cplusplus >= 201103L
599 template<typename _InputIterator
,
600 typename
= std::_RequireInputIter
<_InputIterator
>>
602 template<typename _InputIterator
>
604 basic_string(_InputIterator __beg
, _InputIterator __end
,
605 const _Alloc
& __a
= _Alloc())
606 : _M_dataplus(_M_local_data(), __a
)
607 { _M_construct(__beg
, __end
); }
609 #if __cplusplus > 201402L
611 * @brief Construct string from a substring of a string_view.
612 * @param __t Source object convertible to string view.
613 * @param __pos The index of the first character to copy from __t.
614 * @param __n The number of characters to copy from __t.
615 * @param __a Allocator to use.
617 template<typename _Tp
, typename
= _If_sv
<_Tp
, void>>
618 basic_string(const _Tp
& __t
, size_type __pos
, size_type __n
,
619 const _Alloc
& __a
= _Alloc())
620 : basic_string(_S_to_string_view(__t
).substr(__pos
, __n
), __a
) { }
623 * @brief Construct string from a string_view.
624 * @param __t Source object convertible to string view.
625 * @param __a Allocator to use (default is default allocator).
627 template<typename _Tp
, typename
= _If_sv
<_Tp
, void>>
629 basic_string(const _Tp
& __t
, const _Alloc
& __a
= _Alloc())
630 : basic_string(__sv_wrapper(_S_to_string_view(__t
)), __a
) { }
633 * @brief Only internally used: Construct string from a string view
635 * @param __svw string view wrapper.
636 * @param __a Allocator to use.
639 basic_string(__sv_wrapper __svw
, const _Alloc
& __a
)
640 : basic_string(__svw
._M_sv
.data(), __svw
._M_sv
.size(), __a
) { }
644 * @brief Destroy the string instance.
650 * @brief Assign the value of @a str to this string.
651 * @param __str Source string.
654 operator=(const basic_string
& __str
)
656 #if __cplusplus >= 201103L
657 if (_Alloc_traits::_S_propagate_on_copy_assign())
659 if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
660 && _M_get_allocator() != __str
._M_get_allocator())
662 // Propagating allocator cannot free existing storage so must
663 // deallocate it before replacing current allocator.
664 if (__str
.size() <= _S_local_capacity
)
666 _M_destroy(_M_allocated_capacity
);
667 _M_data(_M_local_data());
672 const auto __len
= __str
.size();
673 auto __alloc
= __str
._M_get_allocator();
674 // If this allocation throws there are no effects:
675 auto __ptr
= _Alloc_traits::allocate(__alloc
, __len
+ 1);
676 _M_destroy(_M_allocated_capacity
);
679 _M_set_length(__len
);
682 std::__alloc_on_copy(_M_get_allocator(), __str
._M_get_allocator());
685 return this->assign(__str
);
689 * @brief Copy contents of @a s into this string.
690 * @param __s Source null-terminated string.
693 operator=(const _CharT
* __s
)
694 { return this->assign(__s
); }
697 * @brief Set value to string of length 1.
698 * @param __c Source character.
700 * Assigning to a character makes this string length 1 and
701 * (*this)[0] == @a c.
704 operator=(_CharT __c
)
706 this->assign(1, __c
);
710 #if __cplusplus >= 201103L
712 * @brief Move assign the value of @a str to this string.
713 * @param __str Source string.
715 * The contents of @a str are moved into this string (without copying).
716 * @a str is a valid, but unspecified string.
718 // PR 58265, this should be noexcept.
719 // _GLIBCXX_RESOLVE_LIB_DEFECTS
720 // 2063. Contradictory requirements for string move assignment
722 operator=(basic_string
&& __str
)
723 noexcept(_Alloc_traits::_S_nothrow_move())
725 if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
726 && !_Alloc_traits::_S_always_equal()
727 && _M_get_allocator() != __str
._M_get_allocator())
729 // Destroy existing storage before replacing allocator.
730 _M_destroy(_M_allocated_capacity
);
731 _M_data(_M_local_data());
734 // Replace allocator if POCMA is true.
735 std::__alloc_on_move(_M_get_allocator(), __str
._M_get_allocator());
737 if (!__str
._M_is_local()
738 && (_Alloc_traits::_S_propagate_on_move_assign()
739 || _Alloc_traits::_S_always_equal()))
741 pointer __data
= nullptr;
742 size_type __capacity
;
745 if (_Alloc_traits::_S_always_equal())
748 __capacity
= _M_allocated_capacity
;
751 _M_destroy(_M_allocated_capacity
);
754 _M_data(__str
._M_data());
755 _M_length(__str
.length());
756 _M_capacity(__str
._M_allocated_capacity
);
759 __str
._M_data(__data
);
760 __str
._M_capacity(__capacity
);
763 __str
._M_data(__str
._M_local_buf
);
772 * @brief Set value to string constructed from initializer %list.
773 * @param __l std::initializer_list.
776 operator=(initializer_list
<_CharT
> __l
)
778 this->assign(__l
.begin(), __l
.size());
783 #if __cplusplus > 201402L
785 * @brief Set value to string constructed from a string_view.
786 * @param __svt An object convertible to string_view.
788 template<typename _Tp
>
789 _If_sv
<_Tp
, basic_string
&>
790 operator=(const _Tp
& __svt
)
791 { return this->assign(__svt
); }
794 * @brief Convert to a string_view.
795 * @return A string_view.
797 operator __sv_type() const noexcept
798 { return __sv_type(data(), size()); }
803 * Returns a read/write iterator that points to the first character in
807 begin() _GLIBCXX_NOEXCEPT
808 { return iterator(_M_data()); }
811 * Returns a read-only (constant) iterator that points to the first
812 * character in the %string.
815 begin() const _GLIBCXX_NOEXCEPT
816 { return const_iterator(_M_data()); }
819 * Returns a read/write iterator that points one past the last
820 * character in the %string.
823 end() _GLIBCXX_NOEXCEPT
824 { return iterator(_M_data() + this->size()); }
827 * Returns a read-only (constant) iterator that points one past the
828 * last character in the %string.
831 end() const _GLIBCXX_NOEXCEPT
832 { return const_iterator(_M_data() + this->size()); }
835 * Returns a read/write reverse iterator that points to the last
836 * character in the %string. Iteration is done in reverse element
840 rbegin() _GLIBCXX_NOEXCEPT
841 { return reverse_iterator(this->end()); }
844 * Returns a read-only (constant) reverse iterator that points
845 * to the last character in the %string. Iteration is done in
846 * reverse element order.
848 const_reverse_iterator
849 rbegin() const _GLIBCXX_NOEXCEPT
850 { return const_reverse_iterator(this->end()); }
853 * Returns a read/write reverse iterator that points to one before the
854 * first character in the %string. Iteration is done in reverse
858 rend() _GLIBCXX_NOEXCEPT
859 { return reverse_iterator(this->begin()); }
862 * Returns a read-only (constant) reverse iterator that points
863 * to one before the first character in the %string. Iteration
864 * is done in reverse element order.
866 const_reverse_iterator
867 rend() const _GLIBCXX_NOEXCEPT
868 { return const_reverse_iterator(this->begin()); }
870 #if __cplusplus >= 201103L
872 * Returns a read-only (constant) iterator that points to the first
873 * character in the %string.
876 cbegin() const noexcept
877 { return const_iterator(this->_M_data()); }
880 * Returns a read-only (constant) iterator that points one past the
881 * last character in the %string.
884 cend() const noexcept
885 { return const_iterator(this->_M_data() + this->size()); }
888 * Returns a read-only (constant) reverse iterator that points
889 * to the last character in the %string. Iteration is done in
890 * reverse element order.
892 const_reverse_iterator
893 crbegin() const noexcept
894 { return const_reverse_iterator(this->end()); }
897 * Returns a read-only (constant) reverse iterator that points
898 * to one before the first character in the %string. Iteration
899 * is done in reverse element order.
901 const_reverse_iterator
902 crend() const noexcept
903 { return const_reverse_iterator(this->begin()); }
908 /// Returns the number of characters in the string, not including any
909 /// null-termination.
911 size() const _GLIBCXX_NOEXCEPT
912 { return _M_string_length
; }
914 /// Returns the number of characters in the string, not including any
915 /// null-termination.
917 length() const _GLIBCXX_NOEXCEPT
918 { return _M_string_length
; }
920 /// Returns the size() of the largest possible %string.
922 max_size() const _GLIBCXX_NOEXCEPT
923 { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
926 * @brief Resizes the %string to the specified number of characters.
927 * @param __n Number of characters the %string should contain.
928 * @param __c Character to fill any new elements.
930 * This function will %resize the %string to the specified
931 * number of characters. If the number is smaller than the
932 * %string's current size the %string is truncated, otherwise
933 * the %string is extended and new elements are %set to @a __c.
936 resize(size_type __n
, _CharT __c
);
939 * @brief Resizes the %string to the specified number of characters.
940 * @param __n Number of characters the %string should contain.
942 * This function will resize the %string to the specified length. If
943 * the new size is smaller than the %string's current size the %string
944 * is truncated, otherwise the %string is extended and new characters
945 * are default-constructed. For basic types such as char, this means
949 resize(size_type __n
)
950 { this->resize(__n
, _CharT()); }
952 #if __cplusplus >= 201103L
953 /// A non-binding request to reduce capacity() to size().
955 shrink_to_fit() noexcept
958 if (capacity() > size())
970 * Returns the total number of characters that the %string can hold
971 * before needing to allocate more memory.
974 capacity() const _GLIBCXX_NOEXCEPT
976 return _M_is_local() ? size_type(_S_local_capacity
)
977 : _M_allocated_capacity
;
981 * @brief Attempt to preallocate enough memory for specified number of
983 * @param __res_arg Number of characters required.
984 * @throw std::length_error If @a __res_arg exceeds @c max_size().
986 * This function attempts to reserve enough memory for the
987 * %string to hold the specified number of characters. If the
988 * number requested is more than max_size(), length_error is
991 * The advantage of this function is that if optimal code is a
992 * necessity and the user can determine the string length that will be
993 * required, the user can reserve the memory in %advance, and thus
994 * prevent a possible reallocation of memory and copying of %string
998 reserve(size_type __res_arg
= 0);
1001 * Erases the string, making it empty.
1004 clear() _GLIBCXX_NOEXCEPT
1005 { _M_set_length(0); }
1008 * Returns true if the %string is empty. Equivalent to
1009 * <code>*this == ""</code>.
1012 empty() const _GLIBCXX_NOEXCEPT
1013 { return this->size() == 0; }
1017 * @brief Subscript access to the data contained in the %string.
1018 * @param __pos The index of the character to access.
1019 * @return Read-only (constant) reference to the character.
1021 * This operator allows for easy, array-style, data access.
1022 * Note that data access with this operator is unchecked and
1023 * out_of_range lookups are not defined. (For checked lookups
1027 operator[] (size_type __pos
) const _GLIBCXX_NOEXCEPT
1029 __glibcxx_assert(__pos
<= size());
1030 return _M_data()[__pos
];
1034 * @brief Subscript access to the data contained in the %string.
1035 * @param __pos The index of the character to access.
1036 * @return Read/write reference to the character.
1038 * This operator allows for easy, array-style, data access.
1039 * Note that data access with this operator is unchecked and
1040 * out_of_range lookups are not defined. (For checked lookups
1044 operator[](size_type __pos
)
1046 // Allow pos == size() both in C++98 mode, as v3 extension,
1047 // and in C++11 mode.
1048 __glibcxx_assert(__pos
<= size());
1049 // In pedantic mode be strict in C++98 mode.
1050 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus
>= 201103L || __pos
< size());
1051 return _M_data()[__pos
];
1055 * @brief Provides access to the data contained in the %string.
1056 * @param __n The index of the character to access.
1057 * @return Read-only (const) reference to the character.
1058 * @throw std::out_of_range If @a n is an invalid index.
1060 * This function provides for safer data access. The parameter is
1061 * first checked that it is in the range of the string. The function
1062 * throws out_of_range if the check fails.
1065 at(size_type __n
) const
1067 if (__n
>= this->size())
1068 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1069 "(which is %zu) >= this->size() "
1072 return _M_data()[__n
];
1076 * @brief Provides access to the data contained in the %string.
1077 * @param __n The index of the character to access.
1078 * @return Read/write reference to the character.
1079 * @throw std::out_of_range If @a n is an invalid index.
1081 * This function provides for safer data access. The parameter is
1082 * first checked that it is in the range of the string. The function
1083 * throws out_of_range if the check fails.
1089 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1090 "(which is %zu) >= this->size() "
1093 return _M_data()[__n
];
1096 #if __cplusplus >= 201103L
1098 * Returns a read/write reference to the data at the first
1099 * element of the %string.
1104 __glibcxx_assert(!empty());
1105 return operator[](0);
1109 * Returns a read-only (constant) reference to the data at the first
1110 * element of the %string.
1113 front() const noexcept
1115 __glibcxx_assert(!empty());
1116 return operator[](0);
1120 * Returns a read/write reference to the data at the last
1121 * element of the %string.
1126 __glibcxx_assert(!empty());
1127 return operator[](this->size() - 1);
1131 * Returns a read-only (constant) reference to the data at the
1132 * last element of the %string.
1135 back() const noexcept
1137 __glibcxx_assert(!empty());
1138 return operator[](this->size() - 1);
1144 * @brief Append a string to this string.
1145 * @param __str The string to append.
1146 * @return Reference to this string.
1149 operator+=(const basic_string
& __str
)
1150 { return this->append(__str
); }
1153 * @brief Append a C string.
1154 * @param __s The C string to append.
1155 * @return Reference to this string.
1158 operator+=(const _CharT
* __s
)
1159 { return this->append(__s
); }
1162 * @brief Append a character.
1163 * @param __c The character to append.
1164 * @return Reference to this string.
1167 operator+=(_CharT __c
)
1169 this->push_back(__c
);
1173 #if __cplusplus >= 201103L
1175 * @brief Append an initializer_list of characters.
1176 * @param __l The initializer_list of characters to be appended.
1177 * @return Reference to this string.
1180 operator+=(initializer_list
<_CharT
> __l
)
1181 { return this->append(__l
.begin(), __l
.size()); }
1184 #if __cplusplus > 201402L
1186 * @brief Append a string_view.
1187 * @param __svt An object convertible to string_view to be appended.
1188 * @return Reference to this string.
1190 template<typename _Tp
>
1191 _If_sv
<_Tp
, basic_string
&>
1192 operator+=(const _Tp
& __svt
)
1193 { return this->append(__svt
); }
1197 * @brief Append a string to this string.
1198 * @param __str The string to append.
1199 * @return Reference to this string.
1202 append(const basic_string
& __str
)
1203 { return _M_append(__str
._M_data(), __str
.size()); }
1206 * @brief Append a substring.
1207 * @param __str The string to append.
1208 * @param __pos Index of the first character of str to append.
1209 * @param __n The number of characters to append.
1210 * @return Reference to this string.
1211 * @throw std::out_of_range if @a __pos is not a valid index.
1213 * This function appends @a __n characters from @a __str
1214 * starting at @a __pos to this string. If @a __n is is larger
1215 * than the number of available characters in @a __str, the
1216 * remainder of @a __str is appended.
1219 append(const basic_string
& __str
, size_type __pos
, size_type __n
)
1220 { return _M_append(__str
._M_data()
1221 + __str
._M_check(__pos
, "basic_string::append"),
1222 __str
._M_limit(__pos
, __n
)); }
1225 * @brief Append a C substring.
1226 * @param __s The C string to append.
1227 * @param __n The number of characters to append.
1228 * @return Reference to this string.
1231 append(const _CharT
* __s
, size_type __n
)
1233 __glibcxx_requires_string_len(__s
, __n
);
1234 _M_check_length(size_type(0), __n
, "basic_string::append");
1235 return _M_append(__s
, __n
);
1239 * @brief Append a C string.
1240 * @param __s The C string to append.
1241 * @return Reference to this string.
1244 append(const _CharT
* __s
)
1246 __glibcxx_requires_string(__s
);
1247 const size_type __n
= traits_type::length(__s
);
1248 _M_check_length(size_type(0), __n
, "basic_string::append");
1249 return _M_append(__s
, __n
);
1253 * @brief Append multiple characters.
1254 * @param __n The number of characters to append.
1255 * @param __c The character to use.
1256 * @return Reference to this string.
1258 * Appends __n copies of __c to this string.
1261 append(size_type __n
, _CharT __c
)
1262 { return _M_replace_aux(this->size(), size_type(0), __n
, __c
); }
1264 #if __cplusplus >= 201103L
1266 * @brief Append an initializer_list of characters.
1267 * @param __l The initializer_list of characters to append.
1268 * @return Reference to this string.
1271 append(initializer_list
<_CharT
> __l
)
1272 { return this->append(__l
.begin(), __l
.size()); }
1276 * @brief Append a range of characters.
1277 * @param __first Iterator referencing the first character to append.
1278 * @param __last Iterator marking the end of the range.
1279 * @return Reference to this string.
1281 * Appends characters in the range [__first,__last) to this string.
1283 #if __cplusplus >= 201103L
1284 template<class _InputIterator
,
1285 typename
= std::_RequireInputIter
<_InputIterator
>>
1287 template<class _InputIterator
>
1290 append(_InputIterator __first
, _InputIterator __last
)
1291 { return this->replace(end(), end(), __first
, __last
); }
1293 #if __cplusplus > 201402L
1295 * @brief Append a string_view.
1296 * @param __svt An object convertible to string_view to be appended.
1297 * @return Reference to this string.
1299 template<typename _Tp
>
1300 _If_sv
<_Tp
, basic_string
&>
1301 append(const _Tp
& __svt
)
1303 __sv_type __sv
= __svt
;
1304 return this->append(__sv
.data(), __sv
.size());
1308 * @brief Append a range of characters from a string_view.
1309 * @param __svt An object convertible to string_view to be appended from.
1310 * @param __pos The position in the string_view to append from.
1311 * @param __n The number of characters to append from the string_view.
1312 * @return Reference to this string.
1314 template<typename _Tp
>
1315 _If_sv
<_Tp
, basic_string
&>
1316 append(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
1318 __sv_type __sv
= __svt
;
1319 return _M_append(__sv
.data()
1320 + __sv
._M_check(__pos
, "basic_string::append"),
1321 __sv
._M_limit(__pos
, __n
));
1326 * @brief Append a single character.
1327 * @param __c Character to append.
1330 push_back(_CharT __c
)
1332 const size_type __size
= this->size();
1333 if (__size
+ 1 > this->capacity())
1334 this->_M_mutate(__size
, size_type(0), 0, size_type(1));
1335 traits_type::assign(this->_M_data()[__size
], __c
);
1336 this->_M_set_length(__size
+ 1);
1340 * @brief Set value to contents of another string.
1341 * @param __str Source string to use.
1342 * @return Reference to this string.
1345 assign(const basic_string
& __str
)
1347 this->_M_assign(__str
);
1351 #if __cplusplus >= 201103L
1353 * @brief Set value to contents of another string.
1354 * @param __str Source string to use.
1355 * @return Reference to this string.
1357 * This function sets this string to the exact contents of @a __str.
1358 * @a __str is a valid, but unspecified string.
1361 assign(basic_string
&& __str
)
1362 noexcept(_Alloc_traits::_S_nothrow_move())
1364 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1365 // 2063. Contradictory requirements for string move assignment
1366 return *this = std::move(__str
);
1371 * @brief Set value to a substring of a string.
1372 * @param __str The string to use.
1373 * @param __pos Index of the first character of str.
1374 * @param __n Number of characters to use.
1375 * @return Reference to this string.
1376 * @throw std::out_of_range if @a pos is not a valid index.
1378 * This function sets this string to the substring of @a __str
1379 * consisting of @a __n characters at @a __pos. If @a __n is
1380 * is larger than the number of available characters in @a
1381 * __str, the remainder of @a __str is used.
1384 assign(const basic_string
& __str
, size_type __pos
, size_type __n
)
1385 { return _M_replace(size_type(0), this->size(), __str
._M_data()
1386 + __str
._M_check(__pos
, "basic_string::assign"),
1387 __str
._M_limit(__pos
, __n
)); }
1390 * @brief Set value to a C substring.
1391 * @param __s The C string to use.
1392 * @param __n Number of characters to use.
1393 * @return Reference to this string.
1395 * This function sets the value of this string to the first @a __n
1396 * characters of @a __s. If @a __n is is larger than the number of
1397 * available characters in @a __s, the remainder of @a __s is used.
1400 assign(const _CharT
* __s
, size_type __n
)
1402 __glibcxx_requires_string_len(__s
, __n
);
1403 return _M_replace(size_type(0), this->size(), __s
, __n
);
1407 * @brief Set value to contents of a C string.
1408 * @param __s The C string to use.
1409 * @return Reference to this string.
1411 * This function sets the value of this string to the value of @a __s.
1412 * The data is copied, so there is no dependence on @a __s once the
1416 assign(const _CharT
* __s
)
1418 __glibcxx_requires_string(__s
);
1419 return _M_replace(size_type(0), this->size(), __s
,
1420 traits_type::length(__s
));
1424 * @brief Set value to multiple characters.
1425 * @param __n Length of the resulting string.
1426 * @param __c The character to use.
1427 * @return Reference to this string.
1429 * This function sets the value of this string to @a __n copies of
1433 assign(size_type __n
, _CharT __c
)
1434 { return _M_replace_aux(size_type(0), this->size(), __n
, __c
); }
1437 * @brief Set value to a range of characters.
1438 * @param __first Iterator referencing the first character to append.
1439 * @param __last Iterator marking the end of the range.
1440 * @return Reference to this string.
1442 * Sets value of string to characters in the range [__first,__last).
1444 #if __cplusplus >= 201103L
1445 template<class _InputIterator
,
1446 typename
= std::_RequireInputIter
<_InputIterator
>>
1448 template<class _InputIterator
>
1451 assign(_InputIterator __first
, _InputIterator __last
)
1452 { return this->replace(begin(), end(), __first
, __last
); }
1454 #if __cplusplus >= 201103L
1456 * @brief Set value to an initializer_list of characters.
1457 * @param __l The initializer_list of characters to assign.
1458 * @return Reference to this string.
1461 assign(initializer_list
<_CharT
> __l
)
1462 { return this->assign(__l
.begin(), __l
.size()); }
1465 #if __cplusplus > 201402L
1467 * @brief Set value from a string_view.
1468 * @param __svt The source object convertible to string_view.
1469 * @return Reference to this string.
1471 template<typename _Tp
>
1472 _If_sv
<_Tp
, basic_string
&>
1473 assign(const _Tp
& __svt
)
1475 __sv_type __sv
= __svt
;
1476 return this->assign(__sv
.data(), __sv
.size());
1480 * @brief Set value from a range of characters in a string_view.
1481 * @param __svt The source object convertible to string_view.
1482 * @param __pos The position in the string_view to assign from.
1483 * @param __n The number of characters to assign.
1484 * @return Reference to this string.
1486 template<typename _Tp
>
1487 _If_sv
<_Tp
, basic_string
&>
1488 assign(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
1490 __sv_type __sv
= __svt
;
1491 return _M_replace(size_type(0), this->size(), __sv
.data()
1492 + __sv
._M_check(__pos
, "basic_string::assign"),
1493 __sv
._M_limit(__pos
, __n
));
1497 #if __cplusplus >= 201103L
1499 * @brief Insert multiple characters.
1500 * @param __p Const_iterator referencing location in string to
1502 * @param __n Number of characters to insert
1503 * @param __c The character to insert.
1504 * @return Iterator referencing the first inserted char.
1505 * @throw std::length_error If new length exceeds @c max_size().
1507 * Inserts @a __n copies of character @a __c starting at the
1508 * position referenced by iterator @a __p. If adding
1509 * characters causes the length to exceed max_size(),
1510 * length_error is thrown. The value of the string doesn't
1511 * change if an error is thrown.
1514 insert(const_iterator __p
, size_type __n
, _CharT __c
)
1516 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1517 const size_type __pos
= __p
- begin();
1518 this->replace(__p
, __p
, __n
, __c
);
1519 return iterator(this->_M_data() + __pos
);
1523 * @brief Insert multiple characters.
1524 * @param __p Iterator referencing location in string to insert at.
1525 * @param __n Number of characters to insert
1526 * @param __c The character to insert.
1527 * @throw std::length_error If new length exceeds @c max_size().
1529 * Inserts @a __n copies of character @a __c starting at the
1530 * position referenced by iterator @a __p. If adding
1531 * characters causes the length to exceed max_size(),
1532 * length_error is thrown. The value of the string doesn't
1533 * change if an error is thrown.
1536 insert(iterator __p
, size_type __n
, _CharT __c
)
1537 { this->replace(__p
, __p
, __n
, __c
); }
1540 #if __cplusplus >= 201103L
1542 * @brief Insert a range of characters.
1543 * @param __p Const_iterator referencing location in string to
1545 * @param __beg Start of range.
1546 * @param __end End of range.
1547 * @return Iterator referencing the first inserted char.
1548 * @throw std::length_error If new length exceeds @c max_size().
1550 * Inserts characters in range [beg,end). If adding characters
1551 * causes the length to exceed max_size(), length_error is
1552 * thrown. The value of the string doesn't change if an error
1555 template<class _InputIterator
,
1556 typename
= std::_RequireInputIter
<_InputIterator
>>
1558 insert(const_iterator __p
, _InputIterator __beg
, _InputIterator __end
)
1560 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1561 const size_type __pos
= __p
- begin();
1562 this->replace(__p
, __p
, __beg
, __end
);
1563 return iterator(this->_M_data() + __pos
);
1567 * @brief Insert a range of characters.
1568 * @param __p Iterator referencing location in string to insert at.
1569 * @param __beg Start of range.
1570 * @param __end End of range.
1571 * @throw std::length_error If new length exceeds @c max_size().
1573 * Inserts characters in range [__beg,__end). If adding
1574 * characters causes the length to exceed max_size(),
1575 * length_error is thrown. The value of the string doesn't
1576 * change if an error is thrown.
1578 template<class _InputIterator
>
1580 insert(iterator __p
, _InputIterator __beg
, _InputIterator __end
)
1581 { this->replace(__p
, __p
, __beg
, __end
); }
1584 #if __cplusplus >= 201103L
1586 * @brief Insert an initializer_list of characters.
1587 * @param __p Iterator referencing location in string to insert at.
1588 * @param __l The initializer_list of characters to insert.
1589 * @throw std::length_error If new length exceeds @c max_size().
1592 insert(iterator __p
, initializer_list
<_CharT
> __l
)
1594 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1595 this->insert(__p
- begin(), __l
.begin(), __l
.size());
1600 * @brief Insert value of a string.
1601 * @param __pos1 Iterator referencing location in string to insert at.
1602 * @param __str The string to insert.
1603 * @return Reference to this string.
1604 * @throw std::length_error If new length exceeds @c max_size().
1606 * Inserts value of @a __str starting at @a __pos1. If adding
1607 * characters causes the length to exceed max_size(),
1608 * length_error is thrown. The value of the string doesn't
1609 * change if an error is thrown.
1612 insert(size_type __pos1
, const basic_string
& __str
)
1613 { return this->replace(__pos1
, size_type(0),
1614 __str
._M_data(), __str
.size()); }
1617 * @brief Insert a substring.
1618 * @param __pos1 Iterator referencing location in string to insert at.
1619 * @param __str The string to insert.
1620 * @param __pos2 Start of characters in str to insert.
1621 * @param __n Number of characters to insert.
1622 * @return Reference to this string.
1623 * @throw std::length_error If new length exceeds @c max_size().
1624 * @throw std::out_of_range If @a pos1 > size() or
1625 * @a __pos2 > @a str.size().
1627 * Starting at @a pos1, insert @a __n character of @a __str
1628 * beginning with @a __pos2. If adding characters causes the
1629 * length to exceed max_size(), length_error is thrown. If @a
1630 * __pos1 is beyond the end of this string or @a __pos2 is
1631 * beyond the end of @a __str, out_of_range is thrown. The
1632 * value of the string doesn't change if an error is thrown.
1635 insert(size_type __pos1
, const basic_string
& __str
,
1636 size_type __pos2
, size_type __n
)
1637 { return this->replace(__pos1
, size_type(0), __str
._M_data()
1638 + __str
._M_check(__pos2
, "basic_string::insert"),
1639 __str
._M_limit(__pos2
, __n
)); }
1642 * @brief Insert a C substring.
1643 * @param __pos Iterator referencing location in string to insert at.
1644 * @param __s The C string to insert.
1645 * @param __n The number of characters to insert.
1646 * @return Reference to this string.
1647 * @throw std::length_error If new length exceeds @c max_size().
1648 * @throw std::out_of_range If @a __pos is beyond the end of this
1651 * Inserts the first @a __n characters of @a __s starting at @a
1652 * __pos. If adding characters causes the length to exceed
1653 * max_size(), length_error is thrown. If @a __pos is beyond
1654 * end(), out_of_range is thrown. The value of the string
1655 * doesn't change if an error is thrown.
1658 insert(size_type __pos
, const _CharT
* __s
, size_type __n
)
1659 { return this->replace(__pos
, size_type(0), __s
, __n
); }
1662 * @brief Insert a C string.
1663 * @param __pos Iterator referencing location in string to insert at.
1664 * @param __s The C string to insert.
1665 * @return Reference to this string.
1666 * @throw std::length_error If new length exceeds @c max_size().
1667 * @throw std::out_of_range If @a pos is beyond the end of this
1670 * Inserts the first @a n characters of @a __s starting at @a __pos. If
1671 * adding characters causes the length to exceed max_size(),
1672 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
1673 * thrown. The value of the string doesn't change if an error is
1677 insert(size_type __pos
, const _CharT
* __s
)
1679 __glibcxx_requires_string(__s
);
1680 return this->replace(__pos
, size_type(0), __s
,
1681 traits_type::length(__s
));
1685 * @brief Insert multiple characters.
1686 * @param __pos Index in string to insert at.
1687 * @param __n Number of characters to insert
1688 * @param __c The character to insert.
1689 * @return Reference to this string.
1690 * @throw std::length_error If new length exceeds @c max_size().
1691 * @throw std::out_of_range If @a __pos is beyond the end of this
1694 * Inserts @a __n copies of character @a __c starting at index
1695 * @a __pos. If adding characters causes the length to exceed
1696 * max_size(), length_error is thrown. If @a __pos > length(),
1697 * out_of_range is thrown. The value of the string doesn't
1698 * change if an error is thrown.
1701 insert(size_type __pos
, size_type __n
, _CharT __c
)
1702 { return _M_replace_aux(_M_check(__pos
, "basic_string::insert"),
1703 size_type(0), __n
, __c
); }
1706 * @brief Insert one character.
1707 * @param __p Iterator referencing position in string to insert at.
1708 * @param __c The character to insert.
1709 * @return Iterator referencing newly inserted char.
1710 * @throw std::length_error If new length exceeds @c max_size().
1712 * Inserts character @a __c at position referenced by @a __p.
1713 * If adding character causes the length to exceed max_size(),
1714 * length_error is thrown. If @a __p is beyond end of string,
1715 * out_of_range is thrown. The value of the string doesn't
1716 * change if an error is thrown.
1719 insert(__const_iterator __p
, _CharT __c
)
1721 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1722 const size_type __pos
= __p
- begin();
1723 _M_replace_aux(__pos
, size_type(0), size_type(1), __c
);
1724 return iterator(_M_data() + __pos
);
1727 #if __cplusplus > 201402L
1729 * @brief Insert a string_view.
1730 * @param __pos Iterator referencing position in string to insert at.
1731 * @param __svt The object convertible to string_view to insert.
1732 * @return Reference to this string.
1734 template<typename _Tp
>
1735 _If_sv
<_Tp
, basic_string
&>
1736 insert(size_type __pos
, const _Tp
& __svt
)
1738 __sv_type __sv
= __svt
;
1739 return this->insert(__pos
, __sv
.data(), __sv
.size());
1743 * @brief Insert a string_view.
1744 * @param __pos Iterator referencing position in string to insert at.
1745 * @param __svt The object convertible to string_view to insert from.
1746 * @param __pos Iterator referencing position in string_view to insert
1748 * @param __n The number of characters to insert.
1749 * @return Reference to this string.
1751 template<typename _Tp
>
1752 _If_sv
<_Tp
, basic_string
&>
1753 insert(size_type __pos1
, const _Tp
& __svt
,
1754 size_type __pos2
, size_type __n
= npos
)
1756 __sv_type __sv
= __svt
;
1757 return this->replace(__pos1
, size_type(0), __sv
.data()
1758 + __sv
._M_check(__pos2
, "basic_string::insert"),
1759 __sv
._M_limit(__pos2
, __n
));
1764 * @brief Remove characters.
1765 * @param __pos Index of first character to remove (default 0).
1766 * @param __n Number of characters to remove (default remainder).
1767 * @return Reference to this string.
1768 * @throw std::out_of_range If @a pos is beyond the end of this
1771 * Removes @a __n characters from this string starting at @a
1772 * __pos. The length of the string is reduced by @a __n. If
1773 * there are < @a __n characters to remove, the remainder of
1774 * the string is truncated. If @a __p is beyond end of string,
1775 * out_of_range is thrown. The value of the string doesn't
1776 * change if an error is thrown.
1779 erase(size_type __pos
= 0, size_type __n
= npos
)
1781 _M_check(__pos
, "basic_string::erase");
1783 this->_M_set_length(__pos
);
1785 this->_M_erase(__pos
, _M_limit(__pos
, __n
));
1790 * @brief Remove one character.
1791 * @param __position Iterator referencing the character to remove.
1792 * @return iterator referencing same location after removal.
1794 * Removes the character at @a __position from this string. The value
1795 * of the string doesn't change if an error is thrown.
1798 erase(__const_iterator __position
)
1800 _GLIBCXX_DEBUG_PEDASSERT(__position
>= begin()
1801 && __position
< end());
1802 const size_type __pos
= __position
- begin();
1803 this->_M_erase(__pos
, size_type(1));
1804 return iterator(_M_data() + __pos
);
1808 * @brief Remove a range of characters.
1809 * @param __first Iterator referencing the first character to remove.
1810 * @param __last Iterator referencing the end of the range.
1811 * @return Iterator referencing location of first after removal.
1813 * Removes the characters in the range [first,last) from this string.
1814 * The value of the string doesn't change if an error is thrown.
1817 erase(__const_iterator __first
, __const_iterator __last
)
1819 _GLIBCXX_DEBUG_PEDASSERT(__first
>= begin() && __first
<= __last
1820 && __last
<= end());
1821 const size_type __pos
= __first
- begin();
1822 if (__last
== end())
1823 this->_M_set_length(__pos
);
1825 this->_M_erase(__pos
, __last
- __first
);
1826 return iterator(this->_M_data() + __pos
);
1829 #if __cplusplus >= 201103L
1831 * @brief Remove the last character.
1833 * The string must be non-empty.
1838 __glibcxx_assert(!empty());
1839 _M_erase(size() - 1, 1);
1844 * @brief Replace characters with value from another string.
1845 * @param __pos Index of first character to replace.
1846 * @param __n Number of characters to be replaced.
1847 * @param __str String to insert.
1848 * @return Reference to this string.
1849 * @throw std::out_of_range If @a pos is beyond the end of this
1851 * @throw std::length_error If new length exceeds @c max_size().
1853 * Removes the characters in the range [__pos,__pos+__n) from
1854 * this string. In place, the value of @a __str is inserted.
1855 * If @a __pos is beyond end of string, out_of_range is thrown.
1856 * If the length of the result exceeds max_size(), length_error
1857 * is thrown. The value of the string doesn't change if an
1861 replace(size_type __pos
, size_type __n
, const basic_string
& __str
)
1862 { return this->replace(__pos
, __n
, __str
._M_data(), __str
.size()); }
1865 * @brief Replace characters with value from another string.
1866 * @param __pos1 Index of first character to replace.
1867 * @param __n1 Number of characters to be replaced.
1868 * @param __str String to insert.
1869 * @param __pos2 Index of first character of str to use.
1870 * @param __n2 Number of characters from str to use.
1871 * @return Reference to this string.
1872 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
1874 * @throw std::length_error If new length exceeds @c max_size().
1876 * Removes the characters in the range [__pos1,__pos1 + n) from this
1877 * string. In place, the value of @a __str is inserted. If @a __pos is
1878 * beyond end of string, out_of_range is thrown. If the length of the
1879 * result exceeds max_size(), length_error is thrown. The value of the
1880 * string doesn't change if an error is thrown.
1883 replace(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
1884 size_type __pos2
, size_type __n2
)
1885 { return this->replace(__pos1
, __n1
, __str
._M_data()
1886 + __str
._M_check(__pos2
, "basic_string::replace"),
1887 __str
._M_limit(__pos2
, __n2
)); }
1890 * @brief Replace characters with value of a C substring.
1891 * @param __pos Index of first character to replace.
1892 * @param __n1 Number of characters to be replaced.
1893 * @param __s C string to insert.
1894 * @param __n2 Number of characters from @a s to use.
1895 * @return Reference to this string.
1896 * @throw std::out_of_range If @a pos1 > size().
1897 * @throw std::length_error If new length exceeds @c max_size().
1899 * Removes the characters in the range [__pos,__pos + __n1)
1900 * from this string. In place, the first @a __n2 characters of
1901 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
1902 * @a __pos is beyond end of string, out_of_range is thrown. If
1903 * the length of result exceeds max_size(), length_error is
1904 * thrown. The value of the string doesn't change if an error
1908 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
1911 __glibcxx_requires_string_len(__s
, __n2
);
1912 return _M_replace(_M_check(__pos
, "basic_string::replace"),
1913 _M_limit(__pos
, __n1
), __s
, __n2
);
1917 * @brief Replace characters with value of a C string.
1918 * @param __pos Index of first character to replace.
1919 * @param __n1 Number of characters to be replaced.
1920 * @param __s C string to insert.
1921 * @return Reference to this string.
1922 * @throw std::out_of_range If @a pos > size().
1923 * @throw std::length_error If new length exceeds @c max_size().
1925 * Removes the characters in the range [__pos,__pos + __n1)
1926 * from this string. In place, the characters of @a __s are
1927 * inserted. If @a __pos is beyond end of string, out_of_range
1928 * is thrown. If the length of result exceeds max_size(),
1929 * length_error is thrown. The value of the string doesn't
1930 * change if an error is thrown.
1933 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
)
1935 __glibcxx_requires_string(__s
);
1936 return this->replace(__pos
, __n1
, __s
, traits_type::length(__s
));
1940 * @brief Replace characters with multiple characters.
1941 * @param __pos Index of first character to replace.
1942 * @param __n1 Number of characters to be replaced.
1943 * @param __n2 Number of characters to insert.
1944 * @param __c Character to insert.
1945 * @return Reference to this string.
1946 * @throw std::out_of_range If @a __pos > size().
1947 * @throw std::length_error If new length exceeds @c max_size().
1949 * Removes the characters in the range [pos,pos + n1) from this
1950 * string. In place, @a __n2 copies of @a __c are inserted.
1951 * If @a __pos is beyond end of string, out_of_range is thrown.
1952 * If the length of result exceeds max_size(), length_error is
1953 * thrown. The value of the string doesn't change if an error
1957 replace(size_type __pos
, size_type __n1
, size_type __n2
, _CharT __c
)
1958 { return _M_replace_aux(_M_check(__pos
, "basic_string::replace"),
1959 _M_limit(__pos
, __n1
), __n2
, __c
); }
1962 * @brief Replace range of characters with string.
1963 * @param __i1 Iterator referencing start of range to replace.
1964 * @param __i2 Iterator referencing end of range to replace.
1965 * @param __str String value to insert.
1966 * @return Reference to this string.
1967 * @throw std::length_error If new length exceeds @c max_size().
1969 * Removes the characters in the range [__i1,__i2). In place,
1970 * the value of @a __str is inserted. If the length of result
1971 * exceeds max_size(), length_error is thrown. The value of
1972 * the string doesn't change if an error is thrown.
1975 replace(__const_iterator __i1
, __const_iterator __i2
,
1976 const basic_string
& __str
)
1977 { return this->replace(__i1
, __i2
, __str
._M_data(), __str
.size()); }
1980 * @brief Replace range of characters with C substring.
1981 * @param __i1 Iterator referencing start of range to replace.
1982 * @param __i2 Iterator referencing end of range to replace.
1983 * @param __s C string value to insert.
1984 * @param __n Number of characters from s to insert.
1985 * @return Reference to this string.
1986 * @throw std::length_error If new length exceeds @c max_size().
1988 * Removes the characters in the range [__i1,__i2). In place,
1989 * the first @a __n characters of @a __s are inserted. If the
1990 * length of result exceeds max_size(), length_error is thrown.
1991 * The value of the string doesn't change if an error is
1995 replace(__const_iterator __i1
, __const_iterator __i2
,
1996 const _CharT
* __s
, size_type __n
)
1998 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2000 return this->replace(__i1
- begin(), __i2
- __i1
, __s
, __n
);
2004 * @brief Replace range of characters with C string.
2005 * @param __i1 Iterator referencing start of range to replace.
2006 * @param __i2 Iterator referencing end of range to replace.
2007 * @param __s C string value to insert.
2008 * @return Reference to this string.
2009 * @throw std::length_error If new length exceeds @c max_size().
2011 * Removes the characters in the range [__i1,__i2). In place,
2012 * the characters of @a __s are inserted. If the length of
2013 * result exceeds max_size(), length_error is thrown. The
2014 * value of the string doesn't change if an error is thrown.
2017 replace(__const_iterator __i1
, __const_iterator __i2
, const _CharT
* __s
)
2019 __glibcxx_requires_string(__s
);
2020 return this->replace(__i1
, __i2
, __s
, traits_type::length(__s
));
2024 * @brief Replace range of characters with multiple characters
2025 * @param __i1 Iterator referencing start of range to replace.
2026 * @param __i2 Iterator referencing end of range to replace.
2027 * @param __n Number of characters to insert.
2028 * @param __c Character to insert.
2029 * @return Reference to this string.
2030 * @throw std::length_error If new length exceeds @c max_size().
2032 * Removes the characters in the range [__i1,__i2). In place,
2033 * @a __n copies of @a __c are inserted. If the length of
2034 * result exceeds max_size(), length_error is thrown. The
2035 * value of the string doesn't change if an error is thrown.
2038 replace(__const_iterator __i1
, __const_iterator __i2
, size_type __n
,
2041 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2043 return _M_replace_aux(__i1
- begin(), __i2
- __i1
, __n
, __c
);
2047 * @brief Replace range of characters with range.
2048 * @param __i1 Iterator referencing start of range to replace.
2049 * @param __i2 Iterator referencing end of range to replace.
2050 * @param __k1 Iterator referencing start of range to insert.
2051 * @param __k2 Iterator referencing end of range to insert.
2052 * @return Reference to this string.
2053 * @throw std::length_error If new length exceeds @c max_size().
2055 * Removes the characters in the range [__i1,__i2). In place,
2056 * characters in the range [__k1,__k2) are inserted. If the
2057 * length of result exceeds max_size(), length_error is thrown.
2058 * The value of the string doesn't change if an error is
2061 #if __cplusplus >= 201103L
2062 template<class _InputIterator
,
2063 typename
= std::_RequireInputIter
<_InputIterator
>>
2065 replace(const_iterator __i1
, const_iterator __i2
,
2066 _InputIterator __k1
, _InputIterator __k2
)
2068 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2070 __glibcxx_requires_valid_range(__k1
, __k2
);
2071 return this->_M_replace_dispatch(__i1
, __i2
, __k1
, __k2
,
2072 std::__false_type());
2075 template<class _InputIterator
>
2076 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
2077 typename __enable_if_not_native_iterator
<_InputIterator
>::__type
2081 replace(iterator __i1
, iterator __i2
,
2082 _InputIterator __k1
, _InputIterator __k2
)
2084 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2086 __glibcxx_requires_valid_range(__k1
, __k2
);
2087 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
2088 return _M_replace_dispatch(__i1
, __i2
, __k1
, __k2
, _Integral());
2092 // Specializations for the common case of pointer and iterator:
2093 // useful to avoid the overhead of temporary buffering in _M_replace.
2095 replace(__const_iterator __i1
, __const_iterator __i2
,
2096 _CharT
* __k1
, _CharT
* __k2
)
2098 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2100 __glibcxx_requires_valid_range(__k1
, __k2
);
2101 return this->replace(__i1
- begin(), __i2
- __i1
,
2106 replace(__const_iterator __i1
, __const_iterator __i2
,
2107 const _CharT
* __k1
, const _CharT
* __k2
)
2109 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2111 __glibcxx_requires_valid_range(__k1
, __k2
);
2112 return this->replace(__i1
- begin(), __i2
- __i1
,
2117 replace(__const_iterator __i1
, __const_iterator __i2
,
2118 iterator __k1
, iterator __k2
)
2120 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2122 __glibcxx_requires_valid_range(__k1
, __k2
);
2123 return this->replace(__i1
- begin(), __i2
- __i1
,
2124 __k1
.base(), __k2
- __k1
);
2128 replace(__const_iterator __i1
, __const_iterator __i2
,
2129 const_iterator __k1
, const_iterator __k2
)
2131 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2133 __glibcxx_requires_valid_range(__k1
, __k2
);
2134 return this->replace(__i1
- begin(), __i2
- __i1
,
2135 __k1
.base(), __k2
- __k1
);
2138 #if __cplusplus >= 201103L
2140 * @brief Replace range of characters with initializer_list.
2141 * @param __i1 Iterator referencing start of range to replace.
2142 * @param __i2 Iterator referencing end of range to replace.
2143 * @param __l The initializer_list of characters to insert.
2144 * @return Reference to this string.
2145 * @throw std::length_error If new length exceeds @c max_size().
2147 * Removes the characters in the range [__i1,__i2). In place,
2148 * characters in the range [__k1,__k2) are inserted. If the
2149 * length of result exceeds max_size(), length_error is thrown.
2150 * The value of the string doesn't change if an error is
2153 basic_string
& replace(const_iterator __i1
, const_iterator __i2
,
2154 initializer_list
<_CharT
> __l
)
2155 { return this->replace(__i1
, __i2
, __l
.begin(), __l
.size()); }
2158 #if __cplusplus > 201402L
2160 * @brief Replace range of characters with string_view.
2161 * @param __pos The position to replace at.
2162 * @param __n The number of characters to replace.
2163 * @param __svt The object convertible to string_view to insert.
2164 * @return Reference to this string.
2166 template<typename _Tp
>
2167 _If_sv
<_Tp
, basic_string
&>
2168 replace(size_type __pos
, size_type __n
, const _Tp
& __svt
)
2170 __sv_type __sv
= __svt
;
2171 return this->replace(__pos
, __n
, __sv
.data(), __sv
.size());
2175 * @brief Replace range of characters with string_view.
2176 * @param __pos1 The position to replace at.
2177 * @param __n1 The number of characters to replace.
2178 * @param __svt The object convertible to string_view to insert from.
2179 * @param __pos2 The position in the string_view to insert from.
2180 * @param __n2 The number of characters to insert.
2181 * @return Reference to this string.
2183 template<typename _Tp
>
2184 _If_sv
<_Tp
, basic_string
&>
2185 replace(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
2186 size_type __pos2
, size_type __n2
= npos
)
2188 __sv_type __sv
= __svt
;
2189 return this->replace(__pos1
, __n1
, __sv
.data()
2190 + __sv
._M_check(__pos2
, "basic_string::replace"),
2191 __sv
._M_limit(__pos2
, __n2
));
2195 * @brief Replace range of characters with string_view.
2196 * @param __i1 An iterator referencing the start position
2198 * @param __i2 An iterator referencing the end position
2200 * @param __svt The object convertible to string_view to insert from.
2201 * @return Reference to this string.
2203 template<typename _Tp
>
2204 _If_sv
<_Tp
, basic_string
&>
2205 replace(const_iterator __i1
, const_iterator __i2
, const _Tp
& __svt
)
2207 __sv_type __sv
= __svt
;
2208 return this->replace(__i1
- begin(), __i2
- __i1
, __sv
);
2213 template<class _Integer
>
2215 _M_replace_dispatch(const_iterator __i1
, const_iterator __i2
,
2216 _Integer __n
, _Integer __val
, __true_type
)
2217 { return _M_replace_aux(__i1
- begin(), __i2
- __i1
, __n
, __val
); }
2219 template<class _InputIterator
>
2221 _M_replace_dispatch(const_iterator __i1
, const_iterator __i2
,
2222 _InputIterator __k1
, _InputIterator __k2
,
2226 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
2230 _M_replace(size_type __pos
, size_type __len1
, const _CharT
* __s
,
2231 const size_type __len2
);
2234 _M_append(const _CharT
* __s
, size_type __n
);
2239 * @brief Copy substring into C string.
2240 * @param __s C string to copy value into.
2241 * @param __n Number of characters to copy.
2242 * @param __pos Index of first character to copy.
2243 * @return Number of characters actually copied
2244 * @throw std::out_of_range If __pos > size().
2246 * Copies up to @a __n characters starting at @a __pos into the
2247 * C string @a __s. If @a __pos is %greater than size(),
2248 * out_of_range is thrown.
2251 copy(_CharT
* __s
, size_type __n
, size_type __pos
= 0) const;
2254 * @brief Swap contents with another string.
2255 * @param __s String to swap with.
2257 * Exchanges the contents of this string with that of @a __s in constant
2261 swap(basic_string
& __s
) _GLIBCXX_NOEXCEPT
;
2263 // String operations:
2265 * @brief Return const pointer to null-terminated contents.
2267 * This is a handle to internal data. Do not modify or dire things may
2271 c_str() const _GLIBCXX_NOEXCEPT
2272 { return _M_data(); }
2275 * @brief Return const pointer to contents.
2277 * This is a pointer to internal data. It is undefined to modify
2278 * the contents through the returned pointer. To get a pointer that
2279 * allows modifying the contents use @c &str[0] instead,
2280 * (or in C++17 the non-const @c str.data() overload).
2283 data() const _GLIBCXX_NOEXCEPT
2284 { return _M_data(); }
2286 #if __cplusplus > 201402L
2288 * @brief Return non-const pointer to contents.
2290 * This is a pointer to the character sequence held by the string.
2291 * Modifying the characters in the sequence is allowed.
2295 { return _M_data(); }
2299 * @brief Return copy of allocator used to construct this string.
2302 get_allocator() const _GLIBCXX_NOEXCEPT
2303 { return _M_get_allocator(); }
2306 * @brief Find position of a C substring.
2307 * @param __s C string to locate.
2308 * @param __pos Index of character to search from.
2309 * @param __n Number of characters from @a s to search for.
2310 * @return Index of start of first occurrence.
2312 * Starting from @a __pos, searches forward for the first @a
2313 * __n characters in @a __s within this string. If found,
2314 * returns the index where it begins. If not found, returns
2318 find(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2322 * @brief Find position of a string.
2323 * @param __str String to locate.
2324 * @param __pos Index of character to search from (default 0).
2325 * @return Index of start of first occurrence.
2327 * Starting from @a __pos, searches forward for value of @a __str within
2328 * this string. If found, returns the index where it begins. If not
2329 * found, returns npos.
2332 find(const basic_string
& __str
, size_type __pos
= 0) const
2334 { return this->find(__str
.data(), __pos
, __str
.size()); }
2336 #if __cplusplus > 201402L
2338 * @brief Find position of a string_view.
2339 * @param __svt The object convertible to string_view to locate.
2340 * @param __pos Index of character to search from (default 0).
2341 * @return Index of start of first occurrence.
2343 template<typename _Tp
>
2344 _If_sv
<_Tp
, size_type
>
2345 find(const _Tp
& __svt
, size_type __pos
= 0) const
2346 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2348 __sv_type __sv
= __svt
;
2349 return this->find(__sv
.data(), __pos
, __sv
.size());
2354 * @brief Find position of a C string.
2355 * @param __s C string to locate.
2356 * @param __pos Index of character to search from (default 0).
2357 * @return Index of start of first occurrence.
2359 * Starting from @a __pos, searches forward for the value of @a
2360 * __s within this string. If found, returns the index where
2361 * it begins. If not found, returns npos.
2364 find(const _CharT
* __s
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
2366 __glibcxx_requires_string(__s
);
2367 return this->find(__s
, __pos
, traits_type::length(__s
));
2371 * @brief Find position of a character.
2372 * @param __c Character to locate.
2373 * @param __pos Index of character to search from (default 0).
2374 * @return Index of first occurrence.
2376 * Starting from @a __pos, searches forward for @a __c within
2377 * this string. If found, returns the index where it was
2378 * found. If not found, returns npos.
2381 find(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
;
2384 * @brief Find last position of a string.
2385 * @param __str String to locate.
2386 * @param __pos Index of character to search back from (default end).
2387 * @return Index of start of last occurrence.
2389 * Starting from @a __pos, searches backward for value of @a
2390 * __str within this string. If found, returns the index where
2391 * it begins. If not found, returns npos.
2394 rfind(const basic_string
& __str
, size_type __pos
= npos
) const
2396 { return this->rfind(__str
.data(), __pos
, __str
.size()); }
2398 #if __cplusplus > 201402L
2400 * @brief Find last position of a string_view.
2401 * @param __svt The object convertible to string_view to locate.
2402 * @param __pos Index of character to search back from (default end).
2403 * @return Index of start of last occurrence.
2405 template<typename _Tp
>
2406 _If_sv
<_Tp
, size_type
>
2407 rfind(const _Tp
& __svt
, size_type __pos
= npos
) const
2408 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2410 __sv_type __sv
= __svt
;
2411 return this->rfind(__sv
.data(), __pos
, __sv
.size());
2416 * @brief Find last position of a C substring.
2417 * @param __s C string to locate.
2418 * @param __pos Index of character to search back from.
2419 * @param __n Number of characters from s to search for.
2420 * @return Index of start of last occurrence.
2422 * Starting from @a __pos, searches backward for the first @a
2423 * __n characters in @a __s within this string. If found,
2424 * returns the index where it begins. If not found, returns
2428 rfind(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2432 * @brief Find last position of a C string.
2433 * @param __s C string to locate.
2434 * @param __pos Index of character to start search at (default end).
2435 * @return Index of start of last occurrence.
2437 * Starting from @a __pos, searches backward for the value of
2438 * @a __s within this string. If found, returns the index
2439 * where it begins. If not found, returns npos.
2442 rfind(const _CharT
* __s
, size_type __pos
= npos
) const
2444 __glibcxx_requires_string(__s
);
2445 return this->rfind(__s
, __pos
, traits_type::length(__s
));
2449 * @brief Find last position of a character.
2450 * @param __c Character to locate.
2451 * @param __pos Index of character to search back from (default end).
2452 * @return Index of last occurrence.
2454 * Starting from @a __pos, searches backward for @a __c within
2455 * this string. If found, returns the index where it was
2456 * found. If not found, returns npos.
2459 rfind(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
;
2462 * @brief Find position of a character of string.
2463 * @param __str String containing characters to locate.
2464 * @param __pos Index of character to search from (default 0).
2465 * @return Index of first occurrence.
2467 * Starting from @a __pos, searches forward for one of the
2468 * characters of @a __str within this string. If found,
2469 * returns the index where it was found. If not found, returns
2473 find_first_of(const basic_string
& __str
, size_type __pos
= 0) const
2475 { return this->find_first_of(__str
.data(), __pos
, __str
.size()); }
2477 #if __cplusplus > 201402L
2479 * @brief Find position of a character of a string_view.
2480 * @param __svt An object convertible to string_view containing
2481 * characters to locate.
2482 * @param __pos Index of character to search from (default 0).
2483 * @return Index of first occurrence.
2485 template<typename _Tp
>
2486 _If_sv
<_Tp
, size_type
>
2487 find_first_of(const _Tp
& __svt
, size_type __pos
= 0) const
2488 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2490 __sv_type __sv
= __svt
;
2491 return this->find_first_of(__sv
.data(), __pos
, __sv
.size());
2496 * @brief Find position of a character of C substring.
2497 * @param __s String containing characters to locate.
2498 * @param __pos Index of character to search from.
2499 * @param __n Number of characters from s to search for.
2500 * @return Index of first occurrence.
2502 * Starting from @a __pos, searches forward for one of the
2503 * first @a __n characters of @a __s within this string. If
2504 * found, returns the index where it was found. If not found,
2508 find_first_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2512 * @brief Find position of a character of C string.
2513 * @param __s String containing characters to locate.
2514 * @param __pos Index of character to search from (default 0).
2515 * @return Index of first occurrence.
2517 * Starting from @a __pos, searches forward for one of the
2518 * characters of @a __s within this string. If found, returns
2519 * the index where it was found. If not found, returns npos.
2522 find_first_of(const _CharT
* __s
, size_type __pos
= 0) const
2525 __glibcxx_requires_string(__s
);
2526 return this->find_first_of(__s
, __pos
, traits_type::length(__s
));
2530 * @brief Find position of a character.
2531 * @param __c Character to locate.
2532 * @param __pos Index of character to search from (default 0).
2533 * @return Index of first occurrence.
2535 * Starting from @a __pos, searches forward for the character
2536 * @a __c within this string. If found, returns the index
2537 * where it was found. If not found, returns npos.
2539 * Note: equivalent to find(__c, __pos).
2542 find_first_of(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
2543 { return this->find(__c
, __pos
); }
2546 * @brief Find last position of a character of string.
2547 * @param __str String containing characters to locate.
2548 * @param __pos Index of character to search back from (default end).
2549 * @return Index of last occurrence.
2551 * Starting from @a __pos, searches backward for one of the
2552 * characters of @a __str within this string. If found,
2553 * returns the index where it was found. If not found, returns
2557 find_last_of(const basic_string
& __str
, size_type __pos
= npos
) const
2559 { return this->find_last_of(__str
.data(), __pos
, __str
.size()); }
2561 #if __cplusplus > 201402L
2563 * @brief Find last position of a character of string.
2564 * @param __svt An object convertible to string_view containing
2565 * characters to locate.
2566 * @param __pos Index of character to search back from (default end).
2567 * @return Index of last occurrence.
2569 template<typename _Tp
>
2570 _If_sv
<_Tp
, size_type
>
2571 find_last_of(const _Tp
& __svt
, size_type __pos
= npos
) const
2572 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2574 __sv_type __sv
= __svt
;
2575 return this->find_last_of(__sv
.data(), __pos
, __sv
.size());
2580 * @brief Find last position of a character of C substring.
2581 * @param __s C string containing characters to locate.
2582 * @param __pos Index of character to search back from.
2583 * @param __n Number of characters from s to search for.
2584 * @return Index of last occurrence.
2586 * Starting from @a __pos, searches backward for one of the
2587 * first @a __n characters of @a __s within this string. If
2588 * found, returns the index where it was found. If not found,
2592 find_last_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2596 * @brief Find last position of a character of C string.
2597 * @param __s C string containing characters to locate.
2598 * @param __pos Index of character to search back from (default end).
2599 * @return Index of last occurrence.
2601 * Starting from @a __pos, searches backward for one of the
2602 * characters of @a __s within this string. If found, returns
2603 * the index where it was found. If not found, returns npos.
2606 find_last_of(const _CharT
* __s
, size_type __pos
= npos
) const
2609 __glibcxx_requires_string(__s
);
2610 return this->find_last_of(__s
, __pos
, traits_type::length(__s
));
2614 * @brief Find last position of a character.
2615 * @param __c Character to locate.
2616 * @param __pos Index of character to search back from (default end).
2617 * @return Index of last occurrence.
2619 * Starting from @a __pos, searches backward for @a __c within
2620 * this string. If found, returns the index where it was
2621 * found. If not found, returns npos.
2623 * Note: equivalent to rfind(__c, __pos).
2626 find_last_of(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
2627 { return this->rfind(__c
, __pos
); }
2630 * @brief Find position of a character not in string.
2631 * @param __str String containing characters to avoid.
2632 * @param __pos Index of character to search from (default 0).
2633 * @return Index of first occurrence.
2635 * Starting from @a __pos, searches forward for a character not contained
2636 * in @a __str within this string. If found, returns the index where it
2637 * was found. If not found, returns npos.
2640 find_first_not_of(const basic_string
& __str
, size_type __pos
= 0) const
2642 { return this->find_first_not_of(__str
.data(), __pos
, __str
.size()); }
2644 #if __cplusplus > 201402L
2646 * @brief Find position of a character not in a string_view.
2647 * @param __svt A object convertible to string_view containing
2648 * characters to avoid.
2649 * @param __pos Index of character to search from (default 0).
2650 * @return Index of first occurrence.
2652 template<typename _Tp
>
2653 _If_sv
<_Tp
, size_type
>
2654 find_first_not_of(const _Tp
& __svt
, size_type __pos
= 0) const
2655 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2657 __sv_type __sv
= __svt
;
2658 return this->find_first_not_of(__sv
.data(), __pos
, __sv
.size());
2663 * @brief Find 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 from.
2666 * @param __n Number of characters from __s to consider.
2667 * @return Index of first occurrence.
2669 * Starting from @a __pos, searches forward for a character not
2670 * contained in the first @a __n characters of @a __s within
2671 * this string. If found, returns the index where it was
2672 * found. If not found, returns npos.
2675 find_first_not_of(const _CharT
* __s
, size_type __pos
,
2676 size_type __n
) const _GLIBCXX_NOEXCEPT
;
2679 * @brief Find position of a character not in C string.
2680 * @param __s C string containing characters to avoid.
2681 * @param __pos Index of character to search from (default 0).
2682 * @return Index of first occurrence.
2684 * Starting from @a __pos, searches forward for a character not
2685 * contained in @a __s within this string. If found, returns
2686 * the index where it was found. If not found, returns npos.
2689 find_first_not_of(const _CharT
* __s
, size_type __pos
= 0) const
2692 __glibcxx_requires_string(__s
);
2693 return this->find_first_not_of(__s
, __pos
, traits_type::length(__s
));
2697 * @brief Find position of a different character.
2698 * @param __c Character to avoid.
2699 * @param __pos Index of character to search from (default 0).
2700 * @return Index of first occurrence.
2702 * Starting from @a __pos, searches forward for a character
2703 * other than @a __c within this string. If found, returns the
2704 * index where it was found. If not found, returns npos.
2707 find_first_not_of(_CharT __c
, size_type __pos
= 0) const
2711 * @brief Find last position of a character not in string.
2712 * @param __str String containing characters to avoid.
2713 * @param __pos Index of character to search back from (default end).
2714 * @return Index of last occurrence.
2716 * Starting from @a __pos, searches backward for a character
2717 * not contained in @a __str within this string. If found,
2718 * returns the index where it was found. If not found, returns
2722 find_last_not_of(const basic_string
& __str
, size_type __pos
= npos
) const
2724 { return this->find_last_not_of(__str
.data(), __pos
, __str
.size()); }
2726 #if __cplusplus > 201402L
2728 * @brief Find last position of a character not in a string_view.
2729 * @param __svt An object convertible to string_view containing
2730 * characters to avoid.
2731 * @param __pos Index of character to search back from (default end).
2732 * @return Index of last occurrence.
2734 template<typename _Tp
>
2735 _If_sv
<_Tp
, size_type
>
2736 find_last_not_of(const _Tp
& __svt
, size_type __pos
= npos
) const
2737 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2739 __sv_type __sv
= __svt
;
2740 return this->find_last_not_of(__sv
.data(), __pos
, __sv
.size());
2745 * @brief Find last position of a character not in C substring.
2746 * @param __s C string containing characters to avoid.
2747 * @param __pos Index of character to search back from.
2748 * @param __n Number of characters from s to consider.
2749 * @return Index of last occurrence.
2751 * Starting from @a __pos, searches backward for a character not
2752 * contained in the first @a __n characters of @a __s within this string.
2753 * If found, returns the index where it was found. If not found,
2757 find_last_not_of(const _CharT
* __s
, size_type __pos
,
2758 size_type __n
) const _GLIBCXX_NOEXCEPT
;
2760 * @brief Find last position of a character not in C string.
2761 * @param __s C string containing characters to avoid.
2762 * @param __pos Index of character to search back from (default end).
2763 * @return Index of last occurrence.
2765 * Starting from @a __pos, searches backward for a character
2766 * not contained in @a __s within this string. If found,
2767 * returns the index where it was found. If not found, returns
2771 find_last_not_of(const _CharT
* __s
, size_type __pos
= npos
) const
2774 __glibcxx_requires_string(__s
);
2775 return this->find_last_not_of(__s
, __pos
, traits_type::length(__s
));
2779 * @brief Find last position of a different character.
2780 * @param __c Character to avoid.
2781 * @param __pos Index of character to search back from (default end).
2782 * @return Index of last occurrence.
2784 * Starting from @a __pos, searches backward for a character other than
2785 * @a __c within this string. If found, returns the index where it was
2786 * found. If not found, returns npos.
2789 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const
2793 * @brief Get a substring.
2794 * @param __pos Index of first character (default 0).
2795 * @param __n Number of characters in substring (default remainder).
2796 * @return The new string.
2797 * @throw std::out_of_range If __pos > size().
2799 * Construct and return a new string using the @a __n
2800 * characters starting at @a __pos. If the string is too
2801 * short, use the remainder of the characters. If @a __pos is
2802 * beyond the end of the string, out_of_range is thrown.
2805 substr(size_type __pos
= 0, size_type __n
= npos
) const
2806 { return basic_string(*this,
2807 _M_check(__pos
, "basic_string::substr"), __n
); }
2810 * @brief Compare to a string.
2811 * @param __str String to compare against.
2812 * @return Integer < 0, 0, or > 0.
2814 * Returns an integer < 0 if this string is ordered before @a
2815 * __str, 0 if their values are equivalent, or > 0 if this
2816 * string is ordered after @a __str. Determines the effective
2817 * length rlen of the strings to compare as the smallest of
2818 * size() and str.size(). The function then compares the two
2819 * strings by calling traits::compare(data(), str.data(),rlen).
2820 * If the result of the comparison is nonzero returns it,
2821 * otherwise the shorter one is ordered first.
2824 compare(const basic_string
& __str
) const
2826 const size_type __size
= this->size();
2827 const size_type __osize
= __str
.size();
2828 const size_type __len
= std::min(__size
, __osize
);
2830 int __r
= traits_type::compare(_M_data(), __str
.data(), __len
);
2832 __r
= _S_compare(__size
, __osize
);
2836 #if __cplusplus > 201402L
2838 * @brief Compare to a string_view.
2839 * @param __svt An object convertible to string_view to compare against.
2840 * @return Integer < 0, 0, or > 0.
2842 template<typename _Tp
>
2844 compare(const _Tp
& __svt
) const
2845 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2847 __sv_type __sv
= __svt
;
2848 const size_type __size
= this->size();
2849 const size_type __osize
= __sv
.size();
2850 const size_type __len
= std::min(__size
, __osize
);
2852 int __r
= traits_type::compare(_M_data(), __sv
.data(), __len
);
2854 __r
= _S_compare(__size
, __osize
);
2859 * @brief Compare to a string_view.
2860 * @param __pos A position in the string to start comparing from.
2861 * @param __n The number of characters to compare.
2862 * @param __svt An object convertible to string_view to compare
2864 * @return Integer < 0, 0, or > 0.
2866 template<typename _Tp
>
2868 compare(size_type __pos
, size_type __n
, const _Tp
& __svt
) const
2869 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2871 __sv_type __sv
= __svt
;
2872 return __sv_type(*this).substr(__pos
, __n
).compare(__sv
);
2876 * @brief Compare to a string_view.
2877 * @param __pos1 A position in the string to start comparing from.
2878 * @param __n1 The number of characters to compare.
2879 * @param __svt An object convertible to string_view to compare
2881 * @param __pos2 A position in the string_view to start comparing from.
2882 * @param __n2 The number of characters to compare.
2883 * @return Integer < 0, 0, or > 0.
2885 template<typename _Tp
>
2887 compare(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
2888 size_type __pos2
, size_type __n2
= npos
) const
2889 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2891 __sv_type __sv
= __svt
;
2892 return __sv_type(*this)
2893 .substr(__pos1
, __n1
).compare(__sv
.substr(__pos2
, __n2
));
2898 * @brief Compare substring to a string.
2899 * @param __pos Index of first character of substring.
2900 * @param __n Number of characters in substring.
2901 * @param __str String to compare against.
2902 * @return Integer < 0, 0, or > 0.
2904 * Form the substring of this string from the @a __n characters
2905 * starting at @a __pos. Returns an integer < 0 if the
2906 * substring is ordered before @a __str, 0 if their values are
2907 * equivalent, or > 0 if the substring is ordered after @a
2908 * __str. Determines the effective length rlen of the strings
2909 * to compare as the smallest of the length of the substring
2910 * and @a __str.size(). The function then compares the two
2911 * strings by calling
2912 * traits::compare(substring.data(),str.data(),rlen). If the
2913 * result of the comparison is nonzero returns it, otherwise
2914 * the shorter one is ordered first.
2917 compare(size_type __pos
, size_type __n
, const basic_string
& __str
) const;
2920 * @brief Compare substring to a substring.
2921 * @param __pos1 Index of first character of substring.
2922 * @param __n1 Number of characters in substring.
2923 * @param __str String to compare against.
2924 * @param __pos2 Index of first character of substring of str.
2925 * @param __n2 Number of characters in substring of str.
2926 * @return Integer < 0, 0, or > 0.
2928 * Form the substring of this string from the @a __n1
2929 * characters starting at @a __pos1. Form the substring of @a
2930 * __str from the @a __n2 characters starting at @a __pos2.
2931 * Returns an integer < 0 if this substring is ordered before
2932 * the substring of @a __str, 0 if their values are equivalent,
2933 * or > 0 if this substring is ordered after the substring of
2934 * @a __str. Determines the effective length rlen of the
2935 * strings to compare as the smallest of the lengths of the
2936 * substrings. The function then compares the two strings by
2938 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2939 * If the result of the comparison is nonzero returns it,
2940 * otherwise the shorter one is ordered first.
2943 compare(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
2944 size_type __pos2
, size_type __n2
) const;
2947 * @brief Compare to a C string.
2948 * @param __s C string to compare against.
2949 * @return Integer < 0, 0, or > 0.
2951 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
2952 * their values are equivalent, or > 0 if this string is ordered after
2953 * @a __s. Determines the effective length rlen of the strings to
2954 * compare as the smallest of size() and the length of a string
2955 * constructed from @a __s. The function then compares the two strings
2956 * by calling traits::compare(data(),s,rlen). If the result of the
2957 * comparison is nonzero returns it, otherwise the shorter one is
2961 compare(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
;
2963 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2964 // 5 String::compare specification questionable
2966 * @brief Compare substring to a C string.
2967 * @param __pos Index of first character of substring.
2968 * @param __n1 Number of characters in substring.
2969 * @param __s C string to compare against.
2970 * @return Integer < 0, 0, or > 0.
2972 * Form the substring of this string from the @a __n1
2973 * characters starting at @a pos. Returns an integer < 0 if
2974 * the substring is ordered before @a __s, 0 if their values
2975 * are equivalent, or > 0 if the substring is ordered after @a
2976 * __s. Determines the effective length rlen of the strings to
2977 * compare as the smallest of the length of the substring and
2978 * the length of a string constructed from @a __s. The
2979 * function then compares the two string by calling
2980 * traits::compare(substring.data(),__s,rlen). If the result of
2981 * the comparison is nonzero returns it, otherwise the shorter
2982 * one is ordered first.
2985 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
) const;
2988 * @brief Compare substring against a character %array.
2989 * @param __pos Index of first character of substring.
2990 * @param __n1 Number of characters in substring.
2991 * @param __s character %array to compare against.
2992 * @param __n2 Number of characters of s.
2993 * @return Integer < 0, 0, or > 0.
2995 * Form the substring of this string from the @a __n1
2996 * characters starting at @a __pos. Form a string from the
2997 * first @a __n2 characters of @a __s. Returns an integer < 0
2998 * if this substring is ordered before the string from @a __s,
2999 * 0 if their values are equivalent, or > 0 if this substring
3000 * is ordered after the string from @a __s. Determines the
3001 * effective length rlen of the strings to compare as the
3002 * smallest of the length of the substring and @a __n2. The
3003 * function then compares the two strings by calling
3004 * traits::compare(substring.data(),s,rlen). If the result of
3005 * the comparison is nonzero returns it, otherwise the shorter
3006 * one is ordered first.
3008 * NB: s must have at least n2 characters, '\\0' has
3009 * no special meaning.
3012 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
,
3013 size_type __n2
) const;
3015 // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
3016 template<typename
, typename
, typename
> friend class basic_stringbuf
;
3018 _GLIBCXX_END_NAMESPACE_CXX11
3019 #else // !_GLIBCXX_USE_CXX11_ABI
3020 // Reference-counted COW string implentation
3023 * @class basic_string basic_string.h <string>
3024 * @brief Managing sequences of characters and character-like objects.
3027 * @ingroup sequences
3029 * @tparam _CharT Type of character
3030 * @tparam _Traits Traits for character type, defaults to
3031 * char_traits<_CharT>.
3032 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
3034 * Meets the requirements of a <a href="tables.html#65">container</a>, a
3035 * <a href="tables.html#66">reversible container</a>, and a
3036 * <a href="tables.html#67">sequence</a>. Of the
3037 * <a href="tables.html#68">optional sequence requirements</a>, only
3038 * @c push_back, @c at, and @c %array access are supported.
3043 * Documentation? What's that?
3044 * Nathan Myers <ncm@cantrip.org>.
3046 * A string looks like this:
3051 * [basic_string<char_type>] _M_capacity
3052 * _M_dataplus _M_refcount
3053 * _M_p ----------------> unnamed array of char_type
3056 * Where the _M_p points to the first character in the string, and
3057 * you cast it to a pointer-to-_Rep and subtract 1 to get a
3058 * pointer to the header.
3060 * This approach has the enormous advantage that a string object
3061 * requires only one allocation. All the ugliness is confined
3062 * within a single %pair of inline functions, which each compile to
3063 * a single @a add instruction: _Rep::_M_data(), and
3064 * string::_M_rep(); and the allocation function which gets a
3065 * block of raw bytes and with room enough and constructs a _Rep
3066 * object at the front.
3068 * The reason you want _M_data pointing to the character %array and
3069 * not the _Rep is so that the debugger can see the string
3070 * contents. (Probably we should add a non-inline member to get
3071 * the _Rep for the debugger to use, so users can check the actual
3074 * Note that the _Rep object is a POD so that you can have a
3075 * static <em>empty string</em> _Rep object already @a constructed before
3076 * static constructors have run. The reference-count encoding is
3077 * chosen so that a 0 indicates one reference, so you never try to
3078 * destroy the empty-string _Rep object.
3080 * All but the last paragraph is considered pretty conventional
3081 * for a C++ string implementation.
3083 // 21.3 Template class basic_string
3084 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3087 typedef typename
_Alloc::template rebind
<_CharT
>::other _CharT_alloc_type
;
3091 typedef _Traits traits_type
;
3092 typedef typename
_Traits::char_type value_type
;
3093 typedef _Alloc allocator_type
;
3094 typedef typename
_CharT_alloc_type::size_type size_type
;
3095 typedef typename
_CharT_alloc_type::difference_type difference_type
;
3096 typedef typename
_CharT_alloc_type::reference reference
;
3097 typedef typename
_CharT_alloc_type::const_reference const_reference
;
3098 typedef typename
_CharT_alloc_type::pointer pointer
;
3099 typedef typename
_CharT_alloc_type::const_pointer const_pointer
;
3100 typedef __gnu_cxx::__normal_iterator
<pointer
, basic_string
> iterator
;
3101 typedef __gnu_cxx::__normal_iterator
<const_pointer
, basic_string
>
3103 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
3104 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
3107 // _Rep: string representation
3109 // 1. String really contains _M_length + 1 characters: due to 21.3.4
3110 // must be kept null-terminated.
3111 // 2. _M_capacity >= _M_length
3112 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
3113 // 3. _M_refcount has three states:
3114 // -1: leaked, one reference, no ref-copies allowed, non-const.
3115 // 0: one reference, non-const.
3116 // n>0: n + 1 references, operations require a lock, const.
3117 // 4. All fields==0 is an empty string, given the extra storage
3118 // beyond-the-end for a null terminator; thus, the shared
3119 // empty string representation needs no constructor.
3123 size_type _M_length
;
3124 size_type _M_capacity
;
3125 _Atomic_word _M_refcount
;
3128 struct _Rep
: _Rep_base
3131 typedef typename
_Alloc::template rebind
<char>::other _Raw_bytes_alloc
;
3133 // (Public) Data members:
3135 // The maximum number of individual char_type elements of an
3136 // individual string is determined by _S_max_size. This is the
3137 // value that will be returned by max_size(). (Whereas npos
3138 // is the maximum number of bytes the allocator can allocate.)
3139 // If one was to divvy up the theoretical largest size string,
3140 // with a terminating character and m _CharT elements, it'd
3142 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
3144 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
3145 // In addition, this implementation quarters this amount.
3146 static const size_type _S_max_size
;
3147 static const _CharT _S_terminal
;
3149 // The following storage is init'd to 0 by the linker, resulting
3150 // (carefully) in an empty string with one reference.
3151 static size_type _S_empty_rep_storage
[];
3154 _S_empty_rep() _GLIBCXX_NOEXCEPT
3156 // NB: Mild hack to avoid strict-aliasing warnings. Note that
3157 // _S_empty_rep_storage is never modified and the punning should
3158 // be reasonably safe in this case.
3159 void* __p
= reinterpret_cast<void*>(&_S_empty_rep_storage
);
3160 return *reinterpret_cast<_Rep
*>(__p
);
3164 _M_is_leaked() const _GLIBCXX_NOEXCEPT
3166 #if defined(__GTHREADS)
3167 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3168 // so we need to use an atomic load. However, _M_is_leaked
3169 // predicate does not change concurrently (i.e. the string is either
3170 // leaked or not), so a relaxed load is enough.
3171 return __atomic_load_n(&this->_M_refcount
, __ATOMIC_RELAXED
) < 0;
3173 return this->_M_refcount
< 0;
3178 _M_is_shared() const _GLIBCXX_NOEXCEPT
3180 #if defined(__GTHREADS)
3181 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3182 // so we need to use an atomic load. Another thread can drop last
3183 // but one reference concurrently with this check, so we need this
3184 // load to be acquire to synchronize with release fetch_and_add in
3186 return __atomic_load_n(&this->_M_refcount
, __ATOMIC_ACQUIRE
) > 0;
3188 return this->_M_refcount
> 0;
3193 _M_set_leaked() _GLIBCXX_NOEXCEPT
3194 { this->_M_refcount
= -1; }
3197 _M_set_sharable() _GLIBCXX_NOEXCEPT
3198 { this->_M_refcount
= 0; }
3201 _M_set_length_and_sharable(size_type __n
) _GLIBCXX_NOEXCEPT
3203 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3204 if (__builtin_expect(this != &_S_empty_rep(), false))
3207 this->_M_set_sharable(); // One reference.
3208 this->_M_length
= __n
;
3209 traits_type::assign(this->_M_refdata()[__n
], _S_terminal
);
3210 // grrr. (per 21.3.4)
3211 // You cannot leave those LWG people alone for a second.
3216 _M_refdata() throw()
3217 { return reinterpret_cast<_CharT
*>(this + 1); }
3220 _M_grab(const _Alloc
& __alloc1
, const _Alloc
& __alloc2
)
3222 return (!_M_is_leaked() && __alloc1
== __alloc2
)
3223 ? _M_refcopy() : _M_clone(__alloc1
);
3228 _S_create(size_type
, size_type
, const _Alloc
&);
3231 _M_dispose(const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
3233 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3234 if (__builtin_expect(this != &_S_empty_rep(), false))
3237 // Be race-detector-friendly. For more info see bits/c++config.
3238 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount
);
3239 // Decrement of _M_refcount is acq_rel, because:
3240 // - all but last decrements need to release to synchronize with
3241 // the last decrement that will delete the object.
3242 // - the last decrement needs to acquire to synchronize with
3243 // all the previous decrements.
3244 // - last but one decrement needs to release to synchronize with
3245 // the acquire load in _M_is_shared that will conclude that
3246 // the object is not shared anymore.
3247 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount
,
3250 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount
);
3257 _M_destroy(const _Alloc
&) throw();
3260 _M_refcopy() throw()
3262 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3263 if (__builtin_expect(this != &_S_empty_rep(), false))
3265 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount
, 1);
3266 return _M_refdata();
3270 _M_clone(const _Alloc
&, size_type __res
= 0);
3273 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
3274 struct _Alloc_hider
: _Alloc
3276 _Alloc_hider(_CharT
* __dat
, const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
3277 : _Alloc(__a
), _M_p(__dat
) { }
3279 _CharT
* _M_p
; // The actual data.
3283 // Data Members (public):
3284 // NB: This is an unsigned type, and thus represents the maximum
3285 // size that the allocator can hold.
3286 /// Value returned by various member functions when they fail.
3287 static const size_type npos
= static_cast<size_type
>(-1);
3290 // Data Members (private):
3291 mutable _Alloc_hider _M_dataplus
;
3294 _M_data() const _GLIBCXX_NOEXCEPT
3295 { return _M_dataplus
._M_p
; }
3298 _M_data(_CharT
* __p
) _GLIBCXX_NOEXCEPT
3299 { return (_M_dataplus
._M_p
= __p
); }
3302 _M_rep() const _GLIBCXX_NOEXCEPT
3303 { return &((reinterpret_cast<_Rep
*> (_M_data()))[-1]); }
3305 // For the internal use we have functions similar to `begin'/`end'
3306 // but they do not call _M_leak.
3308 _M_ibegin() const _GLIBCXX_NOEXCEPT
3309 { return iterator(_M_data()); }
3312 _M_iend() const _GLIBCXX_NOEXCEPT
3313 { return iterator(_M_data() + this->size()); }
3316 _M_leak() // for use in begin() & non-const op[]
3318 if (!_M_rep()->_M_is_leaked())
3323 _M_check(size_type __pos
, const char* __s
) const
3325 if (__pos
> this->size())
3326 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
3327 "this->size() (which is %zu)"),
3328 __s
, __pos
, this->size());
3333 _M_check_length(size_type __n1
, size_type __n2
, const char* __s
) const
3335 if (this->max_size() - (this->size() - __n1
) < __n2
)
3336 __throw_length_error(__N(__s
));
3339 // NB: _M_limit doesn't check for a bad __pos value.
3341 _M_limit(size_type __pos
, size_type __off
) const _GLIBCXX_NOEXCEPT
3343 const bool __testoff
= __off
< this->size() - __pos
;
3344 return __testoff
? __off
: this->size() - __pos
;
3347 // True if _Rep and source do not overlap.
3349 _M_disjunct(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
3351 return (less
<const _CharT
*>()(__s
, _M_data())
3352 || less
<const _CharT
*>()(_M_data() + this->size(), __s
));
3355 // When __n = 1 way faster than the general multichar
3356 // traits_type::copy/move/assign.
3358 _M_copy(_CharT
* __d
, const _CharT
* __s
, size_type __n
) _GLIBCXX_NOEXCEPT
3361 traits_type::assign(*__d
, *__s
);
3363 traits_type::copy(__d
, __s
, __n
);
3367 _M_move(_CharT
* __d
, const _CharT
* __s
, size_type __n
) _GLIBCXX_NOEXCEPT
3370 traits_type::assign(*__d
, *__s
);
3372 traits_type::move(__d
, __s
, __n
);
3376 _M_assign(_CharT
* __d
, size_type __n
, _CharT __c
) _GLIBCXX_NOEXCEPT
3379 traits_type::assign(*__d
, __c
);
3381 traits_type::assign(__d
, __n
, __c
);
3384 // _S_copy_chars is a separate template to permit specialization
3385 // to optimize for the common case of pointers as iterators.
3386 template<class _Iterator
>
3388 _S_copy_chars(_CharT
* __p
, _Iterator __k1
, _Iterator __k2
)
3390 for (; __k1
!= __k2
; ++__k1
, (void)++__p
)
3391 traits_type::assign(*__p
, *__k1
); // These types are off.
3395 _S_copy_chars(_CharT
* __p
, iterator __k1
, iterator __k2
) _GLIBCXX_NOEXCEPT
3396 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
3399 _S_copy_chars(_CharT
* __p
, const_iterator __k1
, const_iterator __k2
)
3401 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
3404 _S_copy_chars(_CharT
* __p
, _CharT
* __k1
, _CharT
* __k2
) _GLIBCXX_NOEXCEPT
3405 { _M_copy(__p
, __k1
, __k2
- __k1
); }
3408 _S_copy_chars(_CharT
* __p
, const _CharT
* __k1
, const _CharT
* __k2
)
3410 { _M_copy(__p
, __k1
, __k2
- __k1
); }
3413 _S_compare(size_type __n1
, size_type __n2
) _GLIBCXX_NOEXCEPT
3415 const difference_type __d
= difference_type(__n1
- __n2
);
3417 if (__d
> __gnu_cxx::__numeric_traits
<int>::__max
)
3418 return __gnu_cxx::__numeric_traits
<int>::__max
;
3419 else if (__d
< __gnu_cxx::__numeric_traits
<int>::__min
)
3420 return __gnu_cxx::__numeric_traits
<int>::__min
;
3426 _M_mutate(size_type __pos
, size_type __len1
, size_type __len2
);
3432 _S_empty_rep() _GLIBCXX_NOEXCEPT
3433 { return _Rep::_S_empty_rep(); }
3435 #if __cplusplus > 201402L
3436 // A helper type for avoiding boiler-plate.
3437 typedef basic_string_view
<_CharT
, _Traits
> __sv_type
;
3439 template<typename _Tp
, typename _Res
>
3440 using _If_sv
= enable_if_t
<
3441 __and_
<is_convertible
<const _Tp
&, __sv_type
>,
3442 __not_
<is_convertible
<const _Tp
*, const basic_string
*>>,
3443 __not_
<is_convertible
<const _Tp
&, const _CharT
*>>>::value
,
3446 // Allows an implicit conversion to __sv_type.
3448 _S_to_string_view(__sv_type __svt
) noexcept
3451 // Wraps a string_view by explicit conversion and thus
3452 // allows to add an internal constructor that does not
3453 // participate in overload resolution when a string_view
3457 explicit __sv_wrapper(__sv_type __sv
) noexcept
: _M_sv(__sv
) { }
3463 // Construct/copy/destroy:
3464 // NB: We overload ctors in some cases instead of using default
3465 // arguments, per 17.4.4.4 para. 2 item 2.
3468 * @brief Default constructor creates an empty string.
3471 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3472 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
3474 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ }
3478 * @brief Construct an empty string using allocator @a a.
3481 basic_string(const _Alloc
& __a
);
3483 // NB: per LWG issue 42, semantics different from IS:
3485 * @brief Construct string with copy of value of @a str.
3486 * @param __str Source string.
3488 basic_string(const basic_string
& __str
);
3490 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3491 // 2583. no way to supply an allocator for basic_string(str, pos)
3493 * @brief Construct string as copy of a substring.
3494 * @param __str Source string.
3495 * @param __pos Index of first character to copy from.
3496 * @param __a Allocator to use.
3498 basic_string(const basic_string
& __str
, size_type __pos
,
3499 const _Alloc
& __a
= _Alloc());
3502 * @brief Construct string as copy of a substring.
3503 * @param __str Source string.
3504 * @param __pos Index of first character to copy from.
3505 * @param __n Number of characters to copy.
3507 basic_string(const basic_string
& __str
, size_type __pos
,
3510 * @brief Construct string as copy of a substring.
3511 * @param __str Source string.
3512 * @param __pos Index of first character to copy from.
3513 * @param __n Number of characters to copy.
3514 * @param __a Allocator to use.
3516 basic_string(const basic_string
& __str
, size_type __pos
,
3517 size_type __n
, const _Alloc
& __a
);
3520 * @brief Construct string initialized by a character %array.
3521 * @param __s Source character %array.
3522 * @param __n Number of characters to copy.
3523 * @param __a Allocator to use (default is default allocator).
3525 * NB: @a __s must have at least @a __n characters, '\\0'
3526 * has no special meaning.
3528 basic_string(const _CharT
* __s
, size_type __n
,
3529 const _Alloc
& __a
= _Alloc());
3531 * @brief Construct string as copy of a C string.
3532 * @param __s Source C string.
3533 * @param __a Allocator to use (default is default allocator).
3535 basic_string(const _CharT
* __s
, const _Alloc
& __a
= _Alloc());
3537 * @brief Construct string as multiple characters.
3538 * @param __n Number of characters.
3539 * @param __c Character to use.
3540 * @param __a Allocator to use (default is default allocator).
3542 basic_string(size_type __n
, _CharT __c
, const _Alloc
& __a
= _Alloc());
3544 #if __cplusplus >= 201103L
3546 * @brief Move construct string.
3547 * @param __str Source string.
3549 * The newly-created string contains the exact contents of @a __str.
3550 * @a __str is a valid, but unspecified string.
3552 basic_string(basic_string
&& __str
)
3553 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3554 noexcept
// FIXME C++11: should always be noexcept.
3556 : _M_dataplus(__str
._M_dataplus
)
3558 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3559 __str
._M_data(_S_empty_rep()._M_refdata());
3561 __str
._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
3566 * @brief Construct string from an initializer %list.
3567 * @param __l std::initializer_list of characters.
3568 * @param __a Allocator to use (default is default allocator).
3570 basic_string(initializer_list
<_CharT
> __l
, const _Alloc
& __a
= _Alloc());
3574 * @brief Construct string as copy of a range.
3575 * @param __beg Start of range.
3576 * @param __end End of range.
3577 * @param __a Allocator to use (default is default allocator).
3579 template<class _InputIterator
>
3580 basic_string(_InputIterator __beg
, _InputIterator __end
,
3581 const _Alloc
& __a
= _Alloc());
3583 #if __cplusplus > 201402L
3585 * @brief Construct string from a substring of a string_view.
3586 * @param __t Source object convertible to string view.
3587 * @param __pos The index of the first character to copy from __t.
3588 * @param __n The number of characters to copy from __t.
3589 * @param __a Allocator to use.
3591 template<typename _Tp
, typename
= _If_sv
<_Tp
, void>>
3592 basic_string(const _Tp
& __t
, size_type __pos
, size_type __n
,
3593 const _Alloc
& __a
= _Alloc())
3594 : basic_string(_S_to_string_view(__t
).substr(__pos
, __n
), __a
) { }
3597 * @brief Construct string from a string_view.
3598 * @param __t Source object convertible to string view.
3599 * @param __a Allocator to use (default is default allocator).
3601 template<typename _Tp
, typename
= _If_sv
<_Tp
, void>>
3603 basic_string(const _Tp
& __t
, const _Alloc
& __a
= _Alloc())
3604 : basic_string(__sv_wrapper(_S_to_string_view(__t
)), __a
) { }
3607 * @brief Only internally used: Construct string from a string view
3609 * @param __svw string view wrapper.
3610 * @param __a Allocator to use.
3613 basic_string(__sv_wrapper __svw
, const _Alloc
& __a
)
3614 : basic_string(__svw
._M_sv
.data(), __svw
._M_sv
.size(), __a
) { }
3618 * @brief Destroy the string instance.
3620 ~basic_string() _GLIBCXX_NOEXCEPT
3621 { _M_rep()->_M_dispose(this->get_allocator()); }
3624 * @brief Assign the value of @a str to this string.
3625 * @param __str Source string.
3628 operator=(const basic_string
& __str
)
3629 { return this->assign(__str
); }
3632 * @brief Copy contents of @a s into this string.
3633 * @param __s Source null-terminated string.
3636 operator=(const _CharT
* __s
)
3637 { return this->assign(__s
); }
3640 * @brief Set value to string of length 1.
3641 * @param __c Source character.
3643 * Assigning to a character makes this string length 1 and
3644 * (*this)[0] == @a c.
3647 operator=(_CharT __c
)
3649 this->assign(1, __c
);
3653 #if __cplusplus >= 201103L
3655 * @brief Move assign the value of @a str to this string.
3656 * @param __str Source string.
3658 * The contents of @a str are moved into this string (without copying).
3659 * @a str is a valid, but unspecified string.
3661 // PR 58265, this should be noexcept.
3663 operator=(basic_string
&& __str
)
3671 * @brief Set value to string constructed from initializer %list.
3672 * @param __l std::initializer_list.
3675 operator=(initializer_list
<_CharT
> __l
)
3677 this->assign(__l
.begin(), __l
.size());
3682 #if __cplusplus > 201402L
3684 * @brief Set value to string constructed from a string_view.
3685 * @param __svt An object convertible to string_view.
3687 template<typename _Tp
>
3688 _If_sv
<_Tp
, basic_string
&>
3689 operator=(const _Tp
& __svt
)
3690 { return this->assign(__svt
); }
3693 * @brief Convert to a string_view.
3694 * @return A string_view.
3696 operator __sv_type() const noexcept
3697 { return __sv_type(data(), size()); }
3702 * Returns a read/write iterator that points to the first character in
3703 * the %string. Unshares the string.
3706 begin() // FIXME C++11: should be noexcept.
3709 return iterator(_M_data());
3713 * Returns a read-only (constant) iterator that points to the first
3714 * character in the %string.
3717 begin() const _GLIBCXX_NOEXCEPT
3718 { return const_iterator(_M_data()); }
3721 * Returns a read/write iterator that points one past the last
3722 * character in the %string. Unshares the string.
3725 end() // FIXME C++11: should be noexcept.
3728 return iterator(_M_data() + this->size());
3732 * Returns a read-only (constant) iterator that points one past the
3733 * last character in the %string.
3736 end() const _GLIBCXX_NOEXCEPT
3737 { return const_iterator(_M_data() + this->size()); }
3740 * Returns a read/write reverse iterator that points to the last
3741 * character in the %string. Iteration is done in reverse element
3742 * order. Unshares the string.
3745 rbegin() // FIXME C++11: should be noexcept.
3746 { return reverse_iterator(this->end()); }
3749 * Returns a read-only (constant) reverse iterator that points
3750 * to the last character in the %string. Iteration is done in
3751 * reverse element order.
3753 const_reverse_iterator
3754 rbegin() const _GLIBCXX_NOEXCEPT
3755 { return const_reverse_iterator(this->end()); }
3758 * Returns a read/write reverse iterator that points to one before the
3759 * first character in the %string. Iteration is done in reverse
3760 * element order. Unshares the string.
3763 rend() // FIXME C++11: should be noexcept.
3764 { return reverse_iterator(this->begin()); }
3767 * Returns a read-only (constant) reverse iterator that points
3768 * to one before the first character in the %string. Iteration
3769 * is done in reverse element order.
3771 const_reverse_iterator
3772 rend() const _GLIBCXX_NOEXCEPT
3773 { return const_reverse_iterator(this->begin()); }
3775 #if __cplusplus >= 201103L
3777 * Returns a read-only (constant) iterator that points to the first
3778 * character in the %string.
3781 cbegin() const noexcept
3782 { return const_iterator(this->_M_data()); }
3785 * Returns a read-only (constant) iterator that points one past the
3786 * last character in the %string.
3789 cend() const noexcept
3790 { return const_iterator(this->_M_data() + this->size()); }
3793 * Returns a read-only (constant) reverse iterator that points
3794 * to the last character in the %string. Iteration is done in
3795 * reverse element order.
3797 const_reverse_iterator
3798 crbegin() const noexcept
3799 { return const_reverse_iterator(this->end()); }
3802 * Returns a read-only (constant) reverse iterator that points
3803 * to one before the first character in the %string. Iteration
3804 * is done in reverse element order.
3806 const_reverse_iterator
3807 crend() const noexcept
3808 { return const_reverse_iterator(this->begin()); }
3813 /// Returns the number of characters in the string, not including any
3814 /// null-termination.
3816 size() const _GLIBCXX_NOEXCEPT
3817 { return _M_rep()->_M_length
; }
3819 /// Returns the number of characters in the string, not including any
3820 /// null-termination.
3822 length() const _GLIBCXX_NOEXCEPT
3823 { return _M_rep()->_M_length
; }
3825 /// Returns the size() of the largest possible %string.
3827 max_size() const _GLIBCXX_NOEXCEPT
3828 { return _Rep::_S_max_size
; }
3831 * @brief Resizes the %string to the specified number of characters.
3832 * @param __n Number of characters the %string should contain.
3833 * @param __c Character to fill any new elements.
3835 * This function will %resize the %string to the specified
3836 * number of characters. If the number is smaller than the
3837 * %string's current size the %string is truncated, otherwise
3838 * the %string is extended and new elements are %set to @a __c.
3841 resize(size_type __n
, _CharT __c
);
3844 * @brief Resizes the %string to the specified number of characters.
3845 * @param __n Number of characters the %string should contain.
3847 * This function will resize the %string to the specified length. If
3848 * the new size is smaller than the %string's current size the %string
3849 * is truncated, otherwise the %string is extended and new characters
3850 * are default-constructed. For basic types such as char, this means
3851 * setting them to 0.
3854 resize(size_type __n
)
3855 { this->resize(__n
, _CharT()); }
3857 #if __cplusplus >= 201103L
3858 /// A non-binding request to reduce capacity() to size().
3860 shrink_to_fit() _GLIBCXX_NOEXCEPT
3862 #if __cpp_exceptions
3863 if (capacity() > size())
3875 * Returns the total number of characters that the %string can hold
3876 * before needing to allocate more memory.
3879 capacity() const _GLIBCXX_NOEXCEPT
3880 { return _M_rep()->_M_capacity
; }
3883 * @brief Attempt to preallocate enough memory for specified number of
3885 * @param __res_arg Number of characters required.
3886 * @throw std::length_error If @a __res_arg exceeds @c max_size().
3888 * This function attempts to reserve enough memory for the
3889 * %string to hold the specified number of characters. If the
3890 * number requested is more than max_size(), length_error is
3893 * The advantage of this function is that if optimal code is a
3894 * necessity and the user can determine the string length that will be
3895 * required, the user can reserve the memory in %advance, and thus
3896 * prevent a possible reallocation of memory and copying of %string
3900 reserve(size_type __res_arg
= 0);
3903 * Erases the string, making it empty.
3905 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3907 clear() _GLIBCXX_NOEXCEPT
3909 if (_M_rep()->_M_is_shared())
3911 _M_rep()->_M_dispose(this->get_allocator());
3912 _M_data(_S_empty_rep()._M_refdata());
3915 _M_rep()->_M_set_length_and_sharable(0);
3918 // PR 56166: this should not throw.
3921 { _M_mutate(0, this->size(), 0); }
3925 * Returns true if the %string is empty. Equivalent to
3926 * <code>*this == ""</code>.
3929 empty() const _GLIBCXX_NOEXCEPT
3930 { return this->size() == 0; }
3934 * @brief Subscript access to the data contained in the %string.
3935 * @param __pos The index of the character to access.
3936 * @return Read-only (constant) reference to the character.
3938 * This operator allows for easy, array-style, data access.
3939 * Note that data access with this operator is unchecked and
3940 * out_of_range lookups are not defined. (For checked lookups
3944 operator[] (size_type __pos
) const _GLIBCXX_NOEXCEPT
3946 __glibcxx_assert(__pos
<= size());
3947 return _M_data()[__pos
];
3951 * @brief Subscript access to the data contained in the %string.
3952 * @param __pos The index of the character to access.
3953 * @return Read/write reference to the character.
3955 * This operator allows for easy, array-style, data access.
3956 * Note that data access with this operator is unchecked and
3957 * out_of_range lookups are not defined. (For checked lookups
3958 * see at().) Unshares the string.
3961 operator[](size_type __pos
)
3963 // Allow pos == size() both in C++98 mode, as v3 extension,
3964 // and in C++11 mode.
3965 __glibcxx_assert(__pos
<= size());
3966 // In pedantic mode be strict in C++98 mode.
3967 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus
>= 201103L || __pos
< size());
3969 return _M_data()[__pos
];
3973 * @brief Provides access to the data contained in the %string.
3974 * @param __n The index of the character to access.
3975 * @return Read-only (const) reference to the character.
3976 * @throw std::out_of_range If @a n is an invalid index.
3978 * This function provides for safer data access. The parameter is
3979 * first checked that it is in the range of the string. The function
3980 * throws out_of_range if the check fails.
3983 at(size_type __n
) const
3985 if (__n
>= this->size())
3986 __throw_out_of_range_fmt(__N("basic_string::at: __n "
3987 "(which is %zu) >= this->size() "
3990 return _M_data()[__n
];
3994 * @brief Provides access to the data contained in the %string.
3995 * @param __n The index of the character to access.
3996 * @return Read/write reference to the character.
3997 * @throw std::out_of_range If @a n is an invalid index.
3999 * This function provides for safer data access. The parameter is
4000 * first checked that it is in the range of the string. The function
4001 * throws out_of_range if the check fails. Success results in
4002 * unsharing the string.
4008 __throw_out_of_range_fmt(__N("basic_string::at: __n "
4009 "(which is %zu) >= this->size() "
4013 return _M_data()[__n
];
4016 #if __cplusplus >= 201103L
4018 * Returns a read/write reference to the data at the first
4019 * element of the %string.
4024 __glibcxx_assert(!empty());
4025 return operator[](0);
4029 * Returns a read-only (constant) reference to the data at the first
4030 * element of the %string.
4033 front() const noexcept
4035 __glibcxx_assert(!empty());
4036 return operator[](0);
4040 * Returns a read/write reference to the data at the last
4041 * element of the %string.
4046 __glibcxx_assert(!empty());
4047 return operator[](this->size() - 1);
4051 * Returns a read-only (constant) reference to the data at the
4052 * last element of the %string.
4055 back() const noexcept
4057 __glibcxx_assert(!empty());
4058 return operator[](this->size() - 1);
4064 * @brief Append a string to this string.
4065 * @param __str The string to append.
4066 * @return Reference to this string.
4069 operator+=(const basic_string
& __str
)
4070 { return this->append(__str
); }
4073 * @brief Append a C string.
4074 * @param __s The C string to append.
4075 * @return Reference to this string.
4078 operator+=(const _CharT
* __s
)
4079 { return this->append(__s
); }
4082 * @brief Append a character.
4083 * @param __c The character to append.
4084 * @return Reference to this string.
4087 operator+=(_CharT __c
)
4089 this->push_back(__c
);
4093 #if __cplusplus >= 201103L
4095 * @brief Append an initializer_list of characters.
4096 * @param __l The initializer_list of characters to be appended.
4097 * @return Reference to this string.
4100 operator+=(initializer_list
<_CharT
> __l
)
4101 { return this->append(__l
.begin(), __l
.size()); }
4104 #if __cplusplus > 201402L
4106 * @brief Append a string_view.
4107 * @param __svt The object convertible to string_view to be appended.
4108 * @return Reference to this string.
4110 template<typename _Tp
>
4111 _If_sv
<_Tp
, basic_string
&>
4112 operator+=(const _Tp
& __svt
)
4113 { return this->append(__svt
); }
4117 * @brief Append a string to this string.
4118 * @param __str The string to append.
4119 * @return Reference to this string.
4122 append(const basic_string
& __str
);
4125 * @brief Append a substring.
4126 * @param __str The string to append.
4127 * @param __pos Index of the first character of str to append.
4128 * @param __n The number of characters to append.
4129 * @return Reference to this string.
4130 * @throw std::out_of_range if @a __pos is not a valid index.
4132 * This function appends @a __n characters from @a __str
4133 * starting at @a __pos to this string. If @a __n is is larger
4134 * than the number of available characters in @a __str, the
4135 * remainder of @a __str is appended.
4138 append(const basic_string
& __str
, size_type __pos
, size_type __n
);
4141 * @brief Append a C substring.
4142 * @param __s The C string to append.
4143 * @param __n The number of characters to append.
4144 * @return Reference to this string.
4147 append(const _CharT
* __s
, size_type __n
);
4150 * @brief Append a C string.
4151 * @param __s The C string to append.
4152 * @return Reference to this string.
4155 append(const _CharT
* __s
)
4157 __glibcxx_requires_string(__s
);
4158 return this->append(__s
, traits_type::length(__s
));
4162 * @brief Append multiple characters.
4163 * @param __n The number of characters to append.
4164 * @param __c The character to use.
4165 * @return Reference to this string.
4167 * Appends __n copies of __c to this string.
4170 append(size_type __n
, _CharT __c
);
4172 #if __cplusplus >= 201103L
4174 * @brief Append an initializer_list of characters.
4175 * @param __l The initializer_list of characters to append.
4176 * @return Reference to this string.
4179 append(initializer_list
<_CharT
> __l
)
4180 { return this->append(__l
.begin(), __l
.size()); }
4184 * @brief Append a range of characters.
4185 * @param __first Iterator referencing the first character to append.
4186 * @param __last Iterator marking the end of the range.
4187 * @return Reference to this string.
4189 * Appends characters in the range [__first,__last) to this string.
4191 template<class _InputIterator
>
4193 append(_InputIterator __first
, _InputIterator __last
)
4194 { return this->replace(_M_iend(), _M_iend(), __first
, __last
); }
4196 #if __cplusplus > 201402L
4198 * @brief Append a string_view.
4199 * @param __svt The object convertible to string_view to be appended.
4200 * @return Reference to this string.
4202 template<typename _Tp
>
4203 _If_sv
<_Tp
, basic_string
&>
4204 append(const _Tp
& __svt
)
4206 __sv_type __sv
= __svt
;
4207 return this->append(__sv
.data(), __sv
.size());
4211 * @brief Append a range of characters from a string_view.
4212 * @param __svt The object convertible to string_view to be appended
4214 * @param __pos The position in the string_view to append from.
4215 * @param __n The number of characters to append from the string_view.
4216 * @return Reference to this string.
4218 template<typename _Tp
>
4219 _If_sv
<_Tp
, basic_string
&>
4220 append(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
4222 __sv_type __sv
= __svt
;
4223 return append(__sv
.data()
4224 + __sv
._M_check(__pos
, "basic_string::append"),
4225 __sv
._M_limit(__pos
, __n
));
4230 * @brief Append a single character.
4231 * @param __c Character to append.
4234 push_back(_CharT __c
)
4236 const size_type __len
= 1 + this->size();
4237 if (__len
> this->capacity() || _M_rep()->_M_is_shared())
4238 this->reserve(__len
);
4239 traits_type::assign(_M_data()[this->size()], __c
);
4240 _M_rep()->_M_set_length_and_sharable(__len
);
4244 * @brief Set value to contents of another string.
4245 * @param __str Source string to use.
4246 * @return Reference to this string.
4249 assign(const basic_string
& __str
);
4251 #if __cplusplus >= 201103L
4253 * @brief Set value to contents of another string.
4254 * @param __str Source string to use.
4255 * @return Reference to this string.
4257 * This function sets this string to the exact contents of @a __str.
4258 * @a __str is a valid, but unspecified string.
4260 // PR 58265, this should be noexcept.
4262 assign(basic_string
&& __str
)
4270 * @brief Set value to a substring of a string.
4271 * @param __str The string to use.
4272 * @param __pos Index of the first character of str.
4273 * @param __n Number of characters to use.
4274 * @return Reference to this string.
4275 * @throw std::out_of_range if @a pos is not a valid index.
4277 * This function sets this string to the substring of @a __str
4278 * consisting of @a __n characters at @a __pos. If @a __n is
4279 * is larger than the number of available characters in @a
4280 * __str, the remainder of @a __str is used.
4283 assign(const basic_string
& __str
, size_type __pos
, size_type __n
)
4284 { return this->assign(__str
._M_data()
4285 + __str
._M_check(__pos
, "basic_string::assign"),
4286 __str
._M_limit(__pos
, __n
)); }
4289 * @brief Set value to a C substring.
4290 * @param __s The C string to use.
4291 * @param __n Number of characters to use.
4292 * @return Reference to this string.
4294 * This function sets the value of this string to the first @a __n
4295 * characters of @a __s. If @a __n is is larger than the number of
4296 * available characters in @a __s, the remainder of @a __s is used.
4299 assign(const _CharT
* __s
, size_type __n
);
4302 * @brief Set value to contents of a C string.
4303 * @param __s The C string to use.
4304 * @return Reference to this string.
4306 * This function sets the value of this string to the value of @a __s.
4307 * The data is copied, so there is no dependence on @a __s once the
4311 assign(const _CharT
* __s
)
4313 __glibcxx_requires_string(__s
);
4314 return this->assign(__s
, traits_type::length(__s
));
4318 * @brief Set value to multiple characters.
4319 * @param __n Length of the resulting string.
4320 * @param __c The character to use.
4321 * @return Reference to this string.
4323 * This function sets the value of this string to @a __n copies of
4327 assign(size_type __n
, _CharT __c
)
4328 { return _M_replace_aux(size_type(0), this->size(), __n
, __c
); }
4331 * @brief Set value to a range of characters.
4332 * @param __first Iterator referencing the first character to append.
4333 * @param __last Iterator marking the end of the range.
4334 * @return Reference to this string.
4336 * Sets value of string to characters in the range [__first,__last).
4338 template<class _InputIterator
>
4340 assign(_InputIterator __first
, _InputIterator __last
)
4341 { return this->replace(_M_ibegin(), _M_iend(), __first
, __last
); }
4343 #if __cplusplus >= 201103L
4345 * @brief Set value to an initializer_list of characters.
4346 * @param __l The initializer_list of characters to assign.
4347 * @return Reference to this string.
4350 assign(initializer_list
<_CharT
> __l
)
4351 { return this->assign(__l
.begin(), __l
.size()); }
4354 #if __cplusplus > 201402L
4356 * @brief Set value from a string_view.
4357 * @param __svt The source object convertible to string_view.
4358 * @return Reference to this string.
4360 template<typename _Tp
>
4361 _If_sv
<_Tp
, basic_string
&>
4362 assign(const _Tp
& __svt
)
4364 __sv_type __sv
= __svt
;
4365 return this->assign(__sv
.data(), __sv
.size());
4369 * @brief Set value from a range of characters in a string_view.
4370 * @param __svt The source object convertible to string_view.
4371 * @param __pos The position in the string_view to assign from.
4372 * @param __n The number of characters to assign.
4373 * @return Reference to this string.
4375 template<typename _Tp
>
4376 _If_sv
<_Tp
, basic_string
&>
4377 assign(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
4379 __sv_type __sv
= __svt
;
4380 return assign(__sv
.data()
4381 + __sv
._M_check(__pos
, "basic_string::assign"),
4382 __sv
._M_limit(__pos
, __n
));
4387 * @brief Insert multiple characters.
4388 * @param __p Iterator referencing location in string to insert at.
4389 * @param __n Number of characters to insert
4390 * @param __c The character to insert.
4391 * @throw std::length_error If new length exceeds @c max_size().
4393 * Inserts @a __n copies of character @a __c starting at the
4394 * position referenced by iterator @a __p. If adding
4395 * characters causes the length to exceed max_size(),
4396 * length_error is thrown. The value of the string doesn't
4397 * change if an error is thrown.
4400 insert(iterator __p
, size_type __n
, _CharT __c
)
4401 { this->replace(__p
, __p
, __n
, __c
); }
4404 * @brief Insert a range of characters.
4405 * @param __p Iterator referencing location in string to insert at.
4406 * @param __beg Start of range.
4407 * @param __end End of range.
4408 * @throw std::length_error If new length exceeds @c max_size().
4410 * Inserts characters in range [__beg,__end). If adding
4411 * characters causes the length to exceed max_size(),
4412 * length_error is thrown. The value of the string doesn't
4413 * change if an error is thrown.
4415 template<class _InputIterator
>
4417 insert(iterator __p
, _InputIterator __beg
, _InputIterator __end
)
4418 { this->replace(__p
, __p
, __beg
, __end
); }
4420 #if __cplusplus >= 201103L
4422 * @brief Insert an initializer_list of characters.
4423 * @param __p Iterator referencing location in string to insert at.
4424 * @param __l The initializer_list of characters to insert.
4425 * @throw std::length_error If new length exceeds @c max_size().
4428 insert(iterator __p
, initializer_list
<_CharT
> __l
)
4430 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
4431 this->insert(__p
- _M_ibegin(), __l
.begin(), __l
.size());
4436 * @brief Insert value of a string.
4437 * @param __pos1 Iterator referencing location in string to insert at.
4438 * @param __str The string to insert.
4439 * @return Reference to this string.
4440 * @throw std::length_error If new length exceeds @c max_size().
4442 * Inserts value of @a __str starting at @a __pos1. If adding
4443 * characters causes the length to exceed max_size(),
4444 * length_error is thrown. The value of the string doesn't
4445 * change if an error is thrown.
4448 insert(size_type __pos1
, const basic_string
& __str
)
4449 { return this->insert(__pos1
, __str
, size_type(0), __str
.size()); }
4452 * @brief Insert a substring.
4453 * @param __pos1 Iterator referencing location in string to insert at.
4454 * @param __str The string to insert.
4455 * @param __pos2 Start of characters in str to insert.
4456 * @param __n Number of characters to insert.
4457 * @return Reference to this string.
4458 * @throw std::length_error If new length exceeds @c max_size().
4459 * @throw std::out_of_range If @a pos1 > size() or
4460 * @a __pos2 > @a str.size().
4462 * Starting at @a pos1, insert @a __n character of @a __str
4463 * beginning with @a __pos2. If adding characters causes the
4464 * length to exceed max_size(), length_error is thrown. If @a
4465 * __pos1 is beyond the end of this string or @a __pos2 is
4466 * beyond the end of @a __str, out_of_range is thrown. The
4467 * value of the string doesn't change if an error is thrown.
4470 insert(size_type __pos1
, const basic_string
& __str
,
4471 size_type __pos2
, size_type __n
)
4472 { return this->insert(__pos1
, __str
._M_data()
4473 + __str
._M_check(__pos2
, "basic_string::insert"),
4474 __str
._M_limit(__pos2
, __n
)); }
4477 * @brief Insert a C substring.
4478 * @param __pos Iterator referencing location in string to insert at.
4479 * @param __s The C string to insert.
4480 * @param __n The number of characters to insert.
4481 * @return Reference to this string.
4482 * @throw std::length_error If new length exceeds @c max_size().
4483 * @throw std::out_of_range If @a __pos is beyond the end of this
4486 * Inserts the first @a __n characters of @a __s starting at @a
4487 * __pos. If adding characters causes the length to exceed
4488 * max_size(), length_error is thrown. If @a __pos is beyond
4489 * end(), out_of_range is thrown. The value of the string
4490 * doesn't change if an error is thrown.
4493 insert(size_type __pos
, const _CharT
* __s
, size_type __n
);
4496 * @brief Insert a C string.
4497 * @param __pos Iterator referencing location in string to insert at.
4498 * @param __s The C string to insert.
4499 * @return Reference to this string.
4500 * @throw std::length_error If new length exceeds @c max_size().
4501 * @throw std::out_of_range If @a pos is beyond the end of this
4504 * Inserts the first @a n characters of @a __s starting at @a __pos. If
4505 * adding characters causes the length to exceed max_size(),
4506 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
4507 * thrown. The value of the string doesn't change if an error is
4511 insert(size_type __pos
, const _CharT
* __s
)
4513 __glibcxx_requires_string(__s
);
4514 return this->insert(__pos
, __s
, traits_type::length(__s
));
4518 * @brief Insert multiple characters.
4519 * @param __pos Index in string to insert at.
4520 * @param __n Number of characters to insert
4521 * @param __c The character to insert.
4522 * @return Reference to this string.
4523 * @throw std::length_error If new length exceeds @c max_size().
4524 * @throw std::out_of_range If @a __pos is beyond the end of this
4527 * Inserts @a __n copies of character @a __c starting at index
4528 * @a __pos. If adding characters causes the length to exceed
4529 * max_size(), length_error is thrown. If @a __pos > length(),
4530 * out_of_range is thrown. The value of the string doesn't
4531 * change if an error is thrown.
4534 insert(size_type __pos
, size_type __n
, _CharT __c
)
4535 { return _M_replace_aux(_M_check(__pos
, "basic_string::insert"),
4536 size_type(0), __n
, __c
); }
4539 * @brief Insert one character.
4540 * @param __p Iterator referencing position in string to insert at.
4541 * @param __c The character to insert.
4542 * @return Iterator referencing newly inserted char.
4543 * @throw std::length_error If new length exceeds @c max_size().
4545 * Inserts character @a __c at position referenced by @a __p.
4546 * If adding character causes the length to exceed max_size(),
4547 * length_error is thrown. If @a __p is beyond end of string,
4548 * out_of_range is thrown. The value of the string doesn't
4549 * change if an error is thrown.
4552 insert(iterator __p
, _CharT __c
)
4554 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
4555 const size_type __pos
= __p
- _M_ibegin();
4556 _M_replace_aux(__pos
, size_type(0), size_type(1), __c
);
4557 _M_rep()->_M_set_leaked();
4558 return iterator(_M_data() + __pos
);
4561 #if __cplusplus > 201402L
4563 * @brief Insert a string_view.
4564 * @param __pos Iterator referencing position in string to insert at.
4565 * @param __svt The object convertible to string_view to insert.
4566 * @return Reference to this string.
4568 template<typename _Tp
>
4569 _If_sv
<_Tp
, basic_string
&>
4570 insert(size_type __pos
, const _Tp
& __svt
)
4572 __sv_type __sv
= __svt
;
4573 return this->insert(__pos
, __sv
.data(), __sv
.size());
4577 * @brief Insert a string_view.
4578 * @param __pos Iterator referencing position in string to insert at.
4579 * @param __svt The object convertible to string_view to insert from.
4580 * @param __pos Iterator referencing position in string_view to insert
4582 * @param __n The number of characters to insert.
4583 * @return Reference to this string.
4585 template<typename _Tp
>
4586 _If_sv
<_Tp
, basic_string
&>
4587 insert(size_type __pos1
, const _Tp
& __svt
,
4588 size_type __pos2
, size_type __n
= npos
)
4590 __sv_type __sv
= __svt
;
4591 return this->replace(__pos1
, size_type(0), __sv
.data()
4592 + __sv
._M_check(__pos2
, "basic_string::insert"),
4593 __sv
._M_limit(__pos2
, __n
));
4598 * @brief Remove characters.
4599 * @param __pos Index of first character to remove (default 0).
4600 * @param __n Number of characters to remove (default remainder).
4601 * @return Reference to this string.
4602 * @throw std::out_of_range If @a pos is beyond the end of this
4605 * Removes @a __n characters from this string starting at @a
4606 * __pos. The length of the string is reduced by @a __n. If
4607 * there are < @a __n characters to remove, the remainder of
4608 * the string is truncated. If @a __p is beyond end of string,
4609 * out_of_range is thrown. The value of the string doesn't
4610 * change if an error is thrown.
4613 erase(size_type __pos
= 0, size_type __n
= npos
)
4615 _M_mutate(_M_check(__pos
, "basic_string::erase"),
4616 _M_limit(__pos
, __n
), size_type(0));
4621 * @brief Remove one character.
4622 * @param __position Iterator referencing the character to remove.
4623 * @return iterator referencing same location after removal.
4625 * Removes the character at @a __position from this string. The value
4626 * of the string doesn't change if an error is thrown.
4629 erase(iterator __position
)
4631 _GLIBCXX_DEBUG_PEDASSERT(__position
>= _M_ibegin()
4632 && __position
< _M_iend());
4633 const size_type __pos
= __position
- _M_ibegin();
4634 _M_mutate(__pos
, size_type(1), size_type(0));
4635 _M_rep()->_M_set_leaked();
4636 return iterator(_M_data() + __pos
);
4640 * @brief Remove a range of characters.
4641 * @param __first Iterator referencing the first character to remove.
4642 * @param __last Iterator referencing the end of the range.
4643 * @return Iterator referencing location of first after removal.
4645 * Removes the characters in the range [first,last) from this string.
4646 * The value of the string doesn't change if an error is thrown.
4649 erase(iterator __first
, iterator __last
);
4651 #if __cplusplus >= 201103L
4653 * @brief Remove the last character.
4655 * The string must be non-empty.
4658 pop_back() // FIXME C++11: should be noexcept.
4660 __glibcxx_assert(!empty());
4661 erase(size() - 1, 1);
4666 * @brief Replace characters with value from another string.
4667 * @param __pos Index of first character to replace.
4668 * @param __n Number of characters to be replaced.
4669 * @param __str String to insert.
4670 * @return Reference to this string.
4671 * @throw std::out_of_range If @a pos is beyond the end of this
4673 * @throw std::length_error If new length exceeds @c max_size().
4675 * Removes the characters in the range [__pos,__pos+__n) from
4676 * this string. In place, the value of @a __str is inserted.
4677 * If @a __pos is beyond end of string, out_of_range is thrown.
4678 * If the length of the result exceeds max_size(), length_error
4679 * is thrown. The value of the string doesn't change if an
4683 replace(size_type __pos
, size_type __n
, const basic_string
& __str
)
4684 { return this->replace(__pos
, __n
, __str
._M_data(), __str
.size()); }
4687 * @brief Replace characters with value from another string.
4688 * @param __pos1 Index of first character to replace.
4689 * @param __n1 Number of characters to be replaced.
4690 * @param __str String to insert.
4691 * @param __pos2 Index of first character of str to use.
4692 * @param __n2 Number of characters from str to use.
4693 * @return Reference to this string.
4694 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
4696 * @throw std::length_error If new length exceeds @c max_size().
4698 * Removes the characters in the range [__pos1,__pos1 + n) from this
4699 * string. In place, the value of @a __str is inserted. If @a __pos is
4700 * beyond end of string, out_of_range is thrown. If the length of the
4701 * result exceeds max_size(), length_error is thrown. The value of the
4702 * string doesn't change if an error is thrown.
4705 replace(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
4706 size_type __pos2
, size_type __n2
)
4707 { return this->replace(__pos1
, __n1
, __str
._M_data()
4708 + __str
._M_check(__pos2
, "basic_string::replace"),
4709 __str
._M_limit(__pos2
, __n2
)); }
4712 * @brief Replace characters with value of a C substring.
4713 * @param __pos Index of first character to replace.
4714 * @param __n1 Number of characters to be replaced.
4715 * @param __s C string to insert.
4716 * @param __n2 Number of characters from @a s to use.
4717 * @return Reference to this string.
4718 * @throw std::out_of_range If @a pos1 > size().
4719 * @throw std::length_error If new length exceeds @c max_size().
4721 * Removes the characters in the range [__pos,__pos + __n1)
4722 * from this string. In place, the first @a __n2 characters of
4723 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
4724 * @a __pos is beyond end of string, out_of_range is thrown. If
4725 * the length of result exceeds max_size(), length_error is
4726 * thrown. The value of the string doesn't change if an error
4730 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
4734 * @brief Replace characters with value of a C string.
4735 * @param __pos Index of first character to replace.
4736 * @param __n1 Number of characters to be replaced.
4737 * @param __s C string to insert.
4738 * @return Reference to this string.
4739 * @throw std::out_of_range If @a pos > size().
4740 * @throw std::length_error If new length exceeds @c max_size().
4742 * Removes the characters in the range [__pos,__pos + __n1)
4743 * from this string. In place, the characters of @a __s are
4744 * inserted. If @a __pos is beyond end of string, out_of_range
4745 * is thrown. If the length of result exceeds max_size(),
4746 * length_error is thrown. The value of the string doesn't
4747 * change if an error is thrown.
4750 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
)
4752 __glibcxx_requires_string(__s
);
4753 return this->replace(__pos
, __n1
, __s
, traits_type::length(__s
));
4757 * @brief Replace characters with multiple characters.
4758 * @param __pos Index of first character to replace.
4759 * @param __n1 Number of characters to be replaced.
4760 * @param __n2 Number of characters to insert.
4761 * @param __c Character to insert.
4762 * @return Reference to this string.
4763 * @throw std::out_of_range If @a __pos > size().
4764 * @throw std::length_error If new length exceeds @c max_size().
4766 * Removes the characters in the range [pos,pos + n1) from this
4767 * string. In place, @a __n2 copies of @a __c are inserted.
4768 * If @a __pos is beyond end of string, out_of_range is thrown.
4769 * If the length of result exceeds max_size(), length_error is
4770 * thrown. The value of the string doesn't change if an error
4774 replace(size_type __pos
, size_type __n1
, size_type __n2
, _CharT __c
)
4775 { return _M_replace_aux(_M_check(__pos
, "basic_string::replace"),
4776 _M_limit(__pos
, __n1
), __n2
, __c
); }
4779 * @brief Replace range of characters with string.
4780 * @param __i1 Iterator referencing start of range to replace.
4781 * @param __i2 Iterator referencing end of range to replace.
4782 * @param __str String value to insert.
4783 * @return Reference to this string.
4784 * @throw std::length_error If new length exceeds @c max_size().
4786 * Removes the characters in the range [__i1,__i2). In place,
4787 * the value of @a __str is inserted. If the length of result
4788 * exceeds max_size(), length_error is thrown. The value of
4789 * the string doesn't change if an error is thrown.
4792 replace(iterator __i1
, iterator __i2
, const basic_string
& __str
)
4793 { return this->replace(__i1
, __i2
, __str
._M_data(), __str
.size()); }
4796 * @brief Replace range of characters with C substring.
4797 * @param __i1 Iterator referencing start of range to replace.
4798 * @param __i2 Iterator referencing end of range to replace.
4799 * @param __s C string value to insert.
4800 * @param __n Number of characters from s to insert.
4801 * @return Reference to this string.
4802 * @throw std::length_error If new length exceeds @c max_size().
4804 * Removes the characters in the range [__i1,__i2). In place,
4805 * the first @a __n characters of @a __s are inserted. If the
4806 * length of result exceeds max_size(), length_error is thrown.
4807 * The value of the string doesn't change if an error is
4811 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
, size_type __n
)
4813 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4814 && __i2
<= _M_iend());
4815 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
, __s
, __n
);
4819 * @brief Replace range of characters with C string.
4820 * @param __i1 Iterator referencing start of range to replace.
4821 * @param __i2 Iterator referencing end of range to replace.
4822 * @param __s C string value to insert.
4823 * @return Reference to this string.
4824 * @throw std::length_error If new length exceeds @c max_size().
4826 * Removes the characters in the range [__i1,__i2). In place,
4827 * the characters of @a __s are inserted. If the length of
4828 * result exceeds max_size(), length_error is thrown. The
4829 * value of the string doesn't change if an error is thrown.
4832 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
)
4834 __glibcxx_requires_string(__s
);
4835 return this->replace(__i1
, __i2
, __s
, traits_type::length(__s
));
4839 * @brief Replace range of characters with multiple characters
4840 * @param __i1 Iterator referencing start of range to replace.
4841 * @param __i2 Iterator referencing end of range to replace.
4842 * @param __n Number of characters to insert.
4843 * @param __c Character to insert.
4844 * @return Reference to this string.
4845 * @throw std::length_error If new length exceeds @c max_size().
4847 * Removes the characters in the range [__i1,__i2). In place,
4848 * @a __n copies of @a __c are inserted. If the length of
4849 * result exceeds max_size(), length_error is thrown. The
4850 * value of the string doesn't change if an error is thrown.
4853 replace(iterator __i1
, iterator __i2
, size_type __n
, _CharT __c
)
4855 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4856 && __i2
<= _M_iend());
4857 return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __c
);
4861 * @brief Replace range of characters with range.
4862 * @param __i1 Iterator referencing start of range to replace.
4863 * @param __i2 Iterator referencing end of range to replace.
4864 * @param __k1 Iterator referencing start of range to insert.
4865 * @param __k2 Iterator referencing end of range to insert.
4866 * @return Reference to this string.
4867 * @throw std::length_error If new length exceeds @c max_size().
4869 * Removes the characters in the range [__i1,__i2). In place,
4870 * characters in the range [__k1,__k2) are inserted. If the
4871 * length of result exceeds max_size(), length_error is thrown.
4872 * The value of the string doesn't change if an error is
4875 template<class _InputIterator
>
4877 replace(iterator __i1
, iterator __i2
,
4878 _InputIterator __k1
, _InputIterator __k2
)
4880 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4881 && __i2
<= _M_iend());
4882 __glibcxx_requires_valid_range(__k1
, __k2
);
4883 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
4884 return _M_replace_dispatch(__i1
, __i2
, __k1
, __k2
, _Integral());
4887 // Specializations for the common case of pointer and iterator:
4888 // useful to avoid the overhead of temporary buffering in _M_replace.
4890 replace(iterator __i1
, iterator __i2
, _CharT
* __k1
, _CharT
* __k2
)
4892 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4893 && __i2
<= _M_iend());
4894 __glibcxx_requires_valid_range(__k1
, __k2
);
4895 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4900 replace(iterator __i1
, iterator __i2
,
4901 const _CharT
* __k1
, const _CharT
* __k2
)
4903 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4904 && __i2
<= _M_iend());
4905 __glibcxx_requires_valid_range(__k1
, __k2
);
4906 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4911 replace(iterator __i1
, iterator __i2
, iterator __k1
, iterator __k2
)
4913 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4914 && __i2
<= _M_iend());
4915 __glibcxx_requires_valid_range(__k1
, __k2
);
4916 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4917 __k1
.base(), __k2
- __k1
);
4921 replace(iterator __i1
, iterator __i2
,
4922 const_iterator __k1
, const_iterator __k2
)
4924 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4925 && __i2
<= _M_iend());
4926 __glibcxx_requires_valid_range(__k1
, __k2
);
4927 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4928 __k1
.base(), __k2
- __k1
);
4931 #if __cplusplus >= 201103L
4933 * @brief Replace range of characters with initializer_list.
4934 * @param __i1 Iterator referencing start of range to replace.
4935 * @param __i2 Iterator referencing end of range to replace.
4936 * @param __l The initializer_list of characters to insert.
4937 * @return Reference to this string.
4938 * @throw std::length_error If new length exceeds @c max_size().
4940 * Removes the characters in the range [__i1,__i2). In place,
4941 * characters in the range [__k1,__k2) are inserted. If the
4942 * length of result exceeds max_size(), length_error is thrown.
4943 * The value of the string doesn't change if an error is
4946 basic_string
& replace(iterator __i1
, iterator __i2
,
4947 initializer_list
<_CharT
> __l
)
4948 { return this->replace(__i1
, __i2
, __l
.begin(), __l
.end()); }
4951 #if __cplusplus > 201402L
4953 * @brief Replace range of characters with string_view.
4954 * @param __pos The position to replace at.
4955 * @param __n The number of characters to replace.
4956 * @param __svt The object convertible to string_view to insert.
4957 * @return Reference to this string.
4959 template<typename _Tp
>
4960 _If_sv
<_Tp
, basic_string
&>
4961 replace(size_type __pos
, size_type __n
, const _Tp
& __svt
)
4963 __sv_type __sv
= __svt
;
4964 return this->replace(__pos
, __n
, __sv
.data(), __sv
.size());
4968 * @brief Replace range of characters with string_view.
4969 * @param __pos1 The position to replace at.
4970 * @param __n1 The number of characters to replace.
4971 * @param __svt The object convertible to string_view to insert from.
4972 * @param __pos2 The position in the string_view to insert from.
4973 * @param __n2 The number of characters to insert.
4974 * @return Reference to this string.
4976 template<typename _Tp
>
4977 _If_sv
<_Tp
, basic_string
&>
4978 replace(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
4979 size_type __pos2
, size_type __n2
= npos
)
4981 __sv_type __sv
= __svt
;
4982 return this->replace(__pos1
, __n1
,
4983 __sv
.data() + __sv
._M_check(__pos2
, "basic_string::replace"),
4984 __sv
._M_limit(__pos2
, __n2
));
4988 * @brief Replace range of characters with string_view.
4989 * @param __i1 An iterator referencing the start position
4991 * @param __i2 An iterator referencing the end position
4993 * @param __svt The object convertible to string_view to insert from.
4994 * @return Reference to this string.
4996 template<typename _Tp
>
4997 _If_sv
<_Tp
, basic_string
&>
4998 replace(const_iterator __i1
, const_iterator __i2
, const _Tp
& __svt
)
5000 __sv_type __sv
= __svt
;
5001 return this->replace(__i1
- begin(), __i2
- __i1
, __sv
);
5006 template<class _Integer
>
5008 _M_replace_dispatch(iterator __i1
, iterator __i2
, _Integer __n
,
5009 _Integer __val
, __true_type
)
5010 { return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __val
); }
5012 template<class _InputIterator
>
5014 _M_replace_dispatch(iterator __i1
, iterator __i2
, _InputIterator __k1
,
5015 _InputIterator __k2
, __false_type
);
5018 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
5022 _M_replace_safe(size_type __pos1
, size_type __n1
, const _CharT
* __s
,
5025 // _S_construct_aux is used to implement the 21.3.1 para 15 which
5026 // requires special behaviour if _InIter is an integral type
5027 template<class _InIterator
>
5029 _S_construct_aux(_InIterator __beg
, _InIterator __end
,
5030 const _Alloc
& __a
, __false_type
)
5032 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
5033 return _S_construct(__beg
, __end
, __a
, _Tag());
5036 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5037 // 438. Ambiguity in the "do the right thing" clause
5038 template<class _Integer
>
5040 _S_construct_aux(_Integer __beg
, _Integer __end
,
5041 const _Alloc
& __a
, __true_type
)
5042 { return _S_construct_aux_2(static_cast<size_type
>(__beg
),
5046 _S_construct_aux_2(size_type __req
, _CharT __c
, const _Alloc
& __a
)
5047 { return _S_construct(__req
, __c
, __a
); }
5049 template<class _InIterator
>
5051 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
)
5053 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
5054 return _S_construct_aux(__beg
, __end
, __a
, _Integral());
5057 // For Input Iterators, used in istreambuf_iterators, etc.
5058 template<class _InIterator
>
5060 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
,
5061 input_iterator_tag
);
5063 // For forward_iterators up to random_access_iterators, used for
5064 // string::iterator, _CharT*, etc.
5065 template<class _FwdIterator
>
5067 _S_construct(_FwdIterator __beg
, _FwdIterator __end
, const _Alloc
& __a
,
5068 forward_iterator_tag
);
5071 _S_construct(size_type __req
, _CharT __c
, const _Alloc
& __a
);
5076 * @brief Copy substring into C string.
5077 * @param __s C string to copy value into.
5078 * @param __n Number of characters to copy.
5079 * @param __pos Index of first character to copy.
5080 * @return Number of characters actually copied
5081 * @throw std::out_of_range If __pos > size().
5083 * Copies up to @a __n characters starting at @a __pos into the
5084 * C string @a __s. If @a __pos is %greater than size(),
5085 * out_of_range is thrown.
5088 copy(_CharT
* __s
, size_type __n
, size_type __pos
= 0) const;
5091 * @brief Swap contents with another string.
5092 * @param __s String to swap with.
5094 * Exchanges the contents of this string with that of @a __s in constant
5097 // PR 58265, this should be noexcept.
5099 swap(basic_string
& __s
);
5101 // String operations:
5103 * @brief Return const pointer to null-terminated contents.
5105 * This is a handle to internal data. Do not modify or dire things may
5109 c_str() const _GLIBCXX_NOEXCEPT
5110 { return _M_data(); }
5113 * @brief Return const pointer to contents.
5115 * This is a pointer to internal data. It is undefined to modify
5116 * the contents through the returned pointer. To get a pointer that
5117 * allows modifying the contents use @c &str[0] instead,
5118 * (or in C++17 the non-const @c str.data() overload).
5121 data() const _GLIBCXX_NOEXCEPT
5122 { return _M_data(); }
5124 #if __cplusplus > 201402L
5126 * @brief Return non-const pointer to contents.
5128 * This is a pointer to the character sequence held by the string.
5129 * Modifying the characters in the sequence is allowed.
5133 { return _M_data(); }
5137 * @brief Return copy of allocator used to construct this string.
5140 get_allocator() const _GLIBCXX_NOEXCEPT
5141 { return _M_dataplus
; }
5144 * @brief Find position of a C substring.
5145 * @param __s C string to locate.
5146 * @param __pos Index of character to search from.
5147 * @param __n Number of characters from @a s to search for.
5148 * @return Index of start of first occurrence.
5150 * Starting from @a __pos, searches forward for the first @a
5151 * __n characters in @a __s within this string. If found,
5152 * returns the index where it begins. If not found, returns
5156 find(const _CharT
* __s
, size_type __pos
, size_type __n
) const
5160 * @brief Find position of a string.
5161 * @param __str String to locate.
5162 * @param __pos Index of character to search from (default 0).
5163 * @return Index of start of first occurrence.
5165 * Starting from @a __pos, searches forward for value of @a __str within
5166 * this string. If found, returns the index where it begins. If not
5167 * found, returns npos.
5170 find(const basic_string
& __str
, size_type __pos
= 0) const
5172 { return this->find(__str
.data(), __pos
, __str
.size()); }
5175 * @brief Find position of a C string.
5176 * @param __s C string to locate.
5177 * @param __pos Index of character to search from (default 0).
5178 * @return Index of start of first occurrence.
5180 * Starting from @a __pos, searches forward for the value of @a
5181 * __s within this string. If found, returns the index where
5182 * it begins. If not found, returns npos.
5185 find(const _CharT
* __s
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
5187 __glibcxx_requires_string(__s
);
5188 return this->find(__s
, __pos
, traits_type::length(__s
));
5192 * @brief Find position of a character.
5193 * @param __c Character to locate.
5194 * @param __pos Index of character to search from (default 0).
5195 * @return Index of first occurrence.
5197 * Starting from @a __pos, searches forward for @a __c within
5198 * this string. If found, returns the index where it was
5199 * found. If not found, returns npos.
5202 find(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
;
5204 #if __cplusplus > 201402L
5206 * @brief Find position of a string_view.
5207 * @param __svt The object convertible to string_view to locate.
5208 * @param __pos Index of character to search from (default 0).
5209 * @return Index of start of first occurrence.
5211 template<typename _Tp
>
5212 _If_sv
<_Tp
, size_type
>
5213 find(const _Tp
& __svt
, size_type __pos
= 0) const
5214 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5216 __sv_type __sv
= __svt
;
5217 return this->find(__sv
.data(), __pos
, __sv
.size());
5222 * @brief Find last position of a string.
5223 * @param __str String to locate.
5224 * @param __pos Index of character to search back from (default end).
5225 * @return Index of start of last occurrence.
5227 * Starting from @a __pos, searches backward for value of @a
5228 * __str within this string. If found, returns the index where
5229 * it begins. If not found, returns npos.
5232 rfind(const basic_string
& __str
, size_type __pos
= npos
) const
5234 { return this->rfind(__str
.data(), __pos
, __str
.size()); }
5237 * @brief Find last position of a C substring.
5238 * @param __s C string to locate.
5239 * @param __pos Index of character to search back from.
5240 * @param __n Number of characters from s to search for.
5241 * @return Index of start of last occurrence.
5243 * Starting from @a __pos, searches backward for the first @a
5244 * __n characters in @a __s within this string. If found,
5245 * returns the index where it begins. If not found, returns
5249 rfind(const _CharT
* __s
, size_type __pos
, size_type __n
) const
5253 * @brief Find last position of a C string.
5254 * @param __s C string to locate.
5255 * @param __pos Index of character to start search at (default end).
5256 * @return Index of start of last occurrence.
5258 * Starting from @a __pos, searches backward for the value of
5259 * @a __s within this string. If found, returns the index
5260 * where it begins. If not found, returns npos.
5263 rfind(const _CharT
* __s
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
5265 __glibcxx_requires_string(__s
);
5266 return this->rfind(__s
, __pos
, traits_type::length(__s
));
5270 * @brief Find last position of a character.
5271 * @param __c Character to locate.
5272 * @param __pos Index of character to search back from (default end).
5273 * @return Index of last occurrence.
5275 * Starting from @a __pos, searches backward for @a __c within
5276 * this string. If found, returns the index where it was
5277 * found. If not found, returns npos.
5280 rfind(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
;
5282 #if __cplusplus > 201402L
5284 * @brief Find last position of a string_view.
5285 * @param __svt The object convertible to string_view to locate.
5286 * @param __pos Index of character to search back from (default end).
5287 * @return Index of start of last occurrence.
5289 template<typename _Tp
>
5290 _If_sv
<_Tp
, size_type
>
5291 rfind(const _Tp
& __svt
, size_type __pos
= npos
) const
5292 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5294 __sv_type __sv
= __svt
;
5295 return this->rfind(__sv
.data(), __pos
, __sv
.size());
5300 * @brief Find position of a character of string.
5301 * @param __str String containing characters to locate.
5302 * @param __pos Index of character to search from (default 0).
5303 * @return Index of first occurrence.
5305 * Starting from @a __pos, searches forward for one of the
5306 * characters of @a __str within this string. If found,
5307 * returns the index where it was found. If not found, returns
5311 find_first_of(const basic_string
& __str
, size_type __pos
= 0) const
5313 { return this->find_first_of(__str
.data(), __pos
, __str
.size()); }
5316 * @brief Find position of a character of C substring.
5317 * @param __s String containing characters to locate.
5318 * @param __pos Index of character to search from.
5319 * @param __n Number of characters from s to search for.
5320 * @return Index of first occurrence.
5322 * Starting from @a __pos, searches forward for one of the
5323 * first @a __n characters of @a __s within this string. If
5324 * found, returns the index where it was found. If not found,
5328 find_first_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
5332 * @brief Find position of a character of C string.
5333 * @param __s String containing characters to locate.
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 one of the
5338 * characters of @a __s within this string. If found, returns
5339 * the index where it was found. If not found, returns npos.
5342 find_first_of(const _CharT
* __s
, size_type __pos
= 0) const
5345 __glibcxx_requires_string(__s
);
5346 return this->find_first_of(__s
, __pos
, traits_type::length(__s
));
5350 * @brief Find position of a character.
5351 * @param __c Character to locate.
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 the character
5356 * @a __c within this string. If found, returns the index
5357 * where it was found. If not found, returns npos.
5359 * Note: equivalent to find(__c, __pos).
5362 find_first_of(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
5363 { return this->find(__c
, __pos
); }
5365 #if __cplusplus > 201402L
5367 * @brief Find position of a character of a string_view.
5368 * @param __svt An object convertible to string_view containing
5369 * characters to locate.
5370 * @param __pos Index of character to search from (default 0).
5371 * @return Index of first occurrence.
5373 template<typename _Tp
>
5374 _If_sv
<_Tp
, size_type
>
5375 find_first_of(const _Tp
& __svt
, size_type __pos
= 0) const
5376 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5378 __sv_type __sv
= __svt
;
5379 return this->find_first_of(__sv
.data(), __pos
, __sv
.size());
5384 * @brief Find last position of a character of string.
5385 * @param __str String containing characters to locate.
5386 * @param __pos Index of character to search back from (default end).
5387 * @return Index of last occurrence.
5389 * Starting from @a __pos, searches backward for one of the
5390 * characters of @a __str within this string. If found,
5391 * returns the index where it was found. If not found, returns
5395 find_last_of(const basic_string
& __str
, size_type __pos
= npos
) const
5397 { return this->find_last_of(__str
.data(), __pos
, __str
.size()); }
5400 * @brief Find last position of a character of C substring.
5401 * @param __s C string containing characters to locate.
5402 * @param __pos Index of character to search back from.
5403 * @param __n Number of characters from s to search for.
5404 * @return Index of last occurrence.
5406 * Starting from @a __pos, searches backward for one of the
5407 * first @a __n characters of @a __s within this string. If
5408 * found, returns the index where it was found. If not found,
5412 find_last_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
5416 * @brief Find last position of a character of C string.
5417 * @param __s C string containing characters to locate.
5418 * @param __pos Index of character to search back from (default end).
5419 * @return Index of last occurrence.
5421 * Starting from @a __pos, searches backward for one of the
5422 * characters of @a __s within this string. If found, returns
5423 * the index where it was found. If not found, returns npos.
5426 find_last_of(const _CharT
* __s
, size_type __pos
= npos
) const
5429 __glibcxx_requires_string(__s
);
5430 return this->find_last_of(__s
, __pos
, traits_type::length(__s
));
5434 * @brief Find last position of a character.
5435 * @param __c Character to locate.
5436 * @param __pos Index of character to search back from (default end).
5437 * @return Index of last occurrence.
5439 * Starting from @a __pos, searches backward for @a __c within
5440 * this string. If found, returns the index where it was
5441 * found. If not found, returns npos.
5443 * Note: equivalent to rfind(__c, __pos).
5446 find_last_of(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
5447 { return this->rfind(__c
, __pos
); }
5449 #if __cplusplus > 201402L
5451 * @brief Find last position of a character of string.
5452 * @param __svt An object convertible to string_view containing
5453 * characters to locate.
5454 * @param __pos Index of character to search back from (default end).
5455 * @return Index of last occurrence.
5457 template<typename _Tp
>
5458 _If_sv
<_Tp
, size_type
>
5459 find_last_of(const _Tp
& __svt
, size_type __pos
= npos
) const
5460 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5462 __sv_type __sv
= __svt
;
5463 return this->find_last_of(__sv
.data(), __pos
, __sv
.size());
5468 * @brief Find position of a character not in string.
5469 * @param __str String containing characters to avoid.
5470 * @param __pos Index of character to search from (default 0).
5471 * @return Index of first occurrence.
5473 * Starting from @a __pos, searches forward for a character not contained
5474 * in @a __str within this string. If found, returns the index where it
5475 * was found. If not found, returns npos.
5478 find_first_not_of(const basic_string
& __str
, size_type __pos
= 0) const
5480 { return this->find_first_not_of(__str
.data(), __pos
, __str
.size()); }
5483 * @brief Find position of a character not in C substring.
5484 * @param __s C string containing characters to avoid.
5485 * @param __pos Index of character to search from.
5486 * @param __n Number of characters from __s to consider.
5487 * @return Index of first occurrence.
5489 * Starting from @a __pos, searches forward for a character not
5490 * contained in the first @a __n characters of @a __s within
5491 * this string. If found, returns the index where it was
5492 * found. If not found, returns npos.
5495 find_first_not_of(const _CharT
* __s
, size_type __pos
,
5496 size_type __n
) const _GLIBCXX_NOEXCEPT
;
5499 * @brief Find position of a character not in C string.
5500 * @param __s C string containing characters to avoid.
5501 * @param __pos Index of character to search from (default 0).
5502 * @return Index of first occurrence.
5504 * Starting from @a __pos, searches forward for a character not
5505 * contained in @a __s within this string. If found, returns
5506 * the index where it was found. If not found, returns npos.
5509 find_first_not_of(const _CharT
* __s
, size_type __pos
= 0) const
5512 __glibcxx_requires_string(__s
);
5513 return this->find_first_not_of(__s
, __pos
, traits_type::length(__s
));
5517 * @brief Find position of a different character.
5518 * @param __c Character to avoid.
5519 * @param __pos Index of character to search from (default 0).
5520 * @return Index of first occurrence.
5522 * Starting from @a __pos, searches forward for a character
5523 * other than @a __c within this string. If found, returns the
5524 * index where it was found. If not found, returns npos.
5527 find_first_not_of(_CharT __c
, size_type __pos
= 0) const
5530 #if __cplusplus > 201402L
5532 * @brief Find position of a character not in a string_view.
5533 * @param __svt An object convertible to string_view containing
5534 * characters to avoid.
5535 * @param __pos Index of character to search from (default 0).
5536 * @return Index of first occurrence.
5538 template<typename _Tp
>
5539 _If_sv
<_Tp
, size_type
>
5540 find_first_not_of(const _Tp
& __svt
, size_type __pos
= 0) const
5541 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5543 __sv_type __sv
= __svt
;
5544 return this->find_first_not_of(__sv
.data(), __pos
, __sv
.size());
5549 * @brief Find last position of a character not in string.
5550 * @param __str String containing characters to avoid.
5551 * @param __pos Index of character to search back from (default end).
5552 * @return Index of last occurrence.
5554 * Starting from @a __pos, searches backward for a character
5555 * not contained in @a __str within this string. If found,
5556 * returns the index where it was found. If not found, returns
5560 find_last_not_of(const basic_string
& __str
, size_type __pos
= npos
) const
5562 { return this->find_last_not_of(__str
.data(), __pos
, __str
.size()); }
5565 * @brief Find last position of a character not in C substring.
5566 * @param __s C string containing characters to avoid.
5567 * @param __pos Index of character to search back from.
5568 * @param __n Number of characters from s to consider.
5569 * @return Index of last occurrence.
5571 * Starting from @a __pos, searches backward for a character not
5572 * contained in the first @a __n characters of @a __s within this string.
5573 * If found, returns the index where it was found. If not found,
5577 find_last_not_of(const _CharT
* __s
, size_type __pos
,
5578 size_type __n
) const _GLIBCXX_NOEXCEPT
;
5580 * @brief Find last position of a character not in C string.
5581 * @param __s C string containing characters to avoid.
5582 * @param __pos Index of character to search back from (default end).
5583 * @return Index of last occurrence.
5585 * Starting from @a __pos, searches backward for a character
5586 * not contained in @a __s within this string. If found,
5587 * returns the index where it was found. If not found, returns
5591 find_last_not_of(const _CharT
* __s
, size_type __pos
= npos
) const
5594 __glibcxx_requires_string(__s
);
5595 return this->find_last_not_of(__s
, __pos
, traits_type::length(__s
));
5599 * @brief Find last position of a different character.
5600 * @param __c Character to avoid.
5601 * @param __pos Index of character to search back from (default end).
5602 * @return Index of last occurrence.
5604 * Starting from @a __pos, searches backward for a character other than
5605 * @a __c within this string. If found, returns the index where it was
5606 * found. If not found, returns npos.
5609 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const
5612 #if __cplusplus > 201402L
5614 * @brief Find last position of a character not in a string_view.
5615 * @param __svt An object convertible to string_view containing
5616 * characters to avoid.
5617 * @param __pos Index of character to search back from (default end).
5618 * @return Index of last occurrence.
5620 template<typename _Tp
>
5621 _If_sv
<_Tp
, size_type
>
5622 find_last_not_of(const _Tp
& __svt
, size_type __pos
= npos
) const
5623 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5625 __sv_type __sv
= __svt
;
5626 return this->find_last_not_of(__sv
.data(), __pos
, __sv
.size());
5631 * @brief Get a substring.
5632 * @param __pos Index of first character (default 0).
5633 * @param __n Number of characters in substring (default remainder).
5634 * @return The new string.
5635 * @throw std::out_of_range If __pos > size().
5637 * Construct and return a new string using the @a __n
5638 * characters starting at @a __pos. If the string is too
5639 * short, use the remainder of the characters. If @a __pos is
5640 * beyond the end of the string, out_of_range is thrown.
5643 substr(size_type __pos
= 0, size_type __n
= npos
) const
5644 { return basic_string(*this,
5645 _M_check(__pos
, "basic_string::substr"), __n
); }
5648 * @brief Compare to a string.
5649 * @param __str String to compare against.
5650 * @return Integer < 0, 0, or > 0.
5652 * Returns an integer < 0 if this string is ordered before @a
5653 * __str, 0 if their values are equivalent, or > 0 if this
5654 * string is ordered after @a __str. Determines the effective
5655 * length rlen of the strings to compare as the smallest of
5656 * size() and str.size(). The function then compares the two
5657 * strings by calling traits::compare(data(), str.data(),rlen).
5658 * If the result of the comparison is nonzero returns it,
5659 * otherwise the shorter one is ordered first.
5662 compare(const basic_string
& __str
) const
5664 const size_type __size
= this->size();
5665 const size_type __osize
= __str
.size();
5666 const size_type __len
= std::min(__size
, __osize
);
5668 int __r
= traits_type::compare(_M_data(), __str
.data(), __len
);
5670 __r
= _S_compare(__size
, __osize
);
5674 #if __cplusplus > 201402L
5676 * @brief Compare to a string_view.
5677 * @param __svt An object convertible to string_view to compare against.
5678 * @return Integer < 0, 0, or > 0.
5680 template<typename _Tp
>
5682 compare(const _Tp
& __svt
) const
5683 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5685 __sv_type __sv
= __svt
;
5686 const size_type __size
= this->size();
5687 const size_type __osize
= __sv
.size();
5688 const size_type __len
= std::min(__size
, __osize
);
5690 int __r
= traits_type::compare(_M_data(), __sv
.data(), __len
);
5692 __r
= _S_compare(__size
, __osize
);
5697 * @brief Compare to a string_view.
5698 * @param __pos A position in the string to start comparing from.
5699 * @param __n The number of characters to compare.
5700 * @param __svt An object convertible to string_view to compare
5702 * @return Integer < 0, 0, or > 0.
5704 template<typename _Tp
>
5706 compare(size_type __pos
, size_type __n
, const _Tp
& __svt
) const
5707 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5709 __sv_type __sv
= __svt
;
5710 return __sv_type(*this).substr(__pos
, __n
).compare(__sv
);
5714 * @brief Compare to a string_view.
5715 * @param __pos1 A position in the string to start comparing from.
5716 * @param __n1 The number of characters to compare.
5717 * @param __svt An object convertible to string_view to compare
5719 * @param __pos2 A position in the string_view to start comparing from.
5720 * @param __n2 The number of characters to compare.
5721 * @return Integer < 0, 0, or > 0.
5723 template<typename _Tp
>
5725 compare(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
5726 size_type __pos2
, size_type __n2
= npos
) const
5727 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5729 __sv_type __sv
= __svt
;
5730 return __sv_type(*this)
5731 .substr(__pos1
, __n1
).compare(__sv
.substr(__pos2
, __n2
));
5736 * @brief Compare substring to a string.
5737 * @param __pos Index of first character of substring.
5738 * @param __n Number of characters in substring.
5739 * @param __str String to compare against.
5740 * @return Integer < 0, 0, or > 0.
5742 * Form the substring of this string from the @a __n characters
5743 * starting at @a __pos. Returns an integer < 0 if the
5744 * substring is ordered before @a __str, 0 if their values are
5745 * equivalent, or > 0 if the substring is ordered after @a
5746 * __str. Determines the effective length rlen of the strings
5747 * to compare as the smallest of the length of the substring
5748 * and @a __str.size(). The function then compares the two
5749 * strings by calling
5750 * traits::compare(substring.data(),str.data(),rlen). If the
5751 * result of the comparison is nonzero returns it, otherwise
5752 * the shorter one is ordered first.
5755 compare(size_type __pos
, size_type __n
, const basic_string
& __str
) const;
5758 * @brief Compare substring to a substring.
5759 * @param __pos1 Index of first character of substring.
5760 * @param __n1 Number of characters in substring.
5761 * @param __str String to compare against.
5762 * @param __pos2 Index of first character of substring of str.
5763 * @param __n2 Number of characters in substring of str.
5764 * @return Integer < 0, 0, or > 0.
5766 * Form the substring of this string from the @a __n1
5767 * characters starting at @a __pos1. Form the substring of @a
5768 * __str from the @a __n2 characters starting at @a __pos2.
5769 * Returns an integer < 0 if this substring is ordered before
5770 * the substring of @a __str, 0 if their values are equivalent,
5771 * or > 0 if this substring is ordered after the substring of
5772 * @a __str. Determines the effective length rlen of the
5773 * strings to compare as the smallest of the lengths of the
5774 * substrings. The function then compares the two strings by
5776 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
5777 * If the result of the comparison is nonzero returns it,
5778 * otherwise the shorter one is ordered first.
5781 compare(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
5782 size_type __pos2
, size_type __n2
) const;
5785 * @brief Compare to a C string.
5786 * @param __s C string to compare against.
5787 * @return Integer < 0, 0, or > 0.
5789 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
5790 * their values are equivalent, or > 0 if this string is ordered after
5791 * @a __s. Determines the effective length rlen of the strings to
5792 * compare as the smallest of size() and the length of a string
5793 * constructed from @a __s. The function then compares the two strings
5794 * by calling traits::compare(data(),s,rlen). If the result of the
5795 * comparison is nonzero returns it, otherwise the shorter one is
5799 compare(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
;
5801 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5802 // 5 String::compare specification questionable
5804 * @brief Compare substring to a C string.
5805 * @param __pos Index of first character of substring.
5806 * @param __n1 Number of characters in substring.
5807 * @param __s C string to compare against.
5808 * @return Integer < 0, 0, or > 0.
5810 * Form the substring of this string from the @a __n1
5811 * characters starting at @a pos. Returns an integer < 0 if
5812 * the substring is ordered before @a __s, 0 if their values
5813 * are equivalent, or > 0 if the substring is ordered after @a
5814 * __s. Determines the effective length rlen of the strings to
5815 * compare as the smallest of the length of the substring and
5816 * the length of a string constructed from @a __s. The
5817 * function then compares the two string by calling
5818 * traits::compare(substring.data(),__s,rlen). If the result of
5819 * the comparison is nonzero returns it, otherwise the shorter
5820 * one is ordered first.
5823 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
) const;
5826 * @brief Compare substring against a character %array.
5827 * @param __pos Index of first character of substring.
5828 * @param __n1 Number of characters in substring.
5829 * @param __s character %array to compare against.
5830 * @param __n2 Number of characters of s.
5831 * @return Integer < 0, 0, or > 0.
5833 * Form the substring of this string from the @a __n1
5834 * characters starting at @a __pos. Form a string from the
5835 * first @a __n2 characters of @a __s. Returns an integer < 0
5836 * if this substring is ordered before the string from @a __s,
5837 * 0 if their values are equivalent, or > 0 if this substring
5838 * is ordered after the string from @a __s. Determines the
5839 * effective length rlen of the strings to compare as the
5840 * smallest of the length of the substring and @a __n2. The
5841 * function then compares the two strings by calling
5842 * traits::compare(substring.data(),s,rlen). If the result of
5843 * the comparison is nonzero returns it, otherwise the shorter
5844 * one is ordered first.
5846 * NB: s must have at least n2 characters, '\\0' has
5847 * no special meaning.
5850 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
,
5851 size_type __n2
) const;
5853 # ifdef _GLIBCXX_TM_TS_INTERNAL
5855 ::_txnal_cow_string_C1_for_exceptions(void* that
, const char* s
,
5858 ::_txnal_cow_string_c_str(const void *that
);
5860 ::_txnal_cow_string_D1(void *that
);
5862 ::_txnal_cow_string_D1_commit(void *that
);
5865 #endif // !_GLIBCXX_USE_CXX11_ABI
5867 #if __cpp_deduction_guides >= 201606
5868 _GLIBCXX_BEGIN_NAMESPACE_CXX11
5869 template<typename _InputIterator
, typename _CharT
5870 = typename iterator_traits
<_InputIterator
>::value_type
,
5871 typename _Allocator
= allocator
<_CharT
>,
5872 typename
= _RequireInputIter
<_InputIterator
>,
5873 typename
= _RequireAllocator
<_Allocator
>>
5874 basic_string(_InputIterator
, _InputIterator
, _Allocator
= _Allocator())
5875 -> basic_string
<_CharT
, char_traits
<_CharT
>, _Allocator
>;
5876 _GLIBCXX_END_NAMESPACE_CXX11
5881 * @brief Concatenate two strings.
5882 * @param __lhs First string.
5883 * @param __rhs Last string.
5884 * @return New string with value of @a __lhs followed by @a __rhs.
5886 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5887 basic_string
<_CharT
, _Traits
, _Alloc
>
5888 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5889 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5891 basic_string
<_CharT
, _Traits
, _Alloc
> __str(__lhs
);
5892 __str
.append(__rhs
);
5897 * @brief Concatenate C string and string.
5898 * @param __lhs First string.
5899 * @param __rhs Last string.
5900 * @return New string with value of @a __lhs followed by @a __rhs.
5902 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5903 basic_string
<_CharT
,_Traits
,_Alloc
>
5904 operator+(const _CharT
* __lhs
,
5905 const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
);
5908 * @brief Concatenate character and string.
5909 * @param __lhs First string.
5910 * @param __rhs Last string.
5911 * @return New string with @a __lhs followed by @a __rhs.
5913 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5914 basic_string
<_CharT
,_Traits
,_Alloc
>
5915 operator+(_CharT __lhs
, const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
);
5918 * @brief Concatenate string and C string.
5919 * @param __lhs First string.
5920 * @param __rhs Last string.
5921 * @return New string with @a __lhs followed by @a __rhs.
5923 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5924 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5925 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5926 const _CharT
* __rhs
)
5928 basic_string
<_CharT
, _Traits
, _Alloc
> __str(__lhs
);
5929 __str
.append(__rhs
);
5934 * @brief Concatenate string and character.
5935 * @param __lhs First string.
5936 * @param __rhs Last string.
5937 * @return New string with @a __lhs followed by @a __rhs.
5939 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5940 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5941 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
, _CharT __rhs
)
5943 typedef basic_string
<_CharT
, _Traits
, _Alloc
> __string_type
;
5944 typedef typename
__string_type::size_type __size_type
;
5945 __string_type
__str(__lhs
);
5946 __str
.append(__size_type(1), __rhs
);
5950 #if __cplusplus >= 201103L
5951 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5952 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5953 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
5954 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5955 { return std::move(__lhs
.append(__rhs
)); }
5957 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5958 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5959 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5960 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
5961 { return std::move(__rhs
.insert(0, __lhs
)); }
5963 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5964 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5965 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
5966 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
5968 const auto __size
= __lhs
.size() + __rhs
.size();
5969 const bool __cond
= (__size
> __lhs
.capacity()
5970 && __size
<= __rhs
.capacity());
5971 return __cond
? std::move(__rhs
.insert(0, __lhs
))
5972 : std::move(__lhs
.append(__rhs
));
5975 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5976 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5977 operator+(const _CharT
* __lhs
,
5978 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
5979 { return std::move(__rhs
.insert(0, __lhs
)); }
5981 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5982 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5983 operator+(_CharT __lhs
,
5984 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
5985 { return std::move(__rhs
.insert(0, 1, __lhs
)); }
5987 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5988 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5989 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
5990 const _CharT
* __rhs
)
5991 { return std::move(__lhs
.append(__rhs
)); }
5993 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5994 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5995 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
5997 { return std::move(__lhs
.append(1, __rhs
)); }
6002 * @brief Test equivalence of two strings.
6003 * @param __lhs First string.
6004 * @param __rhs Second string.
6005 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
6007 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6009 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6010 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6012 { return __lhs
.compare(__rhs
) == 0; }
6014 template<typename _CharT
>
6016 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
, bool>::__type
6017 operator==(const basic_string
<_CharT
>& __lhs
,
6018 const basic_string
<_CharT
>& __rhs
) _GLIBCXX_NOEXCEPT
6019 { return (__lhs
.size() == __rhs
.size()
6020 && !std::char_traits
<_CharT
>::compare(__lhs
.data(), __rhs
.data(),
6024 * @brief Test equivalence of C string and string.
6025 * @param __lhs C string.
6026 * @param __rhs String.
6027 * @return True if @a __rhs.compare(@a __lhs) == 0. 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 Test equivalence of string and C string.
6037 * @param __lhs String.
6038 * @param __rhs C string.
6039 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
6041 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6043 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6044 const _CharT
* __rhs
)
6045 { return __lhs
.compare(__rhs
) == 0; }
6049 * @brief Test difference of two strings.
6050 * @param __lhs First string.
6051 * @param __rhs Second string.
6052 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
6054 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6056 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6057 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6059 { return !(__lhs
== __rhs
); }
6062 * @brief Test difference of C string and string.
6063 * @param __lhs C string.
6064 * @param __rhs String.
6065 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
6067 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6069 operator!=(const _CharT
* __lhs
,
6070 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6071 { return !(__lhs
== __rhs
); }
6074 * @brief Test difference of string and C string.
6075 * @param __lhs String.
6076 * @param __rhs C string.
6077 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
6079 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6081 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6082 const _CharT
* __rhs
)
6083 { return !(__lhs
== __rhs
); }
6087 * @brief Test if string precedes string.
6088 * @param __lhs First string.
6089 * @param __rhs Second string.
6090 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6092 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6094 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6095 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6097 { return __lhs
.compare(__rhs
) < 0; }
6100 * @brief Test if string precedes C string.
6101 * @param __lhs String.
6102 * @param __rhs C string.
6103 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6105 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6107 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6108 const _CharT
* __rhs
)
6109 { return __lhs
.compare(__rhs
) < 0; }
6112 * @brief Test if C string precedes string.
6113 * @param __lhs C string.
6114 * @param __rhs String.
6115 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6117 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6119 operator<(const _CharT
* __lhs
,
6120 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6121 { return __rhs
.compare(__lhs
) > 0; }
6125 * @brief Test if string follows string.
6126 * @param __lhs First string.
6127 * @param __rhs Second string.
6128 * @return True if @a __lhs follows @a __rhs. False otherwise.
6130 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6132 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6133 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6135 { return __lhs
.compare(__rhs
) > 0; }
6138 * @brief Test if string follows C string.
6139 * @param __lhs String.
6140 * @param __rhs C string.
6141 * @return True if @a __lhs follows @a __rhs. False otherwise.
6143 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6145 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6146 const _CharT
* __rhs
)
6147 { return __lhs
.compare(__rhs
) > 0; }
6150 * @brief Test if C string follows string.
6151 * @param __lhs C string.
6152 * @param __rhs String.
6153 * @return True if @a __lhs follows @a __rhs. False otherwise.
6155 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6157 operator>(const _CharT
* __lhs
,
6158 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6159 { return __rhs
.compare(__lhs
) < 0; }
6163 * @brief Test if string doesn't follow string.
6164 * @param __lhs First string.
6165 * @param __rhs Second string.
6166 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6168 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6170 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6171 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6173 { return __lhs
.compare(__rhs
) <= 0; }
6176 * @brief Test if string doesn't follow C string.
6177 * @param __lhs String.
6178 * @param __rhs C string.
6179 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6181 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6183 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6184 const _CharT
* __rhs
)
6185 { return __lhs
.compare(__rhs
) <= 0; }
6188 * @brief Test if C string doesn't follow string.
6189 * @param __lhs C string.
6190 * @param __rhs String.
6191 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6193 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6195 operator<=(const _CharT
* __lhs
,
6196 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6197 { return __rhs
.compare(__lhs
) >= 0; }
6201 * @brief Test if string doesn't precede string.
6202 * @param __lhs First string.
6203 * @param __rhs Second string.
6204 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6206 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6208 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6209 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6211 { return __lhs
.compare(__rhs
) >= 0; }
6214 * @brief Test if string doesn't precede C string.
6215 * @param __lhs String.
6216 * @param __rhs C string.
6217 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6219 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6221 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6222 const _CharT
* __rhs
)
6223 { return __lhs
.compare(__rhs
) >= 0; }
6226 * @brief Test if C string doesn't precede string.
6227 * @param __lhs C string.
6228 * @param __rhs String.
6229 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6231 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6233 operator>=(const _CharT
* __lhs
,
6234 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6235 { return __rhs
.compare(__lhs
) <= 0; }
6238 * @brief Swap contents of two strings.
6239 * @param __lhs First string.
6240 * @param __rhs Second string.
6242 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
6244 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6246 swap(basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6247 basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6248 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs
.swap(__rhs
)))
6249 { __lhs
.swap(__rhs
); }
6253 * @brief Read stream into a string.
6254 * @param __is Input stream.
6255 * @param __str Buffer to store into.
6256 * @return Reference to the input stream.
6258 * Stores characters from @a __is into @a __str until whitespace is
6259 * found, the end of the stream is encountered, or str.max_size()
6260 * is reached. If is.width() is non-zero, that is the limit on the
6261 * number of characters stored into @a __str. Any previous
6262 * contents of @a __str are erased.
6264 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6265 basic_istream
<_CharT
, _Traits
>&
6266 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
6267 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
);
6270 basic_istream
<char>&
6271 operator>>(basic_istream
<char>& __is
, basic_string
<char>& __str
);
6274 * @brief Write string to a stream.
6275 * @param __os Output stream.
6276 * @param __str String to write out.
6277 * @return Reference to the output stream.
6279 * Output characters of @a __str into os following the same rules as for
6280 * writing a C string.
6282 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6283 inline basic_ostream
<_CharT
, _Traits
>&
6284 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
6285 const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
6287 // _GLIBCXX_RESOLVE_LIB_DEFECTS
6288 // 586. string inserter not a formatted function
6289 return __ostream_insert(__os
, __str
.data(), __str
.size());
6293 * @brief Read a line from stream into a string.
6294 * @param __is Input stream.
6295 * @param __str Buffer to store into.
6296 * @param __delim Character marking end of line.
6297 * @return Reference to the input stream.
6299 * Stores characters from @a __is into @a __str until @a __delim is
6300 * found, the end of the stream is encountered, or str.max_size()
6301 * is reached. Any previous contents of @a __str are erased. If
6302 * @a __delim is encountered, it is extracted but not stored into
6305 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6306 basic_istream
<_CharT
, _Traits
>&
6307 getline(basic_istream
<_CharT
, _Traits
>& __is
,
6308 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
);
6311 * @brief Read a line from stream into a string.
6312 * @param __is Input stream.
6313 * @param __str Buffer to store into.
6314 * @return Reference to the input stream.
6316 * Stores characters from is into @a __str until '\n' is
6317 * found, the end of the stream is encountered, or str.max_size()
6318 * is reached. Any previous contents of @a __str are erased. If
6319 * end of line is encountered, it is extracted but not stored into
6322 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6323 inline basic_istream
<_CharT
, _Traits
>&
6324 getline(basic_istream
<_CharT
, _Traits
>& __is
,
6325 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
6326 { return std::getline(__is
, __str
, __is
.widen('\n')); }
6328 #if __cplusplus >= 201103L
6329 /// Read a line from an rvalue stream into a string.
6330 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6331 inline basic_istream
<_CharT
, _Traits
>&
6332 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
6333 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
)
6334 { return std::getline(__is
, __str
, __delim
); }
6336 /// Read a line from an rvalue stream into a string.
6337 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6338 inline basic_istream
<_CharT
, _Traits
>&
6339 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
6340 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
6341 { return std::getline(__is
, __str
); }
6345 basic_istream
<char>&
6346 getline(basic_istream
<char>& __in
, basic_string
<char>& __str
,
6349 #ifdef _GLIBCXX_USE_WCHAR_T
6351 basic_istream
<wchar_t>&
6352 getline(basic_istream
<wchar_t>& __in
, basic_string
<wchar_t>& __str
,
6356 _GLIBCXX_END_NAMESPACE_VERSION
6359 #if __cplusplus >= 201103L
6361 #include <ext/string_conversions.h>
6363 namespace std
_GLIBCXX_VISIBILITY(default)
6365 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6366 _GLIBCXX_BEGIN_NAMESPACE_CXX11
6368 #if _GLIBCXX_USE_C99_STDLIB
6369 // 21.4 Numeric Conversions [string.conversions].
6371 stoi(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6372 { return __gnu_cxx::__stoa
<long, int>(&std::strtol
, "stoi", __str
.c_str(),
6376 stol(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6377 { return __gnu_cxx::__stoa(&std::strtol
, "stol", __str
.c_str(),
6380 inline unsigned long
6381 stoul(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6382 { return __gnu_cxx::__stoa(&std::strtoul
, "stoul", __str
.c_str(),
6386 stoll(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6387 { return __gnu_cxx::__stoa(&std::strtoll
, "stoll", __str
.c_str(),
6390 inline unsigned long long
6391 stoull(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6392 { return __gnu_cxx::__stoa(&std::strtoull
, "stoull", __str
.c_str(),
6395 // NB: strtof vs strtod.
6397 stof(const string
& __str
, size_t* __idx
= 0)
6398 { return __gnu_cxx::__stoa(&std::strtof
, "stof", __str
.c_str(), __idx
); }
6401 stod(const string
& __str
, size_t* __idx
= 0)
6402 { return __gnu_cxx::__stoa(&std::strtod
, "stod", __str
.c_str(), __idx
); }
6405 stold(const string
& __str
, size_t* __idx
= 0)
6406 { return __gnu_cxx::__stoa(&std::strtold
, "stold", __str
.c_str(), __idx
); }
6407 #endif // _GLIBCXX_USE_C99_STDLIB
6409 #if _GLIBCXX_USE_C99_STDIO
6410 // NB: (v)snprintf vs sprintf.
6414 to_string(int __val
)
6415 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, 4 * sizeof(int),
6419 to_string(unsigned __val
)
6420 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
6421 4 * sizeof(unsigned),
6425 to_string(long __val
)
6426 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, 4 * sizeof(long),
6430 to_string(unsigned long __val
)
6431 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
6432 4 * sizeof(unsigned long),
6436 to_string(long long __val
)
6437 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
6438 4 * sizeof(long long),
6442 to_string(unsigned long long __val
)
6443 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
6444 4 * sizeof(unsigned long long),
6448 to_string(float __val
)
6451 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
6452 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
6457 to_string(double __val
)
6460 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
6461 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
6466 to_string(long double __val
)
6469 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
6470 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
6473 #endif // _GLIBCXX_USE_C99_STDIO
6475 #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
6477 stoi(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6478 { return __gnu_cxx::__stoa
<long, int>(&std::wcstol
, "stoi", __str
.c_str(),
6482 stol(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6483 { return __gnu_cxx::__stoa(&std::wcstol
, "stol", __str
.c_str(),
6486 inline unsigned long
6487 stoul(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6488 { return __gnu_cxx::__stoa(&std::wcstoul
, "stoul", __str
.c_str(),
6492 stoll(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6493 { return __gnu_cxx::__stoa(&std::wcstoll
, "stoll", __str
.c_str(),
6496 inline unsigned long long
6497 stoull(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6498 { return __gnu_cxx::__stoa(&std::wcstoull
, "stoull", __str
.c_str(),
6501 // NB: wcstof vs wcstod.
6503 stof(const wstring
& __str
, size_t* __idx
= 0)
6504 { return __gnu_cxx::__stoa(&std::wcstof
, "stof", __str
.c_str(), __idx
); }
6507 stod(const wstring
& __str
, size_t* __idx
= 0)
6508 { return __gnu_cxx::__stoa(&std::wcstod
, "stod", __str
.c_str(), __idx
); }
6511 stold(const wstring
& __str
, size_t* __idx
= 0)
6512 { return __gnu_cxx::__stoa(&std::wcstold
, "stold", __str
.c_str(), __idx
); }
6514 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6517 to_wstring(int __val
)
6518 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(int),
6522 to_wstring(unsigned __val
)
6523 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
6524 4 * sizeof(unsigned),
6528 to_wstring(long __val
)
6529 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(long),
6533 to_wstring(unsigned long __val
)
6534 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
6535 4 * sizeof(unsigned long),
6539 to_wstring(long long __val
)
6540 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
6541 4 * sizeof(long long),
6545 to_wstring(unsigned long long __val
)
6546 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
6547 4 * sizeof(unsigned long long),
6551 to_wstring(float __val
)
6554 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
6555 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
6560 to_wstring(double __val
)
6563 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
6564 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
6569 to_wstring(long double __val
)
6572 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
6573 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
6576 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6577 #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
6579 _GLIBCXX_END_NAMESPACE_CXX11
6580 _GLIBCXX_END_NAMESPACE_VERSION
6585 #if __cplusplus >= 201103L
6587 #include <bits/functional_hash.h>
6589 namespace std
_GLIBCXX_VISIBILITY(default)
6591 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6595 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
6596 /// std::hash specialization for string.
6599 : public __hash_base
<size_t, string
>
6602 operator()(const string
& __s
) const noexcept
6603 { return std::_Hash_impl::hash(__s
.data(), __s
.length()); }
6607 struct __is_fast_hash
<hash
<string
>> : std::false_type
6610 #ifdef _GLIBCXX_USE_WCHAR_T
6611 /// std::hash specialization for wstring.
6613 struct hash
<wstring
>
6614 : public __hash_base
<size_t, wstring
>
6617 operator()(const wstring
& __s
) const noexcept
6618 { return std::_Hash_impl::hash(__s
.data(),
6619 __s
.length() * sizeof(wchar_t)); }
6623 struct __is_fast_hash
<hash
<wstring
>> : std::false_type
6626 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
6628 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
6629 /// std::hash specialization for u16string.
6631 struct hash
<u16string
>
6632 : public __hash_base
<size_t, u16string
>
6635 operator()(const u16string
& __s
) const noexcept
6636 { return std::_Hash_impl::hash(__s
.data(),
6637 __s
.length() * sizeof(char16_t
)); }
6641 struct __is_fast_hash
<hash
<u16string
>> : std::false_type
6644 /// std::hash specialization for u32string.
6646 struct hash
<u32string
>
6647 : public __hash_base
<size_t, u32string
>
6650 operator()(const u32string
& __s
) const noexcept
6651 { return std::_Hash_impl::hash(__s
.data(),
6652 __s
.length() * sizeof(char32_t
)); }
6656 struct __is_fast_hash
<hash
<u32string
>> : std::false_type
6660 #if __cplusplus > 201103L
6662 #define __cpp_lib_string_udls 201304
6664 inline namespace literals
6666 inline namespace string_literals
6668 #pragma GCC diagnostic push
6669 #pragma GCC diagnostic ignored "-Wliteral-suffix"
6670 _GLIBCXX_DEFAULT_ABI_TAG
6671 inline basic_string
<char>
6672 operator""s(const char* __str
, size_t __len
)
6673 { return basic_string
<char>{__str
, __len
}; }
6675 #ifdef _GLIBCXX_USE_WCHAR_T
6676 _GLIBCXX_DEFAULT_ABI_TAG
6677 inline basic_string
<wchar_t>
6678 operator""s(const wchar_t* __str
, size_t __len
)
6679 { return basic_string
<wchar_t>{__str
, __len
}; }
6682 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
6683 _GLIBCXX_DEFAULT_ABI_TAG
6684 inline basic_string
<char16_t
>
6685 operator""s(const char16_t
* __str
, size_t __len
)
6686 { return basic_string
<char16_t
>{__str
, __len
}; }
6688 _GLIBCXX_DEFAULT_ABI_TAG
6689 inline basic_string
<char32_t
>
6690 operator""s(const char32_t
* __str
, size_t __len
)
6691 { return basic_string
<char32_t
>{__str
, __len
}; }
6694 #pragma GCC diagnostic pop
6695 } // inline namespace string_literals
6696 } // inline namespace literals
6698 #endif // __cplusplus > 201103L
6700 _GLIBCXX_END_NAMESPACE_VERSION
6705 #endif /* _BASIC_STRING_H */