1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997-2017 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/basic_string.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{string}
31 // ISO C++ 14882: 21 Strings library
34 #ifndef _BASIC_STRING_H
35 #define _BASIC_STRING_H 1
37 #pragma GCC system_header
39 #include <ext/atomicity.h>
40 #include <ext/alloc_traits.h>
41 #include <debug/debug.h>
43 #if __cplusplus >= 201103L
44 #include <initializer_list>
47 #if __cplusplus > 201402L
48 # include <string_view>
52 namespace std
_GLIBCXX_VISIBILITY(default)
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
56 #if _GLIBCXX_USE_CXX11_ABI
57 _GLIBCXX_BEGIN_NAMESPACE_CXX11
59 * @class basic_string basic_string.h <string>
60 * @brief Managing sequences of characters and character-like objects.
65 * @tparam _CharT Type of character
66 * @tparam _Traits Traits for character type, defaults to
67 * char_traits<_CharT>.
68 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
70 * Meets the requirements of a <a href="tables.html#65">container</a>, a
71 * <a href="tables.html#66">reversible container</a>, and a
72 * <a href="tables.html#67">sequence</a>. Of the
73 * <a href="tables.html#68">optional sequence requirements</a>, only
74 * @c push_back, @c at, and @c %array access are supported.
76 template<typename _CharT
, typename _Traits
, typename _Alloc
>
79 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
80 rebind
<_CharT
>::other _Char_alloc_type
;
81 typedef __gnu_cxx::__alloc_traits
<_Char_alloc_type
> _Alloc_traits
;
85 typedef _Traits traits_type
;
86 typedef typename
_Traits::char_type value_type
;
87 typedef _Char_alloc_type allocator_type
;
88 typedef typename
_Alloc_traits::size_type size_type
;
89 typedef typename
_Alloc_traits::difference_type difference_type
;
90 typedef typename
_Alloc_traits::reference reference
;
91 typedef typename
_Alloc_traits::const_reference const_reference
;
92 typedef typename
_Alloc_traits::pointer pointer
;
93 typedef typename
_Alloc_traits::const_pointer const_pointer
;
94 typedef __gnu_cxx::__normal_iterator
<pointer
, basic_string
> iterator
;
95 typedef __gnu_cxx::__normal_iterator
<const_pointer
, basic_string
>
97 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
98 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
100 /// Value returned by various member functions when they fail.
101 static const size_type npos
= static_cast<size_type
>(-1);
104 // type used for positions in insert, erase etc.
105 #if __cplusplus < 201103L
106 typedef iterator __const_iterator
;
108 typedef const_iterator __const_iterator
;
111 #if __cplusplus > 201402L
112 // A helper type for avoiding boiler-plate.
113 typedef basic_string_view
<_CharT
, _Traits
> __sv_type
;
115 template<typename _Tp
, typename _Res
>
116 using _If_sv
= enable_if_t
<
117 __and_
<is_convertible
<const _Tp
&, __sv_type
>,
118 __not_
<is_convertible
<const _Tp
*, const 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 _CharT
*>>>::value
,
3445 // Allows an implicit conversion to __sv_type.
3447 _S_to_string_view(__sv_type __svt
) noexcept
3450 // Wraps a string_view by explicit conversion and thus
3451 // allows to add an internal constructor that does not
3452 // participate in overload resolution when a string_view
3456 explicit __sv_wrapper(__sv_type __sv
) noexcept
: _M_sv(__sv
) { }
3462 // Construct/copy/destroy:
3463 // NB: We overload ctors in some cases instead of using default
3464 // arguments, per 17.4.4.4 para. 2 item 2.
3467 * @brief Default constructor creates an empty string.
3470 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3471 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
3473 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ }
3477 * @brief Construct an empty string using allocator @a a.
3480 basic_string(const _Alloc
& __a
);
3482 // NB: per LWG issue 42, semantics different from IS:
3484 * @brief Construct string with copy of value of @a str.
3485 * @param __str Source string.
3487 basic_string(const basic_string
& __str
);
3489 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3490 // 2583. no way to supply an allocator for basic_string(str, pos)
3492 * @brief Construct string as copy of a substring.
3493 * @param __str Source string.
3494 * @param __pos Index of first character to copy from.
3495 * @param __a Allocator to use.
3497 basic_string(const basic_string
& __str
, size_type __pos
,
3498 const _Alloc
& __a
= _Alloc());
3501 * @brief Construct string as copy of a substring.
3502 * @param __str Source string.
3503 * @param __pos Index of first character to copy from.
3504 * @param __n Number of characters to copy.
3506 basic_string(const basic_string
& __str
, size_type __pos
,
3509 * @brief Construct string as copy of a substring.
3510 * @param __str Source string.
3511 * @param __pos Index of first character to copy from.
3512 * @param __n Number of characters to copy.
3513 * @param __a Allocator to use.
3515 basic_string(const basic_string
& __str
, size_type __pos
,
3516 size_type __n
, const _Alloc
& __a
);
3519 * @brief Construct string initialized by a character %array.
3520 * @param __s Source character %array.
3521 * @param __n Number of characters to copy.
3522 * @param __a Allocator to use (default is default allocator).
3524 * NB: @a __s must have at least @a __n characters, '\\0'
3525 * has no special meaning.
3527 basic_string(const _CharT
* __s
, size_type __n
,
3528 const _Alloc
& __a
= _Alloc());
3530 * @brief Construct string as copy of a C string.
3531 * @param __s Source C string.
3532 * @param __a Allocator to use (default is default allocator).
3534 basic_string(const _CharT
* __s
, const _Alloc
& __a
= _Alloc());
3536 * @brief Construct string as multiple characters.
3537 * @param __n Number of characters.
3538 * @param __c Character to use.
3539 * @param __a Allocator to use (default is default allocator).
3541 basic_string(size_type __n
, _CharT __c
, const _Alloc
& __a
= _Alloc());
3543 #if __cplusplus >= 201103L
3545 * @brief Move construct string.
3546 * @param __str Source string.
3548 * The newly-created string contains the exact contents of @a __str.
3549 * @a __str is a valid, but unspecified string.
3551 basic_string(basic_string
&& __str
)
3552 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3553 noexcept
// FIXME C++11: should always be noexcept.
3555 : _M_dataplus(__str
._M_dataplus
)
3557 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3558 __str
._M_data(_S_empty_rep()._M_refdata());
3560 __str
._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
3565 * @brief Construct string from an initializer %list.
3566 * @param __l std::initializer_list of characters.
3567 * @param __a Allocator to use (default is default allocator).
3569 basic_string(initializer_list
<_CharT
> __l
, const _Alloc
& __a
= _Alloc());
3573 * @brief Construct string as copy of a range.
3574 * @param __beg Start of range.
3575 * @param __end End of range.
3576 * @param __a Allocator to use (default is default allocator).
3578 template<class _InputIterator
>
3579 basic_string(_InputIterator __beg
, _InputIterator __end
,
3580 const _Alloc
& __a
= _Alloc());
3582 #if __cplusplus > 201402L
3584 * @brief Construct string from a substring of a string_view.
3585 * @param __t Source object convertible to string view.
3586 * @param __pos The index of the first character to copy from __t.
3587 * @param __n The number of characters to copy from __t.
3588 * @param __a Allocator to use.
3590 template<typename _Tp
, typename
= _If_sv
<_Tp
, void>>
3591 basic_string(const _Tp
& __t
, size_type __pos
, size_type __n
,
3592 const _Alloc
& __a
= _Alloc())
3593 : basic_string(_S_to_string_view(__t
).substr(__pos
, __n
), __a
) { }
3596 * @brief Construct string from a string_view.
3597 * @param __t Source object convertible to string view.
3598 * @param __a Allocator to use (default is default allocator).
3600 template<typename _Tp
, typename
= _If_sv
<_Tp
, void>>
3602 basic_string(const _Tp
& __t
, const _Alloc
& __a
= _Alloc())
3603 : basic_string(__sv_wrapper(_S_to_string_view(__t
)), __a
) { }
3606 * @brief Only internally used: Construct string from a string view
3608 * @param __svw string view wrapper.
3609 * @param __a Allocator to use.
3612 basic_string(__sv_wrapper __svw
, const _Alloc
& __a
)
3613 : basic_string(__svw
._M_sv
.data(), __svw
._M_sv
.size(), __a
) { }
3617 * @brief Destroy the string instance.
3619 ~basic_string() _GLIBCXX_NOEXCEPT
3620 { _M_rep()->_M_dispose(this->get_allocator()); }
3623 * @brief Assign the value of @a str to this string.
3624 * @param __str Source string.
3627 operator=(const basic_string
& __str
)
3628 { return this->assign(__str
); }
3631 * @brief Copy contents of @a s into this string.
3632 * @param __s Source null-terminated string.
3635 operator=(const _CharT
* __s
)
3636 { return this->assign(__s
); }
3639 * @brief Set value to string of length 1.
3640 * @param __c Source character.
3642 * Assigning to a character makes this string length 1 and
3643 * (*this)[0] == @a c.
3646 operator=(_CharT __c
)
3648 this->assign(1, __c
);
3652 #if __cplusplus >= 201103L
3654 * @brief Move assign the value of @a str to this string.
3655 * @param __str Source string.
3657 * The contents of @a str are moved into this string (without copying).
3658 * @a str is a valid, but unspecified string.
3660 // PR 58265, this should be noexcept.
3662 operator=(basic_string
&& __str
)
3670 * @brief Set value to string constructed from initializer %list.
3671 * @param __l std::initializer_list.
3674 operator=(initializer_list
<_CharT
> __l
)
3676 this->assign(__l
.begin(), __l
.size());
3681 #if __cplusplus > 201402L
3683 * @brief Set value to string constructed from a string_view.
3684 * @param __svt An object convertible to string_view.
3686 template<typename _Tp
>
3687 _If_sv
<_Tp
, basic_string
&>
3688 operator=(const _Tp
& __svt
)
3689 { return this->assign(__svt
); }
3692 * @brief Convert to a string_view.
3693 * @return A string_view.
3695 operator __sv_type() const noexcept
3696 { return __sv_type(data(), size()); }
3701 * Returns a read/write iterator that points to the first character in
3702 * the %string. Unshares the string.
3705 begin() // FIXME C++11: should be noexcept.
3708 return iterator(_M_data());
3712 * Returns a read-only (constant) iterator that points to the first
3713 * character in the %string.
3716 begin() const _GLIBCXX_NOEXCEPT
3717 { return const_iterator(_M_data()); }
3720 * Returns a read/write iterator that points one past the last
3721 * character in the %string. Unshares the string.
3724 end() // FIXME C++11: should be noexcept.
3727 return iterator(_M_data() + this->size());
3731 * Returns a read-only (constant) iterator that points one past the
3732 * last character in the %string.
3735 end() const _GLIBCXX_NOEXCEPT
3736 { return const_iterator(_M_data() + this->size()); }
3739 * Returns a read/write reverse iterator that points to the last
3740 * character in the %string. Iteration is done in reverse element
3741 * order. Unshares the string.
3744 rbegin() // FIXME C++11: should be noexcept.
3745 { return reverse_iterator(this->end()); }
3748 * Returns a read-only (constant) reverse iterator that points
3749 * to the last character in the %string. Iteration is done in
3750 * reverse element order.
3752 const_reverse_iterator
3753 rbegin() const _GLIBCXX_NOEXCEPT
3754 { return const_reverse_iterator(this->end()); }
3757 * Returns a read/write reverse iterator that points to one before the
3758 * first character in the %string. Iteration is done in reverse
3759 * element order. Unshares the string.
3762 rend() // FIXME C++11: should be noexcept.
3763 { return reverse_iterator(this->begin()); }
3766 * Returns a read-only (constant) reverse iterator that points
3767 * to one before the first character in the %string. Iteration
3768 * is done in reverse element order.
3770 const_reverse_iterator
3771 rend() const _GLIBCXX_NOEXCEPT
3772 { return const_reverse_iterator(this->begin()); }
3774 #if __cplusplus >= 201103L
3776 * Returns a read-only (constant) iterator that points to the first
3777 * character in the %string.
3780 cbegin() const noexcept
3781 { return const_iterator(this->_M_data()); }
3784 * Returns a read-only (constant) iterator that points one past the
3785 * last character in the %string.
3788 cend() const noexcept
3789 { return const_iterator(this->_M_data() + this->size()); }
3792 * Returns a read-only (constant) reverse iterator that points
3793 * to the last character in the %string. Iteration is done in
3794 * reverse element order.
3796 const_reverse_iterator
3797 crbegin() const noexcept
3798 { return const_reverse_iterator(this->end()); }
3801 * Returns a read-only (constant) reverse iterator that points
3802 * to one before the first character in the %string. Iteration
3803 * is done in reverse element order.
3805 const_reverse_iterator
3806 crend() const noexcept
3807 { return const_reverse_iterator(this->begin()); }
3812 /// Returns the number of characters in the string, not including any
3813 /// null-termination.
3815 size() const _GLIBCXX_NOEXCEPT
3816 { return _M_rep()->_M_length
; }
3818 /// Returns the number of characters in the string, not including any
3819 /// null-termination.
3821 length() const _GLIBCXX_NOEXCEPT
3822 { return _M_rep()->_M_length
; }
3824 /// Returns the size() of the largest possible %string.
3826 max_size() const _GLIBCXX_NOEXCEPT
3827 { return _Rep::_S_max_size
; }
3830 * @brief Resizes the %string to the specified number of characters.
3831 * @param __n Number of characters the %string should contain.
3832 * @param __c Character to fill any new elements.
3834 * This function will %resize the %string to the specified
3835 * number of characters. If the number is smaller than the
3836 * %string's current size the %string is truncated, otherwise
3837 * the %string is extended and new elements are %set to @a __c.
3840 resize(size_type __n
, _CharT __c
);
3843 * @brief Resizes the %string to the specified number of characters.
3844 * @param __n Number of characters the %string should contain.
3846 * This function will resize the %string to the specified length. If
3847 * the new size is smaller than the %string's current size the %string
3848 * is truncated, otherwise the %string is extended and new characters
3849 * are default-constructed. For basic types such as char, this means
3850 * setting them to 0.
3853 resize(size_type __n
)
3854 { this->resize(__n
, _CharT()); }
3856 #if __cplusplus >= 201103L
3857 /// A non-binding request to reduce capacity() to size().
3859 shrink_to_fit() _GLIBCXX_NOEXCEPT
3861 #if __cpp_exceptions
3862 if (capacity() > size())
3874 * Returns the total number of characters that the %string can hold
3875 * before needing to allocate more memory.
3878 capacity() const _GLIBCXX_NOEXCEPT
3879 { return _M_rep()->_M_capacity
; }
3882 * @brief Attempt to preallocate enough memory for specified number of
3884 * @param __res_arg Number of characters required.
3885 * @throw std::length_error If @a __res_arg exceeds @c max_size().
3887 * This function attempts to reserve enough memory for the
3888 * %string to hold the specified number of characters. If the
3889 * number requested is more than max_size(), length_error is
3892 * The advantage of this function is that if optimal code is a
3893 * necessity and the user can determine the string length that will be
3894 * required, the user can reserve the memory in %advance, and thus
3895 * prevent a possible reallocation of memory and copying of %string
3899 reserve(size_type __res_arg
= 0);
3902 * Erases the string, making it empty.
3904 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3906 clear() _GLIBCXX_NOEXCEPT
3908 if (_M_rep()->_M_is_shared())
3910 _M_rep()->_M_dispose(this->get_allocator());
3911 _M_data(_S_empty_rep()._M_refdata());
3914 _M_rep()->_M_set_length_and_sharable(0);
3917 // PR 56166: this should not throw.
3920 { _M_mutate(0, this->size(), 0); }
3924 * Returns true if the %string is empty. Equivalent to
3925 * <code>*this == ""</code>.
3928 empty() const _GLIBCXX_NOEXCEPT
3929 { return this->size() == 0; }
3933 * @brief Subscript access to the data contained in the %string.
3934 * @param __pos The index of the character to access.
3935 * @return Read-only (constant) reference to the character.
3937 * This operator allows for easy, array-style, data access.
3938 * Note that data access with this operator is unchecked and
3939 * out_of_range lookups are not defined. (For checked lookups
3943 operator[] (size_type __pos
) const _GLIBCXX_NOEXCEPT
3945 __glibcxx_assert(__pos
<= size());
3946 return _M_data()[__pos
];
3950 * @brief Subscript access to the data contained in the %string.
3951 * @param __pos The index of the character to access.
3952 * @return Read/write reference to the character.
3954 * This operator allows for easy, array-style, data access.
3955 * Note that data access with this operator is unchecked and
3956 * out_of_range lookups are not defined. (For checked lookups
3957 * see at().) Unshares the string.
3960 operator[](size_type __pos
)
3962 // Allow pos == size() both in C++98 mode, as v3 extension,
3963 // and in C++11 mode.
3964 __glibcxx_assert(__pos
<= size());
3965 // In pedantic mode be strict in C++98 mode.
3966 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus
>= 201103L || __pos
< size());
3968 return _M_data()[__pos
];
3972 * @brief Provides access to the data contained in the %string.
3973 * @param __n The index of the character to access.
3974 * @return Read-only (const) reference to the character.
3975 * @throw std::out_of_range If @a n is an invalid index.
3977 * This function provides for safer data access. The parameter is
3978 * first checked that it is in the range of the string. The function
3979 * throws out_of_range if the check fails.
3982 at(size_type __n
) const
3984 if (__n
>= this->size())
3985 __throw_out_of_range_fmt(__N("basic_string::at: __n "
3986 "(which is %zu) >= this->size() "
3989 return _M_data()[__n
];
3993 * @brief Provides access to the data contained in the %string.
3994 * @param __n The index of the character to access.
3995 * @return Read/write reference to the character.
3996 * @throw std::out_of_range If @a n is an invalid index.
3998 * This function provides for safer data access. The parameter is
3999 * first checked that it is in the range of the string. The function
4000 * throws out_of_range if the check fails. Success results in
4001 * unsharing the string.
4007 __throw_out_of_range_fmt(__N("basic_string::at: __n "
4008 "(which is %zu) >= this->size() "
4012 return _M_data()[__n
];
4015 #if __cplusplus >= 201103L
4017 * Returns a read/write reference to the data at the first
4018 * element of the %string.
4023 __glibcxx_assert(!empty());
4024 return operator[](0);
4028 * Returns a read-only (constant) reference to the data at the first
4029 * element of the %string.
4032 front() const noexcept
4034 __glibcxx_assert(!empty());
4035 return operator[](0);
4039 * Returns a read/write reference to the data at the last
4040 * element of the %string.
4045 __glibcxx_assert(!empty());
4046 return operator[](this->size() - 1);
4050 * Returns a read-only (constant) reference to the data at the
4051 * last element of the %string.
4054 back() const noexcept
4056 __glibcxx_assert(!empty());
4057 return operator[](this->size() - 1);
4063 * @brief Append a string to this string.
4064 * @param __str The string to append.
4065 * @return Reference to this string.
4068 operator+=(const basic_string
& __str
)
4069 { return this->append(__str
); }
4072 * @brief Append a C string.
4073 * @param __s The C string to append.
4074 * @return Reference to this string.
4077 operator+=(const _CharT
* __s
)
4078 { return this->append(__s
); }
4081 * @brief Append a character.
4082 * @param __c The character to append.
4083 * @return Reference to this string.
4086 operator+=(_CharT __c
)
4088 this->push_back(__c
);
4092 #if __cplusplus >= 201103L
4094 * @brief Append an initializer_list of characters.
4095 * @param __l The initializer_list of characters to be appended.
4096 * @return Reference to this string.
4099 operator+=(initializer_list
<_CharT
> __l
)
4100 { return this->append(__l
.begin(), __l
.size()); }
4103 #if __cplusplus > 201402L
4105 * @brief Append a string_view.
4106 * @param __svt The object convertible to string_view to be appended.
4107 * @return Reference to this string.
4109 template<typename _Tp
>
4110 _If_sv
<_Tp
, basic_string
&>
4111 operator+=(const _Tp
& __svt
)
4112 { return this->append(__svt
); }
4116 * @brief Append a string to this string.
4117 * @param __str The string to append.
4118 * @return Reference to this string.
4121 append(const basic_string
& __str
);
4124 * @brief Append a substring.
4125 * @param __str The string to append.
4126 * @param __pos Index of the first character of str to append.
4127 * @param __n The number of characters to append.
4128 * @return Reference to this string.
4129 * @throw std::out_of_range if @a __pos is not a valid index.
4131 * This function appends @a __n characters from @a __str
4132 * starting at @a __pos to this string. If @a __n is is larger
4133 * than the number of available characters in @a __str, the
4134 * remainder of @a __str is appended.
4137 append(const basic_string
& __str
, size_type __pos
, size_type __n
);
4140 * @brief Append a C substring.
4141 * @param __s The C string to append.
4142 * @param __n The number of characters to append.
4143 * @return Reference to this string.
4146 append(const _CharT
* __s
, size_type __n
);
4149 * @brief Append a C string.
4150 * @param __s The C string to append.
4151 * @return Reference to this string.
4154 append(const _CharT
* __s
)
4156 __glibcxx_requires_string(__s
);
4157 return this->append(__s
, traits_type::length(__s
));
4161 * @brief Append multiple characters.
4162 * @param __n The number of characters to append.
4163 * @param __c The character to use.
4164 * @return Reference to this string.
4166 * Appends __n copies of __c to this string.
4169 append(size_type __n
, _CharT __c
);
4171 #if __cplusplus >= 201103L
4173 * @brief Append an initializer_list of characters.
4174 * @param __l The initializer_list of characters to append.
4175 * @return Reference to this string.
4178 append(initializer_list
<_CharT
> __l
)
4179 { return this->append(__l
.begin(), __l
.size()); }
4183 * @brief Append a range of characters.
4184 * @param __first Iterator referencing the first character to append.
4185 * @param __last Iterator marking the end of the range.
4186 * @return Reference to this string.
4188 * Appends characters in the range [__first,__last) to this string.
4190 template<class _InputIterator
>
4192 append(_InputIterator __first
, _InputIterator __last
)
4193 { return this->replace(_M_iend(), _M_iend(), __first
, __last
); }
4195 #if __cplusplus > 201402L
4197 * @brief Append a string_view.
4198 * @param __svt The object convertible to string_view to be appended.
4199 * @return Reference to this string.
4201 template<typename _Tp
>
4202 _If_sv
<_Tp
, basic_string
&>
4203 append(const _Tp
& __svt
)
4205 __sv_type __sv
= __svt
;
4206 return this->append(__sv
.data(), __sv
.size());
4210 * @brief Append a range of characters from a string_view.
4211 * @param __svt The object convertible to string_view to be appended
4213 * @param __pos The position in the string_view to append from.
4214 * @param __n The number of characters to append from the string_view.
4215 * @return Reference to this string.
4217 template<typename _Tp
>
4218 _If_sv
<_Tp
, basic_string
&>
4219 append(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
4221 __sv_type __sv
= __svt
;
4222 return append(__sv
.data()
4223 + __sv
._M_check(__pos
, "basic_string::append"),
4224 __sv
._M_limit(__pos
, __n
));
4229 * @brief Append a single character.
4230 * @param __c Character to append.
4233 push_back(_CharT __c
)
4235 const size_type __len
= 1 + this->size();
4236 if (__len
> this->capacity() || _M_rep()->_M_is_shared())
4237 this->reserve(__len
);
4238 traits_type::assign(_M_data()[this->size()], __c
);
4239 _M_rep()->_M_set_length_and_sharable(__len
);
4243 * @brief Set value to contents of another string.
4244 * @param __str Source string to use.
4245 * @return Reference to this string.
4248 assign(const basic_string
& __str
);
4250 #if __cplusplus >= 201103L
4252 * @brief Set value to contents of another string.
4253 * @param __str Source string to use.
4254 * @return Reference to this string.
4256 * This function sets this string to the exact contents of @a __str.
4257 * @a __str is a valid, but unspecified string.
4259 // PR 58265, this should be noexcept.
4261 assign(basic_string
&& __str
)
4269 * @brief Set value to a substring of a string.
4270 * @param __str The string to use.
4271 * @param __pos Index of the first character of str.
4272 * @param __n Number of characters to use.
4273 * @return Reference to this string.
4274 * @throw std::out_of_range if @a pos is not a valid index.
4276 * This function sets this string to the substring of @a __str
4277 * consisting of @a __n characters at @a __pos. If @a __n is
4278 * is larger than the number of available characters in @a
4279 * __str, the remainder of @a __str is used.
4282 assign(const basic_string
& __str
, size_type __pos
, size_type __n
)
4283 { return this->assign(__str
._M_data()
4284 + __str
._M_check(__pos
, "basic_string::assign"),
4285 __str
._M_limit(__pos
, __n
)); }
4288 * @brief Set value to a C substring.
4289 * @param __s The C string to use.
4290 * @param __n Number of characters to use.
4291 * @return Reference to this string.
4293 * This function sets the value of this string to the first @a __n
4294 * characters of @a __s. If @a __n is is larger than the number of
4295 * available characters in @a __s, the remainder of @a __s is used.
4298 assign(const _CharT
* __s
, size_type __n
);
4301 * @brief Set value to contents of a C string.
4302 * @param __s The C string to use.
4303 * @return Reference to this string.
4305 * This function sets the value of this string to the value of @a __s.
4306 * The data is copied, so there is no dependence on @a __s once the
4310 assign(const _CharT
* __s
)
4312 __glibcxx_requires_string(__s
);
4313 return this->assign(__s
, traits_type::length(__s
));
4317 * @brief Set value to multiple characters.
4318 * @param __n Length of the resulting string.
4319 * @param __c The character to use.
4320 * @return Reference to this string.
4322 * This function sets the value of this string to @a __n copies of
4326 assign(size_type __n
, _CharT __c
)
4327 { return _M_replace_aux(size_type(0), this->size(), __n
, __c
); }
4330 * @brief Set value to a range of characters.
4331 * @param __first Iterator referencing the first character to append.
4332 * @param __last Iterator marking the end of the range.
4333 * @return Reference to this string.
4335 * Sets value of string to characters in the range [__first,__last).
4337 template<class _InputIterator
>
4339 assign(_InputIterator __first
, _InputIterator __last
)
4340 { return this->replace(_M_ibegin(), _M_iend(), __first
, __last
); }
4342 #if __cplusplus >= 201103L
4344 * @brief Set value to an initializer_list of characters.
4345 * @param __l The initializer_list of characters to assign.
4346 * @return Reference to this string.
4349 assign(initializer_list
<_CharT
> __l
)
4350 { return this->assign(__l
.begin(), __l
.size()); }
4353 #if __cplusplus > 201402L
4355 * @brief Set value from a string_view.
4356 * @param __svt The source object convertible to string_view.
4357 * @return Reference to this string.
4359 template<typename _Tp
>
4360 _If_sv
<_Tp
, basic_string
&>
4361 assign(const _Tp
& __svt
)
4363 __sv_type __sv
= __svt
;
4364 return this->assign(__sv
.data(), __sv
.size());
4368 * @brief Set value from a range of characters in a string_view.
4369 * @param __svt The source object convertible to string_view.
4370 * @param __pos The position in the string_view to assign from.
4371 * @param __n The number of characters to assign.
4372 * @return Reference to this string.
4374 template<typename _Tp
>
4375 _If_sv
<_Tp
, basic_string
&>
4376 assign(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
4378 __sv_type __sv
= __svt
;
4379 return assign(__sv
.data()
4380 + __sv
._M_check(__pos
, "basic_string::assign"),
4381 __sv
._M_limit(__pos
, __n
));
4386 * @brief Insert multiple characters.
4387 * @param __p Iterator referencing location in string to insert at.
4388 * @param __n Number of characters to insert
4389 * @param __c The character to insert.
4390 * @throw std::length_error If new length exceeds @c max_size().
4392 * Inserts @a __n copies of character @a __c starting at the
4393 * position referenced by iterator @a __p. If adding
4394 * characters causes the length to exceed max_size(),
4395 * length_error is thrown. The value of the string doesn't
4396 * change if an error is thrown.
4399 insert(iterator __p
, size_type __n
, _CharT __c
)
4400 { this->replace(__p
, __p
, __n
, __c
); }
4403 * @brief Insert a range of characters.
4404 * @param __p Iterator referencing location in string to insert at.
4405 * @param __beg Start of range.
4406 * @param __end End of range.
4407 * @throw std::length_error If new length exceeds @c max_size().
4409 * Inserts characters in range [__beg,__end). If adding
4410 * characters causes the length to exceed max_size(),
4411 * length_error is thrown. The value of the string doesn't
4412 * change if an error is thrown.
4414 template<class _InputIterator
>
4416 insert(iterator __p
, _InputIterator __beg
, _InputIterator __end
)
4417 { this->replace(__p
, __p
, __beg
, __end
); }
4419 #if __cplusplus >= 201103L
4421 * @brief Insert an initializer_list of characters.
4422 * @param __p Iterator referencing location in string to insert at.
4423 * @param __l The initializer_list of characters to insert.
4424 * @throw std::length_error If new length exceeds @c max_size().
4427 insert(iterator __p
, initializer_list
<_CharT
> __l
)
4429 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
4430 this->insert(__p
- _M_ibegin(), __l
.begin(), __l
.size());
4435 * @brief Insert value of a string.
4436 * @param __pos1 Iterator referencing location in string to insert at.
4437 * @param __str The string to insert.
4438 * @return Reference to this string.
4439 * @throw std::length_error If new length exceeds @c max_size().
4441 * Inserts value of @a __str starting at @a __pos1. If adding
4442 * characters causes the length to exceed max_size(),
4443 * length_error is thrown. The value of the string doesn't
4444 * change if an error is thrown.
4447 insert(size_type __pos1
, const basic_string
& __str
)
4448 { return this->insert(__pos1
, __str
, size_type(0), __str
.size()); }
4451 * @brief Insert a substring.
4452 * @param __pos1 Iterator referencing location in string to insert at.
4453 * @param __str The string to insert.
4454 * @param __pos2 Start of characters in str to insert.
4455 * @param __n Number of characters to insert.
4456 * @return Reference to this string.
4457 * @throw std::length_error If new length exceeds @c max_size().
4458 * @throw std::out_of_range If @a pos1 > size() or
4459 * @a __pos2 > @a str.size().
4461 * Starting at @a pos1, insert @a __n character of @a __str
4462 * beginning with @a __pos2. If adding characters causes the
4463 * length to exceed max_size(), length_error is thrown. If @a
4464 * __pos1 is beyond the end of this string or @a __pos2 is
4465 * beyond the end of @a __str, out_of_range is thrown. The
4466 * value of the string doesn't change if an error is thrown.
4469 insert(size_type __pos1
, const basic_string
& __str
,
4470 size_type __pos2
, size_type __n
)
4471 { return this->insert(__pos1
, __str
._M_data()
4472 + __str
._M_check(__pos2
, "basic_string::insert"),
4473 __str
._M_limit(__pos2
, __n
)); }
4476 * @brief Insert a C substring.
4477 * @param __pos Iterator referencing location in string to insert at.
4478 * @param __s The C string to insert.
4479 * @param __n The number of characters to insert.
4480 * @return Reference to this string.
4481 * @throw std::length_error If new length exceeds @c max_size().
4482 * @throw std::out_of_range If @a __pos is beyond the end of this
4485 * Inserts the first @a __n characters of @a __s starting at @a
4486 * __pos. If adding characters causes the length to exceed
4487 * max_size(), length_error is thrown. If @a __pos is beyond
4488 * end(), out_of_range is thrown. The value of the string
4489 * doesn't change if an error is thrown.
4492 insert(size_type __pos
, const _CharT
* __s
, size_type __n
);
4495 * @brief Insert a C string.
4496 * @param __pos Iterator referencing location in string to insert at.
4497 * @param __s The C string to insert.
4498 * @return Reference to this string.
4499 * @throw std::length_error If new length exceeds @c max_size().
4500 * @throw std::out_of_range If @a pos is beyond the end of this
4503 * Inserts the first @a n characters of @a __s starting at @a __pos. If
4504 * adding characters causes the length to exceed max_size(),
4505 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
4506 * thrown. The value of the string doesn't change if an error is
4510 insert(size_type __pos
, const _CharT
* __s
)
4512 __glibcxx_requires_string(__s
);
4513 return this->insert(__pos
, __s
, traits_type::length(__s
));
4517 * @brief Insert multiple characters.
4518 * @param __pos Index in string to insert at.
4519 * @param __n Number of characters to insert
4520 * @param __c The character to insert.
4521 * @return Reference to this string.
4522 * @throw std::length_error If new length exceeds @c max_size().
4523 * @throw std::out_of_range If @a __pos is beyond the end of this
4526 * Inserts @a __n copies of character @a __c starting at index
4527 * @a __pos. If adding characters causes the length to exceed
4528 * max_size(), length_error is thrown. If @a __pos > length(),
4529 * out_of_range is thrown. The value of the string doesn't
4530 * change if an error is thrown.
4533 insert(size_type __pos
, size_type __n
, _CharT __c
)
4534 { return _M_replace_aux(_M_check(__pos
, "basic_string::insert"),
4535 size_type(0), __n
, __c
); }
4538 * @brief Insert one character.
4539 * @param __p Iterator referencing position in string to insert at.
4540 * @param __c The character to insert.
4541 * @return Iterator referencing newly inserted char.
4542 * @throw std::length_error If new length exceeds @c max_size().
4544 * Inserts character @a __c at position referenced by @a __p.
4545 * If adding character causes the length to exceed max_size(),
4546 * length_error is thrown. If @a __p is beyond end of string,
4547 * out_of_range is thrown. The value of the string doesn't
4548 * change if an error is thrown.
4551 insert(iterator __p
, _CharT __c
)
4553 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
4554 const size_type __pos
= __p
- _M_ibegin();
4555 _M_replace_aux(__pos
, size_type(0), size_type(1), __c
);
4556 _M_rep()->_M_set_leaked();
4557 return iterator(_M_data() + __pos
);
4560 #if __cplusplus > 201402L
4562 * @brief Insert a string_view.
4563 * @param __pos Iterator referencing position in string to insert at.
4564 * @param __svt The object convertible to string_view to insert.
4565 * @return Reference to this string.
4567 template<typename _Tp
>
4568 _If_sv
<_Tp
, basic_string
&>
4569 insert(size_type __pos
, const _Tp
& __svt
)
4571 __sv_type __sv
= __svt
;
4572 return this->insert(__pos
, __sv
.data(), __sv
.size());
4576 * @brief Insert a string_view.
4577 * @param __pos Iterator referencing position in string to insert at.
4578 * @param __svt The object convertible to string_view to insert from.
4579 * @param __pos Iterator referencing position in string_view to insert
4581 * @param __n The number of characters to insert.
4582 * @return Reference to this string.
4584 template<typename _Tp
>
4585 _If_sv
<_Tp
, basic_string
&>
4586 insert(size_type __pos1
, const _Tp
& __svt
,
4587 size_type __pos2
, size_type __n
= npos
)
4589 __sv_type __sv
= __svt
;
4590 return this->replace(__pos1
, size_type(0), __sv
.data()
4591 + __sv
._M_check(__pos2
, "basic_string::insert"),
4592 __sv
._M_limit(__pos2
, __n
));
4597 * @brief Remove characters.
4598 * @param __pos Index of first character to remove (default 0).
4599 * @param __n Number of characters to remove (default remainder).
4600 * @return Reference to this string.
4601 * @throw std::out_of_range If @a pos is beyond the end of this
4604 * Removes @a __n characters from this string starting at @a
4605 * __pos. The length of the string is reduced by @a __n. If
4606 * there are < @a __n characters to remove, the remainder of
4607 * the string is truncated. If @a __p is beyond end of string,
4608 * out_of_range is thrown. The value of the string doesn't
4609 * change if an error is thrown.
4612 erase(size_type __pos
= 0, size_type __n
= npos
)
4614 _M_mutate(_M_check(__pos
, "basic_string::erase"),
4615 _M_limit(__pos
, __n
), size_type(0));
4620 * @brief Remove one character.
4621 * @param __position Iterator referencing the character to remove.
4622 * @return iterator referencing same location after removal.
4624 * Removes the character at @a __position from this string. The value
4625 * of the string doesn't change if an error is thrown.
4628 erase(iterator __position
)
4630 _GLIBCXX_DEBUG_PEDASSERT(__position
>= _M_ibegin()
4631 && __position
< _M_iend());
4632 const size_type __pos
= __position
- _M_ibegin();
4633 _M_mutate(__pos
, size_type(1), size_type(0));
4634 _M_rep()->_M_set_leaked();
4635 return iterator(_M_data() + __pos
);
4639 * @brief Remove a range of characters.
4640 * @param __first Iterator referencing the first character to remove.
4641 * @param __last Iterator referencing the end of the range.
4642 * @return Iterator referencing location of first after removal.
4644 * Removes the characters in the range [first,last) from this string.
4645 * The value of the string doesn't change if an error is thrown.
4648 erase(iterator __first
, iterator __last
);
4650 #if __cplusplus >= 201103L
4652 * @brief Remove the last character.
4654 * The string must be non-empty.
4657 pop_back() // FIXME C++11: should be noexcept.
4659 __glibcxx_assert(!empty());
4660 erase(size() - 1, 1);
4665 * @brief Replace characters with value from another string.
4666 * @param __pos Index of first character to replace.
4667 * @param __n Number of characters to be replaced.
4668 * @param __str String to insert.
4669 * @return Reference to this string.
4670 * @throw std::out_of_range If @a pos is beyond the end of this
4672 * @throw std::length_error If new length exceeds @c max_size().
4674 * Removes the characters in the range [__pos,__pos+__n) from
4675 * this string. In place, the value of @a __str is inserted.
4676 * If @a __pos is beyond end of string, out_of_range is thrown.
4677 * If the length of the result exceeds max_size(), length_error
4678 * is thrown. The value of the string doesn't change if an
4682 replace(size_type __pos
, size_type __n
, const basic_string
& __str
)
4683 { return this->replace(__pos
, __n
, __str
._M_data(), __str
.size()); }
4686 * @brief Replace characters with value from another string.
4687 * @param __pos1 Index of first character to replace.
4688 * @param __n1 Number of characters to be replaced.
4689 * @param __str String to insert.
4690 * @param __pos2 Index of first character of str to use.
4691 * @param __n2 Number of characters from str to use.
4692 * @return Reference to this string.
4693 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
4695 * @throw std::length_error If new length exceeds @c max_size().
4697 * Removes the characters in the range [__pos1,__pos1 + n) from this
4698 * string. In place, the value of @a __str is inserted. If @a __pos is
4699 * beyond end of string, out_of_range is thrown. If the length of the
4700 * result exceeds max_size(), length_error is thrown. The value of the
4701 * string doesn't change if an error is thrown.
4704 replace(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
4705 size_type __pos2
, size_type __n2
)
4706 { return this->replace(__pos1
, __n1
, __str
._M_data()
4707 + __str
._M_check(__pos2
, "basic_string::replace"),
4708 __str
._M_limit(__pos2
, __n2
)); }
4711 * @brief Replace characters with value of a C substring.
4712 * @param __pos Index of first character to replace.
4713 * @param __n1 Number of characters to be replaced.
4714 * @param __s C string to insert.
4715 * @param __n2 Number of characters from @a s to use.
4716 * @return Reference to this string.
4717 * @throw std::out_of_range If @a pos1 > size().
4718 * @throw std::length_error If new length exceeds @c max_size().
4720 * Removes the characters in the range [__pos,__pos + __n1)
4721 * from this string. In place, the first @a __n2 characters of
4722 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
4723 * @a __pos is beyond end of string, out_of_range is thrown. If
4724 * the length of result exceeds max_size(), length_error is
4725 * thrown. The value of the string doesn't change if an error
4729 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
4733 * @brief Replace characters with value of a C string.
4734 * @param __pos Index of first character to replace.
4735 * @param __n1 Number of characters to be replaced.
4736 * @param __s C string to insert.
4737 * @return Reference to this string.
4738 * @throw std::out_of_range If @a pos > size().
4739 * @throw std::length_error If new length exceeds @c max_size().
4741 * Removes the characters in the range [__pos,__pos + __n1)
4742 * from this string. In place, the characters of @a __s are
4743 * inserted. If @a __pos is beyond end of string, out_of_range
4744 * is thrown. If the length of result exceeds max_size(),
4745 * length_error is thrown. The value of the string doesn't
4746 * change if an error is thrown.
4749 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
)
4751 __glibcxx_requires_string(__s
);
4752 return this->replace(__pos
, __n1
, __s
, traits_type::length(__s
));
4756 * @brief Replace characters with multiple characters.
4757 * @param __pos Index of first character to replace.
4758 * @param __n1 Number of characters to be replaced.
4759 * @param __n2 Number of characters to insert.
4760 * @param __c Character to insert.
4761 * @return Reference to this string.
4762 * @throw std::out_of_range If @a __pos > size().
4763 * @throw std::length_error If new length exceeds @c max_size().
4765 * Removes the characters in the range [pos,pos + n1) from this
4766 * string. In place, @a __n2 copies of @a __c are inserted.
4767 * If @a __pos is beyond end of string, out_of_range is thrown.
4768 * If the length of result exceeds max_size(), length_error is
4769 * thrown. The value of the string doesn't change if an error
4773 replace(size_type __pos
, size_type __n1
, size_type __n2
, _CharT __c
)
4774 { return _M_replace_aux(_M_check(__pos
, "basic_string::replace"),
4775 _M_limit(__pos
, __n1
), __n2
, __c
); }
4778 * @brief Replace range of characters with string.
4779 * @param __i1 Iterator referencing start of range to replace.
4780 * @param __i2 Iterator referencing end of range to replace.
4781 * @param __str String value to insert.
4782 * @return Reference to this string.
4783 * @throw std::length_error If new length exceeds @c max_size().
4785 * Removes the characters in the range [__i1,__i2). In place,
4786 * the value of @a __str is inserted. If the length of result
4787 * exceeds max_size(), length_error is thrown. The value of
4788 * the string doesn't change if an error is thrown.
4791 replace(iterator __i1
, iterator __i2
, const basic_string
& __str
)
4792 { return this->replace(__i1
, __i2
, __str
._M_data(), __str
.size()); }
4795 * @brief Replace range of characters with C substring.
4796 * @param __i1 Iterator referencing start of range to replace.
4797 * @param __i2 Iterator referencing end of range to replace.
4798 * @param __s C string value to insert.
4799 * @param __n Number of characters from s to insert.
4800 * @return Reference to this string.
4801 * @throw std::length_error If new length exceeds @c max_size().
4803 * Removes the characters in the range [__i1,__i2). In place,
4804 * the first @a __n characters of @a __s are inserted. If the
4805 * length of result exceeds max_size(), length_error is thrown.
4806 * The value of the string doesn't change if an error is
4810 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
, size_type __n
)
4812 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4813 && __i2
<= _M_iend());
4814 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
, __s
, __n
);
4818 * @brief Replace range of characters with C string.
4819 * @param __i1 Iterator referencing start of range to replace.
4820 * @param __i2 Iterator referencing end of range to replace.
4821 * @param __s C string value to insert.
4822 * @return Reference to this string.
4823 * @throw std::length_error If new length exceeds @c max_size().
4825 * Removes the characters in the range [__i1,__i2). In place,
4826 * the characters of @a __s are inserted. If the length of
4827 * result exceeds max_size(), length_error is thrown. The
4828 * value of the string doesn't change if an error is thrown.
4831 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
)
4833 __glibcxx_requires_string(__s
);
4834 return this->replace(__i1
, __i2
, __s
, traits_type::length(__s
));
4838 * @brief Replace range of characters with multiple characters
4839 * @param __i1 Iterator referencing start of range to replace.
4840 * @param __i2 Iterator referencing end of range to replace.
4841 * @param __n Number of characters to insert.
4842 * @param __c Character to insert.
4843 * @return Reference to this string.
4844 * @throw std::length_error If new length exceeds @c max_size().
4846 * Removes the characters in the range [__i1,__i2). In place,
4847 * @a __n copies of @a __c are inserted. If the length of
4848 * result exceeds max_size(), length_error is thrown. The
4849 * value of the string doesn't change if an error is thrown.
4852 replace(iterator __i1
, iterator __i2
, size_type __n
, _CharT __c
)
4854 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4855 && __i2
<= _M_iend());
4856 return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __c
);
4860 * @brief Replace range of characters with range.
4861 * @param __i1 Iterator referencing start of range to replace.
4862 * @param __i2 Iterator referencing end of range to replace.
4863 * @param __k1 Iterator referencing start of range to insert.
4864 * @param __k2 Iterator referencing end of range to insert.
4865 * @return Reference to this string.
4866 * @throw std::length_error If new length exceeds @c max_size().
4868 * Removes the characters in the range [__i1,__i2). In place,
4869 * characters in the range [__k1,__k2) are inserted. If the
4870 * length of result exceeds max_size(), length_error is thrown.
4871 * The value of the string doesn't change if an error is
4874 template<class _InputIterator
>
4876 replace(iterator __i1
, iterator __i2
,
4877 _InputIterator __k1
, _InputIterator __k2
)
4879 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4880 && __i2
<= _M_iend());
4881 __glibcxx_requires_valid_range(__k1
, __k2
);
4882 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
4883 return _M_replace_dispatch(__i1
, __i2
, __k1
, __k2
, _Integral());
4886 // Specializations for the common case of pointer and iterator:
4887 // useful to avoid the overhead of temporary buffering in _M_replace.
4889 replace(iterator __i1
, iterator __i2
, _CharT
* __k1
, _CharT
* __k2
)
4891 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4892 && __i2
<= _M_iend());
4893 __glibcxx_requires_valid_range(__k1
, __k2
);
4894 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4899 replace(iterator __i1
, iterator __i2
,
4900 const _CharT
* __k1
, const _CharT
* __k2
)
4902 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4903 && __i2
<= _M_iend());
4904 __glibcxx_requires_valid_range(__k1
, __k2
);
4905 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4910 replace(iterator __i1
, iterator __i2
, iterator __k1
, iterator __k2
)
4912 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4913 && __i2
<= _M_iend());
4914 __glibcxx_requires_valid_range(__k1
, __k2
);
4915 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4916 __k1
.base(), __k2
- __k1
);
4920 replace(iterator __i1
, iterator __i2
,
4921 const_iterator __k1
, const_iterator __k2
)
4923 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4924 && __i2
<= _M_iend());
4925 __glibcxx_requires_valid_range(__k1
, __k2
);
4926 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4927 __k1
.base(), __k2
- __k1
);
4930 #if __cplusplus >= 201103L
4932 * @brief Replace range of characters with initializer_list.
4933 * @param __i1 Iterator referencing start of range to replace.
4934 * @param __i2 Iterator referencing end of range to replace.
4935 * @param __l The initializer_list of characters to insert.
4936 * @return Reference to this string.
4937 * @throw std::length_error If new length exceeds @c max_size().
4939 * Removes the characters in the range [__i1,__i2). In place,
4940 * characters in the range [__k1,__k2) are inserted. If the
4941 * length of result exceeds max_size(), length_error is thrown.
4942 * The value of the string doesn't change if an error is
4945 basic_string
& replace(iterator __i1
, iterator __i2
,
4946 initializer_list
<_CharT
> __l
)
4947 { return this->replace(__i1
, __i2
, __l
.begin(), __l
.end()); }
4950 #if __cplusplus > 201402L
4952 * @brief Replace range of characters with string_view.
4953 * @param __pos The position to replace at.
4954 * @param __n The number of characters to replace.
4955 * @param __svt The object convertible to string_view to insert.
4956 * @return Reference to this string.
4958 template<typename _Tp
>
4959 _If_sv
<_Tp
, basic_string
&>
4960 replace(size_type __pos
, size_type __n
, const _Tp
& __svt
)
4962 __sv_type __sv
= __svt
;
4963 return this->replace(__pos
, __n
, __sv
.data(), __sv
.size());
4967 * @brief Replace range of characters with string_view.
4968 * @param __pos1 The position to replace at.
4969 * @param __n1 The number of characters to replace.
4970 * @param __svt The object convertible to string_view to insert from.
4971 * @param __pos2 The position in the string_view to insert from.
4972 * @param __n2 The number of characters to insert.
4973 * @return Reference to this string.
4975 template<typename _Tp
>
4976 _If_sv
<_Tp
, basic_string
&>
4977 replace(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
4978 size_type __pos2
, size_type __n2
= npos
)
4980 __sv_type __sv
= __svt
;
4981 return this->replace(__pos1
, __n1
,
4982 __sv
.data() + __sv
._M_check(__pos2
, "basic_string::replace"),
4983 __sv
._M_limit(__pos2
, __n2
));
4987 * @brief Replace range of characters with string_view.
4988 * @param __i1 An iterator referencing the start position
4990 * @param __i2 An iterator referencing the end position
4992 * @param __svt The object convertible to string_view to insert from.
4993 * @return Reference to this string.
4995 template<typename _Tp
>
4996 _If_sv
<_Tp
, basic_string
&>
4997 replace(const_iterator __i1
, const_iterator __i2
, const _Tp
& __svt
)
4999 __sv_type __sv
= __svt
;
5000 return this->replace(__i1
- begin(), __i2
- __i1
, __sv
);
5005 template<class _Integer
>
5007 _M_replace_dispatch(iterator __i1
, iterator __i2
, _Integer __n
,
5008 _Integer __val
, __true_type
)
5009 { return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __val
); }
5011 template<class _InputIterator
>
5013 _M_replace_dispatch(iterator __i1
, iterator __i2
, _InputIterator __k1
,
5014 _InputIterator __k2
, __false_type
);
5017 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
5021 _M_replace_safe(size_type __pos1
, size_type __n1
, const _CharT
* __s
,
5024 // _S_construct_aux is used to implement the 21.3.1 para 15 which
5025 // requires special behaviour if _InIter is an integral type
5026 template<class _InIterator
>
5028 _S_construct_aux(_InIterator __beg
, _InIterator __end
,
5029 const _Alloc
& __a
, __false_type
)
5031 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
5032 return _S_construct(__beg
, __end
, __a
, _Tag());
5035 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5036 // 438. Ambiguity in the "do the right thing" clause
5037 template<class _Integer
>
5039 _S_construct_aux(_Integer __beg
, _Integer __end
,
5040 const _Alloc
& __a
, __true_type
)
5041 { return _S_construct_aux_2(static_cast<size_type
>(__beg
),
5045 _S_construct_aux_2(size_type __req
, _CharT __c
, const _Alloc
& __a
)
5046 { return _S_construct(__req
, __c
, __a
); }
5048 template<class _InIterator
>
5050 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
)
5052 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
5053 return _S_construct_aux(__beg
, __end
, __a
, _Integral());
5056 // For Input Iterators, used in istreambuf_iterators, etc.
5057 template<class _InIterator
>
5059 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
,
5060 input_iterator_tag
);
5062 // For forward_iterators up to random_access_iterators, used for
5063 // string::iterator, _CharT*, etc.
5064 template<class _FwdIterator
>
5066 _S_construct(_FwdIterator __beg
, _FwdIterator __end
, const _Alloc
& __a
,
5067 forward_iterator_tag
);
5070 _S_construct(size_type __req
, _CharT __c
, const _Alloc
& __a
);
5075 * @brief Copy substring into C string.
5076 * @param __s C string to copy value into.
5077 * @param __n Number of characters to copy.
5078 * @param __pos Index of first character to copy.
5079 * @return Number of characters actually copied
5080 * @throw std::out_of_range If __pos > size().
5082 * Copies up to @a __n characters starting at @a __pos into the
5083 * C string @a __s. If @a __pos is %greater than size(),
5084 * out_of_range is thrown.
5087 copy(_CharT
* __s
, size_type __n
, size_type __pos
= 0) const;
5090 * @brief Swap contents with another string.
5091 * @param __s String to swap with.
5093 * Exchanges the contents of this string with that of @a __s in constant
5096 // PR 58265, this should be noexcept.
5098 swap(basic_string
& __s
);
5100 // String operations:
5102 * @brief Return const pointer to null-terminated contents.
5104 * This is a handle to internal data. Do not modify or dire things may
5108 c_str() const _GLIBCXX_NOEXCEPT
5109 { return _M_data(); }
5112 * @brief Return const pointer to contents.
5114 * This is a pointer to internal data. It is undefined to modify
5115 * the contents through the returned pointer. To get a pointer that
5116 * allows modifying the contents use @c &str[0] instead,
5117 * (or in C++17 the non-const @c str.data() overload).
5120 data() const _GLIBCXX_NOEXCEPT
5121 { return _M_data(); }
5123 #if __cplusplus > 201402L
5125 * @brief Return non-const pointer to contents.
5127 * This is a pointer to the character sequence held by the string.
5128 * Modifying the characters in the sequence is allowed.
5132 { return _M_data(); }
5136 * @brief Return copy of allocator used to construct this string.
5139 get_allocator() const _GLIBCXX_NOEXCEPT
5140 { return _M_dataplus
; }
5143 * @brief Find position of a C substring.
5144 * @param __s C string to locate.
5145 * @param __pos Index of character to search from.
5146 * @param __n Number of characters from @a s to search for.
5147 * @return Index of start of first occurrence.
5149 * Starting from @a __pos, searches forward for the first @a
5150 * __n characters in @a __s within this string. If found,
5151 * returns the index where it begins. If not found, returns
5155 find(const _CharT
* __s
, size_type __pos
, size_type __n
) const
5159 * @brief Find position of a string.
5160 * @param __str String to locate.
5161 * @param __pos Index of character to search from (default 0).
5162 * @return Index of start of first occurrence.
5164 * Starting from @a __pos, searches forward for value of @a __str within
5165 * this string. If found, returns the index where it begins. If not
5166 * found, returns npos.
5169 find(const basic_string
& __str
, size_type __pos
= 0) const
5171 { return this->find(__str
.data(), __pos
, __str
.size()); }
5174 * @brief Find position of a C string.
5175 * @param __s C string to locate.
5176 * @param __pos Index of character to search from (default 0).
5177 * @return Index of start of first occurrence.
5179 * Starting from @a __pos, searches forward for the value of @a
5180 * __s within this string. If found, returns the index where
5181 * it begins. If not found, returns npos.
5184 find(const _CharT
* __s
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
5186 __glibcxx_requires_string(__s
);
5187 return this->find(__s
, __pos
, traits_type::length(__s
));
5191 * @brief Find position of a character.
5192 * @param __c Character to locate.
5193 * @param __pos Index of character to search from (default 0).
5194 * @return Index of first occurrence.
5196 * Starting from @a __pos, searches forward for @a __c within
5197 * this string. If found, returns the index where it was
5198 * found. If not found, returns npos.
5201 find(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
;
5203 #if __cplusplus > 201402L
5205 * @brief Find position of a string_view.
5206 * @param __svt The object convertible to string_view to locate.
5207 * @param __pos Index of character to search from (default 0).
5208 * @return Index of start of first occurrence.
5210 template<typename _Tp
>
5211 _If_sv
<_Tp
, size_type
>
5212 find(const _Tp
& __svt
, size_type __pos
= 0) const
5213 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5215 __sv_type __sv
= __svt
;
5216 return this->find(__sv
.data(), __pos
, __sv
.size());
5221 * @brief Find last position of a string.
5222 * @param __str String to locate.
5223 * @param __pos Index of character to search back from (default end).
5224 * @return Index of start of last occurrence.
5226 * Starting from @a __pos, searches backward for value of @a
5227 * __str within this string. If found, returns the index where
5228 * it begins. If not found, returns npos.
5231 rfind(const basic_string
& __str
, size_type __pos
= npos
) const
5233 { return this->rfind(__str
.data(), __pos
, __str
.size()); }
5236 * @brief Find last position of a C substring.
5237 * @param __s C string to locate.
5238 * @param __pos Index of character to search back from.
5239 * @param __n Number of characters from s to search for.
5240 * @return Index of start of last occurrence.
5242 * Starting from @a __pos, searches backward for the first @a
5243 * __n characters in @a __s within this string. If found,
5244 * returns the index where it begins. If not found, returns
5248 rfind(const _CharT
* __s
, size_type __pos
, size_type __n
) const
5252 * @brief Find last position of a C string.
5253 * @param __s C string to locate.
5254 * @param __pos Index of character to start search at (default end).
5255 * @return Index of start of last occurrence.
5257 * Starting from @a __pos, searches backward for the value of
5258 * @a __s within this string. If found, returns the index
5259 * where it begins. If not found, returns npos.
5262 rfind(const _CharT
* __s
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
5264 __glibcxx_requires_string(__s
);
5265 return this->rfind(__s
, __pos
, traits_type::length(__s
));
5269 * @brief Find last position of a character.
5270 * @param __c Character to locate.
5271 * @param __pos Index of character to search back from (default end).
5272 * @return Index of last occurrence.
5274 * Starting from @a __pos, searches backward for @a __c within
5275 * this string. If found, returns the index where it was
5276 * found. If not found, returns npos.
5279 rfind(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
;
5281 #if __cplusplus > 201402L
5283 * @brief Find last position of a string_view.
5284 * @param __svt The object convertible to string_view to locate.
5285 * @param __pos Index of character to search back from (default end).
5286 * @return Index of start of last occurrence.
5288 template<typename _Tp
>
5289 _If_sv
<_Tp
, size_type
>
5290 rfind(const _Tp
& __svt
, size_type __pos
= npos
) const
5291 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5293 __sv_type __sv
= __svt
;
5294 return this->rfind(__sv
.data(), __pos
, __sv
.size());
5299 * @brief Find position of a character of string.
5300 * @param __str String containing characters to locate.
5301 * @param __pos Index of character to search from (default 0).
5302 * @return Index of first occurrence.
5304 * Starting from @a __pos, searches forward for one of the
5305 * characters of @a __str within this string. If found,
5306 * returns the index where it was found. If not found, returns
5310 find_first_of(const basic_string
& __str
, size_type __pos
= 0) const
5312 { return this->find_first_of(__str
.data(), __pos
, __str
.size()); }
5315 * @brief Find position of a character of C substring.
5316 * @param __s String containing characters to locate.
5317 * @param __pos Index of character to search from.
5318 * @param __n Number of characters from s to search for.
5319 * @return Index of first occurrence.
5321 * Starting from @a __pos, searches forward for one of the
5322 * first @a __n characters of @a __s within this string. If
5323 * found, returns the index where it was found. If not found,
5327 find_first_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
5331 * @brief Find position of a character of C string.
5332 * @param __s String containing characters to locate.
5333 * @param __pos Index of character to search from (default 0).
5334 * @return Index of first occurrence.
5336 * Starting from @a __pos, searches forward for one of the
5337 * characters of @a __s within this string. If found, returns
5338 * the index where it was found. If not found, returns npos.
5341 find_first_of(const _CharT
* __s
, size_type __pos
= 0) const
5344 __glibcxx_requires_string(__s
);
5345 return this->find_first_of(__s
, __pos
, traits_type::length(__s
));
5349 * @brief Find position of a character.
5350 * @param __c Character to locate.
5351 * @param __pos Index of character to search from (default 0).
5352 * @return Index of first occurrence.
5354 * Starting from @a __pos, searches forward for the character
5355 * @a __c within this string. If found, returns the index
5356 * where it was found. If not found, returns npos.
5358 * Note: equivalent to find(__c, __pos).
5361 find_first_of(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
5362 { return this->find(__c
, __pos
); }
5364 #if __cplusplus > 201402L
5366 * @brief Find position of a character of a string_view.
5367 * @param __svt An object convertible to string_view containing
5368 * characters to locate.
5369 * @param __pos Index of character to search from (default 0).
5370 * @return Index of first occurrence.
5372 template<typename _Tp
>
5373 _If_sv
<_Tp
, size_type
>
5374 find_first_of(const _Tp
& __svt
, size_type __pos
= 0) const
5375 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5377 __sv_type __sv
= __svt
;
5378 return this->find_first_of(__sv
.data(), __pos
, __sv
.size());
5383 * @brief Find last position of a character of string.
5384 * @param __str String containing characters to locate.
5385 * @param __pos Index of character to search back from (default end).
5386 * @return Index of last occurrence.
5388 * Starting from @a __pos, searches backward for one of the
5389 * characters of @a __str within this string. If found,
5390 * returns the index where it was found. If not found, returns
5394 find_last_of(const basic_string
& __str
, size_type __pos
= npos
) const
5396 { return this->find_last_of(__str
.data(), __pos
, __str
.size()); }
5399 * @brief Find last position of a character of C substring.
5400 * @param __s C string containing characters to locate.
5401 * @param __pos Index of character to search back from.
5402 * @param __n Number of characters from s to search for.
5403 * @return Index of last occurrence.
5405 * Starting from @a __pos, searches backward for one of the
5406 * first @a __n characters of @a __s within this string. If
5407 * found, returns the index where it was found. If not found,
5411 find_last_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
5415 * @brief Find last position of a character of C string.
5416 * @param __s C string containing characters to locate.
5417 * @param __pos Index of character to search back from (default end).
5418 * @return Index of last occurrence.
5420 * Starting from @a __pos, searches backward for one of the
5421 * characters of @a __s within this string. If found, returns
5422 * the index where it was found. If not found, returns npos.
5425 find_last_of(const _CharT
* __s
, size_type __pos
= npos
) const
5428 __glibcxx_requires_string(__s
);
5429 return this->find_last_of(__s
, __pos
, traits_type::length(__s
));
5433 * @brief Find last position of a character.
5434 * @param __c Character to locate.
5435 * @param __pos Index of character to search back from (default end).
5436 * @return Index of last occurrence.
5438 * Starting from @a __pos, searches backward for @a __c within
5439 * this string. If found, returns the index where it was
5440 * found. If not found, returns npos.
5442 * Note: equivalent to rfind(__c, __pos).
5445 find_last_of(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
5446 { return this->rfind(__c
, __pos
); }
5448 #if __cplusplus > 201402L
5450 * @brief Find last position of a character of string.
5451 * @param __svt An object convertible to string_view containing
5452 * characters to locate.
5453 * @param __pos Index of character to search back from (default end).
5454 * @return Index of last occurrence.
5456 template<typename _Tp
>
5457 _If_sv
<_Tp
, size_type
>
5458 find_last_of(const _Tp
& __svt
, size_type __pos
= npos
) const
5459 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5461 __sv_type __sv
= __svt
;
5462 return this->find_last_of(__sv
.data(), __pos
, __sv
.size());
5467 * @brief Find position of a character not in string.
5468 * @param __str String containing characters to avoid.
5469 * @param __pos Index of character to search from (default 0).
5470 * @return Index of first occurrence.
5472 * Starting from @a __pos, searches forward for a character not contained
5473 * in @a __str within this string. If found, returns the index where it
5474 * was found. If not found, returns npos.
5477 find_first_not_of(const basic_string
& __str
, size_type __pos
= 0) const
5479 { return this->find_first_not_of(__str
.data(), __pos
, __str
.size()); }
5482 * @brief Find position of a character not in C substring.
5483 * @param __s C string containing characters to avoid.
5484 * @param __pos Index of character to search from.
5485 * @param __n Number of characters from __s to consider.
5486 * @return Index of first occurrence.
5488 * Starting from @a __pos, searches forward for a character not
5489 * contained in the first @a __n characters of @a __s within
5490 * this string. If found, returns the index where it was
5491 * found. If not found, returns npos.
5494 find_first_not_of(const _CharT
* __s
, size_type __pos
,
5495 size_type __n
) const _GLIBCXX_NOEXCEPT
;
5498 * @brief Find position of a character not in C string.
5499 * @param __s C string containing characters to avoid.
5500 * @param __pos Index of character to search from (default 0).
5501 * @return Index of first occurrence.
5503 * Starting from @a __pos, searches forward for a character not
5504 * contained in @a __s within this string. If found, returns
5505 * the index where it was found. If not found, returns npos.
5508 find_first_not_of(const _CharT
* __s
, size_type __pos
= 0) const
5511 __glibcxx_requires_string(__s
);
5512 return this->find_first_not_of(__s
, __pos
, traits_type::length(__s
));
5516 * @brief Find position of a different character.
5517 * @param __c Character to avoid.
5518 * @param __pos Index of character to search from (default 0).
5519 * @return Index of first occurrence.
5521 * Starting from @a __pos, searches forward for a character
5522 * other than @a __c within this string. If found, returns the
5523 * index where it was found. If not found, returns npos.
5526 find_first_not_of(_CharT __c
, size_type __pos
= 0) const
5529 #if __cplusplus > 201402L
5531 * @brief Find position of a character not in a string_view.
5532 * @param __svt An object convertible to string_view containing
5533 * characters to avoid.
5534 * @param __pos Index of character to search from (default 0).
5535 * @return Index of first occurrence.
5537 template<typename _Tp
>
5538 _If_sv
<_Tp
, size_type
>
5539 find_first_not_of(const _Tp
& __svt
, size_type __pos
= 0) const
5540 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5542 __sv_type __sv
= __svt
;
5543 return this->find_first_not_of(__sv
.data(), __pos
, __sv
.size());
5548 * @brief Find last position of a character not in string.
5549 * @param __str String containing characters to avoid.
5550 * @param __pos Index of character to search back from (default end).
5551 * @return Index of last occurrence.
5553 * Starting from @a __pos, searches backward for a character
5554 * not contained in @a __str within this string. If found,
5555 * returns the index where it was found. If not found, returns
5559 find_last_not_of(const basic_string
& __str
, size_type __pos
= npos
) const
5561 { return this->find_last_not_of(__str
.data(), __pos
, __str
.size()); }
5564 * @brief Find last position of a character not in C substring.
5565 * @param __s C string containing characters to avoid.
5566 * @param __pos Index of character to search back from.
5567 * @param __n Number of characters from s to consider.
5568 * @return Index of last occurrence.
5570 * Starting from @a __pos, searches backward for a character not
5571 * contained in the first @a __n characters of @a __s within this string.
5572 * If found, returns the index where it was found. If not found,
5576 find_last_not_of(const _CharT
* __s
, size_type __pos
,
5577 size_type __n
) const _GLIBCXX_NOEXCEPT
;
5579 * @brief Find last position of a character not in C string.
5580 * @param __s C string containing characters to avoid.
5581 * @param __pos Index of character to search back from (default end).
5582 * @return Index of last occurrence.
5584 * Starting from @a __pos, searches backward for a character
5585 * not contained in @a __s within this string. If found,
5586 * returns the index where it was found. If not found, returns
5590 find_last_not_of(const _CharT
* __s
, size_type __pos
= npos
) const
5593 __glibcxx_requires_string(__s
);
5594 return this->find_last_not_of(__s
, __pos
, traits_type::length(__s
));
5598 * @brief Find last position of a different character.
5599 * @param __c Character to avoid.
5600 * @param __pos Index of character to search back from (default end).
5601 * @return Index of last occurrence.
5603 * Starting from @a __pos, searches backward for a character other than
5604 * @a __c within this string. If found, returns the index where it was
5605 * found. If not found, returns npos.
5608 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const
5611 #if __cplusplus > 201402L
5613 * @brief Find last position of a character not in a string_view.
5614 * @param __svt An object convertible to string_view containing
5615 * characters to avoid.
5616 * @param __pos Index of character to search back from (default end).
5617 * @return Index of last occurrence.
5619 template<typename _Tp
>
5620 _If_sv
<_Tp
, size_type
>
5621 find_last_not_of(const _Tp
& __svt
, size_type __pos
= npos
) const
5622 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5624 __sv_type __sv
= __svt
;
5625 return this->find_last_not_of(__sv
.data(), __pos
, __sv
.size());
5630 * @brief Get a substring.
5631 * @param __pos Index of first character (default 0).
5632 * @param __n Number of characters in substring (default remainder).
5633 * @return The new string.
5634 * @throw std::out_of_range If __pos > size().
5636 * Construct and return a new string using the @a __n
5637 * characters starting at @a __pos. If the string is too
5638 * short, use the remainder of the characters. If @a __pos is
5639 * beyond the end of the string, out_of_range is thrown.
5642 substr(size_type __pos
= 0, size_type __n
= npos
) const
5643 { return basic_string(*this,
5644 _M_check(__pos
, "basic_string::substr"), __n
); }
5647 * @brief Compare to a string.
5648 * @param __str String to compare against.
5649 * @return Integer < 0, 0, or > 0.
5651 * Returns an integer < 0 if this string is ordered before @a
5652 * __str, 0 if their values are equivalent, or > 0 if this
5653 * string is ordered after @a __str. Determines the effective
5654 * length rlen of the strings to compare as the smallest of
5655 * size() and str.size(). The function then compares the two
5656 * strings by calling traits::compare(data(), str.data(),rlen).
5657 * If the result of the comparison is nonzero returns it,
5658 * otherwise the shorter one is ordered first.
5661 compare(const basic_string
& __str
) const
5663 const size_type __size
= this->size();
5664 const size_type __osize
= __str
.size();
5665 const size_type __len
= std::min(__size
, __osize
);
5667 int __r
= traits_type::compare(_M_data(), __str
.data(), __len
);
5669 __r
= _S_compare(__size
, __osize
);
5673 #if __cplusplus > 201402L
5675 * @brief Compare to a string_view.
5676 * @param __svt An object convertible to string_view to compare against.
5677 * @return Integer < 0, 0, or > 0.
5679 template<typename _Tp
>
5681 compare(const _Tp
& __svt
) const
5682 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5684 __sv_type __sv
= __svt
;
5685 const size_type __size
= this->size();
5686 const size_type __osize
= __sv
.size();
5687 const size_type __len
= std::min(__size
, __osize
);
5689 int __r
= traits_type::compare(_M_data(), __sv
.data(), __len
);
5691 __r
= _S_compare(__size
, __osize
);
5696 * @brief Compare to a string_view.
5697 * @param __pos A position in the string to start comparing from.
5698 * @param __n The number of characters to compare.
5699 * @param __svt An object convertible to string_view to compare
5701 * @return Integer < 0, 0, or > 0.
5703 template<typename _Tp
>
5705 compare(size_type __pos
, size_type __n
, const _Tp
& __svt
) const
5706 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5708 __sv_type __sv
= __svt
;
5709 return __sv_type(*this).substr(__pos
, __n
).compare(__sv
);
5713 * @brief Compare to a string_view.
5714 * @param __pos1 A position in the string to start comparing from.
5715 * @param __n1 The number of characters to compare.
5716 * @param __svt An object convertible to string_view to compare
5718 * @param __pos2 A position in the string_view to start comparing from.
5719 * @param __n2 The number of characters to compare.
5720 * @return Integer < 0, 0, or > 0.
5722 template<typename _Tp
>
5724 compare(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
5725 size_type __pos2
, size_type __n2
= npos
) const
5726 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5728 __sv_type __sv
= __svt
;
5729 return __sv_type(*this)
5730 .substr(__pos1
, __n1
).compare(__sv
.substr(__pos2
, __n2
));
5735 * @brief Compare substring to a string.
5736 * @param __pos Index of first character of substring.
5737 * @param __n Number of characters in substring.
5738 * @param __str String to compare against.
5739 * @return Integer < 0, 0, or > 0.
5741 * Form the substring of this string from the @a __n characters
5742 * starting at @a __pos. Returns an integer < 0 if the
5743 * substring is ordered before @a __str, 0 if their values are
5744 * equivalent, or > 0 if the substring is ordered after @a
5745 * __str. Determines the effective length rlen of the strings
5746 * to compare as the smallest of the length of the substring
5747 * and @a __str.size(). The function then compares the two
5748 * strings by calling
5749 * traits::compare(substring.data(),str.data(),rlen). If the
5750 * result of the comparison is nonzero returns it, otherwise
5751 * the shorter one is ordered first.
5754 compare(size_type __pos
, size_type __n
, const basic_string
& __str
) const;
5757 * @brief Compare substring to a substring.
5758 * @param __pos1 Index of first character of substring.
5759 * @param __n1 Number of characters in substring.
5760 * @param __str String to compare against.
5761 * @param __pos2 Index of first character of substring of str.
5762 * @param __n2 Number of characters in substring of str.
5763 * @return Integer < 0, 0, or > 0.
5765 * Form the substring of this string from the @a __n1
5766 * characters starting at @a __pos1. Form the substring of @a
5767 * __str from the @a __n2 characters starting at @a __pos2.
5768 * Returns an integer < 0 if this substring is ordered before
5769 * the substring of @a __str, 0 if their values are equivalent,
5770 * or > 0 if this substring is ordered after the substring of
5771 * @a __str. Determines the effective length rlen of the
5772 * strings to compare as the smallest of the lengths of the
5773 * substrings. The function then compares the two strings by
5775 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
5776 * If the result of the comparison is nonzero returns it,
5777 * otherwise the shorter one is ordered first.
5780 compare(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
5781 size_type __pos2
, size_type __n2
) const;
5784 * @brief Compare to a C string.
5785 * @param __s C string to compare against.
5786 * @return Integer < 0, 0, or > 0.
5788 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
5789 * their values are equivalent, or > 0 if this string is ordered after
5790 * @a __s. Determines the effective length rlen of the strings to
5791 * compare as the smallest of size() and the length of a string
5792 * constructed from @a __s. The function then compares the two strings
5793 * by calling traits::compare(data(),s,rlen). If the result of the
5794 * comparison is nonzero returns it, otherwise the shorter one is
5798 compare(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
;
5800 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5801 // 5 String::compare specification questionable
5803 * @brief Compare substring to a C string.
5804 * @param __pos Index of first character of substring.
5805 * @param __n1 Number of characters in substring.
5806 * @param __s C string to compare against.
5807 * @return Integer < 0, 0, or > 0.
5809 * Form the substring of this string from the @a __n1
5810 * characters starting at @a pos. Returns an integer < 0 if
5811 * the substring is ordered before @a __s, 0 if their values
5812 * are equivalent, or > 0 if the substring is ordered after @a
5813 * __s. Determines the effective length rlen of the strings to
5814 * compare as the smallest of the length of the substring and
5815 * the length of a string constructed from @a __s. The
5816 * function then compares the two string by calling
5817 * traits::compare(substring.data(),__s,rlen). If the result of
5818 * the comparison is nonzero returns it, otherwise the shorter
5819 * one is ordered first.
5822 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
) const;
5825 * @brief Compare substring against a character %array.
5826 * @param __pos Index of first character of substring.
5827 * @param __n1 Number of characters in substring.
5828 * @param __s character %array to compare against.
5829 * @param __n2 Number of characters of s.
5830 * @return Integer < 0, 0, or > 0.
5832 * Form the substring of this string from the @a __n1
5833 * characters starting at @a __pos. Form a string from the
5834 * first @a __n2 characters of @a __s. Returns an integer < 0
5835 * if this substring is ordered before the string from @a __s,
5836 * 0 if their values are equivalent, or > 0 if this substring
5837 * is ordered after the string from @a __s. Determines the
5838 * effective length rlen of the strings to compare as the
5839 * smallest of the length of the substring and @a __n2. The
5840 * function then compares the two strings by calling
5841 * traits::compare(substring.data(),s,rlen). If the result of
5842 * the comparison is nonzero returns it, otherwise the shorter
5843 * one is ordered first.
5845 * NB: s must have at least n2 characters, '\\0' has
5846 * no special meaning.
5849 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
,
5850 size_type __n2
) const;
5852 # ifdef _GLIBCXX_TM_TS_INTERNAL
5854 ::_txnal_cow_string_C1_for_exceptions(void* that
, const char* s
,
5857 ::_txnal_cow_string_c_str(const void *that
);
5859 ::_txnal_cow_string_D1(void *that
);
5861 ::_txnal_cow_string_D1_commit(void *that
);
5864 #endif // !_GLIBCXX_USE_CXX11_ABI
5866 #if __cpp_deduction_guides >= 201606
5867 _GLIBCXX_BEGIN_NAMESPACE_CXX11
5868 template<typename _InputIterator
, typename _CharT
5869 = typename iterator_traits
<_InputIterator
>::value_type
,
5870 typename _Allocator
= allocator
<_CharT
>,
5871 typename
= _RequireInputIter
<_InputIterator
>,
5872 typename
= _RequireAllocator
<_Allocator
>>
5873 basic_string(_InputIterator
, _InputIterator
, _Allocator
= _Allocator())
5874 -> basic_string
<_CharT
, char_traits
<_CharT
>, _Allocator
>;
5875 _GLIBCXX_END_NAMESPACE_CXX11
5880 * @brief Concatenate two strings.
5881 * @param __lhs First string.
5882 * @param __rhs Last string.
5883 * @return New string with value of @a __lhs followed by @a __rhs.
5885 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5886 basic_string
<_CharT
, _Traits
, _Alloc
>
5887 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5888 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5890 basic_string
<_CharT
, _Traits
, _Alloc
> __str(__lhs
);
5891 __str
.append(__rhs
);
5896 * @brief Concatenate C string and string.
5897 * @param __lhs First string.
5898 * @param __rhs Last string.
5899 * @return New string with value of @a __lhs followed by @a __rhs.
5901 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5902 basic_string
<_CharT
,_Traits
,_Alloc
>
5903 operator+(const _CharT
* __lhs
,
5904 const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
);
5907 * @brief Concatenate character and string.
5908 * @param __lhs First string.
5909 * @param __rhs Last string.
5910 * @return New string with @a __lhs followed by @a __rhs.
5912 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5913 basic_string
<_CharT
,_Traits
,_Alloc
>
5914 operator+(_CharT __lhs
, const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
);
5917 * @brief Concatenate string and C string.
5918 * @param __lhs First string.
5919 * @param __rhs Last string.
5920 * @return New string with @a __lhs followed by @a __rhs.
5922 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5923 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5924 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5925 const _CharT
* __rhs
)
5927 basic_string
<_CharT
, _Traits
, _Alloc
> __str(__lhs
);
5928 __str
.append(__rhs
);
5933 * @brief Concatenate string and character.
5934 * @param __lhs First string.
5935 * @param __rhs Last string.
5936 * @return New string with @a __lhs followed by @a __rhs.
5938 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5939 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5940 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
, _CharT __rhs
)
5942 typedef basic_string
<_CharT
, _Traits
, _Alloc
> __string_type
;
5943 typedef typename
__string_type::size_type __size_type
;
5944 __string_type
__str(__lhs
);
5945 __str
.append(__size_type(1), __rhs
);
5949 #if __cplusplus >= 201103L
5950 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5951 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5952 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
5953 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5954 { return std::move(__lhs
.append(__rhs
)); }
5956 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5957 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5958 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5959 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
5960 { return std::move(__rhs
.insert(0, __lhs
)); }
5962 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5963 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5964 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
5965 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
5967 const auto __size
= __lhs
.size() + __rhs
.size();
5968 const bool __cond
= (__size
> __lhs
.capacity()
5969 && __size
<= __rhs
.capacity());
5970 return __cond
? std::move(__rhs
.insert(0, __lhs
))
5971 : std::move(__lhs
.append(__rhs
));
5974 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5975 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5976 operator+(const _CharT
* __lhs
,
5977 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
5978 { return std::move(__rhs
.insert(0, __lhs
)); }
5980 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5981 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5982 operator+(_CharT __lhs
,
5983 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
5984 { return std::move(__rhs
.insert(0, 1, __lhs
)); }
5986 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5987 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5988 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
5989 const _CharT
* __rhs
)
5990 { return std::move(__lhs
.append(__rhs
)); }
5992 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5993 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5994 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
5996 { return std::move(__lhs
.append(1, __rhs
)); }
6001 * @brief Test equivalence of two strings.
6002 * @param __lhs First string.
6003 * @param __rhs Second string.
6004 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
6006 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6008 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6009 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6011 { return __lhs
.compare(__rhs
) == 0; }
6013 template<typename _CharT
>
6015 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
, bool>::__type
6016 operator==(const basic_string
<_CharT
>& __lhs
,
6017 const basic_string
<_CharT
>& __rhs
) _GLIBCXX_NOEXCEPT
6018 { return (__lhs
.size() == __rhs
.size()
6019 && !std::char_traits
<_CharT
>::compare(__lhs
.data(), __rhs
.data(),
6023 * @brief Test equivalence of C string and string.
6024 * @param __lhs C string.
6025 * @param __rhs String.
6026 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
6028 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6030 operator==(const _CharT
* __lhs
,
6031 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6032 { return __rhs
.compare(__lhs
) == 0; }
6035 * @brief Test equivalence of string and C string.
6036 * @param __lhs String.
6037 * @param __rhs C string.
6038 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
6040 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6042 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6043 const _CharT
* __rhs
)
6044 { return __lhs
.compare(__rhs
) == 0; }
6048 * @brief Test difference of two strings.
6049 * @param __lhs First string.
6050 * @param __rhs Second string.
6051 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
6053 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6055 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6056 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6058 { return !(__lhs
== __rhs
); }
6061 * @brief Test difference of C string and string.
6062 * @param __lhs C string.
6063 * @param __rhs String.
6064 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
6066 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6068 operator!=(const _CharT
* __lhs
,
6069 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6070 { return !(__lhs
== __rhs
); }
6073 * @brief Test difference of string and C string.
6074 * @param __lhs String.
6075 * @param __rhs C string.
6076 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
6078 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6080 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6081 const _CharT
* __rhs
)
6082 { return !(__lhs
== __rhs
); }
6086 * @brief Test if string precedes string.
6087 * @param __lhs First string.
6088 * @param __rhs Second string.
6089 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6091 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6093 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6094 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6096 { return __lhs
.compare(__rhs
) < 0; }
6099 * @brief Test if string precedes C string.
6100 * @param __lhs String.
6101 * @param __rhs C string.
6102 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6104 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6106 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6107 const _CharT
* __rhs
)
6108 { return __lhs
.compare(__rhs
) < 0; }
6111 * @brief Test if C string precedes string.
6112 * @param __lhs C string.
6113 * @param __rhs String.
6114 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6116 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6118 operator<(const _CharT
* __lhs
,
6119 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6120 { return __rhs
.compare(__lhs
) > 0; }
6124 * @brief Test if string follows string.
6125 * @param __lhs First string.
6126 * @param __rhs Second string.
6127 * @return True if @a __lhs follows @a __rhs. False otherwise.
6129 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6131 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6132 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6134 { return __lhs
.compare(__rhs
) > 0; }
6137 * @brief Test if string follows C string.
6138 * @param __lhs String.
6139 * @param __rhs C string.
6140 * @return True if @a __lhs follows @a __rhs. False otherwise.
6142 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6144 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6145 const _CharT
* __rhs
)
6146 { return __lhs
.compare(__rhs
) > 0; }
6149 * @brief Test if C string follows string.
6150 * @param __lhs C string.
6151 * @param __rhs String.
6152 * @return True if @a __lhs follows @a __rhs. False otherwise.
6154 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6156 operator>(const _CharT
* __lhs
,
6157 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6158 { return __rhs
.compare(__lhs
) < 0; }
6162 * @brief Test if string doesn't follow string.
6163 * @param __lhs First string.
6164 * @param __rhs Second string.
6165 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6167 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6169 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6170 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6172 { return __lhs
.compare(__rhs
) <= 0; }
6175 * @brief Test if string doesn't follow C string.
6176 * @param __lhs String.
6177 * @param __rhs C string.
6178 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6180 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6182 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6183 const _CharT
* __rhs
)
6184 { return __lhs
.compare(__rhs
) <= 0; }
6187 * @brief Test if C string doesn't follow string.
6188 * @param __lhs C string.
6189 * @param __rhs String.
6190 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6192 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6194 operator<=(const _CharT
* __lhs
,
6195 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6196 { return __rhs
.compare(__lhs
) >= 0; }
6200 * @brief Test if string doesn't precede string.
6201 * @param __lhs First string.
6202 * @param __rhs Second string.
6203 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6205 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6207 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6208 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6210 { return __lhs
.compare(__rhs
) >= 0; }
6213 * @brief Test if string doesn't precede C string.
6214 * @param __lhs String.
6215 * @param __rhs C string.
6216 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6218 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6220 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6221 const _CharT
* __rhs
)
6222 { return __lhs
.compare(__rhs
) >= 0; }
6225 * @brief Test if C string doesn't precede string.
6226 * @param __lhs C string.
6227 * @param __rhs String.
6228 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6230 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6232 operator>=(const _CharT
* __lhs
,
6233 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6234 { return __rhs
.compare(__lhs
) <= 0; }
6237 * @brief Swap contents of two strings.
6238 * @param __lhs First string.
6239 * @param __rhs Second string.
6241 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
6243 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6245 swap(basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6246 basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6247 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs
.swap(__rhs
)))
6248 { __lhs
.swap(__rhs
); }
6252 * @brief Read stream into a string.
6253 * @param __is Input stream.
6254 * @param __str Buffer to store into.
6255 * @return Reference to the input stream.
6257 * Stores characters from @a __is into @a __str until whitespace is
6258 * found, the end of the stream is encountered, or str.max_size()
6259 * is reached. If is.width() is non-zero, that is the limit on the
6260 * number of characters stored into @a __str. Any previous
6261 * contents of @a __str are erased.
6263 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6264 basic_istream
<_CharT
, _Traits
>&
6265 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
6266 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
);
6269 basic_istream
<char>&
6270 operator>>(basic_istream
<char>& __is
, basic_string
<char>& __str
);
6273 * @brief Write string to a stream.
6274 * @param __os Output stream.
6275 * @param __str String to write out.
6276 * @return Reference to the output stream.
6278 * Output characters of @a __str into os following the same rules as for
6279 * writing a C string.
6281 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6282 inline basic_ostream
<_CharT
, _Traits
>&
6283 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
6284 const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
6286 // _GLIBCXX_RESOLVE_LIB_DEFECTS
6287 // 586. string inserter not a formatted function
6288 return __ostream_insert(__os
, __str
.data(), __str
.size());
6292 * @brief Read a line from stream into a string.
6293 * @param __is Input stream.
6294 * @param __str Buffer to store into.
6295 * @param __delim Character marking end of line.
6296 * @return Reference to the input stream.
6298 * Stores characters from @a __is into @a __str until @a __delim is
6299 * found, the end of the stream is encountered, or str.max_size()
6300 * is reached. Any previous contents of @a __str are erased. If
6301 * @a __delim is encountered, it is extracted but not stored into
6304 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6305 basic_istream
<_CharT
, _Traits
>&
6306 getline(basic_istream
<_CharT
, _Traits
>& __is
,
6307 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
);
6310 * @brief Read a line from stream into a string.
6311 * @param __is Input stream.
6312 * @param __str Buffer to store into.
6313 * @return Reference to the input stream.
6315 * Stores characters from is into @a __str until '\n' is
6316 * found, the end of the stream is encountered, or str.max_size()
6317 * is reached. Any previous contents of @a __str are erased. If
6318 * end of line is encountered, it is extracted but not stored into
6321 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6322 inline basic_istream
<_CharT
, _Traits
>&
6323 getline(basic_istream
<_CharT
, _Traits
>& __is
,
6324 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
6325 { return std::getline(__is
, __str
, __is
.widen('\n')); }
6327 #if __cplusplus >= 201103L
6328 /// Read a line from an rvalue stream into a string.
6329 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6330 inline basic_istream
<_CharT
, _Traits
>&
6331 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
6332 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
)
6333 { return std::getline(__is
, __str
, __delim
); }
6335 /// Read a line from an rvalue stream into a string.
6336 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6337 inline basic_istream
<_CharT
, _Traits
>&
6338 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
6339 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
6340 { return std::getline(__is
, __str
); }
6344 basic_istream
<char>&
6345 getline(basic_istream
<char>& __in
, basic_string
<char>& __str
,
6348 #ifdef _GLIBCXX_USE_WCHAR_T
6350 basic_istream
<wchar_t>&
6351 getline(basic_istream
<wchar_t>& __in
, basic_string
<wchar_t>& __str
,
6355 _GLIBCXX_END_NAMESPACE_VERSION
6358 #if __cplusplus >= 201103L
6360 #include <ext/string_conversions.h>
6362 namespace std
_GLIBCXX_VISIBILITY(default)
6364 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6365 _GLIBCXX_BEGIN_NAMESPACE_CXX11
6367 #if _GLIBCXX_USE_C99_STDLIB
6368 // 21.4 Numeric Conversions [string.conversions].
6370 stoi(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6371 { return __gnu_cxx::__stoa
<long, int>(&std::strtol
, "stoi", __str
.c_str(),
6375 stol(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6376 { return __gnu_cxx::__stoa(&std::strtol
, "stol", __str
.c_str(),
6379 inline unsigned long
6380 stoul(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6381 { return __gnu_cxx::__stoa(&std::strtoul
, "stoul", __str
.c_str(),
6385 stoll(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6386 { return __gnu_cxx::__stoa(&std::strtoll
, "stoll", __str
.c_str(),
6389 inline unsigned long long
6390 stoull(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6391 { return __gnu_cxx::__stoa(&std::strtoull
, "stoull", __str
.c_str(),
6394 // NB: strtof vs strtod.
6396 stof(const string
& __str
, size_t* __idx
= 0)
6397 { return __gnu_cxx::__stoa(&std::strtof
, "stof", __str
.c_str(), __idx
); }
6400 stod(const string
& __str
, size_t* __idx
= 0)
6401 { return __gnu_cxx::__stoa(&std::strtod
, "stod", __str
.c_str(), __idx
); }
6404 stold(const string
& __str
, size_t* __idx
= 0)
6405 { return __gnu_cxx::__stoa(&std::strtold
, "stold", __str
.c_str(), __idx
); }
6406 #endif // _GLIBCXX_USE_C99_STDLIB
6408 #if _GLIBCXX_USE_C99_STDIO
6409 // NB: (v)snprintf vs sprintf.
6413 to_string(int __val
)
6414 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, 4 * sizeof(int),
6418 to_string(unsigned __val
)
6419 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
6420 4 * sizeof(unsigned),
6424 to_string(long __val
)
6425 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, 4 * sizeof(long),
6429 to_string(unsigned long __val
)
6430 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
6431 4 * sizeof(unsigned long),
6435 to_string(long long __val
)
6436 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
6437 4 * sizeof(long long),
6441 to_string(unsigned long long __val
)
6442 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
6443 4 * sizeof(unsigned long long),
6447 to_string(float __val
)
6450 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
6451 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
6456 to_string(double __val
)
6459 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
6460 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
6465 to_string(long double __val
)
6468 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
6469 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
6472 #endif // _GLIBCXX_USE_C99_STDIO
6474 #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
6476 stoi(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6477 { return __gnu_cxx::__stoa
<long, int>(&std::wcstol
, "stoi", __str
.c_str(),
6481 stol(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6482 { return __gnu_cxx::__stoa(&std::wcstol
, "stol", __str
.c_str(),
6485 inline unsigned long
6486 stoul(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6487 { return __gnu_cxx::__stoa(&std::wcstoul
, "stoul", __str
.c_str(),
6491 stoll(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6492 { return __gnu_cxx::__stoa(&std::wcstoll
, "stoll", __str
.c_str(),
6495 inline unsigned long long
6496 stoull(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6497 { return __gnu_cxx::__stoa(&std::wcstoull
, "stoull", __str
.c_str(),
6500 // NB: wcstof vs wcstod.
6502 stof(const wstring
& __str
, size_t* __idx
= 0)
6503 { return __gnu_cxx::__stoa(&std::wcstof
, "stof", __str
.c_str(), __idx
); }
6506 stod(const wstring
& __str
, size_t* __idx
= 0)
6507 { return __gnu_cxx::__stoa(&std::wcstod
, "stod", __str
.c_str(), __idx
); }
6510 stold(const wstring
& __str
, size_t* __idx
= 0)
6511 { return __gnu_cxx::__stoa(&std::wcstold
, "stold", __str
.c_str(), __idx
); }
6513 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6516 to_wstring(int __val
)
6517 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(int),
6521 to_wstring(unsigned __val
)
6522 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
6523 4 * sizeof(unsigned),
6527 to_wstring(long __val
)
6528 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(long),
6532 to_wstring(unsigned long __val
)
6533 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
6534 4 * sizeof(unsigned long),
6538 to_wstring(long long __val
)
6539 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
6540 4 * sizeof(long long),
6544 to_wstring(unsigned long long __val
)
6545 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
6546 4 * sizeof(unsigned long long),
6550 to_wstring(float __val
)
6553 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
6554 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
6559 to_wstring(double __val
)
6562 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
6563 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
6568 to_wstring(long double __val
)
6571 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
6572 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
6575 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6576 #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
6578 _GLIBCXX_END_NAMESPACE_CXX11
6579 _GLIBCXX_END_NAMESPACE_VERSION
6584 #if __cplusplus >= 201103L
6586 #include <bits/functional_hash.h>
6588 namespace std
_GLIBCXX_VISIBILITY(default)
6590 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6594 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
6595 /// std::hash specialization for string.
6598 : public __hash_base
<size_t, string
>
6601 operator()(const string
& __s
) const noexcept
6602 { return std::_Hash_impl::hash(__s
.data(), __s
.length()); }
6606 struct __is_fast_hash
<hash
<string
>> : std::false_type
6609 #ifdef _GLIBCXX_USE_WCHAR_T
6610 /// std::hash specialization for wstring.
6612 struct hash
<wstring
>
6613 : public __hash_base
<size_t, wstring
>
6616 operator()(const wstring
& __s
) const noexcept
6617 { return std::_Hash_impl::hash(__s
.data(),
6618 __s
.length() * sizeof(wchar_t)); }
6622 struct __is_fast_hash
<hash
<wstring
>> : std::false_type
6625 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
6627 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
6628 /// std::hash specialization for u16string.
6630 struct hash
<u16string
>
6631 : public __hash_base
<size_t, u16string
>
6634 operator()(const u16string
& __s
) const noexcept
6635 { return std::_Hash_impl::hash(__s
.data(),
6636 __s
.length() * sizeof(char16_t
)); }
6640 struct __is_fast_hash
<hash
<u16string
>> : std::false_type
6643 /// std::hash specialization for u32string.
6645 struct hash
<u32string
>
6646 : public __hash_base
<size_t, u32string
>
6649 operator()(const u32string
& __s
) const noexcept
6650 { return std::_Hash_impl::hash(__s
.data(),
6651 __s
.length() * sizeof(char32_t
)); }
6655 struct __is_fast_hash
<hash
<u32string
>> : std::false_type
6659 #if __cplusplus > 201103L
6661 #define __cpp_lib_string_udls 201304
6663 inline namespace literals
6665 inline namespace string_literals
6667 _GLIBCXX_DEFAULT_ABI_TAG
6668 inline basic_string
<char>
6669 operator""s(const char* __str
, size_t __len
)
6670 { return basic_string
<char>{__str
, __len
}; }
6672 #ifdef _GLIBCXX_USE_WCHAR_T
6673 _GLIBCXX_DEFAULT_ABI_TAG
6674 inline basic_string
<wchar_t>
6675 operator""s(const wchar_t* __str
, size_t __len
)
6676 { return basic_string
<wchar_t>{__str
, __len
}; }
6679 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
6680 _GLIBCXX_DEFAULT_ABI_TAG
6681 inline basic_string
<char16_t
>
6682 operator""s(const char16_t
* __str
, size_t __len
)
6683 { return basic_string
<char16_t
>{__str
, __len
}; }
6685 _GLIBCXX_DEFAULT_ABI_TAG
6686 inline basic_string
<char32_t
>
6687 operator""s(const char32_t
* __str
, size_t __len
)
6688 { return basic_string
<char32_t
>{__str
, __len
}; }
6691 } // inline namespace string_literals
6692 } // inline namespace literals
6694 #endif // __cplusplus > 201103L
6696 _GLIBCXX_END_NAMESPACE_VERSION
6701 #endif /* _BASIC_STRING_H */