1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997-2016 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file bits/basic_string.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{string}
31 // ISO C++ 14882: 21 Strings library
34 #ifndef _BASIC_STRING_H
35 #define _BASIC_STRING_H 1
37 #pragma GCC system_header
39 #include <ext/atomicity.h>
40 #include <ext/alloc_traits.h>
41 #include <debug/debug.h>
43 #if __cplusplus >= 201103L
44 #include <initializer_list>
47 #if __cplusplus > 201402L
48 # include <string_view>
52 namespace std
_GLIBCXX_VISIBILITY(default)
54 _GLIBCXX_BEGIN_NAMESPACE_VERSION
56 #if _GLIBCXX_USE_CXX11_ABI
57 _GLIBCXX_BEGIN_NAMESPACE_CXX11
59 * @class basic_string basic_string.h <string>
60 * @brief Managing sequences of characters and character-like objects.
65 * @tparam _CharT Type of character
66 * @tparam _Traits Traits for character type, defaults to
67 * char_traits<_CharT>.
68 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
70 * Meets the requirements of a <a href="tables.html#65">container</a>, a
71 * <a href="tables.html#66">reversible container</a>, and a
72 * <a href="tables.html#67">sequence</a>. Of the
73 * <a href="tables.html#68">optional sequence requirements</a>, only
74 * @c push_back, @c at, and @c %array access are supported.
76 template<typename _CharT
, typename _Traits
, typename _Alloc
>
79 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
80 rebind
<_CharT
>::other _Char_alloc_type
;
81 typedef __gnu_cxx::__alloc_traits
<_Char_alloc_type
> _Alloc_traits
;
85 typedef _Traits traits_type
;
86 typedef typename
_Traits::char_type value_type
;
87 typedef _Char_alloc_type allocator_type
;
88 typedef typename
_Alloc_traits::size_type size_type
;
89 typedef typename
_Alloc_traits::difference_type difference_type
;
90 typedef typename
_Alloc_traits::reference reference
;
91 typedef typename
_Alloc_traits::const_reference const_reference
;
92 typedef typename
_Alloc_traits::pointer pointer
;
93 typedef typename
_Alloc_traits::const_pointer const_pointer
;
94 typedef __gnu_cxx::__normal_iterator
<pointer
, basic_string
> iterator
;
95 typedef __gnu_cxx::__normal_iterator
<const_pointer
, basic_string
>
97 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
98 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
100 /// Value returned by various member functions when they fail.
101 static const size_type npos
= static_cast<size_type
>(-1);
104 // type used for positions in insert, erase etc.
105 #if __cplusplus < 201103L
106 typedef iterator __const_iterator
;
108 typedef const_iterator __const_iterator
;
111 #if __cplusplus > 201402L
112 // A helper type for avoiding boiler-plate.
113 typedef basic_string_view
<_CharT
, _Traits
> __sv_type
;
116 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
117 struct _Alloc_hider
: allocator_type
// TODO check __is_final
119 #if __cplusplus < 201103L
120 _Alloc_hider(pointer __dat
, const _Alloc
& __a
= _Alloc())
121 : allocator_type(__a
), _M_p(__dat
) { }
123 _Alloc_hider(pointer __dat
, const _Alloc
& __a
)
124 : allocator_type(__a
), _M_p(__dat
) { }
126 _Alloc_hider(pointer __dat
, _Alloc
&& __a
= _Alloc())
127 : allocator_type(std::move(__a
)), _M_p(__dat
) { }
130 pointer _M_p
; // The actual data.
133 _Alloc_hider _M_dataplus
;
134 size_type _M_string_length
;
136 enum { _S_local_capacity
= 15 / sizeof(_CharT
) };
140 _CharT _M_local_buf
[_S_local_capacity
+ 1];
141 size_type _M_allocated_capacity
;
146 { _M_dataplus
._M_p
= __p
; }
149 _M_length(size_type __length
)
150 { _M_string_length
= __length
; }
154 { return _M_dataplus
._M_p
; }
159 #if __cplusplus >= 201103L
160 return std::pointer_traits
<pointer
>::pointer_to(*_M_local_buf
);
162 return pointer(_M_local_buf
);
167 _M_local_data() const
169 #if __cplusplus >= 201103L
170 return std::pointer_traits
<const_pointer
>::pointer_to(*_M_local_buf
);
172 return const_pointer(_M_local_buf
);
177 _M_capacity(size_type __capacity
)
178 { _M_allocated_capacity
= __capacity
; }
181 _M_set_length(size_type __n
)
184 traits_type::assign(_M_data()[__n
], _CharT());
189 { return _M_data() == _M_local_data(); }
193 _M_create(size_type
&, size_type
);
199 _M_destroy(_M_allocated_capacity
);
203 _M_destroy(size_type __size
) throw()
204 { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size
+ 1); }
206 // _M_construct_aux is used to implement the 21.3.1 para 15 which
207 // requires special behaviour if _InIterator is an integral type
208 template<typename _InIterator
>
210 _M_construct_aux(_InIterator __beg
, _InIterator __end
,
213 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
214 _M_construct(__beg
, __end
, _Tag());
217 // _GLIBCXX_RESOLVE_LIB_DEFECTS
218 // 438. Ambiguity in the "do the right thing" clause
219 template<typename _Integer
>
221 _M_construct_aux(_Integer __beg
, _Integer __end
, std::__true_type
)
222 { _M_construct_aux_2(static_cast<size_type
>(__beg
), __end
); }
225 _M_construct_aux_2(size_type __req
, _CharT __c
)
226 { _M_construct(__req
, __c
); }
228 template<typename _InIterator
>
230 _M_construct(_InIterator __beg
, _InIterator __end
)
232 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
233 _M_construct_aux(__beg
, __end
, _Integral());
236 // For Input Iterators, used in istreambuf_iterators, etc.
237 template<typename _InIterator
>
239 _M_construct(_InIterator __beg
, _InIterator __end
,
240 std::input_iterator_tag
);
242 // For forward_iterators up to random_access_iterators, used for
243 // string::iterator, _CharT*, etc.
244 template<typename _FwdIterator
>
246 _M_construct(_FwdIterator __beg
, _FwdIterator __end
,
247 std::forward_iterator_tag
);
250 _M_construct(size_type __req
, _CharT __c
);
254 { return _M_dataplus
; }
256 const allocator_type
&
257 _M_get_allocator() const
258 { return _M_dataplus
; }
262 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
263 // The explicit instantiations in misc-inst.cc require this due to
264 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
265 template<typename _Tp
, bool _Requires
=
266 !__are_same
<_Tp
, _CharT
*>::__value
267 && !__are_same
<_Tp
, const _CharT
*>::__value
268 && !__are_same
<_Tp
, iterator
>::__value
269 && !__are_same
<_Tp
, const_iterator
>::__value
>
270 struct __enable_if_not_native_iterator
271 { typedef basic_string
& __type
; };
272 template<typename _Tp
>
273 struct __enable_if_not_native_iterator
<_Tp
, false> { };
277 _M_check(size_type __pos
, const char* __s
) const
279 if (__pos
> this->size())
280 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
281 "this->size() (which is %zu)"),
282 __s
, __pos
, this->size());
287 _M_check_length(size_type __n1
, size_type __n2
, const char* __s
) const
289 if (this->max_size() - (this->size() - __n1
) < __n2
)
290 __throw_length_error(__N(__s
));
294 // NB: _M_limit doesn't check for a bad __pos value.
296 _M_limit(size_type __pos
, size_type __off
) const _GLIBCXX_NOEXCEPT
298 const bool __testoff
= __off
< this->size() - __pos
;
299 return __testoff
? __off
: this->size() - __pos
;
302 // True if _Rep and source do not overlap.
304 _M_disjunct(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
306 return (less
<const _CharT
*>()(__s
, _M_data())
307 || less
<const _CharT
*>()(_M_data() + this->size(), __s
));
310 // When __n = 1 way faster than the general multichar
311 // traits_type::copy/move/assign.
313 _S_copy(_CharT
* __d
, const _CharT
* __s
, size_type __n
)
316 traits_type::assign(*__d
, *__s
);
318 traits_type::copy(__d
, __s
, __n
);
322 _S_move(_CharT
* __d
, const _CharT
* __s
, size_type __n
)
325 traits_type::assign(*__d
, *__s
);
327 traits_type::move(__d
, __s
, __n
);
331 _S_assign(_CharT
* __d
, size_type __n
, _CharT __c
)
334 traits_type::assign(*__d
, __c
);
336 traits_type::assign(__d
, __n
, __c
);
339 // _S_copy_chars is a separate template to permit specialization
340 // to optimize for the common case of pointers as iterators.
341 template<class _Iterator
>
343 _S_copy_chars(_CharT
* __p
, _Iterator __k1
, _Iterator __k2
)
345 for (; __k1
!= __k2
; ++__k1
, (void)++__p
)
346 traits_type::assign(*__p
, *__k1
); // These types are off.
350 _S_copy_chars(_CharT
* __p
, iterator __k1
, iterator __k2
) _GLIBCXX_NOEXCEPT
351 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
354 _S_copy_chars(_CharT
* __p
, const_iterator __k1
, const_iterator __k2
)
356 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
359 _S_copy_chars(_CharT
* __p
, _CharT
* __k1
, _CharT
* __k2
) _GLIBCXX_NOEXCEPT
360 { _S_copy(__p
, __k1
, __k2
- __k1
); }
363 _S_copy_chars(_CharT
* __p
, const _CharT
* __k1
, const _CharT
* __k2
)
365 { _S_copy(__p
, __k1
, __k2
- __k1
); }
368 _S_compare(size_type __n1
, size_type __n2
) _GLIBCXX_NOEXCEPT
370 const difference_type __d
= difference_type(__n1
- __n2
);
372 if (__d
> __gnu_cxx::__numeric_traits
<int>::__max
)
373 return __gnu_cxx::__numeric_traits
<int>::__max
;
374 else if (__d
< __gnu_cxx::__numeric_traits
<int>::__min
)
375 return __gnu_cxx::__numeric_traits
<int>::__min
;
381 _M_assign(const basic_string
& __rcs
);
384 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
388 _M_erase(size_type __pos
, size_type __n
);
391 // Construct/copy/destroy:
392 // NB: We overload ctors in some cases instead of using default
393 // arguments, per 17.4.4.4 para. 2 item 2.
396 * @brief Default constructor creates an empty string.
399 _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible
<_Alloc
>::value
)
400 : _M_dataplus(_M_local_data())
401 { _M_set_length(0); }
404 * @brief Construct an empty string using allocator @a a.
407 basic_string(const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
408 : _M_dataplus(_M_local_data(), __a
)
409 { _M_set_length(0); }
412 * @brief Construct string with copy of value of @a __str.
413 * @param __str Source string.
415 basic_string(const basic_string
& __str
)
416 : _M_dataplus(_M_local_data(),
417 _Alloc_traits::_S_select_on_copy(__str
._M_get_allocator()))
418 { _M_construct(__str
._M_data(), __str
._M_data() + __str
.length()); }
420 // _GLIBCXX_RESOLVE_LIB_DEFECTS
421 // 2583. no way to supply an allocator for basic_string(str, pos)
423 * @brief Construct string as copy of a substring.
424 * @param __str Source string.
425 * @param __pos Index of first character to copy from.
426 * @param __a Allocator to use.
428 basic_string(const basic_string
& __str
, size_type __pos
,
429 const _Alloc
& __a
= _Alloc())
430 : _M_dataplus(_M_local_data(), __a
)
432 const _CharT
* __start
= __str
._M_data()
433 + __str
._M_check(__pos
, "basic_string::basic_string");
434 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, npos
));
438 * @brief Construct string as copy of a substring.
439 * @param __str Source string.
440 * @param __pos Index of first character to copy from.
441 * @param __n Number of characters to copy.
443 basic_string(const basic_string
& __str
, size_type __pos
,
445 : _M_dataplus(_M_local_data())
447 const _CharT
* __start
= __str
._M_data()
448 + __str
._M_check(__pos
, "basic_string::basic_string");
449 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, __n
));
453 * @brief Construct string as copy of a substring.
454 * @param __str Source string.
455 * @param __pos Index of first character to copy from.
456 * @param __n Number of characters to copy.
457 * @param __a Allocator to use.
459 basic_string(const basic_string
& __str
, size_type __pos
,
460 size_type __n
, const _Alloc
& __a
)
461 : _M_dataplus(_M_local_data(), __a
)
463 const _CharT
* __start
464 = __str
._M_data() + __str
._M_check(__pos
, "string::string");
465 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, __n
));
469 * @brief Construct string initialized by a character %array.
470 * @param __s Source character %array.
471 * @param __n Number of characters to copy.
472 * @param __a Allocator to use (default is default allocator).
474 * NB: @a __s must have at least @a __n characters, '\\0'
475 * has no special meaning.
477 basic_string(const _CharT
* __s
, size_type __n
,
478 const _Alloc
& __a
= _Alloc())
479 : _M_dataplus(_M_local_data(), __a
)
480 { _M_construct(__s
, __s
+ __n
); }
483 * @brief Construct string as copy of a C string.
484 * @param __s Source C string.
485 * @param __a Allocator to use (default is default allocator).
487 basic_string(const _CharT
* __s
, const _Alloc
& __a
= _Alloc())
488 : _M_dataplus(_M_local_data(), __a
)
489 { _M_construct(__s
, __s
? __s
+ traits_type::length(__s
) : __s
+npos
); }
492 * @brief Construct string as multiple characters.
493 * @param __n Number of characters.
494 * @param __c Character to use.
495 * @param __a Allocator to use (default is default allocator).
497 basic_string(size_type __n
, _CharT __c
, const _Alloc
& __a
= _Alloc())
498 : _M_dataplus(_M_local_data(), __a
)
499 { _M_construct(__n
, __c
); }
501 #if __cplusplus >= 201103L
503 * @brief Move construct string.
504 * @param __str Source string.
506 * The newly-created string contains the exact contents of @a __str.
507 * @a __str is a valid, but unspecified string.
509 basic_string(basic_string
&& __str
) noexcept
510 : _M_dataplus(_M_local_data(), std::move(__str
._M_get_allocator()))
512 if (__str
._M_is_local())
514 traits_type::copy(_M_local_buf
, __str
._M_local_buf
,
515 _S_local_capacity
+ 1);
519 _M_data(__str
._M_data());
520 _M_capacity(__str
._M_allocated_capacity
);
523 // Must use _M_length() here not _M_set_length() because
524 // basic_stringbuf relies on writing into unallocated capacity so
525 // we mess up the contents if we put a '\0' in the string.
526 _M_length(__str
.length());
527 __str
._M_data(__str
._M_local_data());
528 __str
._M_set_length(0);
532 * @brief Construct string from an initializer %list.
533 * @param __l std::initializer_list of characters.
534 * @param __a Allocator to use (default is default allocator).
536 basic_string(initializer_list
<_CharT
> __l
, const _Alloc
& __a
= _Alloc())
537 : _M_dataplus(_M_local_data(), __a
)
538 { _M_construct(__l
.begin(), __l
.end()); }
540 basic_string(const basic_string
& __str
, const _Alloc
& __a
)
541 : _M_dataplus(_M_local_data(), __a
)
542 { _M_construct(__str
.begin(), __str
.end()); }
544 basic_string(basic_string
&& __str
, const _Alloc
& __a
)
545 noexcept(_Alloc_traits::_S_always_equal())
546 : _M_dataplus(_M_local_data(), __a
)
548 if (__str
._M_is_local())
550 traits_type::copy(_M_local_buf
, __str
._M_local_buf
,
551 _S_local_capacity
+ 1);
552 _M_length(__str
.length());
553 __str
._M_set_length(0);
555 else if (_Alloc_traits::_S_always_equal()
556 || __str
.get_allocator() == __a
)
558 _M_data(__str
._M_data());
559 _M_length(__str
.length());
560 _M_capacity(__str
._M_allocated_capacity
);
561 __str
._M_data(__str
._M_local_buf
);
562 __str
._M_set_length(0);
565 _M_construct(__str
.begin(), __str
.end());
571 * @brief Construct string as copy of a range.
572 * @param __beg Start of range.
573 * @param __end End of range.
574 * @param __a Allocator to use (default is default allocator).
576 #if __cplusplus >= 201103L
577 template<typename _InputIterator
,
578 typename
= std::_RequireInputIter
<_InputIterator
>>
580 template<typename _InputIterator
>
582 basic_string(_InputIterator __beg
, _InputIterator __end
,
583 const _Alloc
& __a
= _Alloc())
584 : _M_dataplus(_M_local_data(), __a
)
585 { _M_construct(__beg
, __end
); }
587 #if __cplusplus > 201402L
589 * @brief Construct string from a substring of a string_view.
590 * @param __t Source string view.
591 * @param __pos The index of the first character to copy from __t.
592 * @param __n The number of characters to copy from __t.
593 * @param __a Allocator to use.
595 template<typename _Tp
, typename
=
596 _Require
<is_convertible
<_Tp
, __sv_type
>,
597 __not_
<is_convertible
<const _Tp
&, const _CharT
*>>>>
598 basic_string(const _Tp
& __t
, size_type __pos
, size_type __n
,
599 const _Alloc
& __a
= _Alloc())
600 : basic_string(__sv_type(__t
).substr(__pos
, __n
), __a
) { }
603 * @brief Construct string from a string_view.
604 * @param __sv Source string view.
605 * @param __a Allocator to use (default is default allocator).
608 basic_string(__sv_type __sv
, const _Alloc
& __a
= _Alloc())
609 : basic_string(__sv
.data(), __sv
.size(), __a
) { }
613 * @brief Destroy the string instance.
619 * @brief Assign the value of @a str to this string.
620 * @param __str Source string.
623 operator=(const basic_string
& __str
)
625 #if __cplusplus >= 201103L
626 if (_Alloc_traits::_S_propagate_on_copy_assign())
628 if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
629 && _M_get_allocator() != __str
._M_get_allocator())
631 // replacement allocator cannot free existing storage
632 _M_destroy(_M_allocated_capacity
);
633 _M_data(_M_local_data());
636 std::__alloc_on_copy(_M_get_allocator(), __str
._M_get_allocator());
639 return this->assign(__str
);
643 * @brief Copy contents of @a s into this string.
644 * @param __s Source null-terminated string.
647 operator=(const _CharT
* __s
)
648 { return this->assign(__s
); }
651 * @brief Set value to string of length 1.
652 * @param __c Source character.
654 * Assigning to a character makes this string length 1 and
655 * (*this)[0] == @a c.
658 operator=(_CharT __c
)
660 this->assign(1, __c
);
664 #if __cplusplus >= 201103L
666 * @brief Move assign the value of @a str to this string.
667 * @param __str Source string.
669 * The contents of @a str are moved into this string (without copying).
670 * @a str is a valid, but unspecified string.
672 // PR 58265, this should be noexcept.
673 // _GLIBCXX_RESOLVE_LIB_DEFECTS
674 // 2063. Contradictory requirements for string move assignment
676 operator=(basic_string
&& __str
)
677 noexcept(_Alloc_traits::_S_nothrow_move())
679 if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
680 && !_Alloc_traits::_S_always_equal()
681 && _M_get_allocator() != __str
._M_get_allocator())
683 // Destroy existing storage before replacing allocator.
684 _M_destroy(_M_allocated_capacity
);
685 _M_data(_M_local_data());
688 // Replace allocator if POCMA is true.
689 std::__alloc_on_move(_M_get_allocator(), __str
._M_get_allocator());
691 if (!__str
._M_is_local()
692 && (_Alloc_traits::_S_propagate_on_move_assign()
693 || _Alloc_traits::_S_always_equal()))
695 pointer __data
= nullptr;
696 size_type __capacity
;
699 if (_Alloc_traits::_S_always_equal())
702 __capacity
= _M_allocated_capacity
;
705 _M_destroy(_M_allocated_capacity
);
708 _M_data(__str
._M_data());
709 _M_length(__str
.length());
710 _M_capacity(__str
._M_allocated_capacity
);
713 __str
._M_data(__data
);
714 __str
._M_capacity(__capacity
);
717 __str
._M_data(__str
._M_local_buf
);
726 * @brief Set value to string constructed from initializer %list.
727 * @param __l std::initializer_list.
730 operator=(initializer_list
<_CharT
> __l
)
732 this->assign(__l
.begin(), __l
.size());
737 #if __cplusplus > 201402L
739 * @brief Set value to string constructed from a string_view.
740 * @param __sv A string_view.
743 operator=(__sv_type __sv
)
744 { return this->assign(__sv
); }
747 * @brief Convert to a string_view.
748 * @return A string_view.
750 operator __sv_type() const noexcept
751 { return __sv_type(data(), size()); }
756 * Returns a read/write iterator that points to the first character in
760 begin() _GLIBCXX_NOEXCEPT
761 { return iterator(_M_data()); }
764 * Returns a read-only (constant) iterator that points to the first
765 * character in the %string.
768 begin() const _GLIBCXX_NOEXCEPT
769 { return const_iterator(_M_data()); }
772 * Returns a read/write iterator that points one past the last
773 * character in the %string.
776 end() _GLIBCXX_NOEXCEPT
777 { return iterator(_M_data() + this->size()); }
780 * Returns a read-only (constant) iterator that points one past the
781 * last character in the %string.
784 end() const _GLIBCXX_NOEXCEPT
785 { return const_iterator(_M_data() + this->size()); }
788 * Returns a read/write reverse iterator that points to the last
789 * character in the %string. Iteration is done in reverse element
793 rbegin() _GLIBCXX_NOEXCEPT
794 { return reverse_iterator(this->end()); }
797 * Returns a read-only (constant) reverse iterator that points
798 * to the last character in the %string. Iteration is done in
799 * reverse element order.
801 const_reverse_iterator
802 rbegin() const _GLIBCXX_NOEXCEPT
803 { return const_reverse_iterator(this->end()); }
806 * Returns a read/write reverse iterator that points to one before the
807 * first character in the %string. Iteration is done in reverse
811 rend() _GLIBCXX_NOEXCEPT
812 { return reverse_iterator(this->begin()); }
815 * Returns a read-only (constant) reverse iterator that points
816 * to one before the first character in the %string. Iteration
817 * is done in reverse element order.
819 const_reverse_iterator
820 rend() const _GLIBCXX_NOEXCEPT
821 { return const_reverse_iterator(this->begin()); }
823 #if __cplusplus >= 201103L
825 * Returns a read-only (constant) iterator that points to the first
826 * character in the %string.
829 cbegin() const noexcept
830 { return const_iterator(this->_M_data()); }
833 * Returns a read-only (constant) iterator that points one past the
834 * last character in the %string.
837 cend() const noexcept
838 { return const_iterator(this->_M_data() + this->size()); }
841 * Returns a read-only (constant) reverse iterator that points
842 * to the last character in the %string. Iteration is done in
843 * reverse element order.
845 const_reverse_iterator
846 crbegin() const noexcept
847 { return const_reverse_iterator(this->end()); }
850 * Returns a read-only (constant) reverse iterator that points
851 * to one before the first character in the %string. Iteration
852 * is done in reverse element order.
854 const_reverse_iterator
855 crend() const noexcept
856 { return const_reverse_iterator(this->begin()); }
861 /// Returns the number of characters in the string, not including any
862 /// null-termination.
864 size() const _GLIBCXX_NOEXCEPT
865 { return _M_string_length
; }
867 /// Returns the number of characters in the string, not including any
868 /// null-termination.
870 length() const _GLIBCXX_NOEXCEPT
871 { return _M_string_length
; }
873 /// Returns the size() of the largest possible %string.
875 max_size() const _GLIBCXX_NOEXCEPT
876 { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
879 * @brief Resizes the %string to the specified number of characters.
880 * @param __n Number of characters the %string should contain.
881 * @param __c Character to fill any new elements.
883 * This function will %resize the %string to the specified
884 * number of characters. If the number is smaller than the
885 * %string's current size the %string is truncated, otherwise
886 * the %string is extended and new elements are %set to @a __c.
889 resize(size_type __n
, _CharT __c
);
892 * @brief Resizes the %string to the specified number of characters.
893 * @param __n Number of characters the %string should contain.
895 * This function will resize the %string to the specified length. If
896 * the new size is smaller than the %string's current size the %string
897 * is truncated, otherwise the %string is extended and new characters
898 * are default-constructed. For basic types such as char, this means
902 resize(size_type __n
)
903 { this->resize(__n
, _CharT()); }
905 #if __cplusplus >= 201103L
906 /// A non-binding request to reduce capacity() to size().
908 shrink_to_fit() noexcept
911 if (capacity() > size())
923 * Returns the total number of characters that the %string can hold
924 * before needing to allocate more memory.
927 capacity() const _GLIBCXX_NOEXCEPT
929 return _M_is_local() ? size_type(_S_local_capacity
)
930 : _M_allocated_capacity
;
934 * @brief Attempt to preallocate enough memory for specified number of
936 * @param __res_arg Number of characters required.
937 * @throw std::length_error If @a __res_arg exceeds @c max_size().
939 * This function attempts to reserve enough memory for the
940 * %string to hold the specified number of characters. If the
941 * number requested is more than max_size(), length_error is
944 * The advantage of this function is that if optimal code is a
945 * necessity and the user can determine the string length that will be
946 * required, the user can reserve the memory in %advance, and thus
947 * prevent a possible reallocation of memory and copying of %string
951 reserve(size_type __res_arg
= 0);
954 * Erases the string, making it empty.
957 clear() _GLIBCXX_NOEXCEPT
958 { _M_set_length(0); }
961 * Returns true if the %string is empty. Equivalent to
962 * <code>*this == ""</code>.
965 empty() const _GLIBCXX_NOEXCEPT
966 { return this->size() == 0; }
970 * @brief Subscript access to the data contained in the %string.
971 * @param __pos The index of the character to access.
972 * @return Read-only (constant) reference to the character.
974 * This operator allows for easy, array-style, data access.
975 * Note that data access with this operator is unchecked and
976 * out_of_range lookups are not defined. (For checked lookups
980 operator[] (size_type __pos
) const _GLIBCXX_NOEXCEPT
982 __glibcxx_assert(__pos
<= size());
983 return _M_data()[__pos
];
987 * @brief Subscript access to the data contained in the %string.
988 * @param __pos The index of the character to access.
989 * @return Read/write reference to the character.
991 * This operator allows for easy, array-style, data access.
992 * Note that data access with this operator is unchecked and
993 * out_of_range lookups are not defined. (For checked lookups
997 operator[](size_type __pos
)
999 // Allow pos == size() both in C++98 mode, as v3 extension,
1000 // and in C++11 mode.
1001 __glibcxx_assert(__pos
<= size());
1002 // In pedantic mode be strict in C++98 mode.
1003 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus
>= 201103L || __pos
< size());
1004 return _M_data()[__pos
];
1008 * @brief Provides access to the data contained in the %string.
1009 * @param __n The index of the character to access.
1010 * @return Read-only (const) reference to the character.
1011 * @throw std::out_of_range If @a n is an invalid index.
1013 * This function provides for safer data access. The parameter is
1014 * first checked that it is in the range of the string. The function
1015 * throws out_of_range if the check fails.
1018 at(size_type __n
) const
1020 if (__n
>= this->size())
1021 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1022 "(which is %zu) >= this->size() "
1025 return _M_data()[__n
];
1029 * @brief Provides access to the data contained in the %string.
1030 * @param __n The index of the character to access.
1031 * @return Read/write reference to the character.
1032 * @throw std::out_of_range If @a n is an invalid index.
1034 * This function provides for safer data access. The parameter is
1035 * first checked that it is in the range of the string. The function
1036 * throws out_of_range if the check fails.
1042 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1043 "(which is %zu) >= this->size() "
1046 return _M_data()[__n
];
1049 #if __cplusplus >= 201103L
1051 * Returns a read/write reference to the data at the first
1052 * element of the %string.
1057 __glibcxx_assert(!empty());
1058 return operator[](0);
1062 * Returns a read-only (constant) reference to the data at the first
1063 * element of the %string.
1066 front() const noexcept
1068 __glibcxx_assert(!empty());
1069 return operator[](0);
1073 * Returns a read/write reference to the data at the last
1074 * element of the %string.
1079 __glibcxx_assert(!empty());
1080 return operator[](this->size() - 1);
1084 * Returns a read-only (constant) reference to the data at the
1085 * last element of the %string.
1088 back() const noexcept
1090 __glibcxx_assert(!empty());
1091 return operator[](this->size() - 1);
1097 * @brief Append a string to this string.
1098 * @param __str The string to append.
1099 * @return Reference to this string.
1102 operator+=(const basic_string
& __str
)
1103 { return this->append(__str
); }
1106 * @brief Append a C string.
1107 * @param __s The C string to append.
1108 * @return Reference to this string.
1111 operator+=(const _CharT
* __s
)
1112 { return this->append(__s
); }
1115 * @brief Append a character.
1116 * @param __c The character to append.
1117 * @return Reference to this string.
1120 operator+=(_CharT __c
)
1122 this->push_back(__c
);
1126 #if __cplusplus >= 201103L
1128 * @brief Append an initializer_list of characters.
1129 * @param __l The initializer_list of characters to be appended.
1130 * @return Reference to this string.
1133 operator+=(initializer_list
<_CharT
> __l
)
1134 { return this->append(__l
.begin(), __l
.size()); }
1137 #if __cplusplus > 201402L
1139 * @brief Append a string_view.
1140 * @param __sv The string_view to be appended.
1141 * @return Reference to this string.
1144 operator+=(__sv_type __sv
)
1145 { return this->append(__sv
); }
1149 * @brief Append a string to this string.
1150 * @param __str The string to append.
1151 * @return Reference to this string.
1154 append(const basic_string
& __str
)
1155 { return _M_append(__str
._M_data(), __str
.size()); }
1158 * @brief Append a substring.
1159 * @param __str The string to append.
1160 * @param __pos Index of the first character of str to append.
1161 * @param __n The number of characters to append.
1162 * @return Reference to this string.
1163 * @throw std::out_of_range if @a __pos is not a valid index.
1165 * This function appends @a __n characters from @a __str
1166 * starting at @a __pos to this string. If @a __n is is larger
1167 * than the number of available characters in @a __str, the
1168 * remainder of @a __str is appended.
1171 append(const basic_string
& __str
, size_type __pos
, size_type __n
)
1172 { return _M_append(__str
._M_data()
1173 + __str
._M_check(__pos
, "basic_string::append"),
1174 __str
._M_limit(__pos
, __n
)); }
1177 * @brief Append a C substring.
1178 * @param __s The C string to append.
1179 * @param __n The number of characters to append.
1180 * @return Reference to this string.
1183 append(const _CharT
* __s
, size_type __n
)
1185 __glibcxx_requires_string_len(__s
, __n
);
1186 _M_check_length(size_type(0), __n
, "basic_string::append");
1187 return _M_append(__s
, __n
);
1191 * @brief Append a C string.
1192 * @param __s The C string to append.
1193 * @return Reference to this string.
1196 append(const _CharT
* __s
)
1198 __glibcxx_requires_string(__s
);
1199 const size_type __n
= traits_type::length(__s
);
1200 _M_check_length(size_type(0), __n
, "basic_string::append");
1201 return _M_append(__s
, __n
);
1205 * @brief Append multiple characters.
1206 * @param __n The number of characters to append.
1207 * @param __c The character to use.
1208 * @return Reference to this string.
1210 * Appends __n copies of __c to this string.
1213 append(size_type __n
, _CharT __c
)
1214 { return _M_replace_aux(this->size(), size_type(0), __n
, __c
); }
1216 #if __cplusplus >= 201103L
1218 * @brief Append an initializer_list of characters.
1219 * @param __l The initializer_list of characters to append.
1220 * @return Reference to this string.
1223 append(initializer_list
<_CharT
> __l
)
1224 { return this->append(__l
.begin(), __l
.size()); }
1228 * @brief Append a range of characters.
1229 * @param __first Iterator referencing the first character to append.
1230 * @param __last Iterator marking the end of the range.
1231 * @return Reference to this string.
1233 * Appends characters in the range [__first,__last) to this string.
1235 #if __cplusplus >= 201103L
1236 template<class _InputIterator
,
1237 typename
= std::_RequireInputIter
<_InputIterator
>>
1239 template<class _InputIterator
>
1242 append(_InputIterator __first
, _InputIterator __last
)
1243 { return this->replace(end(), end(), __first
, __last
); }
1245 #if __cplusplus > 201402L
1247 * @brief Append a string_view.
1248 * @param __sv The string_view to be appended.
1249 * @return Reference to this string.
1252 append(__sv_type __sv
)
1253 { return this->append(__sv
.data(), __sv
.size()); }
1255 template<typename _Tp
, typename _Res
>
1256 using _If_sv
= enable_if_t
<
1257 __and_
<is_convertible
<const _Tp
&, __sv_type
>,
1258 __not_
<is_convertible
<const _Tp
&, const _CharT
*>>>::value
,
1262 * @brief Append a range of characters from a string_view.
1263 * @param __sv The string_view to be appended from.
1264 * @param __pos The position in the string_view to append from.
1265 * @param __n The number of characters to append from the string_view.
1266 * @return Reference to this string.
1268 template <typename _Tp
>
1269 _If_sv
<_Tp
, basic_string
&>
1270 append(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
1272 __sv_type __sv
= __svt
;
1273 return _M_append(__sv
.data()
1274 + __sv
._M_check(__pos
, "basic_string::append"),
1275 __sv
._M_limit(__pos
, __n
));
1280 * @brief Append a single character.
1281 * @param __c Character to append.
1284 push_back(_CharT __c
)
1286 const size_type __size
= this->size();
1287 if (__size
+ 1 > this->capacity())
1288 this->_M_mutate(__size
, size_type(0), 0, size_type(1));
1289 traits_type::assign(this->_M_data()[__size
], __c
);
1290 this->_M_set_length(__size
+ 1);
1294 * @brief Set value to contents of another string.
1295 * @param __str Source string to use.
1296 * @return Reference to this string.
1299 assign(const basic_string
& __str
)
1301 this->_M_assign(__str
);
1305 #if __cplusplus >= 201103L
1307 * @brief Set value to contents of another string.
1308 * @param __str Source string to use.
1309 * @return Reference to this string.
1311 * This function sets this string to the exact contents of @a __str.
1312 * @a __str is a valid, but unspecified string.
1315 assign(basic_string
&& __str
)
1316 noexcept(_Alloc_traits::_S_nothrow_move())
1318 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1319 // 2063. Contradictory requirements for string move assignment
1320 return *this = std::move(__str
);
1325 * @brief Set value to a substring of a string.
1326 * @param __str The string to use.
1327 * @param __pos Index of the first character of str.
1328 * @param __n Number of characters to use.
1329 * @return Reference to this string.
1330 * @throw std::out_of_range if @a pos is not a valid index.
1332 * This function sets this string to the substring of @a __str
1333 * consisting of @a __n characters at @a __pos. If @a __n is
1334 * is larger than the number of available characters in @a
1335 * __str, the remainder of @a __str is used.
1338 assign(const basic_string
& __str
, size_type __pos
, size_type __n
)
1339 { return _M_replace(size_type(0), this->size(), __str
._M_data()
1340 + __str
._M_check(__pos
, "basic_string::assign"),
1341 __str
._M_limit(__pos
, __n
)); }
1344 * @brief Set value to a C substring.
1345 * @param __s The C string to use.
1346 * @param __n Number of characters to use.
1347 * @return Reference to this string.
1349 * This function sets the value of this string to the first @a __n
1350 * characters of @a __s. If @a __n is is larger than the number of
1351 * available characters in @a __s, the remainder of @a __s is used.
1354 assign(const _CharT
* __s
, size_type __n
)
1356 __glibcxx_requires_string_len(__s
, __n
);
1357 return _M_replace(size_type(0), this->size(), __s
, __n
);
1361 * @brief Set value to contents of a C string.
1362 * @param __s The C string to use.
1363 * @return Reference to this string.
1365 * This function sets the value of this string to the value of @a __s.
1366 * The data is copied, so there is no dependence on @a __s once the
1370 assign(const _CharT
* __s
)
1372 __glibcxx_requires_string(__s
);
1373 return _M_replace(size_type(0), this->size(), __s
,
1374 traits_type::length(__s
));
1378 * @brief Set value to multiple characters.
1379 * @param __n Length of the resulting string.
1380 * @param __c The character to use.
1381 * @return Reference to this string.
1383 * This function sets the value of this string to @a __n copies of
1387 assign(size_type __n
, _CharT __c
)
1388 { return _M_replace_aux(size_type(0), this->size(), __n
, __c
); }
1391 * @brief Set value to a range of characters.
1392 * @param __first Iterator referencing the first character to append.
1393 * @param __last Iterator marking the end of the range.
1394 * @return Reference to this string.
1396 * Sets value of string to characters in the range [__first,__last).
1398 #if __cplusplus >= 201103L
1399 template<class _InputIterator
,
1400 typename
= std::_RequireInputIter
<_InputIterator
>>
1402 template<class _InputIterator
>
1405 assign(_InputIterator __first
, _InputIterator __last
)
1406 { return this->replace(begin(), end(), __first
, __last
); }
1408 #if __cplusplus >= 201103L
1410 * @brief Set value to an initializer_list of characters.
1411 * @param __l The initializer_list of characters to assign.
1412 * @return Reference to this string.
1415 assign(initializer_list
<_CharT
> __l
)
1416 { return this->assign(__l
.begin(), __l
.size()); }
1419 #if __cplusplus > 201402L
1421 * @brief Set value from a string_view.
1422 * @param __sv The source string_view.
1423 * @return Reference to this string.
1426 assign(__sv_type __sv
)
1427 { return this->assign(__sv
.data(), __sv
.size()); }
1430 * @brief Set value from a range of characters in a string_view.
1431 * @param __sv The source string_view.
1432 * @param __pos The position in the string_view to assign from.
1433 * @param __n The number of characters to assign.
1434 * @return Reference to this string.
1436 template <typename _Tp
>
1437 _If_sv
<_Tp
, basic_string
&>
1438 assign(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
1440 __sv_type __sv
= __svt
;
1441 return _M_replace(size_type(0), this->size(), __sv
.data()
1442 + __sv
._M_check(__pos
, "basic_string::assign"),
1443 __sv
._M_limit(__pos
, __n
));
1447 #if __cplusplus >= 201103L
1449 * @brief Insert multiple characters.
1450 * @param __p Const_iterator referencing location in string to
1452 * @param __n Number of characters to insert
1453 * @param __c The character to insert.
1454 * @return Iterator referencing the first inserted char.
1455 * @throw std::length_error If new length exceeds @c max_size().
1457 * Inserts @a __n copies of character @a __c starting at the
1458 * position referenced by iterator @a __p. If adding
1459 * characters causes the length to exceed max_size(),
1460 * length_error is thrown. The value of the string doesn't
1461 * change if an error is thrown.
1464 insert(const_iterator __p
, size_type __n
, _CharT __c
)
1466 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1467 const size_type __pos
= __p
- begin();
1468 this->replace(__p
, __p
, __n
, __c
);
1469 return iterator(this->_M_data() + __pos
);
1473 * @brief Insert multiple characters.
1474 * @param __p Iterator referencing location in string to insert at.
1475 * @param __n Number of characters to insert
1476 * @param __c The character to insert.
1477 * @throw std::length_error If new length exceeds @c max_size().
1479 * Inserts @a __n copies of character @a __c starting at the
1480 * position referenced by iterator @a __p. If adding
1481 * characters causes the length to exceed max_size(),
1482 * length_error is thrown. The value of the string doesn't
1483 * change if an error is thrown.
1486 insert(iterator __p
, size_type __n
, _CharT __c
)
1487 { this->replace(__p
, __p
, __n
, __c
); }
1490 #if __cplusplus >= 201103L
1492 * @brief Insert a range of characters.
1493 * @param __p Const_iterator referencing location in string to
1495 * @param __beg Start of range.
1496 * @param __end End of range.
1497 * @return Iterator referencing the first inserted char.
1498 * @throw std::length_error If new length exceeds @c max_size().
1500 * Inserts characters in range [beg,end). If adding characters
1501 * causes the length to exceed max_size(), length_error is
1502 * thrown. The value of the string doesn't change if an error
1505 template<class _InputIterator
,
1506 typename
= std::_RequireInputIter
<_InputIterator
>>
1508 insert(const_iterator __p
, _InputIterator __beg
, _InputIterator __end
)
1510 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1511 const size_type __pos
= __p
- begin();
1512 this->replace(__p
, __p
, __beg
, __end
);
1513 return iterator(this->_M_data() + __pos
);
1517 * @brief Insert a range of characters.
1518 * @param __p Iterator referencing location in string to insert at.
1519 * @param __beg Start of range.
1520 * @param __end End of range.
1521 * @throw std::length_error If new length exceeds @c max_size().
1523 * Inserts characters in range [__beg,__end). If adding
1524 * characters causes the length to exceed max_size(),
1525 * length_error is thrown. The value of the string doesn't
1526 * change if an error is thrown.
1528 template<class _InputIterator
>
1530 insert(iterator __p
, _InputIterator __beg
, _InputIterator __end
)
1531 { this->replace(__p
, __p
, __beg
, __end
); }
1534 #if __cplusplus >= 201103L
1536 * @brief Insert an initializer_list of characters.
1537 * @param __p Iterator referencing location in string to insert at.
1538 * @param __l The initializer_list of characters to insert.
1539 * @throw std::length_error If new length exceeds @c max_size().
1542 insert(iterator __p
, initializer_list
<_CharT
> __l
)
1544 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1545 this->insert(__p
- begin(), __l
.begin(), __l
.size());
1550 * @brief Insert value of a string.
1551 * @param __pos1 Iterator referencing location in string to insert at.
1552 * @param __str The string to insert.
1553 * @return Reference to this string.
1554 * @throw std::length_error If new length exceeds @c max_size().
1556 * Inserts value of @a __str starting at @a __pos1. If adding
1557 * characters causes the length to exceed max_size(),
1558 * length_error is thrown. The value of the string doesn't
1559 * change if an error is thrown.
1562 insert(size_type __pos1
, const basic_string
& __str
)
1563 { return this->replace(__pos1
, size_type(0),
1564 __str
._M_data(), __str
.size()); }
1567 * @brief Insert a substring.
1568 * @param __pos1 Iterator referencing location in string to insert at.
1569 * @param __str The string to insert.
1570 * @param __pos2 Start of characters in str to insert.
1571 * @param __n Number of characters to insert.
1572 * @return Reference to this string.
1573 * @throw std::length_error If new length exceeds @c max_size().
1574 * @throw std::out_of_range If @a pos1 > size() or
1575 * @a __pos2 > @a str.size().
1577 * Starting at @a pos1, insert @a __n character of @a __str
1578 * beginning with @a __pos2. If adding characters causes the
1579 * length to exceed max_size(), length_error is thrown. If @a
1580 * __pos1 is beyond the end of this string or @a __pos2 is
1581 * beyond the end of @a __str, out_of_range is thrown. The
1582 * value of the string doesn't change if an error is thrown.
1585 insert(size_type __pos1
, const basic_string
& __str
,
1586 size_type __pos2
, size_type __n
)
1587 { return this->replace(__pos1
, size_type(0), __str
._M_data()
1588 + __str
._M_check(__pos2
, "basic_string::insert"),
1589 __str
._M_limit(__pos2
, __n
)); }
1592 * @brief Insert a C substring.
1593 * @param __pos Iterator referencing location in string to insert at.
1594 * @param __s The C string to insert.
1595 * @param __n The number of characters to insert.
1596 * @return Reference to this string.
1597 * @throw std::length_error If new length exceeds @c max_size().
1598 * @throw std::out_of_range If @a __pos is beyond the end of this
1601 * Inserts the first @a __n characters of @a __s starting at @a
1602 * __pos. If adding characters causes the length to exceed
1603 * max_size(), length_error is thrown. If @a __pos is beyond
1604 * end(), out_of_range is thrown. The value of the string
1605 * doesn't change if an error is thrown.
1608 insert(size_type __pos
, const _CharT
* __s
, size_type __n
)
1609 { return this->replace(__pos
, size_type(0), __s
, __n
); }
1612 * @brief Insert a C string.
1613 * @param __pos Iterator referencing location in string to insert at.
1614 * @param __s The C string to insert.
1615 * @return Reference to this string.
1616 * @throw std::length_error If new length exceeds @c max_size().
1617 * @throw std::out_of_range If @a pos is beyond the end of this
1620 * Inserts the first @a n characters of @a __s starting at @a __pos. If
1621 * adding characters causes the length to exceed max_size(),
1622 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
1623 * thrown. The value of the string doesn't change if an error is
1627 insert(size_type __pos
, const _CharT
* __s
)
1629 __glibcxx_requires_string(__s
);
1630 return this->replace(__pos
, size_type(0), __s
,
1631 traits_type::length(__s
));
1635 * @brief Insert multiple characters.
1636 * @param __pos Index in string to insert at.
1637 * @param __n Number of characters to insert
1638 * @param __c The character to insert.
1639 * @return Reference to this string.
1640 * @throw std::length_error If new length exceeds @c max_size().
1641 * @throw std::out_of_range If @a __pos is beyond the end of this
1644 * Inserts @a __n copies of character @a __c starting at index
1645 * @a __pos. If adding characters causes the length to exceed
1646 * max_size(), length_error is thrown. If @a __pos > length(),
1647 * out_of_range is thrown. The value of the string doesn't
1648 * change if an error is thrown.
1651 insert(size_type __pos
, size_type __n
, _CharT __c
)
1652 { return _M_replace_aux(_M_check(__pos
, "basic_string::insert"),
1653 size_type(0), __n
, __c
); }
1656 * @brief Insert one character.
1657 * @param __p Iterator referencing position in string to insert at.
1658 * @param __c The character to insert.
1659 * @return Iterator referencing newly inserted char.
1660 * @throw std::length_error If new length exceeds @c max_size().
1662 * Inserts character @a __c at position referenced by @a __p.
1663 * If adding character causes the length to exceed max_size(),
1664 * length_error is thrown. If @a __p is beyond end of string,
1665 * out_of_range is thrown. The value of the string doesn't
1666 * change if an error is thrown.
1669 insert(__const_iterator __p
, _CharT __c
)
1671 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1672 const size_type __pos
= __p
- begin();
1673 _M_replace_aux(__pos
, size_type(0), size_type(1), __c
);
1674 return iterator(_M_data() + __pos
);
1677 #if __cplusplus > 201402L
1679 * @brief Insert a string_view.
1680 * @param __pos Iterator referencing position in string to insert at.
1681 * @param __sv The string_view to insert.
1682 * @return Reference to this string.
1685 insert(size_type __pos
, __sv_type __sv
)
1686 { return this->insert(__pos
, __sv
.data(), __sv
.size()); }
1689 * @brief Insert a string_view.
1690 * @param __pos Iterator referencing position in string to insert at.
1691 * @param __sv The string_view to insert from.
1692 * @param __pos Iterator referencing position in string_view to insert
1694 * @param __n The number of characters to insert.
1695 * @return Reference to this string.
1697 template <typename _Tp
>
1698 _If_sv
<_Tp
, basic_string
&>
1699 insert(size_type __pos1
, const _Tp
& __svt
,
1700 size_type __pos2
, size_type __n
= npos
)
1702 __sv_type __sv
= __svt
;
1703 return this->replace(__pos1
, size_type(0), __sv
.data()
1704 + __sv
._M_check(__pos2
, "basic_string::insert"),
1705 __sv
._M_limit(__pos2
, __n
));
1710 * @brief Remove characters.
1711 * @param __pos Index of first character to remove (default 0).
1712 * @param __n Number of characters to remove (default remainder).
1713 * @return Reference to this string.
1714 * @throw std::out_of_range If @a pos is beyond the end of this
1717 * Removes @a __n characters from this string starting at @a
1718 * __pos. The length of the string is reduced by @a __n. If
1719 * there are < @a __n characters to remove, the remainder of
1720 * the string is truncated. If @a __p is beyond end of string,
1721 * out_of_range is thrown. The value of the string doesn't
1722 * change if an error is thrown.
1725 erase(size_type __pos
= 0, size_type __n
= npos
)
1727 _M_check(__pos
, "basic_string::erase");
1729 this->_M_set_length(__pos
);
1731 this->_M_erase(__pos
, _M_limit(__pos
, __n
));
1736 * @brief Remove one character.
1737 * @param __position Iterator referencing the character to remove.
1738 * @return iterator referencing same location after removal.
1740 * Removes the character at @a __position from this string. The value
1741 * of the string doesn't change if an error is thrown.
1744 erase(__const_iterator __position
)
1746 _GLIBCXX_DEBUG_PEDASSERT(__position
>= begin()
1747 && __position
< end());
1748 const size_type __pos
= __position
- begin();
1749 this->_M_erase(__pos
, size_type(1));
1750 return iterator(_M_data() + __pos
);
1754 * @brief Remove a range of characters.
1755 * @param __first Iterator referencing the first character to remove.
1756 * @param __last Iterator referencing the end of the range.
1757 * @return Iterator referencing location of first after removal.
1759 * Removes the characters in the range [first,last) from this string.
1760 * The value of the string doesn't change if an error is thrown.
1763 erase(__const_iterator __first
, __const_iterator __last
)
1765 _GLIBCXX_DEBUG_PEDASSERT(__first
>= begin() && __first
<= __last
1766 && __last
<= end());
1767 const size_type __pos
= __first
- begin();
1768 if (__last
== end())
1769 this->_M_set_length(__pos
);
1771 this->_M_erase(__pos
, __last
- __first
);
1772 return iterator(this->_M_data() + __pos
);
1775 #if __cplusplus >= 201103L
1777 * @brief Remove the last character.
1779 * The string must be non-empty.
1784 __glibcxx_assert(!empty());
1785 _M_erase(size() - 1, 1);
1790 * @brief Replace characters with value from another string.
1791 * @param __pos Index of first character to replace.
1792 * @param __n Number of characters to be replaced.
1793 * @param __str String to insert.
1794 * @return Reference to this string.
1795 * @throw std::out_of_range If @a pos is beyond the end of this
1797 * @throw std::length_error If new length exceeds @c max_size().
1799 * Removes the characters in the range [__pos,__pos+__n) from
1800 * this string. In place, the value of @a __str is inserted.
1801 * If @a __pos is beyond end of string, out_of_range is thrown.
1802 * If the length of the result exceeds max_size(), length_error
1803 * is thrown. The value of the string doesn't change if an
1807 replace(size_type __pos
, size_type __n
, const basic_string
& __str
)
1808 { return this->replace(__pos
, __n
, __str
._M_data(), __str
.size()); }
1811 * @brief Replace characters with value from another string.
1812 * @param __pos1 Index of first character to replace.
1813 * @param __n1 Number of characters to be replaced.
1814 * @param __str String to insert.
1815 * @param __pos2 Index of first character of str to use.
1816 * @param __n2 Number of characters from str to use.
1817 * @return Reference to this string.
1818 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
1820 * @throw std::length_error If new length exceeds @c max_size().
1822 * Removes the characters in the range [__pos1,__pos1 + n) from this
1823 * string. In place, the value of @a __str is inserted. If @a __pos is
1824 * beyond end of string, out_of_range is thrown. If the length of the
1825 * result exceeds max_size(), length_error is thrown. The value of the
1826 * string doesn't change if an error is thrown.
1829 replace(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
1830 size_type __pos2
, size_type __n2
)
1831 { return this->replace(__pos1
, __n1
, __str
._M_data()
1832 + __str
._M_check(__pos2
, "basic_string::replace"),
1833 __str
._M_limit(__pos2
, __n2
)); }
1836 * @brief Replace characters with value of a C substring.
1837 * @param __pos Index of first character to replace.
1838 * @param __n1 Number of characters to be replaced.
1839 * @param __s C string to insert.
1840 * @param __n2 Number of characters from @a s to use.
1841 * @return Reference to this string.
1842 * @throw std::out_of_range If @a pos1 > size().
1843 * @throw std::length_error If new length exceeds @c max_size().
1845 * Removes the characters in the range [__pos,__pos + __n1)
1846 * from this string. In place, the first @a __n2 characters of
1847 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
1848 * @a __pos is beyond end of string, out_of_range is thrown. If
1849 * the length of result exceeds max_size(), length_error is
1850 * thrown. The value of the string doesn't change if an error
1854 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
1857 __glibcxx_requires_string_len(__s
, __n2
);
1858 return _M_replace(_M_check(__pos
, "basic_string::replace"),
1859 _M_limit(__pos
, __n1
), __s
, __n2
);
1863 * @brief Replace characters with value of a C string.
1864 * @param __pos Index of first character to replace.
1865 * @param __n1 Number of characters to be replaced.
1866 * @param __s C string to insert.
1867 * @return Reference to this string.
1868 * @throw std::out_of_range If @a pos > size().
1869 * @throw std::length_error If new length exceeds @c max_size().
1871 * Removes the characters in the range [__pos,__pos + __n1)
1872 * from this string. In place, the characters of @a __s are
1873 * inserted. If @a __pos is beyond end of string, out_of_range
1874 * is thrown. If the length of result exceeds max_size(),
1875 * length_error is thrown. The value of the string doesn't
1876 * change if an error is thrown.
1879 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
)
1881 __glibcxx_requires_string(__s
);
1882 return this->replace(__pos
, __n1
, __s
, traits_type::length(__s
));
1886 * @brief Replace characters with multiple characters.
1887 * @param __pos Index of first character to replace.
1888 * @param __n1 Number of characters to be replaced.
1889 * @param __n2 Number of characters to insert.
1890 * @param __c Character to insert.
1891 * @return Reference to this string.
1892 * @throw std::out_of_range If @a __pos > size().
1893 * @throw std::length_error If new length exceeds @c max_size().
1895 * Removes the characters in the range [pos,pos + n1) from this
1896 * string. In place, @a __n2 copies of @a __c are inserted.
1897 * If @a __pos is beyond end of string, out_of_range is thrown.
1898 * If the length of result exceeds max_size(), length_error is
1899 * thrown. The value of the string doesn't change if an error
1903 replace(size_type __pos
, size_type __n1
, size_type __n2
, _CharT __c
)
1904 { return _M_replace_aux(_M_check(__pos
, "basic_string::replace"),
1905 _M_limit(__pos
, __n1
), __n2
, __c
); }
1908 * @brief Replace range of characters with string.
1909 * @param __i1 Iterator referencing start of range to replace.
1910 * @param __i2 Iterator referencing end of range to replace.
1911 * @param __str String value to insert.
1912 * @return Reference to this string.
1913 * @throw std::length_error If new length exceeds @c max_size().
1915 * Removes the characters in the range [__i1,__i2). In place,
1916 * the value of @a __str is inserted. If the length of result
1917 * exceeds max_size(), length_error is thrown. The value of
1918 * the string doesn't change if an error is thrown.
1921 replace(__const_iterator __i1
, __const_iterator __i2
,
1922 const basic_string
& __str
)
1923 { return this->replace(__i1
, __i2
, __str
._M_data(), __str
.size()); }
1926 * @brief Replace range of characters with C substring.
1927 * @param __i1 Iterator referencing start of range to replace.
1928 * @param __i2 Iterator referencing end of range to replace.
1929 * @param __s C string value to insert.
1930 * @param __n Number of characters from s to insert.
1931 * @return Reference to this string.
1932 * @throw std::length_error If new length exceeds @c max_size().
1934 * Removes the characters in the range [__i1,__i2). In place,
1935 * the first @a __n characters of @a __s are inserted. If the
1936 * length of result exceeds max_size(), length_error is thrown.
1937 * The value of the string doesn't change if an error is
1941 replace(__const_iterator __i1
, __const_iterator __i2
,
1942 const _CharT
* __s
, size_type __n
)
1944 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
1946 return this->replace(__i1
- begin(), __i2
- __i1
, __s
, __n
);
1950 * @brief Replace range of characters with C string.
1951 * @param __i1 Iterator referencing start of range to replace.
1952 * @param __i2 Iterator referencing end of range to replace.
1953 * @param __s C string value to insert.
1954 * @return Reference to this string.
1955 * @throw std::length_error If new length exceeds @c max_size().
1957 * Removes the characters in the range [__i1,__i2). In place,
1958 * the characters of @a __s are inserted. If the length of
1959 * result exceeds max_size(), length_error is thrown. The
1960 * value of the string doesn't change if an error is thrown.
1963 replace(__const_iterator __i1
, __const_iterator __i2
, const _CharT
* __s
)
1965 __glibcxx_requires_string(__s
);
1966 return this->replace(__i1
, __i2
, __s
, traits_type::length(__s
));
1970 * @brief Replace range of characters with multiple characters
1971 * @param __i1 Iterator referencing start of range to replace.
1972 * @param __i2 Iterator referencing end of range to replace.
1973 * @param __n Number of characters to insert.
1974 * @param __c Character to insert.
1975 * @return Reference to this string.
1976 * @throw std::length_error If new length exceeds @c max_size().
1978 * Removes the characters in the range [__i1,__i2). In place,
1979 * @a __n copies of @a __c are inserted. If the length of
1980 * result exceeds max_size(), length_error is thrown. The
1981 * value of the string doesn't change if an error is thrown.
1984 replace(__const_iterator __i1
, __const_iterator __i2
, size_type __n
,
1987 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
1989 return _M_replace_aux(__i1
- begin(), __i2
- __i1
, __n
, __c
);
1993 * @brief Replace range of characters with range.
1994 * @param __i1 Iterator referencing start of range to replace.
1995 * @param __i2 Iterator referencing end of range to replace.
1996 * @param __k1 Iterator referencing start of range to insert.
1997 * @param __k2 Iterator referencing end of range to insert.
1998 * @return Reference to this string.
1999 * @throw std::length_error If new length exceeds @c max_size().
2001 * Removes the characters in the range [__i1,__i2). In place,
2002 * characters in the range [__k1,__k2) are inserted. If the
2003 * length of result exceeds max_size(), length_error is thrown.
2004 * The value of the string doesn't change if an error is
2007 #if __cplusplus >= 201103L
2008 template<class _InputIterator
,
2009 typename
= std::_RequireInputIter
<_InputIterator
>>
2011 replace(const_iterator __i1
, const_iterator __i2
,
2012 _InputIterator __k1
, _InputIterator __k2
)
2014 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2016 __glibcxx_requires_valid_range(__k1
, __k2
);
2017 return this->_M_replace_dispatch(__i1
, __i2
, __k1
, __k2
,
2018 std::__false_type());
2021 template<class _InputIterator
>
2022 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
2023 typename __enable_if_not_native_iterator
<_InputIterator
>::__type
2027 replace(iterator __i1
, iterator __i2
,
2028 _InputIterator __k1
, _InputIterator __k2
)
2030 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2032 __glibcxx_requires_valid_range(__k1
, __k2
);
2033 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
2034 return _M_replace_dispatch(__i1
, __i2
, __k1
, __k2
, _Integral());
2038 // Specializations for the common case of pointer and iterator:
2039 // useful to avoid the overhead of temporary buffering in _M_replace.
2041 replace(__const_iterator __i1
, __const_iterator __i2
,
2042 _CharT
* __k1
, _CharT
* __k2
)
2044 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2046 __glibcxx_requires_valid_range(__k1
, __k2
);
2047 return this->replace(__i1
- begin(), __i2
- __i1
,
2052 replace(__const_iterator __i1
, __const_iterator __i2
,
2053 const _CharT
* __k1
, const _CharT
* __k2
)
2055 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2057 __glibcxx_requires_valid_range(__k1
, __k2
);
2058 return this->replace(__i1
- begin(), __i2
- __i1
,
2063 replace(__const_iterator __i1
, __const_iterator __i2
,
2064 iterator __k1
, iterator __k2
)
2066 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2068 __glibcxx_requires_valid_range(__k1
, __k2
);
2069 return this->replace(__i1
- begin(), __i2
- __i1
,
2070 __k1
.base(), __k2
- __k1
);
2074 replace(__const_iterator __i1
, __const_iterator __i2
,
2075 const_iterator __k1
, const_iterator __k2
)
2077 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2079 __glibcxx_requires_valid_range(__k1
, __k2
);
2080 return this->replace(__i1
- begin(), __i2
- __i1
,
2081 __k1
.base(), __k2
- __k1
);
2084 #if __cplusplus >= 201103L
2086 * @brief Replace range of characters with initializer_list.
2087 * @param __i1 Iterator referencing start of range to replace.
2088 * @param __i2 Iterator referencing end of range to replace.
2089 * @param __l The initializer_list of characters to insert.
2090 * @return Reference to this string.
2091 * @throw std::length_error If new length exceeds @c max_size().
2093 * Removes the characters in the range [__i1,__i2). In place,
2094 * characters in the range [__k1,__k2) are inserted. If the
2095 * length of result exceeds max_size(), length_error is thrown.
2096 * The value of the string doesn't change if an error is
2099 basic_string
& replace(const_iterator __i1
, const_iterator __i2
,
2100 initializer_list
<_CharT
> __l
)
2101 { return this->replace(__i1
, __i2
, __l
.begin(), __l
.size()); }
2104 #if __cplusplus > 201402L
2106 * @brief Replace range of characters with string_view.
2107 * @param __pos The position to replace at.
2108 * @param __n The number of characters to replace.
2109 * @param __sv The string_view to insert.
2110 * @return Reference to this string.
2113 replace(size_type __pos
, size_type __n
, __sv_type __sv
)
2114 { return this->replace(__pos
, __n
, __sv
.data(), __sv
.size()); }
2117 * @brief Replace range of characters with string_view.
2118 * @param __pos1 The position to replace at.
2119 * @param __n1 The number of characters to replace.
2120 * @param __sv The string_view to insert from.
2121 * @param __pos2 The position in the string_view to insert from.
2122 * @param __n2 The number of characters to insert.
2123 * @return Reference to this string.
2125 template <typename _Tp
>
2126 _If_sv
<_Tp
, basic_string
&>
2127 replace(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
2128 size_type __pos2
, size_type __n2
= npos
)
2130 __sv_type __sv
= __svt
;
2131 return this->replace(__pos1
, __n1
, __sv
.data()
2132 + __sv
._M_check(__pos2
, "basic_string::replace"),
2133 __sv
._M_limit(__pos2
, __n2
));
2137 * @brief Replace range of characters with string_view.
2138 * @param __i1 An iterator referencing the start position
2140 * @param __i2 An iterator referencing the end position
2142 * @param __sv The string_view to insert from.
2143 * @return Reference to this string.
2146 replace(const_iterator __i1
, const_iterator __i2
, __sv_type __sv
)
2147 { return this->replace(__i1
- begin(), __i2
- __i1
, __sv
); }
2151 template<class _Integer
>
2153 _M_replace_dispatch(const_iterator __i1
, const_iterator __i2
,
2154 _Integer __n
, _Integer __val
, __true_type
)
2155 { return _M_replace_aux(__i1
- begin(), __i2
- __i1
, __n
, __val
); }
2157 template<class _InputIterator
>
2159 _M_replace_dispatch(const_iterator __i1
, const_iterator __i2
,
2160 _InputIterator __k1
, _InputIterator __k2
,
2164 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
2168 _M_replace(size_type __pos
, size_type __len1
, const _CharT
* __s
,
2169 const size_type __len2
);
2172 _M_append(const _CharT
* __s
, size_type __n
);
2177 * @brief Copy substring into C string.
2178 * @param __s C string to copy value into.
2179 * @param __n Number of characters to copy.
2180 * @param __pos Index of first character to copy.
2181 * @return Number of characters actually copied
2182 * @throw std::out_of_range If __pos > size().
2184 * Copies up to @a __n characters starting at @a __pos into the
2185 * C string @a __s. If @a __pos is %greater than size(),
2186 * out_of_range is thrown.
2189 copy(_CharT
* __s
, size_type __n
, size_type __pos
= 0) const;
2192 * @brief Swap contents with another string.
2193 * @param __s String to swap with.
2195 * Exchanges the contents of this string with that of @a __s in constant
2199 swap(basic_string
& __s
) _GLIBCXX_NOEXCEPT
;
2201 // String operations:
2203 * @brief Return const pointer to null-terminated contents.
2205 * This is a handle to internal data. Do not modify or dire things may
2209 c_str() const _GLIBCXX_NOEXCEPT
2210 { return _M_data(); }
2213 * @brief Return const pointer to contents.
2215 * This is a pointer to internal data. It is undefined to modify
2216 * the contents through the returned pointer. To get a pointer that
2217 * allows modifying the contents use @c &str[0] instead,
2218 * (or in C++17 the non-const @c str.data() overload).
2221 data() const _GLIBCXX_NOEXCEPT
2222 { return _M_data(); }
2224 #if __cplusplus > 201402L
2226 * @brief Return non-const pointer to contents.
2228 * This is a pointer to the character sequence held by the string.
2229 * Modifying the characters in the sequence is allowed.
2233 { return _M_data(); }
2237 * @brief Return copy of allocator used to construct this string.
2240 get_allocator() const _GLIBCXX_NOEXCEPT
2241 { return _M_get_allocator(); }
2244 * @brief Find position of a C substring.
2245 * @param __s C string to locate.
2246 * @param __pos Index of character to search from.
2247 * @param __n Number of characters from @a s to search for.
2248 * @return Index of start of first occurrence.
2250 * Starting from @a __pos, searches forward for the first @a
2251 * __n characters in @a __s within this string. If found,
2252 * returns the index where it begins. If not found, returns
2256 find(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
2259 * @brief Find position of a string.
2260 * @param __str String to locate.
2261 * @param __pos Index of character to search from (default 0).
2262 * @return Index of start of first occurrence.
2264 * Starting from @a __pos, searches forward for value of @a __str within
2265 * this string. If found, returns the index where it begins. If not
2266 * found, returns npos.
2269 find(const basic_string
& __str
, size_type __pos
= 0) const
2271 { return this->find(__str
.data(), __pos
, __str
.size()); }
2273 #if __cplusplus > 201402L
2275 * @brief Find position of a string_view.
2276 * @param __sv The string_view to locate.
2277 * @param __pos Index of character to search from (default 0).
2278 * @return Index of start of first occurrence.
2281 find(__sv_type __sv
, size_type __pos
= 0) const noexcept
2282 { return this->find(__sv
.data(), __pos
, __sv
.size()); }
2286 * @brief Find position of a C string.
2287 * @param __s C string to locate.
2288 * @param __pos Index of character to search from (default 0).
2289 * @return Index of start of first occurrence.
2291 * Starting from @a __pos, searches forward for the value of @a
2292 * __s within this string. If found, returns the index where
2293 * it begins. If not found, returns npos.
2296 find(const _CharT
* __s
, size_type __pos
= 0) const
2298 __glibcxx_requires_string(__s
);
2299 return this->find(__s
, __pos
, traits_type::length(__s
));
2303 * @brief Find position of a character.
2304 * @param __c Character to locate.
2305 * @param __pos Index of character to search from (default 0).
2306 * @return Index of first occurrence.
2308 * Starting from @a __pos, searches forward for @a __c within
2309 * this string. If found, returns the index where it was
2310 * found. If not found, returns npos.
2313 find(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
;
2316 * @brief Find last position of a string.
2317 * @param __str String to locate.
2318 * @param __pos Index of character to search back from (default end).
2319 * @return Index of start of last occurrence.
2321 * Starting from @a __pos, searches backward for value of @a
2322 * __str within this string. If found, returns the index where
2323 * it begins. If not found, returns npos.
2326 rfind(const basic_string
& __str
, size_type __pos
= npos
) const
2328 { return this->rfind(__str
.data(), __pos
, __str
.size()); }
2330 #if __cplusplus > 201402L
2332 * @brief Find last position of a string_view.
2333 * @param __sv The string_view to locate.
2334 * @param __pos Index of character to search back from (default end).
2335 * @return Index of start of last occurrence.
2338 rfind(__sv_type __sv
, size_type __pos
= npos
) const noexcept
2339 { return this->rfind(__sv
.data(), __pos
, __sv
.size()); }
2343 * @brief Find last position of a C substring.
2344 * @param __s C string to locate.
2345 * @param __pos Index of character to search back from.
2346 * @param __n Number of characters from s to search for.
2347 * @return Index of start of last occurrence.
2349 * Starting from @a __pos, searches backward for the first @a
2350 * __n characters in @a __s within this string. If found,
2351 * returns the index where it begins. If not found, returns
2355 rfind(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
2358 * @brief Find last position of a C string.
2359 * @param __s C string to locate.
2360 * @param __pos Index of character to start search at (default end).
2361 * @return Index of start of last occurrence.
2363 * Starting from @a __pos, searches backward for the value of
2364 * @a __s within this string. If found, returns the index
2365 * where it begins. If not found, returns npos.
2368 rfind(const _CharT
* __s
, size_type __pos
= npos
) const
2370 __glibcxx_requires_string(__s
);
2371 return this->rfind(__s
, __pos
, traits_type::length(__s
));
2375 * @brief Find last position of a character.
2376 * @param __c Character to locate.
2377 * @param __pos Index of character to search back from (default end).
2378 * @return Index of last occurrence.
2380 * Starting from @a __pos, searches backward for @a __c within
2381 * this string. If found, returns the index where it was
2382 * found. If not found, returns npos.
2385 rfind(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
;
2388 * @brief Find position of a character of string.
2389 * @param __str String containing characters to locate.
2390 * @param __pos Index of character to search from (default 0).
2391 * @return Index of first occurrence.
2393 * Starting from @a __pos, searches forward for one of the
2394 * characters of @a __str within this string. If found,
2395 * returns the index where it was found. If not found, returns
2399 find_first_of(const basic_string
& __str
, size_type __pos
= 0) const
2401 { return this->find_first_of(__str
.data(), __pos
, __str
.size()); }
2403 #if __cplusplus > 201402L
2405 * @brief Find position of a character of a string_view.
2406 * @param __sv A string_view containing characters to locate.
2407 * @param __pos Index of character to search from (default 0).
2408 * @return Index of first occurrence.
2411 find_first_of(__sv_type __sv
, size_type __pos
= 0) const noexcept
2412 { return this->find_first_of(__sv
.data(), __pos
, __sv
.size()); }
2416 * @brief Find position of a character of C substring.
2417 * @param __s String containing characters to locate.
2418 * @param __pos Index of character to search from.
2419 * @param __n Number of characters from s to search for.
2420 * @return Index of first occurrence.
2422 * Starting from @a __pos, searches forward for one of the
2423 * first @a __n characters of @a __s within this string. If
2424 * found, returns the index where it was found. If not found,
2428 find_first_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
2431 * @brief Find position of a character of C string.
2432 * @param __s String containing characters to locate.
2433 * @param __pos Index of character to search from (default 0).
2434 * @return Index of first occurrence.
2436 * Starting from @a __pos, searches forward for one of the
2437 * characters of @a __s within this string. If found, returns
2438 * the index where it was found. If not found, returns npos.
2441 find_first_of(const _CharT
* __s
, size_type __pos
= 0) const
2443 __glibcxx_requires_string(__s
);
2444 return this->find_first_of(__s
, __pos
, traits_type::length(__s
));
2448 * @brief Find position of a character.
2449 * @param __c Character to locate.
2450 * @param __pos Index of character to search from (default 0).
2451 * @return Index of first occurrence.
2453 * Starting from @a __pos, searches forward for the character
2454 * @a __c within this string. If found, returns the index
2455 * where it was found. If not found, returns npos.
2457 * Note: equivalent to find(__c, __pos).
2460 find_first_of(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
2461 { return this->find(__c
, __pos
); }
2464 * @brief Find last position of a character of string.
2465 * @param __str String containing characters to locate.
2466 * @param __pos Index of character to search back from (default end).
2467 * @return Index of last occurrence.
2469 * Starting from @a __pos, searches backward for one of the
2470 * characters of @a __str within this string. If found,
2471 * returns the index where it was found. If not found, returns
2475 find_last_of(const basic_string
& __str
, size_type __pos
= npos
) const
2477 { return this->find_last_of(__str
.data(), __pos
, __str
.size()); }
2479 #if __cplusplus > 201402L
2481 * @brief Find last position of a character of string.
2482 * @param __sv A string_view containing characters to locate.
2483 * @param __pos Index of character to search back from (default end).
2484 * @return Index of last occurrence.
2487 find_last_of(__sv_type __sv
, size_type __pos
= npos
) const noexcept
2488 { return this->find_last_of(__sv
.data(), __pos
, __sv
.size()); }
2492 * @brief Find last position of a character of C substring.
2493 * @param __s C string containing characters to locate.
2494 * @param __pos Index of character to search back from.
2495 * @param __n Number of characters from s to search for.
2496 * @return Index of last occurrence.
2498 * Starting from @a __pos, searches backward for one of the
2499 * first @a __n characters of @a __s within this string. If
2500 * found, returns the index where it was found. If not found,
2504 find_last_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
2507 * @brief Find last position of a character of C string.
2508 * @param __s C string containing characters to locate.
2509 * @param __pos Index of character to search back from (default end).
2510 * @return Index of last occurrence.
2512 * Starting from @a __pos, searches backward for one of the
2513 * characters of @a __s within this string. If found, returns
2514 * the index where it was found. If not found, returns npos.
2517 find_last_of(const _CharT
* __s
, size_type __pos
= npos
) const
2519 __glibcxx_requires_string(__s
);
2520 return this->find_last_of(__s
, __pos
, traits_type::length(__s
));
2524 * @brief Find last position of a character.
2525 * @param __c Character to locate.
2526 * @param __pos Index of character to search back from (default end).
2527 * @return Index of last occurrence.
2529 * Starting from @a __pos, searches backward for @a __c within
2530 * this string. If found, returns the index where it was
2531 * found. If not found, returns npos.
2533 * Note: equivalent to rfind(__c, __pos).
2536 find_last_of(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
2537 { return this->rfind(__c
, __pos
); }
2540 * @brief Find position of a character not in string.
2541 * @param __str String containing characters to avoid.
2542 * @param __pos Index of character to search from (default 0).
2543 * @return Index of first occurrence.
2545 * Starting from @a __pos, searches forward for a character not contained
2546 * in @a __str within this string. If found, returns the index where it
2547 * was found. If not found, returns npos.
2550 find_first_not_of(const basic_string
& __str
, size_type __pos
= 0) const
2552 { return this->find_first_not_of(__str
.data(), __pos
, __str
.size()); }
2554 #if __cplusplus > 201402L
2556 * @brief Find position of a character not in a string_view.
2557 * @param __sv A string_view containing characters to avoid.
2558 * @param __pos Index of character to search from (default 0).
2559 * @return Index of first occurrence.
2562 find_first_not_of(__sv_type __sv
, size_type __pos
= 0) const noexcept
2563 { return this->find_first_not_of(__sv
.data(), __pos
, __sv
.size()); }
2567 * @brief Find position of a character not in C substring.
2568 * @param __s C string containing characters to avoid.
2569 * @param __pos Index of character to search from.
2570 * @param __n Number of characters from __s to consider.
2571 * @return Index of first occurrence.
2573 * Starting from @a __pos, searches forward for a character not
2574 * contained in the first @a __n characters of @a __s within
2575 * this string. If found, returns the index where it was
2576 * found. If not found, returns npos.
2579 find_first_not_of(const _CharT
* __s
, size_type __pos
,
2580 size_type __n
) const;
2583 * @brief Find position of a character not in C string.
2584 * @param __s C string containing characters to avoid.
2585 * @param __pos Index of character to search from (default 0).
2586 * @return Index of first occurrence.
2588 * Starting from @a __pos, searches forward for a character not
2589 * contained in @a __s within this string. If found, returns
2590 * the index where it was found. If not found, returns npos.
2593 find_first_not_of(const _CharT
* __s
, size_type __pos
= 0) const
2595 __glibcxx_requires_string(__s
);
2596 return this->find_first_not_of(__s
, __pos
, traits_type::length(__s
));
2600 * @brief Find position of a different character.
2601 * @param __c Character to avoid.
2602 * @param __pos Index of character to search from (default 0).
2603 * @return Index of first occurrence.
2605 * Starting from @a __pos, searches forward for a character
2606 * other than @a __c within this string. If found, returns the
2607 * index where it was found. If not found, returns npos.
2610 find_first_not_of(_CharT __c
, size_type __pos
= 0) const
2614 * @brief Find last position of a character not in string.
2615 * @param __str String containing characters to avoid.
2616 * @param __pos Index of character to search back from (default end).
2617 * @return Index of last occurrence.
2619 * Starting from @a __pos, searches backward for a character
2620 * not contained in @a __str within this string. If found,
2621 * returns the index where it was found. If not found, returns
2625 find_last_not_of(const basic_string
& __str
, size_type __pos
= npos
) const
2627 { return this->find_last_not_of(__str
.data(), __pos
, __str
.size()); }
2629 #if __cplusplus > 201402L
2631 * @brief Find last position of a character not in a string_view.
2632 * @param __sv A string_view containing characters to avoid.
2633 * @param __pos Index of character to search back from (default end).
2634 * @return Index of last occurrence.
2637 find_last_not_of(__sv_type __sv
, size_type __pos
= npos
) const noexcept
2638 { return this->find_last_not_of(__sv
.data(), __pos
, __sv
.size()); }
2642 * @brief Find last position of a character not in C substring.
2643 * @param __s C string containing characters to avoid.
2644 * @param __pos Index of character to search back from.
2645 * @param __n Number of characters from s to consider.
2646 * @return Index of last occurrence.
2648 * Starting from @a __pos, searches backward for a character not
2649 * contained in the first @a __n characters of @a __s within this string.
2650 * If found, returns the index where it was found. If not found,
2654 find_last_not_of(const _CharT
* __s
, size_type __pos
,
2655 size_type __n
) const;
2657 * @brief Find last position of a character not in C string.
2658 * @param __s C string containing characters to avoid.
2659 * @param __pos Index of character to search back from (default end).
2660 * @return Index of last occurrence.
2662 * Starting from @a __pos, searches backward for a character
2663 * not contained in @a __s within this string. If found,
2664 * returns the index where it was found. If not found, returns
2668 find_last_not_of(const _CharT
* __s
, size_type __pos
= npos
) const
2670 __glibcxx_requires_string(__s
);
2671 return this->find_last_not_of(__s
, __pos
, traits_type::length(__s
));
2675 * @brief Find last position of a different character.
2676 * @param __c Character to avoid.
2677 * @param __pos Index of character to search back from (default end).
2678 * @return Index of last occurrence.
2680 * Starting from @a __pos, searches backward for a character other than
2681 * @a __c within this string. If found, returns the index where it was
2682 * found. If not found, returns npos.
2685 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const
2689 * @brief Get a substring.
2690 * @param __pos Index of first character (default 0).
2691 * @param __n Number of characters in substring (default remainder).
2692 * @return The new string.
2693 * @throw std::out_of_range If __pos > size().
2695 * Construct and return a new string using the @a __n
2696 * characters starting at @a __pos. If the string is too
2697 * short, use the remainder of the characters. If @a __pos is
2698 * beyond the end of the string, out_of_range is thrown.
2701 substr(size_type __pos
= 0, size_type __n
= npos
) const
2702 { return basic_string(*this,
2703 _M_check(__pos
, "basic_string::substr"), __n
); }
2706 * @brief Compare to a string.
2707 * @param __str String to compare against.
2708 * @return Integer < 0, 0, or > 0.
2710 * Returns an integer < 0 if this string is ordered before @a
2711 * __str, 0 if their values are equivalent, or > 0 if this
2712 * string is ordered after @a __str. Determines the effective
2713 * length rlen of the strings to compare as the smallest of
2714 * size() and str.size(). The function then compares the two
2715 * strings by calling traits::compare(data(), str.data(),rlen).
2716 * If the result of the comparison is nonzero returns it,
2717 * otherwise the shorter one is ordered first.
2720 compare(const basic_string
& __str
) const
2722 const size_type __size
= this->size();
2723 const size_type __osize
= __str
.size();
2724 const size_type __len
= std::min(__size
, __osize
);
2726 int __r
= traits_type::compare(_M_data(), __str
.data(), __len
);
2728 __r
= _S_compare(__size
, __osize
);
2732 #if __cplusplus > 201402L
2734 * @brief Compare to a string_view.
2735 * @param __sv A string_view to compare against.
2736 * @return Integer < 0, 0, or > 0.
2739 compare(__sv_type __sv
) const
2741 const size_type __size
= this->size();
2742 const size_type __osize
= __sv
.size();
2743 const size_type __len
= std::min(__size
, __osize
);
2745 int __r
= traits_type::compare(_M_data(), __sv
.data(), __len
);
2747 __r
= _S_compare(__size
, __osize
);
2752 * @brief Compare to a string_view.
2753 * @param __pos A position in the string to start comparing from.
2754 * @param __n The number of characters to compare.
2755 * @param __sv A string_view to compare against.
2756 * @return Integer < 0, 0, or > 0.
2759 compare(size_type __pos
, size_type __n
, __sv_type __sv
) const
2760 { return __sv_type(*this).substr(__pos
, __n
).compare(__sv
); }
2763 * @brief Compare to a string_view.
2764 * @param __pos1 A position in the string to start comparing from.
2765 * @param __n1 The number of characters to compare.
2766 * @param __sv A string_view to compare against.
2767 * @param __pos2 A position in the string_view to start comparing from.
2768 * @param __n2 The number of characters to compare.
2769 * @return Integer < 0, 0, or > 0.
2771 template <typename _Tp
>
2773 compare(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
2774 size_type __pos2
, size_type __n2
= npos
) const
2776 __sv_type __sv
= __svt
;
2777 return __sv_type(*this)
2778 .substr(__pos1
, __n1
).compare(__sv
.substr(__pos2
, __n2
));
2783 * @brief Compare substring to a string.
2784 * @param __pos Index of first character of substring.
2785 * @param __n Number of characters in substring.
2786 * @param __str String to compare against.
2787 * @return Integer < 0, 0, or > 0.
2789 * Form the substring of this string from the @a __n characters
2790 * starting at @a __pos. Returns an integer < 0 if the
2791 * substring is ordered before @a __str, 0 if their values are
2792 * equivalent, or > 0 if the substring is ordered after @a
2793 * __str. Determines the effective length rlen of the strings
2794 * to compare as the smallest of the length of the substring
2795 * and @a __str.size(). The function then compares the two
2796 * strings by calling
2797 * traits::compare(substring.data(),str.data(),rlen). If the
2798 * result of the comparison is nonzero returns it, otherwise
2799 * the shorter one is ordered first.
2802 compare(size_type __pos
, size_type __n
, const basic_string
& __str
) const;
2805 * @brief Compare substring to a substring.
2806 * @param __pos1 Index of first character of substring.
2807 * @param __n1 Number of characters in substring.
2808 * @param __str String to compare against.
2809 * @param __pos2 Index of first character of substring of str.
2810 * @param __n2 Number of characters in substring of str.
2811 * @return Integer < 0, 0, or > 0.
2813 * Form the substring of this string from the @a __n1
2814 * characters starting at @a __pos1. Form the substring of @a
2815 * __str from the @a __n2 characters starting at @a __pos2.
2816 * Returns an integer < 0 if this substring is ordered before
2817 * the substring of @a __str, 0 if their values are equivalent,
2818 * or > 0 if this substring is ordered after the substring of
2819 * @a __str. Determines the effective length rlen of the
2820 * strings to compare as the smallest of the lengths of the
2821 * substrings. The function then compares the two strings by
2823 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2824 * If the result of the comparison is nonzero returns it,
2825 * otherwise the shorter one is ordered first.
2828 compare(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
2829 size_type __pos2
, size_type __n2
) const;
2832 * @brief Compare to a C string.
2833 * @param __s C string to compare against.
2834 * @return Integer < 0, 0, or > 0.
2836 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
2837 * their values are equivalent, or > 0 if this string is ordered after
2838 * @a __s. Determines the effective length rlen of the strings to
2839 * compare as the smallest of size() and the length of a string
2840 * constructed from @a __s. The function then compares the two strings
2841 * by calling traits::compare(data(),s,rlen). If the result of the
2842 * comparison is nonzero returns it, otherwise the shorter one is
2846 compare(const _CharT
* __s
) const;
2848 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2849 // 5 String::compare specification questionable
2851 * @brief Compare substring to a C string.
2852 * @param __pos Index of first character of substring.
2853 * @param __n1 Number of characters in substring.
2854 * @param __s C string to compare against.
2855 * @return Integer < 0, 0, or > 0.
2857 * Form the substring of this string from the @a __n1
2858 * characters starting at @a pos. Returns an integer < 0 if
2859 * the substring is ordered before @a __s, 0 if their values
2860 * are equivalent, or > 0 if the substring is ordered after @a
2861 * __s. Determines the effective length rlen of the strings to
2862 * compare as the smallest of the length of the substring and
2863 * the length of a string constructed from @a __s. The
2864 * function then compares the two string by calling
2865 * traits::compare(substring.data(),__s,rlen). If the result of
2866 * the comparison is nonzero returns it, otherwise the shorter
2867 * one is ordered first.
2870 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
) const;
2873 * @brief Compare substring against a character %array.
2874 * @param __pos Index of first character of substring.
2875 * @param __n1 Number of characters in substring.
2876 * @param __s character %array to compare against.
2877 * @param __n2 Number of characters of s.
2878 * @return Integer < 0, 0, or > 0.
2880 * Form the substring of this string from the @a __n1
2881 * characters starting at @a __pos. Form a string from the
2882 * first @a __n2 characters of @a __s. Returns an integer < 0
2883 * if this substring is ordered before the string from @a __s,
2884 * 0 if their values are equivalent, or > 0 if this substring
2885 * is ordered after the string from @a __s. Determines the
2886 * effective length rlen of the strings to compare as the
2887 * smallest of the length of the substring and @a __n2. The
2888 * function then compares the two strings by calling
2889 * traits::compare(substring.data(),s,rlen). If the result of
2890 * the comparison is nonzero returns it, otherwise the shorter
2891 * one is ordered first.
2893 * NB: s must have at least n2 characters, '\\0' has
2894 * no special meaning.
2897 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
,
2898 size_type __n2
) const;
2900 _GLIBCXX_END_NAMESPACE_CXX11
2901 #else // !_GLIBCXX_USE_CXX11_ABI
2902 // Reference-counted COW string implentation
2905 * @class basic_string basic_string.h <string>
2906 * @brief Managing sequences of characters and character-like objects.
2909 * @ingroup sequences
2911 * @tparam _CharT Type of character
2912 * @tparam _Traits Traits for character type, defaults to
2913 * char_traits<_CharT>.
2914 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
2916 * Meets the requirements of a <a href="tables.html#65">container</a>, a
2917 * <a href="tables.html#66">reversible container</a>, and a
2918 * <a href="tables.html#67">sequence</a>. Of the
2919 * <a href="tables.html#68">optional sequence requirements</a>, only
2920 * @c push_back, @c at, and @c %array access are supported.
2925 * Documentation? What's that?
2926 * Nathan Myers <ncm@cantrip.org>.
2928 * A string looks like this:
2933 * [basic_string<char_type>] _M_capacity
2934 * _M_dataplus _M_refcount
2935 * _M_p ----------------> unnamed array of char_type
2938 * Where the _M_p points to the first character in the string, and
2939 * you cast it to a pointer-to-_Rep and subtract 1 to get a
2940 * pointer to the header.
2942 * This approach has the enormous advantage that a string object
2943 * requires only one allocation. All the ugliness is confined
2944 * within a single %pair of inline functions, which each compile to
2945 * a single @a add instruction: _Rep::_M_data(), and
2946 * string::_M_rep(); and the allocation function which gets a
2947 * block of raw bytes and with room enough and constructs a _Rep
2948 * object at the front.
2950 * The reason you want _M_data pointing to the character %array and
2951 * not the _Rep is so that the debugger can see the string
2952 * contents. (Probably we should add a non-inline member to get
2953 * the _Rep for the debugger to use, so users can check the actual
2956 * Note that the _Rep object is a POD so that you can have a
2957 * static <em>empty string</em> _Rep object already @a constructed before
2958 * static constructors have run. The reference-count encoding is
2959 * chosen so that a 0 indicates one reference, so you never try to
2960 * destroy the empty-string _Rep object.
2962 * All but the last paragraph is considered pretty conventional
2963 * for a C++ string implementation.
2965 // 21.3 Template class basic_string
2966 template<typename _CharT
, typename _Traits
, typename _Alloc
>
2969 typedef typename
_Alloc::template rebind
<_CharT
>::other _CharT_alloc_type
;
2973 typedef _Traits traits_type
;
2974 typedef typename
_Traits::char_type value_type
;
2975 typedef _Alloc allocator_type
;
2976 typedef typename
_CharT_alloc_type::size_type size_type
;
2977 typedef typename
_CharT_alloc_type::difference_type difference_type
;
2978 typedef typename
_CharT_alloc_type::reference reference
;
2979 typedef typename
_CharT_alloc_type::const_reference const_reference
;
2980 typedef typename
_CharT_alloc_type::pointer pointer
;
2981 typedef typename
_CharT_alloc_type::const_pointer const_pointer
;
2982 typedef __gnu_cxx::__normal_iterator
<pointer
, basic_string
> iterator
;
2983 typedef __gnu_cxx::__normal_iterator
<const_pointer
, basic_string
>
2985 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
2986 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
2989 // _Rep: string representation
2991 // 1. String really contains _M_length + 1 characters: due to 21.3.4
2992 // must be kept null-terminated.
2993 // 2. _M_capacity >= _M_length
2994 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
2995 // 3. _M_refcount has three states:
2996 // -1: leaked, one reference, no ref-copies allowed, non-const.
2997 // 0: one reference, non-const.
2998 // n>0: n + 1 references, operations require a lock, const.
2999 // 4. All fields==0 is an empty string, given the extra storage
3000 // beyond-the-end for a null terminator; thus, the shared
3001 // empty string representation needs no constructor.
3005 size_type _M_length
;
3006 size_type _M_capacity
;
3007 _Atomic_word _M_refcount
;
3010 struct _Rep
: _Rep_base
3013 typedef typename
_Alloc::template rebind
<char>::other _Raw_bytes_alloc
;
3015 // (Public) Data members:
3017 // The maximum number of individual char_type elements of an
3018 // individual string is determined by _S_max_size. This is the
3019 // value that will be returned by max_size(). (Whereas npos
3020 // is the maximum number of bytes the allocator can allocate.)
3021 // If one was to divvy up the theoretical largest size string,
3022 // with a terminating character and m _CharT elements, it'd
3024 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
3026 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
3027 // In addition, this implementation quarters this amount.
3028 static const size_type _S_max_size
;
3029 static const _CharT _S_terminal
;
3031 // The following storage is init'd to 0 by the linker, resulting
3032 // (carefully) in an empty string with one reference.
3033 static size_type _S_empty_rep_storage
[];
3036 _S_empty_rep() _GLIBCXX_NOEXCEPT
3038 // NB: Mild hack to avoid strict-aliasing warnings. Note that
3039 // _S_empty_rep_storage is never modified and the punning should
3040 // be reasonably safe in this case.
3041 void* __p
= reinterpret_cast<void*>(&_S_empty_rep_storage
);
3042 return *reinterpret_cast<_Rep
*>(__p
);
3046 _M_is_leaked() const _GLIBCXX_NOEXCEPT
3048 #if defined(__GTHREADS)
3049 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3050 // so we need to use an atomic load. However, _M_is_leaked
3051 // predicate does not change concurrently (i.e. the string is either
3052 // leaked or not), so a relaxed load is enough.
3053 return __atomic_load_n(&this->_M_refcount
, __ATOMIC_RELAXED
) < 0;
3055 return this->_M_refcount
< 0;
3060 _M_is_shared() const _GLIBCXX_NOEXCEPT
3062 #if defined(__GTHREADS)
3063 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3064 // so we need to use an atomic load. Another thread can drop last
3065 // but one reference concurrently with this check, so we need this
3066 // load to be acquire to synchronize with release fetch_and_add in
3068 return __atomic_load_n(&this->_M_refcount
, __ATOMIC_ACQUIRE
) > 0;
3070 return this->_M_refcount
> 0;
3075 _M_set_leaked() _GLIBCXX_NOEXCEPT
3076 { this->_M_refcount
= -1; }
3079 _M_set_sharable() _GLIBCXX_NOEXCEPT
3080 { this->_M_refcount
= 0; }
3083 _M_set_length_and_sharable(size_type __n
) _GLIBCXX_NOEXCEPT
3085 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3086 if (__builtin_expect(this != &_S_empty_rep(), false))
3089 this->_M_set_sharable(); // One reference.
3090 this->_M_length
= __n
;
3091 traits_type::assign(this->_M_refdata()[__n
], _S_terminal
);
3092 // grrr. (per 21.3.4)
3093 // You cannot leave those LWG people alone for a second.
3098 _M_refdata() throw()
3099 { return reinterpret_cast<_CharT
*>(this + 1); }
3102 _M_grab(const _Alloc
& __alloc1
, const _Alloc
& __alloc2
)
3104 return (!_M_is_leaked() && __alloc1
== __alloc2
)
3105 ? _M_refcopy() : _M_clone(__alloc1
);
3110 _S_create(size_type
, size_type
, const _Alloc
&);
3113 _M_dispose(const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
3115 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3116 if (__builtin_expect(this != &_S_empty_rep(), false))
3119 // Be race-detector-friendly. For more info see bits/c++config.
3120 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount
);
3121 // Decrement of _M_refcount is acq_rel, because:
3122 // - all but last decrements need to release to synchronize with
3123 // the last decrement that will delete the object.
3124 // - the last decrement needs to acquire to synchronize with
3125 // all the previous decrements.
3126 // - last but one decrement needs to release to synchronize with
3127 // the acquire load in _M_is_shared that will conclude that
3128 // the object is not shared anymore.
3129 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount
,
3132 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount
);
3139 _M_destroy(const _Alloc
&) throw();
3142 _M_refcopy() throw()
3144 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3145 if (__builtin_expect(this != &_S_empty_rep(), false))
3147 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount
, 1);
3148 return _M_refdata();
3152 _M_clone(const _Alloc
&, size_type __res
= 0);
3155 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
3156 struct _Alloc_hider
: _Alloc
3158 _Alloc_hider(_CharT
* __dat
, const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
3159 : _Alloc(__a
), _M_p(__dat
) { }
3161 _CharT
* _M_p
; // The actual data.
3165 // Data Members (public):
3166 // NB: This is an unsigned type, and thus represents the maximum
3167 // size that the allocator can hold.
3168 /// Value returned by various member functions when they fail.
3169 static const size_type npos
= static_cast<size_type
>(-1);
3172 // Data Members (private):
3173 mutable _Alloc_hider _M_dataplus
;
3176 _M_data() const _GLIBCXX_NOEXCEPT
3177 { return _M_dataplus
._M_p
; }
3180 _M_data(_CharT
* __p
) _GLIBCXX_NOEXCEPT
3181 { return (_M_dataplus
._M_p
= __p
); }
3184 _M_rep() const _GLIBCXX_NOEXCEPT
3185 { return &((reinterpret_cast<_Rep
*> (_M_data()))[-1]); }
3187 // For the internal use we have functions similar to `begin'/`end'
3188 // but they do not call _M_leak.
3190 _M_ibegin() const _GLIBCXX_NOEXCEPT
3191 { return iterator(_M_data()); }
3194 _M_iend() const _GLIBCXX_NOEXCEPT
3195 { return iterator(_M_data() + this->size()); }
3198 _M_leak() // for use in begin() & non-const op[]
3200 if (!_M_rep()->_M_is_leaked())
3205 _M_check(size_type __pos
, const char* __s
) const
3207 if (__pos
> this->size())
3208 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
3209 "this->size() (which is %zu)"),
3210 __s
, __pos
, this->size());
3215 _M_check_length(size_type __n1
, size_type __n2
, const char* __s
) const
3217 if (this->max_size() - (this->size() - __n1
) < __n2
)
3218 __throw_length_error(__N(__s
));
3221 // NB: _M_limit doesn't check for a bad __pos value.
3223 _M_limit(size_type __pos
, size_type __off
) const _GLIBCXX_NOEXCEPT
3225 const bool __testoff
= __off
< this->size() - __pos
;
3226 return __testoff
? __off
: this->size() - __pos
;
3229 // True if _Rep and source do not overlap.
3231 _M_disjunct(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
3233 return (less
<const _CharT
*>()(__s
, _M_data())
3234 || less
<const _CharT
*>()(_M_data() + this->size(), __s
));
3237 // When __n = 1 way faster than the general multichar
3238 // traits_type::copy/move/assign.
3240 _M_copy(_CharT
* __d
, const _CharT
* __s
, size_type __n
) _GLIBCXX_NOEXCEPT
3243 traits_type::assign(*__d
, *__s
);
3245 traits_type::copy(__d
, __s
, __n
);
3249 _M_move(_CharT
* __d
, const _CharT
* __s
, size_type __n
) _GLIBCXX_NOEXCEPT
3252 traits_type::assign(*__d
, *__s
);
3254 traits_type::move(__d
, __s
, __n
);
3258 _M_assign(_CharT
* __d
, size_type __n
, _CharT __c
) _GLIBCXX_NOEXCEPT
3261 traits_type::assign(*__d
, __c
);
3263 traits_type::assign(__d
, __n
, __c
);
3266 // _S_copy_chars is a separate template to permit specialization
3267 // to optimize for the common case of pointers as iterators.
3268 template<class _Iterator
>
3270 _S_copy_chars(_CharT
* __p
, _Iterator __k1
, _Iterator __k2
)
3272 for (; __k1
!= __k2
; ++__k1
, (void)++__p
)
3273 traits_type::assign(*__p
, *__k1
); // These types are off.
3277 _S_copy_chars(_CharT
* __p
, iterator __k1
, iterator __k2
) _GLIBCXX_NOEXCEPT
3278 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
3281 _S_copy_chars(_CharT
* __p
, const_iterator __k1
, const_iterator __k2
)
3283 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
3286 _S_copy_chars(_CharT
* __p
, _CharT
* __k1
, _CharT
* __k2
) _GLIBCXX_NOEXCEPT
3287 { _M_copy(__p
, __k1
, __k2
- __k1
); }
3290 _S_copy_chars(_CharT
* __p
, const _CharT
* __k1
, const _CharT
* __k2
)
3292 { _M_copy(__p
, __k1
, __k2
- __k1
); }
3295 _S_compare(size_type __n1
, size_type __n2
) _GLIBCXX_NOEXCEPT
3297 const difference_type __d
= difference_type(__n1
- __n2
);
3299 if (__d
> __gnu_cxx::__numeric_traits
<int>::__max
)
3300 return __gnu_cxx::__numeric_traits
<int>::__max
;
3301 else if (__d
< __gnu_cxx::__numeric_traits
<int>::__min
)
3302 return __gnu_cxx::__numeric_traits
<int>::__min
;
3308 _M_mutate(size_type __pos
, size_type __len1
, size_type __len2
);
3314 _S_empty_rep() _GLIBCXX_NOEXCEPT
3315 { return _Rep::_S_empty_rep(); }
3318 // Construct/copy/destroy:
3319 // NB: We overload ctors in some cases instead of using default
3320 // arguments, per 17.4.4.4 para. 2 item 2.
3323 * @brief Default constructor creates an empty string.
3326 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3327 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc()) { }
3329 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc()){ }
3333 * @brief Construct an empty string using allocator @a a.
3336 basic_string(const _Alloc
& __a
);
3338 // NB: per LWG issue 42, semantics different from IS:
3340 * @brief Construct string with copy of value of @a str.
3341 * @param __str Source string.
3343 basic_string(const basic_string
& __str
);
3345 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3346 // 2583. no way to supply an allocator for basic_string(str, pos)
3348 * @brief Construct string as copy of a substring.
3349 * @param __str Source string.
3350 * @param __pos Index of first character to copy from.
3351 * @param __a Allocator to use.
3353 basic_string(const basic_string
& __str
, size_type __pos
,
3354 const _Alloc
& __a
= _Alloc());
3357 * @brief Construct string as copy of a substring.
3358 * @param __str Source string.
3359 * @param __pos Index of first character to copy from.
3360 * @param __n Number of characters to copy.
3362 basic_string(const basic_string
& __str
, size_type __pos
,
3365 * @brief Construct string as copy of a substring.
3366 * @param __str Source string.
3367 * @param __pos Index of first character to copy from.
3368 * @param __n Number of characters to copy.
3369 * @param __a Allocator to use.
3371 basic_string(const basic_string
& __str
, size_type __pos
,
3372 size_type __n
, const _Alloc
& __a
);
3375 * @brief Construct string initialized by a character %array.
3376 * @param __s Source character %array.
3377 * @param __n Number of characters to copy.
3378 * @param __a Allocator to use (default is default allocator).
3380 * NB: @a __s must have at least @a __n characters, '\\0'
3381 * has no special meaning.
3383 basic_string(const _CharT
* __s
, size_type __n
,
3384 const _Alloc
& __a
= _Alloc());
3386 * @brief Construct string as copy of a C string.
3387 * @param __s Source C string.
3388 * @param __a Allocator to use (default is default allocator).
3390 basic_string(const _CharT
* __s
, const _Alloc
& __a
= _Alloc());
3392 * @brief Construct string as multiple characters.
3393 * @param __n Number of characters.
3394 * @param __c Character to use.
3395 * @param __a Allocator to use (default is default allocator).
3397 basic_string(size_type __n
, _CharT __c
, const _Alloc
& __a
= _Alloc());
3399 #if __cplusplus >= 201103L
3401 * @brief Move construct string.
3402 * @param __str Source string.
3404 * The newly-created string contains the exact contents of @a __str.
3405 * @a __str is a valid, but unspecified string.
3407 basic_string(basic_string
&& __str
)
3408 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3409 noexcept
// FIXME C++11: should always be noexcept.
3411 : _M_dataplus(__str
._M_dataplus
)
3413 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3414 __str
._M_data(_S_empty_rep()._M_refdata());
3416 __str
._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
3421 * @brief Construct string from an initializer %list.
3422 * @param __l std::initializer_list of characters.
3423 * @param __a Allocator to use (default is default allocator).
3425 basic_string(initializer_list
<_CharT
> __l
, const _Alloc
& __a
= _Alloc());
3429 * @brief Construct string as copy of a range.
3430 * @param __beg Start of range.
3431 * @param __end End of range.
3432 * @param __a Allocator to use (default is default allocator).
3434 template<class _InputIterator
>
3435 basic_string(_InputIterator __beg
, _InputIterator __end
,
3436 const _Alloc
& __a
= _Alloc());
3439 * @brief Destroy the string instance.
3441 ~basic_string() _GLIBCXX_NOEXCEPT
3442 { _M_rep()->_M_dispose(this->get_allocator()); }
3445 * @brief Assign the value of @a str to this string.
3446 * @param __str Source string.
3449 operator=(const basic_string
& __str
)
3450 { return this->assign(__str
); }
3453 * @brief Copy contents of @a s into this string.
3454 * @param __s Source null-terminated string.
3457 operator=(const _CharT
* __s
)
3458 { return this->assign(__s
); }
3461 * @brief Set value to string of length 1.
3462 * @param __c Source character.
3464 * Assigning to a character makes this string length 1 and
3465 * (*this)[0] == @a c.
3468 operator=(_CharT __c
)
3470 this->assign(1, __c
);
3474 #if __cplusplus >= 201103L
3476 * @brief Move assign the value of @a str to this string.
3477 * @param __str Source string.
3479 * The contents of @a str are moved into this string (without copying).
3480 * @a str is a valid, but unspecified string.
3482 // PR 58265, this should be noexcept.
3484 operator=(basic_string
&& __str
)
3492 * @brief Set value to string constructed from initializer %list.
3493 * @param __l std::initializer_list.
3496 operator=(initializer_list
<_CharT
> __l
)
3498 this->assign(__l
.begin(), __l
.size());
3505 * Returns a read/write iterator that points to the first character in
3506 * the %string. Unshares the string.
3509 begin() // FIXME C++11: should be noexcept.
3512 return iterator(_M_data());
3516 * Returns a read-only (constant) iterator that points to the first
3517 * character in the %string.
3520 begin() const _GLIBCXX_NOEXCEPT
3521 { return const_iterator(_M_data()); }
3524 * Returns a read/write iterator that points one past the last
3525 * character in the %string. Unshares the string.
3528 end() // FIXME C++11: should be noexcept.
3531 return iterator(_M_data() + this->size());
3535 * Returns a read-only (constant) iterator that points one past the
3536 * last character in the %string.
3539 end() const _GLIBCXX_NOEXCEPT
3540 { return const_iterator(_M_data() + this->size()); }
3543 * Returns a read/write reverse iterator that points to the last
3544 * character in the %string. Iteration is done in reverse element
3545 * order. Unshares the string.
3548 rbegin() // FIXME C++11: should be noexcept.
3549 { return reverse_iterator(this->end()); }
3552 * Returns a read-only (constant) reverse iterator that points
3553 * to the last character in the %string. Iteration is done in
3554 * reverse element order.
3556 const_reverse_iterator
3557 rbegin() const _GLIBCXX_NOEXCEPT
3558 { return const_reverse_iterator(this->end()); }
3561 * Returns a read/write reverse iterator that points to one before the
3562 * first character in the %string. Iteration is done in reverse
3563 * element order. Unshares the string.
3566 rend() // FIXME C++11: should be noexcept.
3567 { return reverse_iterator(this->begin()); }
3570 * Returns a read-only (constant) reverse iterator that points
3571 * to one before the first character in the %string. Iteration
3572 * is done in reverse element order.
3574 const_reverse_iterator
3575 rend() const _GLIBCXX_NOEXCEPT
3576 { return const_reverse_iterator(this->begin()); }
3578 #if __cplusplus >= 201103L
3580 * Returns a read-only (constant) iterator that points to the first
3581 * character in the %string.
3584 cbegin() const noexcept
3585 { return const_iterator(this->_M_data()); }
3588 * Returns a read-only (constant) iterator that points one past the
3589 * last character in the %string.
3592 cend() const noexcept
3593 { return const_iterator(this->_M_data() + this->size()); }
3596 * Returns a read-only (constant) reverse iterator that points
3597 * to the last character in the %string. Iteration is done in
3598 * reverse element order.
3600 const_reverse_iterator
3601 crbegin() const noexcept
3602 { return const_reverse_iterator(this->end()); }
3605 * Returns a read-only (constant) reverse iterator that points
3606 * to one before the first character in the %string. Iteration
3607 * is done in reverse element order.
3609 const_reverse_iterator
3610 crend() const noexcept
3611 { return const_reverse_iterator(this->begin()); }
3616 /// Returns the number of characters in the string, not including any
3617 /// null-termination.
3619 size() const _GLIBCXX_NOEXCEPT
3620 { return _M_rep()->_M_length
; }
3622 /// Returns the number of characters in the string, not including any
3623 /// null-termination.
3625 length() const _GLIBCXX_NOEXCEPT
3626 { return _M_rep()->_M_length
; }
3628 /// Returns the size() of the largest possible %string.
3630 max_size() const _GLIBCXX_NOEXCEPT
3631 { return _Rep::_S_max_size
; }
3634 * @brief Resizes the %string to the specified number of characters.
3635 * @param __n Number of characters the %string should contain.
3636 * @param __c Character to fill any new elements.
3638 * This function will %resize the %string to the specified
3639 * number of characters. If the number is smaller than the
3640 * %string's current size the %string is truncated, otherwise
3641 * the %string is extended and new elements are %set to @a __c.
3644 resize(size_type __n
, _CharT __c
);
3647 * @brief Resizes the %string to the specified number of characters.
3648 * @param __n Number of characters the %string should contain.
3650 * This function will resize the %string to the specified length. If
3651 * the new size is smaller than the %string's current size the %string
3652 * is truncated, otherwise the %string is extended and new characters
3653 * are default-constructed. For basic types such as char, this means
3654 * setting them to 0.
3657 resize(size_type __n
)
3658 { this->resize(__n
, _CharT()); }
3660 #if __cplusplus >= 201103L
3661 /// A non-binding request to reduce capacity() to size().
3663 shrink_to_fit() _GLIBCXX_NOEXCEPT
3665 #if __cpp_exceptions
3666 if (capacity() > size())
3678 * Returns the total number of characters that the %string can hold
3679 * before needing to allocate more memory.
3682 capacity() const _GLIBCXX_NOEXCEPT
3683 { return _M_rep()->_M_capacity
; }
3686 * @brief Attempt to preallocate enough memory for specified number of
3688 * @param __res_arg Number of characters required.
3689 * @throw std::length_error If @a __res_arg exceeds @c max_size().
3691 * This function attempts to reserve enough memory for the
3692 * %string to hold the specified number of characters. If the
3693 * number requested is more than max_size(), length_error is
3696 * The advantage of this function is that if optimal code is a
3697 * necessity and the user can determine the string length that will be
3698 * required, the user can reserve the memory in %advance, and thus
3699 * prevent a possible reallocation of memory and copying of %string
3703 reserve(size_type __res_arg
= 0);
3706 * Erases the string, making it empty.
3708 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3710 clear() _GLIBCXX_NOEXCEPT
3712 if (_M_rep()->_M_is_shared())
3714 _M_rep()->_M_dispose(this->get_allocator());
3715 _M_data(_S_empty_rep()._M_refdata());
3718 _M_rep()->_M_set_length_and_sharable(0);
3721 // PR 56166: this should not throw.
3724 { _M_mutate(0, this->size(), 0); }
3728 * Returns true if the %string is empty. Equivalent to
3729 * <code>*this == ""</code>.
3732 empty() const _GLIBCXX_NOEXCEPT
3733 { return this->size() == 0; }
3737 * @brief Subscript access to the data contained in the %string.
3738 * @param __pos The index of the character to access.
3739 * @return Read-only (constant) reference to the character.
3741 * This operator allows for easy, array-style, data access.
3742 * Note that data access with this operator is unchecked and
3743 * out_of_range lookups are not defined. (For checked lookups
3747 operator[] (size_type __pos
) const _GLIBCXX_NOEXCEPT
3749 __glibcxx_assert(__pos
<= size());
3750 return _M_data()[__pos
];
3754 * @brief Subscript access to the data contained in the %string.
3755 * @param __pos The index of the character to access.
3756 * @return Read/write reference to the character.
3758 * This operator allows for easy, array-style, data access.
3759 * Note that data access with this operator is unchecked and
3760 * out_of_range lookups are not defined. (For checked lookups
3761 * see at().) Unshares the string.
3764 operator[](size_type __pos
)
3766 // Allow pos == size() both in C++98 mode, as v3 extension,
3767 // and in C++11 mode.
3768 __glibcxx_assert(__pos
<= size());
3769 // In pedantic mode be strict in C++98 mode.
3770 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus
>= 201103L || __pos
< size());
3772 return _M_data()[__pos
];
3776 * @brief Provides access to the data contained in the %string.
3777 * @param __n The index of the character to access.
3778 * @return Read-only (const) reference to the character.
3779 * @throw std::out_of_range If @a n is an invalid index.
3781 * This function provides for safer data access. The parameter is
3782 * first checked that it is in the range of the string. The function
3783 * throws out_of_range if the check fails.
3786 at(size_type __n
) const
3788 if (__n
>= this->size())
3789 __throw_out_of_range_fmt(__N("basic_string::at: __n "
3790 "(which is %zu) >= this->size() "
3793 return _M_data()[__n
];
3797 * @brief Provides access to the data contained in the %string.
3798 * @param __n The index of the character to access.
3799 * @return Read/write reference to the character.
3800 * @throw std::out_of_range If @a n is an invalid index.
3802 * This function provides for safer data access. The parameter is
3803 * first checked that it is in the range of the string. The function
3804 * throws out_of_range if the check fails. Success results in
3805 * unsharing the string.
3811 __throw_out_of_range_fmt(__N("basic_string::at: __n "
3812 "(which is %zu) >= this->size() "
3816 return _M_data()[__n
];
3819 #if __cplusplus >= 201103L
3821 * Returns a read/write reference to the data at the first
3822 * element of the %string.
3827 __glibcxx_assert(!empty());
3828 return operator[](0);
3832 * Returns a read-only (constant) reference to the data at the first
3833 * element of the %string.
3836 front() const noexcept
3838 __glibcxx_assert(!empty());
3839 return operator[](0);
3843 * Returns a read/write reference to the data at the last
3844 * element of the %string.
3849 __glibcxx_assert(!empty());
3850 return operator[](this->size() - 1);
3854 * Returns a read-only (constant) reference to the data at the
3855 * last element of the %string.
3858 back() const noexcept
3860 __glibcxx_assert(!empty());
3861 return operator[](this->size() - 1);
3867 * @brief Append a string to this string.
3868 * @param __str The string to append.
3869 * @return Reference to this string.
3872 operator+=(const basic_string
& __str
)
3873 { return this->append(__str
); }
3876 * @brief Append a C string.
3877 * @param __s The C string to append.
3878 * @return Reference to this string.
3881 operator+=(const _CharT
* __s
)
3882 { return this->append(__s
); }
3885 * @brief Append a character.
3886 * @param __c The character to append.
3887 * @return Reference to this string.
3890 operator+=(_CharT __c
)
3892 this->push_back(__c
);
3896 #if __cplusplus >= 201103L
3898 * @brief Append an initializer_list of characters.
3899 * @param __l The initializer_list of characters to be appended.
3900 * @return Reference to this string.
3903 operator+=(initializer_list
<_CharT
> __l
)
3904 { return this->append(__l
.begin(), __l
.size()); }
3908 * @brief Append a string to this string.
3909 * @param __str The string to append.
3910 * @return Reference to this string.
3913 append(const basic_string
& __str
);
3916 * @brief Append a substring.
3917 * @param __str The string to append.
3918 * @param __pos Index of the first character of str to append.
3919 * @param __n The number of characters to append.
3920 * @return Reference to this string.
3921 * @throw std::out_of_range if @a __pos is not a valid index.
3923 * This function appends @a __n characters from @a __str
3924 * starting at @a __pos to this string. If @a __n is is larger
3925 * than the number of available characters in @a __str, the
3926 * remainder of @a __str is appended.
3929 append(const basic_string
& __str
, size_type __pos
, size_type __n
);
3932 * @brief Append a C substring.
3933 * @param __s The C string to append.
3934 * @param __n The number of characters to append.
3935 * @return Reference to this string.
3938 append(const _CharT
* __s
, size_type __n
);
3941 * @brief Append a C string.
3942 * @param __s The C string to append.
3943 * @return Reference to this string.
3946 append(const _CharT
* __s
)
3948 __glibcxx_requires_string(__s
);
3949 return this->append(__s
, traits_type::length(__s
));
3953 * @brief Append multiple characters.
3954 * @param __n The number of characters to append.
3955 * @param __c The character to use.
3956 * @return Reference to this string.
3958 * Appends __n copies of __c to this string.
3961 append(size_type __n
, _CharT __c
);
3963 #if __cplusplus >= 201103L
3965 * @brief Append an initializer_list of characters.
3966 * @param __l The initializer_list of characters to append.
3967 * @return Reference to this string.
3970 append(initializer_list
<_CharT
> __l
)
3971 { return this->append(__l
.begin(), __l
.size()); }
3975 * @brief Append a range of characters.
3976 * @param __first Iterator referencing the first character to append.
3977 * @param __last Iterator marking the end of the range.
3978 * @return Reference to this string.
3980 * Appends characters in the range [__first,__last) to this string.
3982 template<class _InputIterator
>
3984 append(_InputIterator __first
, _InputIterator __last
)
3985 { return this->replace(_M_iend(), _M_iend(), __first
, __last
); }
3988 * @brief Append a single character.
3989 * @param __c Character to append.
3992 push_back(_CharT __c
)
3994 const size_type __len
= 1 + this->size();
3995 if (__len
> this->capacity() || _M_rep()->_M_is_shared())
3996 this->reserve(__len
);
3997 traits_type::assign(_M_data()[this->size()], __c
);
3998 _M_rep()->_M_set_length_and_sharable(__len
);
4002 * @brief Set value to contents of another string.
4003 * @param __str Source string to use.
4004 * @return Reference to this string.
4007 assign(const basic_string
& __str
);
4009 #if __cplusplus >= 201103L
4011 * @brief Set value to contents of another string.
4012 * @param __str Source string to use.
4013 * @return Reference to this string.
4015 * This function sets this string to the exact contents of @a __str.
4016 * @a __str is a valid, but unspecified string.
4018 // PR 58265, this should be noexcept.
4020 assign(basic_string
&& __str
)
4028 * @brief Set value to a substring of a string.
4029 * @param __str The string to use.
4030 * @param __pos Index of the first character of str.
4031 * @param __n Number of characters to use.
4032 * @return Reference to this string.
4033 * @throw std::out_of_range if @a pos is not a valid index.
4035 * This function sets this string to the substring of @a __str
4036 * consisting of @a __n characters at @a __pos. If @a __n is
4037 * is larger than the number of available characters in @a
4038 * __str, the remainder of @a __str is used.
4041 assign(const basic_string
& __str
, size_type __pos
, size_type __n
)
4042 { return this->assign(__str
._M_data()
4043 + __str
._M_check(__pos
, "basic_string::assign"),
4044 __str
._M_limit(__pos
, __n
)); }
4047 * @brief Set value to a C substring.
4048 * @param __s The C string to use.
4049 * @param __n Number of characters to use.
4050 * @return Reference to this string.
4052 * This function sets the value of this string to the first @a __n
4053 * characters of @a __s. If @a __n is is larger than the number of
4054 * available characters in @a __s, the remainder of @a __s is used.
4057 assign(const _CharT
* __s
, size_type __n
);
4060 * @brief Set value to contents of a C string.
4061 * @param __s The C string to use.
4062 * @return Reference to this string.
4064 * This function sets the value of this string to the value of @a __s.
4065 * The data is copied, so there is no dependence on @a __s once the
4069 assign(const _CharT
* __s
)
4071 __glibcxx_requires_string(__s
);
4072 return this->assign(__s
, traits_type::length(__s
));
4076 * @brief Set value to multiple characters.
4077 * @param __n Length of the resulting string.
4078 * @param __c The character to use.
4079 * @return Reference to this string.
4081 * This function sets the value of this string to @a __n copies of
4085 assign(size_type __n
, _CharT __c
)
4086 { return _M_replace_aux(size_type(0), this->size(), __n
, __c
); }
4089 * @brief Set value to a range of characters.
4090 * @param __first Iterator referencing the first character to append.
4091 * @param __last Iterator marking the end of the range.
4092 * @return Reference to this string.
4094 * Sets value of string to characters in the range [__first,__last).
4096 template<class _InputIterator
>
4098 assign(_InputIterator __first
, _InputIterator __last
)
4099 { return this->replace(_M_ibegin(), _M_iend(), __first
, __last
); }
4101 #if __cplusplus >= 201103L
4103 * @brief Set value to an initializer_list of characters.
4104 * @param __l The initializer_list of characters to assign.
4105 * @return Reference to this string.
4108 assign(initializer_list
<_CharT
> __l
)
4109 { return this->assign(__l
.begin(), __l
.size()); }
4113 * @brief Insert multiple characters.
4114 * @param __p Iterator referencing location in string to insert at.
4115 * @param __n Number of characters to insert
4116 * @param __c The character to insert.
4117 * @throw std::length_error If new length exceeds @c max_size().
4119 * Inserts @a __n copies of character @a __c starting at the
4120 * position referenced by iterator @a __p. If adding
4121 * characters causes the length to exceed max_size(),
4122 * length_error is thrown. The value of the string doesn't
4123 * change if an error is thrown.
4126 insert(iterator __p
, size_type __n
, _CharT __c
)
4127 { this->replace(__p
, __p
, __n
, __c
); }
4130 * @brief Insert a range of characters.
4131 * @param __p Iterator referencing location in string to insert at.
4132 * @param __beg Start of range.
4133 * @param __end End of range.
4134 * @throw std::length_error If new length exceeds @c max_size().
4136 * Inserts characters in range [__beg,__end). If adding
4137 * characters causes the length to exceed max_size(),
4138 * length_error is thrown. The value of the string doesn't
4139 * change if an error is thrown.
4141 template<class _InputIterator
>
4143 insert(iterator __p
, _InputIterator __beg
, _InputIterator __end
)
4144 { this->replace(__p
, __p
, __beg
, __end
); }
4146 #if __cplusplus >= 201103L
4148 * @brief Insert an initializer_list of characters.
4149 * @param __p Iterator referencing location in string to insert at.
4150 * @param __l The initializer_list of characters to insert.
4151 * @throw std::length_error If new length exceeds @c max_size().
4154 insert(iterator __p
, initializer_list
<_CharT
> __l
)
4156 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
4157 this->insert(__p
- _M_ibegin(), __l
.begin(), __l
.size());
4162 * @brief Insert value of a string.
4163 * @param __pos1 Iterator referencing location in string to insert at.
4164 * @param __str The string to insert.
4165 * @return Reference to this string.
4166 * @throw std::length_error If new length exceeds @c max_size().
4168 * Inserts value of @a __str starting at @a __pos1. If adding
4169 * characters causes the length to exceed max_size(),
4170 * length_error is thrown. The value of the string doesn't
4171 * change if an error is thrown.
4174 insert(size_type __pos1
, const basic_string
& __str
)
4175 { return this->insert(__pos1
, __str
, size_type(0), __str
.size()); }
4178 * @brief Insert a substring.
4179 * @param __pos1 Iterator referencing location in string to insert at.
4180 * @param __str The string to insert.
4181 * @param __pos2 Start of characters in str to insert.
4182 * @param __n Number of characters to insert.
4183 * @return Reference to this string.
4184 * @throw std::length_error If new length exceeds @c max_size().
4185 * @throw std::out_of_range If @a pos1 > size() or
4186 * @a __pos2 > @a str.size().
4188 * Starting at @a pos1, insert @a __n character of @a __str
4189 * beginning with @a __pos2. If adding characters causes the
4190 * length to exceed max_size(), length_error is thrown. If @a
4191 * __pos1 is beyond the end of this string or @a __pos2 is
4192 * beyond the end of @a __str, out_of_range is thrown. The
4193 * value of the string doesn't change if an error is thrown.
4196 insert(size_type __pos1
, const basic_string
& __str
,
4197 size_type __pos2
, size_type __n
)
4198 { return this->insert(__pos1
, __str
._M_data()
4199 + __str
._M_check(__pos2
, "basic_string::insert"),
4200 __str
._M_limit(__pos2
, __n
)); }
4203 * @brief Insert a C substring.
4204 * @param __pos Iterator referencing location in string to insert at.
4205 * @param __s The C string to insert.
4206 * @param __n The number of characters to insert.
4207 * @return Reference to this string.
4208 * @throw std::length_error If new length exceeds @c max_size().
4209 * @throw std::out_of_range If @a __pos is beyond the end of this
4212 * Inserts the first @a __n characters of @a __s starting at @a
4213 * __pos. If adding characters causes the length to exceed
4214 * max_size(), length_error is thrown. If @a __pos is beyond
4215 * end(), out_of_range is thrown. The value of the string
4216 * doesn't change if an error is thrown.
4219 insert(size_type __pos
, const _CharT
* __s
, size_type __n
);
4222 * @brief Insert a C string.
4223 * @param __pos Iterator referencing location in string to insert at.
4224 * @param __s The C string to insert.
4225 * @return Reference to this string.
4226 * @throw std::length_error If new length exceeds @c max_size().
4227 * @throw std::out_of_range If @a pos is beyond the end of this
4230 * Inserts the first @a n characters of @a __s starting at @a __pos. If
4231 * adding characters causes the length to exceed max_size(),
4232 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
4233 * thrown. The value of the string doesn't change if an error is
4237 insert(size_type __pos
, const _CharT
* __s
)
4239 __glibcxx_requires_string(__s
);
4240 return this->insert(__pos
, __s
, traits_type::length(__s
));
4244 * @brief Insert multiple characters.
4245 * @param __pos Index in string to insert at.
4246 * @param __n Number of characters to insert
4247 * @param __c The character to insert.
4248 * @return Reference to this string.
4249 * @throw std::length_error If new length exceeds @c max_size().
4250 * @throw std::out_of_range If @a __pos is beyond the end of this
4253 * Inserts @a __n copies of character @a __c starting at index
4254 * @a __pos. If adding characters causes the length to exceed
4255 * max_size(), length_error is thrown. If @a __pos > length(),
4256 * out_of_range is thrown. The value of the string doesn't
4257 * change if an error is thrown.
4260 insert(size_type __pos
, size_type __n
, _CharT __c
)
4261 { return _M_replace_aux(_M_check(__pos
, "basic_string::insert"),
4262 size_type(0), __n
, __c
); }
4265 * @brief Insert one character.
4266 * @param __p Iterator referencing position in string to insert at.
4267 * @param __c The character to insert.
4268 * @return Iterator referencing newly inserted char.
4269 * @throw std::length_error If new length exceeds @c max_size().
4271 * Inserts character @a __c at position referenced by @a __p.
4272 * If adding character causes the length to exceed max_size(),
4273 * length_error is thrown. If @a __p is beyond end of string,
4274 * out_of_range is thrown. The value of the string doesn't
4275 * change if an error is thrown.
4278 insert(iterator __p
, _CharT __c
)
4280 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
4281 const size_type __pos
= __p
- _M_ibegin();
4282 _M_replace_aux(__pos
, size_type(0), size_type(1), __c
);
4283 _M_rep()->_M_set_leaked();
4284 return iterator(_M_data() + __pos
);
4288 * @brief Remove characters.
4289 * @param __pos Index of first character to remove (default 0).
4290 * @param __n Number of characters to remove (default remainder).
4291 * @return Reference to this string.
4292 * @throw std::out_of_range If @a pos is beyond the end of this
4295 * Removes @a __n characters from this string starting at @a
4296 * __pos. The length of the string is reduced by @a __n. If
4297 * there are < @a __n characters to remove, the remainder of
4298 * the string is truncated. If @a __p is beyond end of string,
4299 * out_of_range is thrown. The value of the string doesn't
4300 * change if an error is thrown.
4303 erase(size_type __pos
= 0, size_type __n
= npos
)
4305 _M_mutate(_M_check(__pos
, "basic_string::erase"),
4306 _M_limit(__pos
, __n
), size_type(0));
4311 * @brief Remove one character.
4312 * @param __position Iterator referencing the character to remove.
4313 * @return iterator referencing same location after removal.
4315 * Removes the character at @a __position from this string. The value
4316 * of the string doesn't change if an error is thrown.
4319 erase(iterator __position
)
4321 _GLIBCXX_DEBUG_PEDASSERT(__position
>= _M_ibegin()
4322 && __position
< _M_iend());
4323 const size_type __pos
= __position
- _M_ibegin();
4324 _M_mutate(__pos
, size_type(1), size_type(0));
4325 _M_rep()->_M_set_leaked();
4326 return iterator(_M_data() + __pos
);
4330 * @brief Remove a range of characters.
4331 * @param __first Iterator referencing the first character to remove.
4332 * @param __last Iterator referencing the end of the range.
4333 * @return Iterator referencing location of first after removal.
4335 * Removes the characters in the range [first,last) from this string.
4336 * The value of the string doesn't change if an error is thrown.
4339 erase(iterator __first
, iterator __last
);
4341 #if __cplusplus >= 201103L
4343 * @brief Remove the last character.
4345 * The string must be non-empty.
4348 pop_back() // FIXME C++11: should be noexcept.
4350 __glibcxx_assert(!empty());
4351 erase(size() - 1, 1);
4356 * @brief Replace characters with value from another string.
4357 * @param __pos Index of first character to replace.
4358 * @param __n Number of characters to be replaced.
4359 * @param __str String to insert.
4360 * @return Reference to this string.
4361 * @throw std::out_of_range If @a pos is beyond the end of this
4363 * @throw std::length_error If new length exceeds @c max_size().
4365 * Removes the characters in the range [__pos,__pos+__n) from
4366 * this string. In place, the value of @a __str is inserted.
4367 * If @a __pos is beyond end of string, out_of_range is thrown.
4368 * If the length of the result exceeds max_size(), length_error
4369 * is thrown. The value of the string doesn't change if an
4373 replace(size_type __pos
, size_type __n
, const basic_string
& __str
)
4374 { return this->replace(__pos
, __n
, __str
._M_data(), __str
.size()); }
4377 * @brief Replace characters with value from another string.
4378 * @param __pos1 Index of first character to replace.
4379 * @param __n1 Number of characters to be replaced.
4380 * @param __str String to insert.
4381 * @param __pos2 Index of first character of str to use.
4382 * @param __n2 Number of characters from str to use.
4383 * @return Reference to this string.
4384 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
4386 * @throw std::length_error If new length exceeds @c max_size().
4388 * Removes the characters in the range [__pos1,__pos1 + n) from this
4389 * string. In place, the value of @a __str is inserted. If @a __pos is
4390 * beyond end of string, out_of_range is thrown. If the length of the
4391 * result exceeds max_size(), length_error is thrown. The value of the
4392 * string doesn't change if an error is thrown.
4395 replace(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
4396 size_type __pos2
, size_type __n2
)
4397 { return this->replace(__pos1
, __n1
, __str
._M_data()
4398 + __str
._M_check(__pos2
, "basic_string::replace"),
4399 __str
._M_limit(__pos2
, __n2
)); }
4402 * @brief Replace characters with value of a C substring.
4403 * @param __pos Index of first character to replace.
4404 * @param __n1 Number of characters to be replaced.
4405 * @param __s C string to insert.
4406 * @param __n2 Number of characters from @a s to use.
4407 * @return Reference to this string.
4408 * @throw std::out_of_range If @a pos1 > size().
4409 * @throw std::length_error If new length exceeds @c max_size().
4411 * Removes the characters in the range [__pos,__pos + __n1)
4412 * from this string. In place, the first @a __n2 characters of
4413 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
4414 * @a __pos is beyond end of string, out_of_range is thrown. If
4415 * the length of result exceeds max_size(), length_error is
4416 * thrown. The value of the string doesn't change if an error
4420 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
4424 * @brief Replace characters with value of a C string.
4425 * @param __pos Index of first character to replace.
4426 * @param __n1 Number of characters to be replaced.
4427 * @param __s C string to insert.
4428 * @return Reference to this string.
4429 * @throw std::out_of_range If @a pos > size().
4430 * @throw std::length_error If new length exceeds @c max_size().
4432 * Removes the characters in the range [__pos,__pos + __n1)
4433 * from this string. In place, the characters of @a __s are
4434 * inserted. If @a __pos is beyond end of string, out_of_range
4435 * is thrown. If the length of result exceeds max_size(),
4436 * length_error is thrown. The value of the string doesn't
4437 * change if an error is thrown.
4440 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
)
4442 __glibcxx_requires_string(__s
);
4443 return this->replace(__pos
, __n1
, __s
, traits_type::length(__s
));
4447 * @brief Replace characters with multiple characters.
4448 * @param __pos Index of first character to replace.
4449 * @param __n1 Number of characters to be replaced.
4450 * @param __n2 Number of characters to insert.
4451 * @param __c Character to insert.
4452 * @return Reference to this string.
4453 * @throw std::out_of_range If @a __pos > size().
4454 * @throw std::length_error If new length exceeds @c max_size().
4456 * Removes the characters in the range [pos,pos + n1) from this
4457 * string. In place, @a __n2 copies of @a __c are inserted.
4458 * If @a __pos is beyond end of string, out_of_range is thrown.
4459 * If the length of result exceeds max_size(), length_error is
4460 * thrown. The value of the string doesn't change if an error
4464 replace(size_type __pos
, size_type __n1
, size_type __n2
, _CharT __c
)
4465 { return _M_replace_aux(_M_check(__pos
, "basic_string::replace"),
4466 _M_limit(__pos
, __n1
), __n2
, __c
); }
4469 * @brief Replace range of characters with string.
4470 * @param __i1 Iterator referencing start of range to replace.
4471 * @param __i2 Iterator referencing end of range to replace.
4472 * @param __str String value to insert.
4473 * @return Reference to this string.
4474 * @throw std::length_error If new length exceeds @c max_size().
4476 * Removes the characters in the range [__i1,__i2). In place,
4477 * the value of @a __str is inserted. If the length of result
4478 * exceeds max_size(), length_error is thrown. The value of
4479 * the string doesn't change if an error is thrown.
4482 replace(iterator __i1
, iterator __i2
, const basic_string
& __str
)
4483 { return this->replace(__i1
, __i2
, __str
._M_data(), __str
.size()); }
4486 * @brief Replace range of characters with C substring.
4487 * @param __i1 Iterator referencing start of range to replace.
4488 * @param __i2 Iterator referencing end of range to replace.
4489 * @param __s C string value to insert.
4490 * @param __n Number of characters from s to insert.
4491 * @return Reference to this string.
4492 * @throw std::length_error If new length exceeds @c max_size().
4494 * Removes the characters in the range [__i1,__i2). In place,
4495 * the first @a __n characters of @a __s are inserted. If the
4496 * length of result exceeds max_size(), length_error is thrown.
4497 * The value of the string doesn't change if an error is
4501 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
, size_type __n
)
4503 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4504 && __i2
<= _M_iend());
4505 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
, __s
, __n
);
4509 * @brief Replace range of characters with C string.
4510 * @param __i1 Iterator referencing start of range to replace.
4511 * @param __i2 Iterator referencing end of range to replace.
4512 * @param __s C string value to insert.
4513 * @return Reference to this string.
4514 * @throw std::length_error If new length exceeds @c max_size().
4516 * Removes the characters in the range [__i1,__i2). In place,
4517 * the characters of @a __s are inserted. If the length of
4518 * result exceeds max_size(), length_error is thrown. The
4519 * value of the string doesn't change if an error is thrown.
4522 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
)
4524 __glibcxx_requires_string(__s
);
4525 return this->replace(__i1
, __i2
, __s
, traits_type::length(__s
));
4529 * @brief Replace range of characters with multiple characters
4530 * @param __i1 Iterator referencing start of range to replace.
4531 * @param __i2 Iterator referencing end of range to replace.
4532 * @param __n Number of characters to insert.
4533 * @param __c Character to insert.
4534 * @return Reference to this string.
4535 * @throw std::length_error If new length exceeds @c max_size().
4537 * Removes the characters in the range [__i1,__i2). In place,
4538 * @a __n copies of @a __c are inserted. If the length of
4539 * result exceeds max_size(), length_error is thrown. The
4540 * value of the string doesn't change if an error is thrown.
4543 replace(iterator __i1
, iterator __i2
, size_type __n
, _CharT __c
)
4545 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4546 && __i2
<= _M_iend());
4547 return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __c
);
4551 * @brief Replace range of characters with range.
4552 * @param __i1 Iterator referencing start of range to replace.
4553 * @param __i2 Iterator referencing end of range to replace.
4554 * @param __k1 Iterator referencing start of range to insert.
4555 * @param __k2 Iterator referencing end of range to insert.
4556 * @return Reference to this string.
4557 * @throw std::length_error If new length exceeds @c max_size().
4559 * Removes the characters in the range [__i1,__i2). In place,
4560 * characters in the range [__k1,__k2) are inserted. If the
4561 * length of result exceeds max_size(), length_error is thrown.
4562 * The value of the string doesn't change if an error is
4565 template<class _InputIterator
>
4567 replace(iterator __i1
, iterator __i2
,
4568 _InputIterator __k1
, _InputIterator __k2
)
4570 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4571 && __i2
<= _M_iend());
4572 __glibcxx_requires_valid_range(__k1
, __k2
);
4573 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
4574 return _M_replace_dispatch(__i1
, __i2
, __k1
, __k2
, _Integral());
4577 // Specializations for the common case of pointer and iterator:
4578 // useful to avoid the overhead of temporary buffering in _M_replace.
4580 replace(iterator __i1
, iterator __i2
, _CharT
* __k1
, _CharT
* __k2
)
4582 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4583 && __i2
<= _M_iend());
4584 __glibcxx_requires_valid_range(__k1
, __k2
);
4585 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4590 replace(iterator __i1
, iterator __i2
,
4591 const _CharT
* __k1
, const _CharT
* __k2
)
4593 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4594 && __i2
<= _M_iend());
4595 __glibcxx_requires_valid_range(__k1
, __k2
);
4596 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4601 replace(iterator __i1
, iterator __i2
, iterator __k1
, iterator __k2
)
4603 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4604 && __i2
<= _M_iend());
4605 __glibcxx_requires_valid_range(__k1
, __k2
);
4606 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4607 __k1
.base(), __k2
- __k1
);
4611 replace(iterator __i1
, iterator __i2
,
4612 const_iterator __k1
, const_iterator __k2
)
4614 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4615 && __i2
<= _M_iend());
4616 __glibcxx_requires_valid_range(__k1
, __k2
);
4617 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4618 __k1
.base(), __k2
- __k1
);
4621 #if __cplusplus >= 201103L
4623 * @brief Replace range of characters with initializer_list.
4624 * @param __i1 Iterator referencing start of range to replace.
4625 * @param __i2 Iterator referencing end of range to replace.
4626 * @param __l The initializer_list of characters to insert.
4627 * @return Reference to this string.
4628 * @throw std::length_error If new length exceeds @c max_size().
4630 * Removes the characters in the range [__i1,__i2). In place,
4631 * characters in the range [__k1,__k2) are inserted. If the
4632 * length of result exceeds max_size(), length_error is thrown.
4633 * The value of the string doesn't change if an error is
4636 basic_string
& replace(iterator __i1
, iterator __i2
,
4637 initializer_list
<_CharT
> __l
)
4638 { return this->replace(__i1
, __i2
, __l
.begin(), __l
.end()); }
4642 template<class _Integer
>
4644 _M_replace_dispatch(iterator __i1
, iterator __i2
, _Integer __n
,
4645 _Integer __val
, __true_type
)
4646 { return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __val
); }
4648 template<class _InputIterator
>
4650 _M_replace_dispatch(iterator __i1
, iterator __i2
, _InputIterator __k1
,
4651 _InputIterator __k2
, __false_type
);
4654 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
4658 _M_replace_safe(size_type __pos1
, size_type __n1
, const _CharT
* __s
,
4661 // _S_construct_aux is used to implement the 21.3.1 para 15 which
4662 // requires special behaviour if _InIter is an integral type
4663 template<class _InIterator
>
4665 _S_construct_aux(_InIterator __beg
, _InIterator __end
,
4666 const _Alloc
& __a
, __false_type
)
4668 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
4669 return _S_construct(__beg
, __end
, __a
, _Tag());
4672 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4673 // 438. Ambiguity in the "do the right thing" clause
4674 template<class _Integer
>
4676 _S_construct_aux(_Integer __beg
, _Integer __end
,
4677 const _Alloc
& __a
, __true_type
)
4678 { return _S_construct_aux_2(static_cast<size_type
>(__beg
),
4682 _S_construct_aux_2(size_type __req
, _CharT __c
, const _Alloc
& __a
)
4683 { return _S_construct(__req
, __c
, __a
); }
4685 template<class _InIterator
>
4687 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
)
4689 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
4690 return _S_construct_aux(__beg
, __end
, __a
, _Integral());
4693 // For Input Iterators, used in istreambuf_iterators, etc.
4694 template<class _InIterator
>
4696 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
,
4697 input_iterator_tag
);
4699 // For forward_iterators up to random_access_iterators, used for
4700 // string::iterator, _CharT*, etc.
4701 template<class _FwdIterator
>
4703 _S_construct(_FwdIterator __beg
, _FwdIterator __end
, const _Alloc
& __a
,
4704 forward_iterator_tag
);
4707 _S_construct(size_type __req
, _CharT __c
, const _Alloc
& __a
);
4712 * @brief Copy substring into C string.
4713 * @param __s C string to copy value into.
4714 * @param __n Number of characters to copy.
4715 * @param __pos Index of first character to copy.
4716 * @return Number of characters actually copied
4717 * @throw std::out_of_range If __pos > size().
4719 * Copies up to @a __n characters starting at @a __pos into the
4720 * C string @a __s. If @a __pos is %greater than size(),
4721 * out_of_range is thrown.
4724 copy(_CharT
* __s
, size_type __n
, size_type __pos
= 0) const;
4727 * @brief Swap contents with another string.
4728 * @param __s String to swap with.
4730 * Exchanges the contents of this string with that of @a __s in constant
4733 // PR 58265, this should be noexcept.
4735 swap(basic_string
& __s
);
4737 // String operations:
4739 * @brief Return const pointer to null-terminated contents.
4741 * This is a handle to internal data. Do not modify or dire things may
4745 c_str() const _GLIBCXX_NOEXCEPT
4746 { return _M_data(); }
4749 * @brief Return const pointer to contents.
4751 * This is a pointer to internal data. It is undefined to modify
4752 * the contents through the returned pointer. To get a pointer that
4753 * allows modifying the contents use @c &str[0] instead,
4754 * (or in C++17 the non-const @c str.data() overload).
4757 data() const _GLIBCXX_NOEXCEPT
4758 { return _M_data(); }
4760 #if __cplusplus > 201402L
4762 * @brief Return non-const pointer to contents.
4764 * This is a pointer to the character sequence held by the string.
4765 * Modifying the characters in the sequence is allowed.
4769 { return _M_data(); }
4773 * @brief Return copy of allocator used to construct this string.
4776 get_allocator() const _GLIBCXX_NOEXCEPT
4777 { return _M_dataplus
; }
4780 * @brief Find position of a C substring.
4781 * @param __s C string to locate.
4782 * @param __pos Index of character to search from.
4783 * @param __n Number of characters from @a s to search for.
4784 * @return Index of start of first occurrence.
4786 * Starting from @a __pos, searches forward for the first @a
4787 * __n characters in @a __s within this string. If found,
4788 * returns the index where it begins. If not found, returns
4792 find(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
4795 * @brief Find position of a string.
4796 * @param __str String to locate.
4797 * @param __pos Index of character to search from (default 0).
4798 * @return Index of start of first occurrence.
4800 * Starting from @a __pos, searches forward for value of @a __str within
4801 * this string. If found, returns the index where it begins. If not
4802 * found, returns npos.
4805 find(const basic_string
& __str
, size_type __pos
= 0) const
4807 { return this->find(__str
.data(), __pos
, __str
.size()); }
4810 * @brief Find position of a C string.
4811 * @param __s C string to locate.
4812 * @param __pos Index of character to search from (default 0).
4813 * @return Index of start of first occurrence.
4815 * Starting from @a __pos, searches forward for the value of @a
4816 * __s within this string. If found, returns the index where
4817 * it begins. If not found, returns npos.
4820 find(const _CharT
* __s
, size_type __pos
= 0) const
4822 __glibcxx_requires_string(__s
);
4823 return this->find(__s
, __pos
, traits_type::length(__s
));
4827 * @brief Find position of a character.
4828 * @param __c Character to locate.
4829 * @param __pos Index of character to search from (default 0).
4830 * @return Index of first occurrence.
4832 * Starting from @a __pos, searches forward for @a __c within
4833 * this string. If found, returns the index where it was
4834 * found. If not found, returns npos.
4837 find(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
;
4840 * @brief Find last position of a string.
4841 * @param __str String to locate.
4842 * @param __pos Index of character to search back from (default end).
4843 * @return Index of start of last occurrence.
4845 * Starting from @a __pos, searches backward for value of @a
4846 * __str within this string. If found, returns the index where
4847 * it begins. If not found, returns npos.
4850 rfind(const basic_string
& __str
, size_type __pos
= npos
) const
4852 { return this->rfind(__str
.data(), __pos
, __str
.size()); }
4855 * @brief Find last position of a C substring.
4856 * @param __s C string to locate.
4857 * @param __pos Index of character to search back from.
4858 * @param __n Number of characters from s to search for.
4859 * @return Index of start of last occurrence.
4861 * Starting from @a __pos, searches backward for the first @a
4862 * __n characters in @a __s within this string. If found,
4863 * returns the index where it begins. If not found, returns
4867 rfind(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
4870 * @brief Find last position of a C string.
4871 * @param __s C string to locate.
4872 * @param __pos Index of character to start search at (default end).
4873 * @return Index of start of last occurrence.
4875 * Starting from @a __pos, searches backward for the value of
4876 * @a __s within this string. If found, returns the index
4877 * where it begins. If not found, returns npos.
4880 rfind(const _CharT
* __s
, size_type __pos
= npos
) const
4882 __glibcxx_requires_string(__s
);
4883 return this->rfind(__s
, __pos
, traits_type::length(__s
));
4887 * @brief Find last position of a character.
4888 * @param __c Character to locate.
4889 * @param __pos Index of character to search back from (default end).
4890 * @return Index of last occurrence.
4892 * Starting from @a __pos, searches backward for @a __c within
4893 * this string. If found, returns the index where it was
4894 * found. If not found, returns npos.
4897 rfind(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
;
4900 * @brief Find position of a character of string.
4901 * @param __str String containing characters to locate.
4902 * @param __pos Index of character to search from (default 0).
4903 * @return Index of first occurrence.
4905 * Starting from @a __pos, searches forward for one of the
4906 * characters of @a __str within this string. If found,
4907 * returns the index where it was found. If not found, returns
4911 find_first_of(const basic_string
& __str
, size_type __pos
= 0) const
4913 { return this->find_first_of(__str
.data(), __pos
, __str
.size()); }
4916 * @brief Find position of a character of C substring.
4917 * @param __s String containing characters to locate.
4918 * @param __pos Index of character to search from.
4919 * @param __n Number of characters from s to search for.
4920 * @return Index of first occurrence.
4922 * Starting from @a __pos, searches forward for one of the
4923 * first @a __n characters of @a __s within this string. If
4924 * found, returns the index where it was found. If not found,
4928 find_first_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
4931 * @brief Find position of a character of C string.
4932 * @param __s String containing characters to locate.
4933 * @param __pos Index of character to search from (default 0).
4934 * @return Index of first occurrence.
4936 * Starting from @a __pos, searches forward for one of the
4937 * characters of @a __s within this string. If found, returns
4938 * the index where it was found. If not found, returns npos.
4941 find_first_of(const _CharT
* __s
, size_type __pos
= 0) const
4943 __glibcxx_requires_string(__s
);
4944 return this->find_first_of(__s
, __pos
, traits_type::length(__s
));
4948 * @brief Find position of a character.
4949 * @param __c Character to locate.
4950 * @param __pos Index of character to search from (default 0).
4951 * @return Index of first occurrence.
4953 * Starting from @a __pos, searches forward for the character
4954 * @a __c within this string. If found, returns the index
4955 * where it was found. If not found, returns npos.
4957 * Note: equivalent to find(__c, __pos).
4960 find_first_of(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
4961 { return this->find(__c
, __pos
); }
4964 * @brief Find last position of a character of string.
4965 * @param __str String containing characters to locate.
4966 * @param __pos Index of character to search back from (default end).
4967 * @return Index of last occurrence.
4969 * Starting from @a __pos, searches backward for one of the
4970 * characters of @a __str within this string. If found,
4971 * returns the index where it was found. If not found, returns
4975 find_last_of(const basic_string
& __str
, size_type __pos
= npos
) const
4977 { return this->find_last_of(__str
.data(), __pos
, __str
.size()); }
4980 * @brief Find last position of a character of C substring.
4981 * @param __s C string containing characters to locate.
4982 * @param __pos Index of character to search back from.
4983 * @param __n Number of characters from s to search for.
4984 * @return Index of last occurrence.
4986 * Starting from @a __pos, searches backward for one of the
4987 * first @a __n characters of @a __s within this string. If
4988 * found, returns the index where it was found. If not found,
4992 find_last_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const;
4995 * @brief Find last position of a character of C string.
4996 * @param __s C string containing characters to locate.
4997 * @param __pos Index of character to search back from (default end).
4998 * @return Index of last occurrence.
5000 * Starting from @a __pos, searches backward for one of the
5001 * characters of @a __s within this string. If found, returns
5002 * the index where it was found. If not found, returns npos.
5005 find_last_of(const _CharT
* __s
, size_type __pos
= npos
) const
5007 __glibcxx_requires_string(__s
);
5008 return this->find_last_of(__s
, __pos
, traits_type::length(__s
));
5012 * @brief Find last position of a character.
5013 * @param __c Character to locate.
5014 * @param __pos Index of character to search back from (default end).
5015 * @return Index of last occurrence.
5017 * Starting from @a __pos, searches backward for @a __c within
5018 * this string. If found, returns the index where it was
5019 * found. If not found, returns npos.
5021 * Note: equivalent to rfind(__c, __pos).
5024 find_last_of(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
5025 { return this->rfind(__c
, __pos
); }
5028 * @brief Find position of a character not in string.
5029 * @param __str String containing characters to avoid.
5030 * @param __pos Index of character to search from (default 0).
5031 * @return Index of first occurrence.
5033 * Starting from @a __pos, searches forward for a character not contained
5034 * in @a __str within this string. If found, returns the index where it
5035 * was found. If not found, returns npos.
5038 find_first_not_of(const basic_string
& __str
, size_type __pos
= 0) const
5040 { return this->find_first_not_of(__str
.data(), __pos
, __str
.size()); }
5043 * @brief Find position of a character not in C substring.
5044 * @param __s C string containing characters to avoid.
5045 * @param __pos Index of character to search from.
5046 * @param __n Number of characters from __s to consider.
5047 * @return Index of first occurrence.
5049 * Starting from @a __pos, searches forward for a character not
5050 * contained in the first @a __n characters of @a __s within
5051 * this string. If found, returns the index where it was
5052 * found. If not found, returns npos.
5055 find_first_not_of(const _CharT
* __s
, size_type __pos
,
5056 size_type __n
) const;
5059 * @brief Find position of a character not in C string.
5060 * @param __s C string containing characters to avoid.
5061 * @param __pos Index of character to search from (default 0).
5062 * @return Index of first occurrence.
5064 * Starting from @a __pos, searches forward for a character not
5065 * contained in @a __s within this string. If found, returns
5066 * the index where it was found. If not found, returns npos.
5069 find_first_not_of(const _CharT
* __s
, size_type __pos
= 0) const
5071 __glibcxx_requires_string(__s
);
5072 return this->find_first_not_of(__s
, __pos
, traits_type::length(__s
));
5076 * @brief Find position of a different character.
5077 * @param __c Character to avoid.
5078 * @param __pos Index of character to search from (default 0).
5079 * @return Index of first occurrence.
5081 * Starting from @a __pos, searches forward for a character
5082 * other than @a __c within this string. If found, returns the
5083 * index where it was found. If not found, returns npos.
5086 find_first_not_of(_CharT __c
, size_type __pos
= 0) const
5090 * @brief Find last position of a character not in string.
5091 * @param __str String containing characters to avoid.
5092 * @param __pos Index of character to search back from (default end).
5093 * @return Index of last occurrence.
5095 * Starting from @a __pos, searches backward for a character
5096 * not contained in @a __str within this string. If found,
5097 * returns the index where it was found. If not found, returns
5101 find_last_not_of(const basic_string
& __str
, size_type __pos
= npos
) const
5103 { return this->find_last_not_of(__str
.data(), __pos
, __str
.size()); }
5106 * @brief Find last position of a character not in C substring.
5107 * @param __s C string containing characters to avoid.
5108 * @param __pos Index of character to search back from.
5109 * @param __n Number of characters from s to consider.
5110 * @return Index of last occurrence.
5112 * Starting from @a __pos, searches backward for a character not
5113 * contained in the first @a __n characters of @a __s within this string.
5114 * If found, returns the index where it was found. If not found,
5118 find_last_not_of(const _CharT
* __s
, size_type __pos
,
5119 size_type __n
) const;
5121 * @brief Find last position of a character not in C string.
5122 * @param __s C string containing characters to avoid.
5123 * @param __pos Index of character to search back from (default end).
5124 * @return Index of last occurrence.
5126 * Starting from @a __pos, searches backward for a character
5127 * not contained in @a __s within this string. If found,
5128 * returns the index where it was found. If not found, returns
5132 find_last_not_of(const _CharT
* __s
, size_type __pos
= npos
) const
5134 __glibcxx_requires_string(__s
);
5135 return this->find_last_not_of(__s
, __pos
, traits_type::length(__s
));
5139 * @brief Find last position of a different character.
5140 * @param __c Character to avoid.
5141 * @param __pos Index of character to search back from (default end).
5142 * @return Index of last occurrence.
5144 * Starting from @a __pos, searches backward for a character other than
5145 * @a __c within this string. If found, returns the index where it was
5146 * found. If not found, returns npos.
5149 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const
5153 * @brief Get a substring.
5154 * @param __pos Index of first character (default 0).
5155 * @param __n Number of characters in substring (default remainder).
5156 * @return The new string.
5157 * @throw std::out_of_range If __pos > size().
5159 * Construct and return a new string using the @a __n
5160 * characters starting at @a __pos. If the string is too
5161 * short, use the remainder of the characters. If @a __pos is
5162 * beyond the end of the string, out_of_range is thrown.
5165 substr(size_type __pos
= 0, size_type __n
= npos
) const
5166 { return basic_string(*this,
5167 _M_check(__pos
, "basic_string::substr"), __n
); }
5170 * @brief Compare to a string.
5171 * @param __str String to compare against.
5172 * @return Integer < 0, 0, or > 0.
5174 * Returns an integer < 0 if this string is ordered before @a
5175 * __str, 0 if their values are equivalent, or > 0 if this
5176 * string is ordered after @a __str. Determines the effective
5177 * length rlen of the strings to compare as the smallest of
5178 * size() and str.size(). The function then compares the two
5179 * strings by calling traits::compare(data(), str.data(),rlen).
5180 * If the result of the comparison is nonzero returns it,
5181 * otherwise the shorter one is ordered first.
5184 compare(const basic_string
& __str
) const
5186 const size_type __size
= this->size();
5187 const size_type __osize
= __str
.size();
5188 const size_type __len
= std::min(__size
, __osize
);
5190 int __r
= traits_type::compare(_M_data(), __str
.data(), __len
);
5192 __r
= _S_compare(__size
, __osize
);
5197 * @brief Compare substring to a string.
5198 * @param __pos Index of first character of substring.
5199 * @param __n Number of characters in substring.
5200 * @param __str String to compare against.
5201 * @return Integer < 0, 0, or > 0.
5203 * Form the substring of this string from the @a __n characters
5204 * starting at @a __pos. Returns an integer < 0 if the
5205 * substring is ordered before @a __str, 0 if their values are
5206 * equivalent, or > 0 if the substring is ordered after @a
5207 * __str. Determines the effective length rlen of the strings
5208 * to compare as the smallest of the length of the substring
5209 * and @a __str.size(). The function then compares the two
5210 * strings by calling
5211 * traits::compare(substring.data(),str.data(),rlen). If the
5212 * result of the comparison is nonzero returns it, otherwise
5213 * the shorter one is ordered first.
5216 compare(size_type __pos
, size_type __n
, const basic_string
& __str
) const;
5219 * @brief Compare substring to a substring.
5220 * @param __pos1 Index of first character of substring.
5221 * @param __n1 Number of characters in substring.
5222 * @param __str String to compare against.
5223 * @param __pos2 Index of first character of substring of str.
5224 * @param __n2 Number of characters in substring of str.
5225 * @return Integer < 0, 0, or > 0.
5227 * Form the substring of this string from the @a __n1
5228 * characters starting at @a __pos1. Form the substring of @a
5229 * __str from the @a __n2 characters starting at @a __pos2.
5230 * Returns an integer < 0 if this substring is ordered before
5231 * the substring of @a __str, 0 if their values are equivalent,
5232 * or > 0 if this substring is ordered after the substring of
5233 * @a __str. Determines the effective length rlen of the
5234 * strings to compare as the smallest of the lengths of the
5235 * substrings. The function then compares the two strings by
5237 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
5238 * If the result of the comparison is nonzero returns it,
5239 * otherwise the shorter one is ordered first.
5242 compare(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
5243 size_type __pos2
, size_type __n2
) const;
5246 * @brief Compare to a C string.
5247 * @param __s C string to compare against.
5248 * @return Integer < 0, 0, or > 0.
5250 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
5251 * their values are equivalent, or > 0 if this string is ordered after
5252 * @a __s. Determines the effective length rlen of the strings to
5253 * compare as the smallest of size() and the length of a string
5254 * constructed from @a __s. The function then compares the two strings
5255 * by calling traits::compare(data(),s,rlen). If the result of the
5256 * comparison is nonzero returns it, otherwise the shorter one is
5260 compare(const _CharT
* __s
) const;
5262 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5263 // 5 String::compare specification questionable
5265 * @brief Compare substring to a C string.
5266 * @param __pos Index of first character of substring.
5267 * @param __n1 Number of characters in substring.
5268 * @param __s C string to compare against.
5269 * @return Integer < 0, 0, or > 0.
5271 * Form the substring of this string from the @a __n1
5272 * characters starting at @a pos. Returns an integer < 0 if
5273 * the substring is ordered before @a __s, 0 if their values
5274 * are equivalent, or > 0 if the substring is ordered after @a
5275 * __s. Determines the effective length rlen of the strings to
5276 * compare as the smallest of the length of the substring and
5277 * the length of a string constructed from @a __s. The
5278 * function then compares the two string by calling
5279 * traits::compare(substring.data(),__s,rlen). If the result of
5280 * the comparison is nonzero returns it, otherwise the shorter
5281 * one is ordered first.
5284 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
) const;
5287 * @brief Compare substring against a character %array.
5288 * @param __pos Index of first character of substring.
5289 * @param __n1 Number of characters in substring.
5290 * @param __s character %array to compare against.
5291 * @param __n2 Number of characters of s.
5292 * @return Integer < 0, 0, or > 0.
5294 * Form the substring of this string from the @a __n1
5295 * characters starting at @a __pos. Form a string from the
5296 * first @a __n2 characters of @a __s. Returns an integer < 0
5297 * if this substring is ordered before the string from @a __s,
5298 * 0 if their values are equivalent, or > 0 if this substring
5299 * is ordered after the string from @a __s. Determines the
5300 * effective length rlen of the strings to compare as the
5301 * smallest of the length of the substring and @a __n2. The
5302 * function then compares the two strings by calling
5303 * traits::compare(substring.data(),s,rlen). If the result of
5304 * the comparison is nonzero returns it, otherwise the shorter
5305 * one is ordered first.
5307 * NB: s must have at least n2 characters, '\\0' has
5308 * no special meaning.
5311 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
,
5312 size_type __n2
) const;
5314 # ifdef _GLIBCXX_TM_TS_INTERNAL
5316 ::_txnal_cow_string_C1_for_exceptions(void* that
, const char* s
,
5319 ::_txnal_cow_string_c_str(const void *that
);
5321 ::_txnal_cow_string_D1(void *that
);
5323 ::_txnal_cow_string_D1_commit(void *that
);
5326 #endif // !_GLIBCXX_USE_CXX11_ABI
5330 * @brief Concatenate two strings.
5331 * @param __lhs First string.
5332 * @param __rhs Last string.
5333 * @return New string with value of @a __lhs followed by @a __rhs.
5335 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5336 basic_string
<_CharT
, _Traits
, _Alloc
>
5337 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5338 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5340 basic_string
<_CharT
, _Traits
, _Alloc
> __str(__lhs
);
5341 __str
.append(__rhs
);
5346 * @brief Concatenate C string and string.
5347 * @param __lhs First string.
5348 * @param __rhs Last string.
5349 * @return New string with value of @a __lhs followed by @a __rhs.
5351 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5352 basic_string
<_CharT
,_Traits
,_Alloc
>
5353 operator+(const _CharT
* __lhs
,
5354 const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
);
5357 * @brief Concatenate character and string.
5358 * @param __lhs First string.
5359 * @param __rhs Last string.
5360 * @return New string with @a __lhs followed by @a __rhs.
5362 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5363 basic_string
<_CharT
,_Traits
,_Alloc
>
5364 operator+(_CharT __lhs
, const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
);
5367 * @brief Concatenate string and C string.
5368 * @param __lhs First string.
5369 * @param __rhs Last string.
5370 * @return New string with @a __lhs followed by @a __rhs.
5372 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5373 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5374 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5375 const _CharT
* __rhs
)
5377 basic_string
<_CharT
, _Traits
, _Alloc
> __str(__lhs
);
5378 __str
.append(__rhs
);
5383 * @brief Concatenate string and character.
5384 * @param __lhs First string.
5385 * @param __rhs Last string.
5386 * @return New string with @a __lhs followed by @a __rhs.
5388 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5389 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5390 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
, _CharT __rhs
)
5392 typedef basic_string
<_CharT
, _Traits
, _Alloc
> __string_type
;
5393 typedef typename
__string_type::size_type __size_type
;
5394 __string_type
__str(__lhs
);
5395 __str
.append(__size_type(1), __rhs
);
5399 #if __cplusplus >= 201103L
5400 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5401 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5402 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
5403 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5404 { return std::move(__lhs
.append(__rhs
)); }
5406 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5407 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5408 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5409 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
5410 { return std::move(__rhs
.insert(0, __lhs
)); }
5412 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5413 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5414 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
5415 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
5417 const auto __size
= __lhs
.size() + __rhs
.size();
5418 const bool __cond
= (__size
> __lhs
.capacity()
5419 && __size
<= __rhs
.capacity());
5420 return __cond
? std::move(__rhs
.insert(0, __lhs
))
5421 : std::move(__lhs
.append(__rhs
));
5424 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5425 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5426 operator+(const _CharT
* __lhs
,
5427 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
5428 { return std::move(__rhs
.insert(0, __lhs
)); }
5430 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5431 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5432 operator+(_CharT __lhs
,
5433 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
5434 { return std::move(__rhs
.insert(0, 1, __lhs
)); }
5436 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5437 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5438 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
5439 const _CharT
* __rhs
)
5440 { return std::move(__lhs
.append(__rhs
)); }
5442 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5443 inline basic_string
<_CharT
, _Traits
, _Alloc
>
5444 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
5446 { return std::move(__lhs
.append(1, __rhs
)); }
5451 * @brief Test equivalence of two strings.
5452 * @param __lhs First string.
5453 * @param __rhs Second string.
5454 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
5456 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5458 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5459 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5461 { return __lhs
.compare(__rhs
) == 0; }
5463 template<typename _CharT
>
5465 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
, bool>::__type
5466 operator==(const basic_string
<_CharT
>& __lhs
,
5467 const basic_string
<_CharT
>& __rhs
) _GLIBCXX_NOEXCEPT
5468 { return (__lhs
.size() == __rhs
.size()
5469 && !std::char_traits
<_CharT
>::compare(__lhs
.data(), __rhs
.data(),
5473 * @brief Test equivalence of C string and string.
5474 * @param __lhs C string.
5475 * @param __rhs String.
5476 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
5478 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5480 operator==(const _CharT
* __lhs
,
5481 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5482 { return __rhs
.compare(__lhs
) == 0; }
5485 * @brief Test equivalence of string and C string.
5486 * @param __lhs String.
5487 * @param __rhs C string.
5488 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
5490 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5492 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5493 const _CharT
* __rhs
)
5494 { return __lhs
.compare(__rhs
) == 0; }
5498 * @brief Test difference of two strings.
5499 * @param __lhs First string.
5500 * @param __rhs Second string.
5501 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
5503 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5505 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5506 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5508 { return !(__lhs
== __rhs
); }
5511 * @brief Test difference of C string and string.
5512 * @param __lhs C string.
5513 * @param __rhs String.
5514 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
5516 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5518 operator!=(const _CharT
* __lhs
,
5519 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5520 { return !(__lhs
== __rhs
); }
5523 * @brief Test difference of string and C string.
5524 * @param __lhs String.
5525 * @param __rhs C string.
5526 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
5528 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5530 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5531 const _CharT
* __rhs
)
5532 { return !(__lhs
== __rhs
); }
5536 * @brief Test if string precedes string.
5537 * @param __lhs First string.
5538 * @param __rhs Second string.
5539 * @return True if @a __lhs precedes @a __rhs. False otherwise.
5541 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5543 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5544 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5546 { return __lhs
.compare(__rhs
) < 0; }
5549 * @brief Test if string precedes C string.
5550 * @param __lhs String.
5551 * @param __rhs C string.
5552 * @return True if @a __lhs precedes @a __rhs. False otherwise.
5554 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5556 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5557 const _CharT
* __rhs
)
5558 { return __lhs
.compare(__rhs
) < 0; }
5561 * @brief Test if C string precedes string.
5562 * @param __lhs C string.
5563 * @param __rhs String.
5564 * @return True if @a __lhs precedes @a __rhs. False otherwise.
5566 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5568 operator<(const _CharT
* __lhs
,
5569 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5570 { return __rhs
.compare(__lhs
) > 0; }
5574 * @brief Test if string follows string.
5575 * @param __lhs First string.
5576 * @param __rhs Second string.
5577 * @return True if @a __lhs follows @a __rhs. False otherwise.
5579 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5581 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5582 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5584 { return __lhs
.compare(__rhs
) > 0; }
5587 * @brief Test if string follows C string.
5588 * @param __lhs String.
5589 * @param __rhs C string.
5590 * @return True if @a __lhs follows @a __rhs. False otherwise.
5592 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5594 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5595 const _CharT
* __rhs
)
5596 { return __lhs
.compare(__rhs
) > 0; }
5599 * @brief Test if C string follows string.
5600 * @param __lhs C string.
5601 * @param __rhs String.
5602 * @return True if @a __lhs follows @a __rhs. False otherwise.
5604 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5606 operator>(const _CharT
* __lhs
,
5607 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5608 { return __rhs
.compare(__lhs
) < 0; }
5612 * @brief Test if string doesn't follow string.
5613 * @param __lhs First string.
5614 * @param __rhs Second string.
5615 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5617 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5619 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5620 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5622 { return __lhs
.compare(__rhs
) <= 0; }
5625 * @brief Test if string doesn't follow C string.
5626 * @param __lhs String.
5627 * @param __rhs C string.
5628 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5630 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5632 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5633 const _CharT
* __rhs
)
5634 { return __lhs
.compare(__rhs
) <= 0; }
5637 * @brief Test if C string doesn't follow string.
5638 * @param __lhs C string.
5639 * @param __rhs String.
5640 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
5642 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5644 operator<=(const _CharT
* __lhs
,
5645 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5646 { return __rhs
.compare(__lhs
) >= 0; }
5650 * @brief Test if string doesn't precede string.
5651 * @param __lhs First string.
5652 * @param __rhs Second string.
5653 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
5655 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5657 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5658 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5660 { return __lhs
.compare(__rhs
) >= 0; }
5663 * @brief Test if string doesn't precede C string.
5664 * @param __lhs String.
5665 * @param __rhs C string.
5666 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
5668 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5670 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5671 const _CharT
* __rhs
)
5672 { return __lhs
.compare(__rhs
) >= 0; }
5675 * @brief Test if C string doesn't precede string.
5676 * @param __lhs C string.
5677 * @param __rhs String.
5678 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
5680 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5682 operator>=(const _CharT
* __lhs
,
5683 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5684 { return __rhs
.compare(__lhs
) <= 0; }
5687 * @brief Swap contents of two strings.
5688 * @param __lhs First string.
5689 * @param __rhs Second string.
5691 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
5693 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5695 swap(basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
5696 basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
5697 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs
.swap(__rhs
)))
5698 { __lhs
.swap(__rhs
); }
5702 * @brief Read stream into a string.
5703 * @param __is Input stream.
5704 * @param __str Buffer to store into.
5705 * @return Reference to the input stream.
5707 * Stores characters from @a __is into @a __str until whitespace is
5708 * found, the end of the stream is encountered, or str.max_size()
5709 * is reached. If is.width() is non-zero, that is the limit on the
5710 * number of characters stored into @a __str. Any previous
5711 * contents of @a __str are erased.
5713 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5714 basic_istream
<_CharT
, _Traits
>&
5715 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
5716 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
);
5719 basic_istream
<char>&
5720 operator>>(basic_istream
<char>& __is
, basic_string
<char>& __str
);
5723 * @brief Write string to a stream.
5724 * @param __os Output stream.
5725 * @param __str String to write out.
5726 * @return Reference to the output stream.
5728 * Output characters of @a __str into os following the same rules as for
5729 * writing a C string.
5731 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5732 inline basic_ostream
<_CharT
, _Traits
>&
5733 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
5734 const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
5736 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5737 // 586. string inserter not a formatted function
5738 return __ostream_insert(__os
, __str
.data(), __str
.size());
5742 * @brief Read a line from stream into a string.
5743 * @param __is Input stream.
5744 * @param __str Buffer to store into.
5745 * @param __delim Character marking end of line.
5746 * @return Reference to the input stream.
5748 * Stores characters from @a __is into @a __str until @a __delim is
5749 * found, the end of the stream is encountered, or str.max_size()
5750 * is reached. Any previous contents of @a __str are erased. If
5751 * @a __delim is encountered, it is extracted but not stored into
5754 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5755 basic_istream
<_CharT
, _Traits
>&
5756 getline(basic_istream
<_CharT
, _Traits
>& __is
,
5757 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
);
5760 * @brief Read a line from stream into a string.
5761 * @param __is Input stream.
5762 * @param __str Buffer to store into.
5763 * @return Reference to the input stream.
5765 * Stores characters from is into @a __str until '\n' is
5766 * found, the end of the stream is encountered, or str.max_size()
5767 * is reached. Any previous contents of @a __str are erased. If
5768 * end of line is encountered, it is extracted but not stored into
5771 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5772 inline basic_istream
<_CharT
, _Traits
>&
5773 getline(basic_istream
<_CharT
, _Traits
>& __is
,
5774 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
5775 { return std::getline(__is
, __str
, __is
.widen('\n')); }
5777 #if __cplusplus >= 201103L
5778 /// Read a line from an rvalue stream into a string.
5779 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5780 inline basic_istream
<_CharT
, _Traits
>&
5781 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
5782 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
)
5783 { return std::getline(__is
, __str
, __delim
); }
5785 /// Read a line from an rvalue stream into a string.
5786 template<typename _CharT
, typename _Traits
, typename _Alloc
>
5787 inline basic_istream
<_CharT
, _Traits
>&
5788 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
5789 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
5790 { return std::getline(__is
, __str
); }
5794 basic_istream
<char>&
5795 getline(basic_istream
<char>& __in
, basic_string
<char>& __str
,
5798 #ifdef _GLIBCXX_USE_WCHAR_T
5800 basic_istream
<wchar_t>&
5801 getline(basic_istream
<wchar_t>& __in
, basic_string
<wchar_t>& __str
,
5805 _GLIBCXX_END_NAMESPACE_VERSION
5808 #if __cplusplus >= 201103L
5810 #include <ext/string_conversions.h>
5812 namespace std
_GLIBCXX_VISIBILITY(default)
5814 _GLIBCXX_BEGIN_NAMESPACE_VERSION
5815 _GLIBCXX_BEGIN_NAMESPACE_CXX11
5817 #if _GLIBCXX_USE_C99_STDLIB
5818 // 21.4 Numeric Conversions [string.conversions].
5820 stoi(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
5821 { return __gnu_cxx::__stoa
<long, int>(&std::strtol
, "stoi", __str
.c_str(),
5825 stol(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
5826 { return __gnu_cxx::__stoa(&std::strtol
, "stol", __str
.c_str(),
5829 inline unsigned long
5830 stoul(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
5831 { return __gnu_cxx::__stoa(&std::strtoul
, "stoul", __str
.c_str(),
5835 stoll(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
5836 { return __gnu_cxx::__stoa(&std::strtoll
, "stoll", __str
.c_str(),
5839 inline unsigned long long
5840 stoull(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
5841 { return __gnu_cxx::__stoa(&std::strtoull
, "stoull", __str
.c_str(),
5844 // NB: strtof vs strtod.
5846 stof(const string
& __str
, size_t* __idx
= 0)
5847 { return __gnu_cxx::__stoa(&std::strtof
, "stof", __str
.c_str(), __idx
); }
5850 stod(const string
& __str
, size_t* __idx
= 0)
5851 { return __gnu_cxx::__stoa(&std::strtod
, "stod", __str
.c_str(), __idx
); }
5854 stold(const string
& __str
, size_t* __idx
= 0)
5855 { return __gnu_cxx::__stoa(&std::strtold
, "stold", __str
.c_str(), __idx
); }
5856 #endif // _GLIBCXX_USE_C99_STDLIB
5858 #if _GLIBCXX_USE_C99_STDIO
5859 // NB: (v)snprintf vs sprintf.
5863 to_string(int __val
)
5864 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, 4 * sizeof(int),
5868 to_string(unsigned __val
)
5869 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
5870 4 * sizeof(unsigned),
5874 to_string(long __val
)
5875 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, 4 * sizeof(long),
5879 to_string(unsigned long __val
)
5880 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
5881 4 * sizeof(unsigned long),
5885 to_string(long long __val
)
5886 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
5887 4 * sizeof(long long),
5891 to_string(unsigned long long __val
)
5892 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
5893 4 * sizeof(unsigned long long),
5897 to_string(float __val
)
5900 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
5901 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
5906 to_string(double __val
)
5909 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
5910 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
5915 to_string(long double __val
)
5918 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
5919 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
5922 #endif // _GLIBCXX_USE_C99_STDIO
5924 #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
5926 stoi(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
5927 { return __gnu_cxx::__stoa
<long, int>(&std::wcstol
, "stoi", __str
.c_str(),
5931 stol(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
5932 { return __gnu_cxx::__stoa(&std::wcstol
, "stol", __str
.c_str(),
5935 inline unsigned long
5936 stoul(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
5937 { return __gnu_cxx::__stoa(&std::wcstoul
, "stoul", __str
.c_str(),
5941 stoll(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
5942 { return __gnu_cxx::__stoa(&std::wcstoll
, "stoll", __str
.c_str(),
5945 inline unsigned long long
5946 stoull(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
5947 { return __gnu_cxx::__stoa(&std::wcstoull
, "stoull", __str
.c_str(),
5950 // NB: wcstof vs wcstod.
5952 stof(const wstring
& __str
, size_t* __idx
= 0)
5953 { return __gnu_cxx::__stoa(&std::wcstof
, "stof", __str
.c_str(), __idx
); }
5956 stod(const wstring
& __str
, size_t* __idx
= 0)
5957 { return __gnu_cxx::__stoa(&std::wcstod
, "stod", __str
.c_str(), __idx
); }
5960 stold(const wstring
& __str
, size_t* __idx
= 0)
5961 { return __gnu_cxx::__stoa(&std::wcstold
, "stold", __str
.c_str(), __idx
); }
5963 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
5966 to_wstring(int __val
)
5967 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(int),
5971 to_wstring(unsigned __val
)
5972 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
5973 4 * sizeof(unsigned),
5977 to_wstring(long __val
)
5978 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(long),
5982 to_wstring(unsigned long __val
)
5983 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
5984 4 * sizeof(unsigned long),
5988 to_wstring(long long __val
)
5989 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
5990 4 * sizeof(long long),
5994 to_wstring(unsigned long long __val
)
5995 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
5996 4 * sizeof(unsigned long long),
6000 to_wstring(float __val
)
6003 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
6004 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
6009 to_wstring(double __val
)
6012 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
6013 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
6018 to_wstring(long double __val
)
6021 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
6022 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
6025 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6026 #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
6028 _GLIBCXX_END_NAMESPACE_CXX11
6029 _GLIBCXX_END_NAMESPACE_VERSION
6034 #if __cplusplus >= 201103L
6036 #include <bits/functional_hash.h>
6038 namespace std
_GLIBCXX_VISIBILITY(default)
6040 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6044 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
6045 /// std::hash specialization for string.
6048 : public __hash_base
<size_t, string
>
6051 operator()(const string
& __s
) const noexcept
6052 { return std::_Hash_impl::hash(__s
.data(), __s
.length()); }
6056 struct __is_fast_hash
<hash
<string
>> : std::false_type
6059 #ifdef _GLIBCXX_USE_WCHAR_T
6060 /// std::hash specialization for wstring.
6062 struct hash
<wstring
>
6063 : public __hash_base
<size_t, wstring
>
6066 operator()(const wstring
& __s
) const noexcept
6067 { return std::_Hash_impl::hash(__s
.data(),
6068 __s
.length() * sizeof(wchar_t)); }
6072 struct __is_fast_hash
<hash
<wstring
>> : std::false_type
6075 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
6077 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
6078 /// std::hash specialization for u16string.
6080 struct hash
<u16string
>
6081 : public __hash_base
<size_t, u16string
>
6084 operator()(const u16string
& __s
) const noexcept
6085 { return std::_Hash_impl::hash(__s
.data(),
6086 __s
.length() * sizeof(char16_t
)); }
6090 struct __is_fast_hash
<hash
<u16string
>> : std::false_type
6093 /// std::hash specialization for u32string.
6095 struct hash
<u32string
>
6096 : public __hash_base
<size_t, u32string
>
6099 operator()(const u32string
& __s
) const noexcept
6100 { return std::_Hash_impl::hash(__s
.data(),
6101 __s
.length() * sizeof(char32_t
)); }
6105 struct __is_fast_hash
<hash
<u32string
>> : std::false_type
6109 _GLIBCXX_END_NAMESPACE_VERSION
6111 #if __cplusplus > 201103L
6113 #define __cpp_lib_string_udls 201304
6115 inline namespace literals
6117 inline namespace string_literals
6119 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6121 _GLIBCXX_DEFAULT_ABI_TAG
6122 inline basic_string
<char>
6123 operator""s(const char* __str
, size_t __len
)
6124 { return basic_string
<char>{__str
, __len
}; }
6126 #ifdef _GLIBCXX_USE_WCHAR_T
6127 _GLIBCXX_DEFAULT_ABI_TAG
6128 inline basic_string
<wchar_t>
6129 operator""s(const wchar_t* __str
, size_t __len
)
6130 { return basic_string
<wchar_t>{__str
, __len
}; }
6133 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
6134 _GLIBCXX_DEFAULT_ABI_TAG
6135 inline basic_string
<char16_t
>
6136 operator""s(const char16_t
* __str
, size_t __len
)
6137 { return basic_string
<char16_t
>{__str
, __len
}; }
6139 _GLIBCXX_DEFAULT_ABI_TAG
6140 inline basic_string
<char32_t
>
6141 operator""s(const char32_t
* __str
, size_t __len
)
6142 { return basic_string
<char32_t
>{__str
, __len
}; }
6145 _GLIBCXX_END_NAMESPACE_VERSION
6146 } // inline namespace string_literals
6147 } // inline namespace literals
6149 #endif // __cplusplus > 201103L
6155 #endif /* _BASIC_STRING_H */