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>
42 #if __cplusplus >= 201103L
43 #include <initializer_list>
46 namespace std
_GLIBCXX_VISIBILITY(default)
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 #if _GLIBCXX_USE_CXX11_ABI
51 _GLIBCXX_BEGIN_NAMESPACE_CXX11
53 * @class basic_string basic_string.h <string>
54 * @brief Managing sequences of characters and character-like objects.
59 * @tparam _CharT Type of character
60 * @tparam _Traits Traits for character type, defaults to
61 * char_traits<_CharT>.
62 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
64 * Meets the requirements of a <a href="tables.html#65">container</a>, a
65 * <a href="tables.html#66">reversible container</a>, and a
66 * <a href="tables.html#67">sequence</a>. Of the
67 * <a href="tables.html#68">optional sequence requirements</a>, only
68 * @c push_back, @c at, and @c %array access are supported.
70 template<typename _CharT
, typename _Traits
, typename _Alloc
>
73 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
74 rebind
<_CharT
>::other _Char_alloc_type
;
75 typedef __gnu_cxx::__alloc_traits
<_Char_alloc_type
> _Alloc_traits
;
79 typedef _Traits traits_type
;
80 typedef typename
_Traits::char_type value_type
;
81 typedef _Char_alloc_type allocator_type
;
82 typedef typename
_Alloc_traits::size_type size_type
;
83 typedef typename
_Alloc_traits::difference_type difference_type
;
84 typedef typename
_Alloc_traits::reference reference
;
85 typedef typename
_Alloc_traits::const_reference const_reference
;
86 typedef typename
_Alloc_traits::pointer pointer
;
87 typedef typename
_Alloc_traits::const_pointer const_pointer
;
88 typedef __gnu_cxx::__normal_iterator
<pointer
, basic_string
> iterator
;
89 typedef __gnu_cxx::__normal_iterator
<const_pointer
, basic_string
>
91 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
92 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
94 /// Value returned by various member functions when they fail.
95 static const size_type npos
= static_cast<size_type
>(-1);
98 // type used for positions in insert, erase etc.
99 #if __cplusplus < 201103L
100 typedef iterator __const_iterator
;
102 typedef const_iterator __const_iterator
;
105 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
106 struct _Alloc_hider
: allocator_type
// TODO check __is_final
108 _Alloc_hider(pointer __dat
, const _Alloc
& __a
= _Alloc())
109 : allocator_type(__a
), _M_p(__dat
) { }
111 pointer _M_p
; // The actual data.
114 _Alloc_hider _M_dataplus
;
115 size_type _M_string_length
;
117 enum { _S_local_capacity
= 15 / sizeof(_CharT
) };
121 _CharT _M_local_buf
[_S_local_capacity
+ 1];
122 size_type _M_allocated_capacity
;
127 { _M_dataplus
._M_p
= __p
; }
130 _M_length(size_type __length
)
131 { _M_string_length
= __length
; }
135 { return _M_dataplus
._M_p
; }
140 #if __cplusplus >= 201103L
141 return std::pointer_traits
<pointer
>::pointer_to(*_M_local_buf
);
143 return pointer(_M_local_buf
);
148 _M_local_data() const
150 #if __cplusplus >= 201103L
151 return std::pointer_traits
<const_pointer
>::pointer_to(*_M_local_buf
);
153 return const_pointer(_M_local_buf
);
158 _M_capacity(size_type __capacity
)
159 { _M_allocated_capacity
= __capacity
; }
162 _M_set_length(size_type __n
)
165 traits_type::assign(_M_data()[__n
], _CharT());
170 { return _M_data() == _M_local_data(); }
174 _M_create(size_type
&, size_type
);
180 _M_destroy(_M_allocated_capacity
);
184 _M_destroy(size_type __size
) throw()
185 { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size
+ 1); }
187 // _M_construct_aux is used to implement the 21.3.1 para 15 which
188 // requires special behaviour if _InIterator is an integral type
189 template<typename _InIterator
>
191 _M_construct_aux(_InIterator __beg
, _InIterator __end
,
194 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
195 _M_construct(__beg
, __end
, _Tag());
198 // _GLIBCXX_RESOLVE_LIB_DEFECTS
199 // 438. Ambiguity in the "do the right thing" clause
200 template<typename _Integer
>
202 _M_construct_aux(_Integer __beg
, _Integer __end
, std::__true_type
)
203 { _M_construct_aux_2(static_cast<size_type
>(__beg
), __end
); }
206 _M_construct_aux_2(size_type __req
, _CharT __c
)
207 { _M_construct(__req
, __c
); }
209 template<typename _InIterator
>
211 _M_construct(_InIterator __beg
, _InIterator __end
)
213 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
214 _M_construct_aux(__beg
, __end
, _Integral());
217 // For Input Iterators, used in istreambuf_iterators, etc.
218 template<typename _InIterator
>
220 _M_construct(_InIterator __beg
, _InIterator __end
,
221 std::input_iterator_tag
);
223 // For forward_iterators up to random_access_iterators, used for
224 // string::iterator, _CharT*, etc.
225 template<typename _FwdIterator
>
227 _M_construct(_FwdIterator __beg
, _FwdIterator __end
,
228 std::forward_iterator_tag
);
231 _M_construct(size_type __req
, _CharT __c
);
235 { return _M_dataplus
; }
237 const allocator_type
&
238 _M_get_allocator() const
239 { return _M_dataplus
; }
243 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
244 // The explicit instantiations in misc-inst.cc require this due to
245 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
246 template<typename _Tp
, bool _Requires
=
247 !__are_same
<_Tp
, _CharT
*>::__value
248 && !__are_same
<_Tp
, const _CharT
*>::__value
249 && !__are_same
<_Tp
, iterator
>::__value
250 && !__are_same
<_Tp
, const_iterator
>::__value
>
251 struct __enable_if_not_native_iterator
252 { typedef basic_string
& __type
; };
253 template<typename _Tp
>
254 struct __enable_if_not_native_iterator
<_Tp
, false> { };
258 _M_check(size_type __pos
, const char* __s
) const
260 if (__pos
> this->size())
261 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
262 "this->size() (which is %zu)"),
263 __s
, __pos
, this->size());
268 _M_check_length(size_type __n1
, size_type __n2
, const char* __s
) const
270 if (this->max_size() - (this->size() - __n1
) < __n2
)
271 __throw_length_error(__N(__s
));
275 // NB: _M_limit doesn't check for a bad __pos value.
277 _M_limit(size_type __pos
, size_type __off
) const _GLIBCXX_NOEXCEPT
279 const bool __testoff
= __off
< this->size() - __pos
;
280 return __testoff
? __off
: this->size() - __pos
;
283 // True if _Rep and source do not overlap.
285 _M_disjunct(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
287 return (less
<const _CharT
*>()(__s
, _M_data())
288 || less
<const _CharT
*>()(_M_data() + this->size(), __s
));
291 // When __n = 1 way faster than the general multichar
292 // traits_type::copy/move/assign.
294 _S_copy(_CharT
* __d
, const _CharT
* __s
, size_type __n
)
297 traits_type::assign(*__d
, *__s
);
299 traits_type::copy(__d
, __s
, __n
);
303 _S_move(_CharT
* __d
, const _CharT
* __s
, size_type __n
)
306 traits_type::assign(*__d
, *__s
);
308 traits_type::move(__d
, __s
, __n
);
312 _S_assign(_CharT
* __d
, size_type __n
, _CharT __c
)
315 traits_type::assign(*__d
, __c
);
317 traits_type::assign(__d
, __n
, __c
);
320 // _S_copy_chars is a separate template to permit specialization
321 // to optimize for the common case of pointers as iterators.
322 template<class _Iterator
>
324 _S_copy_chars(_CharT
* __p
, _Iterator __k1
, _Iterator __k2
)
327 for (; __k1
!= __k2
; ++__k1
, ++__p
)
328 traits_type::assign(*__p
, *__k1
); // These types are off.
332 _S_copy_chars(_CharT
* __p
, iterator __k1
, iterator __k2
) _GLIBCXX_NOEXCEPT
333 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
336 _S_copy_chars(_CharT
* __p
, const_iterator __k1
, const_iterator __k2
)
338 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
341 _S_copy_chars(_CharT
* __p
, _CharT
* __k1
, _CharT
* __k2
) _GLIBCXX_NOEXCEPT
342 { _S_copy(__p
, __k1
, __k2
- __k1
); }
345 _S_copy_chars(_CharT
* __p
, const _CharT
* __k1
, const _CharT
* __k2
)
347 { _S_copy(__p
, __k1
, __k2
- __k1
); }
350 _S_compare(size_type __n1
, size_type __n2
) _GLIBCXX_NOEXCEPT
352 const difference_type __d
= difference_type(__n1
- __n2
);
354 if (__d
> __gnu_cxx::__numeric_traits
<int>::__max
)
355 return __gnu_cxx::__numeric_traits
<int>::__max
;
356 else if (__d
< __gnu_cxx::__numeric_traits
<int>::__min
)
357 return __gnu_cxx::__numeric_traits
<int>::__min
;
363 _M_assign(const basic_string
& __rcs
);
366 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
370 _M_erase(size_type __pos
, size_type __n
);
373 // Construct/copy/destroy:
374 // NB: We overload ctors in some cases instead of using default
375 // arguments, per 17.4.4.4 para. 2 item 2.
378 * @brief Default constructor creates an empty string.
380 basic_string() _GLIBCXX_NOEXCEPT
381 : _M_dataplus(_M_local_data())
382 { _M_set_length(0); }
385 * @brief Construct an empty string using allocator @a a.
388 basic_string(const _Alloc
& __a
)
389 : _M_dataplus(_M_local_data(), __a
)
390 { _M_set_length(0); }
393 * @brief Construct string with copy of value of @a __str.
394 * @param __str Source string.
396 basic_string(const basic_string
& __str
)
397 : _M_dataplus(_M_local_data(), __str
._M_get_allocator()) // TODO A traits
398 { _M_construct(__str
._M_data(), __str
._M_data() + __str
.length()); }
401 * @brief Construct string as copy of a substring.
402 * @param __str Source string.
403 * @param __pos Index of first character to copy from.
404 * @param __n Number of characters to copy (default remainder).
406 // _GLIBCXX_RESOLVE_LIB_DEFECTS
407 // 2402. [this constructor] shouldn't use Allocator()
408 basic_string(const basic_string
& __str
, size_type __pos
,
409 size_type __n
= npos
)
410 : _M_dataplus(_M_local_data())
412 const _CharT
* __start
= __str
._M_data()
413 + __str
._M_check(__pos
, "basic_string::basic_string");
414 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, __n
));
418 * @brief Construct string as copy of a substring.
419 * @param __str Source string.
420 * @param __pos Index of first character to copy from.
421 * @param __n Number of characters to copy (default remainder).
422 * @param __a Allocator to use.
424 basic_string(const basic_string
& __str
, size_type __pos
,
425 size_type __n
, const _Alloc
& __a
)
426 : _M_dataplus(_M_local_data(), __a
)
428 const _CharT
* __start
429 = __str
._M_data() + __str
._M_check(__pos
, "string::string");
430 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, __n
));
434 * @brief Construct string initialized by a character %array.
435 * @param __s Source character %array.
436 * @param __n Number of characters to copy.
437 * @param __a Allocator to use (default is default allocator).
439 * NB: @a __s must have at least @a __n characters, '\\0'
440 * has no special meaning.
442 basic_string(const _CharT
* __s
, size_type __n
,
443 const _Alloc
& __a
= _Alloc())
444 : _M_dataplus(_M_local_data(), __a
)
445 { _M_construct(__s
, __s
+ __n
); }
448 * @brief Construct string as copy of a C string.
449 * @param __s Source C string.
450 * @param __a Allocator to use (default is default allocator).
452 basic_string(const _CharT
* __s
, const _Alloc
& __a
= _Alloc())
453 : _M_dataplus(_M_local_data(), __a
)
454 { _M_construct(__s
, __s
? __s
+ traits_type::length(__s
) : __s
+npos
); }
457 * @brief Construct string as multiple characters.
458 * @param __n Number of characters.
459 * @param __c Character to use.
460 * @param __a Allocator to use (default is default allocator).
462 basic_string(size_type __n
, _CharT __c
, const _Alloc
& __a
= _Alloc())
463 : _M_dataplus(_M_local_data(), __a
)
464 { _M_construct(__n
, __c
); }
466 #if __cplusplus >= 201103L
468 * @brief Move construct string.
469 * @param __str Source string.
471 * The newly-created string contains the exact contents of @a __str.
472 * @a __str is a valid, but unspecified string.
474 basic_string(basic_string
&& __str
) noexcept
475 : _M_dataplus(_M_local_data(), std::move(__str
._M_get_allocator()))
477 if (__str
._M_is_local())
479 traits_type::copy(_M_local_buf
, __str
._M_local_buf
,
480 _S_local_capacity
+ 1);
484 _M_data(__str
._M_data());
485 _M_capacity(__str
._M_allocated_capacity
);
488 // Must use _M_length() here not _M_set_length() because
489 // basic_stringbuf relies on writing into unallocated capacity so
490 // we mess up the contents if we put a '\0' in the string.
491 _M_length(__str
.length());
492 __str
._M_data(__str
._M_local_data());
493 __str
._M_set_length(0);
497 * @brief Construct string from an initializer %list.
498 * @param __l std::initializer_list of characters.
499 * @param __a Allocator to use (default is default allocator).
501 basic_string(initializer_list
<_CharT
> __l
, const _Alloc
& __a
= _Alloc())
502 : _M_dataplus(_M_local_data(), __a
)
503 { _M_construct(__l
.begin(), __l
.end()); }
505 basic_string(const basic_string
& __str
, const _Alloc
& __a
)
506 : _M_dataplus(_M_local_data(), __a
)
507 { _M_construct(__str
.begin(), __str
.end()); }
509 basic_string(basic_string
&& __str
, const _Alloc
& __a
)
510 : _M_dataplus(_M_local_data(), __a
)
512 if (__str
.get_allocator() == __a
)
513 *this = std::move(__str
);
515 _M_construct(__str
.begin(), __str
.end());
521 * @brief Construct string as copy of a range.
522 * @param __beg Start of range.
523 * @param __end End of range.
524 * @param __a Allocator to use (default is default allocator).
526 #if __cplusplus >= 201103L
527 template<typename _InputIterator
,
528 typename
= std::_RequireInputIter
<_InputIterator
>>
530 template<typename _InputIterator
>
532 basic_string(_InputIterator __beg
, _InputIterator __end
,
533 const _Alloc
& __a
= _Alloc())
534 : _M_dataplus(_M_local_data(), __a
)
535 { _M_construct(__beg
, __end
); }
538 * @brief Destroy the string instance.
544 * @brief Assign the value of @a str to this string.
545 * @param __str Source string.
548 operator=(const basic_string
& __str
)
549 { return this->assign(__str
); }
552 * @brief Copy contents of @a s into this string.
553 * @param __s Source null-terminated string.
556 operator=(const _CharT
* __s
)
557 { return this->assign(__s
); }
560 * @brief Set value to string of length 1.
561 * @param __c Source character.
563 * Assigning to a character makes this string length 1 and
564 * (*this)[0] == @a c.
567 operator=(_CharT __c
)
569 this->assign(1, __c
);
573 #if __cplusplus >= 201103L
575 * @brief Move assign the value of @a str to this string.
576 * @param __str Source string.
578 * The contents of @a str are moved into this string (without copying).
579 * @a str is a valid, but unspecified string.
581 // PR 58265, this should be noexcept.
582 // _GLIBCXX_RESOLVE_LIB_DEFECTS
583 // 2063. Contradictory requirements for string move assignment
585 operator=(basic_string
&& __str
)
592 * @brief Set value to string constructed from initializer %list.
593 * @param __l std::initializer_list.
596 operator=(initializer_list
<_CharT
> __l
)
598 this->assign(__l
.begin(), __l
.size());
605 * Returns a read/write iterator that points to the first character in
609 begin() _GLIBCXX_NOEXCEPT
610 { return iterator(_M_data()); }
613 * Returns a read-only (constant) iterator that points to the first
614 * character in the %string.
617 begin() const _GLIBCXX_NOEXCEPT
618 { return const_iterator(_M_data()); }
621 * Returns a read/write iterator that points one past the last
622 * character in the %string.
625 end() _GLIBCXX_NOEXCEPT
626 { return iterator(_M_data() + this->size()); }
629 * Returns a read-only (constant) iterator that points one past the
630 * last character in the %string.
633 end() const _GLIBCXX_NOEXCEPT
634 { return const_iterator(_M_data() + this->size()); }
637 * Returns a read/write reverse iterator that points to the last
638 * character in the %string. Iteration is done in reverse element
642 rbegin() _GLIBCXX_NOEXCEPT
643 { return reverse_iterator(this->end()); }
646 * Returns a read-only (constant) reverse iterator that points
647 * to the last character in the %string. Iteration is done in
648 * reverse element order.
650 const_reverse_iterator
651 rbegin() const _GLIBCXX_NOEXCEPT
652 { return const_reverse_iterator(this->end()); }
655 * Returns a read/write reverse iterator that points to one before the
656 * first character in the %string. Iteration is done in reverse
660 rend() _GLIBCXX_NOEXCEPT
661 { return reverse_iterator(this->begin()); }
664 * Returns a read-only (constant) reverse iterator that points
665 * to one before the first character in the %string. Iteration
666 * is done in reverse element order.
668 const_reverse_iterator
669 rend() const _GLIBCXX_NOEXCEPT
670 { return const_reverse_iterator(this->begin()); }
672 #if __cplusplus >= 201103L
674 * Returns a read-only (constant) iterator that points to the first
675 * character in the %string.
678 cbegin() const noexcept
679 { return const_iterator(this->_M_data()); }
682 * Returns a read-only (constant) iterator that points one past the
683 * last character in the %string.
686 cend() const noexcept
687 { return const_iterator(this->_M_data() + this->size()); }
690 * Returns a read-only (constant) reverse iterator that points
691 * to the last character in the %string. Iteration is done in
692 * reverse element order.
694 const_reverse_iterator
695 crbegin() const noexcept
696 { return const_reverse_iterator(this->end()); }
699 * Returns a read-only (constant) reverse iterator that points
700 * to one before the first character in the %string. Iteration
701 * is done in reverse element order.
703 const_reverse_iterator
704 crend() const noexcept
705 { return const_reverse_iterator(this->begin()); }
710 /// Returns the number of characters in the string, not including any
711 /// null-termination.
713 size() const _GLIBCXX_NOEXCEPT
714 { return _M_string_length
; }
716 /// Returns the number of characters in the string, not including any
717 /// null-termination.
719 length() const _GLIBCXX_NOEXCEPT
720 { return _M_string_length
; }
722 /// Returns the size() of the largest possible %string.
724 max_size() const _GLIBCXX_NOEXCEPT
725 { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
728 * @brief Resizes the %string to the specified number of characters.
729 * @param __n Number of characters the %string should contain.
730 * @param __c Character to fill any new elements.
732 * This function will %resize the %string to the specified
733 * number of characters. If the number is smaller than the
734 * %string's current size the %string is truncated, otherwise
735 * the %string is extended and new elements are %set to @a __c.
738 resize(size_type __n
, _CharT __c
);
741 * @brief Resizes the %string to the specified number of characters.
742 * @param __n Number of characters the %string should contain.
744 * This function will resize the %string to the specified length. If
745 * the new size is smaller than the %string's current size the %string
746 * is truncated, otherwise the %string is extended and new characters
747 * are default-constructed. For basic types such as char, this means
751 resize(size_type __n
)
752 { this->resize(__n
, _CharT()); }
754 #if __cplusplus >= 201103L
755 /// A non-binding request to reduce capacity() to size().
757 shrink_to_fit() noexcept
759 if (capacity() > size())
770 * Returns the total number of characters that the %string can hold
771 * before needing to allocate more memory.
774 capacity() const _GLIBCXX_NOEXCEPT
776 return _M_is_local() ? size_type(_S_local_capacity
)
777 : _M_allocated_capacity
;
781 * @brief Attempt to preallocate enough memory for specified number of
783 * @param __res_arg Number of characters required.
784 * @throw std::length_error If @a __res_arg exceeds @c max_size().
786 * This function attempts to reserve enough memory for the
787 * %string to hold the specified number of characters. If the
788 * number requested is more than max_size(), length_error is
791 * The advantage of this function is that if optimal code is a
792 * necessity and the user can determine the string length that will be
793 * required, the user can reserve the memory in %advance, and thus
794 * prevent a possible reallocation of memory and copying of %string
798 reserve(size_type __res_arg
= 0);
801 * Erases the string, making it empty.
804 clear() _GLIBCXX_NOEXCEPT
805 { _M_set_length(0); }
808 * Returns true if the %string is empty. Equivalent to
809 * <code>*this == ""</code>.
812 empty() const _GLIBCXX_NOEXCEPT
813 { return this->size() == 0; }
817 * @brief Subscript access to the data contained in the %string.
818 * @param __pos The index of the character to access.
819 * @return Read-only (constant) reference to the character.
821 * This operator allows for easy, array-style, data access.
822 * Note that data access with this operator is unchecked and
823 * out_of_range lookups are not defined. (For checked lookups
827 operator[] (size_type __pos
) const _GLIBCXX_NOEXCEPT
829 _GLIBCXX_DEBUG_ASSERT(__pos
<= size());
830 return _M_data()[__pos
];
834 * @brief Subscript access to the data contained in the %string.
835 * @param __pos The index of the character to access.
836 * @return Read/write reference to the character.
838 * This operator allows for easy, array-style, data access.
839 * Note that data access with this operator is unchecked and
840 * out_of_range lookups are not defined. (For checked lookups
844 operator[](size_type __pos
)
846 // Allow pos == size() both in C++98 mode, as v3 extension,
847 // and in C++11 mode.
848 _GLIBCXX_DEBUG_ASSERT(__pos
<= size());
849 // In pedantic mode be strict in C++98 mode.
850 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus
>= 201103L || __pos
< size());
851 return _M_data()[__pos
];
855 * @brief Provides access to the data contained in the %string.
856 * @param __n The index of the character to access.
857 * @return Read-only (const) reference to the character.
858 * @throw std::out_of_range If @a n is an invalid index.
860 * This function provides for safer data access. The parameter is
861 * first checked that it is in the range of the string. The function
862 * throws out_of_range if the check fails.
865 at(size_type __n
) const
867 if (__n
>= this->size())
868 __throw_out_of_range_fmt(__N("basic_string::at: __n "
869 "(which is %zu) >= this->size() "
872 return _M_data()[__n
];
876 * @brief Provides access to the data contained in the %string.
877 * @param __n The index of the character to access.
878 * @return Read/write reference to the character.
879 * @throw std::out_of_range If @a n is an invalid index.
881 * This function provides for safer data access. The parameter is
882 * first checked that it is in the range of the string. The function
883 * throws out_of_range if the check fails.
889 __throw_out_of_range_fmt(__N("basic_string::at: __n "
890 "(which is %zu) >= this->size() "
893 return _M_data()[__n
];
896 #if __cplusplus >= 201103L
898 * Returns a read/write reference to the data at the first
899 * element of the %string.
903 { return operator[](0); }
906 * Returns a read-only (constant) reference to the data at the first
907 * element of the %string.
910 front() const noexcept
911 { return operator[](0); }
914 * Returns a read/write reference to the data at the last
915 * element of the %string.
919 { return operator[](this->size() - 1); }
922 * Returns a read-only (constant) reference to the data at the
923 * last element of the %string.
926 back() const noexcept
927 { return operator[](this->size() - 1); }
932 * @brief Append a string to this string.
933 * @param __str The string to append.
934 * @return Reference to this string.
937 operator+=(const basic_string
& __str
)
938 { return this->append(__str
); }
941 * @brief Append a C string.
942 * @param __s The C string to append.
943 * @return Reference to this string.
946 operator+=(const _CharT
* __s
)
947 { return this->append(__s
); }
950 * @brief Append a character.
951 * @param __c The character to append.
952 * @return Reference to this string.
955 operator+=(_CharT __c
)
957 this->push_back(__c
);
961 #if __cplusplus >= 201103L
963 * @brief Append an initializer_list of characters.
964 * @param __l The initializer_list of characters to be appended.
965 * @return Reference to this string.
968 operator+=(initializer_list
<_CharT
> __l
)
969 { return this->append(__l
.begin(), __l
.size()); }
973 * @brief Append a string to this string.
974 * @param __str The string to append.
975 * @return Reference to this string.
978 append(const basic_string
& __str
)
979 { return _M_append(__str
._M_data(), __str
.size()); }
982 * @brief Append a substring.
983 * @param __str The string to append.
984 * @param __pos Index of the first character of str to append.
985 * @param __n The number of characters to append.
986 * @return Reference to this string.
987 * @throw std::out_of_range if @a __pos is not a valid index.
989 * This function appends @a __n characters from @a __str
990 * starting at @a __pos to this string. If @a __n is is larger
991 * than the number of available characters in @a __str, the
992 * remainder of @a __str is appended.
995 append(const basic_string
& __str
, size_type __pos
, size_type __n
)
996 { return _M_append(__str
._M_data()
997 + __str
._M_check(__pos
, "basic_string::append"),
998 __str
._M_limit(__pos
, __n
)); }
1001 * @brief Append a C substring.
1002 * @param __s The C string to append.
1003 * @param __n The number of characters to append.
1004 * @return Reference to this string.
1007 append(const _CharT
* __s
, size_type __n
)
1009 __glibcxx_requires_string_len(__s
, __n
);
1010 _M_check_length(size_type(0), __n
, "basic_string::append");
1011 return _M_append(__s
, __n
);
1015 * @brief Append a C string.
1016 * @param __s The C string to append.
1017 * @return Reference to this string.
1020 append(const _CharT
* __s
)
1022 __glibcxx_requires_string(__s
);
1023 const size_type __n
= traits_type::length(__s
);
1024 _M_check_length(size_type(0), __n
, "basic_string::append");
1025 return _M_append(__s
, __n
);
1029 * @brief Append multiple characters.
1030 * @param __n The number of characters to append.
1031 * @param __c The character to use.
1032 * @return Reference to this string.
1034 * Appends __n copies of __c to this string.
1037 append(size_type __n
, _CharT __c
)
1038 { return _M_replace_aux(this->size(), size_type(0), __n
, __c
); }
1040 #if __cplusplus >= 201103L
1042 * @brief Append an initializer_list of characters.
1043 * @param __l The initializer_list of characters to append.
1044 * @return Reference to this string.
1047 append(initializer_list
<_CharT
> __l
)
1048 { return this->append(__l
.begin(), __l
.size()); }
1052 * @brief Append a range of characters.
1053 * @param __first Iterator referencing the first character to append.
1054 * @param __last Iterator marking the end of the range.
1055 * @return Reference to this string.
1057 * Appends characters in the range [__first,__last) to this string.
1059 #if __cplusplus >= 201103L
1060 template<class _InputIterator
,
1061 typename
= std::_RequireInputIter
<_InputIterator
>>
1063 template<class _InputIterator
>
1066 append(_InputIterator __first
, _InputIterator __last
)
1067 { return this->replace(end(), end(), __first
, __last
); }
1070 * @brief Append a single character.
1071 * @param __c Character to append.
1074 push_back(_CharT __c
)
1076 const size_type __size
= this->size();
1077 if (__size
+ 1 > this->capacity())
1078 this->_M_mutate(__size
, size_type(0), 0, size_type(1));
1079 traits_type::assign(this->_M_data()[__size
], __c
);
1080 this->_M_set_length(__size
+ 1);
1084 * @brief Set value to contents of another string.
1085 * @param __str Source string to use.
1086 * @return Reference to this string.
1089 assign(const basic_string
& __str
)
1091 this->_M_assign(__str
);
1095 #if __cplusplus >= 201103L
1097 * @brief Set value to contents of another string.
1098 * @param __str Source string to use.
1099 * @return Reference to this string.
1101 * This function sets this string to the exact contents of @a __str.
1102 * @a __str is a valid, but unspecified string.
1105 assign(basic_string
&& __str
)
1107 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1108 // 2063. Contradictory requirements for string move assignment
1109 return *this = std::move(__str
);
1114 * @brief Set value to a substring of a string.
1115 * @param __str The string to use.
1116 * @param __pos Index of the first character of str.
1117 * @param __n Number of characters to use.
1118 * @return Reference to this string.
1119 * @throw std::out_of_range if @a pos is not a valid index.
1121 * This function sets this string to the substring of @a __str
1122 * consisting of @a __n characters at @a __pos. If @a __n is
1123 * is larger than the number of available characters in @a
1124 * __str, the remainder of @a __str is used.
1127 assign(const basic_string
& __str
, size_type __pos
, size_type __n
)
1128 { return _M_replace(size_type(0), this->size(), __str
._M_data()
1129 + __str
._M_check(__pos
, "basic_string::assign"),
1130 __str
._M_limit(__pos
, __n
)); }
1133 * @brief Set value to a C substring.
1134 * @param __s The C string to use.
1135 * @param __n Number of characters to use.
1136 * @return Reference to this string.
1138 * This function sets the value of this string to the first @a __n
1139 * characters of @a __s. If @a __n is is larger than the number of
1140 * available characters in @a __s, the remainder of @a __s is used.
1143 assign(const _CharT
* __s
, size_type __n
)
1145 __glibcxx_requires_string_len(__s
, __n
);
1146 return _M_replace(size_type(0), this->size(), __s
, __n
);
1150 * @brief Set value to contents of a C string.
1151 * @param __s The C string to use.
1152 * @return Reference to this string.
1154 * This function sets the value of this string to the value of @a __s.
1155 * The data is copied, so there is no dependence on @a __s once the
1159 assign(const _CharT
* __s
)
1161 __glibcxx_requires_string(__s
);
1162 return _M_replace(size_type(0), this->size(), __s
,
1163 traits_type::length(__s
));
1167 * @brief Set value to multiple characters.
1168 * @param __n Length of the resulting string.
1169 * @param __c The character to use.
1170 * @return Reference to this string.
1172 * This function sets the value of this string to @a __n copies of
1176 assign(size_type __n
, _CharT __c
)
1177 { return _M_replace_aux(size_type(0), this->size(), __n
, __c
); }
1180 * @brief Set value to a range of characters.
1181 * @param __first Iterator referencing the first character to append.
1182 * @param __last Iterator marking the end of the range.
1183 * @return Reference to this string.
1185 * Sets value of string to characters in the range [__first,__last).
1187 #if __cplusplus >= 201103L
1188 template<class _InputIterator
,
1189 typename
= std::_RequireInputIter
<_InputIterator
>>
1191 template<class _InputIterator
>
1194 assign(_InputIterator __first
, _InputIterator __last
)
1195 { return this->replace(begin(), end(), __first
, __last
); }
1197 #if __cplusplus >= 201103L
1199 * @brief Set value to an initializer_list of characters.
1200 * @param __l The initializer_list of characters to assign.
1201 * @return Reference to this string.
1204 assign(initializer_list
<_CharT
> __l
)
1205 { return this->assign(__l
.begin(), __l
.size()); }
1208 #if __cplusplus >= 201103L
1210 * @brief Insert multiple characters.
1211 * @param __p Const_iterator referencing location in string to
1213 * @param __n Number of characters to insert
1214 * @param __c The character to insert.
1215 * @return Iterator referencing the first inserted char.
1216 * @throw std::length_error If new length exceeds @c max_size().
1218 * Inserts @a __n copies of character @a __c starting at the
1219 * position referenced by iterator @a __p. If adding
1220 * characters causes the length to exceed max_size(),
1221 * length_error is thrown. The value of the string doesn't
1222 * change if an error is thrown.
1225 insert(const_iterator __p
, size_type __n
, _CharT __c
)
1227 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1228 const size_type __pos
= __p
- begin();
1229 this->replace(__p
, __p
, __n
, __c
);
1230 return iterator(this->_M_data() + __pos
);
1234 * @brief Insert multiple characters.
1235 * @param __p Iterator referencing location in string to insert at.
1236 * @param __n Number of characters to insert
1237 * @param __c The character to insert.
1238 * @throw std::length_error If new length exceeds @c max_size().
1240 * Inserts @a __n copies of character @a __c starting at the
1241 * position referenced by iterator @a __p. If adding
1242 * characters causes the length to exceed max_size(),
1243 * length_error is thrown. The value of the string doesn't
1244 * change if an error is thrown.
1247 insert(iterator __p
, size_type __n
, _CharT __c
)
1248 { this->replace(__p
, __p
, __n
, __c
); }
1251 #if __cplusplus >= 201103L
1253 * @brief Insert a range of characters.
1254 * @param __p Const_iterator referencing location in string to
1256 * @param __beg Start of range.
1257 * @param __end End of range.
1258 * @return Iterator referencing the first inserted char.
1259 * @throw std::length_error If new length exceeds @c max_size().
1261 * Inserts characters in range [beg,end). If adding characters
1262 * causes the length to exceed max_size(), length_error is
1263 * thrown. The value of the string doesn't change if an error
1266 template<class _InputIterator
,
1267 typename
= std::_RequireInputIter
<_InputIterator
>>
1269 insert(const_iterator __p
, _InputIterator __beg
, _InputIterator __end
)
1271 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1272 const size_type __pos
= __p
- begin();
1273 this->replace(__p
, __p
, __beg
, __end
);
1274 return iterator(this->_M_data() + __pos
);
1278 * @brief Insert a range of characters.
1279 * @param __p Iterator referencing location in string to insert at.
1280 * @param __beg Start of range.
1281 * @param __end End of range.
1282 * @throw std::length_error If new length exceeds @c max_size().
1284 * Inserts characters in range [__beg,__end). If adding
1285 * characters causes the length to exceed max_size(),
1286 * length_error is thrown. The value of the string doesn't
1287 * change if an error is thrown.
1289 template<class _InputIterator
>
1291 insert(iterator __p
, _InputIterator __beg
, _InputIterator __end
)
1292 { this->replace(__p
, __p
, __beg
, __end
); }
1295 #if __cplusplus >= 201103L
1297 * @brief Insert an initializer_list of characters.
1298 * @param __p Iterator referencing location in string to insert at.
1299 * @param __l The initializer_list of characters to insert.
1300 * @throw std::length_error If new length exceeds @c max_size().
1303 insert(iterator __p
, initializer_list
<_CharT
> __l
)
1305 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1306 this->insert(__p
- begin(), __l
.begin(), __l
.size());
1311 * @brief Insert value of a string.
1312 * @param __pos1 Iterator referencing location in string to insert at.
1313 * @param __str The string to insert.
1314 * @return Reference to this string.
1315 * @throw std::length_error If new length exceeds @c max_size().
1317 * Inserts value of @a __str starting at @a __pos1. If adding
1318 * characters causes the length to exceed max_size(),
1319 * length_error is thrown. The value of the string doesn't
1320 * change if an error is thrown.
1323 insert(size_type __pos1
, const basic_string
& __str
)
1324 { return this->replace(__pos1
, size_type(0),
1325 __str
._M_data(), __str
.size()); }
1328 * @brief Insert a substring.
1329 * @param __pos1 Iterator referencing location in string to insert at.
1330 * @param __str The string to insert.
1331 * @param __pos2 Start of characters in str to insert.
1332 * @param __n Number of characters to insert.
1333 * @return Reference to this string.
1334 * @throw std::length_error If new length exceeds @c max_size().
1335 * @throw std::out_of_range If @a pos1 > size() or
1336 * @a __pos2 > @a str.size().
1338 * Starting at @a pos1, insert @a __n character of @a __str
1339 * beginning with @a __pos2. If adding characters causes the
1340 * length to exceed max_size(), length_error is thrown. If @a
1341 * __pos1 is beyond the end of this string or @a __pos2 is
1342 * beyond the end of @a __str, out_of_range is thrown. The
1343 * value of the string doesn't change if an error is thrown.
1346 insert(size_type __pos1
, const basic_string
& __str
,
1347 size_type __pos2
, size_type __n
)
1348 { return this->replace(__pos1
, size_type(0), __str
._M_data()
1349 + __str
._M_check(__pos2
, "basic_string::insert"),
1350 __str
._M_limit(__pos2
, __n
)); }
1353 * @brief Insert a C substring.
1354 * @param __pos Iterator referencing location in string to insert at.
1355 * @param __s The C string to insert.
1356 * @param __n The number of characters to insert.
1357 * @return Reference to this string.
1358 * @throw std::length_error If new length exceeds @c max_size().
1359 * @throw std::out_of_range If @a __pos is beyond the end of this
1362 * Inserts the first @a __n characters of @a __s starting at @a
1363 * __pos. If adding characters causes the length to exceed
1364 * max_size(), length_error is thrown. If @a __pos is beyond
1365 * end(), out_of_range is thrown. The value of the string
1366 * doesn't change if an error is thrown.
1369 insert(size_type __pos
, const _CharT
* __s
, size_type __n
)
1370 { return this->replace(__pos
, size_type(0), __s
, __n
); }
1373 * @brief Insert a C string.
1374 * @param __pos Iterator referencing location in string to insert at.
1375 * @param __s The C string to insert.
1376 * @return Reference to this string.
1377 * @throw std::length_error If new length exceeds @c max_size().
1378 * @throw std::out_of_range If @a pos is beyond the end of this
1381 * Inserts the first @a n characters of @a __s starting at @a __pos. If
1382 * adding characters causes the length to exceed max_size(),
1383 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
1384 * thrown. The value of the string doesn't change if an error is
1388 insert(size_type __pos
, const _CharT
* __s
)
1390 __glibcxx_requires_string(__s
);
1391 return this->replace(__pos
, size_type(0), __s
,
1392 traits_type::length(__s
));
1396 * @brief Insert multiple characters.
1397 * @param __pos Index in string to insert at.
1398 * @param __n Number of characters to insert
1399 * @param __c The character to insert.
1400 * @return Reference to this string.
1401 * @throw std::length_error If new length exceeds @c max_size().
1402 * @throw std::out_of_range If @a __pos is beyond the end of this
1405 * Inserts @a __n copies of character @a __c starting at index
1406 * @a __pos. If adding characters causes the length to exceed
1407 * max_size(), length_error is thrown. If @a __pos > length(),
1408 * out_of_range is thrown. The value of the string doesn't
1409 * change if an error is thrown.
1412 insert(size_type __pos
, size_type __n
, _CharT __c
)
1413 { return _M_replace_aux(_M_check(__pos
, "basic_string::insert"),
1414 size_type(0), __n
, __c
); }
1417 * @brief Insert one character.
1418 * @param __p Iterator referencing position in string to insert at.
1419 * @param __c The character to insert.
1420 * @return Iterator referencing newly inserted char.
1421 * @throw std::length_error If new length exceeds @c max_size().
1423 * Inserts character @a __c at position referenced by @a __p.
1424 * If adding character causes the length to exceed max_size(),
1425 * length_error is thrown. If @a __p is beyond end of string,
1426 * out_of_range is thrown. The value of the string doesn't
1427 * change if an error is thrown.
1430 insert(__const_iterator __p
, _CharT __c
)
1432 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1433 const size_type __pos
= __p
- begin();
1434 _M_replace_aux(__pos
, size_type(0), size_type(1), __c
);
1435 return iterator(_M_data() + __pos
);
1439 * @brief Remove characters.
1440 * @param __pos Index of first character to remove (default 0).
1441 * @param __n Number of characters to remove (default remainder).
1442 * @return Reference to this string.
1443 * @throw std::out_of_range If @a pos is beyond the end of this
1446 * Removes @a __n characters from this string starting at @a
1447 * __pos. The length of the string is reduced by @a __n. If
1448 * there are < @a __n characters to remove, the remainder of
1449 * the string is truncated. If @a __p is beyond end of string,
1450 * out_of_range is thrown. The value of the string doesn't
1451 * change if an error is thrown.
1454 erase(size_type __pos
= 0, size_type __n
= npos
)
1456 this->_M_erase(_M_check(__pos
, "basic_string::erase"),
1457 _M_limit(__pos
, __n
));
1462 * @brief Remove one character.
1463 * @param __position Iterator referencing the character to remove.
1464 * @return iterator referencing same location after removal.
1466 * Removes the character at @a __position from this string. The value
1467 * of the string doesn't change if an error is thrown.
1470 erase(__const_iterator __position
)
1472 _GLIBCXX_DEBUG_PEDASSERT(__position
>= begin()
1473 && __position
< end());
1474 const size_type __pos
= __position
- begin();
1475 this->_M_erase(__pos
, size_type(1));
1476 return iterator(_M_data() + __pos
);
1480 * @brief Remove a range of characters.
1481 * @param __first Iterator referencing the first character to remove.
1482 * @param __last Iterator referencing the end of the range.
1483 * @return Iterator referencing location of first after removal.
1485 * Removes the characters in the range [first,last) from this string.
1486 * The value of the string doesn't change if an error is thrown.
1489 erase(__const_iterator __first
, __const_iterator __last
)
1491 _GLIBCXX_DEBUG_PEDASSERT(__first
>= begin() && __first
<= __last
1492 && __last
<= end());
1493 const size_type __pos
= __first
- begin();
1494 this->_M_erase(__pos
, __last
- __first
);
1495 return iterator(this->_M_data() + __pos
);
1498 #if __cplusplus >= 201103L
1500 * @brief Remove the last character.
1502 * The string must be non-empty.
1506 { _M_erase(size()-1, 1); }
1510 * @brief Replace characters with value from another string.
1511 * @param __pos Index of first character to replace.
1512 * @param __n Number of characters to be replaced.
1513 * @param __str String to insert.
1514 * @return Reference to this string.
1515 * @throw std::out_of_range If @a pos is beyond the end of this
1517 * @throw std::length_error If new length exceeds @c max_size().
1519 * Removes the characters in the range [__pos,__pos+__n) from
1520 * this string. In place, the value of @a __str is inserted.
1521 * If @a __pos is beyond end of string, out_of_range is thrown.
1522 * If the length of the result exceeds max_size(), length_error
1523 * is thrown. The value of the string doesn't change if an
1527 replace(size_type __pos
, size_type __n
, const basic_string
& __str
)
1528 { return this->replace(__pos
, __n
, __str
._M_data(), __str
.size()); }
1531 * @brief Replace characters with value from another string.
1532 * @param __pos1 Index of first character to replace.
1533 * @param __n1 Number of characters to be replaced.
1534 * @param __str String to insert.
1535 * @param __pos2 Index of first character of str to use.
1536 * @param __n2 Number of characters from str to use.
1537 * @return Reference to this string.
1538 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
1540 * @throw std::length_error If new length exceeds @c max_size().
1542 * Removes the characters in the range [__pos1,__pos1 + n) from this
1543 * string. In place, the value of @a __str is inserted. If @a __pos is
1544 * beyond end of string, out_of_range is thrown. If the length of the
1545 * result exceeds max_size(), length_error is thrown. The value of the
1546 * string doesn't change if an error is thrown.
1549 replace(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
1550 size_type __pos2
, size_type __n2
)
1551 { return this->replace(__pos1
, __n1
, __str
._M_data()
1552 + __str
._M_check(__pos2
, "basic_string::replace"),
1553 __str
._M_limit(__pos2
, __n2
)); }
1556 * @brief Replace characters with value of a C substring.
1557 * @param __pos Index of first character to replace.
1558 * @param __n1 Number of characters to be replaced.
1559 * @param __s C string to insert.
1560 * @param __n2 Number of characters from @a s to use.
1561 * @return Reference to this string.
1562 * @throw std::out_of_range If @a pos1 > size().
1563 * @throw std::length_error If new length exceeds @c max_size().
1565 * Removes the characters in the range [__pos,__pos + __n1)
1566 * from this string. In place, the first @a __n2 characters of
1567 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
1568 * @a __pos is beyond end of string, out_of_range is thrown. If
1569 * the length of result exceeds max_size(), length_error is
1570 * thrown. The value of the string doesn't change if an error
1574 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
1577 __glibcxx_requires_string_len(__s
, __n2
);
1578 return _M_replace(_M_check(__pos
, "basic_string::replace"),
1579 _M_limit(__pos
, __n1
), __s
, __n2
);
1583 * @brief Replace characters with value of a C string.
1584 * @param __pos Index of first character to replace.
1585 * @param __n1 Number of characters to be replaced.
1586 * @param __s C string to insert.
1587 * @return Reference to this string.
1588 * @throw std::out_of_range If @a pos > size().
1589 * @throw std::length_error If new length exceeds @c max_size().
1591 * Removes the characters in the range [__pos,__pos + __n1)
1592 * from this string. In place, the characters of @a __s are
1593 * inserted. If @a __pos is beyond end of string, out_of_range
1594 * is thrown. If the length of result exceeds max_size(),
1595 * length_error is thrown. The value of the string doesn't
1596 * change if an error is thrown.
1599 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
)
1601 __glibcxx_requires_string(__s
);
1602 return this->replace(__pos
, __n1
, __s
, traits_type::length(__s
));
1606 * @brief Replace characters with multiple characters.
1607 * @param __pos Index of first character to replace.
1608 * @param __n1 Number of characters to be replaced.
1609 * @param __n2 Number of characters to insert.
1610 * @param __c Character to insert.
1611 * @return Reference to this string.
1612 * @throw std::out_of_range If @a __pos > size().
1613 * @throw std::length_error If new length exceeds @c max_size().
1615 * Removes the characters in the range [pos,pos + n1) from this
1616 * string. In place, @a __n2 copies of @a __c are inserted.
1617 * If @a __pos is beyond end of string, out_of_range is thrown.
1618 * If the length of result exceeds max_size(), length_error is
1619 * thrown. The value of the string doesn't change if an error
1623 replace(size_type __pos
, size_type __n1
, size_type __n2
, _CharT __c
)
1624 { return _M_replace_aux(_M_check(__pos
, "basic_string::replace"),
1625 _M_limit(__pos
, __n1
), __n2
, __c
); }
1628 * @brief Replace range of characters with string.
1629 * @param __i1 Iterator referencing start of range to replace.
1630 * @param __i2 Iterator referencing end of range to replace.
1631 * @param __str String value to insert.
1632 * @return Reference to this string.
1633 * @throw std::length_error If new length exceeds @c max_size().
1635 * Removes the characters in the range [__i1,__i2). In place,
1636 * the value of @a __str is inserted. If the length of result
1637 * exceeds max_size(), length_error is thrown. The value of
1638 * the string doesn't change if an error is thrown.
1641 replace(__const_iterator __i1
, __const_iterator __i2
,
1642 const basic_string
& __str
)
1643 { return this->replace(__i1
, __i2
, __str
._M_data(), __str
.size()); }
1646 * @brief Replace range of characters with C substring.
1647 * @param __i1 Iterator referencing start of range to replace.
1648 * @param __i2 Iterator referencing end of range to replace.
1649 * @param __s C string value to insert.
1650 * @param __n Number of characters from s 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 first @a __n characters of @a __s are inserted. If the
1656 * length of result exceeds max_size(), length_error is thrown.
1657 * The value of the string doesn't change if an error is
1661 replace(__const_iterator __i1
, __const_iterator __i2
,
1662 const _CharT
* __s
, size_type __n
)
1664 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
1666 return this->replace(__i1
- begin(), __i2
- __i1
, __s
, __n
);
1670 * @brief Replace range of characters with C string.
1671 * @param __i1 Iterator referencing start of range to replace.
1672 * @param __i2 Iterator referencing end of range to replace.
1673 * @param __s C string value to insert.
1674 * @return Reference to this string.
1675 * @throw std::length_error If new length exceeds @c max_size().
1677 * Removes the characters in the range [__i1,__i2). In place,
1678 * the characters of @a __s are inserted. If the length of
1679 * result exceeds max_size(), length_error is thrown. The
1680 * value of the string doesn't change if an error is thrown.
1683 replace(__const_iterator __i1
, __const_iterator __i2
, const _CharT
* __s
)
1685 __glibcxx_requires_string(__s
);
1686 return this->replace(__i1
, __i2
, __s
, traits_type::length(__s
));
1690 * @brief Replace range of characters with multiple characters
1691 * @param __i1 Iterator referencing start of range to replace.
1692 * @param __i2 Iterator referencing end of range to replace.
1693 * @param __n Number of characters to insert.
1694 * @param __c Character to insert.
1695 * @return Reference to this string.
1696 * @throw std::length_error If new length exceeds @c max_size().
1698 * Removes the characters in the range [__i1,__i2). In place,
1699 * @a __n copies of @a __c are inserted. If the length of
1700 * result exceeds max_size(), length_error is thrown. The
1701 * value of the string doesn't change if an error is thrown.
1704 replace(__const_iterator __i1
, __const_iterator __i2
, size_type __n
,
1707 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
1709 return _M_replace_aux(__i1
- begin(), __i2
- __i1
, __n
, __c
);
1713 * @brief Replace range of characters with range.
1714 * @param __i1 Iterator referencing start of range to replace.
1715 * @param __i2 Iterator referencing end of range to replace.
1716 * @param __k1 Iterator referencing start of range to insert.
1717 * @param __k2 Iterator referencing end of range to insert.
1718 * @return Reference to this string.
1719 * @throw std::length_error If new length exceeds @c max_size().
1721 * Removes the characters in the range [__i1,__i2). In place,
1722 * characters in the range [__k1,__k2) are inserted. If the
1723 * length of result exceeds max_size(), length_error is thrown.
1724 * The value of the string doesn't change if an error is
1727 #if __cplusplus >= 201103L
1728 template<class _InputIterator
,
1729 typename
= std::_RequireInputIter
<_InputIterator
>>
1731 replace(const_iterator __i1
, const_iterator __i2
,
1732 _InputIterator __k1
, _InputIterator __k2
)
1734 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
1736 __glibcxx_requires_valid_range(__k1
, __k2
);
1737 return this->_M_replace_dispatch(__i1
, __i2
, __k1
, __k2
,
1738 std::__false_type());
1741 template<class _InputIterator
>
1742 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
1743 typename __enable_if_not_native_iterator
<_InputIterator
>::__type
1747 replace(iterator __i1
, iterator __i2
,
1748 _InputIterator __k1
, _InputIterator __k2
)
1750 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
1752 __glibcxx_requires_valid_range(__k1
, __k2
);
1753 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
1754 return _M_replace_dispatch(__i1
, __i2
, __k1
, __k2
, _Integral());
1758 // Specializations for the common case of pointer and iterator:
1759 // useful to avoid the overhead of temporary buffering in _M_replace.
1761 replace(__const_iterator __i1
, __const_iterator __i2
,
1762 _CharT
* __k1
, _CharT
* __k2
)
1764 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
1766 __glibcxx_requires_valid_range(__k1
, __k2
);
1767 return this->replace(__i1
- begin(), __i2
- __i1
,
1772 replace(__const_iterator __i1
, __const_iterator __i2
,
1773 const _CharT
* __k1
, const _CharT
* __k2
)
1775 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
1777 __glibcxx_requires_valid_range(__k1
, __k2
);
1778 return this->replace(__i1
- begin(), __i2
- __i1
,
1783 replace(__const_iterator __i1
, __const_iterator __i2
,
1784 iterator __k1
, iterator __k2
)
1786 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
1788 __glibcxx_requires_valid_range(__k1
, __k2
);
1789 return this->replace(__i1
- begin(), __i2
- __i1
,
1790 __k1
.base(), __k2
- __k1
);
1794 replace(__const_iterator __i1
, __const_iterator __i2
,
1795 const_iterator __k1
, const_iterator __k2
)
1797 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
1799 __glibcxx_requires_valid_range(__k1
, __k2
);
1800 return this->replace(__i1
- begin(), __i2
- __i1
,
1801 __k1
.base(), __k2
- __k1
);
1804 #if __cplusplus >= 201103L
1806 * @brief Replace range of characters with initializer_list.
1807 * @param __i1 Iterator referencing start of range to replace.
1808 * @param __i2 Iterator referencing end of range to replace.
1809 * @param __l The initializer_list of characters to insert.
1810 * @return Reference to this string.
1811 * @throw std::length_error If new length exceeds @c max_size().
1813 * Removes the characters in the range [__i1,__i2). In place,
1814 * characters in the range [__k1,__k2) are inserted. If the
1815 * length of result exceeds max_size(), length_error is thrown.
1816 * The value of the string doesn't change if an error is
1819 basic_string
& replace(const_iterator __i1
, const_iterator __i2
,
1820 initializer_list
<_CharT
> __l
)
1821 { return this->replace(__i1
, __i2
, __l
.begin(), __l
.end()); }
1825 template<class _Integer
>
1827 _M_replace_dispatch(const_iterator __i1
, const_iterator __i2
,
1828 _Integer __n
, _Integer __val
, __true_type
)
1829 { return _M_replace_aux(__i1
- begin(), __i2
- __i1
, __n
, __val
); }
1831 template<class _InputIterator
>
1833 _M_replace_dispatch(const_iterator __i1
, const_iterator __i2
,
1834 _InputIterator __k1
, _InputIterator __k2
,
1838 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
1842 _M_replace(size_type __pos
, size_type __len1
, const _CharT
* __s
,
1843 const size_type __len2
);
1846 _M_append(const _CharT
* __s
, size_type __n
);
1851 * @brief Copy substring into C string.
1852 * @param __s C string to copy value into.
1853 * @param __n Number of characters to copy.
1854 * @param __pos Index of first character to copy.
1855 * @return Number of characters actually copied
1856 * @throw std::out_of_range If __pos > size().
1858 * Copies up to @a __n characters starting at @a __pos into the
1859 * C string @a __s. If @a __pos is %greater than size(),
1860 * out_of_range is thrown.
1863 copy(_CharT
* __s
, size_type __n
, size_type __pos
= 0) const;
1866 * @brief Swap contents with another string.
1867 * @param __s String to swap with.
1869 * Exchanges the contents of this string with that of @a __s in constant
1873 swap(basic_string
& __s
) _GLIBCXX_NOEXCEPT
;
1875 // String operations:
1877 * @brief Return const pointer to null-terminated contents.
1879 * This is a handle to internal data. Do not modify or dire things may
1883 c_str() const _GLIBCXX_NOEXCEPT
1884 { return _M_data(); }
1887 * @brief Return const pointer to contents.
1889 * This is a handle to internal data. Do not modify or dire things may
1893 data() const _GLIBCXX_NOEXCEPT
1894 { return _M_data(); }
1897 * @brief Return copy of allocator used to construct this string.
1900 get_allocator() const _GLIBCXX_NOEXCEPT
1901 { return _M_get_allocator(); }
1904 * @brief Find position of a C substring.
1905 * @param __s C string to locate.
1906 * @param __pos Index of character to search from.
1907 * @param __n Number of characters from @a s to search for.
1908 * @return Index of start of first occurrence.
1910 * Starting from @a __pos, searches forward for the first @a
1911 * __n characters in @a __s within this string. If found,
1912 * returns the index where it begins. If not found, returns
1916 find(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
1919 * @brief Find position of a string.
1920 * @param __str String to locate.
1921 * @param __pos Index of character to search from (default 0).
1922 * @return Index of start of first occurrence.
1924 * Starting from @a __pos, searches forward for value of @a __str within
1925 * this string. If found, returns the index where it begins. If not
1926 * found, returns npos.
1929 find(const basic_string
& __str
, size_type __pos
= 0) const
1931 { return this->find(__str
.data(), __pos
, __str
.size()); }
1934 * @brief Find position of a C string.
1935 * @param __s C string to locate.
1936 * @param __pos Index of character to search from (default 0).
1937 * @return Index of start of first occurrence.
1939 * Starting from @a __pos, searches forward for the value of @a
1940 * __s within this string. If found, returns the index where
1941 * it begins. If not found, returns npos.
1944 find(const _CharT
* __s
, size_type __pos
= 0) const
1946 __glibcxx_requires_string(__s
);
1947 return this->find(__s
, __pos
, traits_type::length(__s
));
1951 * @brief Find position of a character.
1952 * @param __c Character to locate.
1953 * @param __pos Index of character to search from (default 0).
1954 * @return Index of first occurrence.
1956 * Starting from @a __pos, searches forward for @a __c within
1957 * this string. If found, returns the index where it was
1958 * found. If not found, returns npos.
1961 find(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
;
1964 * @brief Find last position of a string.
1965 * @param __str String to locate.
1966 * @param __pos Index of character to search back from (default end).
1967 * @return Index of start of last occurrence.
1969 * Starting from @a __pos, searches backward for value of @a
1970 * __str within this string. If found, returns the index where
1971 * it begins. If not found, returns npos.
1974 rfind(const basic_string
& __str
, size_type __pos
= npos
) const
1976 { return this->rfind(__str
.data(), __pos
, __str
.size()); }
1979 * @brief Find last position of a C substring.
1980 * @param __s C string to locate.
1981 * @param __pos Index of character to search back from.
1982 * @param __n Number of characters from s to search for.
1983 * @return Index of start of last occurrence.
1985 * Starting from @a __pos, searches backward for the first @a
1986 * __n characters in @a __s within this string. If found,
1987 * returns the index where it begins. If not found, returns
1991 rfind(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
1994 * @brief Find last position of a C string.
1995 * @param __s C string to locate.
1996 * @param __pos Index of character to start search at (default end).
1997 * @return Index of start of last occurrence.
1999 * Starting from @a __pos, searches backward for the value of
2000 * @a __s within this string. If found, returns the index
2001 * where it begins. If not found, returns npos.
2004 rfind(const _CharT
* __s
, size_type __pos
= npos
) const
2006 __glibcxx_requires_string(__s
);
2007 return this->rfind(__s
, __pos
, traits_type::length(__s
));
2011 * @brief Find last position of a character.
2012 * @param __c Character to locate.
2013 * @param __pos Index of character to search back from (default end).
2014 * @return Index of last occurrence.
2016 * Starting from @a __pos, searches backward for @a __c within
2017 * this string. If found, returns the index where it was
2018 * found. If not found, returns npos.
2021 rfind(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
;
2024 * @brief Find position of a character of string.
2025 * @param __str String containing characters to locate.
2026 * @param __pos Index of character to search from (default 0).
2027 * @return Index of first occurrence.
2029 * Starting from @a __pos, searches forward for one of the
2030 * characters of @a __str within this string. If found,
2031 * returns the index where it was found. If not found, returns
2035 find_first_of(const basic_string
& __str
, size_type __pos
= 0) const
2037 { return this->find_first_of(__str
.data(), __pos
, __str
.size()); }
2040 * @brief Find position of a character of C substring.
2041 * @param __s String containing characters to locate.
2042 * @param __pos Index of character to search from.
2043 * @param __n Number of characters from s to search for.
2044 * @return Index of first occurrence.
2046 * Starting from @a __pos, searches forward for one of the
2047 * first @a __n characters of @a __s within this string. If
2048 * found, returns the index where it was found. If not found,
2052 find_first_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
2055 * @brief Find position of a character of C string.
2056 * @param __s String containing characters to locate.
2057 * @param __pos Index of character to search from (default 0).
2058 * @return Index of first occurrence.
2060 * Starting from @a __pos, searches forward for one of the
2061 * characters of @a __s within this string. If found, returns
2062 * the index where it was found. If not found, returns npos.
2065 find_first_of(const _CharT
* __s
, size_type __pos
= 0) const
2067 __glibcxx_requires_string(__s
);
2068 return this->find_first_of(__s
, __pos
, traits_type::length(__s
));
2072 * @brief Find position of a character.
2073 * @param __c Character to locate.
2074 * @param __pos Index of character to search from (default 0).
2075 * @return Index of first occurrence.
2077 * Starting from @a __pos, searches forward for the character
2078 * @a __c within this string. If found, returns the index
2079 * where it was found. If not found, returns npos.
2081 * Note: equivalent to find(__c, __pos).
2084 find_first_of(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
2085 { return this->find(__c
, __pos
); }
2088 * @brief Find last position of a character of string.
2089 * @param __str String containing characters to locate.
2090 * @param __pos Index of character to search back from (default end).
2091 * @return Index of last occurrence.
2093 * Starting from @a __pos, searches backward for one of the
2094 * characters of @a __str within this string. If found,
2095 * returns the index where it was found. If not found, returns
2099 find_last_of(const basic_string
& __str
, size_type __pos
= npos
) const
2101 { return this->find_last_of(__str
.data(), __pos
, __str
.size()); }
2104 * @brief Find last position of a character of C substring.
2105 * @param __s C string containing characters to locate.
2106 * @param __pos Index of character to search back from.
2107 * @param __n Number of characters from s to search for.
2108 * @return Index of last occurrence.
2110 * Starting from @a __pos, searches backward for one of the
2111 * first @a __n characters of @a __s within this string. If
2112 * found, returns the index where it was found. If not found,
2116 find_last_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
2119 * @brief Find last position of a character of C string.
2120 * @param __s C string containing characters to locate.
2121 * @param __pos Index of character to search back from (default end).
2122 * @return Index of last occurrence.
2124 * Starting from @a __pos, searches backward for one of the
2125 * characters of @a __s within this string. If found, returns
2126 * the index where it was found. If not found, returns npos.
2129 find_last_of(const _CharT
* __s
, size_type __pos
= npos
) const
2131 __glibcxx_requires_string(__s
);
2132 return this->find_last_of(__s
, __pos
, traits_type::length(__s
));
2136 * @brief Find last position of a character.
2137 * @param __c Character to locate.
2138 * @param __pos Index of character to search back from (default end).
2139 * @return Index of last occurrence.
2141 * Starting from @a __pos, searches backward for @a __c within
2142 * this string. If found, returns the index where it was
2143 * found. If not found, returns npos.
2145 * Note: equivalent to rfind(__c, __pos).
2148 find_last_of(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
2149 { return this->rfind(__c
, __pos
); }
2152 * @brief Find position of a character not in string.
2153 * @param __str String containing characters to avoid.
2154 * @param __pos Index of character to search from (default 0).
2155 * @return Index of first occurrence.
2157 * Starting from @a __pos, searches forward for a character not contained
2158 * in @a __str within this string. If found, returns the index where it
2159 * was found. If not found, returns npos.
2162 find_first_not_of(const basic_string
& __str
, size_type __pos
= 0) const
2164 { return this->find_first_not_of(__str
.data(), __pos
, __str
.size()); }
2167 * @brief Find position of a character not in C substring.
2168 * @param __s C string containing characters to avoid.
2169 * @param __pos Index of character to search from.
2170 * @param __n Number of characters from __s to consider.
2171 * @return Index of first occurrence.
2173 * Starting from @a __pos, searches forward for a character not
2174 * contained in the first @a __n characters of @a __s within
2175 * this string. If found, returns the index where it was
2176 * found. If not found, returns npos.
2179 find_first_not_of(const _CharT
* __s
, size_type __pos
,
2180 size_type __n
) const;
2183 * @brief Find position of a character not in C string.
2184 * @param __s C string containing characters to avoid.
2185 * @param __pos Index of character to search from (default 0).
2186 * @return Index of first occurrence.
2188 * Starting from @a __pos, searches forward for a character not
2189 * contained in @a __s within this string. If found, returns
2190 * the index where it was found. If not found, returns npos.
2193 find_first_not_of(const _CharT
* __s
, size_type __pos
= 0) const
2195 __glibcxx_requires_string(__s
);
2196 return this->find_first_not_of(__s
, __pos
, traits_type::length(__s
));
2200 * @brief Find position of a different character.
2201 * @param __c Character to avoid.
2202 * @param __pos Index of character to search from (default 0).
2203 * @return Index of first occurrence.
2205 * Starting from @a __pos, searches forward for a character
2206 * other than @a __c within this string. If found, returns the
2207 * index where it was found. If not found, returns npos.
2210 find_first_not_of(_CharT __c
, size_type __pos
= 0) const
2214 * @brief Find last position of a character not in string.
2215 * @param __str String containing characters to avoid.
2216 * @param __pos Index of character to search back from (default end).
2217 * @return Index of last occurrence.
2219 * Starting from @a __pos, searches backward for a character
2220 * not contained in @a __str within this string. If found,
2221 * returns the index where it was found. If not found, returns
2225 find_last_not_of(const basic_string
& __str
, size_type __pos
= npos
) const
2227 { return this->find_last_not_of(__str
.data(), __pos
, __str
.size()); }
2230 * @brief Find last position of a character not in C substring.
2231 * @param __s C string containing characters to avoid.
2232 * @param __pos Index of character to search back from.
2233 * @param __n Number of characters from s to consider.
2234 * @return Index of last occurrence.
2236 * Starting from @a __pos, searches backward for a character not
2237 * contained in the first @a __n characters of @a __s within this string.
2238 * If found, returns the index where it was found. If not found,
2242 find_last_not_of(const _CharT
* __s
, size_type __pos
,
2243 size_type __n
) const;
2245 * @brief Find last position of a character not in C string.
2246 * @param __s C string containing characters to avoid.
2247 * @param __pos Index of character to search back from (default end).
2248 * @return Index of last occurrence.
2250 * Starting from @a __pos, searches backward for a character
2251 * not contained in @a __s within this string. If found,
2252 * returns the index where it was found. If not found, returns
2256 find_last_not_of(const _CharT
* __s
, size_type __pos
= npos
) const
2258 __glibcxx_requires_string(__s
);
2259 return this->find_last_not_of(__s
, __pos
, traits_type::length(__s
));
2263 * @brief Find last position of a different character.
2264 * @param __c Character to avoid.
2265 * @param __pos Index of character to search back from (default end).
2266 * @return Index of last occurrence.
2268 * Starting from @a __pos, searches backward for a character other than
2269 * @a __c within this string. If found, returns the index where it was
2270 * found. If not found, returns npos.
2273 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const
2277 * @brief Get a substring.
2278 * @param __pos Index of first character (default 0).
2279 * @param __n Number of characters in substring (default remainder).
2280 * @return The new string.
2281 * @throw std::out_of_range If __pos > size().
2283 * Construct and return a new string using the @a __n
2284 * characters starting at @a __pos. If the string is too
2285 * short, use the remainder of the characters. If @a __pos is
2286 * beyond the end of the string, out_of_range is thrown.
2289 substr(size_type __pos
= 0, size_type __n
= npos
) const
2290 { return basic_string(*this,
2291 _M_check(__pos
, "basic_string::substr"), __n
); }
2294 * @brief Compare to a string.
2295 * @param __str String to compare against.
2296 * @return Integer < 0, 0, or > 0.
2298 * Returns an integer < 0 if this string is ordered before @a
2299 * __str, 0 if their values are equivalent, or > 0 if this
2300 * string is ordered after @a __str. Determines the effective
2301 * length rlen of the strings to compare as the smallest of
2302 * size() and str.size(). The function then compares the two
2303 * strings by calling traits::compare(data(), str.data(),rlen).
2304 * If the result of the comparison is nonzero returns it,
2305 * otherwise the shorter one is ordered first.
2308 compare(const basic_string
& __str
) const
2310 const size_type __size
= this->size();
2311 const size_type __osize
= __str
.size();
2312 const size_type __len
= std::min(__size
, __osize
);
2314 int __r
= traits_type::compare(_M_data(), __str
.data(), __len
);
2316 __r
= _S_compare(__size
, __osize
);
2321 * @brief Compare substring to a string.
2322 * @param __pos Index of first character of substring.
2323 * @param __n Number of characters in substring.
2324 * @param __str String to compare against.
2325 * @return Integer < 0, 0, or > 0.
2327 * Form the substring of this string from the @a __n characters
2328 * starting at @a __pos. Returns an integer < 0 if the
2329 * substring is ordered before @a __str, 0 if their values are
2330 * equivalent, or > 0 if the substring is ordered after @a
2331 * __str. Determines the effective length rlen of the strings
2332 * to compare as the smallest of the length of the substring
2333 * and @a __str.size(). The function then compares the two
2334 * strings by calling
2335 * traits::compare(substring.data(),str.data(),rlen). If the
2336 * result of the comparison is nonzero returns it, otherwise
2337 * the shorter one is ordered first.
2340 compare(size_type __pos
, size_type __n
, const basic_string
& __str
) const;
2343 * @brief Compare substring to a substring.
2344 * @param __pos1 Index of first character of substring.
2345 * @param __n1 Number of characters in substring.
2346 * @param __str String to compare against.
2347 * @param __pos2 Index of first character of substring of str.
2348 * @param __n2 Number of characters in substring of str.
2349 * @return Integer < 0, 0, or > 0.
2351 * Form the substring of this string from the @a __n1
2352 * characters starting at @a __pos1. Form the substring of @a
2353 * __str from the @a __n2 characters starting at @a __pos2.
2354 * Returns an integer < 0 if this substring is ordered before
2355 * the substring of @a __str, 0 if their values are equivalent,
2356 * or > 0 if this substring is ordered after the substring of
2357 * @a __str. Determines the effective length rlen of the
2358 * strings to compare as the smallest of the lengths of the
2359 * substrings. The function then compares the two strings by
2361 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2362 * If the result of the comparison is nonzero returns it,
2363 * otherwise the shorter one is ordered first.
2366 compare(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
2367 size_type __pos2
, size_type __n2
) const;
2370 * @brief Compare to a C string.
2371 * @param __s C string to compare against.
2372 * @return Integer < 0, 0, or > 0.
2374 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
2375 * their values are equivalent, or > 0 if this string is ordered after
2376 * @a __s. Determines the effective length rlen of the strings to
2377 * compare as the smallest of size() and the length of a string
2378 * constructed from @a __s. The function then compares the two strings
2379 * by calling traits::compare(data(),s,rlen). If the result of the
2380 * comparison is nonzero returns it, otherwise the shorter one is
2384 compare(const _CharT
* __s
) const;
2386 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2387 // 5 String::compare specification questionable
2389 * @brief Compare substring to a C string.
2390 * @param __pos Index of first character of substring.
2391 * @param __n1 Number of characters in substring.
2392 * @param __s C string to compare against.
2393 * @return Integer < 0, 0, or > 0.
2395 * Form the substring of this string from the @a __n1
2396 * characters starting at @a pos. Returns an integer < 0 if
2397 * the substring is ordered before @a __s, 0 if their values
2398 * are equivalent, or > 0 if the substring is ordered after @a
2399 * __s. Determines the effective length rlen of the strings to
2400 * compare as the smallest of the length of the substring and
2401 * the length of a string constructed from @a __s. The
2402 * function then compares the two string by calling
2403 * traits::compare(substring.data(),__s,rlen). If the result of
2404 * the comparison is nonzero returns it, otherwise the shorter
2405 * one is ordered first.
2408 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
) const;
2411 * @brief Compare substring against a character %array.
2412 * @param __pos Index of first character of substring.
2413 * @param __n1 Number of characters in substring.
2414 * @param __s character %array to compare against.
2415 * @param __n2 Number of characters of s.
2416 * @return Integer < 0, 0, or > 0.
2418 * Form the substring of this string from the @a __n1
2419 * characters starting at @a __pos. Form a string from the
2420 * first @a __n2 characters of @a __s. Returns an integer < 0
2421 * if this substring is ordered before the string from @a __s,
2422 * 0 if their values are equivalent, or > 0 if this substring
2423 * is ordered after the string from @a __s. Determines the
2424 * effective length rlen of the strings to compare as the
2425 * smallest of the length of the substring and @a __n2. The
2426 * function then compares the two strings by calling
2427 * traits::compare(substring.data(),s,rlen). If the result of
2428 * the comparison is nonzero returns it, otherwise the shorter
2429 * one is ordered first.
2431 * NB: s must have at least n2 characters, '\\0' has
2432 * no special meaning.
2435 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
,
2436 size_type __n2
) const;
2438 _GLIBCXX_END_NAMESPACE_CXX11
2439 #else // !_GLIBCXX_USE_CXX11_ABI
2440 // Reference-counted COW string implentation
2443 * @class basic_string basic_string.h <string>
2444 * @brief Managing sequences of characters and character-like objects.
2447 * @ingroup sequences
2449 * @tparam _CharT Type of character
2450 * @tparam _Traits Traits for character type, defaults to
2451 * char_traits<_CharT>.
2452 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
2454 * Meets the requirements of a <a href="tables.html#65">container</a>, a
2455 * <a href="tables.html#66">reversible container</a>, and a
2456 * <a href="tables.html#67">sequence</a>. Of the
2457 * <a href="tables.html#68">optional sequence requirements</a>, only
2458 * @c push_back, @c at, and @c %array access are supported.
2463 * Documentation? What's that?
2464 * Nathan Myers <ncm@cantrip.org>.
2466 * A string looks like this:
2471 * [basic_string<char_type>] _M_capacity
2472 * _M_dataplus _M_refcount
2473 * _M_p ----------------> unnamed array of char_type
2476 * Where the _M_p points to the first character in the string, and
2477 * you cast it to a pointer-to-_Rep and subtract 1 to get a
2478 * pointer to the header.
2480 * This approach has the enormous advantage that a string object
2481 * requires only one allocation. All the ugliness is confined
2482 * within a single %pair of inline functions, which each compile to
2483 * a single @a add instruction: _Rep::_M_data(), and
2484 * string::_M_rep(); and the allocation function which gets a
2485 * block of raw bytes and with room enough and constructs a _Rep
2486 * object at the front.
2488 * The reason you want _M_data pointing to the character %array and
2489 * not the _Rep is so that the debugger can see the string
2490 * contents. (Probably we should add a non-inline member to get
2491 * the _Rep for the debugger to use, so users can check the actual
2494 * Note that the _Rep object is a POD so that you can have a
2495 * static <em>empty string</em> _Rep object already @a constructed before
2496 * static constructors have run. The reference-count encoding is
2497 * chosen so that a 0 indicates one reference, so you never try to
2498 * destroy the empty-string _Rep object.
2500 * All but the last paragraph is considered pretty conventional
2501 * for a C++ string implementation.
2503 // 21.3 Template class basic_string
2504 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2507 typedef typename
_Alloc::template rebind
<_CharT
>::other _CharT_alloc_type
;
2511 typedef _Traits traits_type
;
2512 typedef typename
_Traits::char_type value_type
;
2513 typedef _Alloc allocator_type
;
2514 typedef typename
_CharT_alloc_type::size_type size_type
;
2515 typedef typename
_CharT_alloc_type::difference_type difference_type
;
2516 typedef typename
_CharT_alloc_type::reference reference
;
2517 typedef typename
_CharT_alloc_type::const_reference const_reference
;
2518 typedef typename
_CharT_alloc_type::pointer pointer
;
2519 typedef typename
_CharT_alloc_type::const_pointer const_pointer
;
2520 typedef __gnu_cxx::__normal_iterator
<pointer
, basic_string
> iterator
;
2521 typedef __gnu_cxx::__normal_iterator
<const_pointer
, basic_string
>
2523 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
2524 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
2527 // _Rep: string representation
2529 // 1. String really contains _M_length + 1 characters: due to 21.3.4
2530 // must be kept null-terminated.
2531 // 2. _M_capacity >= _M_length
2532 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
2533 // 3. _M_refcount has three states:
2534 // -1: leaked, one reference, no ref-copies allowed, non-const.
2535 // 0: one reference, non-const.
2536 // n>0: n + 1 references, operations require a lock, const.
2537 // 4. All fields==0 is an empty string, given the extra storage
2538 // beyond-the-end for a null terminator; thus, the shared
2539 // empty string representation needs no constructor.
2543 size_type _M_length
;
2544 size_type _M_capacity
;
2545 _Atomic_word _M_refcount
;
2548 struct _Rep
: _Rep_base
2551 typedef typename
_Alloc::template rebind
<char>::other _Raw_bytes_alloc
;
2553 // (Public) Data members:
2555 // The maximum number of individual char_type elements of an
2556 // individual string is determined by _S_max_size. This is the
2557 // value that will be returned by max_size(). (Whereas npos
2558 // is the maximum number of bytes the allocator can allocate.)
2559 // If one was to divvy up the theoretical largest size string,
2560 // with a terminating character and m _CharT elements, it'd
2562 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
2564 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
2565 // In addition, this implementation quarters this amount.
2566 static const size_type _S_max_size
;
2567 static const _CharT _S_terminal
;
2569 // The following storage is init'd to 0 by the linker, resulting
2570 // (carefully) in an empty string with one reference.
2571 static size_type _S_empty_rep_storage
[];
2574 _S_empty_rep() _GLIBCXX_NOEXCEPT
2576 // NB: Mild hack to avoid strict-aliasing warnings. Note that
2577 // _S_empty_rep_storage is never modified and the punning should
2578 // be reasonably safe in this case.
2579 void* __p
= reinterpret_cast<void*>(&_S_empty_rep_storage
);
2580 return *reinterpret_cast<_Rep
*>(__p
);
2584 _M_is_leaked() const _GLIBCXX_NOEXCEPT
2585 { return this->_M_refcount
< 0; }
2588 _M_is_shared() const _GLIBCXX_NOEXCEPT
2589 { return this->_M_refcount
> 0; }
2592 _M_set_leaked() _GLIBCXX_NOEXCEPT
2593 { this->_M_refcount
= -1; }
2596 _M_set_sharable() _GLIBCXX_NOEXCEPT
2597 { this->_M_refcount
= 0; }
2600 _M_set_length_and_sharable(size_type __n
) _GLIBCXX_NOEXCEPT
2602 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2603 if (__builtin_expect(this != &_S_empty_rep(), false))
2606 this->_M_set_sharable(); // One reference.
2607 this->_M_length
= __n
;
2608 traits_type::assign(this->_M_refdata()[__n
], _S_terminal
);
2609 // grrr. (per 21.3.4)
2610 // You cannot leave those LWG people alone for a second.
2615 _M_refdata() throw()
2616 { return reinterpret_cast<_CharT
*>(this + 1); }
2619 _M_grab(const _Alloc
& __alloc1
, const _Alloc
& __alloc2
)
2621 return (!_M_is_leaked() && __alloc1
== __alloc2
)
2622 ? _M_refcopy() : _M_clone(__alloc1
);
2627 _S_create(size_type
, size_type
, const _Alloc
&);
2630 _M_dispose(const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
2632 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2633 if (__builtin_expect(this != &_S_empty_rep(), false))
2636 // Be race-detector-friendly. For more info see bits/c++config.
2637 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount
);
2638 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount
,
2641 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount
);
2648 _M_destroy(const _Alloc
&) throw();
2651 _M_refcopy() throw()
2653 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2654 if (__builtin_expect(this != &_S_empty_rep(), false))
2656 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount
, 1);
2657 return _M_refdata();
2661 _M_clone(const _Alloc
&, size_type __res
= 0);
2664 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
2665 struct _Alloc_hider
: _Alloc
2667 _Alloc_hider(_CharT
* __dat
, const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
2668 : _Alloc(__a
), _M_p(__dat
) { }
2670 _CharT
* _M_p
; // The actual data.
2674 // Data Members (public):
2675 // NB: This is an unsigned type, and thus represents the maximum
2676 // size that the allocator can hold.
2677 /// Value returned by various member functions when they fail.
2678 static const size_type npos
= static_cast<size_type
>(-1);
2681 // Data Members (private):
2682 mutable _Alloc_hider _M_dataplus
;
2685 _M_data() const _GLIBCXX_NOEXCEPT
2686 { return _M_dataplus
._M_p
; }
2689 _M_data(_CharT
* __p
) _GLIBCXX_NOEXCEPT
2690 { return (_M_dataplus
._M_p
= __p
); }
2693 _M_rep() const _GLIBCXX_NOEXCEPT
2694 { return &((reinterpret_cast<_Rep
*> (_M_data()))[-1]); }
2696 // For the internal use we have functions similar to `begin'/`end'
2697 // but they do not call _M_leak.
2699 _M_ibegin() const _GLIBCXX_NOEXCEPT
2700 { return iterator(_M_data()); }
2703 _M_iend() const _GLIBCXX_NOEXCEPT
2704 { return iterator(_M_data() + this->size()); }
2707 _M_leak() // for use in begin() & non-const op[]
2709 if (!_M_rep()->_M_is_leaked())
2714 _M_check(size_type __pos
, const char* __s
) const
2716 if (__pos
> this->size())
2717 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
2718 "this->size() (which is %zu)"),
2719 __s
, __pos
, this->size());
2724 _M_check_length(size_type __n1
, size_type __n2
, const char* __s
) const
2726 if (this->max_size() - (this->size() - __n1
) < __n2
)
2727 __throw_length_error(__N(__s
));
2730 // NB: _M_limit doesn't check for a bad __pos value.
2732 _M_limit(size_type __pos
, size_type __off
) const _GLIBCXX_NOEXCEPT
2734 const bool __testoff
= __off
< this->size() - __pos
;
2735 return __testoff
? __off
: this->size() - __pos
;
2738 // True if _Rep and source do not overlap.
2740 _M_disjunct(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
2742 return (less
<const _CharT
*>()(__s
, _M_data())
2743 || less
<const _CharT
*>()(_M_data() + this->size(), __s
));
2746 // When __n = 1 way faster than the general multichar
2747 // traits_type::copy/move/assign.
2749 _M_copy(_CharT
* __d
, const _CharT
* __s
, size_type __n
) _GLIBCXX_NOEXCEPT
2752 traits_type::assign(*__d
, *__s
);
2754 traits_type::copy(__d
, __s
, __n
);
2758 _M_move(_CharT
* __d
, const _CharT
* __s
, size_type __n
) _GLIBCXX_NOEXCEPT
2761 traits_type::assign(*__d
, *__s
);
2763 traits_type::move(__d
, __s
, __n
);
2767 _M_assign(_CharT
* __d
, size_type __n
, _CharT __c
) _GLIBCXX_NOEXCEPT
2770 traits_type::assign(*__d
, __c
);
2772 traits_type::assign(__d
, __n
, __c
);
2775 // _S_copy_chars is a separate template to permit specialization
2776 // to optimize for the common case of pointers as iterators.
2777 template<class _Iterator
>
2779 _S_copy_chars(_CharT
* __p
, _Iterator __k1
, _Iterator __k2
)
2782 for (; __k1
!= __k2
; ++__k1
, ++__p
)
2783 traits_type::assign(*__p
, *__k1
); // These types are off.
2787 _S_copy_chars(_CharT
* __p
, iterator __k1
, iterator __k2
) _GLIBCXX_NOEXCEPT
2788 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
2791 _S_copy_chars(_CharT
* __p
, const_iterator __k1
, const_iterator __k2
)
2793 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
2796 _S_copy_chars(_CharT
* __p
, _CharT
* __k1
, _CharT
* __k2
) _GLIBCXX_NOEXCEPT
2797 { _M_copy(__p
, __k1
, __k2
- __k1
); }
2800 _S_copy_chars(_CharT
* __p
, const _CharT
* __k1
, const _CharT
* __k2
)
2802 { _M_copy(__p
, __k1
, __k2
- __k1
); }
2805 _S_compare(size_type __n1
, size_type __n2
) _GLIBCXX_NOEXCEPT
2807 const difference_type __d
= difference_type(__n1
- __n2
);
2809 if (__d
> __gnu_cxx::__numeric_traits
<int>::__max
)
2810 return __gnu_cxx::__numeric_traits
<int>::__max
;
2811 else if (__d
< __gnu_cxx::__numeric_traits
<int>::__min
)
2812 return __gnu_cxx::__numeric_traits
<int>::__min
;
2818 _M_mutate(size_type __pos
, size_type __len1
, size_type __len2
);
2824 _S_empty_rep() _GLIBCXX_NOEXCEPT
2825 { return _Rep::_S_empty_rep(); }
2828 // Construct/copy/destroy:
2829 // NB: We overload ctors in some cases instead of using default
2830 // arguments, per 17.4.4.4 para. 2 item 2.
2833 * @brief Default constructor creates an empty string.
2836 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2837 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
2839 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ }
2843 * @brief Construct an empty string using allocator @a a.
2846 basic_string(const _Alloc
& __a
);
2848 // NB: per LWG issue 42, semantics different from IS:
2850 * @brief Construct string with copy of value of @a str.
2851 * @param __str Source string.
2853 basic_string(const basic_string
& __str
);
2855 * @brief Construct string as copy of a substring.
2856 * @param __str Source string.
2857 * @param __pos Index of first character to copy from.
2858 * @param __n Number of characters to copy (default remainder).
2860 basic_string(const basic_string
& __str
, size_type __pos
,
2861 size_type __n
= npos
);
2863 * @brief Construct string as copy of a substring.
2864 * @param __str Source string.
2865 * @param __pos Index of first character to copy from.
2866 * @param __n Number of characters to copy.
2867 * @param __a Allocator to use.
2869 basic_string(const basic_string
& __str
, size_type __pos
,
2870 size_type __n
, const _Alloc
& __a
);
2873 * @brief Construct string initialized by a character %array.
2874 * @param __s Source character %array.
2875 * @param __n Number of characters to copy.
2876 * @param __a Allocator to use (default is default allocator).
2878 * NB: @a __s must have at least @a __n characters, '\\0'
2879 * has no special meaning.
2881 basic_string(const _CharT
* __s
, size_type __n
,
2882 const _Alloc
& __a
= _Alloc());
2884 * @brief Construct string as copy of a C string.
2885 * @param __s Source C string.
2886 * @param __a Allocator to use (default is default allocator).
2888 basic_string(const _CharT
* __s
, const _Alloc
& __a
= _Alloc());
2890 * @brief Construct string as multiple characters.
2891 * @param __n Number of characters.
2892 * @param __c Character to use.
2893 * @param __a Allocator to use (default is default allocator).
2895 basic_string(size_type __n
, _CharT __c
, const _Alloc
& __a
= _Alloc());
2897 #if __cplusplus >= 201103L
2899 * @brief Move construct string.
2900 * @param __str Source string.
2902 * The newly-created string contains the exact contents of @a __str.
2903 * @a __str is a valid, but unspecified string.
2905 basic_string(basic_string
&& __str
)
2906 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2907 noexcept
// FIXME C++11: should always be noexcept.
2909 : _M_dataplus(__str
._M_dataplus
)
2911 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
2912 __str
._M_data(_S_empty_rep()._M_refdata());
2914 __str
._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
2919 * @brief Construct string from an initializer %list.
2920 * @param __l std::initializer_list of characters.
2921 * @param __a Allocator to use (default is default allocator).
2923 basic_string(initializer_list
<_CharT
> __l
, const _Alloc
& __a
= _Alloc());
2927 * @brief Construct string as copy of a range.
2928 * @param __beg Start of range.
2929 * @param __end End of range.
2930 * @param __a Allocator to use (default is default allocator).
2932 template<class _InputIterator
>
2933 basic_string(_InputIterator __beg
, _InputIterator __end
,
2934 const _Alloc
& __a
= _Alloc());
2937 * @brief Destroy the string instance.
2939 ~basic_string() _GLIBCXX_NOEXCEPT
2940 { _M_rep()->_M_dispose(this->get_allocator()); }
2943 * @brief Assign the value of @a str to this string.
2944 * @param __str Source string.
2947 operator=(const basic_string
& __str
)
2948 { return this->assign(__str
); }
2951 * @brief Copy contents of @a s into this string.
2952 * @param __s Source null-terminated string.
2955 operator=(const _CharT
* __s
)
2956 { return this->assign(__s
); }
2959 * @brief Set value to string of length 1.
2960 * @param __c Source character.
2962 * Assigning to a character makes this string length 1 and
2963 * (*this)[0] == @a c.
2966 operator=(_CharT __c
)
2968 this->assign(1, __c
);
2972 #if __cplusplus >= 201103L
2974 * @brief Move assign the value of @a str to this string.
2975 * @param __str Source string.
2977 * The contents of @a str are moved into this string (without copying).
2978 * @a str is a valid, but unspecified string.
2980 // PR 58265, this should be noexcept.
2982 operator=(basic_string
&& __str
)
2990 * @brief Set value to string constructed from initializer %list.
2991 * @param __l std::initializer_list.
2994 operator=(initializer_list
<_CharT
> __l
)
2996 this->assign(__l
.begin(), __l
.size());
3003 * Returns a read/write iterator that points to the first character in
3004 * the %string. Unshares the string.
3007 begin() // FIXME C++11: should be noexcept.
3010 return iterator(_M_data());
3014 * Returns a read-only (constant) iterator that points to the first
3015 * character in the %string.
3018 begin() const _GLIBCXX_NOEXCEPT
3019 { return const_iterator(_M_data()); }
3022 * Returns a read/write iterator that points one past the last
3023 * character in the %string. Unshares the string.
3026 end() // FIXME C++11: should be noexcept.
3029 return iterator(_M_data() + this->size());
3033 * Returns a read-only (constant) iterator that points one past the
3034 * last character in the %string.
3037 end() const _GLIBCXX_NOEXCEPT
3038 { return const_iterator(_M_data() + this->size()); }
3041 * Returns a read/write reverse iterator that points to the last
3042 * character in the %string. Iteration is done in reverse element
3043 * order. Unshares the string.
3046 rbegin() // FIXME C++11: should be noexcept.
3047 { return reverse_iterator(this->end()); }
3050 * Returns a read-only (constant) reverse iterator that points
3051 * to the last character in the %string. Iteration is done in
3052 * reverse element order.
3054 const_reverse_iterator
3055 rbegin() const _GLIBCXX_NOEXCEPT
3056 { return const_reverse_iterator(this->end()); }
3059 * Returns a read/write reverse iterator that points to one before the
3060 * first character in the %string. Iteration is done in reverse
3061 * element order. Unshares the string.
3064 rend() // FIXME C++11: should be noexcept.
3065 { return reverse_iterator(this->begin()); }
3068 * Returns a read-only (constant) reverse iterator that points
3069 * to one before the first character in the %string. Iteration
3070 * is done in reverse element order.
3072 const_reverse_iterator
3073 rend() const _GLIBCXX_NOEXCEPT
3074 { return const_reverse_iterator(this->begin()); }
3076 #if __cplusplus >= 201103L
3078 * Returns a read-only (constant) iterator that points to the first
3079 * character in the %string.
3082 cbegin() const noexcept
3083 { return const_iterator(this->_M_data()); }
3086 * Returns a read-only (constant) iterator that points one past the
3087 * last character in the %string.
3090 cend() const noexcept
3091 { return const_iterator(this->_M_data() + this->size()); }
3094 * Returns a read-only (constant) reverse iterator that points
3095 * to the last character in the %string. Iteration is done in
3096 * reverse element order.
3098 const_reverse_iterator
3099 crbegin() const noexcept
3100 { return const_reverse_iterator(this->end()); }
3103 * Returns a read-only (constant) reverse iterator that points
3104 * to one before the first character in the %string. Iteration
3105 * is done in reverse element order.
3107 const_reverse_iterator
3108 crend() const noexcept
3109 { return const_reverse_iterator(this->begin()); }
3114 /// Returns the number of characters in the string, not including any
3115 /// null-termination.
3117 size() const _GLIBCXX_NOEXCEPT
3118 { return _M_rep()->_M_length
; }
3120 /// Returns the number of characters in the string, not including any
3121 /// null-termination.
3123 length() const _GLIBCXX_NOEXCEPT
3124 { return _M_rep()->_M_length
; }
3126 /// Returns the size() of the largest possible %string.
3128 max_size() const _GLIBCXX_NOEXCEPT
3129 { return _Rep::_S_max_size
; }
3132 * @brief Resizes the %string to the specified number of characters.
3133 * @param __n Number of characters the %string should contain.
3134 * @param __c Character to fill any new elements.
3136 * This function will %resize the %string to the specified
3137 * number of characters. If the number is smaller than the
3138 * %string's current size the %string is truncated, otherwise
3139 * the %string is extended and new elements are %set to @a __c.
3142 resize(size_type __n
, _CharT __c
);
3145 * @brief Resizes the %string to the specified number of characters.
3146 * @param __n Number of characters the %string should contain.
3148 * This function will resize the %string to the specified length. If
3149 * the new size is smaller than the %string's current size the %string
3150 * is truncated, otherwise the %string is extended and new characters
3151 * are default-constructed. For basic types such as char, this means
3152 * setting them to 0.
3155 resize(size_type __n
)
3156 { this->resize(__n
, _CharT()); }
3158 #if __cplusplus >= 201103L
3159 /// A non-binding request to reduce capacity() to size().
3161 shrink_to_fit() _GLIBCXX_NOEXCEPT
3163 if (capacity() > size())
3174 * Returns the total number of characters that the %string can hold
3175 * before needing to allocate more memory.
3178 capacity() const _GLIBCXX_NOEXCEPT
3179 { return _M_rep()->_M_capacity
; }
3182 * @brief Attempt to preallocate enough memory for specified number of
3184 * @param __res_arg Number of characters required.
3185 * @throw std::length_error If @a __res_arg exceeds @c max_size().
3187 * This function attempts to reserve enough memory for the
3188 * %string to hold the specified number of characters. If the
3189 * number requested is more than max_size(), length_error is
3192 * The advantage of this function is that if optimal code is a
3193 * necessity and the user can determine the string length that will be
3194 * required, the user can reserve the memory in %advance, and thus
3195 * prevent a possible reallocation of memory and copying of %string
3199 reserve(size_type __res_arg
= 0);
3202 * Erases the string, making it empty.
3204 // PR 56166: this should not throw.
3207 { _M_mutate(0, this->size(), 0); }
3210 * Returns true if the %string is empty. Equivalent to
3211 * <code>*this == ""</code>.
3214 empty() const _GLIBCXX_NOEXCEPT
3215 { return this->size() == 0; }
3219 * @brief Subscript access to the data contained in the %string.
3220 * @param __pos The index of the character to access.
3221 * @return Read-only (constant) reference to the character.
3223 * This operator allows for easy, array-style, data access.
3224 * Note that data access with this operator is unchecked and
3225 * out_of_range lookups are not defined. (For checked lookups
3229 operator[] (size_type __pos
) const _GLIBCXX_NOEXCEPT
3231 _GLIBCXX_DEBUG_ASSERT(__pos
<= size());
3232 return _M_data()[__pos
];
3236 * @brief Subscript access to the data contained in the %string.
3237 * @param __pos The index of the character to access.
3238 * @return Read/write reference to the character.
3240 * This operator allows for easy, array-style, data access.
3241 * Note that data access with this operator is unchecked and
3242 * out_of_range lookups are not defined. (For checked lookups
3243 * see at().) Unshares the string.
3246 operator[](size_type __pos
)
3248 // Allow pos == size() both in C++98 mode, as v3 extension,
3249 // and in C++11 mode.
3250 _GLIBCXX_DEBUG_ASSERT(__pos
<= size());
3251 // In pedantic mode be strict in C++98 mode.
3252 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus
>= 201103L || __pos
< size());
3254 return _M_data()[__pos
];
3258 * @brief Provides access to the data contained in the %string.
3259 * @param __n The index of the character to access.
3260 * @return Read-only (const) reference to the character.
3261 * @throw std::out_of_range If @a n is an invalid index.
3263 * This function provides for safer data access. The parameter is
3264 * first checked that it is in the range of the string. The function
3265 * throws out_of_range if the check fails.
3268 at(size_type __n
) const
3270 if (__n
>= this->size())
3271 __throw_out_of_range_fmt(__N("basic_string::at: __n "
3272 "(which is %zu) >= this->size() "
3275 return _M_data()[__n
];
3279 * @brief Provides access to the data contained in the %string.
3280 * @param __n The index of the character to access.
3281 * @return Read/write reference to the character.
3282 * @throw std::out_of_range If @a n is an invalid index.
3284 * This function provides for safer data access. The parameter is
3285 * first checked that it is in the range of the string. The function
3286 * throws out_of_range if the check fails. Success results in
3287 * unsharing the string.
3293 __throw_out_of_range_fmt(__N("basic_string::at: __n "
3294 "(which is %zu) >= this->size() "
3298 return _M_data()[__n
];
3301 #if __cplusplus >= 201103L
3303 * Returns a read/write reference to the data at the first
3304 * element of the %string.
3308 { return operator[](0); }
3311 * Returns a read-only (constant) reference to the data at the first
3312 * element of the %string.
3315 front() const _GLIBCXX_NOEXCEPT
3316 { return operator[](0); }
3319 * Returns a read/write reference to the data at the last
3320 * element of the %string.
3324 { return operator[](this->size() - 1); }
3327 * Returns a read-only (constant) reference to the data at the
3328 * last element of the %string.
3331 back() const _GLIBCXX_NOEXCEPT
3332 { return operator[](this->size() - 1); }
3337 * @brief Append a string to this string.
3338 * @param __str The string to append.
3339 * @return Reference to this string.
3342 operator+=(const basic_string
& __str
)
3343 { return this->append(__str
); }
3346 * @brief Append a C string.
3347 * @param __s The C string to append.
3348 * @return Reference to this string.
3351 operator+=(const _CharT
* __s
)
3352 { return this->append(__s
); }
3355 * @brief Append a character.
3356 * @param __c The character to append.
3357 * @return Reference to this string.
3360 operator+=(_CharT __c
)
3362 this->push_back(__c
);
3366 #if __cplusplus >= 201103L
3368 * @brief Append an initializer_list of characters.
3369 * @param __l The initializer_list of characters to be appended.
3370 * @return Reference to this string.
3373 operator+=(initializer_list
<_CharT
> __l
)
3374 { return this->append(__l
.begin(), __l
.size()); }
3378 * @brief Append a string to this string.
3379 * @param __str The string to append.
3380 * @return Reference to this string.
3383 append(const basic_string
& __str
);
3386 * @brief Append a substring.
3387 * @param __str The string to append.
3388 * @param __pos Index of the first character of str to append.
3389 * @param __n The number of characters to append.
3390 * @return Reference to this string.
3391 * @throw std::out_of_range if @a __pos is not a valid index.
3393 * This function appends @a __n characters from @a __str
3394 * starting at @a __pos to this string. If @a __n is is larger
3395 * than the number of available characters in @a __str, the
3396 * remainder of @a __str is appended.
3399 append(const basic_string
& __str
, size_type __pos
, size_type __n
);
3402 * @brief Append a C substring.
3403 * @param __s The C string to append.
3404 * @param __n The number of characters to append.
3405 * @return Reference to this string.
3408 append(const _CharT
* __s
, size_type __n
);
3411 * @brief Append a C string.
3412 * @param __s The C string to append.
3413 * @return Reference to this string.
3416 append(const _CharT
* __s
)
3418 __glibcxx_requires_string(__s
);
3419 return this->append(__s
, traits_type::length(__s
));
3423 * @brief Append multiple characters.
3424 * @param __n The number of characters to append.
3425 * @param __c The character to use.
3426 * @return Reference to this string.
3428 * Appends __n copies of __c to this string.
3431 append(size_type __n
, _CharT __c
);
3433 #if __cplusplus >= 201103L
3435 * @brief Append an initializer_list of characters.
3436 * @param __l The initializer_list of characters to append.
3437 * @return Reference to this string.
3440 append(initializer_list
<_CharT
> __l
)
3441 { return this->append(__l
.begin(), __l
.size()); }
3445 * @brief Append a range of characters.
3446 * @param __first Iterator referencing the first character to append.
3447 * @param __last Iterator marking the end of the range.
3448 * @return Reference to this string.
3450 * Appends characters in the range [__first,__last) to this string.
3452 template<class _InputIterator
>
3454 append(_InputIterator __first
, _InputIterator __last
)
3455 { return this->replace(_M_iend(), _M_iend(), __first
, __last
); }
3458 * @brief Append a single character.
3459 * @param __c Character to append.
3462 push_back(_CharT __c
)
3464 const size_type __len
= 1 + this->size();
3465 if (__len
> this->capacity() || _M_rep()->_M_is_shared())
3466 this->reserve(__len
);
3467 traits_type::assign(_M_data()[this->size()], __c
);
3468 _M_rep()->_M_set_length_and_sharable(__len
);
3472 * @brief Set value to contents of another string.
3473 * @param __str Source string to use.
3474 * @return Reference to this string.
3477 assign(const basic_string
& __str
);
3479 #if __cplusplus >= 201103L
3481 * @brief Set value to contents of another string.
3482 * @param __str Source string to use.
3483 * @return Reference to this string.
3485 * This function sets this string to the exact contents of @a __str.
3486 * @a __str is a valid, but unspecified string.
3488 // PR 58265, this should be noexcept.
3490 assign(basic_string
&& __str
)
3498 * @brief Set value to a substring of a string.
3499 * @param __str The string to use.
3500 * @param __pos Index of the first character of str.
3501 * @param __n Number of characters to use.
3502 * @return Reference to this string.
3503 * @throw std::out_of_range if @a pos is not a valid index.
3505 * This function sets this string to the substring of @a __str
3506 * consisting of @a __n characters at @a __pos. If @a __n is
3507 * is larger than the number of available characters in @a
3508 * __str, the remainder of @a __str is used.
3511 assign(const basic_string
& __str
, size_type __pos
, size_type __n
)
3512 { return this->assign(__str
._M_data()
3513 + __str
._M_check(__pos
, "basic_string::assign"),
3514 __str
._M_limit(__pos
, __n
)); }
3517 * @brief Set value to a C substring.
3518 * @param __s The C string to use.
3519 * @param __n Number of characters to use.
3520 * @return Reference to this string.
3522 * This function sets the value of this string to the first @a __n
3523 * characters of @a __s. If @a __n is is larger than the number of
3524 * available characters in @a __s, the remainder of @a __s is used.
3527 assign(const _CharT
* __s
, size_type __n
);
3530 * @brief Set value to contents of a C string.
3531 * @param __s The C string to use.
3532 * @return Reference to this string.
3534 * This function sets the value of this string to the value of @a __s.
3535 * The data is copied, so there is no dependence on @a __s once the
3539 assign(const _CharT
* __s
)
3541 __glibcxx_requires_string(__s
);
3542 return this->assign(__s
, traits_type::length(__s
));
3546 * @brief Set value to multiple characters.
3547 * @param __n Length of the resulting string.
3548 * @param __c The character to use.
3549 * @return Reference to this string.
3551 * This function sets the value of this string to @a __n copies of
3555 assign(size_type __n
, _CharT __c
)
3556 { return _M_replace_aux(size_type(0), this->size(), __n
, __c
); }
3559 * @brief Set value to a range of characters.
3560 * @param __first Iterator referencing the first character to append.
3561 * @param __last Iterator marking the end of the range.
3562 * @return Reference to this string.
3564 * Sets value of string to characters in the range [__first,__last).
3566 template<class _InputIterator
>
3568 assign(_InputIterator __first
, _InputIterator __last
)
3569 { return this->replace(_M_ibegin(), _M_iend(), __first
, __last
); }
3571 #if __cplusplus >= 201103L
3573 * @brief Set value to an initializer_list of characters.
3574 * @param __l The initializer_list of characters to assign.
3575 * @return Reference to this string.
3578 assign(initializer_list
<_CharT
> __l
)
3579 { return this->assign(__l
.begin(), __l
.size()); }
3583 * @brief Insert multiple characters.
3584 * @param __p Iterator referencing location in string to insert at.
3585 * @param __n Number of characters to insert
3586 * @param __c The character to insert.
3587 * @throw std::length_error If new length exceeds @c max_size().
3589 * Inserts @a __n copies of character @a __c starting at the
3590 * position referenced by iterator @a __p. If adding
3591 * characters causes the length to exceed max_size(),
3592 * length_error is thrown. The value of the string doesn't
3593 * change if an error is thrown.
3596 insert(iterator __p
, size_type __n
, _CharT __c
)
3597 { this->replace(__p
, __p
, __n
, __c
); }
3600 * @brief Insert a range of characters.
3601 * @param __p Iterator referencing location in string to insert at.
3602 * @param __beg Start of range.
3603 * @param __end End of range.
3604 * @throw std::length_error If new length exceeds @c max_size().
3606 * Inserts characters in range [__beg,__end). If adding
3607 * characters causes the length to exceed max_size(),
3608 * length_error is thrown. The value of the string doesn't
3609 * change if an error is thrown.
3611 template<class _InputIterator
>
3613 insert(iterator __p
, _InputIterator __beg
, _InputIterator __end
)
3614 { this->replace(__p
, __p
, __beg
, __end
); }
3616 #if __cplusplus >= 201103L
3618 * @brief Insert an initializer_list of characters.
3619 * @param __p Iterator referencing location in string to insert at.
3620 * @param __l The initializer_list of characters to insert.
3621 * @throw std::length_error If new length exceeds @c max_size().
3624 insert(iterator __p
, initializer_list
<_CharT
> __l
)
3626 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
3627 this->insert(__p
- _M_ibegin(), __l
.begin(), __l
.size());
3632 * @brief Insert value of a string.
3633 * @param __pos1 Iterator referencing location in string to insert at.
3634 * @param __str The string to insert.
3635 * @return Reference to this string.
3636 * @throw std::length_error If new length exceeds @c max_size().
3638 * Inserts value of @a __str starting at @a __pos1. If adding
3639 * characters causes the length to exceed max_size(),
3640 * length_error is thrown. The value of the string doesn't
3641 * change if an error is thrown.
3644 insert(size_type __pos1
, const basic_string
& __str
)
3645 { return this->insert(__pos1
, __str
, size_type(0), __str
.size()); }
3648 * @brief Insert a substring.
3649 * @param __pos1 Iterator referencing location in string to insert at.
3650 * @param __str The string to insert.
3651 * @param __pos2 Start of characters in str to insert.
3652 * @param __n Number of characters to insert.
3653 * @return Reference to this string.
3654 * @throw std::length_error If new length exceeds @c max_size().
3655 * @throw std::out_of_range If @a pos1 > size() or
3656 * @a __pos2 > @a str.size().
3658 * Starting at @a pos1, insert @a __n character of @a __str
3659 * beginning with @a __pos2. If adding characters causes the
3660 * length to exceed max_size(), length_error is thrown. If @a
3661 * __pos1 is beyond the end of this string or @a __pos2 is
3662 * beyond the end of @a __str, out_of_range is thrown. The
3663 * value of the string doesn't change if an error is thrown.
3666 insert(size_type __pos1
, const basic_string
& __str
,
3667 size_type __pos2
, size_type __n
)
3668 { return this->insert(__pos1
, __str
._M_data()
3669 + __str
._M_check(__pos2
, "basic_string::insert"),
3670 __str
._M_limit(__pos2
, __n
)); }
3673 * @brief Insert a C substring.
3674 * @param __pos Iterator referencing location in string to insert at.
3675 * @param __s The C string to insert.
3676 * @param __n The number of characters to insert.
3677 * @return Reference to this string.
3678 * @throw std::length_error If new length exceeds @c max_size().
3679 * @throw std::out_of_range If @a __pos is beyond the end of this
3682 * Inserts the first @a __n characters of @a __s starting at @a
3683 * __pos. If adding characters causes the length to exceed
3684 * max_size(), length_error is thrown. If @a __pos is beyond
3685 * end(), out_of_range is thrown. The value of the string
3686 * doesn't change if an error is thrown.
3689 insert(size_type __pos
, const _CharT
* __s
, size_type __n
);
3692 * @brief Insert a C string.
3693 * @param __pos Iterator referencing location in string to insert at.
3694 * @param __s The C string to insert.
3695 * @return Reference to this string.
3696 * @throw std::length_error If new length exceeds @c max_size().
3697 * @throw std::out_of_range If @a pos is beyond the end of this
3700 * Inserts the first @a n characters of @a __s starting at @a __pos. If
3701 * adding characters causes the length to exceed max_size(),
3702 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
3703 * thrown. The value of the string doesn't change if an error is
3707 insert(size_type __pos
, const _CharT
* __s
)
3709 __glibcxx_requires_string(__s
);
3710 return this->insert(__pos
, __s
, traits_type::length(__s
));
3714 * @brief Insert multiple characters.
3715 * @param __pos Index in string to insert at.
3716 * @param __n Number of characters to insert
3717 * @param __c The character to insert.
3718 * @return Reference to this string.
3719 * @throw std::length_error If new length exceeds @c max_size().
3720 * @throw std::out_of_range If @a __pos is beyond the end of this
3723 * Inserts @a __n copies of character @a __c starting at index
3724 * @a __pos. If adding characters causes the length to exceed
3725 * max_size(), length_error is thrown. If @a __pos > length(),
3726 * out_of_range is thrown. The value of the string doesn't
3727 * change if an error is thrown.
3730 insert(size_type __pos
, size_type __n
, _CharT __c
)
3731 { return _M_replace_aux(_M_check(__pos
, "basic_string::insert"),
3732 size_type(0), __n
, __c
); }
3735 * @brief Insert one character.
3736 * @param __p Iterator referencing position in string to insert at.
3737 * @param __c The character to insert.
3738 * @return Iterator referencing newly inserted char.
3739 * @throw std::length_error If new length exceeds @c max_size().
3741 * Inserts character @a __c at position referenced by @a __p.
3742 * If adding character causes the length to exceed max_size(),
3743 * length_error is thrown. If @a __p is beyond end of string,
3744 * out_of_range is thrown. The value of the string doesn't
3745 * change if an error is thrown.
3748 insert(iterator __p
, _CharT __c
)
3750 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
3751 const size_type __pos
= __p
- _M_ibegin();
3752 _M_replace_aux(__pos
, size_type(0), size_type(1), __c
);
3753 _M_rep()->_M_set_leaked();
3754 return iterator(_M_data() + __pos
);
3758 * @brief Remove characters.
3759 * @param __pos Index of first character to remove (default 0).
3760 * @param __n Number of characters to remove (default remainder).
3761 * @return Reference to this string.
3762 * @throw std::out_of_range If @a pos is beyond the end of this
3765 * Removes @a __n characters from this string starting at @a
3766 * __pos. The length of the string is reduced by @a __n. If
3767 * there are < @a __n characters to remove, the remainder of
3768 * the string is truncated. If @a __p is beyond end of string,
3769 * out_of_range is thrown. The value of the string doesn't
3770 * change if an error is thrown.
3773 erase(size_type __pos
= 0, size_type __n
= npos
)
3775 _M_mutate(_M_check(__pos
, "basic_string::erase"),
3776 _M_limit(__pos
, __n
), size_type(0));
3781 * @brief Remove one character.
3782 * @param __position Iterator referencing the character to remove.
3783 * @return iterator referencing same location after removal.
3785 * Removes the character at @a __position from this string. The value
3786 * of the string doesn't change if an error is thrown.
3789 erase(iterator __position
)
3791 _GLIBCXX_DEBUG_PEDASSERT(__position
>= _M_ibegin()
3792 && __position
< _M_iend());
3793 const size_type __pos
= __position
- _M_ibegin();
3794 _M_mutate(__pos
, size_type(1), size_type(0));
3795 _M_rep()->_M_set_leaked();
3796 return iterator(_M_data() + __pos
);
3800 * @brief Remove a range of characters.
3801 * @param __first Iterator referencing the first character to remove.
3802 * @param __last Iterator referencing the end of the range.
3803 * @return Iterator referencing location of first after removal.
3805 * Removes the characters in the range [first,last) from this string.
3806 * The value of the string doesn't change if an error is thrown.
3809 erase(iterator __first
, iterator __last
);
3811 #if __cplusplus >= 201103L
3813 * @brief Remove the last character.
3815 * The string must be non-empty.
3818 pop_back() // FIXME C++11: should be noexcept.
3819 { erase(size()-1, 1); }
3823 * @brief Replace characters with value from another string.
3824 * @param __pos Index of first character to replace.
3825 * @param __n Number of characters to be replaced.
3826 * @param __str String to insert.
3827 * @return Reference to this string.
3828 * @throw std::out_of_range If @a pos is beyond the end of this
3830 * @throw std::length_error If new length exceeds @c max_size().
3832 * Removes the characters in the range [__pos,__pos+__n) from
3833 * this string. In place, the value of @a __str is inserted.
3834 * If @a __pos is beyond end of string, out_of_range is thrown.
3835 * If the length of the result exceeds max_size(), length_error
3836 * is thrown. The value of the string doesn't change if an
3840 replace(size_type __pos
, size_type __n
, const basic_string
& __str
)
3841 { return this->replace(__pos
, __n
, __str
._M_data(), __str
.size()); }
3844 * @brief Replace characters with value from another string.
3845 * @param __pos1 Index of first character to replace.
3846 * @param __n1 Number of characters to be replaced.
3847 * @param __str String to insert.
3848 * @param __pos2 Index of first character of str to use.
3849 * @param __n2 Number of characters from str to use.
3850 * @return Reference to this string.
3851 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
3853 * @throw std::length_error If new length exceeds @c max_size().
3855 * Removes the characters in the range [__pos1,__pos1 + n) from this
3856 * string. In place, the value of @a __str is inserted. If @a __pos is
3857 * beyond end of string, out_of_range is thrown. If the length of the
3858 * result exceeds max_size(), length_error is thrown. The value of the
3859 * string doesn't change if an error is thrown.
3862 replace(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
3863 size_type __pos2
, size_type __n2
)
3864 { return this->replace(__pos1
, __n1
, __str
._M_data()
3865 + __str
._M_check(__pos2
, "basic_string::replace"),
3866 __str
._M_limit(__pos2
, __n2
)); }
3869 * @brief Replace characters with value of a C substring.
3870 * @param __pos Index of first character to replace.
3871 * @param __n1 Number of characters to be replaced.
3872 * @param __s C string to insert.
3873 * @param __n2 Number of characters from @a s to use.
3874 * @return Reference to this string.
3875 * @throw std::out_of_range If @a pos1 > size().
3876 * @throw std::length_error If new length exceeds @c max_size().
3878 * Removes the characters in the range [__pos,__pos + __n1)
3879 * from this string. In place, the first @a __n2 characters of
3880 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
3881 * @a __pos is beyond end of string, out_of_range is thrown. If
3882 * the length of result exceeds max_size(), length_error is
3883 * thrown. The value of the string doesn't change if an error
3887 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
3891 * @brief Replace characters with value of a C string.
3892 * @param __pos Index of first character to replace.
3893 * @param __n1 Number of characters to be replaced.
3894 * @param __s C string to insert.
3895 * @return Reference to this string.
3896 * @throw std::out_of_range If @a pos > size().
3897 * @throw std::length_error If new length exceeds @c max_size().
3899 * Removes the characters in the range [__pos,__pos + __n1)
3900 * from this string. In place, the characters of @a __s are
3901 * inserted. If @a __pos is beyond end of string, out_of_range
3902 * is thrown. If the length of result exceeds max_size(),
3903 * length_error is thrown. The value of the string doesn't
3904 * change if an error is thrown.
3907 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
)
3909 __glibcxx_requires_string(__s
);
3910 return this->replace(__pos
, __n1
, __s
, traits_type::length(__s
));
3914 * @brief Replace characters with multiple characters.
3915 * @param __pos Index of first character to replace.
3916 * @param __n1 Number of characters to be replaced.
3917 * @param __n2 Number of characters to insert.
3918 * @param __c Character to insert.
3919 * @return Reference to this string.
3920 * @throw std::out_of_range If @a __pos > size().
3921 * @throw std::length_error If new length exceeds @c max_size().
3923 * Removes the characters in the range [pos,pos + n1) from this
3924 * string. In place, @a __n2 copies of @a __c are inserted.
3925 * If @a __pos is beyond end of string, out_of_range is thrown.
3926 * If the length of result exceeds max_size(), length_error is
3927 * thrown. The value of the string doesn't change if an error
3931 replace(size_type __pos
, size_type __n1
, size_type __n2
, _CharT __c
)
3932 { return _M_replace_aux(_M_check(__pos
, "basic_string::replace"),
3933 _M_limit(__pos
, __n1
), __n2
, __c
); }
3936 * @brief Replace range of characters with string.
3937 * @param __i1 Iterator referencing start of range to replace.
3938 * @param __i2 Iterator referencing end of range to replace.
3939 * @param __str String value to insert.
3940 * @return Reference to this string.
3941 * @throw std::length_error If new length exceeds @c max_size().
3943 * Removes the characters in the range [__i1,__i2). In place,
3944 * the value of @a __str is inserted. If the length of result
3945 * exceeds max_size(), length_error is thrown. The value of
3946 * the string doesn't change if an error is thrown.
3949 replace(iterator __i1
, iterator __i2
, const basic_string
& __str
)
3950 { return this->replace(__i1
, __i2
, __str
._M_data(), __str
.size()); }
3953 * @brief Replace range of characters with C substring.
3954 * @param __i1 Iterator referencing start of range to replace.
3955 * @param __i2 Iterator referencing end of range to replace.
3956 * @param __s C string value to insert.
3957 * @param __n Number of characters from s to insert.
3958 * @return Reference to this string.
3959 * @throw std::length_error If new length exceeds @c max_size().
3961 * Removes the characters in the range [__i1,__i2). In place,
3962 * the first @a __n characters of @a __s are inserted. If the
3963 * length of result exceeds max_size(), length_error is thrown.
3964 * The value of the string doesn't change if an error is
3968 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
, size_type __n
)
3970 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
3971 && __i2
<= _M_iend());
3972 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
, __s
, __n
);
3976 * @brief Replace range of characters with C string.
3977 * @param __i1 Iterator referencing start of range to replace.
3978 * @param __i2 Iterator referencing end of range to replace.
3979 * @param __s C string value to insert.
3980 * @return Reference to this string.
3981 * @throw std::length_error If new length exceeds @c max_size().
3983 * Removes the characters in the range [__i1,__i2). In place,
3984 * the characters of @a __s are inserted. If the length of
3985 * result exceeds max_size(), length_error is thrown. The
3986 * value of the string doesn't change if an error is thrown.
3989 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
)
3991 __glibcxx_requires_string(__s
);
3992 return this->replace(__i1
, __i2
, __s
, traits_type::length(__s
));
3996 * @brief Replace range of characters with multiple characters
3997 * @param __i1 Iterator referencing start of range to replace.
3998 * @param __i2 Iterator referencing end of range to replace.
3999 * @param __n Number of characters to insert.
4000 * @param __c Character to insert.
4001 * @return Reference to this string.
4002 * @throw std::length_error If new length exceeds @c max_size().
4004 * Removes the characters in the range [__i1,__i2). In place,
4005 * @a __n copies of @a __c are inserted. If the length of
4006 * result exceeds max_size(), length_error is thrown. The
4007 * value of the string doesn't change if an error is thrown.
4010 replace(iterator __i1
, iterator __i2
, size_type __n
, _CharT __c
)
4012 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4013 && __i2
<= _M_iend());
4014 return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __c
);
4018 * @brief Replace range of characters with range.
4019 * @param __i1 Iterator referencing start of range to replace.
4020 * @param __i2 Iterator referencing end of range to replace.
4021 * @param __k1 Iterator referencing start of range to insert.
4022 * @param __k2 Iterator referencing end of range to insert.
4023 * @return Reference to this string.
4024 * @throw std::length_error If new length exceeds @c max_size().
4026 * Removes the characters in the range [__i1,__i2). In place,
4027 * characters in the range [__k1,__k2) are inserted. If the
4028 * length of result exceeds max_size(), length_error is thrown.
4029 * The value of the string doesn't change if an error is
4032 template<class _InputIterator
>
4034 replace(iterator __i1
, iterator __i2
,
4035 _InputIterator __k1
, _InputIterator __k2
)
4037 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4038 && __i2
<= _M_iend());
4039 __glibcxx_requires_valid_range(__k1
, __k2
);
4040 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
4041 return _M_replace_dispatch(__i1
, __i2
, __k1
, __k2
, _Integral());
4044 // Specializations for the common case of pointer and iterator:
4045 // useful to avoid the overhead of temporary buffering in _M_replace.
4047 replace(iterator __i1
, iterator __i2
, _CharT
* __k1
, _CharT
* __k2
)
4049 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4050 && __i2
<= _M_iend());
4051 __glibcxx_requires_valid_range(__k1
, __k2
);
4052 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4057 replace(iterator __i1
, iterator __i2
,
4058 const _CharT
* __k1
, const _CharT
* __k2
)
4060 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4061 && __i2
<= _M_iend());
4062 __glibcxx_requires_valid_range(__k1
, __k2
);
4063 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4068 replace(iterator __i1
, iterator __i2
, iterator __k1
, iterator __k2
)
4070 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4071 && __i2
<= _M_iend());
4072 __glibcxx_requires_valid_range(__k1
, __k2
);
4073 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4074 __k1
.base(), __k2
- __k1
);
4078 replace(iterator __i1
, iterator __i2
,
4079 const_iterator __k1
, const_iterator __k2
)
4081 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4082 && __i2
<= _M_iend());
4083 __glibcxx_requires_valid_range(__k1
, __k2
);
4084 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4085 __k1
.base(), __k2
- __k1
);
4088 #if __cplusplus >= 201103L
4090 * @brief Replace range of characters with initializer_list.
4091 * @param __i1 Iterator referencing start of range to replace.
4092 * @param __i2 Iterator referencing end of range to replace.
4093 * @param __l The initializer_list of characters to insert.
4094 * @return Reference to this string.
4095 * @throw std::length_error If new length exceeds @c max_size().
4097 * Removes the characters in the range [__i1,__i2). In place,
4098 * characters in the range [__k1,__k2) are inserted. If the
4099 * length of result exceeds max_size(), length_error is thrown.
4100 * The value of the string doesn't change if an error is
4103 basic_string
& replace(iterator __i1
, iterator __i2
,
4104 initializer_list
<_CharT
> __l
)
4105 { return this->replace(__i1
, __i2
, __l
.begin(), __l
.end()); }
4109 template<class _Integer
>
4111 _M_replace_dispatch(iterator __i1
, iterator __i2
, _Integer __n
,
4112 _Integer __val
, __true_type
)
4113 { return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __val
); }
4115 template<class _InputIterator
>
4117 _M_replace_dispatch(iterator __i1
, iterator __i2
, _InputIterator __k1
,
4118 _InputIterator __k2
, __false_type
);
4121 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
4125 _M_replace_safe(size_type __pos1
, size_type __n1
, const _CharT
* __s
,
4128 // _S_construct_aux is used to implement the 21.3.1 para 15 which
4129 // requires special behaviour if _InIter is an integral type
4130 template<class _InIterator
>
4132 _S_construct_aux(_InIterator __beg
, _InIterator __end
,
4133 const _Alloc
& __a
, __false_type
)
4135 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
4136 return _S_construct(__beg
, __end
, __a
, _Tag());
4139 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4140 // 438. Ambiguity in the "do the right thing" clause
4141 template<class _Integer
>
4143 _S_construct_aux(_Integer __beg
, _Integer __end
,
4144 const _Alloc
& __a
, __true_type
)
4145 { return _S_construct_aux_2(static_cast<size_type
>(__beg
),
4149 _S_construct_aux_2(size_type __req
, _CharT __c
, const _Alloc
& __a
)
4150 { return _S_construct(__req
, __c
, __a
); }
4152 template<class _InIterator
>
4154 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
)
4156 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
4157 return _S_construct_aux(__beg
, __end
, __a
, _Integral());
4160 // For Input Iterators, used in istreambuf_iterators, etc.
4161 template<class _InIterator
>
4163 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
,
4164 input_iterator_tag
);
4166 // For forward_iterators up to random_access_iterators, used for
4167 // string::iterator, _CharT*, etc.
4168 template<class _FwdIterator
>
4170 _S_construct(_FwdIterator __beg
, _FwdIterator __end
, const _Alloc
& __a
,
4171 forward_iterator_tag
);
4174 _S_construct(size_type __req
, _CharT __c
, const _Alloc
& __a
);
4179 * @brief Copy substring into C string.
4180 * @param __s C string to copy value into.
4181 * @param __n Number of characters to copy.
4182 * @param __pos Index of first character to copy.
4183 * @return Number of characters actually copied
4184 * @throw std::out_of_range If __pos > size().
4186 * Copies up to @a __n characters starting at @a __pos into the
4187 * C string @a __s. If @a __pos is %greater than size(),
4188 * out_of_range is thrown.
4191 copy(_CharT
* __s
, size_type __n
, size_type __pos
= 0) const;
4194 * @brief Swap contents with another string.
4195 * @param __s String to swap with.
4197 * Exchanges the contents of this string with that of @a __s in constant
4200 // PR 58265, this should be noexcept.
4202 swap(basic_string
& __s
);
4204 // String operations:
4206 * @brief Return const pointer to null-terminated contents.
4208 * This is a handle to internal data. Do not modify or dire things may
4212 c_str() const _GLIBCXX_NOEXCEPT
4213 { return _M_data(); }
4216 * @brief Return const pointer to contents.
4218 * This is a handle to internal data. Do not modify or dire things may
4222 data() const _GLIBCXX_NOEXCEPT
4223 { return _M_data(); }
4226 * @brief Return copy of allocator used to construct this string.
4229 get_allocator() const _GLIBCXX_NOEXCEPT
4230 { return _M_dataplus
; }
4233 * @brief Find position of a C substring.
4234 * @param __s C string to locate.
4235 * @param __pos Index of character to search from.
4236 * @param __n Number of characters from @a s to search for.
4237 * @return Index of start of first occurrence.
4239 * Starting from @a __pos, searches forward for the first @a
4240 * __n characters in @a __s within this string. If found,
4241 * returns the index where it begins. If not found, returns
4245 find(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
4248 * @brief Find position of a string.
4249 * @param __str String to locate.
4250 * @param __pos Index of character to search from (default 0).
4251 * @return Index of start of first occurrence.
4253 * Starting from @a __pos, searches forward for value of @a __str within
4254 * this string. If found, returns the index where it begins. If not
4255 * found, returns npos.
4258 find(const basic_string
& __str
, size_type __pos
= 0) const
4260 { return this->find(__str
.data(), __pos
, __str
.size()); }
4263 * @brief Find position of a C string.
4264 * @param __s C string to locate.
4265 * @param __pos Index of character to search from (default 0).
4266 * @return Index of start of first occurrence.
4268 * Starting from @a __pos, searches forward for the value of @a
4269 * __s within this string. If found, returns the index where
4270 * it begins. If not found, returns npos.
4273 find(const _CharT
* __s
, size_type __pos
= 0) const
4275 __glibcxx_requires_string(__s
);
4276 return this->find(__s
, __pos
, traits_type::length(__s
));
4280 * @brief Find position of a character.
4281 * @param __c Character to locate.
4282 * @param __pos Index of character to search from (default 0).
4283 * @return Index of first occurrence.
4285 * Starting from @a __pos, searches forward for @a __c within
4286 * this string. If found, returns the index where it was
4287 * found. If not found, returns npos.
4290 find(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
;
4293 * @brief Find last position of a string.
4294 * @param __str String to locate.
4295 * @param __pos Index of character to search back from (default end).
4296 * @return Index of start of last occurrence.
4298 * Starting from @a __pos, searches backward for value of @a
4299 * __str within this string. If found, returns the index where
4300 * it begins. If not found, returns npos.
4303 rfind(const basic_string
& __str
, size_type __pos
= npos
) const
4305 { return this->rfind(__str
.data(), __pos
, __str
.size()); }
4308 * @brief Find last position of a C substring.
4309 * @param __s C string to locate.
4310 * @param __pos Index of character to search back from.
4311 * @param __n Number of characters from s to search for.
4312 * @return Index of start of last occurrence.
4314 * Starting from @a __pos, searches backward for the first @a
4315 * __n characters in @a __s within this string. If found,
4316 * returns the index where it begins. If not found, returns
4320 rfind(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
4323 * @brief Find last position of a C string.
4324 * @param __s C string to locate.
4325 * @param __pos Index of character to start search at (default end).
4326 * @return Index of start of last occurrence.
4328 * Starting from @a __pos, searches backward for the value of
4329 * @a __s within this string. If found, returns the index
4330 * where it begins. If not found, returns npos.
4333 rfind(const _CharT
* __s
, size_type __pos
= npos
) const
4335 __glibcxx_requires_string(__s
);
4336 return this->rfind(__s
, __pos
, traits_type::length(__s
));
4340 * @brief Find last position of a character.
4341 * @param __c Character to locate.
4342 * @param __pos Index of character to search back from (default end).
4343 * @return Index of last occurrence.
4345 * Starting from @a __pos, searches backward for @a __c within
4346 * this string. If found, returns the index where it was
4347 * found. If not found, returns npos.
4350 rfind(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
;
4353 * @brief Find position of a character of string.
4354 * @param __str String containing characters to locate.
4355 * @param __pos Index of character to search from (default 0).
4356 * @return Index of first occurrence.
4358 * Starting from @a __pos, searches forward for one of the
4359 * characters of @a __str within this string. If found,
4360 * returns the index where it was found. If not found, returns
4364 find_first_of(const basic_string
& __str
, size_type __pos
= 0) const
4366 { return this->find_first_of(__str
.data(), __pos
, __str
.size()); }
4369 * @brief Find position of a character of C substring.
4370 * @param __s String containing characters to locate.
4371 * @param __pos Index of character to search from.
4372 * @param __n Number of characters from s to search for.
4373 * @return Index of first occurrence.
4375 * Starting from @a __pos, searches forward for one of the
4376 * first @a __n characters of @a __s within this string. If
4377 * found, returns the index where it was found. If not found,
4381 find_first_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
4384 * @brief Find position of a character of C string.
4385 * @param __s String containing characters to locate.
4386 * @param __pos Index of character to search from (default 0).
4387 * @return Index of first occurrence.
4389 * Starting from @a __pos, searches forward for one of the
4390 * characters of @a __s within this string. If found, returns
4391 * the index where it was found. If not found, returns npos.
4394 find_first_of(const _CharT
* __s
, size_type __pos
= 0) const
4396 __glibcxx_requires_string(__s
);
4397 return this->find_first_of(__s
, __pos
, traits_type::length(__s
));
4401 * @brief Find position of a character.
4402 * @param __c Character to locate.
4403 * @param __pos Index of character to search from (default 0).
4404 * @return Index of first occurrence.
4406 * Starting from @a __pos, searches forward for the character
4407 * @a __c within this string. If found, returns the index
4408 * where it was found. If not found, returns npos.
4410 * Note: equivalent to find(__c, __pos).
4413 find_first_of(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
4414 { return this->find(__c
, __pos
); }
4417 * @brief Find last position of a character of string.
4418 * @param __str String containing characters to locate.
4419 * @param __pos Index of character to search back from (default end).
4420 * @return Index of last occurrence.
4422 * Starting from @a __pos, searches backward for one of the
4423 * characters of @a __str within this string. If found,
4424 * returns the index where it was found. If not found, returns
4428 find_last_of(const basic_string
& __str
, size_type __pos
= npos
) const
4430 { return this->find_last_of(__str
.data(), __pos
, __str
.size()); }
4433 * @brief Find last position of a character of C substring.
4434 * @param __s C string containing characters to locate.
4435 * @param __pos Index of character to search back from.
4436 * @param __n Number of characters from s to search for.
4437 * @return Index of last occurrence.
4439 * Starting from @a __pos, searches backward for one of the
4440 * first @a __n characters of @a __s within this string. If
4441 * found, returns the index where it was found. If not found,
4445 find_last_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
4448 * @brief Find last position of a character of C string.
4449 * @param __s C string containing characters to locate.
4450 * @param __pos Index of character to search back from (default end).
4451 * @return Index of last occurrence.
4453 * Starting from @a __pos, searches backward for one of the
4454 * characters of @a __s within this string. If found, returns
4455 * the index where it was found. If not found, returns npos.
4458 find_last_of(const _CharT
* __s
, size_type __pos
= npos
) const
4460 __glibcxx_requires_string(__s
);
4461 return this->find_last_of(__s
, __pos
, traits_type::length(__s
));
4465 * @brief Find last position of a character.
4466 * @param __c Character to locate.
4467 * @param __pos Index of character to search back from (default end).
4468 * @return Index of last occurrence.
4470 * Starting from @a __pos, searches backward for @a __c within
4471 * this string. If found, returns the index where it was
4472 * found. If not found, returns npos.
4474 * Note: equivalent to rfind(__c, __pos).
4477 find_last_of(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
4478 { return this->rfind(__c
, __pos
); }
4481 * @brief Find position of a character not in string.
4482 * @param __str String containing characters to avoid.
4483 * @param __pos Index of character to search from (default 0).
4484 * @return Index of first occurrence.
4486 * Starting from @a __pos, searches forward for a character not contained
4487 * in @a __str within this string. If found, returns the index where it
4488 * was found. If not found, returns npos.
4491 find_first_not_of(const basic_string
& __str
, size_type __pos
= 0) const
4493 { return this->find_first_not_of(__str
.data(), __pos
, __str
.size()); }
4496 * @brief Find position of a character not in C substring.
4497 * @param __s C string containing characters to avoid.
4498 * @param __pos Index of character to search from.
4499 * @param __n Number of characters from __s to consider.
4500 * @return Index of first occurrence.
4502 * Starting from @a __pos, searches forward for a character not
4503 * contained in the first @a __n characters of @a __s within
4504 * this string. If found, returns the index where it was
4505 * found. If not found, returns npos.
4508 find_first_not_of(const _CharT
* __s
, size_type __pos
,
4509 size_type __n
) const;
4512 * @brief Find position of a character not in C string.
4513 * @param __s C string containing characters to avoid.
4514 * @param __pos Index of character to search from (default 0).
4515 * @return Index of first occurrence.
4517 * Starting from @a __pos, searches forward for a character not
4518 * contained in @a __s within this string. If found, returns
4519 * the index where it was found. If not found, returns npos.
4522 find_first_not_of(const _CharT
* __s
, size_type __pos
= 0) const
4524 __glibcxx_requires_string(__s
);
4525 return this->find_first_not_of(__s
, __pos
, traits_type::length(__s
));
4529 * @brief Find position of a different character.
4530 * @param __c Character to avoid.
4531 * @param __pos Index of character to search from (default 0).
4532 * @return Index of first occurrence.
4534 * Starting from @a __pos, searches forward for a character
4535 * other than @a __c within this string. If found, returns the
4536 * index where it was found. If not found, returns npos.
4539 find_first_not_of(_CharT __c
, size_type __pos
= 0) const
4543 * @brief Find last position of a character not in string.
4544 * @param __str String containing characters to avoid.
4545 * @param __pos Index of character to search back from (default end).
4546 * @return Index of last occurrence.
4548 * Starting from @a __pos, searches backward for a character
4549 * not contained in @a __str within this string. If found,
4550 * returns the index where it was found. If not found, returns
4554 find_last_not_of(const basic_string
& __str
, size_type __pos
= npos
) const
4556 { return this->find_last_not_of(__str
.data(), __pos
, __str
.size()); }
4559 * @brief Find last position of a character not in C substring.
4560 * @param __s C string containing characters to avoid.
4561 * @param __pos Index of character to search back from.
4562 * @param __n Number of characters from s to consider.
4563 * @return Index of last occurrence.
4565 * Starting from @a __pos, searches backward for a character not
4566 * contained in the first @a __n characters of @a __s within this string.
4567 * If found, returns the index where it was found. If not found,
4571 find_last_not_of(const _CharT
* __s
, size_type __pos
,
4572 size_type __n
) const;
4574 * @brief Find last position of a character not in C string.
4575 * @param __s C string containing characters to avoid.
4576 * @param __pos Index of character to search back from (default end).
4577 * @return Index of last occurrence.
4579 * Starting from @a __pos, searches backward for a character
4580 * not contained in @a __s within this string. If found,
4581 * returns the index where it was found. If not found, returns
4585 find_last_not_of(const _CharT
* __s
, size_type __pos
= npos
) const
4587 __glibcxx_requires_string(__s
);
4588 return this->find_last_not_of(__s
, __pos
, traits_type::length(__s
));
4592 * @brief Find last position of a different character.
4593 * @param __c Character to avoid.
4594 * @param __pos Index of character to search back from (default end).
4595 * @return Index of last occurrence.
4597 * Starting from @a __pos, searches backward for a character other than
4598 * @a __c within this string. If found, returns the index where it was
4599 * found. If not found, returns npos.
4602 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const
4606 * @brief Get a substring.
4607 * @param __pos Index of first character (default 0).
4608 * @param __n Number of characters in substring (default remainder).
4609 * @return The new string.
4610 * @throw std::out_of_range If __pos > size().
4612 * Construct and return a new string using the @a __n
4613 * characters starting at @a __pos. If the string is too
4614 * short, use the remainder of the characters. If @a __pos is
4615 * beyond the end of the string, out_of_range is thrown.
4618 substr(size_type __pos
= 0, size_type __n
= npos
) const
4619 { return basic_string(*this,
4620 _M_check(__pos
, "basic_string::substr"), __n
); }
4623 * @brief Compare to a string.
4624 * @param __str String to compare against.
4625 * @return Integer < 0, 0, or > 0.
4627 * Returns an integer < 0 if this string is ordered before @a
4628 * __str, 0 if their values are equivalent, or > 0 if this
4629 * string is ordered after @a __str. Determines the effective
4630 * length rlen of the strings to compare as the smallest of
4631 * size() and str.size(). The function then compares the two
4632 * strings by calling traits::compare(data(), str.data(),rlen).
4633 * If the result of the comparison is nonzero returns it,
4634 * otherwise the shorter one is ordered first.
4637 compare(const basic_string
& __str
) const
4639 const size_type __size
= this->size();
4640 const size_type __osize
= __str
.size();
4641 const size_type __len
= std::min(__size
, __osize
);
4643 int __r
= traits_type::compare(_M_data(), __str
.data(), __len
);
4645 __r
= _S_compare(__size
, __osize
);
4650 * @brief Compare substring to a string.
4651 * @param __pos Index of first character of substring.
4652 * @param __n Number of characters in substring.
4653 * @param __str String to compare against.
4654 * @return Integer < 0, 0, or > 0.
4656 * Form the substring of this string from the @a __n characters
4657 * starting at @a __pos. Returns an integer < 0 if the
4658 * substring is ordered before @a __str, 0 if their values are
4659 * equivalent, or > 0 if the substring is ordered after @a
4660 * __str. Determines the effective length rlen of the strings
4661 * to compare as the smallest of the length of the substring
4662 * and @a __str.size(). The function then compares the two
4663 * strings by calling
4664 * traits::compare(substring.data(),str.data(),rlen). If the
4665 * result of the comparison is nonzero returns it, otherwise
4666 * the shorter one is ordered first.
4669 compare(size_type __pos
, size_type __n
, const basic_string
& __str
) const;
4672 * @brief Compare substring to a substring.
4673 * @param __pos1 Index of first character of substring.
4674 * @param __n1 Number of characters in substring.
4675 * @param __str String to compare against.
4676 * @param __pos2 Index of first character of substring of str.
4677 * @param __n2 Number of characters in substring of str.
4678 * @return Integer < 0, 0, or > 0.
4680 * Form the substring of this string from the @a __n1
4681 * characters starting at @a __pos1. Form the substring of @a
4682 * __str from the @a __n2 characters starting at @a __pos2.
4683 * Returns an integer < 0 if this substring is ordered before
4684 * the substring of @a __str, 0 if their values are equivalent,
4685 * or > 0 if this substring is ordered after the substring of
4686 * @a __str. Determines the effective length rlen of the
4687 * strings to compare as the smallest of the lengths of the
4688 * substrings. The function then compares the two strings by
4690 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
4691 * If the result of the comparison is nonzero returns it,
4692 * otherwise the shorter one is ordered first.
4695 compare(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
4696 size_type __pos2
, size_type __n2
) const;
4699 * @brief Compare to a C string.
4700 * @param __s C string to compare against.
4701 * @return Integer < 0, 0, or > 0.
4703 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
4704 * their values are equivalent, or > 0 if this string is ordered after
4705 * @a __s. Determines the effective length rlen of the strings to
4706 * compare as the smallest of size() and the length of a string
4707 * constructed from @a __s. The function then compares the two strings
4708 * by calling traits::compare(data(),s,rlen). If the result of the
4709 * comparison is nonzero returns it, otherwise the shorter one is
4713 compare(const _CharT
* __s
) const;
4715 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4716 // 5 String::compare specification questionable
4718 * @brief Compare substring to a C string.
4719 * @param __pos Index of first character of substring.
4720 * @param __n1 Number of characters in substring.
4721 * @param __s C string to compare against.
4722 * @return Integer < 0, 0, or > 0.
4724 * Form the substring of this string from the @a __n1
4725 * characters starting at @a pos. Returns an integer < 0 if
4726 * the substring is ordered before @a __s, 0 if their values
4727 * are equivalent, or > 0 if the substring is ordered after @a
4728 * __s. Determines the effective length rlen of the strings to
4729 * compare as the smallest of the length of the substring and
4730 * the length of a string constructed from @a __s. The
4731 * function then compares the two string by calling
4732 * traits::compare(substring.data(),__s,rlen). If the result of
4733 * the comparison is nonzero returns it, otherwise the shorter
4734 * one is ordered first.
4737 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
) const;
4740 * @brief Compare substring against a character %array.
4741 * @param __pos Index of first character of substring.
4742 * @param __n1 Number of characters in substring.
4743 * @param __s character %array to compare against.
4744 * @param __n2 Number of characters of s.
4745 * @return Integer < 0, 0, or > 0.
4747 * Form the substring of this string from the @a __n1
4748 * characters starting at @a __pos. Form a string from the
4749 * first @a __n2 characters of @a __s. Returns an integer < 0
4750 * if this substring is ordered before the string from @a __s,
4751 * 0 if their values are equivalent, or > 0 if this substring
4752 * is ordered after the string from @a __s. Determines the
4753 * effective length rlen of the strings to compare as the
4754 * smallest of the length of the substring and @a __n2. The
4755 * function then compares the two strings by calling
4756 * traits::compare(substring.data(),s,rlen). If the result of
4757 * the comparison is nonzero returns it, otherwise the shorter
4758 * one is ordered first.
4760 * NB: s must have at least n2 characters, '\\0' has
4761 * no special meaning.
4764 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
,
4765 size_type __n2
) const;
4767 #endif // !_GLIBCXX_USE_CXX11_ABI
4771 * @brief Concatenate two strings.
4772 * @param __lhs First string.
4773 * @param __rhs Last string.
4774 * @return New string with value of @a __lhs followed by @a __rhs.
4776 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4777 basic_string
<_CharT
, _Traits
, _Alloc
>
4778 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
4779 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
4781 basic_string
<_CharT
, _Traits
, _Alloc
> __str(__lhs
);
4782 __str
.append(__rhs
);
4787 * @brief Concatenate C string and string.
4788 * @param __lhs First string.
4789 * @param __rhs Last string.
4790 * @return New string with value of @a __lhs followed by @a __rhs.
4792 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4793 basic_string
<_CharT
,_Traits
,_Alloc
>
4794 operator+(const _CharT
* __lhs
,
4795 const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
);
4798 * @brief Concatenate character and string.
4799 * @param __lhs First string.
4800 * @param __rhs Last string.
4801 * @return New string with @a __lhs followed by @a __rhs.
4803 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4804 basic_string
<_CharT
,_Traits
,_Alloc
>
4805 operator+(_CharT __lhs
, const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
);
4808 * @brief Concatenate string and C string.
4809 * @param __lhs First string.
4810 * @param __rhs Last string.
4811 * @return New string with @a __lhs followed by @a __rhs.
4813 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4814 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4815 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
4816 const _CharT
* __rhs
)
4818 basic_string
<_CharT
, _Traits
, _Alloc
> __str(__lhs
);
4819 __str
.append(__rhs
);
4824 * @brief Concatenate string and character.
4825 * @param __lhs First string.
4826 * @param __rhs Last string.
4827 * @return New string with @a __lhs followed by @a __rhs.
4829 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4830 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4831 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
, _CharT __rhs
)
4833 typedef basic_string
<_CharT
, _Traits
, _Alloc
> __string_type
;
4834 typedef typename
__string_type::size_type __size_type
;
4835 __string_type
__str(__lhs
);
4836 __str
.append(__size_type(1), __rhs
);
4840 #if __cplusplus >= 201103L
4841 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4842 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4843 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
4844 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
4845 { return std::move(__lhs
.append(__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 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
4851 { return std::move(__rhs
.insert(0, __lhs
)); }
4853 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4854 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4855 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
4856 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
4858 const auto __size
= __lhs
.size() + __rhs
.size();
4859 const bool __cond
= (__size
> __lhs
.capacity()
4860 && __size
<= __rhs
.capacity());
4861 return __cond
? std::move(__rhs
.insert(0, __lhs
))
4862 : std::move(__lhs
.append(__rhs
));
4865 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4866 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4867 operator+(const _CharT
* __lhs
,
4868 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
4869 { return std::move(__rhs
.insert(0, __lhs
)); }
4871 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4872 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4873 operator+(_CharT __lhs
,
4874 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
4875 { return std::move(__rhs
.insert(0, 1, __lhs
)); }
4877 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4878 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4879 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
4880 const _CharT
* __rhs
)
4881 { return std::move(__lhs
.append(__rhs
)); }
4883 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4884 inline basic_string
<_CharT
, _Traits
, _Alloc
>
4885 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
4887 { return std::move(__lhs
.append(1, __rhs
)); }
4892 * @brief Test equivalence of two strings.
4893 * @param __lhs First string.
4894 * @param __rhs Second string.
4895 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
4897 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4899 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
4900 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
4901 { return __lhs
.compare(__rhs
) == 0; }
4903 template<typename _CharT
>
4905 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
, bool>::__type
4906 operator==(const basic_string
<_CharT
>& __lhs
,
4907 const basic_string
<_CharT
>& __rhs
)
4908 { return (__lhs
.size() == __rhs
.size()
4909 && !std::char_traits
<_CharT
>::compare(__lhs
.data(), __rhs
.data(),
4913 * @brief Test equivalence of C string and string.
4914 * @param __lhs C string.
4915 * @param __rhs String.
4916 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
4918 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4920 operator==(const _CharT
* __lhs
,
4921 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
4922 { return __rhs
.compare(__lhs
) == 0; }
4925 * @brief Test equivalence of string and C string.
4926 * @param __lhs String.
4927 * @param __rhs C string.
4928 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
4930 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4932 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
4933 const _CharT
* __rhs
)
4934 { return __lhs
.compare(__rhs
) == 0; }
4938 * @brief Test difference of two strings.
4939 * @param __lhs First string.
4940 * @param __rhs Second string.
4941 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
4943 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4945 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
4946 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
4947 { return !(__lhs
== __rhs
); }
4950 * @brief Test difference of C string and string.
4951 * @param __lhs C string.
4952 * @param __rhs String.
4953 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
4955 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4957 operator!=(const _CharT
* __lhs
,
4958 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
4959 { return !(__lhs
== __rhs
); }
4962 * @brief Test difference of string and C string.
4963 * @param __lhs String.
4964 * @param __rhs C string.
4965 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
4967 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4969 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
4970 const _CharT
* __rhs
)
4971 { return !(__lhs
== __rhs
); }
4975 * @brief Test if string precedes string.
4976 * @param __lhs First string.
4977 * @param __rhs Second string.
4978 * @return True if @a __lhs precedes @a __rhs. False otherwise.
4980 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4982 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
4983 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
4984 { return __lhs
.compare(__rhs
) < 0; }
4987 * @brief Test if string precedes C string.
4988 * @param __lhs String.
4989 * @param __rhs C string.
4990 * @return True if @a __lhs precedes @a __rhs. False otherwise.
4992 template<typename _CharT
, typename _Traits
, typename _Alloc
>
4994 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
4995 const _CharT
* __rhs
)
4996 { return __lhs
.compare(__rhs
) < 0; }
4999 * @brief Test if C string precedes string.
5000 * @param __lhs C string.
5001 * @param __rhs String.
5002 * @return True if @a __lhs precedes @a __rhs. False otherwise.
5004 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5006 operator<(const _CharT
* __lhs
,
5007 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5008 { return __rhs
.compare(__lhs
) > 0; }
5012 * @brief Test if string follows string.
5013 * @param __lhs First string.
5014 * @param __rhs Second string.
5015 * @return True if @a __lhs follows @a __rhs. False otherwise.
5017 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5019 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5020 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5021 { return __lhs
.compare(__rhs
) > 0; }
5024 * @brief Test if string follows C string.
5025 * @param __lhs String.
5026 * @param __rhs C string.
5027 * @return True if @a __lhs follows @a __rhs. False otherwise.
5029 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5031 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5032 const _CharT
* __rhs
)
5033 { return __lhs
.compare(__rhs
) > 0; }
5036 * @brief Test if C string follows string.
5037 * @param __lhs C string.
5038 * @param __rhs String.
5039 * @return True if @a __lhs follows @a __rhs. False otherwise.
5041 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5043 operator>(const _CharT
* __lhs
,
5044 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5045 { return __rhs
.compare(__lhs
) < 0; }
5049 * @brief Test if string doesn't follow string.
5050 * @param __lhs First string.
5051 * @param __rhs Second string.
5052 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5054 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5056 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5057 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5058 { return __lhs
.compare(__rhs
) <= 0; }
5061 * @brief Test if string doesn't follow C string.
5062 * @param __lhs String.
5063 * @param __rhs C string.
5064 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5066 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5068 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5069 const _CharT
* __rhs
)
5070 { return __lhs
.compare(__rhs
) <= 0; }
5073 * @brief Test if C string doesn't follow string.
5074 * @param __lhs C string.
5075 * @param __rhs String.
5076 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5078 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5080 operator<=(const _CharT
* __lhs
,
5081 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5082 { return __rhs
.compare(__lhs
) >= 0; }
5086 * @brief Test if string doesn't precede string.
5087 * @param __lhs First string.
5088 * @param __rhs Second string.
5089 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
5091 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5093 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5094 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5095 { return __lhs
.compare(__rhs
) >= 0; }
5098 * @brief Test if string doesn't precede C string.
5099 * @param __lhs String.
5100 * @param __rhs C string.
5101 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
5103 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5105 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5106 const _CharT
* __rhs
)
5107 { return __lhs
.compare(__rhs
) >= 0; }
5110 * @brief Test if C string doesn't precede string.
5111 * @param __lhs C string.
5112 * @param __rhs String.
5113 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
5115 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5117 operator>=(const _CharT
* __lhs
,
5118 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5119 { return __rhs
.compare(__lhs
) <= 0; }
5122 * @brief Swap contents of two strings.
5123 * @param __lhs First string.
5124 * @param __rhs Second string.
5126 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
5128 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5130 swap(basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5131 basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5132 { __lhs
.swap(__rhs
); }
5136 * @brief Read stream into a string.
5137 * @param __is Input stream.
5138 * @param __str Buffer to store into.
5139 * @return Reference to the input stream.
5141 * Stores characters from @a __is into @a __str until whitespace is
5142 * found, the end of the stream is encountered, or str.max_size()
5143 * is reached. If is.width() is non-zero, that is the limit on the
5144 * number of characters stored into @a __str. Any previous
5145 * contents of @a __str are erased.
5147 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5148 basic_istream
<_CharT
, _Traits
>&
5149 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
5150 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
);
5153 basic_istream
<char>&
5154 operator>>(basic_istream
<char>& __is
, basic_string
<char>& __str
);
5157 * @brief Write string to a stream.
5158 * @param __os Output stream.
5159 * @param __str String to write out.
5160 * @return Reference to the output stream.
5162 * Output characters of @a __str into os following the same rules as for
5163 * writing a C string.
5165 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5166 inline basic_ostream
<_CharT
, _Traits
>&
5167 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
5168 const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
5170 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5171 // 586. string inserter not a formatted function
5172 return __ostream_insert(__os
, __str
.data(), __str
.size());
5176 * @brief Read a line from stream into a string.
5177 * @param __is Input stream.
5178 * @param __str Buffer to store into.
5179 * @param __delim Character marking end of line.
5180 * @return Reference to the input stream.
5182 * Stores characters from @a __is into @a __str until @a __delim is
5183 * found, the end of the stream is encountered, or str.max_size()
5184 * is reached. Any previous contents of @a __str are erased. If
5185 * @a __delim is encountered, it is extracted but not stored into
5188 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5189 basic_istream
<_CharT
, _Traits
>&
5190 getline(basic_istream
<_CharT
, _Traits
>& __is
,
5191 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
);
5194 * @brief Read a line from stream into a string.
5195 * @param __is Input stream.
5196 * @param __str Buffer to store into.
5197 * @return Reference to the input stream.
5199 * Stores characters from is into @a __str until '\n' is
5200 * found, the end of the stream is encountered, or str.max_size()
5201 * is reached. Any previous contents of @a __str are erased. If
5202 * end of line is encountered, it is extracted but not stored into
5205 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5206 inline basic_istream
<_CharT
, _Traits
>&
5207 getline(basic_istream
<_CharT
, _Traits
>& __is
,
5208 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
5209 { return std::getline(__is
, __str
, __is
.widen('\n')); }
5211 #if __cplusplus >= 201103L
5212 /// Read a line from an rvalue stream into a string.
5213 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5214 inline basic_istream
<_CharT
, _Traits
>&
5215 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
5216 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
)
5217 { return std::getline(__is
, __str
, __delim
); }
5219 /// Read a line from an rvalue stream into a string.
5220 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5221 inline basic_istream
<_CharT
, _Traits
>&
5222 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
5223 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
5224 { return std::getline(__is
, __str
); }
5228 basic_istream
<char>&
5229 getline(basic_istream
<char>& __in
, basic_string
<char>& __str
,
5232 #ifdef _GLIBCXX_USE_WCHAR_T
5234 basic_istream
<wchar_t>&
5235 getline(basic_istream
<wchar_t>& __in
, basic_string
<wchar_t>& __str
,
5239 _GLIBCXX_END_NAMESPACE_VERSION
5242 #if __cplusplus >= 201103L && defined(_GLIBCXX_USE_C99)
5244 #include <ext/string_conversions.h>
5246 namespace std
_GLIBCXX_VISIBILITY(default)
5248 _GLIBCXX_BEGIN_NAMESPACE_VERSION
5249 _GLIBCXX_BEGIN_NAMESPACE_CXX11
5251 // 21.4 Numeric Conversions [string.conversions].
5253 stoi(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
5254 { return __gnu_cxx::__stoa
<long, int>(&std::strtol
, "stoi", __str
.c_str(),
5258 stol(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
5259 { return __gnu_cxx::__stoa(&std::strtol
, "stol", __str
.c_str(),
5262 inline unsigned long
5263 stoul(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
5264 { return __gnu_cxx::__stoa(&std::strtoul
, "stoul", __str
.c_str(),
5268 stoll(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
5269 { return __gnu_cxx::__stoa(&std::strtoll
, "stoll", __str
.c_str(),
5272 inline unsigned long long
5273 stoull(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
5274 { return __gnu_cxx::__stoa(&std::strtoull
, "stoull", __str
.c_str(),
5277 // NB: strtof vs strtod.
5279 stof(const string
& __str
, size_t* __idx
= 0)
5280 { return __gnu_cxx::__stoa(&std::strtof
, "stof", __str
.c_str(), __idx
); }
5283 stod(const string
& __str
, size_t* __idx
= 0)
5284 { return __gnu_cxx::__stoa(&std::strtod
, "stod", __str
.c_str(), __idx
); }
5287 stold(const string
& __str
, size_t* __idx
= 0)
5288 { return __gnu_cxx::__stoa(&std::strtold
, "stold", __str
.c_str(), __idx
); }
5290 // NB: (v)snprintf vs sprintf.
5294 to_string(int __val
)
5295 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, 4 * sizeof(int),
5299 to_string(unsigned __val
)
5300 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
5301 4 * sizeof(unsigned),
5305 to_string(long __val
)
5306 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, 4 * sizeof(long),
5310 to_string(unsigned long __val
)
5311 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
5312 4 * sizeof(unsigned long),
5316 to_string(long long __val
)
5317 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
5318 4 * sizeof(long long),
5322 to_string(unsigned long long __val
)
5323 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
5324 4 * sizeof(unsigned long long),
5328 to_string(float __val
)
5331 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
5332 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
5337 to_string(double __val
)
5340 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
5341 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
5346 to_string(long double __val
)
5349 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
5350 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
5354 #ifdef _GLIBCXX_USE_WCHAR_T
5356 stoi(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
5357 { return __gnu_cxx::__stoa
<long, int>(&std::wcstol
, "stoi", __str
.c_str(),
5361 stol(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
5362 { return __gnu_cxx::__stoa(&std::wcstol
, "stol", __str
.c_str(),
5365 inline unsigned long
5366 stoul(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
5367 { return __gnu_cxx::__stoa(&std::wcstoul
, "stoul", __str
.c_str(),
5371 stoll(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
5372 { return __gnu_cxx::__stoa(&std::wcstoll
, "stoll", __str
.c_str(),
5375 inline unsigned long long
5376 stoull(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
5377 { return __gnu_cxx::__stoa(&std::wcstoull
, "stoull", __str
.c_str(),
5380 // NB: wcstof vs wcstod.
5382 stof(const wstring
& __str
, size_t* __idx
= 0)
5383 { return __gnu_cxx::__stoa(&std::wcstof
, "stof", __str
.c_str(), __idx
); }
5386 stod(const wstring
& __str
, size_t* __idx
= 0)
5387 { return __gnu_cxx::__stoa(&std::wcstod
, "stod", __str
.c_str(), __idx
); }
5390 stold(const wstring
& __str
, size_t* __idx
= 0)
5391 { return __gnu_cxx::__stoa(&std::wcstold
, "stold", __str
.c_str(), __idx
); }
5393 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
5396 to_wstring(int __val
)
5397 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(int),
5401 to_wstring(unsigned __val
)
5402 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
5403 4 * sizeof(unsigned),
5407 to_wstring(long __val
)
5408 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(long),
5412 to_wstring(unsigned long __val
)
5413 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
5414 4 * sizeof(unsigned long),
5418 to_wstring(long long __val
)
5419 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
5420 4 * sizeof(long long),
5424 to_wstring(unsigned long long __val
)
5425 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
5426 4 * sizeof(unsigned long long),
5430 to_wstring(float __val
)
5433 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
5434 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
5439 to_wstring(double __val
)
5442 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
5443 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
5448 to_wstring(long double __val
)
5451 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
5452 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
5455 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
5458 _GLIBCXX_END_NAMESPACE_CXX11
5459 _GLIBCXX_END_NAMESPACE_VERSION
5462 #endif /* C++11 && _GLIBCXX_USE_C99 ... */
5464 #if __cplusplus >= 201103L
5466 #include <bits/functional_hash.h>
5468 namespace std
_GLIBCXX_VISIBILITY(default)
5470 _GLIBCXX_BEGIN_NAMESPACE_VERSION
5474 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
5475 /// std::hash specialization for string.
5478 : public __hash_base
<size_t, string
>
5481 operator()(const string
& __s
) const noexcept
5482 { return std::_Hash_impl::hash(__s
.data(), __s
.length()); }
5486 struct __is_fast_hash
<hash
<string
>> : std::false_type
5489 #ifdef _GLIBCXX_USE_WCHAR_T
5490 /// std::hash specialization for wstring.
5492 struct hash
<wstring
>
5493 : public __hash_base
<size_t, wstring
>
5496 operator()(const wstring
& __s
) const noexcept
5497 { return std::_Hash_impl::hash(__s
.data(),
5498 __s
.length() * sizeof(wchar_t)); }
5502 struct __is_fast_hash
<hash
<wstring
>> : std::false_type
5505 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
5507 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
5508 /// std::hash specialization for u16string.
5510 struct hash
<u16string
>
5511 : public __hash_base
<size_t, u16string
>
5514 operator()(const u16string
& __s
) const noexcept
5515 { return std::_Hash_impl::hash(__s
.data(),
5516 __s
.length() * sizeof(char16_t
)); }
5520 struct __is_fast_hash
<hash
<u16string
>> : std::false_type
5523 /// std::hash specialization for u32string.
5525 struct hash
<u32string
>
5526 : public __hash_base
<size_t, u32string
>
5529 operator()(const u32string
& __s
) const noexcept
5530 { return std::_Hash_impl::hash(__s
.data(),
5531 __s
.length() * sizeof(char32_t
)); }
5535 struct __is_fast_hash
<hash
<u32string
>> : std::false_type
5539 #if __cplusplus > 201103L
5541 #define __cpp_lib_string_udls 201304
5543 inline namespace literals
5545 inline namespace string_literals
5548 _GLIBCXX_DEFAULT_ABI_TAG
5549 inline basic_string
<char>
5550 operator""s(const char* __str
, size_t __len
)
5551 { return basic_string
<char>{__str
, __len
}; }
5553 #ifdef _GLIBCXX_USE_WCHAR_T
5554 _GLIBCXX_DEFAULT_ABI_TAG
5555 inline basic_string
<wchar_t>
5556 operator""s(const wchar_t* __str
, size_t __len
)
5557 { return basic_string
<wchar_t>{__str
, __len
}; }
5560 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
5561 _GLIBCXX_DEFAULT_ABI_TAG
5562 inline basic_string
<char16_t
>
5563 operator""s(const char16_t
* __str
, size_t __len
)
5564 { return basic_string
<char16_t
>{__str
, __len
}; }
5566 _GLIBCXX_DEFAULT_ABI_TAG
5567 inline basic_string
<char32_t
>
5568 operator""s(const char32_t
* __str
, size_t __len
)
5569 { return basic_string
<char32_t
>{__str
, __len
}; }
5572 } // inline namespace string_literals
5573 } // inline namespace literals
5575 #endif // __cplusplus > 201103L
5577 _GLIBCXX_END_NAMESPACE_VERSION
5582 #endif /* _BASIC_STRING_H */