1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997-2015 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 namespace std
_GLIBCXX_VISIBILITY(default)
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
51 #if _GLIBCXX_USE_CXX11_ABI
52 _GLIBCXX_BEGIN_NAMESPACE_CXX11
54 * @class basic_string basic_string.h <string>
55 * @brief Managing sequences of characters and character-like objects.
60 * @tparam _CharT Type of character
61 * @tparam _Traits Traits for character type, defaults to
62 * char_traits<_CharT>.
63 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
65 * Meets the requirements of a <a href="tables.html#65">container</a>, a
66 * <a href="tables.html#66">reversible container</a>, and a
67 * <a href="tables.html#67">sequence</a>. Of the
68 * <a href="tables.html#68">optional sequence requirements</a>, only
69 * @c push_back, @c at, and @c %array access are supported.
71 template<typename _CharT
, typename _Traits
, typename _Alloc
>
74 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
75 rebind
<_CharT
>::other _Char_alloc_type
;
76 typedef __gnu_cxx::__alloc_traits
<_Char_alloc_type
> _Alloc_traits
;
80 typedef _Traits traits_type
;
81 typedef typename
_Traits::char_type value_type
;
82 typedef _Char_alloc_type allocator_type
;
83 typedef typename
_Alloc_traits::size_type size_type
;
84 typedef typename
_Alloc_traits::difference_type difference_type
;
85 typedef typename
_Alloc_traits::reference reference
;
86 typedef typename
_Alloc_traits::const_reference const_reference
;
87 typedef typename
_Alloc_traits::pointer pointer
;
88 typedef typename
_Alloc_traits::const_pointer const_pointer
;
89 typedef __gnu_cxx::__normal_iterator
<pointer
, basic_string
> iterator
;
90 typedef __gnu_cxx::__normal_iterator
<const_pointer
, basic_string
>
92 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
93 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
95 /// Value returned by various member functions when they fail.
96 static const size_type npos
= static_cast<size_type
>(-1);
99 // type used for positions in insert, erase etc.
100 #if __cplusplus < 201103L
101 typedef iterator __const_iterator
;
103 typedef const_iterator __const_iterator
;
106 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
107 struct _Alloc_hider
: allocator_type
// TODO check __is_final
109 _Alloc_hider(pointer __dat
, const _Alloc
& __a
= _Alloc())
110 : allocator_type(__a
), _M_p(__dat
) { }
112 pointer _M_p
; // The actual data.
115 _Alloc_hider _M_dataplus
;
116 size_type _M_string_length
;
118 enum { _S_local_capacity
= 15 / sizeof(_CharT
) };
122 _CharT _M_local_buf
[_S_local_capacity
+ 1];
123 size_type _M_allocated_capacity
;
128 { _M_dataplus
._M_p
= __p
; }
131 _M_length(size_type __length
)
132 { _M_string_length
= __length
; }
136 { return _M_dataplus
._M_p
; }
141 #if __cplusplus >= 201103L
142 return std::pointer_traits
<pointer
>::pointer_to(*_M_local_buf
);
144 return pointer(_M_local_buf
);
149 _M_local_data() const
151 #if __cplusplus >= 201103L
152 return std::pointer_traits
<const_pointer
>::pointer_to(*_M_local_buf
);
154 return const_pointer(_M_local_buf
);
159 _M_capacity(size_type __capacity
)
160 { _M_allocated_capacity
= __capacity
; }
163 _M_set_length(size_type __n
)
166 traits_type::assign(_M_data()[__n
], _CharT());
171 { return _M_data() == _M_local_data(); }
175 _M_create(size_type
&, size_type
);
181 _M_destroy(_M_allocated_capacity
);
185 _M_destroy(size_type __size
) throw()
186 { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size
+ 1); }
188 // _M_construct_aux is used to implement the 21.3.1 para 15 which
189 // requires special behaviour if _InIterator is an integral type
190 template<typename _InIterator
>
192 _M_construct_aux(_InIterator __beg
, _InIterator __end
,
195 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
196 _M_construct(__beg
, __end
, _Tag());
199 // _GLIBCXX_RESOLVE_LIB_DEFECTS
200 // 438. Ambiguity in the "do the right thing" clause
201 template<typename _Integer
>
203 _M_construct_aux(_Integer __beg
, _Integer __end
, std::__true_type
)
204 { _M_construct_aux_2(static_cast<size_type
>(__beg
), __end
); }
207 _M_construct_aux_2(size_type __req
, _CharT __c
)
208 { _M_construct(__req
, __c
); }
210 template<typename _InIterator
>
212 _M_construct(_InIterator __beg
, _InIterator __end
)
214 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
215 _M_construct_aux(__beg
, __end
, _Integral());
218 // For Input Iterators, used in istreambuf_iterators, etc.
219 template<typename _InIterator
>
221 _M_construct(_InIterator __beg
, _InIterator __end
,
222 std::input_iterator_tag
);
224 // For forward_iterators up to random_access_iterators, used for
225 // string::iterator, _CharT*, etc.
226 template<typename _FwdIterator
>
228 _M_construct(_FwdIterator __beg
, _FwdIterator __end
,
229 std::forward_iterator_tag
);
232 _M_construct(size_type __req
, _CharT __c
);
236 { return _M_dataplus
; }
238 const allocator_type
&
239 _M_get_allocator() const
240 { return _M_dataplus
; }
244 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
245 // The explicit instantiations in misc-inst.cc require this due to
246 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
247 template<typename _Tp
, bool _Requires
=
248 !__are_same
<_Tp
, _CharT
*>::__value
249 && !__are_same
<_Tp
, const _CharT
*>::__value
250 && !__are_same
<_Tp
, iterator
>::__value
251 && !__are_same
<_Tp
, const_iterator
>::__value
>
252 struct __enable_if_not_native_iterator
253 { typedef basic_string
& __type
; };
254 template<typename _Tp
>
255 struct __enable_if_not_native_iterator
<_Tp
, false> { };
259 _M_check(size_type __pos
, const char* __s
) const
261 if (__pos
> this->size())
262 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
263 "this->size() (which is %zu)"),
264 __s
, __pos
, this->size());
269 _M_check_length(size_type __n1
, size_type __n2
, const char* __s
) const
271 if (this->max_size() - (this->size() - __n1
) < __n2
)
272 __throw_length_error(__N(__s
));
276 // NB: _M_limit doesn't check for a bad __pos value.
278 _M_limit(size_type __pos
, size_type __off
) const _GLIBCXX_NOEXCEPT
280 const bool __testoff
= __off
< this->size() - __pos
;
281 return __testoff
? __off
: this->size() - __pos
;
284 // True if _Rep and source do not overlap.
286 _M_disjunct(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
288 return (less
<const _CharT
*>()(__s
, _M_data())
289 || less
<const _CharT
*>()(_M_data() + this->size(), __s
));
292 // When __n = 1 way faster than the general multichar
293 // traits_type::copy/move/assign.
295 _S_copy(_CharT
* __d
, const _CharT
* __s
, size_type __n
)
298 traits_type::assign(*__d
, *__s
);
300 traits_type::copy(__d
, __s
, __n
);
304 _S_move(_CharT
* __d
, const _CharT
* __s
, size_type __n
)
307 traits_type::assign(*__d
, *__s
);
309 traits_type::move(__d
, __s
, __n
);
313 _S_assign(_CharT
* __d
, size_type __n
, _CharT __c
)
316 traits_type::assign(*__d
, __c
);
318 traits_type::assign(__d
, __n
, __c
);
321 // _S_copy_chars is a separate template to permit specialization
322 // to optimize for the common case of pointers as iterators.
323 template<class _Iterator
>
325 _S_copy_chars(_CharT
* __p
, _Iterator __k1
, _Iterator __k2
)
328 for (; __k1
!= __k2
; ++__k1
, (void)++__p
)
329 traits_type::assign(*__p
, *__k1
); // These types are off.
333 _S_copy_chars(_CharT
* __p
, iterator __k1
, iterator __k2
) _GLIBCXX_NOEXCEPT
334 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
337 _S_copy_chars(_CharT
* __p
, const_iterator __k1
, const_iterator __k2
)
339 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
342 _S_copy_chars(_CharT
* __p
, _CharT
* __k1
, _CharT
* __k2
) _GLIBCXX_NOEXCEPT
343 { _S_copy(__p
, __k1
, __k2
- __k1
); }
346 _S_copy_chars(_CharT
* __p
, const _CharT
* __k1
, const _CharT
* __k2
)
348 { _S_copy(__p
, __k1
, __k2
- __k1
); }
351 _S_compare(size_type __n1
, size_type __n2
) _GLIBCXX_NOEXCEPT
353 const difference_type __d
= difference_type(__n1
- __n2
);
355 if (__d
> __gnu_cxx::__numeric_traits
<int>::__max
)
356 return __gnu_cxx::__numeric_traits
<int>::__max
;
357 else if (__d
< __gnu_cxx::__numeric_traits
<int>::__min
)
358 return __gnu_cxx::__numeric_traits
<int>::__min
;
364 _M_assign(const basic_string
& __rcs
);
367 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
371 _M_erase(size_type __pos
, size_type __n
);
374 // Construct/copy/destroy:
375 // NB: We overload ctors in some cases instead of using default
376 // arguments, per 17.4.4.4 para. 2 item 2.
379 * @brief Default constructor creates an empty string.
382 #if __cplusplus >= 201103L
383 noexcept(is_nothrow_default_constructible
<_Alloc
>::value
)
385 : _M_dataplus(_M_local_data())
386 { _M_set_length(0); }
389 * @brief Construct an empty string using allocator @a a.
392 basic_string(const _Alloc
& __a
)
393 : _M_dataplus(_M_local_data(), __a
)
394 { _M_set_length(0); }
397 * @brief Construct string with copy of value of @a __str.
398 * @param __str Source string.
400 basic_string(const basic_string
& __str
)
401 : _M_dataplus(_M_local_data(), __str
._M_get_allocator()) // TODO A traits
402 { _M_construct(__str
._M_data(), __str
._M_data() + __str
.length()); }
405 * @brief Construct string as copy of a substring.
406 * @param __str Source string.
407 * @param __pos Index of first character to copy from.
408 * @param __n Number of characters to copy (default remainder).
410 // _GLIBCXX_RESOLVE_LIB_DEFECTS
411 // 2402. [this constructor] shouldn't use Allocator()
412 basic_string(const basic_string
& __str
, size_type __pos
,
413 size_type __n
= npos
)
414 : _M_dataplus(_M_local_data())
416 const _CharT
* __start
= __str
._M_data()
417 + __str
._M_check(__pos
, "basic_string::basic_string");
418 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, __n
));
422 * @brief Construct string as copy of a substring.
423 * @param __str Source string.
424 * @param __pos Index of first character to copy from.
425 * @param __n Number of characters to copy (default remainder).
426 * @param __a Allocator to use.
428 basic_string(const basic_string
& __str
, size_type __pos
,
429 size_type __n
, const _Alloc
& __a
)
430 : _M_dataplus(_M_local_data(), __a
)
432 const _CharT
* __start
433 = __str
._M_data() + __str
._M_check(__pos
, "string::string");
434 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, __n
));
438 * @brief Construct string initialized by a character %array.
439 * @param __s Source character %array.
440 * @param __n Number of characters to copy.
441 * @param __a Allocator to use (default is default allocator).
443 * NB: @a __s must have at least @a __n characters, '\\0'
444 * has no special meaning.
446 basic_string(const _CharT
* __s
, size_type __n
,
447 const _Alloc
& __a
= _Alloc())
448 : _M_dataplus(_M_local_data(), __a
)
449 { _M_construct(__s
, __s
+ __n
); }
452 * @brief Construct string as copy of a C string.
453 * @param __s Source C string.
454 * @param __a Allocator to use (default is default allocator).
456 basic_string(const _CharT
* __s
, const _Alloc
& __a
= _Alloc())
457 : _M_dataplus(_M_local_data(), __a
)
458 { _M_construct(__s
, __s
? __s
+ traits_type::length(__s
) : __s
+npos
); }
461 * @brief Construct string as multiple characters.
462 * @param __n Number of characters.
463 * @param __c Character to use.
464 * @param __a Allocator to use (default is default allocator).
466 basic_string(size_type __n
, _CharT __c
, const _Alloc
& __a
= _Alloc())
467 : _M_dataplus(_M_local_data(), __a
)
468 { _M_construct(__n
, __c
); }
470 #if __cplusplus >= 201103L
472 * @brief Move construct string.
473 * @param __str Source string.
475 * The newly-created string contains the exact contents of @a __str.
476 * @a __str is a valid, but unspecified string.
478 basic_string(basic_string
&& __str
) noexcept
479 : _M_dataplus(_M_local_data(), std::move(__str
._M_get_allocator()))
481 if (__str
._M_is_local())
483 traits_type::copy(_M_local_buf
, __str
._M_local_buf
,
484 _S_local_capacity
+ 1);
488 _M_data(__str
._M_data());
489 _M_capacity(__str
._M_allocated_capacity
);
492 // Must use _M_length() here not _M_set_length() because
493 // basic_stringbuf relies on writing into unallocated capacity so
494 // we mess up the contents if we put a '\0' in the string.
495 _M_length(__str
.length());
496 __str
._M_data(__str
._M_local_data());
497 __str
._M_set_length(0);
501 * @brief Construct string from an initializer %list.
502 * @param __l std::initializer_list of characters.
503 * @param __a Allocator to use (default is default allocator).
505 basic_string(initializer_list
<_CharT
> __l
, const _Alloc
& __a
= _Alloc())
506 : _M_dataplus(_M_local_data(), __a
)
507 { _M_construct(__l
.begin(), __l
.end()); }
509 basic_string(const basic_string
& __str
, const _Alloc
& __a
)
510 : _M_dataplus(_M_local_data(), __a
)
511 { _M_construct(__str
.begin(), __str
.end()); }
513 basic_string(basic_string
&& __str
, const _Alloc
& __a
)
514 : _M_dataplus(_M_local_data(), __a
)
516 if (__str
.get_allocator() == __a
)
517 *this = std::move(__str
);
519 _M_construct(__str
.begin(), __str
.end());
525 * @brief Construct string as copy of a range.
526 * @param __beg Start of range.
527 * @param __end End of range.
528 * @param __a Allocator to use (default is default allocator).
530 #if __cplusplus >= 201103L
531 template<typename _InputIterator
,
532 typename
= std::_RequireInputIter
<_InputIterator
>>
534 template<typename _InputIterator
>
536 basic_string(_InputIterator __beg
, _InputIterator __end
,
537 const _Alloc
& __a
= _Alloc())
538 : _M_dataplus(_M_local_data(), __a
)
539 { _M_construct(__beg
, __end
); }
542 * @brief Destroy the string instance.
548 * @brief Assign the value of @a str to this string.
549 * @param __str Source string.
552 operator=(const basic_string
& __str
)
553 { return this->assign(__str
); }
556 * @brief Copy contents of @a s into this string.
557 * @param __s Source null-terminated string.
560 operator=(const _CharT
* __s
)
561 { return this->assign(__s
); }
564 * @brief Set value to string of length 1.
565 * @param __c Source character.
567 * Assigning to a character makes this string length 1 and
568 * (*this)[0] == @a c.
571 operator=(_CharT __c
)
573 this->assign(1, __c
);
577 #if __cplusplus >= 201103L
579 * @brief Move assign the value of @a str to this string.
580 * @param __str Source string.
582 * The contents of @a str are moved into this string (without copying).
583 * @a str is a valid, but unspecified string.
585 // PR 58265, this should be noexcept.
586 // _GLIBCXX_RESOLVE_LIB_DEFECTS
587 // 2063. Contradictory requirements for string move assignment
589 operator=(basic_string
&& __str
)
596 * @brief Set value to string constructed from initializer %list.
597 * @param __l std::initializer_list.
600 operator=(initializer_list
<_CharT
> __l
)
602 this->assign(__l
.begin(), __l
.size());
609 * Returns a read/write iterator that points to the first character in
613 begin() _GLIBCXX_NOEXCEPT
614 { return iterator(_M_data()); }
617 * Returns a read-only (constant) iterator that points to the first
618 * character in the %string.
621 begin() const _GLIBCXX_NOEXCEPT
622 { return const_iterator(_M_data()); }
625 * Returns a read/write iterator that points one past the last
626 * character in the %string.
629 end() _GLIBCXX_NOEXCEPT
630 { return iterator(_M_data() + this->size()); }
633 * Returns a read-only (constant) iterator that points one past the
634 * last character in the %string.
637 end() const _GLIBCXX_NOEXCEPT
638 { return const_iterator(_M_data() + this->size()); }
641 * Returns a read/write reverse iterator that points to the last
642 * character in the %string. Iteration is done in reverse element
646 rbegin() _GLIBCXX_NOEXCEPT
647 { return reverse_iterator(this->end()); }
650 * Returns a read-only (constant) reverse iterator that points
651 * to the last character in the %string. Iteration is done in
652 * reverse element order.
654 const_reverse_iterator
655 rbegin() const _GLIBCXX_NOEXCEPT
656 { return const_reverse_iterator(this->end()); }
659 * Returns a read/write reverse iterator that points to one before the
660 * first character in the %string. Iteration is done in reverse
664 rend() _GLIBCXX_NOEXCEPT
665 { return reverse_iterator(this->begin()); }
668 * Returns a read-only (constant) reverse iterator that points
669 * to one before the first character in the %string. Iteration
670 * is done in reverse element order.
672 const_reverse_iterator
673 rend() const _GLIBCXX_NOEXCEPT
674 { return const_reverse_iterator(this->begin()); }
676 #if __cplusplus >= 201103L
678 * Returns a read-only (constant) iterator that points to the first
679 * character in the %string.
682 cbegin() const noexcept
683 { return const_iterator(this->_M_data()); }
686 * Returns a read-only (constant) iterator that points one past the
687 * last character in the %string.
690 cend() const noexcept
691 { return const_iterator(this->_M_data() + this->size()); }
694 * Returns a read-only (constant) reverse iterator that points
695 * to the last character in the %string. Iteration is done in
696 * reverse element order.
698 const_reverse_iterator
699 crbegin() const noexcept
700 { return const_reverse_iterator(this->end()); }
703 * Returns a read-only (constant) reverse iterator that points
704 * to one before the first character in the %string. Iteration
705 * is done in reverse element order.
707 const_reverse_iterator
708 crend() const noexcept
709 { return const_reverse_iterator(this->begin()); }
714 /// Returns the number of characters in the string, not including any
715 /// null-termination.
717 size() const _GLIBCXX_NOEXCEPT
718 { return _M_string_length
; }
720 /// Returns the number of characters in the string, not including any
721 /// null-termination.
723 length() const _GLIBCXX_NOEXCEPT
724 { return _M_string_length
; }
726 /// Returns the size() of the largest possible %string.
728 max_size() const _GLIBCXX_NOEXCEPT
729 { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
732 * @brief Resizes the %string to the specified number of characters.
733 * @param __n Number of characters the %string should contain.
734 * @param __c Character to fill any new elements.
736 * This function will %resize the %string to the specified
737 * number of characters. If the number is smaller than the
738 * %string's current size the %string is truncated, otherwise
739 * the %string is extended and new elements are %set to @a __c.
742 resize(size_type __n
, _CharT __c
);
745 * @brief Resizes the %string to the specified number of characters.
746 * @param __n Number of characters the %string should contain.
748 * This function will resize the %string to the specified length. If
749 * the new size is smaller than the %string's current size the %string
750 * is truncated, otherwise the %string is extended and new characters
751 * are default-constructed. For basic types such as char, this means
755 resize(size_type __n
)
756 { this->resize(__n
, _CharT()); }
758 #if __cplusplus >= 201103L
759 /// A non-binding request to reduce capacity() to size().
761 shrink_to_fit() noexcept
763 if (capacity() > size())
774 * Returns the total number of characters that the %string can hold
775 * before needing to allocate more memory.
778 capacity() const _GLIBCXX_NOEXCEPT
780 return _M_is_local() ? size_type(_S_local_capacity
)
781 : _M_allocated_capacity
;
785 * @brief Attempt to preallocate enough memory for specified number of
787 * @param __res_arg Number of characters required.
788 * @throw std::length_error If @a __res_arg exceeds @c max_size().
790 * This function attempts to reserve enough memory for the
791 * %string to hold the specified number of characters. If the
792 * number requested is more than max_size(), length_error is
795 * The advantage of this function is that if optimal code is a
796 * necessity and the user can determine the string length that will be
797 * required, the user can reserve the memory in %advance, and thus
798 * prevent a possible reallocation of memory and copying of %string
802 reserve(size_type __res_arg
= 0);
805 * Erases the string, making it empty.
808 clear() _GLIBCXX_NOEXCEPT
809 { _M_set_length(0); }
812 * Returns true if the %string is empty. Equivalent to
813 * <code>*this == ""</code>.
816 empty() const _GLIBCXX_NOEXCEPT
817 { return this->size() == 0; }
821 * @brief Subscript access to the data contained in the %string.
822 * @param __pos The index of the character to access.
823 * @return Read-only (constant) reference to the character.
825 * This operator allows for easy, array-style, data access.
826 * Note that data access with this operator is unchecked and
827 * out_of_range lookups are not defined. (For checked lookups
831 operator[] (size_type __pos
) const _GLIBCXX_NOEXCEPT
833 _GLIBCXX_DEBUG_ASSERT(__pos
<= size());
834 return _M_data()[__pos
];
838 * @brief Subscript access to the data contained in the %string.
839 * @param __pos The index of the character to access.
840 * @return Read/write reference to the character.
842 * This operator allows for easy, array-style, data access.
843 * Note that data access with this operator is unchecked and
844 * out_of_range lookups are not defined. (For checked lookups
848 operator[](size_type __pos
)
850 // Allow pos == size() both in C++98 mode, as v3 extension,
851 // and in C++11 mode.
852 _GLIBCXX_DEBUG_ASSERT(__pos
<= size());
853 // In pedantic mode be strict in C++98 mode.
854 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus
>= 201103L || __pos
< size());
855 return _M_data()[__pos
];
859 * @brief Provides access to the data contained in the %string.
860 * @param __n The index of the character to access.
861 * @return Read-only (const) reference to the character.
862 * @throw std::out_of_range If @a n is an invalid index.
864 * This function provides for safer data access. The parameter is
865 * first checked that it is in the range of the string. The function
866 * throws out_of_range if the check fails.
869 at(size_type __n
) const
871 if (__n
>= this->size())
872 __throw_out_of_range_fmt(__N("basic_string::at: __n "
873 "(which is %zu) >= this->size() "
876 return _M_data()[__n
];
880 * @brief Provides access to the data contained in the %string.
881 * @param __n The index of the character to access.
882 * @return Read/write reference to the character.
883 * @throw std::out_of_range If @a n is an invalid index.
885 * This function provides for safer data access. The parameter is
886 * first checked that it is in the range of the string. The function
887 * throws out_of_range if the check fails.
893 __throw_out_of_range_fmt(__N("basic_string::at: __n "
894 "(which is %zu) >= this->size() "
897 return _M_data()[__n
];
900 #if __cplusplus >= 201103L
902 * Returns a read/write reference to the data at the first
903 * element of the %string.
908 _GLIBCXX_DEBUG_ASSERT(!empty());
909 return operator[](0);
913 * Returns a read-only (constant) reference to the data at the first
914 * element of the %string.
917 front() const noexcept
919 _GLIBCXX_DEBUG_ASSERT(!empty());
920 return operator[](0);
924 * Returns a read/write reference to the data at the last
925 * element of the %string.
930 _GLIBCXX_DEBUG_ASSERT(!empty());
931 return operator[](this->size() - 1);
935 * Returns a read-only (constant) reference to the data at the
936 * last element of the %string.
939 back() const noexcept
941 _GLIBCXX_DEBUG_ASSERT(!empty());
942 return operator[](this->size() - 1);
948 * @brief Append a string to this string.
949 * @param __str The string to append.
950 * @return Reference to this string.
953 operator+=(const basic_string
& __str
)
954 { return this->append(__str
); }
957 * @brief Append a C string.
958 * @param __s The C string to append.
959 * @return Reference to this string.
962 operator+=(const _CharT
* __s
)
963 { return this->append(__s
); }
966 * @brief Append a character.
967 * @param __c The character to append.
968 * @return Reference to this string.
971 operator+=(_CharT __c
)
973 this->push_back(__c
);
977 #if __cplusplus >= 201103L
979 * @brief Append an initializer_list of characters.
980 * @param __l The initializer_list of characters to be appended.
981 * @return Reference to this string.
984 operator+=(initializer_list
<_CharT
> __l
)
985 { return this->append(__l
.begin(), __l
.size()); }
989 * @brief Append a string to this string.
990 * @param __str The string to append.
991 * @return Reference to this string.
994 append(const basic_string
& __str
)
995 { return _M_append(__str
._M_data(), __str
.size()); }
998 * @brief Append a substring.
999 * @param __str The string to append.
1000 * @param __pos Index of the first character of str to append.
1001 * @param __n The number of characters to append.
1002 * @return Reference to this string.
1003 * @throw std::out_of_range if @a __pos is not a valid index.
1005 * This function appends @a __n characters from @a __str
1006 * starting at @a __pos to this string. If @a __n is is larger
1007 * than the number of available characters in @a __str, the
1008 * remainder of @a __str is appended.
1011 append(const basic_string
& __str
, size_type __pos
, size_type __n
)
1012 { return _M_append(__str
._M_data()
1013 + __str
._M_check(__pos
, "basic_string::append"),
1014 __str
._M_limit(__pos
, __n
)); }
1017 * @brief Append a C substring.
1018 * @param __s The C string to append.
1019 * @param __n The number of characters to append.
1020 * @return Reference to this string.
1023 append(const _CharT
* __s
, size_type __n
)
1025 __glibcxx_requires_string_len(__s
, __n
);
1026 _M_check_length(size_type(0), __n
, "basic_string::append");
1027 return _M_append(__s
, __n
);
1031 * @brief Append a C string.
1032 * @param __s The C string to append.
1033 * @return Reference to this string.
1036 append(const _CharT
* __s
)
1038 __glibcxx_requires_string(__s
);
1039 const size_type __n
= traits_type::length(__s
);
1040 _M_check_length(size_type(0), __n
, "basic_string::append");
1041 return _M_append(__s
, __n
);
1045 * @brief Append multiple characters.
1046 * @param __n The number of characters to append.
1047 * @param __c The character to use.
1048 * @return Reference to this string.
1050 * Appends __n copies of __c to this string.
1053 append(size_type __n
, _CharT __c
)
1054 { return _M_replace_aux(this->size(), size_type(0), __n
, __c
); }
1056 #if __cplusplus >= 201103L
1058 * @brief Append an initializer_list of characters.
1059 * @param __l The initializer_list of characters to append.
1060 * @return Reference to this string.
1063 append(initializer_list
<_CharT
> __l
)
1064 { return this->append(__l
.begin(), __l
.size()); }
1068 * @brief Append a range of characters.
1069 * @param __first Iterator referencing the first character to append.
1070 * @param __last Iterator marking the end of the range.
1071 * @return Reference to this string.
1073 * Appends characters in the range [__first,__last) to this string.
1075 #if __cplusplus >= 201103L
1076 template<class _InputIterator
,
1077 typename
= std::_RequireInputIter
<_InputIterator
>>
1079 template<class _InputIterator
>
1082 append(_InputIterator __first
, _InputIterator __last
)
1083 { return this->replace(end(), end(), __first
, __last
); }
1086 * @brief Append a single character.
1087 * @param __c Character to append.
1090 push_back(_CharT __c
)
1092 const size_type __size
= this->size();
1093 if (__size
+ 1 > this->capacity())
1094 this->_M_mutate(__size
, size_type(0), 0, size_type(1));
1095 traits_type::assign(this->_M_data()[__size
], __c
);
1096 this->_M_set_length(__size
+ 1);
1100 * @brief Set value to contents of another string.
1101 * @param __str Source string to use.
1102 * @return Reference to this string.
1105 assign(const basic_string
& __str
)
1107 this->_M_assign(__str
);
1111 #if __cplusplus >= 201103L
1113 * @brief Set value to contents of another string.
1114 * @param __str Source string to use.
1115 * @return Reference to this string.
1117 * This function sets this string to the exact contents of @a __str.
1118 * @a __str is a valid, but unspecified string.
1121 assign(basic_string
&& __str
)
1123 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1124 // 2063. Contradictory requirements for string move assignment
1125 return *this = std::move(__str
);
1130 * @brief Set value to a substring of a string.
1131 * @param __str The string to use.
1132 * @param __pos Index of the first character of str.
1133 * @param __n Number of characters to use.
1134 * @return Reference to this string.
1135 * @throw std::out_of_range if @a pos is not a valid index.
1137 * This function sets this string to the substring of @a __str
1138 * consisting of @a __n characters at @a __pos. If @a __n is
1139 * is larger than the number of available characters in @a
1140 * __str, the remainder of @a __str is used.
1143 assign(const basic_string
& __str
, size_type __pos
, size_type __n
)
1144 { return _M_replace(size_type(0), this->size(), __str
._M_data()
1145 + __str
._M_check(__pos
, "basic_string::assign"),
1146 __str
._M_limit(__pos
, __n
)); }
1149 * @brief Set value to a C substring.
1150 * @param __s The C string to use.
1151 * @param __n Number of characters to use.
1152 * @return Reference to this string.
1154 * This function sets the value of this string to the first @a __n
1155 * characters of @a __s. If @a __n is is larger than the number of
1156 * available characters in @a __s, the remainder of @a __s is used.
1159 assign(const _CharT
* __s
, size_type __n
)
1161 __glibcxx_requires_string_len(__s
, __n
);
1162 return _M_replace(size_type(0), this->size(), __s
, __n
);
1166 * @brief Set value to contents of a C string.
1167 * @param __s The C string to use.
1168 * @return Reference to this string.
1170 * This function sets the value of this string to the value of @a __s.
1171 * The data is copied, so there is no dependence on @a __s once the
1175 assign(const _CharT
* __s
)
1177 __glibcxx_requires_string(__s
);
1178 return _M_replace(size_type(0), this->size(), __s
,
1179 traits_type::length(__s
));
1183 * @brief Set value to multiple characters.
1184 * @param __n Length of the resulting string.
1185 * @param __c The character to use.
1186 * @return Reference to this string.
1188 * This function sets the value of this string to @a __n copies of
1192 assign(size_type __n
, _CharT __c
)
1193 { return _M_replace_aux(size_type(0), this->size(), __n
, __c
); }
1196 * @brief Set value to a range of characters.
1197 * @param __first Iterator referencing the first character to append.
1198 * @param __last Iterator marking the end of the range.
1199 * @return Reference to this string.
1201 * Sets value of string to characters in the range [__first,__last).
1203 #if __cplusplus >= 201103L
1204 template<class _InputIterator
,
1205 typename
= std::_RequireInputIter
<_InputIterator
>>
1207 template<class _InputIterator
>
1210 assign(_InputIterator __first
, _InputIterator __last
)
1211 { return this->replace(begin(), end(), __first
, __last
); }
1213 #if __cplusplus >= 201103L
1215 * @brief Set value to an initializer_list of characters.
1216 * @param __l The initializer_list of characters to assign.
1217 * @return Reference to this string.
1220 assign(initializer_list
<_CharT
> __l
)
1221 { return this->assign(__l
.begin(), __l
.size()); }
1224 #if __cplusplus >= 201103L
1226 * @brief Insert multiple characters.
1227 * @param __p Const_iterator referencing location in string to
1229 * @param __n Number of characters to insert
1230 * @param __c The character to insert.
1231 * @return Iterator referencing the first inserted char.
1232 * @throw std::length_error If new length exceeds @c max_size().
1234 * Inserts @a __n copies of character @a __c starting at the
1235 * position referenced by iterator @a __p. If adding
1236 * characters causes the length to exceed max_size(),
1237 * length_error is thrown. The value of the string doesn't
1238 * change if an error is thrown.
1241 insert(const_iterator __p
, size_type __n
, _CharT __c
)
1243 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1244 const size_type __pos
= __p
- begin();
1245 this->replace(__p
, __p
, __n
, __c
);
1246 return iterator(this->_M_data() + __pos
);
1250 * @brief Insert multiple characters.
1251 * @param __p Iterator referencing location in string to insert at.
1252 * @param __n Number of characters to insert
1253 * @param __c The character to insert.
1254 * @throw std::length_error If new length exceeds @c max_size().
1256 * Inserts @a __n copies of character @a __c starting at the
1257 * position referenced by iterator @a __p. If adding
1258 * characters causes the length to exceed max_size(),
1259 * length_error is thrown. The value of the string doesn't
1260 * change if an error is thrown.
1263 insert(iterator __p
, size_type __n
, _CharT __c
)
1264 { this->replace(__p
, __p
, __n
, __c
); }
1267 #if __cplusplus >= 201103L
1269 * @brief Insert a range of characters.
1270 * @param __p Const_iterator referencing location in string to
1272 * @param __beg Start of range.
1273 * @param __end End of range.
1274 * @return Iterator referencing the first inserted char.
1275 * @throw std::length_error If new length exceeds @c max_size().
1277 * Inserts characters in range [beg,end). If adding characters
1278 * causes the length to exceed max_size(), length_error is
1279 * thrown. The value of the string doesn't change if an error
1282 template<class _InputIterator
,
1283 typename
= std::_RequireInputIter
<_InputIterator
>>
1285 insert(const_iterator __p
, _InputIterator __beg
, _InputIterator __end
)
1287 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1288 const size_type __pos
= __p
- begin();
1289 this->replace(__p
, __p
, __beg
, __end
);
1290 return iterator(this->_M_data() + __pos
);
1294 * @brief Insert a range of characters.
1295 * @param __p Iterator referencing location in string to insert at.
1296 * @param __beg Start of range.
1297 * @param __end End of range.
1298 * @throw std::length_error If new length exceeds @c max_size().
1300 * Inserts characters in range [__beg,__end). If adding
1301 * characters causes the length to exceed max_size(),
1302 * length_error is thrown. The value of the string doesn't
1303 * change if an error is thrown.
1305 template<class _InputIterator
>
1307 insert(iterator __p
, _InputIterator __beg
, _InputIterator __end
)
1308 { this->replace(__p
, __p
, __beg
, __end
); }
1311 #if __cplusplus >= 201103L
1313 * @brief Insert an initializer_list of characters.
1314 * @param __p Iterator referencing location in string to insert at.
1315 * @param __l The initializer_list of characters to insert.
1316 * @throw std::length_error If new length exceeds @c max_size().
1319 insert(iterator __p
, initializer_list
<_CharT
> __l
)
1321 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1322 this->insert(__p
- begin(), __l
.begin(), __l
.size());
1327 * @brief Insert value of a string.
1328 * @param __pos1 Iterator referencing location in string to insert at.
1329 * @param __str The string to insert.
1330 * @return Reference to this string.
1331 * @throw std::length_error If new length exceeds @c max_size().
1333 * Inserts value of @a __str starting at @a __pos1. If adding
1334 * characters causes the length to exceed max_size(),
1335 * length_error is thrown. The value of the string doesn't
1336 * change if an error is thrown.
1339 insert(size_type __pos1
, const basic_string
& __str
)
1340 { return this->replace(__pos1
, size_type(0),
1341 __str
._M_data(), __str
.size()); }
1344 * @brief Insert a substring.
1345 * @param __pos1 Iterator referencing location in string to insert at.
1346 * @param __str The string to insert.
1347 * @param __pos2 Start of characters in str to insert.
1348 * @param __n Number of characters to insert.
1349 * @return Reference to this string.
1350 * @throw std::length_error If new length exceeds @c max_size().
1351 * @throw std::out_of_range If @a pos1 > size() or
1352 * @a __pos2 > @a str.size().
1354 * Starting at @a pos1, insert @a __n character of @a __str
1355 * beginning with @a __pos2. If adding characters causes the
1356 * length to exceed max_size(), length_error is thrown. If @a
1357 * __pos1 is beyond the end of this string or @a __pos2 is
1358 * beyond the end of @a __str, out_of_range is thrown. The
1359 * value of the string doesn't change if an error is thrown.
1362 insert(size_type __pos1
, const basic_string
& __str
,
1363 size_type __pos2
, size_type __n
)
1364 { return this->replace(__pos1
, size_type(0), __str
._M_data()
1365 + __str
._M_check(__pos2
, "basic_string::insert"),
1366 __str
._M_limit(__pos2
, __n
)); }
1369 * @brief Insert a C substring.
1370 * @param __pos Iterator referencing location in string to insert at.
1371 * @param __s The C string to insert.
1372 * @param __n The number of characters to insert.
1373 * @return Reference to this string.
1374 * @throw std::length_error If new length exceeds @c max_size().
1375 * @throw std::out_of_range If @a __pos is beyond the end of this
1378 * Inserts the first @a __n characters of @a __s starting at @a
1379 * __pos. If adding characters causes the length to exceed
1380 * max_size(), length_error is thrown. If @a __pos is beyond
1381 * end(), out_of_range is thrown. The value of the string
1382 * doesn't change if an error is thrown.
1385 insert(size_type __pos
, const _CharT
* __s
, size_type __n
)
1386 { return this->replace(__pos
, size_type(0), __s
, __n
); }
1389 * @brief Insert a C string.
1390 * @param __pos Iterator referencing location in string to insert at.
1391 * @param __s The C string to insert.
1392 * @return Reference to this string.
1393 * @throw std::length_error If new length exceeds @c max_size().
1394 * @throw std::out_of_range If @a pos is beyond the end of this
1397 * Inserts the first @a n characters of @a __s starting at @a __pos. If
1398 * adding characters causes the length to exceed max_size(),
1399 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
1400 * thrown. The value of the string doesn't change if an error is
1404 insert(size_type __pos
, const _CharT
* __s
)
1406 __glibcxx_requires_string(__s
);
1407 return this->replace(__pos
, size_type(0), __s
,
1408 traits_type::length(__s
));
1412 * @brief Insert multiple characters.
1413 * @param __pos Index in string to insert at.
1414 * @param __n Number of characters to insert
1415 * @param __c The character to insert.
1416 * @return Reference to this string.
1417 * @throw std::length_error If new length exceeds @c max_size().
1418 * @throw std::out_of_range If @a __pos is beyond the end of this
1421 * Inserts @a __n copies of character @a __c starting at index
1422 * @a __pos. If adding characters causes the length to exceed
1423 * max_size(), length_error is thrown. If @a __pos > length(),
1424 * out_of_range is thrown. The value of the string doesn't
1425 * change if an error is thrown.
1428 insert(size_type __pos
, size_type __n
, _CharT __c
)
1429 { return _M_replace_aux(_M_check(__pos
, "basic_string::insert"),
1430 size_type(0), __n
, __c
); }
1433 * @brief Insert one character.
1434 * @param __p Iterator referencing position in string to insert at.
1435 * @param __c The character to insert.
1436 * @return Iterator referencing newly inserted char.
1437 * @throw std::length_error If new length exceeds @c max_size().
1439 * Inserts character @a __c at position referenced by @a __p.
1440 * If adding character causes the length to exceed max_size(),
1441 * length_error is thrown. If @a __p is beyond end of string,
1442 * out_of_range is thrown. The value of the string doesn't
1443 * change if an error is thrown.
1446 insert(__const_iterator __p
, _CharT __c
)
1448 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1449 const size_type __pos
= __p
- begin();
1450 _M_replace_aux(__pos
, size_type(0), size_type(1), __c
);
1451 return iterator(_M_data() + __pos
);
1455 * @brief Remove characters.
1456 * @param __pos Index of first character to remove (default 0).
1457 * @param __n Number of characters to remove (default remainder).
1458 * @return Reference to this string.
1459 * @throw std::out_of_range If @a pos is beyond the end of this
1462 * Removes @a __n characters from this string starting at @a
1463 * __pos. The length of the string is reduced by @a __n. If
1464 * there are < @a __n characters to remove, the remainder of
1465 * the string is truncated. If @a __p is beyond end of string,
1466 * out_of_range is thrown. The value of the string doesn't
1467 * change if an error is thrown.
1470 erase(size_type __pos
= 0, size_type __n
= npos
)
1472 this->_M_erase(_M_check(__pos
, "basic_string::erase"),
1473 _M_limit(__pos
, __n
));
1478 * @brief Remove one character.
1479 * @param __position Iterator referencing the character to remove.
1480 * @return iterator referencing same location after removal.
1482 * Removes the character at @a __position from this string. The value
1483 * of the string doesn't change if an error is thrown.
1486 erase(__const_iterator __position
)
1488 _GLIBCXX_DEBUG_PEDASSERT(__position
>= begin()
1489 && __position
< end());
1490 const size_type __pos
= __position
- begin();
1491 this->_M_erase(__pos
, size_type(1));
1492 return iterator(_M_data() + __pos
);
1496 * @brief Remove a range of characters.
1497 * @param __first Iterator referencing the first character to remove.
1498 * @param __last Iterator referencing the end of the range.
1499 * @return Iterator referencing location of first after removal.
1501 * Removes the characters in the range [first,last) from this string.
1502 * The value of the string doesn't change if an error is thrown.
1505 erase(__const_iterator __first
, __const_iterator __last
)
1507 _GLIBCXX_DEBUG_PEDASSERT(__first
>= begin() && __first
<= __last
1508 && __last
<= end());
1509 const size_type __pos
= __first
- begin();
1510 this->_M_erase(__pos
, __last
- __first
);
1511 return iterator(this->_M_data() + __pos
);
1514 #if __cplusplus >= 201103L
1516 * @brief Remove the last character.
1518 * The string must be non-empty.
1523 _GLIBCXX_DEBUG_ASSERT(!empty());
1524 _M_erase(size() - 1, 1);
1529 * @brief Replace characters with value from another string.
1530 * @param __pos Index of first character to replace.
1531 * @param __n Number of characters to be replaced.
1532 * @param __str String to insert.
1533 * @return Reference to this string.
1534 * @throw std::out_of_range If @a pos is beyond the end of this
1536 * @throw std::length_error If new length exceeds @c max_size().
1538 * Removes the characters in the range [__pos,__pos+__n) from
1539 * this string. In place, the value of @a __str is inserted.
1540 * If @a __pos is beyond end of string, out_of_range is thrown.
1541 * If the length of the result exceeds max_size(), length_error
1542 * is thrown. The value of the string doesn't change if an
1546 replace(size_type __pos
, size_type __n
, const basic_string
& __str
)
1547 { return this->replace(__pos
, __n
, __str
._M_data(), __str
.size()); }
1550 * @brief Replace characters with value from another string.
1551 * @param __pos1 Index of first character to replace.
1552 * @param __n1 Number of characters to be replaced.
1553 * @param __str String to insert.
1554 * @param __pos2 Index of first character of str to use.
1555 * @param __n2 Number of characters from str to use.
1556 * @return Reference to this string.
1557 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
1559 * @throw std::length_error If new length exceeds @c max_size().
1561 * Removes the characters in the range [__pos1,__pos1 + n) from this
1562 * string. In place, the value of @a __str is inserted. If @a __pos is
1563 * beyond end of string, out_of_range is thrown. If the length of the
1564 * result exceeds max_size(), length_error is thrown. The value of the
1565 * string doesn't change if an error is thrown.
1568 replace(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
1569 size_type __pos2
, size_type __n2
)
1570 { return this->replace(__pos1
, __n1
, __str
._M_data()
1571 + __str
._M_check(__pos2
, "basic_string::replace"),
1572 __str
._M_limit(__pos2
, __n2
)); }
1575 * @brief Replace characters with value of a C substring.
1576 * @param __pos Index of first character to replace.
1577 * @param __n1 Number of characters to be replaced.
1578 * @param __s C string to insert.
1579 * @param __n2 Number of characters from @a s to use.
1580 * @return Reference to this string.
1581 * @throw std::out_of_range If @a pos1 > size().
1582 * @throw std::length_error If new length exceeds @c max_size().
1584 * Removes the characters in the range [__pos,__pos + __n1)
1585 * from this string. In place, the first @a __n2 characters of
1586 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
1587 * @a __pos is beyond end of string, out_of_range is thrown. If
1588 * the length of result exceeds max_size(), length_error is
1589 * thrown. The value of the string doesn't change if an error
1593 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
1596 __glibcxx_requires_string_len(__s
, __n2
);
1597 return _M_replace(_M_check(__pos
, "basic_string::replace"),
1598 _M_limit(__pos
, __n1
), __s
, __n2
);
1602 * @brief Replace characters with value of a C string.
1603 * @param __pos Index of first character to replace.
1604 * @param __n1 Number of characters to be replaced.
1605 * @param __s C string to insert.
1606 * @return Reference to this string.
1607 * @throw std::out_of_range If @a pos > size().
1608 * @throw std::length_error If new length exceeds @c max_size().
1610 * Removes the characters in the range [__pos,__pos + __n1)
1611 * from this string. In place, the characters of @a __s are
1612 * inserted. If @a __pos is beyond end of string, out_of_range
1613 * is thrown. If the length of result exceeds max_size(),
1614 * length_error is thrown. The value of the string doesn't
1615 * change if an error is thrown.
1618 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
)
1620 __glibcxx_requires_string(__s
);
1621 return this->replace(__pos
, __n1
, __s
, traits_type::length(__s
));
1625 * @brief Replace characters with multiple characters.
1626 * @param __pos Index of first character to replace.
1627 * @param __n1 Number of characters to be replaced.
1628 * @param __n2 Number of characters to insert.
1629 * @param __c Character to insert.
1630 * @return Reference to this string.
1631 * @throw std::out_of_range If @a __pos > size().
1632 * @throw std::length_error If new length exceeds @c max_size().
1634 * Removes the characters in the range [pos,pos + n1) from this
1635 * string. In place, @a __n2 copies of @a __c are inserted.
1636 * If @a __pos is beyond end of string, out_of_range is thrown.
1637 * If the length of result exceeds max_size(), length_error is
1638 * thrown. The value of the string doesn't change if an error
1642 replace(size_type __pos
, size_type __n1
, size_type __n2
, _CharT __c
)
1643 { return _M_replace_aux(_M_check(__pos
, "basic_string::replace"),
1644 _M_limit(__pos
, __n1
), __n2
, __c
); }
1647 * @brief Replace range of characters with string.
1648 * @param __i1 Iterator referencing start of range to replace.
1649 * @param __i2 Iterator referencing end of range to replace.
1650 * @param __str String value to insert.
1651 * @return Reference to this string.
1652 * @throw std::length_error If new length exceeds @c max_size().
1654 * Removes the characters in the range [__i1,__i2). In place,
1655 * the value of @a __str is inserted. If the length of result
1656 * exceeds max_size(), length_error is thrown. The value of
1657 * the string doesn't change if an error is thrown.
1660 replace(__const_iterator __i1
, __const_iterator __i2
,
1661 const basic_string
& __str
)
1662 { return this->replace(__i1
, __i2
, __str
._M_data(), __str
.size()); }
1665 * @brief Replace range of characters with C substring.
1666 * @param __i1 Iterator referencing start of range to replace.
1667 * @param __i2 Iterator referencing end of range to replace.
1668 * @param __s C string value to insert.
1669 * @param __n Number of characters from s to insert.
1670 * @return Reference to this string.
1671 * @throw std::length_error If new length exceeds @c max_size().
1673 * Removes the characters in the range [__i1,__i2). In place,
1674 * the first @a __n characters of @a __s are inserted. If the
1675 * length of result exceeds max_size(), length_error is thrown.
1676 * The value of the string doesn't change if an error is
1680 replace(__const_iterator __i1
, __const_iterator __i2
,
1681 const _CharT
* __s
, size_type __n
)
1683 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
1685 return this->replace(__i1
- begin(), __i2
- __i1
, __s
, __n
);
1689 * @brief Replace range of characters with C string.
1690 * @param __i1 Iterator referencing start of range to replace.
1691 * @param __i2 Iterator referencing end of range to replace.
1692 * @param __s C string value to insert.
1693 * @return Reference to this string.
1694 * @throw std::length_error If new length exceeds @c max_size().
1696 * Removes the characters in the range [__i1,__i2). In place,
1697 * the characters of @a __s are inserted. If the length of
1698 * result exceeds max_size(), length_error is thrown. The
1699 * value of the string doesn't change if an error is thrown.
1702 replace(__const_iterator __i1
, __const_iterator __i2
, const _CharT
* __s
)
1704 __glibcxx_requires_string(__s
);
1705 return this->replace(__i1
, __i2
, __s
, traits_type::length(__s
));
1709 * @brief Replace range of characters with multiple characters
1710 * @param __i1 Iterator referencing start of range to replace.
1711 * @param __i2 Iterator referencing end of range to replace.
1712 * @param __n Number of characters to insert.
1713 * @param __c Character to insert.
1714 * @return Reference to this string.
1715 * @throw std::length_error If new length exceeds @c max_size().
1717 * Removes the characters in the range [__i1,__i2). In place,
1718 * @a __n copies of @a __c are inserted. If the length of
1719 * result exceeds max_size(), length_error is thrown. The
1720 * value of the string doesn't change if an error is thrown.
1723 replace(__const_iterator __i1
, __const_iterator __i2
, size_type __n
,
1726 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
1728 return _M_replace_aux(__i1
- begin(), __i2
- __i1
, __n
, __c
);
1732 * @brief Replace range of characters with range.
1733 * @param __i1 Iterator referencing start of range to replace.
1734 * @param __i2 Iterator referencing end of range to replace.
1735 * @param __k1 Iterator referencing start of range to insert.
1736 * @param __k2 Iterator referencing end of range to insert.
1737 * @return Reference to this string.
1738 * @throw std::length_error If new length exceeds @c max_size().
1740 * Removes the characters in the range [__i1,__i2). In place,
1741 * characters in the range [__k1,__k2) are inserted. If the
1742 * length of result exceeds max_size(), length_error is thrown.
1743 * The value of the string doesn't change if an error is
1746 #if __cplusplus >= 201103L
1747 template<class _InputIterator
,
1748 typename
= std::_RequireInputIter
<_InputIterator
>>
1750 replace(const_iterator __i1
, const_iterator __i2
,
1751 _InputIterator __k1
, _InputIterator __k2
)
1753 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
1755 __glibcxx_requires_valid_range(__k1
, __k2
);
1756 return this->_M_replace_dispatch(__i1
, __i2
, __k1
, __k2
,
1757 std::__false_type());
1760 template<class _InputIterator
>
1761 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
1762 typename __enable_if_not_native_iterator
<_InputIterator
>::__type
1766 replace(iterator __i1
, iterator __i2
,
1767 _InputIterator __k1
, _InputIterator __k2
)
1769 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
1771 __glibcxx_requires_valid_range(__k1
, __k2
);
1772 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1773 return _M_replace_dispatch(__i1
, __i2
, __k1
, __k2
, _Integral());
1777 // Specializations for the common case of pointer and iterator:
1778 // useful to avoid the overhead of temporary buffering in _M_replace.
1780 replace(__const_iterator __i1
, __const_iterator __i2
,
1781 _CharT
* __k1
, _CharT
* __k2
)
1783 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
1785 __glibcxx_requires_valid_range(__k1
, __k2
);
1786 return this->replace(__i1
- begin(), __i2
- __i1
,
1791 replace(__const_iterator __i1
, __const_iterator __i2
,
1792 const _CharT
* __k1
, const _CharT
* __k2
)
1794 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
1796 __glibcxx_requires_valid_range(__k1
, __k2
);
1797 return this->replace(__i1
- begin(), __i2
- __i1
,
1802 replace(__const_iterator __i1
, __const_iterator __i2
,
1803 iterator __k1
, iterator __k2
)
1805 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
1807 __glibcxx_requires_valid_range(__k1
, __k2
);
1808 return this->replace(__i1
- begin(), __i2
- __i1
,
1809 __k1
.base(), __k2
- __k1
);
1813 replace(__const_iterator __i1
, __const_iterator __i2
,
1814 const_iterator __k1
, const_iterator __k2
)
1816 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
1818 __glibcxx_requires_valid_range(__k1
, __k2
);
1819 return this->replace(__i1
- begin(), __i2
- __i1
,
1820 __k1
.base(), __k2
- __k1
);
1823 #if __cplusplus >= 201103L
1825 * @brief Replace range of characters with initializer_list.
1826 * @param __i1 Iterator referencing start of range to replace.
1827 * @param __i2 Iterator referencing end of range to replace.
1828 * @param __l The initializer_list of characters to insert.
1829 * @return Reference to this string.
1830 * @throw std::length_error If new length exceeds @c max_size().
1832 * Removes the characters in the range [__i1,__i2). In place,
1833 * characters in the range [__k1,__k2) are inserted. If the
1834 * length of result exceeds max_size(), length_error is thrown.
1835 * The value of the string doesn't change if an error is
1838 basic_string
& replace(const_iterator __i1
, const_iterator __i2
,
1839 initializer_list
<_CharT
> __l
)
1840 { return this->replace(__i1
, __i2
, __l
.begin(), __l
.end()); }
1844 template<class _Integer
>
1846 _M_replace_dispatch(const_iterator __i1
, const_iterator __i2
,
1847 _Integer __n
, _Integer __val
, __true_type
)
1848 { return _M_replace_aux(__i1
- begin(), __i2
- __i1
, __n
, __val
); }
1850 template<class _InputIterator
>
1852 _M_replace_dispatch(const_iterator __i1
, const_iterator __i2
,
1853 _InputIterator __k1
, _InputIterator __k2
,
1857 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
1861 _M_replace(size_type __pos
, size_type __len1
, const _CharT
* __s
,
1862 const size_type __len2
);
1865 _M_append(const _CharT
* __s
, size_type __n
);
1870 * @brief Copy substring into C string.
1871 * @param __s C string to copy value into.
1872 * @param __n Number of characters to copy.
1873 * @param __pos Index of first character to copy.
1874 * @return Number of characters actually copied
1875 * @throw std::out_of_range If __pos > size().
1877 * Copies up to @a __n characters starting at @a __pos into the
1878 * C string @a __s. If @a __pos is %greater than size(),
1879 * out_of_range is thrown.
1882 copy(_CharT
* __s
, size_type __n
, size_type __pos
= 0) const;
1885 * @brief Swap contents with another string.
1886 * @param __s String to swap with.
1888 * Exchanges the contents of this string with that of @a __s in constant
1892 swap(basic_string
& __s
) _GLIBCXX_NOEXCEPT
;
1894 // String operations:
1896 * @brief Return const pointer to null-terminated contents.
1898 * This is a handle to internal data. Do not modify or dire things may
1902 c_str() const _GLIBCXX_NOEXCEPT
1903 { return _M_data(); }
1906 * @brief Return const pointer to contents.
1908 * This is a handle to internal data. Do not modify or dire things may
1912 data() const _GLIBCXX_NOEXCEPT
1913 { return _M_data(); }
1916 * @brief Return copy of allocator used to construct this string.
1919 get_allocator() const _GLIBCXX_NOEXCEPT
1920 { return _M_get_allocator(); }
1923 * @brief Find position of a C substring.
1924 * @param __s C string to locate.
1925 * @param __pos Index of character to search from.
1926 * @param __n Number of characters from @a s to search for.
1927 * @return Index of start of first occurrence.
1929 * Starting from @a __pos, searches forward for the first @a
1930 * __n characters in @a __s within this string. If found,
1931 * returns the index where it begins. If not found, returns
1935 find(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
1938 * @brief Find position of a string.
1939 * @param __str String to locate.
1940 * @param __pos Index of character to search from (default 0).
1941 * @return Index of start of first occurrence.
1943 * Starting from @a __pos, searches forward for value of @a __str within
1944 * this string. If found, returns the index where it begins. If not
1945 * found, returns npos.
1948 find(const basic_string
& __str
, size_type __pos
= 0) const
1950 { return this->find(__str
.data(), __pos
, __str
.size()); }
1953 * @brief Find position of a C string.
1954 * @param __s C string to locate.
1955 * @param __pos Index of character to search from (default 0).
1956 * @return Index of start of first occurrence.
1958 * Starting from @a __pos, searches forward for the value of @a
1959 * __s within this string. If found, returns the index where
1960 * it begins. If not found, returns npos.
1963 find(const _CharT
* __s
, size_type __pos
= 0) const
1965 __glibcxx_requires_string(__s
);
1966 return this->find(__s
, __pos
, traits_type::length(__s
));
1970 * @brief Find position of a character.
1971 * @param __c Character to locate.
1972 * @param __pos Index of character to search from (default 0).
1973 * @return Index of first occurrence.
1975 * Starting from @a __pos, searches forward for @a __c within
1976 * this string. If found, returns the index where it was
1977 * found. If not found, returns npos.
1980 find(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
;
1983 * @brief Find last position of a string.
1984 * @param __str String to locate.
1985 * @param __pos Index of character to search back from (default end).
1986 * @return Index of start of last occurrence.
1988 * Starting from @a __pos, searches backward for value of @a
1989 * __str within this string. If found, returns the index where
1990 * it begins. If not found, returns npos.
1993 rfind(const basic_string
& __str
, size_type __pos
= npos
) const
1995 { return this->rfind(__str
.data(), __pos
, __str
.size()); }
1998 * @brief Find last position of a C substring.
1999 * @param __s C string to locate.
2000 * @param __pos Index of character to search back from.
2001 * @param __n Number of characters from s to search for.
2002 * @return Index of start of last occurrence.
2004 * Starting from @a __pos, searches backward for the first @a
2005 * __n characters in @a __s within this string. If found,
2006 * returns the index where it begins. If not found, returns
2010 rfind(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
2013 * @brief Find last position of a C string.
2014 * @param __s C string to locate.
2015 * @param __pos Index of character to start search at (default end).
2016 * @return Index of start of last occurrence.
2018 * Starting from @a __pos, searches backward for the value of
2019 * @a __s within this string. If found, returns the index
2020 * where it begins. If not found, returns npos.
2023 rfind(const _CharT
* __s
, size_type __pos
= npos
) const
2025 __glibcxx_requires_string(__s
);
2026 return this->rfind(__s
, __pos
, traits_type::length(__s
));
2030 * @brief Find last position of a character.
2031 * @param __c Character to locate.
2032 * @param __pos Index of character to search back from (default end).
2033 * @return Index of last occurrence.
2035 * Starting from @a __pos, searches backward for @a __c within
2036 * this string. If found, returns the index where it was
2037 * found. If not found, returns npos.
2040 rfind(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
;
2043 * @brief Find position of a character of string.
2044 * @param __str String containing characters to locate.
2045 * @param __pos Index of character to search from (default 0).
2046 * @return Index of first occurrence.
2048 * Starting from @a __pos, searches forward for one of the
2049 * characters of @a __str within this string. If found,
2050 * returns the index where it was found. If not found, returns
2054 find_first_of(const basic_string
& __str
, size_type __pos
= 0) const
2056 { return this->find_first_of(__str
.data(), __pos
, __str
.size()); }
2059 * @brief Find position of a character of C substring.
2060 * @param __s String containing characters to locate.
2061 * @param __pos Index of character to search from.
2062 * @param __n Number of characters from s to search for.
2063 * @return Index of first occurrence.
2065 * Starting from @a __pos, searches forward for one of the
2066 * first @a __n characters of @a __s within this string. If
2067 * found, returns the index where it was found. If not found,
2071 find_first_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
2074 * @brief Find position of a character of C string.
2075 * @param __s String containing characters to locate.
2076 * @param __pos Index of character to search from (default 0).
2077 * @return Index of first occurrence.
2079 * Starting from @a __pos, searches forward for one of the
2080 * characters of @a __s within this string. If found, returns
2081 * the index where it was found. If not found, returns npos.
2084 find_first_of(const _CharT
* __s
, size_type __pos
= 0) const
2086 __glibcxx_requires_string(__s
);
2087 return this->find_first_of(__s
, __pos
, traits_type::length(__s
));
2091 * @brief Find position of a character.
2092 * @param __c Character to locate.
2093 * @param __pos Index of character to search from (default 0).
2094 * @return Index of first occurrence.
2096 * Starting from @a __pos, searches forward for the character
2097 * @a __c within this string. If found, returns the index
2098 * where it was found. If not found, returns npos.
2100 * Note: equivalent to find(__c, __pos).
2103 find_first_of(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
2104 { return this->find(__c
, __pos
); }
2107 * @brief Find last position of a character of string.
2108 * @param __str String containing characters to locate.
2109 * @param __pos Index of character to search back from (default end).
2110 * @return Index of last occurrence.
2112 * Starting from @a __pos, searches backward for one of the
2113 * characters of @a __str within this string. If found,
2114 * returns the index where it was found. If not found, returns
2118 find_last_of(const basic_string
& __str
, size_type __pos
= npos
) const
2120 { return this->find_last_of(__str
.data(), __pos
, __str
.size()); }
2123 * @brief Find last position of a character of C substring.
2124 * @param __s C string containing characters to locate.
2125 * @param __pos Index of character to search back from.
2126 * @param __n Number of characters from s to search for.
2127 * @return Index of last occurrence.
2129 * Starting from @a __pos, searches backward for one of the
2130 * first @a __n characters of @a __s within this string. If
2131 * found, returns the index where it was found. If not found,
2135 find_last_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
2138 * @brief Find last position of a character of C string.
2139 * @param __s C string containing characters to locate.
2140 * @param __pos Index of character to search back from (default end).
2141 * @return Index of last occurrence.
2143 * Starting from @a __pos, searches backward for one of the
2144 * characters of @a __s within this string. If found, returns
2145 * the index where it was found. If not found, returns npos.
2148 find_last_of(const _CharT
* __s
, size_type __pos
= npos
) const
2150 __glibcxx_requires_string(__s
);
2151 return this->find_last_of(__s
, __pos
, traits_type::length(__s
));
2155 * @brief Find last position of a character.
2156 * @param __c Character to locate.
2157 * @param __pos Index of character to search back from (default end).
2158 * @return Index of last occurrence.
2160 * Starting from @a __pos, searches backward for @a __c within
2161 * this string. If found, returns the index where it was
2162 * found. If not found, returns npos.
2164 * Note: equivalent to rfind(__c, __pos).
2167 find_last_of(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
2168 { return this->rfind(__c
, __pos
); }
2171 * @brief Find position of a character not in string.
2172 * @param __str String containing characters to avoid.
2173 * @param __pos Index of character to search from (default 0).
2174 * @return Index of first occurrence.
2176 * Starting from @a __pos, searches forward for a character not contained
2177 * in @a __str within this string. If found, returns the index where it
2178 * was found. If not found, returns npos.
2181 find_first_not_of(const basic_string
& __str
, size_type __pos
= 0) const
2183 { return this->find_first_not_of(__str
.data(), __pos
, __str
.size()); }
2186 * @brief Find position of a character not in C substring.
2187 * @param __s C string containing characters to avoid.
2188 * @param __pos Index of character to search from.
2189 * @param __n Number of characters from __s to consider.
2190 * @return Index of first occurrence.
2192 * Starting from @a __pos, searches forward for a character not
2193 * contained in the first @a __n characters of @a __s within
2194 * this string. If found, returns the index where it was
2195 * found. If not found, returns npos.
2198 find_first_not_of(const _CharT
* __s
, size_type __pos
,
2199 size_type __n
) const;
2202 * @brief Find position of a character not in C string.
2203 * @param __s C string containing characters to avoid.
2204 * @param __pos Index of character to search from (default 0).
2205 * @return Index of first occurrence.
2207 * Starting from @a __pos, searches forward for a character not
2208 * contained in @a __s within this string. If found, returns
2209 * the index where it was found. If not found, returns npos.
2212 find_first_not_of(const _CharT
* __s
, size_type __pos
= 0) const
2214 __glibcxx_requires_string(__s
);
2215 return this->find_first_not_of(__s
, __pos
, traits_type::length(__s
));
2219 * @brief Find position of a different character.
2220 * @param __c Character to avoid.
2221 * @param __pos Index of character to search from (default 0).
2222 * @return Index of first occurrence.
2224 * Starting from @a __pos, searches forward for a character
2225 * other than @a __c within this string. If found, returns the
2226 * index where it was found. If not found, returns npos.
2229 find_first_not_of(_CharT __c
, size_type __pos
= 0) const
2233 * @brief Find last position of a character not in string.
2234 * @param __str String containing characters to avoid.
2235 * @param __pos Index of character to search back from (default end).
2236 * @return Index of last occurrence.
2238 * Starting from @a __pos, searches backward for a character
2239 * not contained in @a __str within this string. If found,
2240 * returns the index where it was found. If not found, returns
2244 find_last_not_of(const basic_string
& __str
, size_type __pos
= npos
) const
2246 { return this->find_last_not_of(__str
.data(), __pos
, __str
.size()); }
2249 * @brief Find last position of a character not in C substring.
2250 * @param __s C string containing characters to avoid.
2251 * @param __pos Index of character to search back from.
2252 * @param __n Number of characters from s to consider.
2253 * @return Index of last occurrence.
2255 * Starting from @a __pos, searches backward for a character not
2256 * contained in the first @a __n characters of @a __s within this string.
2257 * If found, returns the index where it was found. If not found,
2261 find_last_not_of(const _CharT
* __s
, size_type __pos
,
2262 size_type __n
) const;
2264 * @brief Find last position of a character not in C string.
2265 * @param __s C string containing characters to avoid.
2266 * @param __pos Index of character to search back from (default end).
2267 * @return Index of last occurrence.
2269 * Starting from @a __pos, searches backward for a character
2270 * not contained in @a __s within this string. If found,
2271 * returns the index where it was found. If not found, returns
2275 find_last_not_of(const _CharT
* __s
, size_type __pos
= npos
) const
2277 __glibcxx_requires_string(__s
);
2278 return this->find_last_not_of(__s
, __pos
, traits_type::length(__s
));
2282 * @brief Find last position of a different character.
2283 * @param __c Character to avoid.
2284 * @param __pos Index of character to search back from (default end).
2285 * @return Index of last occurrence.
2287 * Starting from @a __pos, searches backward for a character other than
2288 * @a __c within this string. If found, returns the index where it was
2289 * found. If not found, returns npos.
2292 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const
2296 * @brief Get a substring.
2297 * @param __pos Index of first character (default 0).
2298 * @param __n Number of characters in substring (default remainder).
2299 * @return The new string.
2300 * @throw std::out_of_range If __pos > size().
2302 * Construct and return a new string using the @a __n
2303 * characters starting at @a __pos. If the string is too
2304 * short, use the remainder of the characters. If @a __pos is
2305 * beyond the end of the string, out_of_range is thrown.
2308 substr(size_type __pos
= 0, size_type __n
= npos
) const
2309 { return basic_string(*this,
2310 _M_check(__pos
, "basic_string::substr"), __n
); }
2313 * @brief Compare to a string.
2314 * @param __str String to compare against.
2315 * @return Integer < 0, 0, or > 0.
2317 * Returns an integer < 0 if this string is ordered before @a
2318 * __str, 0 if their values are equivalent, or > 0 if this
2319 * string is ordered after @a __str. Determines the effective
2320 * length rlen of the strings to compare as the smallest of
2321 * size() and str.size(). The function then compares the two
2322 * strings by calling traits::compare(data(), str.data(),rlen).
2323 * If the result of the comparison is nonzero returns it,
2324 * otherwise the shorter one is ordered first.
2327 compare(const basic_string
& __str
) const
2329 const size_type __size
= this->size();
2330 const size_type __osize
= __str
.size();
2331 const size_type __len
= std::min(__size
, __osize
);
2333 int __r
= traits_type::compare(_M_data(), __str
.data(), __len
);
2335 __r
= _S_compare(__size
, __osize
);
2340 * @brief Compare substring to a string.
2341 * @param __pos Index of first character of substring.
2342 * @param __n Number of characters in substring.
2343 * @param __str String to compare against.
2344 * @return Integer < 0, 0, or > 0.
2346 * Form the substring of this string from the @a __n characters
2347 * starting at @a __pos. Returns an integer < 0 if the
2348 * substring is ordered before @a __str, 0 if their values are
2349 * equivalent, or > 0 if the substring is ordered after @a
2350 * __str. Determines the effective length rlen of the strings
2351 * to compare as the smallest of the length of the substring
2352 * and @a __str.size(). The function then compares the two
2353 * strings by calling
2354 * traits::compare(substring.data(),str.data(),rlen). If the
2355 * result of the comparison is nonzero returns it, otherwise
2356 * the shorter one is ordered first.
2359 compare(size_type __pos
, size_type __n
, const basic_string
& __str
) const;
2362 * @brief Compare substring to a substring.
2363 * @param __pos1 Index of first character of substring.
2364 * @param __n1 Number of characters in substring.
2365 * @param __str String to compare against.
2366 * @param __pos2 Index of first character of substring of str.
2367 * @param __n2 Number of characters in substring of str.
2368 * @return Integer < 0, 0, or > 0.
2370 * Form the substring of this string from the @a __n1
2371 * characters starting at @a __pos1. Form the substring of @a
2372 * __str from the @a __n2 characters starting at @a __pos2.
2373 * Returns an integer < 0 if this substring is ordered before
2374 * the substring of @a __str, 0 if their values are equivalent,
2375 * or > 0 if this substring is ordered after the substring of
2376 * @a __str. Determines the effective length rlen of the
2377 * strings to compare as the smallest of the lengths of the
2378 * substrings. The function then compares the two strings by
2380 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2381 * If the result of the comparison is nonzero returns it,
2382 * otherwise the shorter one is ordered first.
2385 compare(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
2386 size_type __pos2
, size_type __n2
) const;
2389 * @brief Compare to a C string.
2390 * @param __s C string to compare against.
2391 * @return Integer < 0, 0, or > 0.
2393 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
2394 * their values are equivalent, or > 0 if this string is ordered after
2395 * @a __s. Determines the effective length rlen of the strings to
2396 * compare as the smallest of size() and the length of a string
2397 * constructed from @a __s. The function then compares the two strings
2398 * by calling traits::compare(data(),s,rlen). If the result of the
2399 * comparison is nonzero returns it, otherwise the shorter one is
2403 compare(const _CharT
* __s
) const;
2405 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2406 // 5 String::compare specification questionable
2408 * @brief Compare substring to a C string.
2409 * @param __pos Index of first character of substring.
2410 * @param __n1 Number of characters in substring.
2411 * @param __s C string to compare against.
2412 * @return Integer < 0, 0, or > 0.
2414 * Form the substring of this string from the @a __n1
2415 * characters starting at @a pos. Returns an integer < 0 if
2416 * the substring is ordered before @a __s, 0 if their values
2417 * are equivalent, or > 0 if the substring is ordered after @a
2418 * __s. Determines the effective length rlen of the strings to
2419 * compare as the smallest of the length of the substring and
2420 * the length of a string constructed from @a __s. The
2421 * function then compares the two string by calling
2422 * traits::compare(substring.data(),__s,rlen). If the result of
2423 * the comparison is nonzero returns it, otherwise the shorter
2424 * one is ordered first.
2427 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
) const;
2430 * @brief Compare substring against a character %array.
2431 * @param __pos Index of first character of substring.
2432 * @param __n1 Number of characters in substring.
2433 * @param __s character %array to compare against.
2434 * @param __n2 Number of characters of s.
2435 * @return Integer < 0, 0, or > 0.
2437 * Form the substring of this string from the @a __n1
2438 * characters starting at @a __pos. Form a string from the
2439 * first @a __n2 characters of @a __s. Returns an integer < 0
2440 * if this substring is ordered before the string from @a __s,
2441 * 0 if their values are equivalent, or > 0 if this substring
2442 * is ordered after the string from @a __s. Determines the
2443 * effective length rlen of the strings to compare as the
2444 * smallest of the length of the substring and @a __n2. The
2445 * function then compares the two strings by calling
2446 * traits::compare(substring.data(),s,rlen). If the result of
2447 * the comparison is nonzero returns it, otherwise the shorter
2448 * one is ordered first.
2450 * NB: s must have at least n2 characters, '\\0' has
2451 * no special meaning.
2454 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
,
2455 size_type __n2
) const;
2457 _GLIBCXX_END_NAMESPACE_CXX11
2458 #else // !_GLIBCXX_USE_CXX11_ABI
2459 // Reference-counted COW string implentation
2462 * @class basic_string basic_string.h <string>
2463 * @brief Managing sequences of characters and character-like objects.
2466 * @ingroup sequences
2468 * @tparam _CharT Type of character
2469 * @tparam _Traits Traits for character type, defaults to
2470 * char_traits<_CharT>.
2471 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
2473 * Meets the requirements of a <a href="tables.html#65">container</a>, a
2474 * <a href="tables.html#66">reversible container</a>, and a
2475 * <a href="tables.html#67">sequence</a>. Of the
2476 * <a href="tables.html#68">optional sequence requirements</a>, only
2477 * @c push_back, @c at, and @c %array access are supported.
2482 * Documentation? What's that?
2483 * Nathan Myers <ncm@cantrip.org>.
2485 * A string looks like this:
2490 * [basic_string<char_type>] _M_capacity
2491 * _M_dataplus _M_refcount
2492 * _M_p ----------------> unnamed array of char_type
2495 * Where the _M_p points to the first character in the string, and
2496 * you cast it to a pointer-to-_Rep and subtract 1 to get a
2497 * pointer to the header.
2499 * This approach has the enormous advantage that a string object
2500 * requires only one allocation. All the ugliness is confined
2501 * within a single %pair of inline functions, which each compile to
2502 * a single @a add instruction: _Rep::_M_data(), and
2503 * string::_M_rep(); and the allocation function which gets a
2504 * block of raw bytes and with room enough and constructs a _Rep
2505 * object at the front.
2507 * The reason you want _M_data pointing to the character %array and
2508 * not the _Rep is so that the debugger can see the string
2509 * contents. (Probably we should add a non-inline member to get
2510 * the _Rep for the debugger to use, so users can check the actual
2513 * Note that the _Rep object is a POD so that you can have a
2514 * static <em>empty string</em> _Rep object already @a constructed before
2515 * static constructors have run. The reference-count encoding is
2516 * chosen so that a 0 indicates one reference, so you never try to
2517 * destroy the empty-string _Rep object.
2519 * All but the last paragraph is considered pretty conventional
2520 * for a C++ string implementation.
2522 // 21.3 Template class basic_string
2523 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2526 typedef typename
_Alloc::template rebind
<_CharT
>::other _CharT_alloc_type
;
2530 typedef _Traits traits_type
;
2531 typedef typename
_Traits::char_type value_type
;
2532 typedef _Alloc allocator_type
;
2533 typedef typename
_CharT_alloc_type::size_type size_type
;
2534 typedef typename
_CharT_alloc_type::difference_type difference_type
;
2535 typedef typename
_CharT_alloc_type::reference reference
;
2536 typedef typename
_CharT_alloc_type::const_reference const_reference
;
2537 typedef typename
_CharT_alloc_type::pointer pointer
;
2538 typedef typename
_CharT_alloc_type::const_pointer const_pointer
;
2539 typedef __gnu_cxx::__normal_iterator
<pointer
, basic_string
> iterator
;
2540 typedef __gnu_cxx::__normal_iterator
<const_pointer
, basic_string
>
2542 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
2543 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
2546 // _Rep: string representation
2548 // 1. String really contains _M_length + 1 characters: due to 21.3.4
2549 // must be kept null-terminated.
2550 // 2. _M_capacity >= _M_length
2551 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
2552 // 3. _M_refcount has three states:
2553 // -1: leaked, one reference, no ref-copies allowed, non-const.
2554 // 0: one reference, non-const.
2555 // n>0: n + 1 references, operations require a lock, const.
2556 // 4. All fields==0 is an empty string, given the extra storage
2557 // beyond-the-end for a null terminator; thus, the shared
2558 // empty string representation needs no constructor.
2562 size_type _M_length
;
2563 size_type _M_capacity
;
2564 _Atomic_word _M_refcount
;
2567 struct _Rep
: _Rep_base
2570 typedef typename
_Alloc::template rebind
<char>::other _Raw_bytes_alloc
;
2572 // (Public) Data members:
2574 // The maximum number of individual char_type elements of an
2575 // individual string is determined by _S_max_size. This is the
2576 // value that will be returned by max_size(). (Whereas npos
2577 // is the maximum number of bytes the allocator can allocate.)
2578 // If one was to divvy up the theoretical largest size string,
2579 // with a terminating character and m _CharT elements, it'd
2581 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
2583 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
2584 // In addition, this implementation quarters this amount.
2585 static const size_type _S_max_size
;
2586 static const _CharT _S_terminal
;
2588 // The following storage is init'd to 0 by the linker, resulting
2589 // (carefully) in an empty string with one reference.
2590 static size_type _S_empty_rep_storage
[];
2593 _S_empty_rep() _GLIBCXX_NOEXCEPT
2595 // NB: Mild hack to avoid strict-aliasing warnings. Note that
2596 // _S_empty_rep_storage is never modified and the punning should
2597 // be reasonably safe in this case.
2598 void* __p
= reinterpret_cast<void*>(&_S_empty_rep_storage
);
2599 return *reinterpret_cast<_Rep
*>(__p
);
2603 _M_is_leaked() const _GLIBCXX_NOEXCEPT
2605 #if defined(__GTHREADS)
2606 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
2607 // so we need to use an atomic load. However, _M_is_leaked
2608 // predicate does not change concurrently (i.e. the string is either
2609 // leaked or not), so a relaxed load is enough.
2610 return __atomic_load_n(&this->_M_refcount
, __ATOMIC_RELAXED
) < 0;
2612 return this->_M_refcount
< 0;
2617 _M_is_shared() const _GLIBCXX_NOEXCEPT
2619 #if defined(__GTHREADS)
2620 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
2621 // so we need to use an atomic load. Another thread can drop last
2622 // but one reference concurrently with this check, so we need this
2623 // load to be acquire to synchronize with release fetch_and_add in
2625 return __atomic_load_n(&this->_M_refcount
, __ATOMIC_ACQUIRE
) > 0;
2627 return this->_M_refcount
> 0;
2632 _M_set_leaked() _GLIBCXX_NOEXCEPT
2633 { this->_M_refcount
= -1; }
2636 _M_set_sharable() _GLIBCXX_NOEXCEPT
2637 { this->_M_refcount
= 0; }
2640 _M_set_length_and_sharable(size_type __n
) _GLIBCXX_NOEXCEPT
2642 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2643 if (__builtin_expect(this != &_S_empty_rep(), false))
2646 this->_M_set_sharable(); // One reference.
2647 this->_M_length
= __n
;
2648 traits_type::assign(this->_M_refdata()[__n
], _S_terminal
);
2649 // grrr. (per 21.3.4)
2650 // You cannot leave those LWG people alone for a second.
2655 _M_refdata() throw()
2656 { return reinterpret_cast<_CharT
*>(this + 1); }
2659 _M_grab(const _Alloc
& __alloc1
, const _Alloc
& __alloc2
)
2661 return (!_M_is_leaked() && __alloc1
== __alloc2
)
2662 ? _M_refcopy() : _M_clone(__alloc1
);
2667 _S_create(size_type
, size_type
, const _Alloc
&);
2670 _M_dispose(const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
2672 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2673 if (__builtin_expect(this != &_S_empty_rep(), false))
2676 // Be race-detector-friendly. For more info see bits/c++config.
2677 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount
);
2678 // Decrement of _M_refcount is acq_rel, because:
2679 // - all but last decrements need to release to synchronize with
2680 // the last decrement that will delete the object.
2681 // - the last decrement needs to acquire to synchronize with
2682 // all the previous decrements.
2683 // - last but one decrement needs to release to synchronize with
2684 // the acquire load in _M_is_shared that will conclude that
2685 // the object is not shared anymore.
2686 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount
,
2689 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount
);
2696 _M_destroy(const _Alloc
&) throw();
2699 _M_refcopy() throw()
2701 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2702 if (__builtin_expect(this != &_S_empty_rep(), false))
2704 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount
, 1);
2705 return _M_refdata();
2709 _M_clone(const _Alloc
&, size_type __res
= 0);
2712 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
2713 struct _Alloc_hider
: _Alloc
2715 _Alloc_hider(_CharT
* __dat
, const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
2716 : _Alloc(__a
), _M_p(__dat
) { }
2718 _CharT
* _M_p
; // The actual data.
2722 // Data Members (public):
2723 // NB: This is an unsigned type, and thus represents the maximum
2724 // size that the allocator can hold.
2725 /// Value returned by various member functions when they fail.
2726 static const size_type npos
= static_cast<size_type
>(-1);
2729 // Data Members (private):
2730 mutable _Alloc_hider _M_dataplus
;
2733 _M_data() const _GLIBCXX_NOEXCEPT
2734 { return _M_dataplus
._M_p
; }
2737 _M_data(_CharT
* __p
) _GLIBCXX_NOEXCEPT
2738 { return (_M_dataplus
._M_p
= __p
); }
2741 _M_rep() const _GLIBCXX_NOEXCEPT
2742 { return &((reinterpret_cast<_Rep
*> (_M_data()))[-1]); }
2744 // For the internal use we have functions similar to `begin'/`end'
2745 // but they do not call _M_leak.
2747 _M_ibegin() const _GLIBCXX_NOEXCEPT
2748 { return iterator(_M_data()); }
2751 _M_iend() const _GLIBCXX_NOEXCEPT
2752 { return iterator(_M_data() + this->size()); }
2755 _M_leak() // for use in begin() & non-const op[]
2757 if (!_M_rep()->_M_is_leaked())
2762 _M_check(size_type __pos
, const char* __s
) const
2764 if (__pos
> this->size())
2765 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
2766 "this->size() (which is %zu)"),
2767 __s
, __pos
, this->size());
2772 _M_check_length(size_type __n1
, size_type __n2
, const char* __s
) const
2774 if (this->max_size() - (this->size() - __n1
) < __n2
)
2775 __throw_length_error(__N(__s
));
2778 // NB: _M_limit doesn't check for a bad __pos value.
2780 _M_limit(size_type __pos
, size_type __off
) const _GLIBCXX_NOEXCEPT
2782 const bool __testoff
= __off
< this->size() - __pos
;
2783 return __testoff
? __off
: this->size() - __pos
;
2786 // True if _Rep and source do not overlap.
2788 _M_disjunct(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
2790 return (less
<const _CharT
*>()(__s
, _M_data())
2791 || less
<const _CharT
*>()(_M_data() + this->size(), __s
));
2794 // When __n = 1 way faster than the general multichar
2795 // traits_type::copy/move/assign.
2797 _M_copy(_CharT
* __d
, const _CharT
* __s
, size_type __n
) _GLIBCXX_NOEXCEPT
2800 traits_type::assign(*__d
, *__s
);
2802 traits_type::copy(__d
, __s
, __n
);
2806 _M_move(_CharT
* __d
, const _CharT
* __s
, size_type __n
) _GLIBCXX_NOEXCEPT
2809 traits_type::assign(*__d
, *__s
);
2811 traits_type::move(__d
, __s
, __n
);
2815 _M_assign(_CharT
* __d
, size_type __n
, _CharT __c
) _GLIBCXX_NOEXCEPT
2818 traits_type::assign(*__d
, __c
);
2820 traits_type::assign(__d
, __n
, __c
);
2823 // _S_copy_chars is a separate template to permit specialization
2824 // to optimize for the common case of pointers as iterators.
2825 template<class _Iterator
>
2827 _S_copy_chars(_CharT
* __p
, _Iterator __k1
, _Iterator __k2
)
2830 for (; __k1
!= __k2
; ++__k1
, (void)++__p
)
2831 traits_type::assign(*__p
, *__k1
); // These types are off.
2835 _S_copy_chars(_CharT
* __p
, iterator __k1
, iterator __k2
) _GLIBCXX_NOEXCEPT
2836 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
2839 _S_copy_chars(_CharT
* __p
, const_iterator __k1
, const_iterator __k2
)
2841 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
2844 _S_copy_chars(_CharT
* __p
, _CharT
* __k1
, _CharT
* __k2
) _GLIBCXX_NOEXCEPT
2845 { _M_copy(__p
, __k1
, __k2
- __k1
); }
2848 _S_copy_chars(_CharT
* __p
, const _CharT
* __k1
, const _CharT
* __k2
)
2850 { _M_copy(__p
, __k1
, __k2
- __k1
); }
2853 _S_compare(size_type __n1
, size_type __n2
) _GLIBCXX_NOEXCEPT
2855 const difference_type __d
= difference_type(__n1
- __n2
);
2857 if (__d
> __gnu_cxx::__numeric_traits
<int>::__max
)
2858 return __gnu_cxx::__numeric_traits
<int>::__max
;
2859 else if (__d
< __gnu_cxx::__numeric_traits
<int>::__min
)
2860 return __gnu_cxx::__numeric_traits
<int>::__min
;
2866 _M_mutate(size_type __pos
, size_type __len1
, size_type __len2
);
2872 _S_empty_rep() _GLIBCXX_NOEXCEPT
2873 { return _Rep::_S_empty_rep(); }
2876 // Construct/copy/destroy:
2877 // NB: We overload ctors in some cases instead of using default
2878 // arguments, per 17.4.4.4 para. 2 item 2.
2881 * @brief Default constructor creates an empty string.
2884 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2885 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
2887 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ }
2891 * @brief Construct an empty string using allocator @a a.
2894 basic_string(const _Alloc
& __a
);
2896 // NB: per LWG issue 42, semantics different from IS:
2898 * @brief Construct string with copy of value of @a str.
2899 * @param __str Source string.
2901 basic_string(const basic_string
& __str
);
2903 * @brief Construct string as copy of a substring.
2904 * @param __str Source string.
2905 * @param __pos Index of first character to copy from.
2906 * @param __n Number of characters to copy (default remainder).
2908 basic_string(const basic_string
& __str
, size_type __pos
,
2909 size_type __n
= npos
);
2911 * @brief Construct string as copy of a substring.
2912 * @param __str Source string.
2913 * @param __pos Index of first character to copy from.
2914 * @param __n Number of characters to copy.
2915 * @param __a Allocator to use.
2917 basic_string(const basic_string
& __str
, size_type __pos
,
2918 size_type __n
, const _Alloc
& __a
);
2921 * @brief Construct string initialized by a character %array.
2922 * @param __s Source character %array.
2923 * @param __n Number of characters to copy.
2924 * @param __a Allocator to use (default is default allocator).
2926 * NB: @a __s must have at least @a __n characters, '\\0'
2927 * has no special meaning.
2929 basic_string(const _CharT
* __s
, size_type __n
,
2930 const _Alloc
& __a
= _Alloc());
2932 * @brief Construct string as copy of a C string.
2933 * @param __s Source C string.
2934 * @param __a Allocator to use (default is default allocator).
2936 basic_string(const _CharT
* __s
, const _Alloc
& __a
= _Alloc());
2938 * @brief Construct string as multiple characters.
2939 * @param __n Number of characters.
2940 * @param __c Character to use.
2941 * @param __a Allocator to use (default is default allocator).
2943 basic_string(size_type __n
, _CharT __c
, const _Alloc
& __a
= _Alloc());
2945 #if __cplusplus >= 201103L
2947 * @brief Move construct string.
2948 * @param __str Source string.
2950 * The newly-created string contains the exact contents of @a __str.
2951 * @a __str is a valid, but unspecified string.
2953 basic_string(basic_string
&& __str
)
2954 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2955 noexcept
// FIXME C++11: should always be noexcept.
2957 : _M_dataplus(__str
._M_dataplus
)
2959 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2960 __str
._M_data(_S_empty_rep()._M_refdata());
2962 __str
._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
2967 * @brief Construct string from an initializer %list.
2968 * @param __l std::initializer_list of characters.
2969 * @param __a Allocator to use (default is default allocator).
2971 basic_string(initializer_list
<_CharT
> __l
, const _Alloc
& __a
= _Alloc());
2975 * @brief Construct string as copy of a range.
2976 * @param __beg Start of range.
2977 * @param __end End of range.
2978 * @param __a Allocator to use (default is default allocator).
2980 template<class _InputIterator
>
2981 basic_string(_InputIterator __beg
, _InputIterator __end
,
2982 const _Alloc
& __a
= _Alloc());
2985 * @brief Destroy the string instance.
2987 ~basic_string() _GLIBCXX_NOEXCEPT
2988 { _M_rep()->_M_dispose(this->get_allocator()); }
2991 * @brief Assign the value of @a str to this string.
2992 * @param __str Source string.
2995 operator=(const basic_string
& __str
)
2996 { return this->assign(__str
); }
2999 * @brief Copy contents of @a s into this string.
3000 * @param __s Source null-terminated string.
3003 operator=(const _CharT
* __s
)
3004 { return this->assign(__s
); }
3007 * @brief Set value to string of length 1.
3008 * @param __c Source character.
3010 * Assigning to a character makes this string length 1 and
3011 * (*this)[0] == @a c.
3014 operator=(_CharT __c
)
3016 this->assign(1, __c
);
3020 #if __cplusplus >= 201103L
3022 * @brief Move assign the value of @a str to this string.
3023 * @param __str Source string.
3025 * The contents of @a str are moved into this string (without copying).
3026 * @a str is a valid, but unspecified string.
3028 // PR 58265, this should be noexcept.
3030 operator=(basic_string
&& __str
)
3038 * @brief Set value to string constructed from initializer %list.
3039 * @param __l std::initializer_list.
3042 operator=(initializer_list
<_CharT
> __l
)
3044 this->assign(__l
.begin(), __l
.size());
3051 * Returns a read/write iterator that points to the first character in
3052 * the %string. Unshares the string.
3055 begin() // FIXME C++11: should be noexcept.
3058 return iterator(_M_data());
3062 * Returns a read-only (constant) iterator that points to the first
3063 * character in the %string.
3066 begin() const _GLIBCXX_NOEXCEPT
3067 { return const_iterator(_M_data()); }
3070 * Returns a read/write iterator that points one past the last
3071 * character in the %string. Unshares the string.
3074 end() // FIXME C++11: should be noexcept.
3077 return iterator(_M_data() + this->size());
3081 * Returns a read-only (constant) iterator that points one past the
3082 * last character in the %string.
3085 end() const _GLIBCXX_NOEXCEPT
3086 { return const_iterator(_M_data() + this->size()); }
3089 * Returns a read/write reverse iterator that points to the last
3090 * character in the %string. Iteration is done in reverse element
3091 * order. Unshares the string.
3094 rbegin() // FIXME C++11: should be noexcept.
3095 { return reverse_iterator(this->end()); }
3098 * Returns a read-only (constant) reverse iterator that points
3099 * to the last character in the %string. Iteration is done in
3100 * reverse element order.
3102 const_reverse_iterator
3103 rbegin() const _GLIBCXX_NOEXCEPT
3104 { return const_reverse_iterator(this->end()); }
3107 * Returns a read/write reverse iterator that points to one before the
3108 * first character in the %string. Iteration is done in reverse
3109 * element order. Unshares the string.
3112 rend() // FIXME C++11: should be noexcept.
3113 { return reverse_iterator(this->begin()); }
3116 * Returns a read-only (constant) reverse iterator that points
3117 * to one before the first character in the %string. Iteration
3118 * is done in reverse element order.
3120 const_reverse_iterator
3121 rend() const _GLIBCXX_NOEXCEPT
3122 { return const_reverse_iterator(this->begin()); }
3124 #if __cplusplus >= 201103L
3126 * Returns a read-only (constant) iterator that points to the first
3127 * character in the %string.
3130 cbegin() const noexcept
3131 { return const_iterator(this->_M_data()); }
3134 * Returns a read-only (constant) iterator that points one past the
3135 * last character in the %string.
3138 cend() const noexcept
3139 { return const_iterator(this->_M_data() + this->size()); }
3142 * Returns a read-only (constant) reverse iterator that points
3143 * to the last character in the %string. Iteration is done in
3144 * reverse element order.
3146 const_reverse_iterator
3147 crbegin() const noexcept
3148 { return const_reverse_iterator(this->end()); }
3151 * Returns a read-only (constant) reverse iterator that points
3152 * to one before the first character in the %string. Iteration
3153 * is done in reverse element order.
3155 const_reverse_iterator
3156 crend() const noexcept
3157 { return const_reverse_iterator(this->begin()); }
3162 /// Returns the number of characters in the string, not including any
3163 /// null-termination.
3165 size() const _GLIBCXX_NOEXCEPT
3166 { return _M_rep()->_M_length
; }
3168 /// Returns the number of characters in the string, not including any
3169 /// null-termination.
3171 length() const _GLIBCXX_NOEXCEPT
3172 { return _M_rep()->_M_length
; }
3174 /// Returns the size() of the largest possible %string.
3176 max_size() const _GLIBCXX_NOEXCEPT
3177 { return _Rep::_S_max_size
; }
3180 * @brief Resizes the %string to the specified number of characters.
3181 * @param __n Number of characters the %string should contain.
3182 * @param __c Character to fill any new elements.
3184 * This function will %resize the %string to the specified
3185 * number of characters. If the number is smaller than the
3186 * %string's current size the %string is truncated, otherwise
3187 * the %string is extended and new elements are %set to @a __c.
3190 resize(size_type __n
, _CharT __c
);
3193 * @brief Resizes the %string to the specified number of characters.
3194 * @param __n Number of characters the %string should contain.
3196 * This function will resize the %string to the specified length. If
3197 * the new size is smaller than the %string's current size the %string
3198 * is truncated, otherwise the %string is extended and new characters
3199 * are default-constructed. For basic types such as char, this means
3200 * setting them to 0.
3203 resize(size_type __n
)
3204 { this->resize(__n
, _CharT()); }
3206 #if __cplusplus >= 201103L
3207 /// A non-binding request to reduce capacity() to size().
3209 shrink_to_fit() _GLIBCXX_NOEXCEPT
3211 if (capacity() > size())
3222 * Returns the total number of characters that the %string can hold
3223 * before needing to allocate more memory.
3226 capacity() const _GLIBCXX_NOEXCEPT
3227 { return _M_rep()->_M_capacity
; }
3230 * @brief Attempt to preallocate enough memory for specified number of
3232 * @param __res_arg Number of characters required.
3233 * @throw std::length_error If @a __res_arg exceeds @c max_size().
3235 * This function attempts to reserve enough memory for the
3236 * %string to hold the specified number of characters. If the
3237 * number requested is more than max_size(), length_error is
3240 * The advantage of this function is that if optimal code is a
3241 * necessity and the user can determine the string length that will be
3242 * required, the user can reserve the memory in %advance, and thus
3243 * prevent a possible reallocation of memory and copying of %string
3247 reserve(size_type __res_arg
= 0);
3250 * Erases the string, making it empty.
3252 // PR 56166: this should not throw.
3255 { _M_mutate(0, this->size(), 0); }
3258 * Returns true if the %string is empty. Equivalent to
3259 * <code>*this == ""</code>.
3262 empty() const _GLIBCXX_NOEXCEPT
3263 { return this->size() == 0; }
3267 * @brief Subscript access to the data contained in the %string.
3268 * @param __pos The index of the character to access.
3269 * @return Read-only (constant) reference to the character.
3271 * This operator allows for easy, array-style, data access.
3272 * Note that data access with this operator is unchecked and
3273 * out_of_range lookups are not defined. (For checked lookups
3277 operator[] (size_type __pos
) const _GLIBCXX_NOEXCEPT
3279 _GLIBCXX_DEBUG_ASSERT(__pos
<= size());
3280 return _M_data()[__pos
];
3284 * @brief Subscript access to the data contained in the %string.
3285 * @param __pos The index of the character to access.
3286 * @return Read/write reference to the character.
3288 * This operator allows for easy, array-style, data access.
3289 * Note that data access with this operator is unchecked and
3290 * out_of_range lookups are not defined. (For checked lookups
3291 * see at().) Unshares the string.
3294 operator[](size_type __pos
)
3296 // Allow pos == size() both in C++98 mode, as v3 extension,
3297 // and in C++11 mode.
3298 _GLIBCXX_DEBUG_ASSERT(__pos
<= size());
3299 // In pedantic mode be strict in C++98 mode.
3300 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus
>= 201103L || __pos
< size());
3302 return _M_data()[__pos
];
3306 * @brief Provides access to the data contained in the %string.
3307 * @param __n The index of the character to access.
3308 * @return Read-only (const) reference to the character.
3309 * @throw std::out_of_range If @a n is an invalid index.
3311 * This function provides for safer data access. The parameter is
3312 * first checked that it is in the range of the string. The function
3313 * throws out_of_range if the check fails.
3316 at(size_type __n
) const
3318 if (__n
>= this->size())
3319 __throw_out_of_range_fmt(__N("basic_string::at: __n "
3320 "(which is %zu) >= this->size() "
3323 return _M_data()[__n
];
3327 * @brief Provides access to the data contained in the %string.
3328 * @param __n The index of the character to access.
3329 * @return Read/write reference to the character.
3330 * @throw std::out_of_range If @a n is an invalid index.
3332 * This function provides for safer data access. The parameter is
3333 * first checked that it is in the range of the string. The function
3334 * throws out_of_range if the check fails. Success results in
3335 * unsharing the string.
3341 __throw_out_of_range_fmt(__N("basic_string::at: __n "
3342 "(which is %zu) >= this->size() "
3346 return _M_data()[__n
];
3349 #if __cplusplus >= 201103L
3351 * Returns a read/write reference to the data at the first
3352 * element of the %string.
3357 _GLIBCXX_DEBUG_ASSERT(!empty());
3358 return operator[](0);
3362 * Returns a read-only (constant) reference to the data at the first
3363 * element of the %string.
3366 front() const _GLIBCXX_NOEXCEPT
3368 _GLIBCXX_DEBUG_ASSERT(!empty());
3369 return operator[](0);
3373 * Returns a read/write reference to the data at the last
3374 * element of the %string.
3379 _GLIBCXX_DEBUG_ASSERT(!empty());
3380 return operator[](this->size() - 1);
3384 * Returns a read-only (constant) reference to the data at the
3385 * last element of the %string.
3388 back() const _GLIBCXX_NOEXCEPT
3390 _GLIBCXX_DEBUG_ASSERT(!empty());
3391 return operator[](this->size() - 1);
3397 * @brief Append a string to this string.
3398 * @param __str The string to append.
3399 * @return Reference to this string.
3402 operator+=(const basic_string
& __str
)
3403 { return this->append(__str
); }
3406 * @brief Append a C string.
3407 * @param __s The C string to append.
3408 * @return Reference to this string.
3411 operator+=(const _CharT
* __s
)
3412 { return this->append(__s
); }
3415 * @brief Append a character.
3416 * @param __c The character to append.
3417 * @return Reference to this string.
3420 operator+=(_CharT __c
)
3422 this->push_back(__c
);
3426 #if __cplusplus >= 201103L
3428 * @brief Append an initializer_list of characters.
3429 * @param __l The initializer_list of characters to be appended.
3430 * @return Reference to this string.
3433 operator+=(initializer_list
<_CharT
> __l
)
3434 { return this->append(__l
.begin(), __l
.size()); }
3438 * @brief Append a string to this string.
3439 * @param __str The string to append.
3440 * @return Reference to this string.
3443 append(const basic_string
& __str
);
3446 * @brief Append a substring.
3447 * @param __str The string to append.
3448 * @param __pos Index of the first character of str to append.
3449 * @param __n The number of characters to append.
3450 * @return Reference to this string.
3451 * @throw std::out_of_range if @a __pos is not a valid index.
3453 * This function appends @a __n characters from @a __str
3454 * starting at @a __pos to this string. If @a __n is is larger
3455 * than the number of available characters in @a __str, the
3456 * remainder of @a __str is appended.
3459 append(const basic_string
& __str
, size_type __pos
, size_type __n
);
3462 * @brief Append a C substring.
3463 * @param __s The C string to append.
3464 * @param __n The number of characters to append.
3465 * @return Reference to this string.
3468 append(const _CharT
* __s
, size_type __n
);
3471 * @brief Append a C string.
3472 * @param __s The C string to append.
3473 * @return Reference to this string.
3476 append(const _CharT
* __s
)
3478 __glibcxx_requires_string(__s
);
3479 return this->append(__s
, traits_type::length(__s
));
3483 * @brief Append multiple characters.
3484 * @param __n The number of characters to append.
3485 * @param __c The character to use.
3486 * @return Reference to this string.
3488 * Appends __n copies of __c to this string.
3491 append(size_type __n
, _CharT __c
);
3493 #if __cplusplus >= 201103L
3495 * @brief Append an initializer_list of characters.
3496 * @param __l The initializer_list of characters to append.
3497 * @return Reference to this string.
3500 append(initializer_list
<_CharT
> __l
)
3501 { return this->append(__l
.begin(), __l
.size()); }
3505 * @brief Append a range of characters.
3506 * @param __first Iterator referencing the first character to append.
3507 * @param __last Iterator marking the end of the range.
3508 * @return Reference to this string.
3510 * Appends characters in the range [__first,__last) to this string.
3512 template<class _InputIterator
>
3514 append(_InputIterator __first
, _InputIterator __last
)
3515 { return this->replace(_M_iend(), _M_iend(), __first
, __last
); }
3518 * @brief Append a single character.
3519 * @param __c Character to append.
3522 push_back(_CharT __c
)
3524 const size_type __len
= 1 + this->size();
3525 if (__len
> this->capacity() || _M_rep()->_M_is_shared())
3526 this->reserve(__len
);
3527 traits_type::assign(_M_data()[this->size()], __c
);
3528 _M_rep()->_M_set_length_and_sharable(__len
);
3532 * @brief Set value to contents of another string.
3533 * @param __str Source string to use.
3534 * @return Reference to this string.
3537 assign(const basic_string
& __str
);
3539 #if __cplusplus >= 201103L
3541 * @brief Set value to contents of another string.
3542 * @param __str Source string to use.
3543 * @return Reference to this string.
3545 * This function sets this string to the exact contents of @a __str.
3546 * @a __str is a valid, but unspecified string.
3548 // PR 58265, this should be noexcept.
3550 assign(basic_string
&& __str
)
3558 * @brief Set value to a substring of a string.
3559 * @param __str The string to use.
3560 * @param __pos Index of the first character of str.
3561 * @param __n Number of characters to use.
3562 * @return Reference to this string.
3563 * @throw std::out_of_range if @a pos is not a valid index.
3565 * This function sets this string to the substring of @a __str
3566 * consisting of @a __n characters at @a __pos. If @a __n is
3567 * is larger than the number of available characters in @a
3568 * __str, the remainder of @a __str is used.
3571 assign(const basic_string
& __str
, size_type __pos
, size_type __n
)
3572 { return this->assign(__str
._M_data()
3573 + __str
._M_check(__pos
, "basic_string::assign"),
3574 __str
._M_limit(__pos
, __n
)); }
3577 * @brief Set value to a C substring.
3578 * @param __s The C string to use.
3579 * @param __n Number of characters to use.
3580 * @return Reference to this string.
3582 * This function sets the value of this string to the first @a __n
3583 * characters of @a __s. If @a __n is is larger than the number of
3584 * available characters in @a __s, the remainder of @a __s is used.
3587 assign(const _CharT
* __s
, size_type __n
);
3590 * @brief Set value to contents of a C string.
3591 * @param __s The C string to use.
3592 * @return Reference to this string.
3594 * This function sets the value of this string to the value of @a __s.
3595 * The data is copied, so there is no dependence on @a __s once the
3599 assign(const _CharT
* __s
)
3601 __glibcxx_requires_string(__s
);
3602 return this->assign(__s
, traits_type::length(__s
));
3606 * @brief Set value to multiple characters.
3607 * @param __n Length of the resulting string.
3608 * @param __c The character to use.
3609 * @return Reference to this string.
3611 * This function sets the value of this string to @a __n copies of
3615 assign(size_type __n
, _CharT __c
)
3616 { return _M_replace_aux(size_type(0), this->size(), __n
, __c
); }
3619 * @brief Set value to a range of characters.
3620 * @param __first Iterator referencing the first character to append.
3621 * @param __last Iterator marking the end of the range.
3622 * @return Reference to this string.
3624 * Sets value of string to characters in the range [__first,__last).
3626 template<class _InputIterator
>
3628 assign(_InputIterator __first
, _InputIterator __last
)
3629 { return this->replace(_M_ibegin(), _M_iend(), __first
, __last
); }
3631 #if __cplusplus >= 201103L
3633 * @brief Set value to an initializer_list of characters.
3634 * @param __l The initializer_list of characters to assign.
3635 * @return Reference to this string.
3638 assign(initializer_list
<_CharT
> __l
)
3639 { return this->assign(__l
.begin(), __l
.size()); }
3643 * @brief Insert multiple characters.
3644 * @param __p Iterator referencing location in string to insert at.
3645 * @param __n Number of characters to insert
3646 * @param __c The character to insert.
3647 * @throw std::length_error If new length exceeds @c max_size().
3649 * Inserts @a __n copies of character @a __c starting at the
3650 * position referenced by iterator @a __p. If adding
3651 * characters causes the length to exceed max_size(),
3652 * length_error is thrown. The value of the string doesn't
3653 * change if an error is thrown.
3656 insert(iterator __p
, size_type __n
, _CharT __c
)
3657 { this->replace(__p
, __p
, __n
, __c
); }
3660 * @brief Insert a range of characters.
3661 * @param __p Iterator referencing location in string to insert at.
3662 * @param __beg Start of range.
3663 * @param __end End of range.
3664 * @throw std::length_error If new length exceeds @c max_size().
3666 * Inserts characters in range [__beg,__end). If adding
3667 * characters causes the length to exceed max_size(),
3668 * length_error is thrown. The value of the string doesn't
3669 * change if an error is thrown.
3671 template<class _InputIterator
>
3673 insert(iterator __p
, _InputIterator __beg
, _InputIterator __end
)
3674 { this->replace(__p
, __p
, __beg
, __end
); }
3676 #if __cplusplus >= 201103L
3678 * @brief Insert an initializer_list of characters.
3679 * @param __p Iterator referencing location in string to insert at.
3680 * @param __l The initializer_list of characters to insert.
3681 * @throw std::length_error If new length exceeds @c max_size().
3684 insert(iterator __p
, initializer_list
<_CharT
> __l
)
3686 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
3687 this->insert(__p
- _M_ibegin(), __l
.begin(), __l
.size());
3692 * @brief Insert value of a string.
3693 * @param __pos1 Iterator referencing location in string to insert at.
3694 * @param __str The string to insert.
3695 * @return Reference to this string.
3696 * @throw std::length_error If new length exceeds @c max_size().
3698 * Inserts value of @a __str starting at @a __pos1. If adding
3699 * characters causes the length to exceed max_size(),
3700 * length_error is thrown. The value of the string doesn't
3701 * change if an error is thrown.
3704 insert(size_type __pos1
, const basic_string
& __str
)
3705 { return this->insert(__pos1
, __str
, size_type(0), __str
.size()); }
3708 * @brief Insert a substring.
3709 * @param __pos1 Iterator referencing location in string to insert at.
3710 * @param __str The string to insert.
3711 * @param __pos2 Start of characters in str to insert.
3712 * @param __n Number of characters to insert.
3713 * @return Reference to this string.
3714 * @throw std::length_error If new length exceeds @c max_size().
3715 * @throw std::out_of_range If @a pos1 > size() or
3716 * @a __pos2 > @a str.size().
3718 * Starting at @a pos1, insert @a __n character of @a __str
3719 * beginning with @a __pos2. If adding characters causes the
3720 * length to exceed max_size(), length_error is thrown. If @a
3721 * __pos1 is beyond the end of this string or @a __pos2 is
3722 * beyond the end of @a __str, out_of_range is thrown. The
3723 * value of the string doesn't change if an error is thrown.
3726 insert(size_type __pos1
, const basic_string
& __str
,
3727 size_type __pos2
, size_type __n
)
3728 { return this->insert(__pos1
, __str
._M_data()
3729 + __str
._M_check(__pos2
, "basic_string::insert"),
3730 __str
._M_limit(__pos2
, __n
)); }
3733 * @brief Insert a C substring.
3734 * @param __pos Iterator referencing location in string to insert at.
3735 * @param __s The C string to insert.
3736 * @param __n The number of characters to insert.
3737 * @return Reference to this string.
3738 * @throw std::length_error If new length exceeds @c max_size().
3739 * @throw std::out_of_range If @a __pos is beyond the end of this
3742 * Inserts the first @a __n characters of @a __s starting at @a
3743 * __pos. If adding characters causes the length to exceed
3744 * max_size(), length_error is thrown. If @a __pos is beyond
3745 * end(), out_of_range is thrown. The value of the string
3746 * doesn't change if an error is thrown.
3749 insert(size_type __pos
, const _CharT
* __s
, size_type __n
);
3752 * @brief Insert a C string.
3753 * @param __pos Iterator referencing location in string to insert at.
3754 * @param __s The C string to insert.
3755 * @return Reference to this string.
3756 * @throw std::length_error If new length exceeds @c max_size().
3757 * @throw std::out_of_range If @a pos is beyond the end of this
3760 * Inserts the first @a n characters of @a __s starting at @a __pos. If
3761 * adding characters causes the length to exceed max_size(),
3762 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
3763 * thrown. The value of the string doesn't change if an error is
3767 insert(size_type __pos
, const _CharT
* __s
)
3769 __glibcxx_requires_string(__s
);
3770 return this->insert(__pos
, __s
, traits_type::length(__s
));
3774 * @brief Insert multiple characters.
3775 * @param __pos Index in string to insert at.
3776 * @param __n Number of characters to insert
3777 * @param __c The character to insert.
3778 * @return Reference to this string.
3779 * @throw std::length_error If new length exceeds @c max_size().
3780 * @throw std::out_of_range If @a __pos is beyond the end of this
3783 * Inserts @a __n copies of character @a __c starting at index
3784 * @a __pos. If adding characters causes the length to exceed
3785 * max_size(), length_error is thrown. If @a __pos > length(),
3786 * out_of_range is thrown. The value of the string doesn't
3787 * change if an error is thrown.
3790 insert(size_type __pos
, size_type __n
, _CharT __c
)
3791 { return _M_replace_aux(_M_check(__pos
, "basic_string::insert"),
3792 size_type(0), __n
, __c
); }
3795 * @brief Insert one character.
3796 * @param __p Iterator referencing position in string to insert at.
3797 * @param __c The character to insert.
3798 * @return Iterator referencing newly inserted char.
3799 * @throw std::length_error If new length exceeds @c max_size().
3801 * Inserts character @a __c at position referenced by @a __p.
3802 * If adding character causes the length to exceed max_size(),
3803 * length_error is thrown. If @a __p is beyond end of string,
3804 * out_of_range is thrown. The value of the string doesn't
3805 * change if an error is thrown.
3808 insert(iterator __p
, _CharT __c
)
3810 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
3811 const size_type __pos
= __p
- _M_ibegin();
3812 _M_replace_aux(__pos
, size_type(0), size_type(1), __c
);
3813 _M_rep()->_M_set_leaked();
3814 return iterator(_M_data() + __pos
);
3818 * @brief Remove characters.
3819 * @param __pos Index of first character to remove (default 0).
3820 * @param __n Number of characters to remove (default remainder).
3821 * @return Reference to this string.
3822 * @throw std::out_of_range If @a pos is beyond the end of this
3825 * Removes @a __n characters from this string starting at @a
3826 * __pos. The length of the string is reduced by @a __n. If
3827 * there are < @a __n characters to remove, the remainder of
3828 * the string is truncated. If @a __p is beyond end of string,
3829 * out_of_range is thrown. The value of the string doesn't
3830 * change if an error is thrown.
3833 erase(size_type __pos
= 0, size_type __n
= npos
)
3835 _M_mutate(_M_check(__pos
, "basic_string::erase"),
3836 _M_limit(__pos
, __n
), size_type(0));
3841 * @brief Remove one character.
3842 * @param __position Iterator referencing the character to remove.
3843 * @return iterator referencing same location after removal.
3845 * Removes the character at @a __position from this string. The value
3846 * of the string doesn't change if an error is thrown.
3849 erase(iterator __position
)
3851 _GLIBCXX_DEBUG_PEDASSERT(__position
>= _M_ibegin()
3852 && __position
< _M_iend());
3853 const size_type __pos
= __position
- _M_ibegin();
3854 _M_mutate(__pos
, size_type(1), size_type(0));
3855 _M_rep()->_M_set_leaked();
3856 return iterator(_M_data() + __pos
);
3860 * @brief Remove a range of characters.
3861 * @param __first Iterator referencing the first character to remove.
3862 * @param __last Iterator referencing the end of the range.
3863 * @return Iterator referencing location of first after removal.
3865 * Removes the characters in the range [first,last) from this string.
3866 * The value of the string doesn't change if an error is thrown.
3869 erase(iterator __first
, iterator __last
);
3871 #if __cplusplus >= 201103L
3873 * @brief Remove the last character.
3875 * The string must be non-empty.
3878 pop_back() // FIXME C++11: should be noexcept.
3880 _GLIBCXX_DEBUG_ASSERT(!empty());
3881 erase(size() - 1, 1);
3886 * @brief Replace characters with value from another string.
3887 * @param __pos Index of first character to replace.
3888 * @param __n Number of characters to be replaced.
3889 * @param __str String to insert.
3890 * @return Reference to this string.
3891 * @throw std::out_of_range If @a pos is beyond the end of this
3893 * @throw std::length_error If new length exceeds @c max_size().
3895 * Removes the characters in the range [__pos,__pos+__n) from
3896 * this string. In place, the value of @a __str is inserted.
3897 * If @a __pos is beyond end of string, out_of_range is thrown.
3898 * If the length of the result exceeds max_size(), length_error
3899 * is thrown. The value of the string doesn't change if an
3903 replace(size_type __pos
, size_type __n
, const basic_string
& __str
)
3904 { return this->replace(__pos
, __n
, __str
._M_data(), __str
.size()); }
3907 * @brief Replace characters with value from another string.
3908 * @param __pos1 Index of first character to replace.
3909 * @param __n1 Number of characters to be replaced.
3910 * @param __str String to insert.
3911 * @param __pos2 Index of first character of str to use.
3912 * @param __n2 Number of characters from str to use.
3913 * @return Reference to this string.
3914 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
3916 * @throw std::length_error If new length exceeds @c max_size().
3918 * Removes the characters in the range [__pos1,__pos1 + n) from this
3919 * string. In place, the value of @a __str is inserted. If @a __pos is
3920 * beyond end of string, out_of_range is thrown. If the length of the
3921 * result exceeds max_size(), length_error is thrown. The value of the
3922 * string doesn't change if an error is thrown.
3925 replace(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
3926 size_type __pos2
, size_type __n2
)
3927 { return this->replace(__pos1
, __n1
, __str
._M_data()
3928 + __str
._M_check(__pos2
, "basic_string::replace"),
3929 __str
._M_limit(__pos2
, __n2
)); }
3932 * @brief Replace characters with value of a C substring.
3933 * @param __pos Index of first character to replace.
3934 * @param __n1 Number of characters to be replaced.
3935 * @param __s C string to insert.
3936 * @param __n2 Number of characters from @a s to use.
3937 * @return Reference to this string.
3938 * @throw std::out_of_range If @a pos1 > size().
3939 * @throw std::length_error If new length exceeds @c max_size().
3941 * Removes the characters in the range [__pos,__pos + __n1)
3942 * from this string. In place, the first @a __n2 characters of
3943 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
3944 * @a __pos is beyond end of string, out_of_range is thrown. If
3945 * the length of result exceeds max_size(), length_error is
3946 * thrown. The value of the string doesn't change if an error
3950 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
3954 * @brief Replace characters with value of a C string.
3955 * @param __pos Index of first character to replace.
3956 * @param __n1 Number of characters to be replaced.
3957 * @param __s C string to insert.
3958 * @return Reference to this string.
3959 * @throw std::out_of_range If @a pos > size().
3960 * @throw std::length_error If new length exceeds @c max_size().
3962 * Removes the characters in the range [__pos,__pos + __n1)
3963 * from this string. In place, the characters of @a __s are
3964 * inserted. If @a __pos is beyond end of string, out_of_range
3965 * is thrown. If the length of result exceeds max_size(),
3966 * length_error is thrown. The value of the string doesn't
3967 * change if an error is thrown.
3970 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
)
3972 __glibcxx_requires_string(__s
);
3973 return this->replace(__pos
, __n1
, __s
, traits_type::length(__s
));
3977 * @brief Replace characters with multiple characters.
3978 * @param __pos Index of first character to replace.
3979 * @param __n1 Number of characters to be replaced.
3980 * @param __n2 Number of characters to insert.
3981 * @param __c Character to insert.
3982 * @return Reference to this string.
3983 * @throw std::out_of_range If @a __pos > size().
3984 * @throw std::length_error If new length exceeds @c max_size().
3986 * Removes the characters in the range [pos,pos + n1) from this
3987 * string. In place, @a __n2 copies of @a __c are inserted.
3988 * If @a __pos is beyond end of string, out_of_range is thrown.
3989 * If the length of result exceeds max_size(), length_error is
3990 * thrown. The value of the string doesn't change if an error
3994 replace(size_type __pos
, size_type __n1
, size_type __n2
, _CharT __c
)
3995 { return _M_replace_aux(_M_check(__pos
, "basic_string::replace"),
3996 _M_limit(__pos
, __n1
), __n2
, __c
); }
3999 * @brief Replace range of characters with string.
4000 * @param __i1 Iterator referencing start of range to replace.
4001 * @param __i2 Iterator referencing end of range to replace.
4002 * @param __str String value to insert.
4003 * @return Reference to this string.
4004 * @throw std::length_error If new length exceeds @c max_size().
4006 * Removes the characters in the range [__i1,__i2). In place,
4007 * the value of @a __str is inserted. If the length of result
4008 * exceeds max_size(), length_error is thrown. The value of
4009 * the string doesn't change if an error is thrown.
4012 replace(iterator __i1
, iterator __i2
, const basic_string
& __str
)
4013 { return this->replace(__i1
, __i2
, __str
._M_data(), __str
.size()); }
4016 * @brief Replace range of characters with C substring.
4017 * @param __i1 Iterator referencing start of range to replace.
4018 * @param __i2 Iterator referencing end of range to replace.
4019 * @param __s C string value to insert.
4020 * @param __n Number of characters from s to insert.
4021 * @return Reference to this string.
4022 * @throw std::length_error If new length exceeds @c max_size().
4024 * Removes the characters in the range [__i1,__i2). In place,
4025 * the first @a __n characters of @a __s are inserted. If the
4026 * length of result exceeds max_size(), length_error is thrown.
4027 * The value of the string doesn't change if an error is
4031 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
, size_type __n
)
4033 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4034 && __i2
<= _M_iend());
4035 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
, __s
, __n
);
4039 * @brief Replace range of characters with C string.
4040 * @param __i1 Iterator referencing start of range to replace.
4041 * @param __i2 Iterator referencing end of range to replace.
4042 * @param __s C string value to insert.
4043 * @return Reference to this string.
4044 * @throw std::length_error If new length exceeds @c max_size().
4046 * Removes the characters in the range [__i1,__i2). In place,
4047 * the characters of @a __s are inserted. If the length of
4048 * result exceeds max_size(), length_error is thrown. The
4049 * value of the string doesn't change if an error is thrown.
4052 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
)
4054 __glibcxx_requires_string(__s
);
4055 return this->replace(__i1
, __i2
, __s
, traits_type::length(__s
));
4059 * @brief Replace range of characters with multiple characters
4060 * @param __i1 Iterator referencing start of range to replace.
4061 * @param __i2 Iterator referencing end of range to replace.
4062 * @param __n Number of characters to insert.
4063 * @param __c Character to insert.
4064 * @return Reference to this string.
4065 * @throw std::length_error If new length exceeds @c max_size().
4067 * Removes the characters in the range [__i1,__i2). In place,
4068 * @a __n copies of @a __c are inserted. If the length of
4069 * result exceeds max_size(), length_error is thrown. The
4070 * value of the string doesn't change if an error is thrown.
4073 replace(iterator __i1
, iterator __i2
, size_type __n
, _CharT __c
)
4075 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4076 && __i2
<= _M_iend());
4077 return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __c
);
4081 * @brief Replace range of characters with range.
4082 * @param __i1 Iterator referencing start of range to replace.
4083 * @param __i2 Iterator referencing end of range to replace.
4084 * @param __k1 Iterator referencing start of range to insert.
4085 * @param __k2 Iterator referencing end of range to insert.
4086 * @return Reference to this string.
4087 * @throw std::length_error If new length exceeds @c max_size().
4089 * Removes the characters in the range [__i1,__i2). In place,
4090 * characters in the range [__k1,__k2) are inserted. If the
4091 * length of result exceeds max_size(), length_error is thrown.
4092 * The value of the string doesn't change if an error is
4095 template<class _InputIterator
>
4097 replace(iterator __i1
, iterator __i2
,
4098 _InputIterator __k1
, _InputIterator __k2
)
4100 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4101 && __i2
<= _M_iend());
4102 __glibcxx_requires_valid_range(__k1
, __k2
);
4103 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
4104 return _M_replace_dispatch(__i1
, __i2
, __k1
, __k2
, _Integral());
4107 // Specializations for the common case of pointer and iterator:
4108 // useful to avoid the overhead of temporary buffering in _M_replace.
4110 replace(iterator __i1
, iterator __i2
, _CharT
* __k1
, _CharT
* __k2
)
4112 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4113 && __i2
<= _M_iend());
4114 __glibcxx_requires_valid_range(__k1
, __k2
);
4115 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4120 replace(iterator __i1
, iterator __i2
,
4121 const _CharT
* __k1
, const _CharT
* __k2
)
4123 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4124 && __i2
<= _M_iend());
4125 __glibcxx_requires_valid_range(__k1
, __k2
);
4126 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4131 replace(iterator __i1
, iterator __i2
, iterator __k1
, iterator __k2
)
4133 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4134 && __i2
<= _M_iend());
4135 __glibcxx_requires_valid_range(__k1
, __k2
);
4136 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4137 __k1
.base(), __k2
- __k1
);
4141 replace(iterator __i1
, iterator __i2
,
4142 const_iterator __k1
, const_iterator __k2
)
4144 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4145 && __i2
<= _M_iend());
4146 __glibcxx_requires_valid_range(__k1
, __k2
);
4147 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4148 __k1
.base(), __k2
- __k1
);
4151 #if __cplusplus >= 201103L
4153 * @brief Replace range of characters with initializer_list.
4154 * @param __i1 Iterator referencing start of range to replace.
4155 * @param __i2 Iterator referencing end of range to replace.
4156 * @param __l The initializer_list of characters to insert.
4157 * @return Reference to this string.
4158 * @throw std::length_error If new length exceeds @c max_size().
4160 * Removes the characters in the range [__i1,__i2). In place,
4161 * characters in the range [__k1,__k2) are inserted. If the
4162 * length of result exceeds max_size(), length_error is thrown.
4163 * The value of the string doesn't change if an error is
4166 basic_string
& replace(iterator __i1
, iterator __i2
,
4167 initializer_list
<_CharT
> __l
)
4168 { return this->replace(__i1
, __i2
, __l
.begin(), __l
.end()); }
4172 template<class _Integer
>
4174 _M_replace_dispatch(iterator __i1
, iterator __i2
, _Integer __n
,
4175 _Integer __val
, __true_type
)
4176 { return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __val
); }
4178 template<class _InputIterator
>
4180 _M_replace_dispatch(iterator __i1
, iterator __i2
, _InputIterator __k1
,
4181 _InputIterator __k2
, __false_type
);
4184 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
4188 _M_replace_safe(size_type __pos1
, size_type __n1
, const _CharT
* __s
,
4191 // _S_construct_aux is used to implement the 21.3.1 para 15 which
4192 // requires special behaviour if _InIter is an integral type
4193 template<class _InIterator
>
4195 _S_construct_aux(_InIterator __beg
, _InIterator __end
,
4196 const _Alloc
& __a
, __false_type
)
4198 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
4199 return _S_construct(__beg
, __end
, __a
, _Tag());
4202 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4203 // 438. Ambiguity in the "do the right thing" clause
4204 template<class _Integer
>
4206 _S_construct_aux(_Integer __beg
, _Integer __end
,
4207 const _Alloc
& __a
, __true_type
)
4208 { return _S_construct_aux_2(static_cast<size_type
>(__beg
),
4212 _S_construct_aux_2(size_type __req
, _CharT __c
, const _Alloc
& __a
)
4213 { return _S_construct(__req
, __c
, __a
); }
4215 template<class _InIterator
>
4217 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
)
4219 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
4220 return _S_construct_aux(__beg
, __end
, __a
, _Integral());
4223 // For Input Iterators, used in istreambuf_iterators, etc.
4224 template<class _InIterator
>
4226 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
,
4227 input_iterator_tag
);
4229 // For forward_iterators up to random_access_iterators, used for
4230 // string::iterator, _CharT*, etc.
4231 template<class _FwdIterator
>
4233 _S_construct(_FwdIterator __beg
, _FwdIterator __end
, const _Alloc
& __a
,
4234 forward_iterator_tag
);
4237 _S_construct(size_type __req
, _CharT __c
, const _Alloc
& __a
);
4242 * @brief Copy substring into C string.
4243 * @param __s C string to copy value into.
4244 * @param __n Number of characters to copy.
4245 * @param __pos Index of first character to copy.
4246 * @return Number of characters actually copied
4247 * @throw std::out_of_range If __pos > size().
4249 * Copies up to @a __n characters starting at @a __pos into the
4250 * C string @a __s. If @a __pos is %greater than size(),
4251 * out_of_range is thrown.
4254 copy(_CharT
* __s
, size_type __n
, size_type __pos
= 0) const;
4257 * @brief Swap contents with another string.
4258 * @param __s String to swap with.
4260 * Exchanges the contents of this string with that of @a __s in constant
4263 // PR 58265, this should be noexcept.
4265 swap(basic_string
& __s
);
4267 // String operations:
4269 * @brief Return const pointer to null-terminated contents.
4271 * This is a handle to internal data. Do not modify or dire things may
4275 c_str() const _GLIBCXX_NOEXCEPT
4276 { return _M_data(); }
4279 * @brief Return const pointer to contents.
4281 * This is a handle to internal data. Do not modify or dire things may
4285 data() const _GLIBCXX_NOEXCEPT
4286 { return _M_data(); }
4289 * @brief Return copy of allocator used to construct this string.
4292 get_allocator() const _GLIBCXX_NOEXCEPT
4293 { return _M_dataplus
; }
4296 * @brief Find position of a C substring.
4297 * @param __s C string to locate.
4298 * @param __pos Index of character to search from.
4299 * @param __n Number of characters from @a s to search for.
4300 * @return Index of start of first occurrence.
4302 * Starting from @a __pos, searches forward for the first @a
4303 * __n characters in @a __s within this string. If found,
4304 * returns the index where it begins. If not found, returns
4308 find(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
4311 * @brief Find position of a string.
4312 * @param __str String to locate.
4313 * @param __pos Index of character to search from (default 0).
4314 * @return Index of start of first occurrence.
4316 * Starting from @a __pos, searches forward for value of @a __str within
4317 * this string. If found, returns the index where it begins. If not
4318 * found, returns npos.
4321 find(const basic_string
& __str
, size_type __pos
= 0) const
4323 { return this->find(__str
.data(), __pos
, __str
.size()); }
4326 * @brief Find position of a C string.
4327 * @param __s C string to locate.
4328 * @param __pos Index of character to search from (default 0).
4329 * @return Index of start of first occurrence.
4331 * Starting from @a __pos, searches forward for the value of @a
4332 * __s within this string. If found, returns the index where
4333 * it begins. If not found, returns npos.
4336 find(const _CharT
* __s
, size_type __pos
= 0) const
4338 __glibcxx_requires_string(__s
);
4339 return this->find(__s
, __pos
, traits_type::length(__s
));
4343 * @brief Find position of a character.
4344 * @param __c Character to locate.
4345 * @param __pos Index of character to search from (default 0).
4346 * @return Index of first occurrence.
4348 * Starting from @a __pos, searches forward for @a __c within
4349 * this string. If found, returns the index where it was
4350 * found. If not found, returns npos.
4353 find(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
;
4356 * @brief Find last position of a string.
4357 * @param __str String to locate.
4358 * @param __pos Index of character to search back from (default end).
4359 * @return Index of start of last occurrence.
4361 * Starting from @a __pos, searches backward for value of @a
4362 * __str within this string. If found, returns the index where
4363 * it begins. If not found, returns npos.
4366 rfind(const basic_string
& __str
, size_type __pos
= npos
) const
4368 { return this->rfind(__str
.data(), __pos
, __str
.size()); }
4371 * @brief Find last position of a C substring.
4372 * @param __s C string to locate.
4373 * @param __pos Index of character to search back from.
4374 * @param __n Number of characters from s to search for.
4375 * @return Index of start of last occurrence.
4377 * Starting from @a __pos, searches backward for the first @a
4378 * __n characters in @a __s within this string. If found,
4379 * returns the index where it begins. If not found, returns
4383 rfind(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
4386 * @brief Find last position of a C string.
4387 * @param __s C string to locate.
4388 * @param __pos Index of character to start search at (default end).
4389 * @return Index of start of last occurrence.
4391 * Starting from @a __pos, searches backward for the value of
4392 * @a __s within this string. If found, returns the index
4393 * where it begins. If not found, returns npos.
4396 rfind(const _CharT
* __s
, size_type __pos
= npos
) const
4398 __glibcxx_requires_string(__s
);
4399 return this->rfind(__s
, __pos
, traits_type::length(__s
));
4403 * @brief Find last position of a character.
4404 * @param __c Character to locate.
4405 * @param __pos Index of character to search back from (default end).
4406 * @return Index of last occurrence.
4408 * Starting from @a __pos, searches backward for @a __c within
4409 * this string. If found, returns the index where it was
4410 * found. If not found, returns npos.
4413 rfind(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
;
4416 * @brief Find position of a character of string.
4417 * @param __str String containing characters to locate.
4418 * @param __pos Index of character to search from (default 0).
4419 * @return Index of first occurrence.
4421 * Starting from @a __pos, searches forward for one of the
4422 * characters of @a __str within this string. If found,
4423 * returns the index where it was found. If not found, returns
4427 find_first_of(const basic_string
& __str
, size_type __pos
= 0) const
4429 { return this->find_first_of(__str
.data(), __pos
, __str
.size()); }
4432 * @brief Find position of a character of C substring.
4433 * @param __s String containing characters to locate.
4434 * @param __pos Index of character to search from.
4435 * @param __n Number of characters from s to search for.
4436 * @return Index of first occurrence.
4438 * Starting from @a __pos, searches forward for one of the
4439 * first @a __n characters of @a __s within this string. If
4440 * found, returns the index where it was found. If not found,
4444 find_first_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
4447 * @brief Find position of a character of C string.
4448 * @param __s String containing characters to locate.
4449 * @param __pos Index of character to search from (default 0).
4450 * @return Index of first occurrence.
4452 * Starting from @a __pos, searches forward for one of the
4453 * characters of @a __s within this string. If found, returns
4454 * the index where it was found. If not found, returns npos.
4457 find_first_of(const _CharT
* __s
, size_type __pos
= 0) const
4459 __glibcxx_requires_string(__s
);
4460 return this->find_first_of(__s
, __pos
, traits_type::length(__s
));
4464 * @brief Find position of a character.
4465 * @param __c Character to locate.
4466 * @param __pos Index of character to search from (default 0).
4467 * @return Index of first occurrence.
4469 * Starting from @a __pos, searches forward for the character
4470 * @a __c within this string. If found, returns the index
4471 * where it was found. If not found, returns npos.
4473 * Note: equivalent to find(__c, __pos).
4476 find_first_of(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
4477 { return this->find(__c
, __pos
); }
4480 * @brief Find last position of a character of string.
4481 * @param __str String containing characters to locate.
4482 * @param __pos Index of character to search back from (default end).
4483 * @return Index of last occurrence.
4485 * Starting from @a __pos, searches backward for one of the
4486 * characters of @a __str within this string. If found,
4487 * returns the index where it was found. If not found, returns
4491 find_last_of(const basic_string
& __str
, size_type __pos
= npos
) const
4493 { return this->find_last_of(__str
.data(), __pos
, __str
.size()); }
4496 * @brief Find last position of a character of C substring.
4497 * @param __s C string containing characters to locate.
4498 * @param __pos Index of character to search back from.
4499 * @param __n Number of characters from s to search for.
4500 * @return Index of last occurrence.
4502 * Starting from @a __pos, searches backward for one of the
4503 * first @a __n characters of @a __s within this string. If
4504 * found, returns the index where it was found. If not found,
4508 find_last_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
4511 * @brief Find last position of a character of C string.
4512 * @param __s C string containing characters to locate.
4513 * @param __pos Index of character to search back from (default end).
4514 * @return Index of last occurrence.
4516 * Starting from @a __pos, searches backward for one of the
4517 * characters of @a __s within this string. If found, returns
4518 * the index where it was found. If not found, returns npos.
4521 find_last_of(const _CharT
* __s
, size_type __pos
= npos
) const
4523 __glibcxx_requires_string(__s
);
4524 return this->find_last_of(__s
, __pos
, traits_type::length(__s
));
4528 * @brief Find last position of a character.
4529 * @param __c Character to locate.
4530 * @param __pos Index of character to search back from (default end).
4531 * @return Index of last occurrence.
4533 * Starting from @a __pos, searches backward for @a __c within
4534 * this string. If found, returns the index where it was
4535 * found. If not found, returns npos.
4537 * Note: equivalent to rfind(__c, __pos).
4540 find_last_of(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
4541 { return this->rfind(__c
, __pos
); }
4544 * @brief Find position of a character not in string.
4545 * @param __str String containing characters to avoid.
4546 * @param __pos Index of character to search from (default 0).
4547 * @return Index of first occurrence.
4549 * Starting from @a __pos, searches forward for a character not contained
4550 * in @a __str within this string. If found, returns the index where it
4551 * was found. If not found, returns npos.
4554 find_first_not_of(const basic_string
& __str
, size_type __pos
= 0) const
4556 { return this->find_first_not_of(__str
.data(), __pos
, __str
.size()); }
4559 * @brief Find position of a character not in C substring.
4560 * @param __s C string containing characters to avoid.
4561 * @param __pos Index of character to search from.
4562 * @param __n Number of characters from __s to consider.
4563 * @return Index of first occurrence.
4565 * Starting from @a __pos, searches forward for a character not
4566 * contained in the first @a __n characters of @a __s within
4567 * this string. If found, returns the index where it was
4568 * found. If not found, returns npos.
4571 find_first_not_of(const _CharT
* __s
, size_type __pos
,
4572 size_type __n
) const;
4575 * @brief Find position of a character not in C string.
4576 * @param __s C string containing characters to avoid.
4577 * @param __pos Index of character to search from (default 0).
4578 * @return Index of first occurrence.
4580 * Starting from @a __pos, searches forward for a character not
4581 * contained in @a __s within this string. If found, returns
4582 * the index where it was found. If not found, returns npos.
4585 find_first_not_of(const _CharT
* __s
, size_type __pos
= 0) const
4587 __glibcxx_requires_string(__s
);
4588 return this->find_first_not_of(__s
, __pos
, traits_type::length(__s
));
4592 * @brief Find position of a different character.
4593 * @param __c Character to avoid.
4594 * @param __pos Index of character to search from (default 0).
4595 * @return Index of first occurrence.
4597 * Starting from @a __pos, searches forward for a character
4598 * other than @a __c within this string. If found, returns the
4599 * index where it was found. If not found, returns npos.
4602 find_first_not_of(_CharT __c
, size_type __pos
= 0) const
4606 * @brief Find last position of a character not in string.
4607 * @param __str String containing characters to avoid.
4608 * @param __pos Index of character to search back from (default end).
4609 * @return Index of last occurrence.
4611 * Starting from @a __pos, searches backward for a character
4612 * not contained in @a __str within this string. If found,
4613 * returns the index where it was found. If not found, returns
4617 find_last_not_of(const basic_string
& __str
, size_type __pos
= npos
) const
4619 { return this->find_last_not_of(__str
.data(), __pos
, __str
.size()); }
4622 * @brief Find last position of a character not in C substring.
4623 * @param __s C string containing characters to avoid.
4624 * @param __pos Index of character to search back from.
4625 * @param __n Number of characters from s to consider.
4626 * @return Index of last occurrence.
4628 * Starting from @a __pos, searches backward for a character not
4629 * contained in the first @a __n characters of @a __s within this string.
4630 * If found, returns the index where it was found. If not found,
4634 find_last_not_of(const _CharT
* __s
, size_type __pos
,
4635 size_type __n
) const;
4637 * @brief Find last position of a character not in C string.
4638 * @param __s C string containing characters to avoid.
4639 * @param __pos Index of character to search back from (default end).
4640 * @return Index of last occurrence.
4642 * Starting from @a __pos, searches backward for a character
4643 * not contained in @a __s within this string. If found,
4644 * returns the index where it was found. If not found, returns
4648 find_last_not_of(const _CharT
* __s
, size_type __pos
= npos
) const
4650 __glibcxx_requires_string(__s
);
4651 return this->find_last_not_of(__s
, __pos
, traits_type::length(__s
));
4655 * @brief Find last position of a different character.
4656 * @param __c Character to avoid.
4657 * @param __pos Index of character to search back from (default end).
4658 * @return Index of last occurrence.
4660 * Starting from @a __pos, searches backward for a character other than
4661 * @a __c within this string. If found, returns the index where it was
4662 * found. If not found, returns npos.
4665 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const
4669 * @brief Get a substring.
4670 * @param __pos Index of first character (default 0).
4671 * @param __n Number of characters in substring (default remainder).
4672 * @return The new string.
4673 * @throw std::out_of_range If __pos > size().
4675 * Construct and return a new string using the @a __n
4676 * characters starting at @a __pos. If the string is too
4677 * short, use the remainder of the characters. If @a __pos is
4678 * beyond the end of the string, out_of_range is thrown.
4681 substr(size_type __pos
= 0, size_type __n
= npos
) const
4682 { return basic_string(*this,
4683 _M_check(__pos
, "basic_string::substr"), __n
); }
4686 * @brief Compare to a string.
4687 * @param __str String to compare against.
4688 * @return Integer < 0, 0, or > 0.
4690 * Returns an integer < 0 if this string is ordered before @a
4691 * __str, 0 if their values are equivalent, or > 0 if this
4692 * string is ordered after @a __str. Determines the effective
4693 * length rlen of the strings to compare as the smallest of
4694 * size() and str.size(). The function then compares the two
4695 * strings by calling traits::compare(data(), str.data(),rlen).
4696 * If the result of the comparison is nonzero returns it,
4697 * otherwise the shorter one is ordered first.
4700 compare(const basic_string
& __str
) const
4702 const size_type __size
= this->size();
4703 const size_type __osize
= __str
.size();
4704 const size_type __len
= std::min(__size
, __osize
);
4706 int __r
= traits_type::compare(_M_data(), __str
.data(), __len
);
4708 __r
= _S_compare(__size
, __osize
);
4713 * @brief Compare substring to a string.
4714 * @param __pos Index of first character of substring.
4715 * @param __n Number of characters in substring.
4716 * @param __str String to compare against.
4717 * @return Integer < 0, 0, or > 0.
4719 * Form the substring of this string from the @a __n characters
4720 * starting at @a __pos. Returns an integer < 0 if the
4721 * substring is ordered before @a __str, 0 if their values are
4722 * equivalent, or > 0 if the substring is ordered after @a
4723 * __str. Determines the effective length rlen of the strings
4724 * to compare as the smallest of the length of the substring
4725 * and @a __str.size(). The function then compares the two
4726 * strings by calling
4727 * traits::compare(substring.data(),str.data(),rlen). If the
4728 * result of the comparison is nonzero returns it, otherwise
4729 * the shorter one is ordered first.
4732 compare(size_type __pos
, size_type __n
, const basic_string
& __str
) const;
4735 * @brief Compare substring to a substring.
4736 * @param __pos1 Index of first character of substring.
4737 * @param __n1 Number of characters in substring.
4738 * @param __str String to compare against.
4739 * @param __pos2 Index of first character of substring of str.
4740 * @param __n2 Number of characters in substring of str.
4741 * @return Integer < 0, 0, or > 0.
4743 * Form the substring of this string from the @a __n1
4744 * characters starting at @a __pos1. Form the substring of @a
4745 * __str from the @a __n2 characters starting at @a __pos2.
4746 * Returns an integer < 0 if this substring is ordered before
4747 * the substring of @a __str, 0 if their values are equivalent,
4748 * or > 0 if this substring is ordered after the substring of
4749 * @a __str. Determines the effective length rlen of the
4750 * strings to compare as the smallest of the lengths of the
4751 * substrings. The function then compares the two strings by
4753 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
4754 * If the result of the comparison is nonzero returns it,
4755 * otherwise the shorter one is ordered first.
4758 compare(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
4759 size_type __pos2
, size_type __n2
) const;
4762 * @brief Compare to a C string.
4763 * @param __s C string to compare against.
4764 * @return Integer < 0, 0, or > 0.
4766 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
4767 * their values are equivalent, or > 0 if this string is ordered after
4768 * @a __s. Determines the effective length rlen of the strings to
4769 * compare as the smallest of size() and the length of a string
4770 * constructed from @a __s. The function then compares the two strings
4771 * by calling traits::compare(data(),s,rlen). If the result of the
4772 * comparison is nonzero returns it, otherwise the shorter one is
4776 compare(const _CharT
* __s
) const;
4778 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4779 // 5 String::compare specification questionable
4781 * @brief Compare substring to a C string.
4782 * @param __pos Index of first character of substring.
4783 * @param __n1 Number of characters in substring.
4784 * @param __s C string to compare against.
4785 * @return Integer < 0, 0, or > 0.
4787 * Form the substring of this string from the @a __n1
4788 * characters starting at @a pos. Returns an integer < 0 if
4789 * the substring is ordered before @a __s, 0 if their values
4790 * are equivalent, or > 0 if the substring is ordered after @a
4791 * __s. Determines the effective length rlen of the strings to
4792 * compare as the smallest of the length of the substring and
4793 * the length of a string constructed from @a __s. The
4794 * function then compares the two string by calling
4795 * traits::compare(substring.data(),__s,rlen). If the result of
4796 * the comparison is nonzero returns it, otherwise the shorter
4797 * one is ordered first.
4800 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
) const;
4803 * @brief Compare substring against a character %array.
4804 * @param __pos Index of first character of substring.
4805 * @param __n1 Number of characters in substring.
4806 * @param __s character %array to compare against.
4807 * @param __n2 Number of characters of s.
4808 * @return Integer < 0, 0, or > 0.
4810 * Form the substring of this string from the @a __n1
4811 * characters starting at @a __pos. Form a string from the
4812 * first @a __n2 characters of @a __s. Returns an integer < 0
4813 * if this substring is ordered before the string from @a __s,
4814 * 0 if their values are equivalent, or > 0 if this substring
4815 * is ordered after the string from @a __s. Determines the
4816 * effective length rlen of the strings to compare as the
4817 * smallest of the length of the substring and @a __n2. The
4818 * function then compares the two strings by calling
4819 * traits::compare(substring.data(),s,rlen). If the result of
4820 * the comparison is nonzero returns it, otherwise the shorter
4821 * one is ordered first.
4823 * NB: s must have at least n2 characters, '\\0' has
4824 * no special meaning.
4827 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
,
4828 size_type __n2
) const;
4830 #endif // !_GLIBCXX_USE_CXX11_ABI
4834 * @brief Concatenate two strings.
4835 * @param __lhs First string.
4836 * @param __rhs Last string.
4837 * @return New string with value of @a __lhs followed by @a __rhs.
4839 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4840 basic_string
<_CharT
, _Traits
, _Alloc
>
4841 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
4842 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
4844 basic_string
<_CharT
, _Traits
, _Alloc
> __str(__lhs
);
4845 __str
.append(__rhs
);
4850 * @brief Concatenate C string and string.
4851 * @param __lhs First string.
4852 * @param __rhs Last string.
4853 * @return New string with value of @a __lhs followed by @a __rhs.
4855 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4856 basic_string
<_CharT
,_Traits
,_Alloc
>
4857 operator+(const _CharT
* __lhs
,
4858 const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
);
4861 * @brief Concatenate character and string.
4862 * @param __lhs First string.
4863 * @param __rhs Last string.
4864 * @return New string with @a __lhs followed by @a __rhs.
4866 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4867 basic_string
<_CharT
,_Traits
,_Alloc
>
4868 operator+(_CharT __lhs
, const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
);
4871 * @brief Concatenate string and C string.
4872 * @param __lhs First string.
4873 * @param __rhs Last string.
4874 * @return New string with @a __lhs followed by @a __rhs.
4876 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4877 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4878 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
4879 const _CharT
* __rhs
)
4881 basic_string
<_CharT
, _Traits
, _Alloc
> __str(__lhs
);
4882 __str
.append(__rhs
);
4887 * @brief Concatenate string and character.
4888 * @param __lhs First string.
4889 * @param __rhs Last string.
4890 * @return New string with @a __lhs followed by @a __rhs.
4892 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4893 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4894 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
, _CharT __rhs
)
4896 typedef basic_string
<_CharT
, _Traits
, _Alloc
> __string_type
;
4897 typedef typename
__string_type::size_type __size_type
;
4898 __string_type
__str(__lhs
);
4899 __str
.append(__size_type(1), __rhs
);
4903 #if __cplusplus >= 201103L
4904 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4905 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4906 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
4907 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
4908 { return std::move(__lhs
.append(__rhs
)); }
4910 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4911 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4912 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
4913 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
4914 { return std::move(__rhs
.insert(0, __lhs
)); }
4916 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4917 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4918 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
4919 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
4921 const auto __size
= __lhs
.size() + __rhs
.size();
4922 const bool __cond
= (__size
> __lhs
.capacity()
4923 && __size
<= __rhs
.capacity());
4924 return __cond
? std::move(__rhs
.insert(0, __lhs
))
4925 : std::move(__lhs
.append(__rhs
));
4928 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4929 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4930 operator+(const _CharT
* __lhs
,
4931 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
4932 { return std::move(__rhs
.insert(0, __lhs
)); }
4934 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4935 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4936 operator+(_CharT __lhs
,
4937 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
4938 { return std::move(__rhs
.insert(0, 1, __lhs
)); }
4940 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4941 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4942 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
4943 const _CharT
* __rhs
)
4944 { return std::move(__lhs
.append(__rhs
)); }
4946 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4947 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4948 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
4950 { return std::move(__lhs
.append(1, __rhs
)); }
4955 * @brief Test equivalence of two strings.
4956 * @param __lhs First string.
4957 * @param __rhs Second string.
4958 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
4960 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4962 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
4963 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
4964 { return __lhs
.compare(__rhs
) == 0; }
4966 template<typename _CharT
>
4968 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
, bool>::__type
4969 operator==(const basic_string
<_CharT
>& __lhs
,
4970 const basic_string
<_CharT
>& __rhs
)
4971 { return (__lhs
.size() == __rhs
.size()
4972 && !std::char_traits
<_CharT
>::compare(__lhs
.data(), __rhs
.data(),
4976 * @brief Test equivalence of C string and string.
4977 * @param __lhs C string.
4978 * @param __rhs String.
4979 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
4981 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4983 operator==(const _CharT
* __lhs
,
4984 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
4985 { return __rhs
.compare(__lhs
) == 0; }
4988 * @brief Test equivalence of string and C string.
4989 * @param __lhs String.
4990 * @param __rhs C string.
4991 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
4993 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4995 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
4996 const _CharT
* __rhs
)
4997 { return __lhs
.compare(__rhs
) == 0; }
5001 * @brief Test difference of two strings.
5002 * @param __lhs First string.
5003 * @param __rhs Second string.
5004 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
5006 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5008 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5009 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5010 { return !(__lhs
== __rhs
); }
5013 * @brief Test difference of C string and string.
5014 * @param __lhs C string.
5015 * @param __rhs String.
5016 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
5018 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5020 operator!=(const _CharT
* __lhs
,
5021 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5022 { return !(__lhs
== __rhs
); }
5025 * @brief Test difference of string and C string.
5026 * @param __lhs String.
5027 * @param __rhs C string.
5028 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
5030 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5032 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5033 const _CharT
* __rhs
)
5034 { return !(__lhs
== __rhs
); }
5038 * @brief Test if string precedes string.
5039 * @param __lhs First string.
5040 * @param __rhs Second string.
5041 * @return True if @a __lhs precedes @a __rhs. False otherwise.
5043 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5045 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5046 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5047 { return __lhs
.compare(__rhs
) < 0; }
5050 * @brief Test if string precedes C string.
5051 * @param __lhs String.
5052 * @param __rhs C string.
5053 * @return True if @a __lhs precedes @a __rhs. False otherwise.
5055 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5057 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5058 const _CharT
* __rhs
)
5059 { return __lhs
.compare(__rhs
) < 0; }
5062 * @brief Test if C string precedes string.
5063 * @param __lhs C string.
5064 * @param __rhs String.
5065 * @return True if @a __lhs precedes @a __rhs. False otherwise.
5067 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5069 operator<(const _CharT
* __lhs
,
5070 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5071 { return __rhs
.compare(__lhs
) > 0; }
5075 * @brief Test if string follows string.
5076 * @param __lhs First string.
5077 * @param __rhs Second string.
5078 * @return True if @a __lhs follows @a __rhs. False otherwise.
5080 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5082 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5083 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5084 { return __lhs
.compare(__rhs
) > 0; }
5087 * @brief Test if string follows C string.
5088 * @param __lhs String.
5089 * @param __rhs C string.
5090 * @return True if @a __lhs follows @a __rhs. False otherwise.
5092 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5094 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5095 const _CharT
* __rhs
)
5096 { return __lhs
.compare(__rhs
) > 0; }
5099 * @brief Test if C string follows string.
5100 * @param __lhs C string.
5101 * @param __rhs String.
5102 * @return True if @a __lhs follows @a __rhs. False otherwise.
5104 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5106 operator>(const _CharT
* __lhs
,
5107 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5108 { return __rhs
.compare(__lhs
) < 0; }
5112 * @brief Test if string doesn't follow string.
5113 * @param __lhs First string.
5114 * @param __rhs Second string.
5115 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5117 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5119 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5120 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5121 { return __lhs
.compare(__rhs
) <= 0; }
5124 * @brief Test if string doesn't follow C string.
5125 * @param __lhs String.
5126 * @param __rhs C string.
5127 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5129 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5131 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5132 const _CharT
* __rhs
)
5133 { return __lhs
.compare(__rhs
) <= 0; }
5136 * @brief Test if C string doesn't follow string.
5137 * @param __lhs C string.
5138 * @param __rhs String.
5139 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5141 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5143 operator<=(const _CharT
* __lhs
,
5144 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5145 { return __rhs
.compare(__lhs
) >= 0; }
5149 * @brief Test if string doesn't precede string.
5150 * @param __lhs First string.
5151 * @param __rhs Second string.
5152 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
5154 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5156 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5157 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5158 { return __lhs
.compare(__rhs
) >= 0; }
5161 * @brief Test if string doesn't precede C string.
5162 * @param __lhs String.
5163 * @param __rhs C string.
5164 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
5166 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5168 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5169 const _CharT
* __rhs
)
5170 { return __lhs
.compare(__rhs
) >= 0; }
5173 * @brief Test if C string doesn't precede string.
5174 * @param __lhs C string.
5175 * @param __rhs String.
5176 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
5178 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5180 operator>=(const _CharT
* __lhs
,
5181 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5182 { return __rhs
.compare(__lhs
) <= 0; }
5185 * @brief Swap contents of two strings.
5186 * @param __lhs First string.
5187 * @param __rhs Second string.
5189 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
5191 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5193 swap(basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5194 basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5195 { __lhs
.swap(__rhs
); }
5199 * @brief Read stream into a string.
5200 * @param __is Input stream.
5201 * @param __str Buffer to store into.
5202 * @return Reference to the input stream.
5204 * Stores characters from @a __is into @a __str until whitespace is
5205 * found, the end of the stream is encountered, or str.max_size()
5206 * is reached. If is.width() is non-zero, that is the limit on the
5207 * number of characters stored into @a __str. Any previous
5208 * contents of @a __str are erased.
5210 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5211 basic_istream
<_CharT
, _Traits
>&
5212 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
5213 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
);
5216 basic_istream
<char>&
5217 operator>>(basic_istream
<char>& __is
, basic_string
<char>& __str
);
5220 * @brief Write string to a stream.
5221 * @param __os Output stream.
5222 * @param __str String to write out.
5223 * @return Reference to the output stream.
5225 * Output characters of @a __str into os following the same rules as for
5226 * writing a C string.
5228 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5229 inline basic_ostream
<_CharT
, _Traits
>&
5230 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
5231 const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
5233 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5234 // 586. string inserter not a formatted function
5235 return __ostream_insert(__os
, __str
.data(), __str
.size());
5239 * @brief Read a line from stream into a string.
5240 * @param __is Input stream.
5241 * @param __str Buffer to store into.
5242 * @param __delim Character marking end of line.
5243 * @return Reference to the input stream.
5245 * Stores characters from @a __is into @a __str until @a __delim is
5246 * found, the end of the stream is encountered, or str.max_size()
5247 * is reached. Any previous contents of @a __str are erased. If
5248 * @a __delim is encountered, it is extracted but not stored into
5251 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5252 basic_istream
<_CharT
, _Traits
>&
5253 getline(basic_istream
<_CharT
, _Traits
>& __is
,
5254 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
);
5257 * @brief Read a line from stream into a string.
5258 * @param __is Input stream.
5259 * @param __str Buffer to store into.
5260 * @return Reference to the input stream.
5262 * Stores characters from is into @a __str until '\n' is
5263 * found, the end of the stream is encountered, or str.max_size()
5264 * is reached. Any previous contents of @a __str are erased. If
5265 * end of line is encountered, it is extracted but not stored into
5268 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5269 inline basic_istream
<_CharT
, _Traits
>&
5270 getline(basic_istream
<_CharT
, _Traits
>& __is
,
5271 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
5272 { return std::getline(__is
, __str
, __is
.widen('\n')); }
5274 #if __cplusplus >= 201103L
5275 /// Read a line from an rvalue stream into a string.
5276 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5277 inline basic_istream
<_CharT
, _Traits
>&
5278 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
5279 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
)
5280 { return std::getline(__is
, __str
, __delim
); }
5282 /// Read a line from an rvalue stream into a string.
5283 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5284 inline basic_istream
<_CharT
, _Traits
>&
5285 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
5286 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
5287 { return std::getline(__is
, __str
); }
5291 basic_istream
<char>&
5292 getline(basic_istream
<char>& __in
, basic_string
<char>& __str
,
5295 #ifdef _GLIBCXX_USE_WCHAR_T
5297 basic_istream
<wchar_t>&
5298 getline(basic_istream
<wchar_t>& __in
, basic_string
<wchar_t>& __str
,
5302 _GLIBCXX_END_NAMESPACE_VERSION
5305 #if __cplusplus >= 201103L && defined(_GLIBCXX_USE_C99)
5307 #include <ext/string_conversions.h>
5309 namespace std
_GLIBCXX_VISIBILITY(default)
5311 _GLIBCXX_BEGIN_NAMESPACE_VERSION
5312 _GLIBCXX_BEGIN_NAMESPACE_CXX11
5314 // 21.4 Numeric Conversions [string.conversions].
5316 stoi(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
5317 { return __gnu_cxx::__stoa
<long, int>(&std::strtol
, "stoi", __str
.c_str(),
5321 stol(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
5322 { return __gnu_cxx::__stoa(&std::strtol
, "stol", __str
.c_str(),
5325 inline unsigned long
5326 stoul(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
5327 { return __gnu_cxx::__stoa(&std::strtoul
, "stoul", __str
.c_str(),
5331 stoll(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
5332 { return __gnu_cxx::__stoa(&std::strtoll
, "stoll", __str
.c_str(),
5335 inline unsigned long long
5336 stoull(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
5337 { return __gnu_cxx::__stoa(&std::strtoull
, "stoull", __str
.c_str(),
5340 // NB: strtof vs strtod.
5342 stof(const string
& __str
, size_t* __idx
= 0)
5343 { return __gnu_cxx::__stoa(&std::strtof
, "stof", __str
.c_str(), __idx
); }
5346 stod(const string
& __str
, size_t* __idx
= 0)
5347 { return __gnu_cxx::__stoa(&std::strtod
, "stod", __str
.c_str(), __idx
); }
5350 stold(const string
& __str
, size_t* __idx
= 0)
5351 { return __gnu_cxx::__stoa(&std::strtold
, "stold", __str
.c_str(), __idx
); }
5353 // NB: (v)snprintf vs sprintf.
5357 to_string(int __val
)
5358 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, 4 * sizeof(int),
5362 to_string(unsigned __val
)
5363 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
5364 4 * sizeof(unsigned),
5368 to_string(long __val
)
5369 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, 4 * sizeof(long),
5373 to_string(unsigned long __val
)
5374 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
5375 4 * sizeof(unsigned long),
5379 to_string(long long __val
)
5380 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
5381 4 * sizeof(long long),
5385 to_string(unsigned long long __val
)
5386 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
5387 4 * sizeof(unsigned long long),
5391 to_string(float __val
)
5394 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
5395 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
5400 to_string(double __val
)
5403 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
5404 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
5409 to_string(long double __val
)
5412 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
5413 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
5417 #ifdef _GLIBCXX_USE_WCHAR_T
5419 stoi(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
5420 { return __gnu_cxx::__stoa
<long, int>(&std::wcstol
, "stoi", __str
.c_str(),
5424 stol(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
5425 { return __gnu_cxx::__stoa(&std::wcstol
, "stol", __str
.c_str(),
5428 inline unsigned long
5429 stoul(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
5430 { return __gnu_cxx::__stoa(&std::wcstoul
, "stoul", __str
.c_str(),
5434 stoll(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
5435 { return __gnu_cxx::__stoa(&std::wcstoll
, "stoll", __str
.c_str(),
5438 inline unsigned long long
5439 stoull(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
5440 { return __gnu_cxx::__stoa(&std::wcstoull
, "stoull", __str
.c_str(),
5443 // NB: wcstof vs wcstod.
5445 stof(const wstring
& __str
, size_t* __idx
= 0)
5446 { return __gnu_cxx::__stoa(&std::wcstof
, "stof", __str
.c_str(), __idx
); }
5449 stod(const wstring
& __str
, size_t* __idx
= 0)
5450 { return __gnu_cxx::__stoa(&std::wcstod
, "stod", __str
.c_str(), __idx
); }
5453 stold(const wstring
& __str
, size_t* __idx
= 0)
5454 { return __gnu_cxx::__stoa(&std::wcstold
, "stold", __str
.c_str(), __idx
); }
5456 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
5459 to_wstring(int __val
)
5460 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(int),
5464 to_wstring(unsigned __val
)
5465 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
5466 4 * sizeof(unsigned),
5470 to_wstring(long __val
)
5471 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(long),
5475 to_wstring(unsigned long __val
)
5476 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
5477 4 * sizeof(unsigned long),
5481 to_wstring(long long __val
)
5482 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
5483 4 * sizeof(long long),
5487 to_wstring(unsigned long long __val
)
5488 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
5489 4 * sizeof(unsigned long long),
5493 to_wstring(float __val
)
5496 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
5497 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
5502 to_wstring(double __val
)
5505 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
5506 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
5511 to_wstring(long double __val
)
5514 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
5515 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
5518 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
5521 _GLIBCXX_END_NAMESPACE_CXX11
5522 _GLIBCXX_END_NAMESPACE_VERSION
5525 #endif /* C++11 && _GLIBCXX_USE_C99 ... */
5527 #if __cplusplus >= 201103L
5529 #include <bits/functional_hash.h>
5531 namespace std
_GLIBCXX_VISIBILITY(default)
5533 _GLIBCXX_BEGIN_NAMESPACE_VERSION
5537 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
5538 /// std::hash specialization for string.
5541 : public __hash_base
<size_t, string
>
5544 operator()(const string
& __s
) const noexcept
5545 { return std::_Hash_impl::hash(__s
.data(), __s
.length()); }
5549 struct __is_fast_hash
<hash
<string
>> : std::false_type
5552 #ifdef _GLIBCXX_USE_WCHAR_T
5553 /// std::hash specialization for wstring.
5555 struct hash
<wstring
>
5556 : public __hash_base
<size_t, wstring
>
5559 operator()(const wstring
& __s
) const noexcept
5560 { return std::_Hash_impl::hash(__s
.data(),
5561 __s
.length() * sizeof(wchar_t)); }
5565 struct __is_fast_hash
<hash
<wstring
>> : std::false_type
5568 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
5570 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
5571 /// std::hash specialization for u16string.
5573 struct hash
<u16string
>
5574 : public __hash_base
<size_t, u16string
>
5577 operator()(const u16string
& __s
) const noexcept
5578 { return std::_Hash_impl::hash(__s
.data(),
5579 __s
.length() * sizeof(char16_t
)); }
5583 struct __is_fast_hash
<hash
<u16string
>> : std::false_type
5586 /// std::hash specialization for u32string.
5588 struct hash
<u32string
>
5589 : public __hash_base
<size_t, u32string
>
5592 operator()(const u32string
& __s
) const noexcept
5593 { return std::_Hash_impl::hash(__s
.data(),
5594 __s
.length() * sizeof(char32_t
)); }
5598 struct __is_fast_hash
<hash
<u32string
>> : std::false_type
5602 #if __cplusplus > 201103L
5604 #define __cpp_lib_string_udls 201304
5606 inline namespace literals
5608 inline namespace string_literals
5611 _GLIBCXX_DEFAULT_ABI_TAG
5612 inline basic_string
<char>
5613 operator""s(const char* __str
, size_t __len
)
5614 { return basic_string
<char>{__str
, __len
}; }
5616 #ifdef _GLIBCXX_USE_WCHAR_T
5617 _GLIBCXX_DEFAULT_ABI_TAG
5618 inline basic_string
<wchar_t>
5619 operator""s(const wchar_t* __str
, size_t __len
)
5620 { return basic_string
<wchar_t>{__str
, __len
}; }
5623 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
5624 _GLIBCXX_DEFAULT_ABI_TAG
5625 inline basic_string
<char16_t
>
5626 operator""s(const char16_t
* __str
, size_t __len
)
5627 { return basic_string
<char16_t
>{__str
, __len
}; }
5629 _GLIBCXX_DEFAULT_ABI_TAG
5630 inline basic_string
<char32_t
>
5631 operator""s(const char32_t
* __str
, size_t __len
)
5632 { return basic_string
<char32_t
>{__str
, __len
}; }
5635 } // inline namespace string_literals
5636 } // inline namespace literals
5638 #endif // __cplusplus > 201103L
5640 _GLIBCXX_END_NAMESPACE_VERSION
5645 #endif /* _BASIC_STRING_H */