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
2604 { return this->_M_refcount
< 0; }
2607 _M_is_shared() const _GLIBCXX_NOEXCEPT
2608 { return this->_M_refcount
> 0; }
2611 _M_set_leaked() _GLIBCXX_NOEXCEPT
2612 { this->_M_refcount
= -1; }
2615 _M_set_sharable() _GLIBCXX_NOEXCEPT
2616 { this->_M_refcount
= 0; }
2619 _M_set_length_and_sharable(size_type __n
) _GLIBCXX_NOEXCEPT
2621 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2622 if (__builtin_expect(this != &_S_empty_rep(), false))
2625 this->_M_set_sharable(); // One reference.
2626 this->_M_length
= __n
;
2627 traits_type::assign(this->_M_refdata()[__n
], _S_terminal
);
2628 // grrr. (per 21.3.4)
2629 // You cannot leave those LWG people alone for a second.
2634 _M_refdata() throw()
2635 { return reinterpret_cast<_CharT
*>(this + 1); }
2638 _M_grab(const _Alloc
& __alloc1
, const _Alloc
& __alloc2
)
2640 return (!_M_is_leaked() && __alloc1
== __alloc2
)
2641 ? _M_refcopy() : _M_clone(__alloc1
);
2646 _S_create(size_type
, size_type
, const _Alloc
&);
2649 _M_dispose(const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
2651 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2652 if (__builtin_expect(this != &_S_empty_rep(), false))
2655 // Be race-detector-friendly. For more info see bits/c++config.
2656 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount
);
2657 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount
,
2660 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount
);
2667 _M_destroy(const _Alloc
&) throw();
2670 _M_refcopy() throw()
2672 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2673 if (__builtin_expect(this != &_S_empty_rep(), false))
2675 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount
, 1);
2676 return _M_refdata();
2680 _M_clone(const _Alloc
&, size_type __res
= 0);
2683 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
2684 struct _Alloc_hider
: _Alloc
2686 _Alloc_hider(_CharT
* __dat
, const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
2687 : _Alloc(__a
), _M_p(__dat
) { }
2689 _CharT
* _M_p
; // The actual data.
2693 // Data Members (public):
2694 // NB: This is an unsigned type, and thus represents the maximum
2695 // size that the allocator can hold.
2696 /// Value returned by various member functions when they fail.
2697 static const size_type npos
= static_cast<size_type
>(-1);
2700 // Data Members (private):
2701 mutable _Alloc_hider _M_dataplus
;
2704 _M_data() const _GLIBCXX_NOEXCEPT
2705 { return _M_dataplus
._M_p
; }
2708 _M_data(_CharT
* __p
) _GLIBCXX_NOEXCEPT
2709 { return (_M_dataplus
._M_p
= __p
); }
2712 _M_rep() const _GLIBCXX_NOEXCEPT
2713 { return &((reinterpret_cast<_Rep
*> (_M_data()))[-1]); }
2715 // For the internal use we have functions similar to `begin'/`end'
2716 // but they do not call _M_leak.
2718 _M_ibegin() const _GLIBCXX_NOEXCEPT
2719 { return iterator(_M_data()); }
2722 _M_iend() const _GLIBCXX_NOEXCEPT
2723 { return iterator(_M_data() + this->size()); }
2726 _M_leak() // for use in begin() & non-const op[]
2728 if (!_M_rep()->_M_is_leaked())
2733 _M_check(size_type __pos
, const char* __s
) const
2735 if (__pos
> this->size())
2736 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
2737 "this->size() (which is %zu)"),
2738 __s
, __pos
, this->size());
2743 _M_check_length(size_type __n1
, size_type __n2
, const char* __s
) const
2745 if (this->max_size() - (this->size() - __n1
) < __n2
)
2746 __throw_length_error(__N(__s
));
2749 // NB: _M_limit doesn't check for a bad __pos value.
2751 _M_limit(size_type __pos
, size_type __off
) const _GLIBCXX_NOEXCEPT
2753 const bool __testoff
= __off
< this->size() - __pos
;
2754 return __testoff
? __off
: this->size() - __pos
;
2757 // True if _Rep and source do not overlap.
2759 _M_disjunct(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
2761 return (less
<const _CharT
*>()(__s
, _M_data())
2762 || less
<const _CharT
*>()(_M_data() + this->size(), __s
));
2765 // When __n = 1 way faster than the general multichar
2766 // traits_type::copy/move/assign.
2768 _M_copy(_CharT
* __d
, const _CharT
* __s
, size_type __n
) _GLIBCXX_NOEXCEPT
2771 traits_type::assign(*__d
, *__s
);
2773 traits_type::copy(__d
, __s
, __n
);
2777 _M_move(_CharT
* __d
, const _CharT
* __s
, size_type __n
) _GLIBCXX_NOEXCEPT
2780 traits_type::assign(*__d
, *__s
);
2782 traits_type::move(__d
, __s
, __n
);
2786 _M_assign(_CharT
* __d
, size_type __n
, _CharT __c
) _GLIBCXX_NOEXCEPT
2789 traits_type::assign(*__d
, __c
);
2791 traits_type::assign(__d
, __n
, __c
);
2794 // _S_copy_chars is a separate template to permit specialization
2795 // to optimize for the common case of pointers as iterators.
2796 template<class _Iterator
>
2798 _S_copy_chars(_CharT
* __p
, _Iterator __k1
, _Iterator __k2
)
2801 for (; __k1
!= __k2
; ++__k1
, (void)++__p
)
2802 traits_type::assign(*__p
, *__k1
); // These types are off.
2806 _S_copy_chars(_CharT
* __p
, iterator __k1
, iterator __k2
) _GLIBCXX_NOEXCEPT
2807 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
2810 _S_copy_chars(_CharT
* __p
, const_iterator __k1
, const_iterator __k2
)
2812 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
2815 _S_copy_chars(_CharT
* __p
, _CharT
* __k1
, _CharT
* __k2
) _GLIBCXX_NOEXCEPT
2816 { _M_copy(__p
, __k1
, __k2
- __k1
); }
2819 _S_copy_chars(_CharT
* __p
, const _CharT
* __k1
, const _CharT
* __k2
)
2821 { _M_copy(__p
, __k1
, __k2
- __k1
); }
2824 _S_compare(size_type __n1
, size_type __n2
) _GLIBCXX_NOEXCEPT
2826 const difference_type __d
= difference_type(__n1
- __n2
);
2828 if (__d
> __gnu_cxx::__numeric_traits
<int>::__max
)
2829 return __gnu_cxx::__numeric_traits
<int>::__max
;
2830 else if (__d
< __gnu_cxx::__numeric_traits
<int>::__min
)
2831 return __gnu_cxx::__numeric_traits
<int>::__min
;
2837 _M_mutate(size_type __pos
, size_type __len1
, size_type __len2
);
2843 _S_empty_rep() _GLIBCXX_NOEXCEPT
2844 { return _Rep::_S_empty_rep(); }
2847 // Construct/copy/destroy:
2848 // NB: We overload ctors in some cases instead of using default
2849 // arguments, per 17.4.4.4 para. 2 item 2.
2852 * @brief Default constructor creates an empty string.
2855 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2856 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
2858 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ }
2862 * @brief Construct an empty string using allocator @a a.
2865 basic_string(const _Alloc
& __a
);
2867 // NB: per LWG issue 42, semantics different from IS:
2869 * @brief Construct string with copy of value of @a str.
2870 * @param __str Source string.
2872 basic_string(const basic_string
& __str
);
2874 * @brief Construct string as copy of a substring.
2875 * @param __str Source string.
2876 * @param __pos Index of first character to copy from.
2877 * @param __n Number of characters to copy (default remainder).
2879 basic_string(const basic_string
& __str
, size_type __pos
,
2880 size_type __n
= npos
);
2882 * @brief Construct string as copy of a substring.
2883 * @param __str Source string.
2884 * @param __pos Index of first character to copy from.
2885 * @param __n Number of characters to copy.
2886 * @param __a Allocator to use.
2888 basic_string(const basic_string
& __str
, size_type __pos
,
2889 size_type __n
, const _Alloc
& __a
);
2892 * @brief Construct string initialized by a character %array.
2893 * @param __s Source character %array.
2894 * @param __n Number of characters to copy.
2895 * @param __a Allocator to use (default is default allocator).
2897 * NB: @a __s must have at least @a __n characters, '\\0'
2898 * has no special meaning.
2900 basic_string(const _CharT
* __s
, size_type __n
,
2901 const _Alloc
& __a
= _Alloc());
2903 * @brief Construct string as copy of a C string.
2904 * @param __s Source C string.
2905 * @param __a Allocator to use (default is default allocator).
2907 basic_string(const _CharT
* __s
, const _Alloc
& __a
= _Alloc());
2909 * @brief Construct string as multiple characters.
2910 * @param __n Number of characters.
2911 * @param __c Character to use.
2912 * @param __a Allocator to use (default is default allocator).
2914 basic_string(size_type __n
, _CharT __c
, const _Alloc
& __a
= _Alloc());
2916 #if __cplusplus >= 201103L
2918 * @brief Move construct string.
2919 * @param __str Source string.
2921 * The newly-created string contains the exact contents of @a __str.
2922 * @a __str is a valid, but unspecified string.
2924 basic_string(basic_string
&& __str
)
2925 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2926 noexcept
// FIXME C++11: should always be noexcept.
2928 : _M_dataplus(__str
._M_dataplus
)
2930 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2931 __str
._M_data(_S_empty_rep()._M_refdata());
2933 __str
._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
2938 * @brief Construct string from an initializer %list.
2939 * @param __l std::initializer_list of characters.
2940 * @param __a Allocator to use (default is default allocator).
2942 basic_string(initializer_list
<_CharT
> __l
, const _Alloc
& __a
= _Alloc());
2946 * @brief Construct string as copy of a range.
2947 * @param __beg Start of range.
2948 * @param __end End of range.
2949 * @param __a Allocator to use (default is default allocator).
2951 template<class _InputIterator
>
2952 basic_string(_InputIterator __beg
, _InputIterator __end
,
2953 const _Alloc
& __a
= _Alloc());
2956 * @brief Destroy the string instance.
2958 ~basic_string() _GLIBCXX_NOEXCEPT
2959 { _M_rep()->_M_dispose(this->get_allocator()); }
2962 * @brief Assign the value of @a str to this string.
2963 * @param __str Source string.
2966 operator=(const basic_string
& __str
)
2967 { return this->assign(__str
); }
2970 * @brief Copy contents of @a s into this string.
2971 * @param __s Source null-terminated string.
2974 operator=(const _CharT
* __s
)
2975 { return this->assign(__s
); }
2978 * @brief Set value to string of length 1.
2979 * @param __c Source character.
2981 * Assigning to a character makes this string length 1 and
2982 * (*this)[0] == @a c.
2985 operator=(_CharT __c
)
2987 this->assign(1, __c
);
2991 #if __cplusplus >= 201103L
2993 * @brief Move assign the value of @a str to this string.
2994 * @param __str Source string.
2996 * The contents of @a str are moved into this string (without copying).
2997 * @a str is a valid, but unspecified string.
2999 // PR 58265, this should be noexcept.
3001 operator=(basic_string
&& __str
)
3009 * @brief Set value to string constructed from initializer %list.
3010 * @param __l std::initializer_list.
3013 operator=(initializer_list
<_CharT
> __l
)
3015 this->assign(__l
.begin(), __l
.size());
3022 * Returns a read/write iterator that points to the first character in
3023 * the %string. Unshares the string.
3026 begin() // FIXME C++11: should be noexcept.
3029 return iterator(_M_data());
3033 * Returns a read-only (constant) iterator that points to the first
3034 * character in the %string.
3037 begin() const _GLIBCXX_NOEXCEPT
3038 { return const_iterator(_M_data()); }
3041 * Returns a read/write iterator that points one past the last
3042 * character in the %string. Unshares the string.
3045 end() // FIXME C++11: should be noexcept.
3048 return iterator(_M_data() + this->size());
3052 * Returns a read-only (constant) iterator that points one past the
3053 * last character in the %string.
3056 end() const _GLIBCXX_NOEXCEPT
3057 { return const_iterator(_M_data() + this->size()); }
3060 * Returns a read/write reverse iterator that points to the last
3061 * character in the %string. Iteration is done in reverse element
3062 * order. Unshares the string.
3065 rbegin() // FIXME C++11: should be noexcept.
3066 { return reverse_iterator(this->end()); }
3069 * Returns a read-only (constant) reverse iterator that points
3070 * to the last character in the %string. Iteration is done in
3071 * reverse element order.
3073 const_reverse_iterator
3074 rbegin() const _GLIBCXX_NOEXCEPT
3075 { return const_reverse_iterator(this->end()); }
3078 * Returns a read/write reverse iterator that points to one before the
3079 * first character in the %string. Iteration is done in reverse
3080 * element order. Unshares the string.
3083 rend() // FIXME C++11: should be noexcept.
3084 { return reverse_iterator(this->begin()); }
3087 * Returns a read-only (constant) reverse iterator that points
3088 * to one before the first character in the %string. Iteration
3089 * is done in reverse element order.
3091 const_reverse_iterator
3092 rend() const _GLIBCXX_NOEXCEPT
3093 { return const_reverse_iterator(this->begin()); }
3095 #if __cplusplus >= 201103L
3097 * Returns a read-only (constant) iterator that points to the first
3098 * character in the %string.
3101 cbegin() const noexcept
3102 { return const_iterator(this->_M_data()); }
3105 * Returns a read-only (constant) iterator that points one past the
3106 * last character in the %string.
3109 cend() const noexcept
3110 { return const_iterator(this->_M_data() + this->size()); }
3113 * Returns a read-only (constant) reverse iterator that points
3114 * to the last character in the %string. Iteration is done in
3115 * reverse element order.
3117 const_reverse_iterator
3118 crbegin() const noexcept
3119 { return const_reverse_iterator(this->end()); }
3122 * Returns a read-only (constant) reverse iterator that points
3123 * to one before the first character in the %string. Iteration
3124 * is done in reverse element order.
3126 const_reverse_iterator
3127 crend() const noexcept
3128 { return const_reverse_iterator(this->begin()); }
3133 /// Returns the number of characters in the string, not including any
3134 /// null-termination.
3136 size() const _GLIBCXX_NOEXCEPT
3137 { return _M_rep()->_M_length
; }
3139 /// Returns the number of characters in the string, not including any
3140 /// null-termination.
3142 length() const _GLIBCXX_NOEXCEPT
3143 { return _M_rep()->_M_length
; }
3145 /// Returns the size() of the largest possible %string.
3147 max_size() const _GLIBCXX_NOEXCEPT
3148 { return _Rep::_S_max_size
; }
3151 * @brief Resizes the %string to the specified number of characters.
3152 * @param __n Number of characters the %string should contain.
3153 * @param __c Character to fill any new elements.
3155 * This function will %resize the %string to the specified
3156 * number of characters. If the number is smaller than the
3157 * %string's current size the %string is truncated, otherwise
3158 * the %string is extended and new elements are %set to @a __c.
3161 resize(size_type __n
, _CharT __c
);
3164 * @brief Resizes the %string to the specified number of characters.
3165 * @param __n Number of characters the %string should contain.
3167 * This function will resize the %string to the specified length. If
3168 * the new size is smaller than the %string's current size the %string
3169 * is truncated, otherwise the %string is extended and new characters
3170 * are default-constructed. For basic types such as char, this means
3171 * setting them to 0.
3174 resize(size_type __n
)
3175 { this->resize(__n
, _CharT()); }
3177 #if __cplusplus >= 201103L
3178 /// A non-binding request to reduce capacity() to size().
3180 shrink_to_fit() _GLIBCXX_NOEXCEPT
3182 if (capacity() > size())
3193 * Returns the total number of characters that the %string can hold
3194 * before needing to allocate more memory.
3197 capacity() const _GLIBCXX_NOEXCEPT
3198 { return _M_rep()->_M_capacity
; }
3201 * @brief Attempt to preallocate enough memory for specified number of
3203 * @param __res_arg Number of characters required.
3204 * @throw std::length_error If @a __res_arg exceeds @c max_size().
3206 * This function attempts to reserve enough memory for the
3207 * %string to hold the specified number of characters. If the
3208 * number requested is more than max_size(), length_error is
3211 * The advantage of this function is that if optimal code is a
3212 * necessity and the user can determine the string length that will be
3213 * required, the user can reserve the memory in %advance, and thus
3214 * prevent a possible reallocation of memory and copying of %string
3218 reserve(size_type __res_arg
= 0);
3221 * Erases the string, making it empty.
3223 // PR 56166: this should not throw.
3226 { _M_mutate(0, this->size(), 0); }
3229 * Returns true if the %string is empty. Equivalent to
3230 * <code>*this == ""</code>.
3233 empty() const _GLIBCXX_NOEXCEPT
3234 { return this->size() == 0; }
3238 * @brief Subscript access to the data contained in the %string.
3239 * @param __pos The index of the character to access.
3240 * @return Read-only (constant) reference to the character.
3242 * This operator allows for easy, array-style, data access.
3243 * Note that data access with this operator is unchecked and
3244 * out_of_range lookups are not defined. (For checked lookups
3248 operator[] (size_type __pos
) const _GLIBCXX_NOEXCEPT
3250 _GLIBCXX_DEBUG_ASSERT(__pos
<= size());
3251 return _M_data()[__pos
];
3255 * @brief Subscript access to the data contained in the %string.
3256 * @param __pos The index of the character to access.
3257 * @return Read/write reference to the character.
3259 * This operator allows for easy, array-style, data access.
3260 * Note that data access with this operator is unchecked and
3261 * out_of_range lookups are not defined. (For checked lookups
3262 * see at().) Unshares the string.
3265 operator[](size_type __pos
)
3267 // Allow pos == size() both in C++98 mode, as v3 extension,
3268 // and in C++11 mode.
3269 _GLIBCXX_DEBUG_ASSERT(__pos
<= size());
3270 // In pedantic mode be strict in C++98 mode.
3271 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus
>= 201103L || __pos
< size());
3273 return _M_data()[__pos
];
3277 * @brief Provides access to the data contained in the %string.
3278 * @param __n The index of the character to access.
3279 * @return Read-only (const) reference to the character.
3280 * @throw std::out_of_range If @a n is an invalid index.
3282 * This function provides for safer data access. The parameter is
3283 * first checked that it is in the range of the string. The function
3284 * throws out_of_range if the check fails.
3287 at(size_type __n
) const
3289 if (__n
>= this->size())
3290 __throw_out_of_range_fmt(__N("basic_string::at: __n "
3291 "(which is %zu) >= this->size() "
3294 return _M_data()[__n
];
3298 * @brief Provides access to the data contained in the %string.
3299 * @param __n The index of the character to access.
3300 * @return Read/write reference to the character.
3301 * @throw std::out_of_range If @a n is an invalid index.
3303 * This function provides for safer data access. The parameter is
3304 * first checked that it is in the range of the string. The function
3305 * throws out_of_range if the check fails. Success results in
3306 * unsharing the string.
3312 __throw_out_of_range_fmt(__N("basic_string::at: __n "
3313 "(which is %zu) >= this->size() "
3317 return _M_data()[__n
];
3320 #if __cplusplus >= 201103L
3322 * Returns a read/write reference to the data at the first
3323 * element of the %string.
3328 _GLIBCXX_DEBUG_ASSERT(!empty());
3329 return operator[](0);
3333 * Returns a read-only (constant) reference to the data at the first
3334 * element of the %string.
3337 front() const _GLIBCXX_NOEXCEPT
3339 _GLIBCXX_DEBUG_ASSERT(!empty());
3340 return operator[](0);
3344 * Returns a read/write reference to the data at the last
3345 * element of the %string.
3350 _GLIBCXX_DEBUG_ASSERT(!empty());
3351 return operator[](this->size() - 1);
3355 * Returns a read-only (constant) reference to the data at the
3356 * last element of the %string.
3359 back() const _GLIBCXX_NOEXCEPT
3361 _GLIBCXX_DEBUG_ASSERT(!empty());
3362 return operator[](this->size() - 1);
3368 * @brief Append a string to this string.
3369 * @param __str The string to append.
3370 * @return Reference to this string.
3373 operator+=(const basic_string
& __str
)
3374 { return this->append(__str
); }
3377 * @brief Append a C string.
3378 * @param __s The C string to append.
3379 * @return Reference to this string.
3382 operator+=(const _CharT
* __s
)
3383 { return this->append(__s
); }
3386 * @brief Append a character.
3387 * @param __c The character to append.
3388 * @return Reference to this string.
3391 operator+=(_CharT __c
)
3393 this->push_back(__c
);
3397 #if __cplusplus >= 201103L
3399 * @brief Append an initializer_list of characters.
3400 * @param __l The initializer_list of characters to be appended.
3401 * @return Reference to this string.
3404 operator+=(initializer_list
<_CharT
> __l
)
3405 { return this->append(__l
.begin(), __l
.size()); }
3409 * @brief Append a string to this string.
3410 * @param __str The string to append.
3411 * @return Reference to this string.
3414 append(const basic_string
& __str
);
3417 * @brief Append a substring.
3418 * @param __str The string to append.
3419 * @param __pos Index of the first character of str to append.
3420 * @param __n The number of characters to append.
3421 * @return Reference to this string.
3422 * @throw std::out_of_range if @a __pos is not a valid index.
3424 * This function appends @a __n characters from @a __str
3425 * starting at @a __pos to this string. If @a __n is is larger
3426 * than the number of available characters in @a __str, the
3427 * remainder of @a __str is appended.
3430 append(const basic_string
& __str
, size_type __pos
, size_type __n
);
3433 * @brief Append a C substring.
3434 * @param __s The C string to append.
3435 * @param __n The number of characters to append.
3436 * @return Reference to this string.
3439 append(const _CharT
* __s
, size_type __n
);
3442 * @brief Append a C string.
3443 * @param __s The C string to append.
3444 * @return Reference to this string.
3447 append(const _CharT
* __s
)
3449 __glibcxx_requires_string(__s
);
3450 return this->append(__s
, traits_type::length(__s
));
3454 * @brief Append multiple characters.
3455 * @param __n The number of characters to append.
3456 * @param __c The character to use.
3457 * @return Reference to this string.
3459 * Appends __n copies of __c to this string.
3462 append(size_type __n
, _CharT __c
);
3464 #if __cplusplus >= 201103L
3466 * @brief Append an initializer_list of characters.
3467 * @param __l The initializer_list of characters to append.
3468 * @return Reference to this string.
3471 append(initializer_list
<_CharT
> __l
)
3472 { return this->append(__l
.begin(), __l
.size()); }
3476 * @brief Append a range of characters.
3477 * @param __first Iterator referencing the first character to append.
3478 * @param __last Iterator marking the end of the range.
3479 * @return Reference to this string.
3481 * Appends characters in the range [__first,__last) to this string.
3483 template<class _InputIterator
>
3485 append(_InputIterator __first
, _InputIterator __last
)
3486 { return this->replace(_M_iend(), _M_iend(), __first
, __last
); }
3489 * @brief Append a single character.
3490 * @param __c Character to append.
3493 push_back(_CharT __c
)
3495 const size_type __len
= 1 + this->size();
3496 if (__len
> this->capacity() || _M_rep()->_M_is_shared())
3497 this->reserve(__len
);
3498 traits_type::assign(_M_data()[this->size()], __c
);
3499 _M_rep()->_M_set_length_and_sharable(__len
);
3503 * @brief Set value to contents of another string.
3504 * @param __str Source string to use.
3505 * @return Reference to this string.
3508 assign(const basic_string
& __str
);
3510 #if __cplusplus >= 201103L
3512 * @brief Set value to contents of another string.
3513 * @param __str Source string to use.
3514 * @return Reference to this string.
3516 * This function sets this string to the exact contents of @a __str.
3517 * @a __str is a valid, but unspecified string.
3519 // PR 58265, this should be noexcept.
3521 assign(basic_string
&& __str
)
3529 * @brief Set value to a substring of a string.
3530 * @param __str The string to use.
3531 * @param __pos Index of the first character of str.
3532 * @param __n Number of characters to use.
3533 * @return Reference to this string.
3534 * @throw std::out_of_range if @a pos is not a valid index.
3536 * This function sets this string to the substring of @a __str
3537 * consisting of @a __n characters at @a __pos. If @a __n is
3538 * is larger than the number of available characters in @a
3539 * __str, the remainder of @a __str is used.
3542 assign(const basic_string
& __str
, size_type __pos
, size_type __n
)
3543 { return this->assign(__str
._M_data()
3544 + __str
._M_check(__pos
, "basic_string::assign"),
3545 __str
._M_limit(__pos
, __n
)); }
3548 * @brief Set value to a C substring.
3549 * @param __s The C string to use.
3550 * @param __n Number of characters to use.
3551 * @return Reference to this string.
3553 * This function sets the value of this string to the first @a __n
3554 * characters of @a __s. If @a __n is is larger than the number of
3555 * available characters in @a __s, the remainder of @a __s is used.
3558 assign(const _CharT
* __s
, size_type __n
);
3561 * @brief Set value to contents of a C string.
3562 * @param __s The C string to use.
3563 * @return Reference to this string.
3565 * This function sets the value of this string to the value of @a __s.
3566 * The data is copied, so there is no dependence on @a __s once the
3570 assign(const _CharT
* __s
)
3572 __glibcxx_requires_string(__s
);
3573 return this->assign(__s
, traits_type::length(__s
));
3577 * @brief Set value to multiple characters.
3578 * @param __n Length of the resulting string.
3579 * @param __c The character to use.
3580 * @return Reference to this string.
3582 * This function sets the value of this string to @a __n copies of
3586 assign(size_type __n
, _CharT __c
)
3587 { return _M_replace_aux(size_type(0), this->size(), __n
, __c
); }
3590 * @brief Set value to a range of characters.
3591 * @param __first Iterator referencing the first character to append.
3592 * @param __last Iterator marking the end of the range.
3593 * @return Reference to this string.
3595 * Sets value of string to characters in the range [__first,__last).
3597 template<class _InputIterator
>
3599 assign(_InputIterator __first
, _InputIterator __last
)
3600 { return this->replace(_M_ibegin(), _M_iend(), __first
, __last
); }
3602 #if __cplusplus >= 201103L
3604 * @brief Set value to an initializer_list of characters.
3605 * @param __l The initializer_list of characters to assign.
3606 * @return Reference to this string.
3609 assign(initializer_list
<_CharT
> __l
)
3610 { return this->assign(__l
.begin(), __l
.size()); }
3614 * @brief Insert multiple characters.
3615 * @param __p Iterator referencing location in string to insert at.
3616 * @param __n Number of characters to insert
3617 * @param __c The character to insert.
3618 * @throw std::length_error If new length exceeds @c max_size().
3620 * Inserts @a __n copies of character @a __c starting at the
3621 * position referenced by iterator @a __p. If adding
3622 * characters causes the length to exceed max_size(),
3623 * length_error is thrown. The value of the string doesn't
3624 * change if an error is thrown.
3627 insert(iterator __p
, size_type __n
, _CharT __c
)
3628 { this->replace(__p
, __p
, __n
, __c
); }
3631 * @brief Insert a range of characters.
3632 * @param __p Iterator referencing location in string to insert at.
3633 * @param __beg Start of range.
3634 * @param __end End of range.
3635 * @throw std::length_error If new length exceeds @c max_size().
3637 * Inserts characters in range [__beg,__end). If adding
3638 * characters causes the length to exceed max_size(),
3639 * length_error is thrown. The value of the string doesn't
3640 * change if an error is thrown.
3642 template<class _InputIterator
>
3644 insert(iterator __p
, _InputIterator __beg
, _InputIterator __end
)
3645 { this->replace(__p
, __p
, __beg
, __end
); }
3647 #if __cplusplus >= 201103L
3649 * @brief Insert an initializer_list of characters.
3650 * @param __p Iterator referencing location in string to insert at.
3651 * @param __l The initializer_list of characters to insert.
3652 * @throw std::length_error If new length exceeds @c max_size().
3655 insert(iterator __p
, initializer_list
<_CharT
> __l
)
3657 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
3658 this->insert(__p
- _M_ibegin(), __l
.begin(), __l
.size());
3663 * @brief Insert value of a string.
3664 * @param __pos1 Iterator referencing location in string to insert at.
3665 * @param __str The string to insert.
3666 * @return Reference to this string.
3667 * @throw std::length_error If new length exceeds @c max_size().
3669 * Inserts value of @a __str starting at @a __pos1. If adding
3670 * characters causes the length to exceed max_size(),
3671 * length_error is thrown. The value of the string doesn't
3672 * change if an error is thrown.
3675 insert(size_type __pos1
, const basic_string
& __str
)
3676 { return this->insert(__pos1
, __str
, size_type(0), __str
.size()); }
3679 * @brief Insert a substring.
3680 * @param __pos1 Iterator referencing location in string to insert at.
3681 * @param __str The string to insert.
3682 * @param __pos2 Start of characters in str to insert.
3683 * @param __n Number of characters to insert.
3684 * @return Reference to this string.
3685 * @throw std::length_error If new length exceeds @c max_size().
3686 * @throw std::out_of_range If @a pos1 > size() or
3687 * @a __pos2 > @a str.size().
3689 * Starting at @a pos1, insert @a __n character of @a __str
3690 * beginning with @a __pos2. If adding characters causes the
3691 * length to exceed max_size(), length_error is thrown. If @a
3692 * __pos1 is beyond the end of this string or @a __pos2 is
3693 * beyond the end of @a __str, out_of_range is thrown. The
3694 * value of the string doesn't change if an error is thrown.
3697 insert(size_type __pos1
, const basic_string
& __str
,
3698 size_type __pos2
, size_type __n
)
3699 { return this->insert(__pos1
, __str
._M_data()
3700 + __str
._M_check(__pos2
, "basic_string::insert"),
3701 __str
._M_limit(__pos2
, __n
)); }
3704 * @brief Insert a C substring.
3705 * @param __pos Iterator referencing location in string to insert at.
3706 * @param __s The C string to insert.
3707 * @param __n The number of characters to insert.
3708 * @return Reference to this string.
3709 * @throw std::length_error If new length exceeds @c max_size().
3710 * @throw std::out_of_range If @a __pos is beyond the end of this
3713 * Inserts the first @a __n characters of @a __s starting at @a
3714 * __pos. If adding characters causes the length to exceed
3715 * max_size(), length_error is thrown. If @a __pos is beyond
3716 * end(), out_of_range is thrown. The value of the string
3717 * doesn't change if an error is thrown.
3720 insert(size_type __pos
, const _CharT
* __s
, size_type __n
);
3723 * @brief Insert a C string.
3724 * @param __pos Iterator referencing location in string to insert at.
3725 * @param __s The C string to insert.
3726 * @return Reference to this string.
3727 * @throw std::length_error If new length exceeds @c max_size().
3728 * @throw std::out_of_range If @a pos is beyond the end of this
3731 * Inserts the first @a n characters of @a __s starting at @a __pos. If
3732 * adding characters causes the length to exceed max_size(),
3733 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
3734 * thrown. The value of the string doesn't change if an error is
3738 insert(size_type __pos
, const _CharT
* __s
)
3740 __glibcxx_requires_string(__s
);
3741 return this->insert(__pos
, __s
, traits_type::length(__s
));
3745 * @brief Insert multiple characters.
3746 * @param __pos Index in string to insert at.
3747 * @param __n Number of characters to insert
3748 * @param __c The character to insert.
3749 * @return Reference to this string.
3750 * @throw std::length_error If new length exceeds @c max_size().
3751 * @throw std::out_of_range If @a __pos is beyond the end of this
3754 * Inserts @a __n copies of character @a __c starting at index
3755 * @a __pos. If adding characters causes the length to exceed
3756 * max_size(), length_error is thrown. If @a __pos > length(),
3757 * out_of_range is thrown. The value of the string doesn't
3758 * change if an error is thrown.
3761 insert(size_type __pos
, size_type __n
, _CharT __c
)
3762 { return _M_replace_aux(_M_check(__pos
, "basic_string::insert"),
3763 size_type(0), __n
, __c
); }
3766 * @brief Insert one character.
3767 * @param __p Iterator referencing position in string to insert at.
3768 * @param __c The character to insert.
3769 * @return Iterator referencing newly inserted char.
3770 * @throw std::length_error If new length exceeds @c max_size().
3772 * Inserts character @a __c at position referenced by @a __p.
3773 * If adding character causes the length to exceed max_size(),
3774 * length_error is thrown. If @a __p is beyond end of string,
3775 * out_of_range is thrown. The value of the string doesn't
3776 * change if an error is thrown.
3779 insert(iterator __p
, _CharT __c
)
3781 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
3782 const size_type __pos
= __p
- _M_ibegin();
3783 _M_replace_aux(__pos
, size_type(0), size_type(1), __c
);
3784 _M_rep()->_M_set_leaked();
3785 return iterator(_M_data() + __pos
);
3789 * @brief Remove characters.
3790 * @param __pos Index of first character to remove (default 0).
3791 * @param __n Number of characters to remove (default remainder).
3792 * @return Reference to this string.
3793 * @throw std::out_of_range If @a pos is beyond the end of this
3796 * Removes @a __n characters from this string starting at @a
3797 * __pos. The length of the string is reduced by @a __n. If
3798 * there are < @a __n characters to remove, the remainder of
3799 * the string is truncated. If @a __p is beyond end of string,
3800 * out_of_range is thrown. The value of the string doesn't
3801 * change if an error is thrown.
3804 erase(size_type __pos
= 0, size_type __n
= npos
)
3806 _M_mutate(_M_check(__pos
, "basic_string::erase"),
3807 _M_limit(__pos
, __n
), size_type(0));
3812 * @brief Remove one character.
3813 * @param __position Iterator referencing the character to remove.
3814 * @return iterator referencing same location after removal.
3816 * Removes the character at @a __position from this string. The value
3817 * of the string doesn't change if an error is thrown.
3820 erase(iterator __position
)
3822 _GLIBCXX_DEBUG_PEDASSERT(__position
>= _M_ibegin()
3823 && __position
< _M_iend());
3824 const size_type __pos
= __position
- _M_ibegin();
3825 _M_mutate(__pos
, size_type(1), size_type(0));
3826 _M_rep()->_M_set_leaked();
3827 return iterator(_M_data() + __pos
);
3831 * @brief Remove a range of characters.
3832 * @param __first Iterator referencing the first character to remove.
3833 * @param __last Iterator referencing the end of the range.
3834 * @return Iterator referencing location of first after removal.
3836 * Removes the characters in the range [first,last) from this string.
3837 * The value of the string doesn't change if an error is thrown.
3840 erase(iterator __first
, iterator __last
);
3842 #if __cplusplus >= 201103L
3844 * @brief Remove the last character.
3846 * The string must be non-empty.
3849 pop_back() // FIXME C++11: should be noexcept.
3851 _GLIBCXX_DEBUG_ASSERT(!empty());
3852 erase(size() - 1, 1);
3857 * @brief Replace characters with value from another string.
3858 * @param __pos Index of first character to replace.
3859 * @param __n Number of characters to be replaced.
3860 * @param __str String to insert.
3861 * @return Reference to this string.
3862 * @throw std::out_of_range If @a pos is beyond the end of this
3864 * @throw std::length_error If new length exceeds @c max_size().
3866 * Removes the characters in the range [__pos,__pos+__n) from
3867 * this string. In place, the value of @a __str is inserted.
3868 * If @a __pos is beyond end of string, out_of_range is thrown.
3869 * If the length of the result exceeds max_size(), length_error
3870 * is thrown. The value of the string doesn't change if an
3874 replace(size_type __pos
, size_type __n
, const basic_string
& __str
)
3875 { return this->replace(__pos
, __n
, __str
._M_data(), __str
.size()); }
3878 * @brief Replace characters with value from another string.
3879 * @param __pos1 Index of first character to replace.
3880 * @param __n1 Number of characters to be replaced.
3881 * @param __str String to insert.
3882 * @param __pos2 Index of first character of str to use.
3883 * @param __n2 Number of characters from str to use.
3884 * @return Reference to this string.
3885 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
3887 * @throw std::length_error If new length exceeds @c max_size().
3889 * Removes the characters in the range [__pos1,__pos1 + n) from this
3890 * string. In place, the value of @a __str is inserted. If @a __pos is
3891 * beyond end of string, out_of_range is thrown. If the length of the
3892 * result exceeds max_size(), length_error is thrown. The value of the
3893 * string doesn't change if an error is thrown.
3896 replace(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
3897 size_type __pos2
, size_type __n2
)
3898 { return this->replace(__pos1
, __n1
, __str
._M_data()
3899 + __str
._M_check(__pos2
, "basic_string::replace"),
3900 __str
._M_limit(__pos2
, __n2
)); }
3903 * @brief Replace characters with value of a C substring.
3904 * @param __pos Index of first character to replace.
3905 * @param __n1 Number of characters to be replaced.
3906 * @param __s C string to insert.
3907 * @param __n2 Number of characters from @a s to use.
3908 * @return Reference to this string.
3909 * @throw std::out_of_range If @a pos1 > size().
3910 * @throw std::length_error If new length exceeds @c max_size().
3912 * Removes the characters in the range [__pos,__pos + __n1)
3913 * from this string. In place, the first @a __n2 characters of
3914 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
3915 * @a __pos is beyond end of string, out_of_range is thrown. If
3916 * the length of result exceeds max_size(), length_error is
3917 * thrown. The value of the string doesn't change if an error
3921 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
3925 * @brief Replace characters with value of a C string.
3926 * @param __pos Index of first character to replace.
3927 * @param __n1 Number of characters to be replaced.
3928 * @param __s C string to insert.
3929 * @return Reference to this string.
3930 * @throw std::out_of_range If @a pos > size().
3931 * @throw std::length_error If new length exceeds @c max_size().
3933 * Removes the characters in the range [__pos,__pos + __n1)
3934 * from this string. In place, the characters of @a __s are
3935 * inserted. If @a __pos is beyond end of string, out_of_range
3936 * is thrown. If the length of result exceeds max_size(),
3937 * length_error is thrown. The value of the string doesn't
3938 * change if an error is thrown.
3941 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
)
3943 __glibcxx_requires_string(__s
);
3944 return this->replace(__pos
, __n1
, __s
, traits_type::length(__s
));
3948 * @brief Replace characters with multiple characters.
3949 * @param __pos Index of first character to replace.
3950 * @param __n1 Number of characters to be replaced.
3951 * @param __n2 Number of characters to insert.
3952 * @param __c Character to insert.
3953 * @return Reference to this string.
3954 * @throw std::out_of_range If @a __pos > size().
3955 * @throw std::length_error If new length exceeds @c max_size().
3957 * Removes the characters in the range [pos,pos + n1) from this
3958 * string. In place, @a __n2 copies of @a __c are inserted.
3959 * If @a __pos is beyond end of string, out_of_range is thrown.
3960 * If the length of result exceeds max_size(), length_error is
3961 * thrown. The value of the string doesn't change if an error
3965 replace(size_type __pos
, size_type __n1
, size_type __n2
, _CharT __c
)
3966 { return _M_replace_aux(_M_check(__pos
, "basic_string::replace"),
3967 _M_limit(__pos
, __n1
), __n2
, __c
); }
3970 * @brief Replace range of characters with string.
3971 * @param __i1 Iterator referencing start of range to replace.
3972 * @param __i2 Iterator referencing end of range to replace.
3973 * @param __str String value to insert.
3974 * @return Reference to this string.
3975 * @throw std::length_error If new length exceeds @c max_size().
3977 * Removes the characters in the range [__i1,__i2). In place,
3978 * the value of @a __str is inserted. If the length of result
3979 * exceeds max_size(), length_error is thrown. The value of
3980 * the string doesn't change if an error is thrown.
3983 replace(iterator __i1
, iterator __i2
, const basic_string
& __str
)
3984 { return this->replace(__i1
, __i2
, __str
._M_data(), __str
.size()); }
3987 * @brief Replace range of characters with C substring.
3988 * @param __i1 Iterator referencing start of range to replace.
3989 * @param __i2 Iterator referencing end of range to replace.
3990 * @param __s C string value to insert.
3991 * @param __n Number of characters from s to insert.
3992 * @return Reference to this string.
3993 * @throw std::length_error If new length exceeds @c max_size().
3995 * Removes the characters in the range [__i1,__i2). In place,
3996 * the first @a __n characters of @a __s are inserted. If the
3997 * length of result exceeds max_size(), length_error is thrown.
3998 * The value of the string doesn't change if an error is
4002 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
, size_type __n
)
4004 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4005 && __i2
<= _M_iend());
4006 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
, __s
, __n
);
4010 * @brief Replace range of characters with C string.
4011 * @param __i1 Iterator referencing start of range to replace.
4012 * @param __i2 Iterator referencing end of range to replace.
4013 * @param __s C string value to insert.
4014 * @return Reference to this string.
4015 * @throw std::length_error If new length exceeds @c max_size().
4017 * Removes the characters in the range [__i1,__i2). In place,
4018 * the characters of @a __s are inserted. If the length of
4019 * result exceeds max_size(), length_error is thrown. The
4020 * value of the string doesn't change if an error is thrown.
4023 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
)
4025 __glibcxx_requires_string(__s
);
4026 return this->replace(__i1
, __i2
, __s
, traits_type::length(__s
));
4030 * @brief Replace range of characters with multiple characters
4031 * @param __i1 Iterator referencing start of range to replace.
4032 * @param __i2 Iterator referencing end of range to replace.
4033 * @param __n Number of characters to insert.
4034 * @param __c Character to insert.
4035 * @return Reference to this string.
4036 * @throw std::length_error If new length exceeds @c max_size().
4038 * Removes the characters in the range [__i1,__i2). In place,
4039 * @a __n copies of @a __c are inserted. If the length of
4040 * result exceeds max_size(), length_error is thrown. The
4041 * value of the string doesn't change if an error is thrown.
4044 replace(iterator __i1
, iterator __i2
, size_type __n
, _CharT __c
)
4046 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4047 && __i2
<= _M_iend());
4048 return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __c
);
4052 * @brief Replace range of characters with range.
4053 * @param __i1 Iterator referencing start of range to replace.
4054 * @param __i2 Iterator referencing end of range to replace.
4055 * @param __k1 Iterator referencing start of range to insert.
4056 * @param __k2 Iterator referencing end of range to insert.
4057 * @return Reference to this string.
4058 * @throw std::length_error If new length exceeds @c max_size().
4060 * Removes the characters in the range [__i1,__i2). In place,
4061 * characters in the range [__k1,__k2) are inserted. If the
4062 * length of result exceeds max_size(), length_error is thrown.
4063 * The value of the string doesn't change if an error is
4066 template<class _InputIterator
>
4068 replace(iterator __i1
, iterator __i2
,
4069 _InputIterator __k1
, _InputIterator __k2
)
4071 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4072 && __i2
<= _M_iend());
4073 __glibcxx_requires_valid_range(__k1
, __k2
);
4074 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
4075 return _M_replace_dispatch(__i1
, __i2
, __k1
, __k2
, _Integral());
4078 // Specializations for the common case of pointer and iterator:
4079 // useful to avoid the overhead of temporary buffering in _M_replace.
4081 replace(iterator __i1
, iterator __i2
, _CharT
* __k1
, _CharT
* __k2
)
4083 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4084 && __i2
<= _M_iend());
4085 __glibcxx_requires_valid_range(__k1
, __k2
);
4086 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4091 replace(iterator __i1
, iterator __i2
,
4092 const _CharT
* __k1
, const _CharT
* __k2
)
4094 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4095 && __i2
<= _M_iend());
4096 __glibcxx_requires_valid_range(__k1
, __k2
);
4097 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4102 replace(iterator __i1
, iterator __i2
, iterator __k1
, iterator __k2
)
4104 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4105 && __i2
<= _M_iend());
4106 __glibcxx_requires_valid_range(__k1
, __k2
);
4107 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4108 __k1
.base(), __k2
- __k1
);
4112 replace(iterator __i1
, iterator __i2
,
4113 const_iterator __k1
, const_iterator __k2
)
4115 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4116 && __i2
<= _M_iend());
4117 __glibcxx_requires_valid_range(__k1
, __k2
);
4118 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4119 __k1
.base(), __k2
- __k1
);
4122 #if __cplusplus >= 201103L
4124 * @brief Replace range of characters with initializer_list.
4125 * @param __i1 Iterator referencing start of range to replace.
4126 * @param __i2 Iterator referencing end of range to replace.
4127 * @param __l The initializer_list of characters to insert.
4128 * @return Reference to this string.
4129 * @throw std::length_error If new length exceeds @c max_size().
4131 * Removes the characters in the range [__i1,__i2). In place,
4132 * characters in the range [__k1,__k2) are inserted. If the
4133 * length of result exceeds max_size(), length_error is thrown.
4134 * The value of the string doesn't change if an error is
4137 basic_string
& replace(iterator __i1
, iterator __i2
,
4138 initializer_list
<_CharT
> __l
)
4139 { return this->replace(__i1
, __i2
, __l
.begin(), __l
.end()); }
4143 template<class _Integer
>
4145 _M_replace_dispatch(iterator __i1
, iterator __i2
, _Integer __n
,
4146 _Integer __val
, __true_type
)
4147 { return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __val
); }
4149 template<class _InputIterator
>
4151 _M_replace_dispatch(iterator __i1
, iterator __i2
, _InputIterator __k1
,
4152 _InputIterator __k2
, __false_type
);
4155 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
4159 _M_replace_safe(size_type __pos1
, size_type __n1
, const _CharT
* __s
,
4162 // _S_construct_aux is used to implement the 21.3.1 para 15 which
4163 // requires special behaviour if _InIter is an integral type
4164 template<class _InIterator
>
4166 _S_construct_aux(_InIterator __beg
, _InIterator __end
,
4167 const _Alloc
& __a
, __false_type
)
4169 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
4170 return _S_construct(__beg
, __end
, __a
, _Tag());
4173 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4174 // 438. Ambiguity in the "do the right thing" clause
4175 template<class _Integer
>
4177 _S_construct_aux(_Integer __beg
, _Integer __end
,
4178 const _Alloc
& __a
, __true_type
)
4179 { return _S_construct_aux_2(static_cast<size_type
>(__beg
),
4183 _S_construct_aux_2(size_type __req
, _CharT __c
, const _Alloc
& __a
)
4184 { return _S_construct(__req
, __c
, __a
); }
4186 template<class _InIterator
>
4188 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
)
4190 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
4191 return _S_construct_aux(__beg
, __end
, __a
, _Integral());
4194 // For Input Iterators, used in istreambuf_iterators, etc.
4195 template<class _InIterator
>
4197 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
,
4198 input_iterator_tag
);
4200 // For forward_iterators up to random_access_iterators, used for
4201 // string::iterator, _CharT*, etc.
4202 template<class _FwdIterator
>
4204 _S_construct(_FwdIterator __beg
, _FwdIterator __end
, const _Alloc
& __a
,
4205 forward_iterator_tag
);
4208 _S_construct(size_type __req
, _CharT __c
, const _Alloc
& __a
);
4213 * @brief Copy substring into C string.
4214 * @param __s C string to copy value into.
4215 * @param __n Number of characters to copy.
4216 * @param __pos Index of first character to copy.
4217 * @return Number of characters actually copied
4218 * @throw std::out_of_range If __pos > size().
4220 * Copies up to @a __n characters starting at @a __pos into the
4221 * C string @a __s. If @a __pos is %greater than size(),
4222 * out_of_range is thrown.
4225 copy(_CharT
* __s
, size_type __n
, size_type __pos
= 0) const;
4228 * @brief Swap contents with another string.
4229 * @param __s String to swap with.
4231 * Exchanges the contents of this string with that of @a __s in constant
4234 // PR 58265, this should be noexcept.
4236 swap(basic_string
& __s
);
4238 // String operations:
4240 * @brief Return const pointer to null-terminated contents.
4242 * This is a handle to internal data. Do not modify or dire things may
4246 c_str() const _GLIBCXX_NOEXCEPT
4247 { return _M_data(); }
4250 * @brief Return const pointer to contents.
4252 * This is a handle to internal data. Do not modify or dire things may
4256 data() const _GLIBCXX_NOEXCEPT
4257 { return _M_data(); }
4260 * @brief Return copy of allocator used to construct this string.
4263 get_allocator() const _GLIBCXX_NOEXCEPT
4264 { return _M_dataplus
; }
4267 * @brief Find position of a C substring.
4268 * @param __s C string to locate.
4269 * @param __pos Index of character to search from.
4270 * @param __n Number of characters from @a s to search for.
4271 * @return Index of start of first occurrence.
4273 * Starting from @a __pos, searches forward for the first @a
4274 * __n characters in @a __s within this string. If found,
4275 * returns the index where it begins. If not found, returns
4279 find(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
4282 * @brief Find position of a string.
4283 * @param __str String to locate.
4284 * @param __pos Index of character to search from (default 0).
4285 * @return Index of start of first occurrence.
4287 * Starting from @a __pos, searches forward for value of @a __str within
4288 * this string. If found, returns the index where it begins. If not
4289 * found, returns npos.
4292 find(const basic_string
& __str
, size_type __pos
= 0) const
4294 { return this->find(__str
.data(), __pos
, __str
.size()); }
4297 * @brief Find position of a C string.
4298 * @param __s C string to locate.
4299 * @param __pos Index of character to search from (default 0).
4300 * @return Index of start of first occurrence.
4302 * Starting from @a __pos, searches forward for the value of @a
4303 * __s within this string. If found, returns the index where
4304 * it begins. If not found, returns npos.
4307 find(const _CharT
* __s
, size_type __pos
= 0) const
4309 __glibcxx_requires_string(__s
);
4310 return this->find(__s
, __pos
, traits_type::length(__s
));
4314 * @brief Find position of a character.
4315 * @param __c Character to locate.
4316 * @param __pos Index of character to search from (default 0).
4317 * @return Index of first occurrence.
4319 * Starting from @a __pos, searches forward for @a __c within
4320 * this string. If found, returns the index where it was
4321 * found. If not found, returns npos.
4324 find(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
;
4327 * @brief Find last position of a string.
4328 * @param __str String to locate.
4329 * @param __pos Index of character to search back from (default end).
4330 * @return Index of start of last occurrence.
4332 * Starting from @a __pos, searches backward for value of @a
4333 * __str within this string. If found, returns the index where
4334 * it begins. If not found, returns npos.
4337 rfind(const basic_string
& __str
, size_type __pos
= npos
) const
4339 { return this->rfind(__str
.data(), __pos
, __str
.size()); }
4342 * @brief Find last position of a C substring.
4343 * @param __s C string to locate.
4344 * @param __pos Index of character to search back from.
4345 * @param __n Number of characters from s to search for.
4346 * @return Index of start of last occurrence.
4348 * Starting from @a __pos, searches backward for the first @a
4349 * __n characters in @a __s within this string. If found,
4350 * returns the index where it begins. If not found, returns
4354 rfind(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
4357 * @brief Find last position of a C string.
4358 * @param __s C string to locate.
4359 * @param __pos Index of character to start search at (default end).
4360 * @return Index of start of last occurrence.
4362 * Starting from @a __pos, searches backward for the value of
4363 * @a __s within this string. If found, returns the index
4364 * where it begins. If not found, returns npos.
4367 rfind(const _CharT
* __s
, size_type __pos
= npos
) const
4369 __glibcxx_requires_string(__s
);
4370 return this->rfind(__s
, __pos
, traits_type::length(__s
));
4374 * @brief Find last position of a character.
4375 * @param __c Character to locate.
4376 * @param __pos Index of character to search back from (default end).
4377 * @return Index of last occurrence.
4379 * Starting from @a __pos, searches backward for @a __c within
4380 * this string. If found, returns the index where it was
4381 * found. If not found, returns npos.
4384 rfind(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
;
4387 * @brief Find position of a character of string.
4388 * @param __str String containing characters to locate.
4389 * @param __pos Index of character to search from (default 0).
4390 * @return Index of first occurrence.
4392 * Starting from @a __pos, searches forward for one of the
4393 * characters of @a __str within this string. If found,
4394 * returns the index where it was found. If not found, returns
4398 find_first_of(const basic_string
& __str
, size_type __pos
= 0) const
4400 { return this->find_first_of(__str
.data(), __pos
, __str
.size()); }
4403 * @brief Find position of a character of C substring.
4404 * @param __s String containing characters to locate.
4405 * @param __pos Index of character to search from.
4406 * @param __n Number of characters from s to search for.
4407 * @return Index of first occurrence.
4409 * Starting from @a __pos, searches forward for one of the
4410 * first @a __n characters of @a __s within this string. If
4411 * found, returns the index where it was found. If not found,
4415 find_first_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
4418 * @brief Find position of a character of C string.
4419 * @param __s String containing characters to locate.
4420 * @param __pos Index of character to search from (default 0).
4421 * @return Index of first occurrence.
4423 * Starting from @a __pos, searches forward for one of the
4424 * characters of @a __s within this string. If found, returns
4425 * the index where it was found. If not found, returns npos.
4428 find_first_of(const _CharT
* __s
, size_type __pos
= 0) const
4430 __glibcxx_requires_string(__s
);
4431 return this->find_first_of(__s
, __pos
, traits_type::length(__s
));
4435 * @brief Find position of a character.
4436 * @param __c Character to locate.
4437 * @param __pos Index of character to search from (default 0).
4438 * @return Index of first occurrence.
4440 * Starting from @a __pos, searches forward for the character
4441 * @a __c within this string. If found, returns the index
4442 * where it was found. If not found, returns npos.
4444 * Note: equivalent to find(__c, __pos).
4447 find_first_of(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
4448 { return this->find(__c
, __pos
); }
4451 * @brief Find last position of a character of string.
4452 * @param __str String containing characters to locate.
4453 * @param __pos Index of character to search back from (default end).
4454 * @return Index of last occurrence.
4456 * Starting from @a __pos, searches backward for one of the
4457 * characters of @a __str within this string. If found,
4458 * returns the index where it was found. If not found, returns
4462 find_last_of(const basic_string
& __str
, size_type __pos
= npos
) const
4464 { return this->find_last_of(__str
.data(), __pos
, __str
.size()); }
4467 * @brief Find last position of a character of C substring.
4468 * @param __s C string containing characters to locate.
4469 * @param __pos Index of character to search back from.
4470 * @param __n Number of characters from s to search for.
4471 * @return Index of last occurrence.
4473 * Starting from @a __pos, searches backward for one of the
4474 * first @a __n characters of @a __s within this string. If
4475 * found, returns the index where it was found. If not found,
4479 find_last_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
4482 * @brief Find last position of a character of C string.
4483 * @param __s C string containing characters to locate.
4484 * @param __pos Index of character to search back from (default end).
4485 * @return Index of last occurrence.
4487 * Starting from @a __pos, searches backward for one of the
4488 * characters of @a __s within this string. If found, returns
4489 * the index where it was found. If not found, returns npos.
4492 find_last_of(const _CharT
* __s
, size_type __pos
= npos
) const
4494 __glibcxx_requires_string(__s
);
4495 return this->find_last_of(__s
, __pos
, traits_type::length(__s
));
4499 * @brief Find last position of a character.
4500 * @param __c Character to locate.
4501 * @param __pos Index of character to search back from (default end).
4502 * @return Index of last occurrence.
4504 * Starting from @a __pos, searches backward for @a __c within
4505 * this string. If found, returns the index where it was
4506 * found. If not found, returns npos.
4508 * Note: equivalent to rfind(__c, __pos).
4511 find_last_of(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
4512 { return this->rfind(__c
, __pos
); }
4515 * @brief Find position of a character not in string.
4516 * @param __str String containing characters to avoid.
4517 * @param __pos Index of character to search from (default 0).
4518 * @return Index of first occurrence.
4520 * Starting from @a __pos, searches forward for a character not contained
4521 * in @a __str within this string. If found, returns the index where it
4522 * was found. If not found, returns npos.
4525 find_first_not_of(const basic_string
& __str
, size_type __pos
= 0) const
4527 { return this->find_first_not_of(__str
.data(), __pos
, __str
.size()); }
4530 * @brief Find position of a character not in C substring.
4531 * @param __s C string containing characters to avoid.
4532 * @param __pos Index of character to search from.
4533 * @param __n Number of characters from __s to consider.
4534 * @return Index of first occurrence.
4536 * Starting from @a __pos, searches forward for a character not
4537 * contained in the first @a __n characters of @a __s within
4538 * this string. If found, returns the index where it was
4539 * found. If not found, returns npos.
4542 find_first_not_of(const _CharT
* __s
, size_type __pos
,
4543 size_type __n
) const;
4546 * @brief Find position of a character not in C string.
4547 * @param __s C string containing characters to avoid.
4548 * @param __pos Index of character to search from (default 0).
4549 * @return Index of first occurrence.
4551 * Starting from @a __pos, searches forward for a character not
4552 * contained in @a __s within this string. If found, returns
4553 * the index where it was found. If not found, returns npos.
4556 find_first_not_of(const _CharT
* __s
, size_type __pos
= 0) const
4558 __glibcxx_requires_string(__s
);
4559 return this->find_first_not_of(__s
, __pos
, traits_type::length(__s
));
4563 * @brief Find position of a different character.
4564 * @param __c Character to avoid.
4565 * @param __pos Index of character to search from (default 0).
4566 * @return Index of first occurrence.
4568 * Starting from @a __pos, searches forward for a character
4569 * other than @a __c within this string. If found, returns the
4570 * index where it was found. If not found, returns npos.
4573 find_first_not_of(_CharT __c
, size_type __pos
= 0) const
4577 * @brief Find last position of a character not in string.
4578 * @param __str String containing characters to avoid.
4579 * @param __pos Index of character to search back from (default end).
4580 * @return Index of last occurrence.
4582 * Starting from @a __pos, searches backward for a character
4583 * not contained in @a __str within this string. If found,
4584 * returns the index where it was found. If not found, returns
4588 find_last_not_of(const basic_string
& __str
, size_type __pos
= npos
) const
4590 { return this->find_last_not_of(__str
.data(), __pos
, __str
.size()); }
4593 * @brief Find last position of a character not in C substring.
4594 * @param __s C string containing characters to avoid.
4595 * @param __pos Index of character to search back from.
4596 * @param __n Number of characters from s to consider.
4597 * @return Index of last occurrence.
4599 * Starting from @a __pos, searches backward for a character not
4600 * contained in the first @a __n characters of @a __s within this string.
4601 * If found, returns the index where it was found. If not found,
4605 find_last_not_of(const _CharT
* __s
, size_type __pos
,
4606 size_type __n
) const;
4608 * @brief Find last position of a character not in C string.
4609 * @param __s C string containing characters to avoid.
4610 * @param __pos Index of character to search back from (default end).
4611 * @return Index of last occurrence.
4613 * Starting from @a __pos, searches backward for a character
4614 * not contained in @a __s within this string. If found,
4615 * returns the index where it was found. If not found, returns
4619 find_last_not_of(const _CharT
* __s
, size_type __pos
= npos
) const
4621 __glibcxx_requires_string(__s
);
4622 return this->find_last_not_of(__s
, __pos
, traits_type::length(__s
));
4626 * @brief Find last position of a different character.
4627 * @param __c Character to avoid.
4628 * @param __pos Index of character to search back from (default end).
4629 * @return Index of last occurrence.
4631 * Starting from @a __pos, searches backward for a character other than
4632 * @a __c within this string. If found, returns the index where it was
4633 * found. If not found, returns npos.
4636 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const
4640 * @brief Get a substring.
4641 * @param __pos Index of first character (default 0).
4642 * @param __n Number of characters in substring (default remainder).
4643 * @return The new string.
4644 * @throw std::out_of_range If __pos > size().
4646 * Construct and return a new string using the @a __n
4647 * characters starting at @a __pos. If the string is too
4648 * short, use the remainder of the characters. If @a __pos is
4649 * beyond the end of the string, out_of_range is thrown.
4652 substr(size_type __pos
= 0, size_type __n
= npos
) const
4653 { return basic_string(*this,
4654 _M_check(__pos
, "basic_string::substr"), __n
); }
4657 * @brief Compare to a string.
4658 * @param __str String to compare against.
4659 * @return Integer < 0, 0, or > 0.
4661 * Returns an integer < 0 if this string is ordered before @a
4662 * __str, 0 if their values are equivalent, or > 0 if this
4663 * string is ordered after @a __str. Determines the effective
4664 * length rlen of the strings to compare as the smallest of
4665 * size() and str.size(). The function then compares the two
4666 * strings by calling traits::compare(data(), str.data(),rlen).
4667 * If the result of the comparison is nonzero returns it,
4668 * otherwise the shorter one is ordered first.
4671 compare(const basic_string
& __str
) const
4673 const size_type __size
= this->size();
4674 const size_type __osize
= __str
.size();
4675 const size_type __len
= std::min(__size
, __osize
);
4677 int __r
= traits_type::compare(_M_data(), __str
.data(), __len
);
4679 __r
= _S_compare(__size
, __osize
);
4684 * @brief Compare substring to a string.
4685 * @param __pos Index of first character of substring.
4686 * @param __n Number of characters in substring.
4687 * @param __str String to compare against.
4688 * @return Integer < 0, 0, or > 0.
4690 * Form the substring of this string from the @a __n characters
4691 * starting at @a __pos. Returns an integer < 0 if the
4692 * substring is ordered before @a __str, 0 if their values are
4693 * equivalent, or > 0 if the substring is ordered after @a
4694 * __str. Determines the effective length rlen of the strings
4695 * to compare as the smallest of the length of the substring
4696 * and @a __str.size(). The function then compares the two
4697 * strings by calling
4698 * traits::compare(substring.data(),str.data(),rlen). If the
4699 * result of the comparison is nonzero returns it, otherwise
4700 * the shorter one is ordered first.
4703 compare(size_type __pos
, size_type __n
, const basic_string
& __str
) const;
4706 * @brief Compare substring to a substring.
4707 * @param __pos1 Index of first character of substring.
4708 * @param __n1 Number of characters in substring.
4709 * @param __str String to compare against.
4710 * @param __pos2 Index of first character of substring of str.
4711 * @param __n2 Number of characters in substring of str.
4712 * @return Integer < 0, 0, or > 0.
4714 * Form the substring of this string from the @a __n1
4715 * characters starting at @a __pos1. Form the substring of @a
4716 * __str from the @a __n2 characters starting at @a __pos2.
4717 * Returns an integer < 0 if this substring is ordered before
4718 * the substring of @a __str, 0 if their values are equivalent,
4719 * or > 0 if this substring is ordered after the substring of
4720 * @a __str. Determines the effective length rlen of the
4721 * strings to compare as the smallest of the lengths of the
4722 * substrings. The function then compares the two strings by
4724 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
4725 * If the result of the comparison is nonzero returns it,
4726 * otherwise the shorter one is ordered first.
4729 compare(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
4730 size_type __pos2
, size_type __n2
) const;
4733 * @brief Compare to a C string.
4734 * @param __s C string to compare against.
4735 * @return Integer < 0, 0, or > 0.
4737 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
4738 * their values are equivalent, or > 0 if this string is ordered after
4739 * @a __s. Determines the effective length rlen of the strings to
4740 * compare as the smallest of size() and the length of a string
4741 * constructed from @a __s. The function then compares the two strings
4742 * by calling traits::compare(data(),s,rlen). If the result of the
4743 * comparison is nonzero returns it, otherwise the shorter one is
4747 compare(const _CharT
* __s
) const;
4749 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4750 // 5 String::compare specification questionable
4752 * @brief Compare substring to a C string.
4753 * @param __pos Index of first character of substring.
4754 * @param __n1 Number of characters in substring.
4755 * @param __s C string to compare against.
4756 * @return Integer < 0, 0, or > 0.
4758 * Form the substring of this string from the @a __n1
4759 * characters starting at @a pos. Returns an integer < 0 if
4760 * the substring is ordered before @a __s, 0 if their values
4761 * are equivalent, or > 0 if the substring is ordered after @a
4762 * __s. Determines the effective length rlen of the strings to
4763 * compare as the smallest of the length of the substring and
4764 * the length of a string constructed from @a __s. The
4765 * function then compares the two string by calling
4766 * traits::compare(substring.data(),__s,rlen). If the result of
4767 * the comparison is nonzero returns it, otherwise the shorter
4768 * one is ordered first.
4771 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
) const;
4774 * @brief Compare substring against a character %array.
4775 * @param __pos Index of first character of substring.
4776 * @param __n1 Number of characters in substring.
4777 * @param __s character %array to compare against.
4778 * @param __n2 Number of characters of s.
4779 * @return Integer < 0, 0, or > 0.
4781 * Form the substring of this string from the @a __n1
4782 * characters starting at @a __pos. Form a string from the
4783 * first @a __n2 characters of @a __s. Returns an integer < 0
4784 * if this substring is ordered before the string from @a __s,
4785 * 0 if their values are equivalent, or > 0 if this substring
4786 * is ordered after the string from @a __s. Determines the
4787 * effective length rlen of the strings to compare as the
4788 * smallest of the length of the substring and @a __n2. The
4789 * function then compares the two strings by calling
4790 * traits::compare(substring.data(),s,rlen). If the result of
4791 * the comparison is nonzero returns it, otherwise the shorter
4792 * one is ordered first.
4794 * NB: s must have at least n2 characters, '\\0' has
4795 * no special meaning.
4798 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
,
4799 size_type __n2
) const;
4801 #endif // !_GLIBCXX_USE_CXX11_ABI
4805 * @brief Concatenate two strings.
4806 * @param __lhs First string.
4807 * @param __rhs Last string.
4808 * @return New string with value of @a __lhs followed by @a __rhs.
4810 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4811 basic_string
<_CharT
, _Traits
, _Alloc
>
4812 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
4813 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
4815 basic_string
<_CharT
, _Traits
, _Alloc
> __str(__lhs
);
4816 __str
.append(__rhs
);
4821 * @brief Concatenate C string and string.
4822 * @param __lhs First string.
4823 * @param __rhs Last string.
4824 * @return New string with value of @a __lhs followed by @a __rhs.
4826 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4827 basic_string
<_CharT
,_Traits
,_Alloc
>
4828 operator+(const _CharT
* __lhs
,
4829 const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
);
4832 * @brief Concatenate character and string.
4833 * @param __lhs First string.
4834 * @param __rhs Last string.
4835 * @return New string with @a __lhs followed by @a __rhs.
4837 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4838 basic_string
<_CharT
,_Traits
,_Alloc
>
4839 operator+(_CharT __lhs
, const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
);
4842 * @brief Concatenate string and C string.
4843 * @param __lhs First string.
4844 * @param __rhs Last string.
4845 * @return New string with @a __lhs followed by @a __rhs.
4847 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4848 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4849 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
4850 const _CharT
* __rhs
)
4852 basic_string
<_CharT
, _Traits
, _Alloc
> __str(__lhs
);
4853 __str
.append(__rhs
);
4858 * @brief Concatenate string and character.
4859 * @param __lhs First string.
4860 * @param __rhs Last string.
4861 * @return New string with @a __lhs followed by @a __rhs.
4863 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4864 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4865 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
, _CharT __rhs
)
4867 typedef basic_string
<_CharT
, _Traits
, _Alloc
> __string_type
;
4868 typedef typename
__string_type::size_type __size_type
;
4869 __string_type
__str(__lhs
);
4870 __str
.append(__size_type(1), __rhs
);
4874 #if __cplusplus >= 201103L
4875 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4876 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4877 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
4878 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
4879 { return std::move(__lhs
.append(__rhs
)); }
4881 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4882 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4883 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
4884 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
4885 { return std::move(__rhs
.insert(0, __lhs
)); }
4887 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4888 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4889 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
4890 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
4892 const auto __size
= __lhs
.size() + __rhs
.size();
4893 const bool __cond
= (__size
> __lhs
.capacity()
4894 && __size
<= __rhs
.capacity());
4895 return __cond
? std::move(__rhs
.insert(0, __lhs
))
4896 : std::move(__lhs
.append(__rhs
));
4899 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4900 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4901 operator+(const _CharT
* __lhs
,
4902 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
4903 { return std::move(__rhs
.insert(0, __lhs
)); }
4905 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4906 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4907 operator+(_CharT __lhs
,
4908 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
4909 { return std::move(__rhs
.insert(0, 1, __lhs
)); }
4911 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4912 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4913 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
4914 const _CharT
* __rhs
)
4915 { return std::move(__lhs
.append(__rhs
)); }
4917 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4918 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4919 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
4921 { return std::move(__lhs
.append(1, __rhs
)); }
4926 * @brief Test equivalence of two strings.
4927 * @param __lhs First string.
4928 * @param __rhs Second string.
4929 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
4931 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4933 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
4934 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
4935 { return __lhs
.compare(__rhs
) == 0; }
4937 template<typename _CharT
>
4939 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
, bool>::__type
4940 operator==(const basic_string
<_CharT
>& __lhs
,
4941 const basic_string
<_CharT
>& __rhs
)
4942 { return (__lhs
.size() == __rhs
.size()
4943 && !std::char_traits
<_CharT
>::compare(__lhs
.data(), __rhs
.data(),
4947 * @brief Test equivalence of C string and string.
4948 * @param __lhs C string.
4949 * @param __rhs String.
4950 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
4952 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4954 operator==(const _CharT
* __lhs
,
4955 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
4956 { return __rhs
.compare(__lhs
) == 0; }
4959 * @brief Test equivalence of string and C string.
4960 * @param __lhs String.
4961 * @param __rhs C string.
4962 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
4964 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4966 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
4967 const _CharT
* __rhs
)
4968 { return __lhs
.compare(__rhs
) == 0; }
4972 * @brief Test difference of two strings.
4973 * @param __lhs First string.
4974 * @param __rhs Second string.
4975 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
4977 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4979 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
4980 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
4981 { return !(__lhs
== __rhs
); }
4984 * @brief Test difference of C string and string.
4985 * @param __lhs C string.
4986 * @param __rhs String.
4987 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
4989 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4991 operator!=(const _CharT
* __lhs
,
4992 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
4993 { return !(__lhs
== __rhs
); }
4996 * @brief Test difference of string and C string.
4997 * @param __lhs String.
4998 * @param __rhs C string.
4999 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
5001 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5003 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5004 const _CharT
* __rhs
)
5005 { return !(__lhs
== __rhs
); }
5009 * @brief Test if string precedes string.
5010 * @param __lhs First string.
5011 * @param __rhs Second string.
5012 * @return True if @a __lhs precedes @a __rhs. False otherwise.
5014 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5016 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5017 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5018 { return __lhs
.compare(__rhs
) < 0; }
5021 * @brief Test if string precedes C string.
5022 * @param __lhs String.
5023 * @param __rhs C string.
5024 * @return True if @a __lhs precedes @a __rhs. False otherwise.
5026 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5028 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5029 const _CharT
* __rhs
)
5030 { return __lhs
.compare(__rhs
) < 0; }
5033 * @brief Test if C string precedes string.
5034 * @param __lhs C string.
5035 * @param __rhs String.
5036 * @return True if @a __lhs precedes @a __rhs. False otherwise.
5038 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5040 operator<(const _CharT
* __lhs
,
5041 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5042 { return __rhs
.compare(__lhs
) > 0; }
5046 * @brief Test if string follows string.
5047 * @param __lhs First string.
5048 * @param __rhs Second string.
5049 * @return True if @a __lhs follows @a __rhs. False otherwise.
5051 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5053 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5054 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5055 { return __lhs
.compare(__rhs
) > 0; }
5058 * @brief Test if string follows C string.
5059 * @param __lhs String.
5060 * @param __rhs C string.
5061 * @return True if @a __lhs follows @a __rhs. False otherwise.
5063 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5065 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5066 const _CharT
* __rhs
)
5067 { return __lhs
.compare(__rhs
) > 0; }
5070 * @brief Test if C string follows string.
5071 * @param __lhs C string.
5072 * @param __rhs String.
5073 * @return True if @a __lhs follows @a __rhs. False otherwise.
5075 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5077 operator>(const _CharT
* __lhs
,
5078 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5079 { return __rhs
.compare(__lhs
) < 0; }
5083 * @brief Test if string doesn't follow string.
5084 * @param __lhs First string.
5085 * @param __rhs Second string.
5086 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5088 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5090 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5091 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5092 { return __lhs
.compare(__rhs
) <= 0; }
5095 * @brief Test if string doesn't follow C string.
5096 * @param __lhs String.
5097 * @param __rhs C string.
5098 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5100 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5102 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5103 const _CharT
* __rhs
)
5104 { return __lhs
.compare(__rhs
) <= 0; }
5107 * @brief Test if C string doesn't follow string.
5108 * @param __lhs C string.
5109 * @param __rhs String.
5110 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5112 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5114 operator<=(const _CharT
* __lhs
,
5115 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5116 { return __rhs
.compare(__lhs
) >= 0; }
5120 * @brief Test if string doesn't precede string.
5121 * @param __lhs First string.
5122 * @param __rhs Second string.
5123 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
5125 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5127 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5128 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5129 { return __lhs
.compare(__rhs
) >= 0; }
5132 * @brief Test if string doesn't precede C string.
5133 * @param __lhs String.
5134 * @param __rhs C string.
5135 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
5137 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5139 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5140 const _CharT
* __rhs
)
5141 { return __lhs
.compare(__rhs
) >= 0; }
5144 * @brief Test if C string doesn't precede string.
5145 * @param __lhs C string.
5146 * @param __rhs String.
5147 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
5149 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5151 operator>=(const _CharT
* __lhs
,
5152 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5153 { return __rhs
.compare(__lhs
) <= 0; }
5156 * @brief Swap contents of two strings.
5157 * @param __lhs First string.
5158 * @param __rhs Second string.
5160 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
5162 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5164 swap(basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5165 basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5166 { __lhs
.swap(__rhs
); }
5170 * @brief Read stream into a string.
5171 * @param __is Input stream.
5172 * @param __str Buffer to store into.
5173 * @return Reference to the input stream.
5175 * Stores characters from @a __is into @a __str until whitespace is
5176 * found, the end of the stream is encountered, or str.max_size()
5177 * is reached. If is.width() is non-zero, that is the limit on the
5178 * number of characters stored into @a __str. Any previous
5179 * contents of @a __str are erased.
5181 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5182 basic_istream
<_CharT
, _Traits
>&
5183 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
5184 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
);
5187 basic_istream
<char>&
5188 operator>>(basic_istream
<char>& __is
, basic_string
<char>& __str
);
5191 * @brief Write string to a stream.
5192 * @param __os Output stream.
5193 * @param __str String to write out.
5194 * @return Reference to the output stream.
5196 * Output characters of @a __str into os following the same rules as for
5197 * writing a C string.
5199 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5200 inline basic_ostream
<_CharT
, _Traits
>&
5201 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
5202 const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
5204 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5205 // 586. string inserter not a formatted function
5206 return __ostream_insert(__os
, __str
.data(), __str
.size());
5210 * @brief Read a line from stream into a string.
5211 * @param __is Input stream.
5212 * @param __str Buffer to store into.
5213 * @param __delim Character marking end of line.
5214 * @return Reference to the input stream.
5216 * Stores characters from @a __is into @a __str until @a __delim is
5217 * found, the end of the stream is encountered, or str.max_size()
5218 * is reached. Any previous contents of @a __str are erased. If
5219 * @a __delim is encountered, it is extracted but not stored into
5222 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5223 basic_istream
<_CharT
, _Traits
>&
5224 getline(basic_istream
<_CharT
, _Traits
>& __is
,
5225 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
);
5228 * @brief Read a line from stream into a string.
5229 * @param __is Input stream.
5230 * @param __str Buffer to store into.
5231 * @return Reference to the input stream.
5233 * Stores characters from is into @a __str until '\n' is
5234 * found, the end of the stream is encountered, or str.max_size()
5235 * is reached. Any previous contents of @a __str are erased. If
5236 * end of line is encountered, it is extracted but not stored into
5239 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5240 inline basic_istream
<_CharT
, _Traits
>&
5241 getline(basic_istream
<_CharT
, _Traits
>& __is
,
5242 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
5243 { return std::getline(__is
, __str
, __is
.widen('\n')); }
5245 #if __cplusplus >= 201103L
5246 /// Read a line from an rvalue stream into a string.
5247 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5248 inline basic_istream
<_CharT
, _Traits
>&
5249 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
5250 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
)
5251 { return std::getline(__is
, __str
, __delim
); }
5253 /// Read a line from an rvalue stream into a string.
5254 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5255 inline basic_istream
<_CharT
, _Traits
>&
5256 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
5257 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
5258 { return std::getline(__is
, __str
); }
5262 basic_istream
<char>&
5263 getline(basic_istream
<char>& __in
, basic_string
<char>& __str
,
5266 #ifdef _GLIBCXX_USE_WCHAR_T
5268 basic_istream
<wchar_t>&
5269 getline(basic_istream
<wchar_t>& __in
, basic_string
<wchar_t>& __str
,
5273 _GLIBCXX_END_NAMESPACE_VERSION
5276 #if __cplusplus >= 201103L && defined(_GLIBCXX_USE_C99)
5278 #include <ext/string_conversions.h>
5280 namespace std
_GLIBCXX_VISIBILITY(default)
5282 _GLIBCXX_BEGIN_NAMESPACE_VERSION
5283 _GLIBCXX_BEGIN_NAMESPACE_CXX11
5285 // 21.4 Numeric Conversions [string.conversions].
5287 stoi(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
5288 { return __gnu_cxx::__stoa
<long, int>(&std::strtol
, "stoi", __str
.c_str(),
5292 stol(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
5293 { return __gnu_cxx::__stoa(&std::strtol
, "stol", __str
.c_str(),
5296 inline unsigned long
5297 stoul(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
5298 { return __gnu_cxx::__stoa(&std::strtoul
, "stoul", __str
.c_str(),
5302 stoll(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
5303 { return __gnu_cxx::__stoa(&std::strtoll
, "stoll", __str
.c_str(),
5306 inline unsigned long long
5307 stoull(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
5308 { return __gnu_cxx::__stoa(&std::strtoull
, "stoull", __str
.c_str(),
5311 // NB: strtof vs strtod.
5313 stof(const string
& __str
, size_t* __idx
= 0)
5314 { return __gnu_cxx::__stoa(&std::strtof
, "stof", __str
.c_str(), __idx
); }
5317 stod(const string
& __str
, size_t* __idx
= 0)
5318 { return __gnu_cxx::__stoa(&std::strtod
, "stod", __str
.c_str(), __idx
); }
5321 stold(const string
& __str
, size_t* __idx
= 0)
5322 { return __gnu_cxx::__stoa(&std::strtold
, "stold", __str
.c_str(), __idx
); }
5324 // NB: (v)snprintf vs sprintf.
5328 to_string(int __val
)
5329 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, 4 * sizeof(int),
5333 to_string(unsigned __val
)
5334 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
5335 4 * sizeof(unsigned),
5339 to_string(long __val
)
5340 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, 4 * sizeof(long),
5344 to_string(unsigned long __val
)
5345 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
5346 4 * sizeof(unsigned long),
5350 to_string(long long __val
)
5351 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
5352 4 * sizeof(long long),
5356 to_string(unsigned long long __val
)
5357 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
5358 4 * sizeof(unsigned long long),
5362 to_string(float __val
)
5365 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
5366 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
5371 to_string(double __val
)
5374 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
5375 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
5380 to_string(long double __val
)
5383 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
5384 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
5388 #ifdef _GLIBCXX_USE_WCHAR_T
5390 stoi(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
5391 { return __gnu_cxx::__stoa
<long, int>(&std::wcstol
, "stoi", __str
.c_str(),
5395 stol(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
5396 { return __gnu_cxx::__stoa(&std::wcstol
, "stol", __str
.c_str(),
5399 inline unsigned long
5400 stoul(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
5401 { return __gnu_cxx::__stoa(&std::wcstoul
, "stoul", __str
.c_str(),
5405 stoll(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
5406 { return __gnu_cxx::__stoa(&std::wcstoll
, "stoll", __str
.c_str(),
5409 inline unsigned long long
5410 stoull(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
5411 { return __gnu_cxx::__stoa(&std::wcstoull
, "stoull", __str
.c_str(),
5414 // NB: wcstof vs wcstod.
5416 stof(const wstring
& __str
, size_t* __idx
= 0)
5417 { return __gnu_cxx::__stoa(&std::wcstof
, "stof", __str
.c_str(), __idx
); }
5420 stod(const wstring
& __str
, size_t* __idx
= 0)
5421 { return __gnu_cxx::__stoa(&std::wcstod
, "stod", __str
.c_str(), __idx
); }
5424 stold(const wstring
& __str
, size_t* __idx
= 0)
5425 { return __gnu_cxx::__stoa(&std::wcstold
, "stold", __str
.c_str(), __idx
); }
5427 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
5430 to_wstring(int __val
)
5431 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(int),
5435 to_wstring(unsigned __val
)
5436 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
5437 4 * sizeof(unsigned),
5441 to_wstring(long __val
)
5442 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(long),
5446 to_wstring(unsigned long __val
)
5447 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
5448 4 * sizeof(unsigned long),
5452 to_wstring(long long __val
)
5453 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
5454 4 * sizeof(long long),
5458 to_wstring(unsigned long long __val
)
5459 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
5460 4 * sizeof(unsigned long long),
5464 to_wstring(float __val
)
5467 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
5468 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
5473 to_wstring(double __val
)
5476 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
5477 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
5482 to_wstring(long double __val
)
5485 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
5486 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
5489 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
5492 _GLIBCXX_END_NAMESPACE_CXX11
5493 _GLIBCXX_END_NAMESPACE_VERSION
5496 #endif /* C++11 && _GLIBCXX_USE_C99 ... */
5498 #if __cplusplus >= 201103L
5500 #include <bits/functional_hash.h>
5502 namespace std
_GLIBCXX_VISIBILITY(default)
5504 _GLIBCXX_BEGIN_NAMESPACE_VERSION
5508 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
5509 /// std::hash specialization for string.
5512 : public __hash_base
<size_t, string
>
5515 operator()(const string
& __s
) const noexcept
5516 { return std::_Hash_impl::hash(__s
.data(), __s
.length()); }
5520 struct __is_fast_hash
<hash
<string
>> : std::false_type
5523 #ifdef _GLIBCXX_USE_WCHAR_T
5524 /// std::hash specialization for wstring.
5526 struct hash
<wstring
>
5527 : public __hash_base
<size_t, wstring
>
5530 operator()(const wstring
& __s
) const noexcept
5531 { return std::_Hash_impl::hash(__s
.data(),
5532 __s
.length() * sizeof(wchar_t)); }
5536 struct __is_fast_hash
<hash
<wstring
>> : std::false_type
5539 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
5541 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
5542 /// std::hash specialization for u16string.
5544 struct hash
<u16string
>
5545 : public __hash_base
<size_t, u16string
>
5548 operator()(const u16string
& __s
) const noexcept
5549 { return std::_Hash_impl::hash(__s
.data(),
5550 __s
.length() * sizeof(char16_t
)); }
5554 struct __is_fast_hash
<hash
<u16string
>> : std::false_type
5557 /// std::hash specialization for u32string.
5559 struct hash
<u32string
>
5560 : public __hash_base
<size_t, u32string
>
5563 operator()(const u32string
& __s
) const noexcept
5564 { return std::_Hash_impl::hash(__s
.data(),
5565 __s
.length() * sizeof(char32_t
)); }
5569 struct __is_fast_hash
<hash
<u32string
>> : std::false_type
5573 #if __cplusplus > 201103L
5575 #define __cpp_lib_string_udls 201304
5577 inline namespace literals
5579 inline namespace string_literals
5582 _GLIBCXX_DEFAULT_ABI_TAG
5583 inline basic_string
<char>
5584 operator""s(const char* __str
, size_t __len
)
5585 { return basic_string
<char>{__str
, __len
}; }
5587 #ifdef _GLIBCXX_USE_WCHAR_T
5588 _GLIBCXX_DEFAULT_ABI_TAG
5589 inline basic_string
<wchar_t>
5590 operator""s(const wchar_t* __str
, size_t __len
)
5591 { return basic_string
<wchar_t>{__str
, __len
}; }
5594 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
5595 _GLIBCXX_DEFAULT_ABI_TAG
5596 inline basic_string
<char16_t
>
5597 operator""s(const char16_t
* __str
, size_t __len
)
5598 { return basic_string
<char16_t
>{__str
, __len
}; }
5600 _GLIBCXX_DEFAULT_ABI_TAG
5601 inline basic_string
<char32_t
>
5602 operator""s(const char32_t
* __str
, size_t __len
)
5603 { return basic_string
<char32_t
>{__str
, __len
}; }
5606 } // inline namespace string_literals
5607 } // inline namespace literals
5609 #endif // __cplusplus > 201103L
5611 _GLIBCXX_END_NAMESPACE_VERSION
5616 #endif /* _BASIC_STRING_H */