1 // Components for manipulating sequences of characters -*- C++ -*-
3 // Copyright (C) 1997-2019 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 >= 201703L
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
;
112 #if __cplusplus >= 201703L
113 // A helper type for avoiding boiler-plate.
114 typedef basic_string_view
<_CharT
, _Traits
> __sv_type
;
116 template<typename _Tp
, typename _Res
>
117 using _If_sv
= enable_if_t
<
118 __and_
<is_convertible
<const _Tp
&, __sv_type
>,
119 __not_
<is_convertible
<const _Tp
*, const basic_string
*>>,
120 __not_
<is_convertible
<const _Tp
&, const _CharT
*>>>::value
,
123 // Allows an implicit conversion to __sv_type.
125 _S_to_string_view(__sv_type __svt
) noexcept
128 // Wraps a string_view by explicit conversion and thus
129 // allows to add an internal constructor that does not
130 // participate in overload resolution when a string_view
134 explicit __sv_wrapper(__sv_type __sv
) noexcept
: _M_sv(__sv
) { }
139 * @brief Only internally used: Construct string from a string view
141 * @param __svw string view wrapper.
142 * @param __a Allocator to use.
145 basic_string(__sv_wrapper __svw
, const _Alloc
& __a
)
146 : basic_string(__svw
._M_sv
.data(), __svw
._M_sv
.size(), __a
) { }
149 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
150 struct _Alloc_hider
: allocator_type
// TODO check __is_final
152 #if __cplusplus < 201103L
153 _Alloc_hider(pointer __dat
, const _Alloc
& __a
= _Alloc())
154 : allocator_type(__a
), _M_p(__dat
) { }
156 _Alloc_hider(pointer __dat
, const _Alloc
& __a
)
157 : allocator_type(__a
), _M_p(__dat
) { }
159 _Alloc_hider(pointer __dat
, _Alloc
&& __a
= _Alloc())
160 : allocator_type(std::move(__a
)), _M_p(__dat
) { }
163 pointer _M_p
; // The actual data.
166 _Alloc_hider _M_dataplus
;
167 size_type _M_string_length
;
169 enum { _S_local_capacity
= 15 / sizeof(_CharT
) };
173 _CharT _M_local_buf
[_S_local_capacity
+ 1];
174 size_type _M_allocated_capacity
;
179 { _M_dataplus
._M_p
= __p
; }
182 _M_length(size_type __length
)
183 { _M_string_length
= __length
; }
187 { return _M_dataplus
._M_p
; }
192 #if __cplusplus >= 201103L
193 return std::pointer_traits
<pointer
>::pointer_to(*_M_local_buf
);
195 return pointer(_M_local_buf
);
200 _M_local_data() const
202 #if __cplusplus >= 201103L
203 return std::pointer_traits
<const_pointer
>::pointer_to(*_M_local_buf
);
205 return const_pointer(_M_local_buf
);
210 _M_capacity(size_type __capacity
)
211 { _M_allocated_capacity
= __capacity
; }
214 _M_set_length(size_type __n
)
217 traits_type::assign(_M_data()[__n
], _CharT());
222 { return _M_data() == _M_local_data(); }
226 _M_create(size_type
&, size_type
);
232 _M_destroy(_M_allocated_capacity
);
236 _M_destroy(size_type __size
) throw()
237 { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size
+ 1); }
239 // _M_construct_aux is used to implement the 21.3.1 para 15 which
240 // requires special behaviour if _InIterator is an integral type
241 template<typename _InIterator
>
243 _M_construct_aux(_InIterator __beg
, _InIterator __end
,
246 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
247 _M_construct(__beg
, __end
, _Tag());
250 // _GLIBCXX_RESOLVE_LIB_DEFECTS
251 // 438. Ambiguity in the "do the right thing" clause
252 template<typename _Integer
>
254 _M_construct_aux(_Integer __beg
, _Integer __end
, std::__true_type
)
255 { _M_construct_aux_2(static_cast<size_type
>(__beg
), __end
); }
258 _M_construct_aux_2(size_type __req
, _CharT __c
)
259 { _M_construct(__req
, __c
); }
261 template<typename _InIterator
>
263 _M_construct(_InIterator __beg
, _InIterator __end
)
265 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
266 _M_construct_aux(__beg
, __end
, _Integral());
269 // For Input Iterators, used in istreambuf_iterators, etc.
270 template<typename _InIterator
>
272 _M_construct(_InIterator __beg
, _InIterator __end
,
273 std::input_iterator_tag
);
275 // For forward_iterators up to random_access_iterators, used for
276 // string::iterator, _CharT*, etc.
277 template<typename _FwdIterator
>
279 _M_construct(_FwdIterator __beg
, _FwdIterator __end
,
280 std::forward_iterator_tag
);
283 _M_construct(size_type __req
, _CharT __c
);
287 { return _M_dataplus
; }
289 const allocator_type
&
290 _M_get_allocator() const
291 { return _M_dataplus
; }
295 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
296 // The explicit instantiations in misc-inst.cc require this due to
297 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
298 template<typename _Tp
, bool _Requires
=
299 !__are_same
<_Tp
, _CharT
*>::__value
300 && !__are_same
<_Tp
, const _CharT
*>::__value
301 && !__are_same
<_Tp
, iterator
>::__value
302 && !__are_same
<_Tp
, const_iterator
>::__value
>
303 struct __enable_if_not_native_iterator
304 { typedef basic_string
& __type
; };
305 template<typename _Tp
>
306 struct __enable_if_not_native_iterator
<_Tp
, false> { };
310 _M_check(size_type __pos
, const char* __s
) const
312 if (__pos
> this->size())
313 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
314 "this->size() (which is %zu)"),
315 __s
, __pos
, this->size());
320 _M_check_length(size_type __n1
, size_type __n2
, const char* __s
) const
322 if (this->max_size() - (this->size() - __n1
) < __n2
)
323 __throw_length_error(__N(__s
));
327 // NB: _M_limit doesn't check for a bad __pos value.
329 _M_limit(size_type __pos
, size_type __off
) const _GLIBCXX_NOEXCEPT
331 const bool __testoff
= __off
< this->size() - __pos
;
332 return __testoff
? __off
: this->size() - __pos
;
335 // True if _Rep and source do not overlap.
337 _M_disjunct(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
339 return (less
<const _CharT
*>()(__s
, _M_data())
340 || less
<const _CharT
*>()(_M_data() + this->size(), __s
));
343 // When __n = 1 way faster than the general multichar
344 // traits_type::copy/move/assign.
346 _S_copy(_CharT
* __d
, const _CharT
* __s
, size_type __n
)
349 traits_type::assign(*__d
, *__s
);
351 traits_type::copy(__d
, __s
, __n
);
355 _S_move(_CharT
* __d
, const _CharT
* __s
, size_type __n
)
358 traits_type::assign(*__d
, *__s
);
360 traits_type::move(__d
, __s
, __n
);
364 _S_assign(_CharT
* __d
, size_type __n
, _CharT __c
)
367 traits_type::assign(*__d
, __c
);
369 traits_type::assign(__d
, __n
, __c
);
372 // _S_copy_chars is a separate template to permit specialization
373 // to optimize for the common case of pointers as iterators.
374 template<class _Iterator
>
376 _S_copy_chars(_CharT
* __p
, _Iterator __k1
, _Iterator __k2
)
378 for (; __k1
!= __k2
; ++__k1
, (void)++__p
)
379 traits_type::assign(*__p
, *__k1
); // These types are off.
383 _S_copy_chars(_CharT
* __p
, iterator __k1
, iterator __k2
) _GLIBCXX_NOEXCEPT
384 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
387 _S_copy_chars(_CharT
* __p
, const_iterator __k1
, const_iterator __k2
)
389 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
392 _S_copy_chars(_CharT
* __p
, _CharT
* __k1
, _CharT
* __k2
) _GLIBCXX_NOEXCEPT
393 { _S_copy(__p
, __k1
, __k2
- __k1
); }
396 _S_copy_chars(_CharT
* __p
, const _CharT
* __k1
, const _CharT
* __k2
)
398 { _S_copy(__p
, __k1
, __k2
- __k1
); }
401 _S_compare(size_type __n1
, size_type __n2
) _GLIBCXX_NOEXCEPT
403 const difference_type __d
= difference_type(__n1
- __n2
);
405 if (__d
> __gnu_cxx::__numeric_traits
<int>::__max
)
406 return __gnu_cxx::__numeric_traits
<int>::__max
;
407 else if (__d
< __gnu_cxx::__numeric_traits
<int>::__min
)
408 return __gnu_cxx::__numeric_traits
<int>::__min
;
414 _M_assign(const basic_string
&);
417 _M_mutate(size_type __pos
, size_type __len1
, const _CharT
* __s
,
421 _M_erase(size_type __pos
, size_type __n
);
424 // Construct/copy/destroy:
425 // NB: We overload ctors in some cases instead of using default
426 // arguments, per 17.4.4.4 para. 2 item 2.
429 * @brief Default constructor creates an empty string.
432 _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible
<_Alloc
>::value
)
433 : _M_dataplus(_M_local_data())
434 { _M_set_length(0); }
437 * @brief Construct an empty string using allocator @a a.
440 basic_string(const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
441 : _M_dataplus(_M_local_data(), __a
)
442 { _M_set_length(0); }
445 * @brief Construct string with copy of value of @a __str.
446 * @param __str Source string.
448 basic_string(const basic_string
& __str
)
449 : _M_dataplus(_M_local_data(),
450 _Alloc_traits::_S_select_on_copy(__str
._M_get_allocator()))
451 { _M_construct(__str
._M_data(), __str
._M_data() + __str
.length()); }
453 // _GLIBCXX_RESOLVE_LIB_DEFECTS
454 // 2583. no way to supply an allocator for basic_string(str, pos)
456 * @brief Construct string as copy of a substring.
457 * @param __str Source string.
458 * @param __pos Index of first character to copy from.
459 * @param __a Allocator to use.
461 basic_string(const basic_string
& __str
, size_type __pos
,
462 const _Alloc
& __a
= _Alloc())
463 : _M_dataplus(_M_local_data(), __a
)
465 const _CharT
* __start
= __str
._M_data()
466 + __str
._M_check(__pos
, "basic_string::basic_string");
467 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, npos
));
471 * @brief Construct string as copy of a substring.
472 * @param __str Source string.
473 * @param __pos Index of first character to copy from.
474 * @param __n Number of characters to copy.
476 basic_string(const basic_string
& __str
, size_type __pos
,
478 : _M_dataplus(_M_local_data())
480 const _CharT
* __start
= __str
._M_data()
481 + __str
._M_check(__pos
, "basic_string::basic_string");
482 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, __n
));
486 * @brief Construct string as copy of a substring.
487 * @param __str Source string.
488 * @param __pos Index of first character to copy from.
489 * @param __n Number of characters to copy.
490 * @param __a Allocator to use.
492 basic_string(const basic_string
& __str
, size_type __pos
,
493 size_type __n
, const _Alloc
& __a
)
494 : _M_dataplus(_M_local_data(), __a
)
496 const _CharT
* __start
497 = __str
._M_data() + __str
._M_check(__pos
, "string::string");
498 _M_construct(__start
, __start
+ __str
._M_limit(__pos
, __n
));
502 * @brief Construct string initialized by a character %array.
503 * @param __s Source character %array.
504 * @param __n Number of characters to copy.
505 * @param __a Allocator to use (default is default allocator).
507 * NB: @a __s must have at least @a __n characters, '\\0'
508 * has no special meaning.
510 basic_string(const _CharT
* __s
, size_type __n
,
511 const _Alloc
& __a
= _Alloc())
512 : _M_dataplus(_M_local_data(), __a
)
513 { _M_construct(__s
, __s
+ __n
); }
516 * @brief Construct string as copy of a C string.
517 * @param __s Source C string.
518 * @param __a Allocator to use (default is default allocator).
520 #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
521 // _GLIBCXX_RESOLVE_LIB_DEFECTS
522 // 3076. basic_string CTAD ambiguity
523 template<typename
= _RequireAllocator
<_Alloc
>>
525 basic_string(const _CharT
* __s
, const _Alloc
& __a
= _Alloc())
526 : _M_dataplus(_M_local_data(), __a
)
527 { _M_construct(__s
, __s
? __s
+ traits_type::length(__s
) : __s
+npos
); }
530 * @brief Construct string as multiple characters.
531 * @param __n Number of characters.
532 * @param __c Character to use.
533 * @param __a Allocator to use (default is default allocator).
535 #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
536 // _GLIBCXX_RESOLVE_LIB_DEFECTS
537 // 3076. basic_string CTAD ambiguity
538 template<typename
= _RequireAllocator
<_Alloc
>>
540 basic_string(size_type __n
, _CharT __c
, const _Alloc
& __a
= _Alloc())
541 : _M_dataplus(_M_local_data(), __a
)
542 { _M_construct(__n
, __c
); }
544 #if __cplusplus >= 201103L
546 * @brief Move construct string.
547 * @param __str Source string.
549 * The newly-created string contains the exact contents of @a __str.
550 * @a __str is a valid, but unspecified string.
552 basic_string(basic_string
&& __str
) noexcept
553 : _M_dataplus(_M_local_data(), std::move(__str
._M_get_allocator()))
555 if (__str
._M_is_local())
557 traits_type::copy(_M_local_buf
, __str
._M_local_buf
,
558 _S_local_capacity
+ 1);
562 _M_data(__str
._M_data());
563 _M_capacity(__str
._M_allocated_capacity
);
566 // Must use _M_length() here not _M_set_length() because
567 // basic_stringbuf relies on writing into unallocated capacity so
568 // we mess up the contents if we put a '\0' in the string.
569 _M_length(__str
.length());
570 __str
._M_data(__str
._M_local_data());
571 __str
._M_set_length(0);
575 * @brief Construct string from an initializer %list.
576 * @param __l std::initializer_list of characters.
577 * @param __a Allocator to use (default is default allocator).
579 basic_string(initializer_list
<_CharT
> __l
, const _Alloc
& __a
= _Alloc())
580 : _M_dataplus(_M_local_data(), __a
)
581 { _M_construct(__l
.begin(), __l
.end()); }
583 basic_string(const basic_string
& __str
, const _Alloc
& __a
)
584 : _M_dataplus(_M_local_data(), __a
)
585 { _M_construct(__str
.begin(), __str
.end()); }
587 basic_string(basic_string
&& __str
, const _Alloc
& __a
)
588 noexcept(_Alloc_traits::_S_always_equal())
589 : _M_dataplus(_M_local_data(), __a
)
591 if (__str
._M_is_local())
593 traits_type::copy(_M_local_buf
, __str
._M_local_buf
,
594 _S_local_capacity
+ 1);
595 _M_length(__str
.length());
596 __str
._M_set_length(0);
598 else if (_Alloc_traits::_S_always_equal()
599 || __str
.get_allocator() == __a
)
601 _M_data(__str
._M_data());
602 _M_length(__str
.length());
603 _M_capacity(__str
._M_allocated_capacity
);
604 __str
._M_data(__str
._M_local_buf
);
605 __str
._M_set_length(0);
608 _M_construct(__str
.begin(), __str
.end());
614 * @brief Construct string as copy of a range.
615 * @param __beg Start of range.
616 * @param __end End of range.
617 * @param __a Allocator to use (default is default allocator).
619 #if __cplusplus >= 201103L
620 template<typename _InputIterator
,
621 typename
= std::_RequireInputIter
<_InputIterator
>>
623 template<typename _InputIterator
>
625 basic_string(_InputIterator __beg
, _InputIterator __end
,
626 const _Alloc
& __a
= _Alloc())
627 : _M_dataplus(_M_local_data(), __a
)
628 { _M_construct(__beg
, __end
); }
630 #if __cplusplus >= 201703L
632 * @brief Construct string from a substring of a string_view.
633 * @param __t Source object convertible to string view.
634 * @param __pos The index of the first character to copy from __t.
635 * @param __n The number of characters to copy from __t.
636 * @param __a Allocator to use.
638 template<typename _Tp
, typename
= _If_sv
<_Tp
, void>>
639 basic_string(const _Tp
& __t
, size_type __pos
, size_type __n
,
640 const _Alloc
& __a
= _Alloc())
641 : basic_string(_S_to_string_view(__t
).substr(__pos
, __n
), __a
) { }
644 * @brief Construct string from a string_view.
645 * @param __t Source object convertible to string view.
646 * @param __a Allocator to use (default is default allocator).
648 template<typename _Tp
, typename
= _If_sv
<_Tp
, void>>
650 basic_string(const _Tp
& __t
, const _Alloc
& __a
= _Alloc())
651 : basic_string(__sv_wrapper(_S_to_string_view(__t
)), __a
) { }
655 * @brief Destroy the string instance.
661 * @brief Assign the value of @a str to this string.
662 * @param __str Source string.
665 operator=(const basic_string
& __str
)
667 #if __cplusplus >= 201103L
668 if (_Alloc_traits::_S_propagate_on_copy_assign())
670 if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
671 && _M_get_allocator() != __str
._M_get_allocator())
673 // Propagating allocator cannot free existing storage so must
674 // deallocate it before replacing current allocator.
675 if (__str
.size() <= _S_local_capacity
)
677 _M_destroy(_M_allocated_capacity
);
678 _M_data(_M_local_data());
683 const auto __len
= __str
.size();
684 auto __alloc
= __str
._M_get_allocator();
685 // If this allocation throws there are no effects:
686 auto __ptr
= _Alloc_traits::allocate(__alloc
, __len
+ 1);
687 _M_destroy(_M_allocated_capacity
);
690 _M_set_length(__len
);
693 std::__alloc_on_copy(_M_get_allocator(), __str
._M_get_allocator());
696 return this->assign(__str
);
700 * @brief Copy contents of @a s into this string.
701 * @param __s Source null-terminated string.
704 operator=(const _CharT
* __s
)
705 { return this->assign(__s
); }
708 * @brief Set value to string of length 1.
709 * @param __c Source character.
711 * Assigning to a character makes this string length 1 and
712 * (*this)[0] == @a c.
715 operator=(_CharT __c
)
717 this->assign(1, __c
);
721 #if __cplusplus >= 201103L
723 * @brief Move assign the value of @a str to this string.
724 * @param __str Source string.
726 * The contents of @a str are moved into this string (without copying).
727 * @a str is a valid, but unspecified string.
729 // _GLIBCXX_RESOLVE_LIB_DEFECTS
730 // 2063. Contradictory requirements for string move assignment
732 operator=(basic_string
&& __str
)
733 noexcept(_Alloc_traits::_S_nothrow_move())
735 if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
736 && !_Alloc_traits::_S_always_equal()
737 && _M_get_allocator() != __str
._M_get_allocator())
739 // Destroy existing storage before replacing allocator.
740 _M_destroy(_M_allocated_capacity
);
741 _M_data(_M_local_data());
744 // Replace allocator if POCMA is true.
745 std::__alloc_on_move(_M_get_allocator(), __str
._M_get_allocator());
747 if (__str
._M_is_local())
749 // We've always got room for a short string, just copy it.
751 this->_S_copy(_M_data(), __str
._M_data(), __str
.size());
752 _M_set_length(__str
.size());
754 else if (_Alloc_traits::_S_propagate_on_move_assign()
755 || _Alloc_traits::_S_always_equal()
756 || _M_get_allocator() == __str
._M_get_allocator())
758 // Just move the allocated pointer, our allocator can free it.
759 pointer __data
= nullptr;
760 size_type __capacity
;
763 if (_Alloc_traits::_S_always_equal())
765 // __str can reuse our existing storage.
767 __capacity
= _M_allocated_capacity
;
769 else // __str can't use it, so free it.
770 _M_destroy(_M_allocated_capacity
);
773 _M_data(__str
._M_data());
774 _M_length(__str
.length());
775 _M_capacity(__str
._M_allocated_capacity
);
778 __str
._M_data(__data
);
779 __str
._M_capacity(__capacity
);
782 __str
._M_data(__str
._M_local_buf
);
784 else // Need to do a deep copy
791 * @brief Set value to string constructed from initializer %list.
792 * @param __l std::initializer_list.
795 operator=(initializer_list
<_CharT
> __l
)
797 this->assign(__l
.begin(), __l
.size());
802 #if __cplusplus >= 201703L
804 * @brief Set value to string constructed from a string_view.
805 * @param __svt An object convertible to string_view.
807 template<typename _Tp
>
808 _If_sv
<_Tp
, basic_string
&>
809 operator=(const _Tp
& __svt
)
810 { return this->assign(__svt
); }
813 * @brief Convert to a string_view.
814 * @return A string_view.
816 operator __sv_type() const noexcept
817 { return __sv_type(data(), size()); }
822 * Returns a read/write iterator that points to the first character in
826 begin() _GLIBCXX_NOEXCEPT
827 { return iterator(_M_data()); }
830 * Returns a read-only (constant) iterator that points to the first
831 * character in the %string.
834 begin() const _GLIBCXX_NOEXCEPT
835 { return const_iterator(_M_data()); }
838 * Returns a read/write iterator that points one past the last
839 * character in the %string.
842 end() _GLIBCXX_NOEXCEPT
843 { return iterator(_M_data() + this->size()); }
846 * Returns a read-only (constant) iterator that points one past the
847 * last character in the %string.
850 end() const _GLIBCXX_NOEXCEPT
851 { return const_iterator(_M_data() + this->size()); }
854 * Returns a read/write reverse iterator that points to the last
855 * character in the %string. Iteration is done in reverse element
859 rbegin() _GLIBCXX_NOEXCEPT
860 { return reverse_iterator(this->end()); }
863 * Returns a read-only (constant) reverse iterator that points
864 * to the last character in the %string. Iteration is done in
865 * reverse element order.
867 const_reverse_iterator
868 rbegin() const _GLIBCXX_NOEXCEPT
869 { return const_reverse_iterator(this->end()); }
872 * Returns a read/write reverse iterator that points to one before the
873 * first character in the %string. Iteration is done in reverse
877 rend() _GLIBCXX_NOEXCEPT
878 { return reverse_iterator(this->begin()); }
881 * Returns a read-only (constant) reverse iterator that points
882 * to one before the first character in the %string. Iteration
883 * is done in reverse element order.
885 const_reverse_iterator
886 rend() const _GLIBCXX_NOEXCEPT
887 { return const_reverse_iterator(this->begin()); }
889 #if __cplusplus >= 201103L
891 * Returns a read-only (constant) iterator that points to the first
892 * character in the %string.
895 cbegin() const noexcept
896 { return const_iterator(this->_M_data()); }
899 * Returns a read-only (constant) iterator that points one past the
900 * last character in the %string.
903 cend() const noexcept
904 { return const_iterator(this->_M_data() + this->size()); }
907 * Returns a read-only (constant) reverse iterator that points
908 * to the last character in the %string. Iteration is done in
909 * reverse element order.
911 const_reverse_iterator
912 crbegin() const noexcept
913 { return const_reverse_iterator(this->end()); }
916 * Returns a read-only (constant) reverse iterator that points
917 * to one before the first character in the %string. Iteration
918 * is done in reverse element order.
920 const_reverse_iterator
921 crend() const noexcept
922 { return const_reverse_iterator(this->begin()); }
927 /// Returns the number of characters in the string, not including any
928 /// null-termination.
930 size() const _GLIBCXX_NOEXCEPT
931 { return _M_string_length
; }
933 /// Returns the number of characters in the string, not including any
934 /// null-termination.
936 length() const _GLIBCXX_NOEXCEPT
937 { return _M_string_length
; }
939 /// Returns the size() of the largest possible %string.
941 max_size() const _GLIBCXX_NOEXCEPT
942 { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
945 * @brief Resizes the %string to the specified number of characters.
946 * @param __n Number of characters the %string should contain.
947 * @param __c Character to fill any new elements.
949 * This function will %resize the %string to the specified
950 * number of characters. If the number is smaller than the
951 * %string's current size the %string is truncated, otherwise
952 * the %string is extended and new elements are %set to @a __c.
955 resize(size_type __n
, _CharT __c
);
958 * @brief Resizes the %string to the specified number of characters.
959 * @param __n Number of characters the %string should contain.
961 * This function will resize the %string to the specified length. If
962 * the new size is smaller than the %string's current size the %string
963 * is truncated, otherwise the %string is extended and new characters
964 * are default-constructed. For basic types such as char, this means
968 resize(size_type __n
)
969 { this->resize(__n
, _CharT()); }
971 #if __cplusplus >= 201103L
972 /// A non-binding request to reduce capacity() to size().
974 shrink_to_fit() noexcept
977 if (capacity() > size())
989 * Returns the total number of characters that the %string can hold
990 * before needing to allocate more memory.
993 capacity() const _GLIBCXX_NOEXCEPT
995 return _M_is_local() ? size_type(_S_local_capacity
)
996 : _M_allocated_capacity
;
1000 * @brief Attempt to preallocate enough memory for specified number of
1002 * @param __res_arg Number of characters required.
1003 * @throw std::length_error If @a __res_arg exceeds @c max_size().
1005 * This function attempts to reserve enough memory for the
1006 * %string to hold the specified number of characters. If the
1007 * number requested is more than max_size(), length_error is
1010 * The advantage of this function is that if optimal code is a
1011 * necessity and the user can determine the string length that will be
1012 * required, the user can reserve the memory in %advance, and thus
1013 * prevent a possible reallocation of memory and copying of %string
1017 reserve(size_type __res_arg
= 0);
1020 * Erases the string, making it empty.
1023 clear() _GLIBCXX_NOEXCEPT
1024 { _M_set_length(0); }
1027 * Returns true if the %string is empty. Equivalent to
1028 * <code>*this == ""</code>.
1030 _GLIBCXX_NODISCARD
bool
1031 empty() const _GLIBCXX_NOEXCEPT
1032 { return this->size() == 0; }
1036 * @brief Subscript access to the data contained in the %string.
1037 * @param __pos The index of the character to access.
1038 * @return Read-only (constant) reference to the character.
1040 * This operator allows for easy, array-style, data access.
1041 * Note that data access with this operator is unchecked and
1042 * out_of_range lookups are not defined. (For checked lookups
1046 operator[] (size_type __pos
) const _GLIBCXX_NOEXCEPT
1048 __glibcxx_assert(__pos
<= size());
1049 return _M_data()[__pos
];
1053 * @brief Subscript access to the data contained in the %string.
1054 * @param __pos The index of the character to access.
1055 * @return Read/write reference to the character.
1057 * This operator allows for easy, array-style, data access.
1058 * Note that data access with this operator is unchecked and
1059 * out_of_range lookups are not defined. (For checked lookups
1063 operator[](size_type __pos
)
1065 // Allow pos == size() both in C++98 mode, as v3 extension,
1066 // and in C++11 mode.
1067 __glibcxx_assert(__pos
<= size());
1068 // In pedantic mode be strict in C++98 mode.
1069 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus
>= 201103L || __pos
< size());
1070 return _M_data()[__pos
];
1074 * @brief Provides access to the data contained in the %string.
1075 * @param __n The index of the character to access.
1076 * @return Read-only (const) reference to the character.
1077 * @throw std::out_of_range If @a n is an invalid index.
1079 * This function provides for safer data access. The parameter is
1080 * first checked that it is in the range of the string. The function
1081 * throws out_of_range if the check fails.
1084 at(size_type __n
) const
1086 if (__n
>= this->size())
1087 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1088 "(which is %zu) >= this->size() "
1091 return _M_data()[__n
];
1095 * @brief Provides access to the data contained in the %string.
1096 * @param __n The index of the character to access.
1097 * @return Read/write reference to the character.
1098 * @throw std::out_of_range If @a n is an invalid index.
1100 * This function provides for safer data access. The parameter is
1101 * first checked that it is in the range of the string. The function
1102 * throws out_of_range if the check fails.
1108 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1109 "(which is %zu) >= this->size() "
1112 return _M_data()[__n
];
1115 #if __cplusplus >= 201103L
1117 * Returns a read/write reference to the data at the first
1118 * element of the %string.
1123 __glibcxx_assert(!empty());
1124 return operator[](0);
1128 * Returns a read-only (constant) reference to the data at the first
1129 * element of the %string.
1132 front() const noexcept
1134 __glibcxx_assert(!empty());
1135 return operator[](0);
1139 * Returns a read/write reference to the data at the last
1140 * element of the %string.
1145 __glibcxx_assert(!empty());
1146 return operator[](this->size() - 1);
1150 * Returns a read-only (constant) reference to the data at the
1151 * last element of the %string.
1154 back() const noexcept
1156 __glibcxx_assert(!empty());
1157 return operator[](this->size() - 1);
1163 * @brief Append a string to this string.
1164 * @param __str The string to append.
1165 * @return Reference to this string.
1168 operator+=(const basic_string
& __str
)
1169 { return this->append(__str
); }
1172 * @brief Append a C string.
1173 * @param __s The C string to append.
1174 * @return Reference to this string.
1177 operator+=(const _CharT
* __s
)
1178 { return this->append(__s
); }
1181 * @brief Append a character.
1182 * @param __c The character to append.
1183 * @return Reference to this string.
1186 operator+=(_CharT __c
)
1188 this->push_back(__c
);
1192 #if __cplusplus >= 201103L
1194 * @brief Append an initializer_list of characters.
1195 * @param __l The initializer_list of characters to be appended.
1196 * @return Reference to this string.
1199 operator+=(initializer_list
<_CharT
> __l
)
1200 { return this->append(__l
.begin(), __l
.size()); }
1203 #if __cplusplus >= 201703L
1205 * @brief Append a string_view.
1206 * @param __svt An object convertible to string_view to be appended.
1207 * @return Reference to this string.
1209 template<typename _Tp
>
1210 _If_sv
<_Tp
, basic_string
&>
1211 operator+=(const _Tp
& __svt
)
1212 { return this->append(__svt
); }
1216 * @brief Append a string to this string.
1217 * @param __str The string to append.
1218 * @return Reference to this string.
1221 append(const basic_string
& __str
)
1222 { return _M_append(__str
._M_data(), __str
.size()); }
1225 * @brief Append a substring.
1226 * @param __str The string to append.
1227 * @param __pos Index of the first character of str to append.
1228 * @param __n The number of characters to append.
1229 * @return Reference to this string.
1230 * @throw std::out_of_range if @a __pos is not a valid index.
1232 * This function appends @a __n characters from @a __str
1233 * starting at @a __pos to this string. If @a __n is is larger
1234 * than the number of available characters in @a __str, the
1235 * remainder of @a __str is appended.
1238 append(const basic_string
& __str
, size_type __pos
, size_type __n
= npos
)
1239 { return _M_append(__str
._M_data()
1240 + __str
._M_check(__pos
, "basic_string::append"),
1241 __str
._M_limit(__pos
, __n
)); }
1244 * @brief Append a C substring.
1245 * @param __s The C string to append.
1246 * @param __n The number of characters to append.
1247 * @return Reference to this string.
1250 append(const _CharT
* __s
, size_type __n
)
1252 __glibcxx_requires_string_len(__s
, __n
);
1253 _M_check_length(size_type(0), __n
, "basic_string::append");
1254 return _M_append(__s
, __n
);
1258 * @brief Append a C string.
1259 * @param __s The C string to append.
1260 * @return Reference to this string.
1263 append(const _CharT
* __s
)
1265 __glibcxx_requires_string(__s
);
1266 const size_type __n
= traits_type::length(__s
);
1267 _M_check_length(size_type(0), __n
, "basic_string::append");
1268 return _M_append(__s
, __n
);
1272 * @brief Append multiple characters.
1273 * @param __n The number of characters to append.
1274 * @param __c The character to use.
1275 * @return Reference to this string.
1277 * Appends __n copies of __c to this string.
1280 append(size_type __n
, _CharT __c
)
1281 { return _M_replace_aux(this->size(), size_type(0), __n
, __c
); }
1283 #if __cplusplus >= 201103L
1285 * @brief Append an initializer_list of characters.
1286 * @param __l The initializer_list of characters to append.
1287 * @return Reference to this string.
1290 append(initializer_list
<_CharT
> __l
)
1291 { return this->append(__l
.begin(), __l
.size()); }
1295 * @brief Append a range of characters.
1296 * @param __first Iterator referencing the first character to append.
1297 * @param __last Iterator marking the end of the range.
1298 * @return Reference to this string.
1300 * Appends characters in the range [__first,__last) to this string.
1302 #if __cplusplus >= 201103L
1303 template<class _InputIterator
,
1304 typename
= std::_RequireInputIter
<_InputIterator
>>
1306 template<class _InputIterator
>
1309 append(_InputIterator __first
, _InputIterator __last
)
1310 { return this->replace(end(), end(), __first
, __last
); }
1312 #if __cplusplus >= 201703L
1314 * @brief Append a string_view.
1315 * @param __svt An object convertible to string_view to be appended.
1316 * @return Reference to this string.
1318 template<typename _Tp
>
1319 _If_sv
<_Tp
, basic_string
&>
1320 append(const _Tp
& __svt
)
1322 __sv_type __sv
= __svt
;
1323 return this->append(__sv
.data(), __sv
.size());
1327 * @brief Append a range of characters from a string_view.
1328 * @param __svt An object convertible to string_view to be appended from.
1329 * @param __pos The position in the string_view to append from.
1330 * @param __n The number of characters to append from the string_view.
1331 * @return Reference to this string.
1333 template<typename _Tp
>
1334 _If_sv
<_Tp
, basic_string
&>
1335 append(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
1337 __sv_type __sv
= __svt
;
1338 return _M_append(__sv
.data()
1339 + __sv
._M_check(__pos
, "basic_string::append"),
1340 __sv
._M_limit(__pos
, __n
));
1345 * @brief Append a single character.
1346 * @param __c Character to append.
1349 push_back(_CharT __c
)
1351 const size_type __size
= this->size();
1352 if (__size
+ 1 > this->capacity())
1353 this->_M_mutate(__size
, size_type(0), 0, size_type(1));
1354 traits_type::assign(this->_M_data()[__size
], __c
);
1355 this->_M_set_length(__size
+ 1);
1359 * @brief Set value to contents of another string.
1360 * @param __str Source string to use.
1361 * @return Reference to this string.
1364 assign(const basic_string
& __str
)
1366 this->_M_assign(__str
);
1370 #if __cplusplus >= 201103L
1372 * @brief Set value to contents of another string.
1373 * @param __str Source string to use.
1374 * @return Reference to this string.
1376 * This function sets this string to the exact contents of @a __str.
1377 * @a __str is a valid, but unspecified string.
1380 assign(basic_string
&& __str
)
1381 noexcept(_Alloc_traits::_S_nothrow_move())
1383 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1384 // 2063. Contradictory requirements for string move assignment
1385 return *this = std::move(__str
);
1390 * @brief Set value to a substring of a string.
1391 * @param __str The string to use.
1392 * @param __pos Index of the first character of str.
1393 * @param __n Number of characters to use.
1394 * @return Reference to this string.
1395 * @throw std::out_of_range if @a pos is not a valid index.
1397 * This function sets this string to the substring of @a __str
1398 * consisting of @a __n characters at @a __pos. If @a __n is
1399 * is larger than the number of available characters in @a
1400 * __str, the remainder of @a __str is used.
1403 assign(const basic_string
& __str
, size_type __pos
, size_type __n
= npos
)
1404 { return _M_replace(size_type(0), this->size(), __str
._M_data()
1405 + __str
._M_check(__pos
, "basic_string::assign"),
1406 __str
._M_limit(__pos
, __n
)); }
1409 * @brief Set value to a C substring.
1410 * @param __s The C string to use.
1411 * @param __n Number of characters to use.
1412 * @return Reference to this string.
1414 * This function sets the value of this string to the first @a __n
1415 * characters of @a __s. If @a __n is is larger than the number of
1416 * available characters in @a __s, the remainder of @a __s is used.
1419 assign(const _CharT
* __s
, size_type __n
)
1421 __glibcxx_requires_string_len(__s
, __n
);
1422 return _M_replace(size_type(0), this->size(), __s
, __n
);
1426 * @brief Set value to contents of a C string.
1427 * @param __s The C string to use.
1428 * @return Reference to this string.
1430 * This function sets the value of this string to the value of @a __s.
1431 * The data is copied, so there is no dependence on @a __s once the
1435 assign(const _CharT
* __s
)
1437 __glibcxx_requires_string(__s
);
1438 return _M_replace(size_type(0), this->size(), __s
,
1439 traits_type::length(__s
));
1443 * @brief Set value to multiple characters.
1444 * @param __n Length of the resulting string.
1445 * @param __c The character to use.
1446 * @return Reference to this string.
1448 * This function sets the value of this string to @a __n copies of
1452 assign(size_type __n
, _CharT __c
)
1453 { return _M_replace_aux(size_type(0), this->size(), __n
, __c
); }
1456 * @brief Set value to a range of characters.
1457 * @param __first Iterator referencing the first character to append.
1458 * @param __last Iterator marking the end of the range.
1459 * @return Reference to this string.
1461 * Sets value of string to characters in the range [__first,__last).
1463 #if __cplusplus >= 201103L
1464 template<class _InputIterator
,
1465 typename
= std::_RequireInputIter
<_InputIterator
>>
1467 template<class _InputIterator
>
1470 assign(_InputIterator __first
, _InputIterator __last
)
1471 { return this->replace(begin(), end(), __first
, __last
); }
1473 #if __cplusplus >= 201103L
1475 * @brief Set value to an initializer_list of characters.
1476 * @param __l The initializer_list of characters to assign.
1477 * @return Reference to this string.
1480 assign(initializer_list
<_CharT
> __l
)
1481 { return this->assign(__l
.begin(), __l
.size()); }
1484 #if __cplusplus >= 201703L
1486 * @brief Set value from a string_view.
1487 * @param __svt The source object convertible to string_view.
1488 * @return Reference to this string.
1490 template<typename _Tp
>
1491 _If_sv
<_Tp
, basic_string
&>
1492 assign(const _Tp
& __svt
)
1494 __sv_type __sv
= __svt
;
1495 return this->assign(__sv
.data(), __sv
.size());
1499 * @brief Set value from a range of characters in a string_view.
1500 * @param __svt The source object convertible to string_view.
1501 * @param __pos The position in the string_view to assign from.
1502 * @param __n The number of characters to assign.
1503 * @return Reference to this string.
1505 template<typename _Tp
>
1506 _If_sv
<_Tp
, basic_string
&>
1507 assign(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
1509 __sv_type __sv
= __svt
;
1510 return _M_replace(size_type(0), this->size(), __sv
.data()
1511 + __sv
._M_check(__pos
, "basic_string::assign"),
1512 __sv
._M_limit(__pos
, __n
));
1516 #if __cplusplus >= 201103L
1518 * @brief Insert multiple characters.
1519 * @param __p Const_iterator referencing location in string to
1521 * @param __n Number of characters to insert
1522 * @param __c The character to insert.
1523 * @return Iterator referencing the first inserted char.
1524 * @throw std::length_error If new length exceeds @c max_size().
1526 * Inserts @a __n copies of character @a __c starting at the
1527 * position referenced by iterator @a __p. If adding
1528 * characters causes the length to exceed max_size(),
1529 * length_error is thrown. The value of the string doesn't
1530 * change if an error is thrown.
1533 insert(const_iterator __p
, size_type __n
, _CharT __c
)
1535 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1536 const size_type __pos
= __p
- begin();
1537 this->replace(__p
, __p
, __n
, __c
);
1538 return iterator(this->_M_data() + __pos
);
1542 * @brief Insert multiple characters.
1543 * @param __p Iterator referencing location in string to insert at.
1544 * @param __n Number of characters to insert
1545 * @param __c The character to insert.
1546 * @throw std::length_error If new length exceeds @c max_size().
1548 * Inserts @a __n copies of character @a __c starting at the
1549 * position referenced by iterator @a __p. If adding
1550 * characters causes the length to exceed max_size(),
1551 * length_error is thrown. The value of the string doesn't
1552 * change if an error is thrown.
1555 insert(iterator __p
, size_type __n
, _CharT __c
)
1556 { this->replace(__p
, __p
, __n
, __c
); }
1559 #if __cplusplus >= 201103L
1561 * @brief Insert a range of characters.
1562 * @param __p Const_iterator referencing location in string to
1564 * @param __beg Start of range.
1565 * @param __end End of range.
1566 * @return Iterator referencing the first inserted char.
1567 * @throw std::length_error If new length exceeds @c max_size().
1569 * Inserts characters in range [beg,end). If adding characters
1570 * causes the length to exceed max_size(), length_error is
1571 * thrown. The value of the string doesn't change if an error
1574 template<class _InputIterator
,
1575 typename
= std::_RequireInputIter
<_InputIterator
>>
1577 insert(const_iterator __p
, _InputIterator __beg
, _InputIterator __end
)
1579 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1580 const size_type __pos
= __p
- begin();
1581 this->replace(__p
, __p
, __beg
, __end
);
1582 return iterator(this->_M_data() + __pos
);
1586 * @brief Insert a range of characters.
1587 * @param __p Iterator referencing location in string to insert at.
1588 * @param __beg Start of range.
1589 * @param __end End of range.
1590 * @throw std::length_error If new length exceeds @c max_size().
1592 * Inserts characters in range [__beg,__end). If adding
1593 * characters causes the length to exceed max_size(),
1594 * length_error is thrown. The value of the string doesn't
1595 * change if an error is thrown.
1597 template<class _InputIterator
>
1599 insert(iterator __p
, _InputIterator __beg
, _InputIterator __end
)
1600 { this->replace(__p
, __p
, __beg
, __end
); }
1603 #if __cplusplus >= 201103L
1605 * @brief Insert an initializer_list of characters.
1606 * @param __p Iterator referencing location in string to insert at.
1607 * @param __l The initializer_list of characters to insert.
1608 * @throw std::length_error If new length exceeds @c max_size().
1611 insert(const_iterator __p
, initializer_list
<_CharT
> __l
)
1612 { return this->insert(__p
, __l
.begin(), __l
.end()); }
1614 #ifdef _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
1615 // See PR libstdc++/83328
1617 insert(iterator __p
, initializer_list
<_CharT
> __l
)
1619 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1620 this->insert(__p
- begin(), __l
.begin(), __l
.size());
1626 * @brief Insert value of a string.
1627 * @param __pos1 Iterator referencing location in string to insert at.
1628 * @param __str The string to insert.
1629 * @return Reference to this string.
1630 * @throw std::length_error If new length exceeds @c max_size().
1632 * Inserts value of @a __str starting at @a __pos1. If adding
1633 * characters causes the length to exceed max_size(),
1634 * length_error is thrown. The value of the string doesn't
1635 * change if an error is thrown.
1638 insert(size_type __pos1
, const basic_string
& __str
)
1639 { return this->replace(__pos1
, size_type(0),
1640 __str
._M_data(), __str
.size()); }
1643 * @brief Insert a substring.
1644 * @param __pos1 Iterator referencing location in string to insert at.
1645 * @param __str The string to insert.
1646 * @param __pos2 Start of characters in str to insert.
1647 * @param __n Number of characters to insert.
1648 * @return Reference to this string.
1649 * @throw std::length_error If new length exceeds @c max_size().
1650 * @throw std::out_of_range If @a pos1 > size() or
1651 * @a __pos2 > @a str.size().
1653 * Starting at @a pos1, insert @a __n character of @a __str
1654 * beginning with @a __pos2. If adding characters causes the
1655 * length to exceed max_size(), length_error is thrown. If @a
1656 * __pos1 is beyond the end of this string or @a __pos2 is
1657 * beyond the end of @a __str, out_of_range is thrown. The
1658 * value of the string doesn't change if an error is thrown.
1661 insert(size_type __pos1
, const basic_string
& __str
,
1662 size_type __pos2
, size_type __n
= npos
)
1663 { return this->replace(__pos1
, size_type(0), __str
._M_data()
1664 + __str
._M_check(__pos2
, "basic_string::insert"),
1665 __str
._M_limit(__pos2
, __n
)); }
1668 * @brief Insert a C substring.
1669 * @param __pos Iterator referencing location in string to insert at.
1670 * @param __s The C string to insert.
1671 * @param __n The number of characters to insert.
1672 * @return Reference to this string.
1673 * @throw std::length_error If new length exceeds @c max_size().
1674 * @throw std::out_of_range If @a __pos is beyond the end of this
1677 * Inserts the first @a __n characters of @a __s starting at @a
1678 * __pos. If adding characters causes the length to exceed
1679 * max_size(), length_error is thrown. If @a __pos is beyond
1680 * end(), out_of_range is thrown. The value of the string
1681 * doesn't change if an error is thrown.
1684 insert(size_type __pos
, const _CharT
* __s
, size_type __n
)
1685 { return this->replace(__pos
, size_type(0), __s
, __n
); }
1688 * @brief Insert a C string.
1689 * @param __pos Iterator referencing location in string to insert at.
1690 * @param __s The C string to insert.
1691 * @return Reference to this string.
1692 * @throw std::length_error If new length exceeds @c max_size().
1693 * @throw std::out_of_range If @a pos is beyond the end of this
1696 * Inserts the first @a n characters of @a __s starting at @a __pos. If
1697 * adding characters causes the length to exceed max_size(),
1698 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
1699 * thrown. The value of the string doesn't change if an error is
1703 insert(size_type __pos
, const _CharT
* __s
)
1705 __glibcxx_requires_string(__s
);
1706 return this->replace(__pos
, size_type(0), __s
,
1707 traits_type::length(__s
));
1711 * @brief Insert multiple characters.
1712 * @param __pos Index in string to insert at.
1713 * @param __n Number of characters to insert
1714 * @param __c The character to insert.
1715 * @return Reference to this string.
1716 * @throw std::length_error If new length exceeds @c max_size().
1717 * @throw std::out_of_range If @a __pos is beyond the end of this
1720 * Inserts @a __n copies of character @a __c starting at index
1721 * @a __pos. If adding characters causes the length to exceed
1722 * max_size(), length_error is thrown. If @a __pos > length(),
1723 * out_of_range is thrown. The value of the string doesn't
1724 * change if an error is thrown.
1727 insert(size_type __pos
, size_type __n
, _CharT __c
)
1728 { return _M_replace_aux(_M_check(__pos
, "basic_string::insert"),
1729 size_type(0), __n
, __c
); }
1732 * @brief Insert one character.
1733 * @param __p Iterator referencing position in string to insert at.
1734 * @param __c The character to insert.
1735 * @return Iterator referencing newly inserted char.
1736 * @throw std::length_error If new length exceeds @c max_size().
1738 * Inserts character @a __c at position referenced by @a __p.
1739 * If adding character causes the length to exceed max_size(),
1740 * length_error is thrown. If @a __p is beyond end of string,
1741 * out_of_range is thrown. The value of the string doesn't
1742 * change if an error is thrown.
1745 insert(__const_iterator __p
, _CharT __c
)
1747 _GLIBCXX_DEBUG_PEDASSERT(__p
>= begin() && __p
<= end());
1748 const size_type __pos
= __p
- begin();
1749 _M_replace_aux(__pos
, size_type(0), size_type(1), __c
);
1750 return iterator(_M_data() + __pos
);
1753 #if __cplusplus >= 201703L
1755 * @brief Insert a string_view.
1756 * @param __pos Iterator referencing position in string to insert at.
1757 * @param __svt The object convertible to string_view to insert.
1758 * @return Reference to this string.
1760 template<typename _Tp
>
1761 _If_sv
<_Tp
, basic_string
&>
1762 insert(size_type __pos
, const _Tp
& __svt
)
1764 __sv_type __sv
= __svt
;
1765 return this->insert(__pos
, __sv
.data(), __sv
.size());
1769 * @brief Insert a string_view.
1770 * @param __pos Iterator referencing position in string to insert at.
1771 * @param __svt The object convertible to string_view to insert from.
1772 * @param __pos Iterator referencing position in string_view to insert
1774 * @param __n The number of characters to insert.
1775 * @return Reference to this string.
1777 template<typename _Tp
>
1778 _If_sv
<_Tp
, basic_string
&>
1779 insert(size_type __pos1
, const _Tp
& __svt
,
1780 size_type __pos2
, size_type __n
= npos
)
1782 __sv_type __sv
= __svt
;
1783 return this->replace(__pos1
, size_type(0), __sv
.data()
1784 + __sv
._M_check(__pos2
, "basic_string::insert"),
1785 __sv
._M_limit(__pos2
, __n
));
1790 * @brief Remove characters.
1791 * @param __pos Index of first character to remove (default 0).
1792 * @param __n Number of characters to remove (default remainder).
1793 * @return Reference to this string.
1794 * @throw std::out_of_range If @a pos is beyond the end of this
1797 * Removes @a __n characters from this string starting at @a
1798 * __pos. The length of the string is reduced by @a __n. If
1799 * there are < @a __n characters to remove, the remainder of
1800 * the string is truncated. If @a __p is beyond end of string,
1801 * out_of_range is thrown. The value of the string doesn't
1802 * change if an error is thrown.
1805 erase(size_type __pos
= 0, size_type __n
= npos
)
1807 _M_check(__pos
, "basic_string::erase");
1809 this->_M_set_length(__pos
);
1811 this->_M_erase(__pos
, _M_limit(__pos
, __n
));
1816 * @brief Remove one character.
1817 * @param __position Iterator referencing the character to remove.
1818 * @return iterator referencing same location after removal.
1820 * Removes the character at @a __position from this string. The value
1821 * of the string doesn't change if an error is thrown.
1824 erase(__const_iterator __position
)
1826 _GLIBCXX_DEBUG_PEDASSERT(__position
>= begin()
1827 && __position
< end());
1828 const size_type __pos
= __position
- begin();
1829 this->_M_erase(__pos
, size_type(1));
1830 return iterator(_M_data() + __pos
);
1834 * @brief Remove a range of characters.
1835 * @param __first Iterator referencing the first character to remove.
1836 * @param __last Iterator referencing the end of the range.
1837 * @return Iterator referencing location of first after removal.
1839 * Removes the characters in the range [first,last) from this string.
1840 * The value of the string doesn't change if an error is thrown.
1843 erase(__const_iterator __first
, __const_iterator __last
)
1845 _GLIBCXX_DEBUG_PEDASSERT(__first
>= begin() && __first
<= __last
1846 && __last
<= end());
1847 const size_type __pos
= __first
- begin();
1848 if (__last
== end())
1849 this->_M_set_length(__pos
);
1851 this->_M_erase(__pos
, __last
- __first
);
1852 return iterator(this->_M_data() + __pos
);
1855 #if __cplusplus >= 201103L
1857 * @brief Remove the last character.
1859 * The string must be non-empty.
1864 __glibcxx_assert(!empty());
1865 _M_erase(size() - 1, 1);
1870 * @brief Replace characters with value from another string.
1871 * @param __pos Index of first character to replace.
1872 * @param __n Number of characters to be replaced.
1873 * @param __str String to insert.
1874 * @return Reference to this string.
1875 * @throw std::out_of_range If @a pos is beyond the end of this
1877 * @throw std::length_error If new length exceeds @c max_size().
1879 * Removes the characters in the range [__pos,__pos+__n) from
1880 * this string. In place, the value of @a __str is inserted.
1881 * If @a __pos is beyond end of string, out_of_range is thrown.
1882 * If the length of the result exceeds max_size(), length_error
1883 * is thrown. The value of the string doesn't change if an
1887 replace(size_type __pos
, size_type __n
, const basic_string
& __str
)
1888 { return this->replace(__pos
, __n
, __str
._M_data(), __str
.size()); }
1891 * @brief Replace characters with value from another string.
1892 * @param __pos1 Index of first character to replace.
1893 * @param __n1 Number of characters to be replaced.
1894 * @param __str String to insert.
1895 * @param __pos2 Index of first character of str to use.
1896 * @param __n2 Number of characters from str to use.
1897 * @return Reference to this string.
1898 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
1900 * @throw std::length_error If new length exceeds @c max_size().
1902 * Removes the characters in the range [__pos1,__pos1 + n) from this
1903 * string. In place, the value of @a __str is inserted. If @a __pos is
1904 * beyond end of string, out_of_range is thrown. If the length of the
1905 * result exceeds max_size(), length_error is thrown. The value of the
1906 * string doesn't change if an error is thrown.
1909 replace(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
1910 size_type __pos2
, size_type __n2
= npos
)
1911 { return this->replace(__pos1
, __n1
, __str
._M_data()
1912 + __str
._M_check(__pos2
, "basic_string::replace"),
1913 __str
._M_limit(__pos2
, __n2
)); }
1916 * @brief Replace characters with value of a C substring.
1917 * @param __pos Index of first character to replace.
1918 * @param __n1 Number of characters to be replaced.
1919 * @param __s C string to insert.
1920 * @param __n2 Number of characters from @a s to use.
1921 * @return Reference to this string.
1922 * @throw std::out_of_range If @a pos1 > size().
1923 * @throw std::length_error If new length exceeds @c max_size().
1925 * Removes the characters in the range [__pos,__pos + __n1)
1926 * from this string. In place, the first @a __n2 characters of
1927 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
1928 * @a __pos is beyond end of string, out_of_range is thrown. If
1929 * the length of result exceeds max_size(), length_error is
1930 * thrown. The value of the string doesn't change if an error
1934 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
1937 __glibcxx_requires_string_len(__s
, __n2
);
1938 return _M_replace(_M_check(__pos
, "basic_string::replace"),
1939 _M_limit(__pos
, __n1
), __s
, __n2
);
1943 * @brief Replace characters with value of a C string.
1944 * @param __pos Index of first character to replace.
1945 * @param __n1 Number of characters to be replaced.
1946 * @param __s C string to insert.
1947 * @return Reference to this string.
1948 * @throw std::out_of_range If @a pos > size().
1949 * @throw std::length_error If new length exceeds @c max_size().
1951 * Removes the characters in the range [__pos,__pos + __n1)
1952 * from this string. In place, the characters of @a __s are
1953 * inserted. If @a __pos is beyond end of string, out_of_range
1954 * is thrown. If the length of result exceeds max_size(),
1955 * length_error is thrown. The value of the string doesn't
1956 * change if an error is thrown.
1959 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
)
1961 __glibcxx_requires_string(__s
);
1962 return this->replace(__pos
, __n1
, __s
, traits_type::length(__s
));
1966 * @brief Replace characters with multiple characters.
1967 * @param __pos Index of first character to replace.
1968 * @param __n1 Number of characters to be replaced.
1969 * @param __n2 Number of characters to insert.
1970 * @param __c Character to insert.
1971 * @return Reference to this string.
1972 * @throw std::out_of_range If @a __pos > size().
1973 * @throw std::length_error If new length exceeds @c max_size().
1975 * Removes the characters in the range [pos,pos + n1) from this
1976 * string. In place, @a __n2 copies of @a __c are inserted.
1977 * If @a __pos is beyond end of string, out_of_range is thrown.
1978 * If the length of result exceeds max_size(), length_error is
1979 * thrown. The value of the string doesn't change if an error
1983 replace(size_type __pos
, size_type __n1
, size_type __n2
, _CharT __c
)
1984 { return _M_replace_aux(_M_check(__pos
, "basic_string::replace"),
1985 _M_limit(__pos
, __n1
), __n2
, __c
); }
1988 * @brief Replace range of characters with string.
1989 * @param __i1 Iterator referencing start of range to replace.
1990 * @param __i2 Iterator referencing end of range to replace.
1991 * @param __str String value to insert.
1992 * @return Reference to this string.
1993 * @throw std::length_error If new length exceeds @c max_size().
1995 * Removes the characters in the range [__i1,__i2). In place,
1996 * the value of @a __str is inserted. If the length of result
1997 * exceeds max_size(), length_error is thrown. The value of
1998 * the string doesn't change if an error is thrown.
2001 replace(__const_iterator __i1
, __const_iterator __i2
,
2002 const basic_string
& __str
)
2003 { return this->replace(__i1
, __i2
, __str
._M_data(), __str
.size()); }
2006 * @brief Replace range of characters with C substring.
2007 * @param __i1 Iterator referencing start of range to replace.
2008 * @param __i2 Iterator referencing end of range to replace.
2009 * @param __s C string value to insert.
2010 * @param __n Number of characters from s to insert.
2011 * @return Reference to this string.
2012 * @throw std::length_error If new length exceeds @c max_size().
2014 * Removes the characters in the range [__i1,__i2). In place,
2015 * the first @a __n characters of @a __s are inserted. If the
2016 * length of result exceeds max_size(), length_error is thrown.
2017 * The value of the string doesn't change if an error is
2021 replace(__const_iterator __i1
, __const_iterator __i2
,
2022 const _CharT
* __s
, size_type __n
)
2024 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2026 return this->replace(__i1
- begin(), __i2
- __i1
, __s
, __n
);
2030 * @brief Replace range of characters with C string.
2031 * @param __i1 Iterator referencing start of range to replace.
2032 * @param __i2 Iterator referencing end of range to replace.
2033 * @param __s C string value to insert.
2034 * @return Reference to this string.
2035 * @throw std::length_error If new length exceeds @c max_size().
2037 * Removes the characters in the range [__i1,__i2). In place,
2038 * the characters of @a __s are inserted. If the length of
2039 * result exceeds max_size(), length_error is thrown. The
2040 * value of the string doesn't change if an error is thrown.
2043 replace(__const_iterator __i1
, __const_iterator __i2
, const _CharT
* __s
)
2045 __glibcxx_requires_string(__s
);
2046 return this->replace(__i1
, __i2
, __s
, traits_type::length(__s
));
2050 * @brief Replace range of characters with multiple characters
2051 * @param __i1 Iterator referencing start of range to replace.
2052 * @param __i2 Iterator referencing end of range to replace.
2053 * @param __n Number of characters to insert.
2054 * @param __c Character to insert.
2055 * @return Reference to this string.
2056 * @throw std::length_error If new length exceeds @c max_size().
2058 * Removes the characters in the range [__i1,__i2). In place,
2059 * @a __n copies of @a __c are inserted. If the length of
2060 * result exceeds max_size(), length_error is thrown. The
2061 * value of the string doesn't change if an error is thrown.
2064 replace(__const_iterator __i1
, __const_iterator __i2
, size_type __n
,
2067 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2069 return _M_replace_aux(__i1
- begin(), __i2
- __i1
, __n
, __c
);
2073 * @brief Replace range of characters with range.
2074 * @param __i1 Iterator referencing start of range to replace.
2075 * @param __i2 Iterator referencing end of range to replace.
2076 * @param __k1 Iterator referencing start of range to insert.
2077 * @param __k2 Iterator referencing end of range to insert.
2078 * @return Reference to this string.
2079 * @throw std::length_error If new length exceeds @c max_size().
2081 * Removes the characters in the range [__i1,__i2). In place,
2082 * characters in the range [__k1,__k2) are inserted. If the
2083 * length of result exceeds max_size(), length_error is thrown.
2084 * The value of the string doesn't change if an error is
2087 #if __cplusplus >= 201103L
2088 template<class _InputIterator
,
2089 typename
= std::_RequireInputIter
<_InputIterator
>>
2091 replace(const_iterator __i1
, const_iterator __i2
,
2092 _InputIterator __k1
, _InputIterator __k2
)
2094 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2096 __glibcxx_requires_valid_range(__k1
, __k2
);
2097 return this->_M_replace_dispatch(__i1
, __i2
, __k1
, __k2
,
2098 std::__false_type());
2101 template<class _InputIterator
>
2102 #ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
2103 typename __enable_if_not_native_iterator
<_InputIterator
>::__type
2107 replace(iterator __i1
, iterator __i2
,
2108 _InputIterator __k1
, _InputIterator __k2
)
2110 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2112 __glibcxx_requires_valid_range(__k1
, __k2
);
2113 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
2114 return _M_replace_dispatch(__i1
, __i2
, __k1
, __k2
, _Integral());
2118 // Specializations for the common case of pointer and iterator:
2119 // useful to avoid the overhead of temporary buffering in _M_replace.
2121 replace(__const_iterator __i1
, __const_iterator __i2
,
2122 _CharT
* __k1
, _CharT
* __k2
)
2124 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2126 __glibcxx_requires_valid_range(__k1
, __k2
);
2127 return this->replace(__i1
- begin(), __i2
- __i1
,
2132 replace(__const_iterator __i1
, __const_iterator __i2
,
2133 const _CharT
* __k1
, const _CharT
* __k2
)
2135 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2137 __glibcxx_requires_valid_range(__k1
, __k2
);
2138 return this->replace(__i1
- begin(), __i2
- __i1
,
2143 replace(__const_iterator __i1
, __const_iterator __i2
,
2144 iterator __k1
, iterator __k2
)
2146 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2148 __glibcxx_requires_valid_range(__k1
, __k2
);
2149 return this->replace(__i1
- begin(), __i2
- __i1
,
2150 __k1
.base(), __k2
- __k1
);
2154 replace(__const_iterator __i1
, __const_iterator __i2
,
2155 const_iterator __k1
, const_iterator __k2
)
2157 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1
&& __i1
<= __i2
2159 __glibcxx_requires_valid_range(__k1
, __k2
);
2160 return this->replace(__i1
- begin(), __i2
- __i1
,
2161 __k1
.base(), __k2
- __k1
);
2164 #if __cplusplus >= 201103L
2166 * @brief Replace range of characters with initializer_list.
2167 * @param __i1 Iterator referencing start of range to replace.
2168 * @param __i2 Iterator referencing end of range to replace.
2169 * @param __l The initializer_list of characters to insert.
2170 * @return Reference to this string.
2171 * @throw std::length_error If new length exceeds @c max_size().
2173 * Removes the characters in the range [__i1,__i2). In place,
2174 * characters in the range [__k1,__k2) are inserted. If the
2175 * length of result exceeds max_size(), length_error is thrown.
2176 * The value of the string doesn't change if an error is
2179 basic_string
& replace(const_iterator __i1
, const_iterator __i2
,
2180 initializer_list
<_CharT
> __l
)
2181 { return this->replace(__i1
, __i2
, __l
.begin(), __l
.size()); }
2184 #if __cplusplus >= 201703L
2186 * @brief Replace range of characters with string_view.
2187 * @param __pos The position to replace at.
2188 * @param __n The number of characters to replace.
2189 * @param __svt The object convertible to string_view to insert.
2190 * @return Reference to this string.
2192 template<typename _Tp
>
2193 _If_sv
<_Tp
, basic_string
&>
2194 replace(size_type __pos
, size_type __n
, const _Tp
& __svt
)
2196 __sv_type __sv
= __svt
;
2197 return this->replace(__pos
, __n
, __sv
.data(), __sv
.size());
2201 * @brief Replace range of characters with string_view.
2202 * @param __pos1 The position to replace at.
2203 * @param __n1 The number of characters to replace.
2204 * @param __svt The object convertible to string_view to insert from.
2205 * @param __pos2 The position in the string_view to insert from.
2206 * @param __n2 The number of characters to insert.
2207 * @return Reference to this string.
2209 template<typename _Tp
>
2210 _If_sv
<_Tp
, basic_string
&>
2211 replace(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
2212 size_type __pos2
, size_type __n2
= npos
)
2214 __sv_type __sv
= __svt
;
2215 return this->replace(__pos1
, __n1
, __sv
.data()
2216 + __sv
._M_check(__pos2
, "basic_string::replace"),
2217 __sv
._M_limit(__pos2
, __n2
));
2221 * @brief Replace range of characters with string_view.
2222 * @param __i1 An iterator referencing the start position
2224 * @param __i2 An iterator referencing the end position
2226 * @param __svt The object convertible to string_view to insert from.
2227 * @return Reference to this string.
2229 template<typename _Tp
>
2230 _If_sv
<_Tp
, basic_string
&>
2231 replace(const_iterator __i1
, const_iterator __i2
, const _Tp
& __svt
)
2233 __sv_type __sv
= __svt
;
2234 return this->replace(__i1
- begin(), __i2
- __i1
, __sv
);
2239 template<class _Integer
>
2241 _M_replace_dispatch(const_iterator __i1
, const_iterator __i2
,
2242 _Integer __n
, _Integer __val
, __true_type
)
2243 { return _M_replace_aux(__i1
- begin(), __i2
- __i1
, __n
, __val
); }
2245 template<class _InputIterator
>
2247 _M_replace_dispatch(const_iterator __i1
, const_iterator __i2
,
2248 _InputIterator __k1
, _InputIterator __k2
,
2252 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
2256 _M_replace(size_type __pos
, size_type __len1
, const _CharT
* __s
,
2257 const size_type __len2
);
2260 _M_append(const _CharT
* __s
, size_type __n
);
2265 * @brief Copy substring into C string.
2266 * @param __s C string to copy value into.
2267 * @param __n Number of characters to copy.
2268 * @param __pos Index of first character to copy.
2269 * @return Number of characters actually copied
2270 * @throw std::out_of_range If __pos > size().
2272 * Copies up to @a __n characters starting at @a __pos into the
2273 * C string @a __s. If @a __pos is %greater than size(),
2274 * out_of_range is thrown.
2277 copy(_CharT
* __s
, size_type __n
, size_type __pos
= 0) const;
2280 * @brief Swap contents with another string.
2281 * @param __s String to swap with.
2283 * Exchanges the contents of this string with that of @a __s in constant
2287 swap(basic_string
& __s
) _GLIBCXX_NOEXCEPT
;
2289 // String operations:
2291 * @brief Return const pointer to null-terminated contents.
2293 * This is a handle to internal data. Do not modify or dire things may
2297 c_str() const _GLIBCXX_NOEXCEPT
2298 { return _M_data(); }
2301 * @brief Return const pointer to contents.
2303 * This is a pointer to internal data. It is undefined to modify
2304 * the contents through the returned pointer. To get a pointer that
2305 * allows modifying the contents use @c &str[0] instead,
2306 * (or in C++17 the non-const @c str.data() overload).
2309 data() const _GLIBCXX_NOEXCEPT
2310 { return _M_data(); }
2312 #if __cplusplus >= 201703L
2314 * @brief Return non-const pointer to contents.
2316 * This is a pointer to the character sequence held by the string.
2317 * Modifying the characters in the sequence is allowed.
2321 { return _M_data(); }
2325 * @brief Return copy of allocator used to construct this string.
2328 get_allocator() const _GLIBCXX_NOEXCEPT
2329 { return _M_get_allocator(); }
2332 * @brief Find position of a C substring.
2333 * @param __s C string to locate.
2334 * @param __pos Index of character to search from.
2335 * @param __n Number of characters from @a s to search for.
2336 * @return Index of start of first occurrence.
2338 * Starting from @a __pos, searches forward for the first @a
2339 * __n characters in @a __s within this string. If found,
2340 * returns the index where it begins. If not found, returns
2344 find(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2348 * @brief Find position of a string.
2349 * @param __str String to locate.
2350 * @param __pos Index of character to search from (default 0).
2351 * @return Index of start of first occurrence.
2353 * Starting from @a __pos, searches forward for value of @a __str within
2354 * this string. If found, returns the index where it begins. If not
2355 * found, returns npos.
2358 find(const basic_string
& __str
, size_type __pos
= 0) const
2360 { return this->find(__str
.data(), __pos
, __str
.size()); }
2362 #if __cplusplus >= 201703L
2364 * @brief Find position of a string_view.
2365 * @param __svt The object convertible to string_view to locate.
2366 * @param __pos Index of character to search from (default 0).
2367 * @return Index of start of first occurrence.
2369 template<typename _Tp
>
2370 _If_sv
<_Tp
, size_type
>
2371 find(const _Tp
& __svt
, size_type __pos
= 0) const
2372 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2374 __sv_type __sv
= __svt
;
2375 return this->find(__sv
.data(), __pos
, __sv
.size());
2380 * @brief Find position of a C string.
2381 * @param __s C string to locate.
2382 * @param __pos Index of character to search from (default 0).
2383 * @return Index of start of first occurrence.
2385 * Starting from @a __pos, searches forward for the value of @a
2386 * __s within this string. If found, returns the index where
2387 * it begins. If not found, returns npos.
2390 find(const _CharT
* __s
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
2392 __glibcxx_requires_string(__s
);
2393 return this->find(__s
, __pos
, traits_type::length(__s
));
2397 * @brief Find position of a character.
2398 * @param __c Character to locate.
2399 * @param __pos Index of character to search from (default 0).
2400 * @return Index of first occurrence.
2402 * Starting from @a __pos, searches forward for @a __c within
2403 * this string. If found, returns the index where it was
2404 * found. If not found, returns npos.
2407 find(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
;
2410 * @brief Find last position of a string.
2411 * @param __str String to locate.
2412 * @param __pos Index of character to search back from (default end).
2413 * @return Index of start of last occurrence.
2415 * Starting from @a __pos, searches backward for value of @a
2416 * __str within this string. If found, returns the index where
2417 * it begins. If not found, returns npos.
2420 rfind(const basic_string
& __str
, size_type __pos
= npos
) const
2422 { return this->rfind(__str
.data(), __pos
, __str
.size()); }
2424 #if __cplusplus >= 201703L
2426 * @brief Find last position of a string_view.
2427 * @param __svt The object convertible to string_view to locate.
2428 * @param __pos Index of character to search back from (default end).
2429 * @return Index of start of last occurrence.
2431 template<typename _Tp
>
2432 _If_sv
<_Tp
, size_type
>
2433 rfind(const _Tp
& __svt
, size_type __pos
= npos
) const
2434 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2436 __sv_type __sv
= __svt
;
2437 return this->rfind(__sv
.data(), __pos
, __sv
.size());
2442 * @brief Find last position of a C substring.
2443 * @param __s C string to locate.
2444 * @param __pos Index of character to search back from.
2445 * @param __n Number of characters from s to search for.
2446 * @return Index of start of last occurrence.
2448 * Starting from @a __pos, searches backward for the first @a
2449 * __n characters in @a __s within this string. If found,
2450 * returns the index where it begins. If not found, returns
2454 rfind(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2458 * @brief Find last position of a C string.
2459 * @param __s C string to locate.
2460 * @param __pos Index of character to start search at (default end).
2461 * @return Index of start of last occurrence.
2463 * Starting from @a __pos, searches backward for the value of
2464 * @a __s within this string. If found, returns the index
2465 * where it begins. If not found, returns npos.
2468 rfind(const _CharT
* __s
, size_type __pos
= npos
) const
2470 __glibcxx_requires_string(__s
);
2471 return this->rfind(__s
, __pos
, traits_type::length(__s
));
2475 * @brief Find last position of a character.
2476 * @param __c Character to locate.
2477 * @param __pos Index of character to search back from (default end).
2478 * @return Index of last occurrence.
2480 * Starting from @a __pos, searches backward for @a __c within
2481 * this string. If found, returns the index where it was
2482 * found. If not found, returns npos.
2485 rfind(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
;
2488 * @brief Find position of a character of string.
2489 * @param __str String containing characters to locate.
2490 * @param __pos Index of character to search from (default 0).
2491 * @return Index of first occurrence.
2493 * Starting from @a __pos, searches forward for one of the
2494 * characters of @a __str within this string. If found,
2495 * returns the index where it was found. If not found, returns
2499 find_first_of(const basic_string
& __str
, size_type __pos
= 0) const
2501 { return this->find_first_of(__str
.data(), __pos
, __str
.size()); }
2503 #if __cplusplus >= 201703L
2505 * @brief Find position of a character of a string_view.
2506 * @param __svt An object convertible to string_view containing
2507 * characters to locate.
2508 * @param __pos Index of character to search from (default 0).
2509 * @return Index of first occurrence.
2511 template<typename _Tp
>
2512 _If_sv
<_Tp
, size_type
>
2513 find_first_of(const _Tp
& __svt
, size_type __pos
= 0) const
2514 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2516 __sv_type __sv
= __svt
;
2517 return this->find_first_of(__sv
.data(), __pos
, __sv
.size());
2522 * @brief Find position of a character of C substring.
2523 * @param __s String containing characters to locate.
2524 * @param __pos Index of character to search from.
2525 * @param __n Number of characters from s to search for.
2526 * @return Index of first occurrence.
2528 * Starting from @a __pos, searches forward for one of the
2529 * first @a __n characters of @a __s within this string. If
2530 * found, returns the index where it was found. If not found,
2534 find_first_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2538 * @brief Find position of a character of C string.
2539 * @param __s String containing characters to locate.
2540 * @param __pos Index of character to search from (default 0).
2541 * @return Index of first occurrence.
2543 * Starting from @a __pos, searches forward for one of the
2544 * characters of @a __s within this string. If found, returns
2545 * the index where it was found. If not found, returns npos.
2548 find_first_of(const _CharT
* __s
, size_type __pos
= 0) const
2551 __glibcxx_requires_string(__s
);
2552 return this->find_first_of(__s
, __pos
, traits_type::length(__s
));
2556 * @brief Find position of a character.
2557 * @param __c Character to locate.
2558 * @param __pos Index of character to search from (default 0).
2559 * @return Index of first occurrence.
2561 * Starting from @a __pos, searches forward for the character
2562 * @a __c within this string. If found, returns the index
2563 * where it was found. If not found, returns npos.
2565 * Note: equivalent to find(__c, __pos).
2568 find_first_of(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
2569 { return this->find(__c
, __pos
); }
2572 * @brief Find last position of a character of string.
2573 * @param __str String containing characters to locate.
2574 * @param __pos Index of character to search back from (default end).
2575 * @return Index of last occurrence.
2577 * Starting from @a __pos, searches backward for one of the
2578 * characters of @a __str within this string. If found,
2579 * returns the index where it was found. If not found, returns
2583 find_last_of(const basic_string
& __str
, size_type __pos
= npos
) const
2585 { return this->find_last_of(__str
.data(), __pos
, __str
.size()); }
2587 #if __cplusplus >= 201703L
2589 * @brief Find last position of a character of string.
2590 * @param __svt An object convertible to string_view containing
2591 * characters to locate.
2592 * @param __pos Index of character to search back from (default end).
2593 * @return Index of last occurrence.
2595 template<typename _Tp
>
2596 _If_sv
<_Tp
, size_type
>
2597 find_last_of(const _Tp
& __svt
, size_type __pos
= npos
) const
2598 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2600 __sv_type __sv
= __svt
;
2601 return this->find_last_of(__sv
.data(), __pos
, __sv
.size());
2606 * @brief Find last position of a character of C substring.
2607 * @param __s C string containing characters to locate.
2608 * @param __pos Index of character to search back from.
2609 * @param __n Number of characters from s to search for.
2610 * @return Index of last occurrence.
2612 * Starting from @a __pos, searches backward for one of the
2613 * first @a __n characters of @a __s within this string. If
2614 * found, returns the index where it was found. If not found,
2618 find_last_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2622 * @brief Find last position of a character of C string.
2623 * @param __s C string containing characters to locate.
2624 * @param __pos Index of character to search back from (default end).
2625 * @return Index of last occurrence.
2627 * Starting from @a __pos, searches backward for one of the
2628 * characters of @a __s within this string. If found, returns
2629 * the index where it was found. If not found, returns npos.
2632 find_last_of(const _CharT
* __s
, size_type __pos
= npos
) const
2635 __glibcxx_requires_string(__s
);
2636 return this->find_last_of(__s
, __pos
, traits_type::length(__s
));
2640 * @brief Find last position of a character.
2641 * @param __c Character to locate.
2642 * @param __pos Index of character to search back from (default end).
2643 * @return Index of last occurrence.
2645 * Starting from @a __pos, searches backward for @a __c within
2646 * this string. If found, returns the index where it was
2647 * found. If not found, returns npos.
2649 * Note: equivalent to rfind(__c, __pos).
2652 find_last_of(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
2653 { return this->rfind(__c
, __pos
); }
2656 * @brief Find position of a character not in string.
2657 * @param __str String containing characters to avoid.
2658 * @param __pos Index of character to search from (default 0).
2659 * @return Index of first occurrence.
2661 * Starting from @a __pos, searches forward for a character not contained
2662 * in @a __str within this string. If found, returns the index where it
2663 * was found. If not found, returns npos.
2666 find_first_not_of(const basic_string
& __str
, size_type __pos
= 0) const
2668 { return this->find_first_not_of(__str
.data(), __pos
, __str
.size()); }
2670 #if __cplusplus >= 201703L
2672 * @brief Find position of a character not in a string_view.
2673 * @param __svt A object convertible to string_view containing
2674 * characters to avoid.
2675 * @param __pos Index of character to search from (default 0).
2676 * @return Index of first occurrence.
2678 template<typename _Tp
>
2679 _If_sv
<_Tp
, size_type
>
2680 find_first_not_of(const _Tp
& __svt
, size_type __pos
= 0) const
2681 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2683 __sv_type __sv
= __svt
;
2684 return this->find_first_not_of(__sv
.data(), __pos
, __sv
.size());
2689 * @brief Find position of a character not in C substring.
2690 * @param __s C string containing characters to avoid.
2691 * @param __pos Index of character to search from.
2692 * @param __n Number of characters from __s to consider.
2693 * @return Index of first occurrence.
2695 * Starting from @a __pos, searches forward for a character not
2696 * contained in the first @a __n characters of @a __s within
2697 * this string. If found, returns the index where it was
2698 * found. If not found, returns npos.
2701 find_first_not_of(const _CharT
* __s
, size_type __pos
,
2702 size_type __n
) const _GLIBCXX_NOEXCEPT
;
2705 * @brief Find position of a character not in C string.
2706 * @param __s C string containing characters to avoid.
2707 * @param __pos Index of character to search from (default 0).
2708 * @return Index of first occurrence.
2710 * Starting from @a __pos, searches forward for a character not
2711 * contained in @a __s within this string. If found, returns
2712 * the index where it was found. If not found, returns npos.
2715 find_first_not_of(const _CharT
* __s
, size_type __pos
= 0) const
2718 __glibcxx_requires_string(__s
);
2719 return this->find_first_not_of(__s
, __pos
, traits_type::length(__s
));
2723 * @brief Find position of a different character.
2724 * @param __c Character to avoid.
2725 * @param __pos Index of character to search from (default 0).
2726 * @return Index of first occurrence.
2728 * Starting from @a __pos, searches forward for a character
2729 * other than @a __c within this string. If found, returns the
2730 * index where it was found. If not found, returns npos.
2733 find_first_not_of(_CharT __c
, size_type __pos
= 0) const
2737 * @brief Find last position of a character not in string.
2738 * @param __str String containing characters to avoid.
2739 * @param __pos Index of character to search back from (default end).
2740 * @return Index of last occurrence.
2742 * Starting from @a __pos, searches backward for a character
2743 * not contained in @a __str within this string. If found,
2744 * returns the index where it was found. If not found, returns
2748 find_last_not_of(const basic_string
& __str
, size_type __pos
= npos
) const
2750 { return this->find_last_not_of(__str
.data(), __pos
, __str
.size()); }
2752 #if __cplusplus >= 201703L
2754 * @brief Find last position of a character not in a string_view.
2755 * @param __svt An object convertible to string_view containing
2756 * characters to avoid.
2757 * @param __pos Index of character to search back from (default end).
2758 * @return Index of last occurrence.
2760 template<typename _Tp
>
2761 _If_sv
<_Tp
, size_type
>
2762 find_last_not_of(const _Tp
& __svt
, size_type __pos
= npos
) const
2763 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2765 __sv_type __sv
= __svt
;
2766 return this->find_last_not_of(__sv
.data(), __pos
, __sv
.size());
2771 * @brief Find last position of a character not in C substring.
2772 * @param __s C string containing characters to avoid.
2773 * @param __pos Index of character to search back from.
2774 * @param __n Number of characters from s to consider.
2775 * @return Index of last occurrence.
2777 * Starting from @a __pos, searches backward for a character not
2778 * contained in the first @a __n characters of @a __s within this string.
2779 * If found, returns the index where it was found. If not found,
2783 find_last_not_of(const _CharT
* __s
, size_type __pos
,
2784 size_type __n
) const _GLIBCXX_NOEXCEPT
;
2786 * @brief Find last position of a character not in C string.
2787 * @param __s C string containing characters to avoid.
2788 * @param __pos Index of character to search back from (default end).
2789 * @return Index of last occurrence.
2791 * Starting from @a __pos, searches backward for a character
2792 * not contained in @a __s within this string. If found,
2793 * returns the index where it was found. If not found, returns
2797 find_last_not_of(const _CharT
* __s
, size_type __pos
= npos
) const
2800 __glibcxx_requires_string(__s
);
2801 return this->find_last_not_of(__s
, __pos
, traits_type::length(__s
));
2805 * @brief Find last position of a different character.
2806 * @param __c Character to avoid.
2807 * @param __pos Index of character to search back from (default end).
2808 * @return Index of last occurrence.
2810 * Starting from @a __pos, searches backward for a character other than
2811 * @a __c within this string. If found, returns the index where it was
2812 * found. If not found, returns npos.
2815 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const
2819 * @brief Get a substring.
2820 * @param __pos Index of first character (default 0).
2821 * @param __n Number of characters in substring (default remainder).
2822 * @return The new string.
2823 * @throw std::out_of_range If __pos > size().
2825 * Construct and return a new string using the @a __n
2826 * characters starting at @a __pos. If the string is too
2827 * short, use the remainder of the characters. If @a __pos is
2828 * beyond the end of the string, out_of_range is thrown.
2831 substr(size_type __pos
= 0, size_type __n
= npos
) const
2832 { return basic_string(*this,
2833 _M_check(__pos
, "basic_string::substr"), __n
); }
2836 * @brief Compare to a string.
2837 * @param __str String to compare against.
2838 * @return Integer < 0, 0, or > 0.
2840 * Returns an integer < 0 if this string is ordered before @a
2841 * __str, 0 if their values are equivalent, or > 0 if this
2842 * string is ordered after @a __str. Determines the effective
2843 * length rlen of the strings to compare as the smallest of
2844 * size() and str.size(). The function then compares the two
2845 * strings by calling traits::compare(data(), str.data(),rlen).
2846 * If the result of the comparison is nonzero returns it,
2847 * otherwise the shorter one is ordered first.
2850 compare(const basic_string
& __str
) const
2852 const size_type __size
= this->size();
2853 const size_type __osize
= __str
.size();
2854 const size_type __len
= std::min(__size
, __osize
);
2856 int __r
= traits_type::compare(_M_data(), __str
.data(), __len
);
2858 __r
= _S_compare(__size
, __osize
);
2862 #if __cplusplus >= 201703L
2864 * @brief Compare to a string_view.
2865 * @param __svt An object convertible to string_view to compare against.
2866 * @return Integer < 0, 0, or > 0.
2868 template<typename _Tp
>
2870 compare(const _Tp
& __svt
) const
2871 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2873 __sv_type __sv
= __svt
;
2874 const size_type __size
= this->size();
2875 const size_type __osize
= __sv
.size();
2876 const size_type __len
= std::min(__size
, __osize
);
2878 int __r
= traits_type::compare(_M_data(), __sv
.data(), __len
);
2880 __r
= _S_compare(__size
, __osize
);
2885 * @brief Compare to a string_view.
2886 * @param __pos A position in the string to start comparing from.
2887 * @param __n The number of characters to compare.
2888 * @param __svt An object convertible to string_view to compare
2890 * @return Integer < 0, 0, or > 0.
2892 template<typename _Tp
>
2894 compare(size_type __pos
, size_type __n
, const _Tp
& __svt
) const
2895 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2897 __sv_type __sv
= __svt
;
2898 return __sv_type(*this).substr(__pos
, __n
).compare(__sv
);
2902 * @brief Compare to a string_view.
2903 * @param __pos1 A position in the string to start comparing from.
2904 * @param __n1 The number of characters to compare.
2905 * @param __svt An object convertible to string_view to compare
2907 * @param __pos2 A position in the string_view to start comparing from.
2908 * @param __n2 The number of characters to compare.
2909 * @return Integer < 0, 0, or > 0.
2911 template<typename _Tp
>
2913 compare(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
2914 size_type __pos2
, size_type __n2
= npos
) const
2915 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2917 __sv_type __sv
= __svt
;
2918 return __sv_type(*this)
2919 .substr(__pos1
, __n1
).compare(__sv
.substr(__pos2
, __n2
));
2924 * @brief Compare substring to a string.
2925 * @param __pos Index of first character of substring.
2926 * @param __n Number of characters in substring.
2927 * @param __str String to compare against.
2928 * @return Integer < 0, 0, or > 0.
2930 * Form the substring of this string from the @a __n characters
2931 * starting at @a __pos. Returns an integer < 0 if the
2932 * substring is ordered before @a __str, 0 if their values are
2933 * equivalent, or > 0 if the substring is ordered after @a
2934 * __str. Determines the effective length rlen of the strings
2935 * to compare as the smallest of the length of the substring
2936 * and @a __str.size(). The function then compares the two
2937 * strings by calling
2938 * traits::compare(substring.data(),str.data(),rlen). If the
2939 * result of the comparison is nonzero returns it, otherwise
2940 * the shorter one is ordered first.
2943 compare(size_type __pos
, size_type __n
, const basic_string
& __str
) const;
2946 * @brief Compare substring to a substring.
2947 * @param __pos1 Index of first character of substring.
2948 * @param __n1 Number of characters in substring.
2949 * @param __str String to compare against.
2950 * @param __pos2 Index of first character of substring of str.
2951 * @param __n2 Number of characters in substring of str.
2952 * @return Integer < 0, 0, or > 0.
2954 * Form the substring of this string from the @a __n1
2955 * characters starting at @a __pos1. Form the substring of @a
2956 * __str from the @a __n2 characters starting at @a __pos2.
2957 * Returns an integer < 0 if this substring is ordered before
2958 * the substring of @a __str, 0 if their values are equivalent,
2959 * or > 0 if this substring is ordered after the substring of
2960 * @a __str. Determines the effective length rlen of the
2961 * strings to compare as the smallest of the lengths of the
2962 * substrings. The function then compares the two strings by
2964 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2965 * If the result of the comparison is nonzero returns it,
2966 * otherwise the shorter one is ordered first.
2969 compare(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
2970 size_type __pos2
, size_type __n2
= npos
) const;
2973 * @brief Compare to a C string.
2974 * @param __s C string to compare against.
2975 * @return Integer < 0, 0, or > 0.
2977 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
2978 * their values are equivalent, or > 0 if this string is ordered after
2979 * @a __s. Determines the effective length rlen of the strings to
2980 * compare as the smallest of size() and the length of a string
2981 * constructed from @a __s. The function then compares the two strings
2982 * by calling traits::compare(data(),s,rlen). If the result of the
2983 * comparison is nonzero returns it, otherwise the shorter one is
2987 compare(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
;
2989 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2990 // 5 String::compare specification questionable
2992 * @brief Compare substring to a C string.
2993 * @param __pos Index of first character of substring.
2994 * @param __n1 Number of characters in substring.
2995 * @param __s C string to compare against.
2996 * @return Integer < 0, 0, or > 0.
2998 * Form the substring of this string from the @a __n1
2999 * characters starting at @a pos. Returns an integer < 0 if
3000 * the substring is ordered before @a __s, 0 if their values
3001 * are equivalent, or > 0 if the substring is ordered after @a
3002 * __s. Determines the effective length rlen of the strings to
3003 * compare as the smallest of the length of the substring and
3004 * the length of a string constructed from @a __s. The
3005 * function then compares the two string by calling
3006 * traits::compare(substring.data(),__s,rlen). If the result of
3007 * the comparison is nonzero returns it, otherwise the shorter
3008 * one is ordered first.
3011 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
) const;
3014 * @brief Compare substring against a character %array.
3015 * @param __pos Index of first character of substring.
3016 * @param __n1 Number of characters in substring.
3017 * @param __s character %array to compare against.
3018 * @param __n2 Number of characters of s.
3019 * @return Integer < 0, 0, or > 0.
3021 * Form the substring of this string from the @a __n1
3022 * characters starting at @a __pos. Form a string from the
3023 * first @a __n2 characters of @a __s. Returns an integer < 0
3024 * if this substring is ordered before the string from @a __s,
3025 * 0 if their values are equivalent, or > 0 if this substring
3026 * is ordered after the string from @a __s. Determines the
3027 * effective length rlen of the strings to compare as the
3028 * smallest of the length of the substring and @a __n2. The
3029 * function then compares the two strings by calling
3030 * traits::compare(substring.data(),s,rlen). If the result of
3031 * the comparison is nonzero returns it, otherwise the shorter
3032 * one is ordered first.
3034 * NB: s must have at least n2 characters, '\\0' has
3035 * no special meaning.
3038 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
,
3039 size_type __n2
) const;
3041 #if __cplusplus > 201703L
3043 starts_with(basic_string_view
<_CharT
, _Traits
> __x
) const noexcept
3044 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
3047 starts_with(_CharT __x
) const noexcept
3048 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
3051 starts_with(const _CharT
* __x
) const noexcept
3052 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
3055 ends_with(basic_string_view
<_CharT
, _Traits
> __x
) const noexcept
3056 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
3059 ends_with(_CharT __x
) const noexcept
3060 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
3063 ends_with(const _CharT
* __x
) const noexcept
3064 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
3067 // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
3068 template<typename
, typename
, typename
> friend class basic_stringbuf
;
3070 _GLIBCXX_END_NAMESPACE_CXX11
3071 #else // !_GLIBCXX_USE_CXX11_ABI
3072 // Reference-counted COW string implentation
3075 * @class basic_string basic_string.h <string>
3076 * @brief Managing sequences of characters and character-like objects.
3079 * @ingroup sequences
3081 * @tparam _CharT Type of character
3082 * @tparam _Traits Traits for character type, defaults to
3083 * char_traits<_CharT>.
3084 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
3086 * Meets the requirements of a <a href="tables.html#65">container</a>, a
3087 * <a href="tables.html#66">reversible container</a>, and a
3088 * <a href="tables.html#67">sequence</a>. Of the
3089 * <a href="tables.html#68">optional sequence requirements</a>, only
3090 * @c push_back, @c at, and @c %array access are supported.
3095 * Documentation? What's that?
3096 * Nathan Myers <ncm@cantrip.org>.
3098 * A string looks like this:
3103 * [basic_string<char_type>] _M_capacity
3104 * _M_dataplus _M_refcount
3105 * _M_p ----------------> unnamed array of char_type
3108 * Where the _M_p points to the first character in the string, and
3109 * you cast it to a pointer-to-_Rep and subtract 1 to get a
3110 * pointer to the header.
3112 * This approach has the enormous advantage that a string object
3113 * requires only one allocation. All the ugliness is confined
3114 * within a single %pair of inline functions, which each compile to
3115 * a single @a add instruction: _Rep::_M_data(), and
3116 * string::_M_rep(); and the allocation function which gets a
3117 * block of raw bytes and with room enough and constructs a _Rep
3118 * object at the front.
3120 * The reason you want _M_data pointing to the character %array and
3121 * not the _Rep is so that the debugger can see the string
3122 * contents. (Probably we should add a non-inline member to get
3123 * the _Rep for the debugger to use, so users can check the actual
3126 * Note that the _Rep object is a POD so that you can have a
3127 * static <em>empty string</em> _Rep object already @a constructed before
3128 * static constructors have run. The reference-count encoding is
3129 * chosen so that a 0 indicates one reference, so you never try to
3130 * destroy the empty-string _Rep object.
3132 * All but the last paragraph is considered pretty conventional
3133 * for a C++ string implementation.
3135 // 21.3 Template class basic_string
3136 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3139 typedef typename
_Alloc::template rebind
<_CharT
>::other _CharT_alloc_type
;
3143 typedef _Traits traits_type
;
3144 typedef typename
_Traits::char_type value_type
;
3145 typedef _Alloc allocator_type
;
3146 typedef typename
_CharT_alloc_type::size_type size_type
;
3147 typedef typename
_CharT_alloc_type::difference_type difference_type
;
3148 #if __cplusplus < 201103L
3149 typedef typename
_CharT_alloc_type::reference reference
;
3150 typedef typename
_CharT_alloc_type::const_reference const_reference
;
3152 typedef value_type
& reference
;
3153 typedef const value_type
& const_reference
;
3155 typedef typename
_CharT_alloc_type::pointer pointer
;
3156 typedef typename
_CharT_alloc_type::const_pointer const_pointer
;
3157 typedef __gnu_cxx::__normal_iterator
<pointer
, basic_string
> iterator
;
3158 typedef __gnu_cxx::__normal_iterator
<const_pointer
, basic_string
>
3160 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
3161 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
3164 // type used for positions in insert, erase etc.
3165 typedef iterator __const_iterator
;
3168 // _Rep: string representation
3170 // 1. String really contains _M_length + 1 characters: due to 21.3.4
3171 // must be kept null-terminated.
3172 // 2. _M_capacity >= _M_length
3173 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
3174 // 3. _M_refcount has three states:
3175 // -1: leaked, one reference, no ref-copies allowed, non-const.
3176 // 0: one reference, non-const.
3177 // n>0: n + 1 references, operations require a lock, const.
3178 // 4. All fields==0 is an empty string, given the extra storage
3179 // beyond-the-end for a null terminator; thus, the shared
3180 // empty string representation needs no constructor.
3184 size_type _M_length
;
3185 size_type _M_capacity
;
3186 _Atomic_word _M_refcount
;
3189 struct _Rep
: _Rep_base
3192 typedef typename
_Alloc::template rebind
<char>::other _Raw_bytes_alloc
;
3194 // (Public) Data members:
3196 // The maximum number of individual char_type elements of an
3197 // individual string is determined by _S_max_size. This is the
3198 // value that will be returned by max_size(). (Whereas npos
3199 // is the maximum number of bytes the allocator can allocate.)
3200 // If one was to divvy up the theoretical largest size string,
3201 // with a terminating character and m _CharT elements, it'd
3203 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
3205 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
3206 // In addition, this implementation quarters this amount.
3207 static const size_type _S_max_size
;
3208 static const _CharT _S_terminal
;
3210 // The following storage is init'd to 0 by the linker, resulting
3211 // (carefully) in an empty string with one reference.
3212 static size_type _S_empty_rep_storage
[];
3215 _S_empty_rep() _GLIBCXX_NOEXCEPT
3217 // NB: Mild hack to avoid strict-aliasing warnings. Note that
3218 // _S_empty_rep_storage is never modified and the punning should
3219 // be reasonably safe in this case.
3220 void* __p
= reinterpret_cast<void*>(&_S_empty_rep_storage
);
3221 return *reinterpret_cast<_Rep
*>(__p
);
3225 _M_is_leaked() const _GLIBCXX_NOEXCEPT
3227 #if defined(__GTHREADS)
3228 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3229 // so we need to use an atomic load. However, _M_is_leaked
3230 // predicate does not change concurrently (i.e. the string is either
3231 // leaked or not), so a relaxed load is enough.
3232 return __atomic_load_n(&this->_M_refcount
, __ATOMIC_RELAXED
) < 0;
3234 return this->_M_refcount
< 0;
3239 _M_is_shared() const _GLIBCXX_NOEXCEPT
3241 #if defined(__GTHREADS)
3242 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3243 // so we need to use an atomic load. Another thread can drop last
3244 // but one reference concurrently with this check, so we need this
3245 // load to be acquire to synchronize with release fetch_and_add in
3247 return __atomic_load_n(&this->_M_refcount
, __ATOMIC_ACQUIRE
) > 0;
3249 return this->_M_refcount
> 0;
3254 _M_set_leaked() _GLIBCXX_NOEXCEPT
3255 { this->_M_refcount
= -1; }
3258 _M_set_sharable() _GLIBCXX_NOEXCEPT
3259 { this->_M_refcount
= 0; }
3262 _M_set_length_and_sharable(size_type __n
) _GLIBCXX_NOEXCEPT
3264 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3265 if (__builtin_expect(this != &_S_empty_rep(), false))
3268 this->_M_set_sharable(); // One reference.
3269 this->_M_length
= __n
;
3270 traits_type::assign(this->_M_refdata()[__n
], _S_terminal
);
3271 // grrr. (per 21.3.4)
3272 // You cannot leave those LWG people alone for a second.
3277 _M_refdata() throw()
3278 { return reinterpret_cast<_CharT
*>(this + 1); }
3281 _M_grab(const _Alloc
& __alloc1
, const _Alloc
& __alloc2
)
3283 return (!_M_is_leaked() && __alloc1
== __alloc2
)
3284 ? _M_refcopy() : _M_clone(__alloc1
);
3289 _S_create(size_type
, size_type
, const _Alloc
&);
3292 _M_dispose(const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
3294 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3295 if (__builtin_expect(this != &_S_empty_rep(), false))
3298 // Be race-detector-friendly. For more info see bits/c++config.
3299 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount
);
3300 // Decrement of _M_refcount is acq_rel, because:
3301 // - all but last decrements need to release to synchronize with
3302 // the last decrement that will delete the object.
3303 // - the last decrement needs to acquire to synchronize with
3304 // all the previous decrements.
3305 // - last but one decrement needs to release to synchronize with
3306 // the acquire load in _M_is_shared that will conclude that
3307 // the object is not shared anymore.
3308 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount
,
3311 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount
);
3318 _M_destroy(const _Alloc
&) throw();
3321 _M_refcopy() throw()
3323 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3324 if (__builtin_expect(this != &_S_empty_rep(), false))
3326 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount
, 1);
3327 return _M_refdata();
3331 _M_clone(const _Alloc
&, size_type __res
= 0);
3334 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
3335 struct _Alloc_hider
: _Alloc
3337 _Alloc_hider(_CharT
* __dat
, const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
3338 : _Alloc(__a
), _M_p(__dat
) { }
3340 _CharT
* _M_p
; // The actual data.
3344 // Data Members (public):
3345 // NB: This is an unsigned type, and thus represents the maximum
3346 // size that the allocator can hold.
3347 /// Value returned by various member functions when they fail.
3348 static const size_type npos
= static_cast<size_type
>(-1);
3351 // Data Members (private):
3352 mutable _Alloc_hider _M_dataplus
;
3355 _M_data() const _GLIBCXX_NOEXCEPT
3356 { return _M_dataplus
._M_p
; }
3359 _M_data(_CharT
* __p
) _GLIBCXX_NOEXCEPT
3360 { return (_M_dataplus
._M_p
= __p
); }
3363 _M_rep() const _GLIBCXX_NOEXCEPT
3364 { return &((reinterpret_cast<_Rep
*> (_M_data()))[-1]); }
3366 // For the internal use we have functions similar to `begin'/`end'
3367 // but they do not call _M_leak.
3369 _M_ibegin() const _GLIBCXX_NOEXCEPT
3370 { return iterator(_M_data()); }
3373 _M_iend() const _GLIBCXX_NOEXCEPT
3374 { return iterator(_M_data() + this->size()); }
3377 _M_leak() // for use in begin() & non-const op[]
3379 if (!_M_rep()->_M_is_leaked())
3384 _M_check(size_type __pos
, const char* __s
) const
3386 if (__pos
> this->size())
3387 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
3388 "this->size() (which is %zu)"),
3389 __s
, __pos
, this->size());
3394 _M_check_length(size_type __n1
, size_type __n2
, const char* __s
) const
3396 if (this->max_size() - (this->size() - __n1
) < __n2
)
3397 __throw_length_error(__N(__s
));
3400 // NB: _M_limit doesn't check for a bad __pos value.
3402 _M_limit(size_type __pos
, size_type __off
) const _GLIBCXX_NOEXCEPT
3404 const bool __testoff
= __off
< this->size() - __pos
;
3405 return __testoff
? __off
: this->size() - __pos
;
3408 // True if _Rep and source do not overlap.
3410 _M_disjunct(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
3412 return (less
<const _CharT
*>()(__s
, _M_data())
3413 || less
<const _CharT
*>()(_M_data() + this->size(), __s
));
3416 // When __n = 1 way faster than the general multichar
3417 // traits_type::copy/move/assign.
3419 _M_copy(_CharT
* __d
, const _CharT
* __s
, size_type __n
) _GLIBCXX_NOEXCEPT
3422 traits_type::assign(*__d
, *__s
);
3424 traits_type::copy(__d
, __s
, __n
);
3428 _M_move(_CharT
* __d
, const _CharT
* __s
, size_type __n
) _GLIBCXX_NOEXCEPT
3431 traits_type::assign(*__d
, *__s
);
3433 traits_type::move(__d
, __s
, __n
);
3437 _M_assign(_CharT
* __d
, size_type __n
, _CharT __c
) _GLIBCXX_NOEXCEPT
3440 traits_type::assign(*__d
, __c
);
3442 traits_type::assign(__d
, __n
, __c
);
3445 // _S_copy_chars is a separate template to permit specialization
3446 // to optimize for the common case of pointers as iterators.
3447 template<class _Iterator
>
3449 _S_copy_chars(_CharT
* __p
, _Iterator __k1
, _Iterator __k2
)
3451 for (; __k1
!= __k2
; ++__k1
, (void)++__p
)
3452 traits_type::assign(*__p
, *__k1
); // These types are off.
3456 _S_copy_chars(_CharT
* __p
, iterator __k1
, iterator __k2
) _GLIBCXX_NOEXCEPT
3457 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
3460 _S_copy_chars(_CharT
* __p
, const_iterator __k1
, const_iterator __k2
)
3462 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
3465 _S_copy_chars(_CharT
* __p
, _CharT
* __k1
, _CharT
* __k2
) _GLIBCXX_NOEXCEPT
3466 { _M_copy(__p
, __k1
, __k2
- __k1
); }
3469 _S_copy_chars(_CharT
* __p
, const _CharT
* __k1
, const _CharT
* __k2
)
3471 { _M_copy(__p
, __k1
, __k2
- __k1
); }
3474 _S_compare(size_type __n1
, size_type __n2
) _GLIBCXX_NOEXCEPT
3476 const difference_type __d
= difference_type(__n1
- __n2
);
3478 if (__d
> __gnu_cxx::__numeric_traits
<int>::__max
)
3479 return __gnu_cxx::__numeric_traits
<int>::__max
;
3480 else if (__d
< __gnu_cxx::__numeric_traits
<int>::__min
)
3481 return __gnu_cxx::__numeric_traits
<int>::__min
;
3487 _M_mutate(size_type __pos
, size_type __len1
, size_type __len2
);
3493 _S_empty_rep() _GLIBCXX_NOEXCEPT
3494 { return _Rep::_S_empty_rep(); }
3496 #if __cplusplus >= 201703L
3497 // A helper type for avoiding boiler-plate.
3498 typedef basic_string_view
<_CharT
, _Traits
> __sv_type
;
3500 template<typename _Tp
, typename _Res
>
3501 using _If_sv
= enable_if_t
<
3502 __and_
<is_convertible
<const _Tp
&, __sv_type
>,
3503 __not_
<is_convertible
<const _Tp
*, const basic_string
*>>,
3504 __not_
<is_convertible
<const _Tp
&, const _CharT
*>>>::value
,
3507 // Allows an implicit conversion to __sv_type.
3509 _S_to_string_view(__sv_type __svt
) noexcept
3512 // Wraps a string_view by explicit conversion and thus
3513 // allows to add an internal constructor that does not
3514 // participate in overload resolution when a string_view
3518 explicit __sv_wrapper(__sv_type __sv
) noexcept
: _M_sv(__sv
) { }
3523 * @brief Only internally used: Construct string from a string view
3525 * @param __svw string view wrapper.
3526 * @param __a Allocator to use.
3529 basic_string(__sv_wrapper __svw
, const _Alloc
& __a
)
3530 : basic_string(__svw
._M_sv
.data(), __svw
._M_sv
.size(), __a
) { }
3534 // Construct/copy/destroy:
3535 // NB: We overload ctors in some cases instead of using default
3536 // arguments, per 17.4.4.4 para. 2 item 2.
3539 * @brief Default constructor creates an empty string.
3542 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3544 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc())
3546 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc())
3551 * @brief Construct an empty string using allocator @a a.
3554 basic_string(const _Alloc
& __a
);
3556 // NB: per LWG issue 42, semantics different from IS:
3558 * @brief Construct string with copy of value of @a str.
3559 * @param __str Source string.
3561 basic_string(const basic_string
& __str
);
3563 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3564 // 2583. no way to supply an allocator for basic_string(str, pos)
3566 * @brief Construct string as copy of a substring.
3567 * @param __str Source string.
3568 * @param __pos Index of first character to copy from.
3569 * @param __a Allocator to use.
3571 basic_string(const basic_string
& __str
, size_type __pos
,
3572 const _Alloc
& __a
= _Alloc());
3575 * @brief Construct string as copy of a substring.
3576 * @param __str Source string.
3577 * @param __pos Index of first character to copy from.
3578 * @param __n Number of characters to copy.
3580 basic_string(const basic_string
& __str
, size_type __pos
,
3583 * @brief Construct string as copy of a substring.
3584 * @param __str Source string.
3585 * @param __pos Index of first character to copy from.
3586 * @param __n Number of characters to copy.
3587 * @param __a Allocator to use.
3589 basic_string(const basic_string
& __str
, size_type __pos
,
3590 size_type __n
, const _Alloc
& __a
);
3593 * @brief Construct string initialized by a character %array.
3594 * @param __s Source character %array.
3595 * @param __n Number of characters to copy.
3596 * @param __a Allocator to use (default is default allocator).
3598 * NB: @a __s must have at least @a __n characters, '\\0'
3599 * has no special meaning.
3601 basic_string(const _CharT
* __s
, size_type __n
,
3602 const _Alloc
& __a
= _Alloc());
3604 * @brief Construct string as copy of a C string.
3605 * @param __s Source C string.
3606 * @param __a Allocator to use (default is default allocator).
3608 basic_string(const _CharT
* __s
, const _Alloc
& __a
= _Alloc());
3610 * @brief Construct string as multiple characters.
3611 * @param __n Number of characters.
3612 * @param __c Character to use.
3613 * @param __a Allocator to use (default is default allocator).
3615 basic_string(size_type __n
, _CharT __c
, const _Alloc
& __a
= _Alloc());
3617 #if __cplusplus >= 201103L
3619 * @brief Move construct string.
3620 * @param __str Source string.
3622 * The newly-created string contains the exact contents of @a __str.
3623 * @a __str is a valid, but unspecified string.
3625 basic_string(basic_string
&& __str
)
3626 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3627 noexcept
// FIXME C++11: should always be noexcept.
3629 : _M_dataplus(std::move(__str
._M_dataplus
))
3631 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3632 __str
._M_data(_S_empty_rep()._M_refdata());
3634 __str
._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
3639 * @brief Construct string from an initializer %list.
3640 * @param __l std::initializer_list of characters.
3641 * @param __a Allocator to use (default is default allocator).
3643 basic_string(initializer_list
<_CharT
> __l
, const _Alloc
& __a
= _Alloc());
3645 basic_string(const basic_string
& __str
, const _Alloc
& __a
)
3646 : _M_dataplus(__str
._M_rep()->_M_grab(__a
, __str
.get_allocator()), __a
)
3649 basic_string(basic_string
&& __str
, const _Alloc
& __a
)
3650 : _M_dataplus(__str
._M_data(), __a
)
3652 if (__a
== __str
.get_allocator())
3654 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3655 __str
._M_data(_S_empty_rep()._M_refdata());
3657 __str
._M_data(_S_construct(size_type(), _CharT(), __a
));
3661 _M_dataplus
._M_p
= _S_construct(__str
.begin(), __str
.end(), __a
);
3666 * @brief Construct string as copy of a range.
3667 * @param __beg Start of range.
3668 * @param __end End of range.
3669 * @param __a Allocator to use (default is default allocator).
3671 template<class _InputIterator
>
3672 basic_string(_InputIterator __beg
, _InputIterator __end
,
3673 const _Alloc
& __a
= _Alloc());
3675 #if __cplusplus >= 201703L
3677 * @brief Construct string from a substring of a string_view.
3678 * @param __t Source object convertible to string view.
3679 * @param __pos The index of the first character to copy from __t.
3680 * @param __n The number of characters to copy from __t.
3681 * @param __a Allocator to use.
3683 template<typename _Tp
, typename
= _If_sv
<_Tp
, void>>
3684 basic_string(const _Tp
& __t
, size_type __pos
, size_type __n
,
3685 const _Alloc
& __a
= _Alloc())
3686 : basic_string(_S_to_string_view(__t
).substr(__pos
, __n
), __a
) { }
3689 * @brief Construct string from a string_view.
3690 * @param __t Source object convertible to string view.
3691 * @param __a Allocator to use (default is default allocator).
3693 template<typename _Tp
, typename
= _If_sv
<_Tp
, void>>
3695 basic_string(const _Tp
& __t
, const _Alloc
& __a
= _Alloc())
3696 : basic_string(__sv_wrapper(_S_to_string_view(__t
)), __a
) { }
3700 * @brief Destroy the string instance.
3702 ~basic_string() _GLIBCXX_NOEXCEPT
3703 { _M_rep()->_M_dispose(this->get_allocator()); }
3706 * @brief Assign the value of @a str to this string.
3707 * @param __str Source string.
3710 operator=(const basic_string
& __str
)
3711 { return this->assign(__str
); }
3714 * @brief Copy contents of @a s into this string.
3715 * @param __s Source null-terminated string.
3718 operator=(const _CharT
* __s
)
3719 { return this->assign(__s
); }
3722 * @brief Set value to string of length 1.
3723 * @param __c Source character.
3725 * Assigning to a character makes this string length 1 and
3726 * (*this)[0] == @a c.
3729 operator=(_CharT __c
)
3731 this->assign(1, __c
);
3735 #if __cplusplus >= 201103L
3737 * @brief Move assign the value of @a str to this string.
3738 * @param __str Source string.
3740 * The contents of @a str are moved into this string (without copying).
3741 * @a str is a valid, but unspecified string.
3744 operator=(basic_string
&& __str
)
3745 _GLIBCXX_NOEXCEPT_IF(allocator_traits
<_Alloc
>::is_always_equal::value
)
3753 * @brief Set value to string constructed from initializer %list.
3754 * @param __l std::initializer_list.
3757 operator=(initializer_list
<_CharT
> __l
)
3759 this->assign(__l
.begin(), __l
.size());
3764 #if __cplusplus >= 201703L
3766 * @brief Set value to string constructed from a string_view.
3767 * @param __svt An object convertible to string_view.
3769 template<typename _Tp
>
3770 _If_sv
<_Tp
, basic_string
&>
3771 operator=(const _Tp
& __svt
)
3772 { return this->assign(__svt
); }
3775 * @brief Convert to a string_view.
3776 * @return A string_view.
3778 operator __sv_type() const noexcept
3779 { return __sv_type(data(), size()); }
3784 * Returns a read/write iterator that points to the first character in
3785 * the %string. Unshares the string.
3788 begin() // FIXME C++11: should be noexcept.
3791 return iterator(_M_data());
3795 * Returns a read-only (constant) iterator that points to the first
3796 * character in the %string.
3799 begin() const _GLIBCXX_NOEXCEPT
3800 { return const_iterator(_M_data()); }
3803 * Returns a read/write iterator that points one past the last
3804 * character in the %string. Unshares the string.
3807 end() // FIXME C++11: should be noexcept.
3810 return iterator(_M_data() + this->size());
3814 * Returns a read-only (constant) iterator that points one past the
3815 * last character in the %string.
3818 end() const _GLIBCXX_NOEXCEPT
3819 { return const_iterator(_M_data() + this->size()); }
3822 * Returns a read/write reverse iterator that points to the last
3823 * character in the %string. Iteration is done in reverse element
3824 * order. Unshares the string.
3827 rbegin() // FIXME C++11: should be noexcept.
3828 { return reverse_iterator(this->end()); }
3831 * Returns a read-only (constant) reverse iterator that points
3832 * to the last character in the %string. Iteration is done in
3833 * reverse element order.
3835 const_reverse_iterator
3836 rbegin() const _GLIBCXX_NOEXCEPT
3837 { return const_reverse_iterator(this->end()); }
3840 * Returns a read/write reverse iterator that points to one before the
3841 * first character in the %string. Iteration is done in reverse
3842 * element order. Unshares the string.
3845 rend() // FIXME C++11: should be noexcept.
3846 { return reverse_iterator(this->begin()); }
3849 * Returns a read-only (constant) reverse iterator that points
3850 * to one before the first character in the %string. Iteration
3851 * is done in reverse element order.
3853 const_reverse_iterator
3854 rend() const _GLIBCXX_NOEXCEPT
3855 { return const_reverse_iterator(this->begin()); }
3857 #if __cplusplus >= 201103L
3859 * Returns a read-only (constant) iterator that points to the first
3860 * character in the %string.
3863 cbegin() const noexcept
3864 { return const_iterator(this->_M_data()); }
3867 * Returns a read-only (constant) iterator that points one past the
3868 * last character in the %string.
3871 cend() const noexcept
3872 { return const_iterator(this->_M_data() + this->size()); }
3875 * Returns a read-only (constant) reverse iterator that points
3876 * to the last character in the %string. Iteration is done in
3877 * reverse element order.
3879 const_reverse_iterator
3880 crbegin() const noexcept
3881 { return const_reverse_iterator(this->end()); }
3884 * Returns a read-only (constant) reverse iterator that points
3885 * to one before the first character in the %string. Iteration
3886 * is done in reverse element order.
3888 const_reverse_iterator
3889 crend() const noexcept
3890 { return const_reverse_iterator(this->begin()); }
3895 /// Returns the number of characters in the string, not including any
3896 /// null-termination.
3898 size() const _GLIBCXX_NOEXCEPT
3899 { return _M_rep()->_M_length
; }
3901 /// Returns the number of characters in the string, not including any
3902 /// null-termination.
3904 length() const _GLIBCXX_NOEXCEPT
3905 { return _M_rep()->_M_length
; }
3907 /// Returns the size() of the largest possible %string.
3909 max_size() const _GLIBCXX_NOEXCEPT
3910 { return _Rep::_S_max_size
; }
3913 * @brief Resizes the %string to the specified number of characters.
3914 * @param __n Number of characters the %string should contain.
3915 * @param __c Character to fill any new elements.
3917 * This function will %resize the %string to the specified
3918 * number of characters. If the number is smaller than the
3919 * %string's current size the %string is truncated, otherwise
3920 * the %string is extended and new elements are %set to @a __c.
3923 resize(size_type __n
, _CharT __c
);
3926 * @brief Resizes the %string to the specified number of characters.
3927 * @param __n Number of characters the %string should contain.
3929 * This function will resize the %string to the specified length. If
3930 * the new size is smaller than the %string's current size the %string
3931 * is truncated, otherwise the %string is extended and new characters
3932 * are default-constructed. For basic types such as char, this means
3933 * setting them to 0.
3936 resize(size_type __n
)
3937 { this->resize(__n
, _CharT()); }
3939 #if __cplusplus >= 201103L
3940 /// A non-binding request to reduce capacity() to size().
3942 shrink_to_fit() _GLIBCXX_NOEXCEPT
3944 #if __cpp_exceptions
3945 if (capacity() > size())
3957 * Returns the total number of characters that the %string can hold
3958 * before needing to allocate more memory.
3961 capacity() const _GLIBCXX_NOEXCEPT
3962 { return _M_rep()->_M_capacity
; }
3965 * @brief Attempt to preallocate enough memory for specified number of
3967 * @param __res_arg Number of characters required.
3968 * @throw std::length_error If @a __res_arg exceeds @c max_size().
3970 * This function attempts to reserve enough memory for the
3971 * %string to hold the specified number of characters. If the
3972 * number requested is more than max_size(), length_error is
3975 * The advantage of this function is that if optimal code is a
3976 * necessity and the user can determine the string length that will be
3977 * required, the user can reserve the memory in %advance, and thus
3978 * prevent a possible reallocation of memory and copying of %string
3982 reserve(size_type __res_arg
= 0);
3985 * Erases the string, making it empty.
3987 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3989 clear() _GLIBCXX_NOEXCEPT
3991 if (_M_rep()->_M_is_shared())
3993 _M_rep()->_M_dispose(this->get_allocator());
3994 _M_data(_S_empty_rep()._M_refdata());
3997 _M_rep()->_M_set_length_and_sharable(0);
4000 // PR 56166: this should not throw.
4003 { _M_mutate(0, this->size(), 0); }
4007 * Returns true if the %string is empty. Equivalent to
4008 * <code>*this == ""</code>.
4010 _GLIBCXX_NODISCARD
bool
4011 empty() const _GLIBCXX_NOEXCEPT
4012 { return this->size() == 0; }
4016 * @brief Subscript access to the data contained in the %string.
4017 * @param __pos The index of the character to access.
4018 * @return Read-only (constant) reference to the character.
4020 * This operator allows for easy, array-style, data access.
4021 * Note that data access with this operator is unchecked and
4022 * out_of_range lookups are not defined. (For checked lookups
4026 operator[] (size_type __pos
) const _GLIBCXX_NOEXCEPT
4028 __glibcxx_assert(__pos
<= size());
4029 return _M_data()[__pos
];
4033 * @brief Subscript access to the data contained in the %string.
4034 * @param __pos The index of the character to access.
4035 * @return Read/write reference to the character.
4037 * This operator allows for easy, array-style, data access.
4038 * Note that data access with this operator is unchecked and
4039 * out_of_range lookups are not defined. (For checked lookups
4040 * see at().) Unshares the string.
4043 operator[](size_type __pos
)
4045 // Allow pos == size() both in C++98 mode, as v3 extension,
4046 // and in C++11 mode.
4047 __glibcxx_assert(__pos
<= size());
4048 // In pedantic mode be strict in C++98 mode.
4049 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus
>= 201103L || __pos
< size());
4051 return _M_data()[__pos
];
4055 * @brief Provides access to the data contained in the %string.
4056 * @param __n The index of the character to access.
4057 * @return Read-only (const) reference to the character.
4058 * @throw std::out_of_range If @a n is an invalid index.
4060 * This function provides for safer data access. The parameter is
4061 * first checked that it is in the range of the string. The function
4062 * throws out_of_range if the check fails.
4065 at(size_type __n
) const
4067 if (__n
>= this->size())
4068 __throw_out_of_range_fmt(__N("basic_string::at: __n "
4069 "(which is %zu) >= this->size() "
4072 return _M_data()[__n
];
4076 * @brief Provides access to the data contained in the %string.
4077 * @param __n The index of the character to access.
4078 * @return Read/write reference to the character.
4079 * @throw std::out_of_range If @a n is an invalid index.
4081 * This function provides for safer data access. The parameter is
4082 * first checked that it is in the range of the string. The function
4083 * throws out_of_range if the check fails. Success results in
4084 * unsharing the string.
4090 __throw_out_of_range_fmt(__N("basic_string::at: __n "
4091 "(which is %zu) >= this->size() "
4095 return _M_data()[__n
];
4098 #if __cplusplus >= 201103L
4100 * Returns a read/write reference to the data at the first
4101 * element of the %string.
4106 __glibcxx_assert(!empty());
4107 return operator[](0);
4111 * Returns a read-only (constant) reference to the data at the first
4112 * element of the %string.
4115 front() const noexcept
4117 __glibcxx_assert(!empty());
4118 return operator[](0);
4122 * Returns a read/write reference to the data at the last
4123 * element of the %string.
4128 __glibcxx_assert(!empty());
4129 return operator[](this->size() - 1);
4133 * Returns a read-only (constant) reference to the data at the
4134 * last element of the %string.
4137 back() const noexcept
4139 __glibcxx_assert(!empty());
4140 return operator[](this->size() - 1);
4146 * @brief Append a string to this string.
4147 * @param __str The string to append.
4148 * @return Reference to this string.
4151 operator+=(const basic_string
& __str
)
4152 { return this->append(__str
); }
4155 * @brief Append a C string.
4156 * @param __s The C string to append.
4157 * @return Reference to this string.
4160 operator+=(const _CharT
* __s
)
4161 { return this->append(__s
); }
4164 * @brief Append a character.
4165 * @param __c The character to append.
4166 * @return Reference to this string.
4169 operator+=(_CharT __c
)
4171 this->push_back(__c
);
4175 #if __cplusplus >= 201103L
4177 * @brief Append an initializer_list of characters.
4178 * @param __l The initializer_list of characters to be appended.
4179 * @return Reference to this string.
4182 operator+=(initializer_list
<_CharT
> __l
)
4183 { return this->append(__l
.begin(), __l
.size()); }
4186 #if __cplusplus >= 201703L
4188 * @brief Append a string_view.
4189 * @param __svt The object convertible to string_view to be appended.
4190 * @return Reference to this string.
4192 template<typename _Tp
>
4193 _If_sv
<_Tp
, basic_string
&>
4194 operator+=(const _Tp
& __svt
)
4195 { return this->append(__svt
); }
4199 * @brief Append a string to this string.
4200 * @param __str The string to append.
4201 * @return Reference to this string.
4204 append(const basic_string
& __str
);
4207 * @brief Append a substring.
4208 * @param __str The string to append.
4209 * @param __pos Index of the first character of str to append.
4210 * @param __n The number of characters to append.
4211 * @return Reference to this string.
4212 * @throw std::out_of_range if @a __pos is not a valid index.
4214 * This function appends @a __n characters from @a __str
4215 * starting at @a __pos to this string. If @a __n is is larger
4216 * than the number of available characters in @a __str, the
4217 * remainder of @a __str is appended.
4220 append(const basic_string
& __str
, size_type __pos
, size_type __n
= npos
);
4223 * @brief Append a C substring.
4224 * @param __s The C string to append.
4225 * @param __n The number of characters to append.
4226 * @return Reference to this string.
4229 append(const _CharT
* __s
, size_type __n
);
4232 * @brief Append a C string.
4233 * @param __s The C string to append.
4234 * @return Reference to this string.
4237 append(const _CharT
* __s
)
4239 __glibcxx_requires_string(__s
);
4240 return this->append(__s
, traits_type::length(__s
));
4244 * @brief Append multiple characters.
4245 * @param __n The number of characters to append.
4246 * @param __c The character to use.
4247 * @return Reference to this string.
4249 * Appends __n copies of __c to this string.
4252 append(size_type __n
, _CharT __c
);
4254 #if __cplusplus >= 201103L
4256 * @brief Append an initializer_list of characters.
4257 * @param __l The initializer_list of characters to append.
4258 * @return Reference to this string.
4261 append(initializer_list
<_CharT
> __l
)
4262 { return this->append(__l
.begin(), __l
.size()); }
4266 * @brief Append a range of characters.
4267 * @param __first Iterator referencing the first character to append.
4268 * @param __last Iterator marking the end of the range.
4269 * @return Reference to this string.
4271 * Appends characters in the range [__first,__last) to this string.
4273 template<class _InputIterator
>
4275 append(_InputIterator __first
, _InputIterator __last
)
4276 { return this->replace(_M_iend(), _M_iend(), __first
, __last
); }
4278 #if __cplusplus >= 201703L
4280 * @brief Append a string_view.
4281 * @param __svt The object convertible to string_view to be appended.
4282 * @return Reference to this string.
4284 template<typename _Tp
>
4285 _If_sv
<_Tp
, basic_string
&>
4286 append(const _Tp
& __svt
)
4288 __sv_type __sv
= __svt
;
4289 return this->append(__sv
.data(), __sv
.size());
4293 * @brief Append a range of characters from a string_view.
4294 * @param __svt The object convertible to string_view to be appended
4296 * @param __pos The position in the string_view to append from.
4297 * @param __n The number of characters to append from the string_view.
4298 * @return Reference to this string.
4300 template<typename _Tp
>
4301 _If_sv
<_Tp
, basic_string
&>
4302 append(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
4304 __sv_type __sv
= __svt
;
4305 return append(__sv
.data()
4306 + __sv
._M_check(__pos
, "basic_string::append"),
4307 __sv
._M_limit(__pos
, __n
));
4312 * @brief Append a single character.
4313 * @param __c Character to append.
4316 push_back(_CharT __c
)
4318 const size_type __len
= 1 + this->size();
4319 if (__len
> this->capacity() || _M_rep()->_M_is_shared())
4320 this->reserve(__len
);
4321 traits_type::assign(_M_data()[this->size()], __c
);
4322 _M_rep()->_M_set_length_and_sharable(__len
);
4326 * @brief Set value to contents of another string.
4327 * @param __str Source string to use.
4328 * @return Reference to this string.
4331 assign(const basic_string
& __str
);
4333 #if __cplusplus >= 201103L
4335 * @brief Set value to contents of another string.
4336 * @param __str Source string to use.
4337 * @return Reference to this string.
4339 * This function sets this string to the exact contents of @a __str.
4340 * @a __str is a valid, but unspecified string.
4343 assign(basic_string
&& __str
)
4344 noexcept(allocator_traits
<_Alloc
>::is_always_equal::value
)
4352 * @brief Set value to a substring of a string.
4353 * @param __str The string to use.
4354 * @param __pos Index of the first character of str.
4355 * @param __n Number of characters to use.
4356 * @return Reference to this string.
4357 * @throw std::out_of_range if @a pos is not a valid index.
4359 * This function sets this string to the substring of @a __str
4360 * consisting of @a __n characters at @a __pos. If @a __n is
4361 * is larger than the number of available characters in @a
4362 * __str, the remainder of @a __str is used.
4365 assign(const basic_string
& __str
, size_type __pos
, size_type __n
= npos
)
4366 { return this->assign(__str
._M_data()
4367 + __str
._M_check(__pos
, "basic_string::assign"),
4368 __str
._M_limit(__pos
, __n
)); }
4371 * @brief Set value to a C substring.
4372 * @param __s The C string to use.
4373 * @param __n Number of characters to use.
4374 * @return Reference to this string.
4376 * This function sets the value of this string to the first @a __n
4377 * characters of @a __s. If @a __n is is larger than the number of
4378 * available characters in @a __s, the remainder of @a __s is used.
4381 assign(const _CharT
* __s
, size_type __n
);
4384 * @brief Set value to contents of a C string.
4385 * @param __s The C string to use.
4386 * @return Reference to this string.
4388 * This function sets the value of this string to the value of @a __s.
4389 * The data is copied, so there is no dependence on @a __s once the
4393 assign(const _CharT
* __s
)
4395 __glibcxx_requires_string(__s
);
4396 return this->assign(__s
, traits_type::length(__s
));
4400 * @brief Set value to multiple characters.
4401 * @param __n Length of the resulting string.
4402 * @param __c The character to use.
4403 * @return Reference to this string.
4405 * This function sets the value of this string to @a __n copies of
4409 assign(size_type __n
, _CharT __c
)
4410 { return _M_replace_aux(size_type(0), this->size(), __n
, __c
); }
4413 * @brief Set value to a range of characters.
4414 * @param __first Iterator referencing the first character to append.
4415 * @param __last Iterator marking the end of the range.
4416 * @return Reference to this string.
4418 * Sets value of string to characters in the range [__first,__last).
4420 template<class _InputIterator
>
4422 assign(_InputIterator __first
, _InputIterator __last
)
4423 { return this->replace(_M_ibegin(), _M_iend(), __first
, __last
); }
4425 #if __cplusplus >= 201103L
4427 * @brief Set value to an initializer_list of characters.
4428 * @param __l The initializer_list of characters to assign.
4429 * @return Reference to this string.
4432 assign(initializer_list
<_CharT
> __l
)
4433 { return this->assign(__l
.begin(), __l
.size()); }
4436 #if __cplusplus >= 201703L
4438 * @brief Set value from a string_view.
4439 * @param __svt The source object convertible to string_view.
4440 * @return Reference to this string.
4442 template<typename _Tp
>
4443 _If_sv
<_Tp
, basic_string
&>
4444 assign(const _Tp
& __svt
)
4446 __sv_type __sv
= __svt
;
4447 return this->assign(__sv
.data(), __sv
.size());
4451 * @brief Set value from a range of characters in a string_view.
4452 * @param __svt The source object convertible to string_view.
4453 * @param __pos The position in the string_view to assign from.
4454 * @param __n The number of characters to assign.
4455 * @return Reference to this string.
4457 template<typename _Tp
>
4458 _If_sv
<_Tp
, basic_string
&>
4459 assign(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
4461 __sv_type __sv
= __svt
;
4462 return assign(__sv
.data()
4463 + __sv
._M_check(__pos
, "basic_string::assign"),
4464 __sv
._M_limit(__pos
, __n
));
4469 * @brief Insert multiple characters.
4470 * @param __p Iterator referencing location in string to insert at.
4471 * @param __n Number of characters to insert
4472 * @param __c The character to insert.
4473 * @throw std::length_error If new length exceeds @c max_size().
4475 * Inserts @a __n copies of character @a __c starting at the
4476 * position referenced by iterator @a __p. If adding
4477 * characters causes the length to exceed max_size(),
4478 * length_error is thrown. The value of the string doesn't
4479 * change if an error is thrown.
4482 insert(iterator __p
, size_type __n
, _CharT __c
)
4483 { this->replace(__p
, __p
, __n
, __c
); }
4486 * @brief Insert a range of characters.
4487 * @param __p Iterator referencing location in string to insert at.
4488 * @param __beg Start of range.
4489 * @param __end End of range.
4490 * @throw std::length_error If new length exceeds @c max_size().
4492 * Inserts characters in range [__beg,__end). If adding
4493 * characters causes the length to exceed max_size(),
4494 * length_error is thrown. The value of the string doesn't
4495 * change if an error is thrown.
4497 template<class _InputIterator
>
4499 insert(iterator __p
, _InputIterator __beg
, _InputIterator __end
)
4500 { this->replace(__p
, __p
, __beg
, __end
); }
4502 #if __cplusplus >= 201103L
4504 * @brief Insert an initializer_list of characters.
4505 * @param __p Iterator referencing location in string to insert at.
4506 * @param __l The initializer_list of characters to insert.
4507 * @throw std::length_error If new length exceeds @c max_size().
4510 insert(iterator __p
, initializer_list
<_CharT
> __l
)
4512 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
4513 this->insert(__p
- _M_ibegin(), __l
.begin(), __l
.size());
4518 * @brief Insert value of a string.
4519 * @param __pos1 Iterator referencing location in string to insert at.
4520 * @param __str The string to insert.
4521 * @return Reference to this string.
4522 * @throw std::length_error If new length exceeds @c max_size().
4524 * Inserts value of @a __str starting at @a __pos1. If adding
4525 * characters causes the length to exceed max_size(),
4526 * length_error is thrown. The value of the string doesn't
4527 * change if an error is thrown.
4530 insert(size_type __pos1
, const basic_string
& __str
)
4531 { return this->insert(__pos1
, __str
, size_type(0), __str
.size()); }
4534 * @brief Insert a substring.
4535 * @param __pos1 Iterator referencing location in string to insert at.
4536 * @param __str The string to insert.
4537 * @param __pos2 Start of characters in str to insert.
4538 * @param __n Number of characters to insert.
4539 * @return Reference to this string.
4540 * @throw std::length_error If new length exceeds @c max_size().
4541 * @throw std::out_of_range If @a pos1 > size() or
4542 * @a __pos2 > @a str.size().
4544 * Starting at @a pos1, insert @a __n character of @a __str
4545 * beginning with @a __pos2. If adding characters causes the
4546 * length to exceed max_size(), length_error is thrown. If @a
4547 * __pos1 is beyond the end of this string or @a __pos2 is
4548 * beyond the end of @a __str, out_of_range is thrown. The
4549 * value of the string doesn't change if an error is thrown.
4552 insert(size_type __pos1
, const basic_string
& __str
,
4553 size_type __pos2
, size_type __n
= npos
)
4554 { return this->insert(__pos1
, __str
._M_data()
4555 + __str
._M_check(__pos2
, "basic_string::insert"),
4556 __str
._M_limit(__pos2
, __n
)); }
4559 * @brief Insert a C substring.
4560 * @param __pos Iterator referencing location in string to insert at.
4561 * @param __s The C string to insert.
4562 * @param __n The number of characters to insert.
4563 * @return Reference to this string.
4564 * @throw std::length_error If new length exceeds @c max_size().
4565 * @throw std::out_of_range If @a __pos is beyond the end of this
4568 * Inserts the first @a __n characters of @a __s starting at @a
4569 * __pos. If adding characters causes the length to exceed
4570 * max_size(), length_error is thrown. If @a __pos is beyond
4571 * end(), out_of_range is thrown. The value of the string
4572 * doesn't change if an error is thrown.
4575 insert(size_type __pos
, const _CharT
* __s
, size_type __n
);
4578 * @brief Insert a C string.
4579 * @param __pos Iterator referencing location in string to insert at.
4580 * @param __s The C string to insert.
4581 * @return Reference to this string.
4582 * @throw std::length_error If new length exceeds @c max_size().
4583 * @throw std::out_of_range If @a pos is beyond the end of this
4586 * Inserts the first @a n characters of @a __s starting at @a __pos. If
4587 * adding characters causes the length to exceed max_size(),
4588 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
4589 * thrown. The value of the string doesn't change if an error is
4593 insert(size_type __pos
, const _CharT
* __s
)
4595 __glibcxx_requires_string(__s
);
4596 return this->insert(__pos
, __s
, traits_type::length(__s
));
4600 * @brief Insert multiple characters.
4601 * @param __pos Index in string to insert at.
4602 * @param __n Number of characters to insert
4603 * @param __c The character to insert.
4604 * @return Reference to this string.
4605 * @throw std::length_error If new length exceeds @c max_size().
4606 * @throw std::out_of_range If @a __pos is beyond the end of this
4609 * Inserts @a __n copies of character @a __c starting at index
4610 * @a __pos. If adding characters causes the length to exceed
4611 * max_size(), length_error is thrown. If @a __pos > length(),
4612 * out_of_range is thrown. The value of the string doesn't
4613 * change if an error is thrown.
4616 insert(size_type __pos
, size_type __n
, _CharT __c
)
4617 { return _M_replace_aux(_M_check(__pos
, "basic_string::insert"),
4618 size_type(0), __n
, __c
); }
4621 * @brief Insert one character.
4622 * @param __p Iterator referencing position in string to insert at.
4623 * @param __c The character to insert.
4624 * @return Iterator referencing newly inserted char.
4625 * @throw std::length_error If new length exceeds @c max_size().
4627 * Inserts character @a __c at position referenced by @a __p.
4628 * If adding character causes the length to exceed max_size(),
4629 * length_error is thrown. If @a __p is beyond end of string,
4630 * out_of_range is thrown. The value of the string doesn't
4631 * change if an error is thrown.
4634 insert(iterator __p
, _CharT __c
)
4636 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
4637 const size_type __pos
= __p
- _M_ibegin();
4638 _M_replace_aux(__pos
, size_type(0), size_type(1), __c
);
4639 _M_rep()->_M_set_leaked();
4640 return iterator(_M_data() + __pos
);
4643 #if __cplusplus >= 201703L
4645 * @brief Insert a string_view.
4646 * @param __pos Iterator referencing position in string to insert at.
4647 * @param __svt The object convertible to string_view to insert.
4648 * @return Reference to this string.
4650 template<typename _Tp
>
4651 _If_sv
<_Tp
, basic_string
&>
4652 insert(size_type __pos
, const _Tp
& __svt
)
4654 __sv_type __sv
= __svt
;
4655 return this->insert(__pos
, __sv
.data(), __sv
.size());
4659 * @brief Insert a string_view.
4660 * @param __pos Iterator referencing position in string to insert at.
4661 * @param __svt The object convertible to string_view to insert from.
4662 * @param __pos Iterator referencing position in string_view to insert
4664 * @param __n The number of characters to insert.
4665 * @return Reference to this string.
4667 template<typename _Tp
>
4668 _If_sv
<_Tp
, basic_string
&>
4669 insert(size_type __pos1
, const _Tp
& __svt
,
4670 size_type __pos2
, size_type __n
= npos
)
4672 __sv_type __sv
= __svt
;
4673 return this->replace(__pos1
, size_type(0), __sv
.data()
4674 + __sv
._M_check(__pos2
, "basic_string::insert"),
4675 __sv
._M_limit(__pos2
, __n
));
4680 * @brief Remove characters.
4681 * @param __pos Index of first character to remove (default 0).
4682 * @param __n Number of characters to remove (default remainder).
4683 * @return Reference to this string.
4684 * @throw std::out_of_range If @a pos is beyond the end of this
4687 * Removes @a __n characters from this string starting at @a
4688 * __pos. The length of the string is reduced by @a __n. If
4689 * there are < @a __n characters to remove, the remainder of
4690 * the string is truncated. If @a __p is beyond end of string,
4691 * out_of_range is thrown. The value of the string doesn't
4692 * change if an error is thrown.
4695 erase(size_type __pos
= 0, size_type __n
= npos
)
4697 _M_mutate(_M_check(__pos
, "basic_string::erase"),
4698 _M_limit(__pos
, __n
), size_type(0));
4703 * @brief Remove one character.
4704 * @param __position Iterator referencing the character to remove.
4705 * @return iterator referencing same location after removal.
4707 * Removes the character at @a __position from this string. The value
4708 * of the string doesn't change if an error is thrown.
4711 erase(iterator __position
)
4713 _GLIBCXX_DEBUG_PEDASSERT(__position
>= _M_ibegin()
4714 && __position
< _M_iend());
4715 const size_type __pos
= __position
- _M_ibegin();
4716 _M_mutate(__pos
, size_type(1), size_type(0));
4717 _M_rep()->_M_set_leaked();
4718 return iterator(_M_data() + __pos
);
4722 * @brief Remove a range of characters.
4723 * @param __first Iterator referencing the first character to remove.
4724 * @param __last Iterator referencing the end of the range.
4725 * @return Iterator referencing location of first after removal.
4727 * Removes the characters in the range [first,last) from this string.
4728 * The value of the string doesn't change if an error is thrown.
4731 erase(iterator __first
, iterator __last
);
4733 #if __cplusplus >= 201103L
4735 * @brief Remove the last character.
4737 * The string must be non-empty.
4740 pop_back() // FIXME C++11: should be noexcept.
4742 __glibcxx_assert(!empty());
4743 erase(size() - 1, 1);
4748 * @brief Replace characters with value from another string.
4749 * @param __pos Index of first character to replace.
4750 * @param __n Number of characters to be replaced.
4751 * @param __str String to insert.
4752 * @return Reference to this string.
4753 * @throw std::out_of_range If @a pos is beyond the end of this
4755 * @throw std::length_error If new length exceeds @c max_size().
4757 * Removes the characters in the range [__pos,__pos+__n) from
4758 * this string. In place, the value of @a __str is inserted.
4759 * If @a __pos is beyond end of string, out_of_range is thrown.
4760 * If the length of the result exceeds max_size(), length_error
4761 * is thrown. The value of the string doesn't change if an
4765 replace(size_type __pos
, size_type __n
, const basic_string
& __str
)
4766 { return this->replace(__pos
, __n
, __str
._M_data(), __str
.size()); }
4769 * @brief Replace characters with value from another string.
4770 * @param __pos1 Index of first character to replace.
4771 * @param __n1 Number of characters to be replaced.
4772 * @param __str String to insert.
4773 * @param __pos2 Index of first character of str to use.
4774 * @param __n2 Number of characters from str to use.
4775 * @return Reference to this string.
4776 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
4778 * @throw std::length_error If new length exceeds @c max_size().
4780 * Removes the characters in the range [__pos1,__pos1 + n) from this
4781 * string. In place, the value of @a __str is inserted. If @a __pos is
4782 * beyond end of string, out_of_range is thrown. If the length of the
4783 * result exceeds max_size(), length_error is thrown. The value of the
4784 * string doesn't change if an error is thrown.
4787 replace(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
4788 size_type __pos2
, size_type __n2
= npos
)
4789 { return this->replace(__pos1
, __n1
, __str
._M_data()
4790 + __str
._M_check(__pos2
, "basic_string::replace"),
4791 __str
._M_limit(__pos2
, __n2
)); }
4794 * @brief Replace characters with value of a C substring.
4795 * @param __pos Index of first character to replace.
4796 * @param __n1 Number of characters to be replaced.
4797 * @param __s C string to insert.
4798 * @param __n2 Number of characters from @a s to use.
4799 * @return Reference to this string.
4800 * @throw std::out_of_range If @a pos1 > size().
4801 * @throw std::length_error If new length exceeds @c max_size().
4803 * Removes the characters in the range [__pos,__pos + __n1)
4804 * from this string. In place, the first @a __n2 characters of
4805 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
4806 * @a __pos is beyond end of string, out_of_range is thrown. If
4807 * the length of result exceeds max_size(), length_error is
4808 * thrown. The value of the string doesn't change if an error
4812 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
4816 * @brief Replace characters with value of a C string.
4817 * @param __pos Index of first character to replace.
4818 * @param __n1 Number of characters to be replaced.
4819 * @param __s C string to insert.
4820 * @return Reference to this string.
4821 * @throw std::out_of_range If @a pos > size().
4822 * @throw std::length_error If new length exceeds @c max_size().
4824 * Removes the characters in the range [__pos,__pos + __n1)
4825 * from this string. In place, the characters of @a __s are
4826 * inserted. If @a __pos is beyond end of string, out_of_range
4827 * is thrown. If the length of result exceeds max_size(),
4828 * length_error is thrown. The value of the string doesn't
4829 * change if an error is thrown.
4832 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
)
4834 __glibcxx_requires_string(__s
);
4835 return this->replace(__pos
, __n1
, __s
, traits_type::length(__s
));
4839 * @brief Replace characters with multiple characters.
4840 * @param __pos Index of first character to replace.
4841 * @param __n1 Number of characters to be replaced.
4842 * @param __n2 Number of characters to insert.
4843 * @param __c Character to insert.
4844 * @return Reference to this string.
4845 * @throw std::out_of_range If @a __pos > size().
4846 * @throw std::length_error If new length exceeds @c max_size().
4848 * Removes the characters in the range [pos,pos + n1) from this
4849 * string. In place, @a __n2 copies of @a __c are inserted.
4850 * If @a __pos is beyond end of string, out_of_range is thrown.
4851 * If the length of result exceeds max_size(), length_error is
4852 * thrown. The value of the string doesn't change if an error
4856 replace(size_type __pos
, size_type __n1
, size_type __n2
, _CharT __c
)
4857 { return _M_replace_aux(_M_check(__pos
, "basic_string::replace"),
4858 _M_limit(__pos
, __n1
), __n2
, __c
); }
4861 * @brief Replace range of characters with string.
4862 * @param __i1 Iterator referencing start of range to replace.
4863 * @param __i2 Iterator referencing end of range to replace.
4864 * @param __str String value to insert.
4865 * @return Reference to this string.
4866 * @throw std::length_error If new length exceeds @c max_size().
4868 * Removes the characters in the range [__i1,__i2). In place,
4869 * the value of @a __str is inserted. If the length of result
4870 * exceeds max_size(), length_error is thrown. The value of
4871 * the string doesn't change if an error is thrown.
4874 replace(iterator __i1
, iterator __i2
, const basic_string
& __str
)
4875 { return this->replace(__i1
, __i2
, __str
._M_data(), __str
.size()); }
4878 * @brief Replace range of characters with C substring.
4879 * @param __i1 Iterator referencing start of range to replace.
4880 * @param __i2 Iterator referencing end of range to replace.
4881 * @param __s C string value to insert.
4882 * @param __n Number of characters from s to insert.
4883 * @return Reference to this string.
4884 * @throw std::length_error If new length exceeds @c max_size().
4886 * Removes the characters in the range [__i1,__i2). In place,
4887 * the first @a __n characters of @a __s are inserted. If the
4888 * length of result exceeds max_size(), length_error is thrown.
4889 * The value of the string doesn't change if an error is
4893 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
, size_type __n
)
4895 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4896 && __i2
<= _M_iend());
4897 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
, __s
, __n
);
4901 * @brief Replace range of characters with C string.
4902 * @param __i1 Iterator referencing start of range to replace.
4903 * @param __i2 Iterator referencing end of range to replace.
4904 * @param __s C string value to insert.
4905 * @return Reference to this string.
4906 * @throw std::length_error If new length exceeds @c max_size().
4908 * Removes the characters in the range [__i1,__i2). In place,
4909 * the characters of @a __s are inserted. If the length of
4910 * result exceeds max_size(), length_error is thrown. The
4911 * value of the string doesn't change if an error is thrown.
4914 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
)
4916 __glibcxx_requires_string(__s
);
4917 return this->replace(__i1
, __i2
, __s
, traits_type::length(__s
));
4921 * @brief Replace range of characters with multiple characters
4922 * @param __i1 Iterator referencing start of range to replace.
4923 * @param __i2 Iterator referencing end of range to replace.
4924 * @param __n Number of characters to insert.
4925 * @param __c Character to insert.
4926 * @return Reference to this string.
4927 * @throw std::length_error If new length exceeds @c max_size().
4929 * Removes the characters in the range [__i1,__i2). In place,
4930 * @a __n copies of @a __c are inserted. If the length of
4931 * result exceeds max_size(), length_error is thrown. The
4932 * value of the string doesn't change if an error is thrown.
4935 replace(iterator __i1
, iterator __i2
, size_type __n
, _CharT __c
)
4937 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4938 && __i2
<= _M_iend());
4939 return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __c
);
4943 * @brief Replace range of characters with range.
4944 * @param __i1 Iterator referencing start of range to replace.
4945 * @param __i2 Iterator referencing end of range to replace.
4946 * @param __k1 Iterator referencing start of range to insert.
4947 * @param __k2 Iterator referencing end of range to insert.
4948 * @return Reference to this string.
4949 * @throw std::length_error If new length exceeds @c max_size().
4951 * Removes the characters in the range [__i1,__i2). In place,
4952 * characters in the range [__k1,__k2) are inserted. If the
4953 * length of result exceeds max_size(), length_error is thrown.
4954 * The value of the string doesn't change if an error is
4957 template<class _InputIterator
>
4959 replace(iterator __i1
, iterator __i2
,
4960 _InputIterator __k1
, _InputIterator __k2
)
4962 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4963 && __i2
<= _M_iend());
4964 __glibcxx_requires_valid_range(__k1
, __k2
);
4965 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
4966 return _M_replace_dispatch(__i1
, __i2
, __k1
, __k2
, _Integral());
4969 // Specializations for the common case of pointer and iterator:
4970 // useful to avoid the overhead of temporary buffering in _M_replace.
4972 replace(iterator __i1
, iterator __i2
, _CharT
* __k1
, _CharT
* __k2
)
4974 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4975 && __i2
<= _M_iend());
4976 __glibcxx_requires_valid_range(__k1
, __k2
);
4977 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4982 replace(iterator __i1
, iterator __i2
,
4983 const _CharT
* __k1
, const _CharT
* __k2
)
4985 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4986 && __i2
<= _M_iend());
4987 __glibcxx_requires_valid_range(__k1
, __k2
);
4988 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4993 replace(iterator __i1
, iterator __i2
, iterator __k1
, iterator __k2
)
4995 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
4996 && __i2
<= _M_iend());
4997 __glibcxx_requires_valid_range(__k1
, __k2
);
4998 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
4999 __k1
.base(), __k2
- __k1
);
5003 replace(iterator __i1
, iterator __i2
,
5004 const_iterator __k1
, const_iterator __k2
)
5006 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
5007 && __i2
<= _M_iend());
5008 __glibcxx_requires_valid_range(__k1
, __k2
);
5009 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
5010 __k1
.base(), __k2
- __k1
);
5013 #if __cplusplus >= 201103L
5015 * @brief Replace range of characters with initializer_list.
5016 * @param __i1 Iterator referencing start of range to replace.
5017 * @param __i2 Iterator referencing end of range to replace.
5018 * @param __l The initializer_list of characters to insert.
5019 * @return Reference to this string.
5020 * @throw std::length_error If new length exceeds @c max_size().
5022 * Removes the characters in the range [__i1,__i2). In place,
5023 * characters in the range [__k1,__k2) are inserted. If the
5024 * length of result exceeds max_size(), length_error is thrown.
5025 * The value of the string doesn't change if an error is
5028 basic_string
& replace(iterator __i1
, iterator __i2
,
5029 initializer_list
<_CharT
> __l
)
5030 { return this->replace(__i1
, __i2
, __l
.begin(), __l
.end()); }
5033 #if __cplusplus >= 201703L
5035 * @brief Replace range of characters with string_view.
5036 * @param __pos The position to replace at.
5037 * @param __n The number of characters to replace.
5038 * @param __svt The object convertible to string_view to insert.
5039 * @return Reference to this string.
5041 template<typename _Tp
>
5042 _If_sv
<_Tp
, basic_string
&>
5043 replace(size_type __pos
, size_type __n
, const _Tp
& __svt
)
5045 __sv_type __sv
= __svt
;
5046 return this->replace(__pos
, __n
, __sv
.data(), __sv
.size());
5050 * @brief Replace range of characters with string_view.
5051 * @param __pos1 The position to replace at.
5052 * @param __n1 The number of characters to replace.
5053 * @param __svt The object convertible to string_view to insert from.
5054 * @param __pos2 The position in the string_view to insert from.
5055 * @param __n2 The number of characters to insert.
5056 * @return Reference to this string.
5058 template<typename _Tp
>
5059 _If_sv
<_Tp
, basic_string
&>
5060 replace(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
5061 size_type __pos2
, size_type __n2
= npos
)
5063 __sv_type __sv
= __svt
;
5064 return this->replace(__pos1
, __n1
,
5065 __sv
.data() + __sv
._M_check(__pos2
, "basic_string::replace"),
5066 __sv
._M_limit(__pos2
, __n2
));
5070 * @brief Replace range of characters with string_view.
5071 * @param __i1 An iterator referencing the start position
5073 * @param __i2 An iterator referencing the end position
5075 * @param __svt The object convertible to string_view to insert from.
5076 * @return Reference to this string.
5078 template<typename _Tp
>
5079 _If_sv
<_Tp
, basic_string
&>
5080 replace(const_iterator __i1
, const_iterator __i2
, const _Tp
& __svt
)
5082 __sv_type __sv
= __svt
;
5083 return this->replace(__i1
- begin(), __i2
- __i1
, __sv
);
5088 template<class _Integer
>
5090 _M_replace_dispatch(iterator __i1
, iterator __i2
, _Integer __n
,
5091 _Integer __val
, __true_type
)
5092 { return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __val
); }
5094 template<class _InputIterator
>
5096 _M_replace_dispatch(iterator __i1
, iterator __i2
, _InputIterator __k1
,
5097 _InputIterator __k2
, __false_type
);
5100 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
5104 _M_replace_safe(size_type __pos1
, size_type __n1
, const _CharT
* __s
,
5107 // _S_construct_aux is used to implement the 21.3.1 para 15 which
5108 // requires special behaviour if _InIter is an integral type
5109 template<class _InIterator
>
5111 _S_construct_aux(_InIterator __beg
, _InIterator __end
,
5112 const _Alloc
& __a
, __false_type
)
5114 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
5115 return _S_construct(__beg
, __end
, __a
, _Tag());
5118 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5119 // 438. Ambiguity in the "do the right thing" clause
5120 template<class _Integer
>
5122 _S_construct_aux(_Integer __beg
, _Integer __end
,
5123 const _Alloc
& __a
, __true_type
)
5124 { return _S_construct_aux_2(static_cast<size_type
>(__beg
),
5128 _S_construct_aux_2(size_type __req
, _CharT __c
, const _Alloc
& __a
)
5129 { return _S_construct(__req
, __c
, __a
); }
5131 template<class _InIterator
>
5133 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
)
5135 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
5136 return _S_construct_aux(__beg
, __end
, __a
, _Integral());
5139 // For Input Iterators, used in istreambuf_iterators, etc.
5140 template<class _InIterator
>
5142 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
,
5143 input_iterator_tag
);
5145 // For forward_iterators up to random_access_iterators, used for
5146 // string::iterator, _CharT*, etc.
5147 template<class _FwdIterator
>
5149 _S_construct(_FwdIterator __beg
, _FwdIterator __end
, const _Alloc
& __a
,
5150 forward_iterator_tag
);
5153 _S_construct(size_type __req
, _CharT __c
, const _Alloc
& __a
);
5158 * @brief Copy substring into C string.
5159 * @param __s C string to copy value into.
5160 * @param __n Number of characters to copy.
5161 * @param __pos Index of first character to copy.
5162 * @return Number of characters actually copied
5163 * @throw std::out_of_range If __pos > size().
5165 * Copies up to @a __n characters starting at @a __pos into the
5166 * C string @a __s. If @a __pos is %greater than size(),
5167 * out_of_range is thrown.
5170 copy(_CharT
* __s
, size_type __n
, size_type __pos
= 0) const;
5173 * @brief Swap contents with another string.
5174 * @param __s String to swap with.
5176 * Exchanges the contents of this string with that of @a __s in constant
5180 swap(basic_string
& __s
)
5181 _GLIBCXX_NOEXCEPT_IF(allocator_traits
<_Alloc
>::is_always_equal::value
);
5183 // String operations:
5185 * @brief Return const pointer to null-terminated contents.
5187 * This is a handle to internal data. Do not modify or dire things may
5191 c_str() const _GLIBCXX_NOEXCEPT
5192 { return _M_data(); }
5195 * @brief Return const pointer to contents.
5197 * This is a pointer to internal data. It is undefined to modify
5198 * the contents through the returned pointer. To get a pointer that
5199 * allows modifying the contents use @c &str[0] instead,
5200 * (or in C++17 the non-const @c str.data() overload).
5203 data() const _GLIBCXX_NOEXCEPT
5204 { return _M_data(); }
5206 #if __cplusplus >= 201703L
5208 * @brief Return non-const pointer to contents.
5210 * This is a pointer to the character sequence held by the string.
5211 * Modifying the characters in the sequence is allowed.
5222 * @brief Return copy of allocator used to construct this string.
5225 get_allocator() const _GLIBCXX_NOEXCEPT
5226 { return _M_dataplus
; }
5229 * @brief Find position of a C substring.
5230 * @param __s C string to locate.
5231 * @param __pos Index of character to search from.
5232 * @param __n Number of characters from @a s to search for.
5233 * @return Index of start of first occurrence.
5235 * Starting from @a __pos, searches forward for the first @a
5236 * __n characters in @a __s within this string. If found,
5237 * returns the index where it begins. If not found, returns
5241 find(const _CharT
* __s
, size_type __pos
, size_type __n
) const
5245 * @brief Find position of a string.
5246 * @param __str String to locate.
5247 * @param __pos Index of character to search from (default 0).
5248 * @return Index of start of first occurrence.
5250 * Starting from @a __pos, searches forward for value of @a __str within
5251 * this string. If found, returns the index where it begins. If not
5252 * found, returns npos.
5255 find(const basic_string
& __str
, size_type __pos
= 0) const
5257 { return this->find(__str
.data(), __pos
, __str
.size()); }
5260 * @brief Find position of a C string.
5261 * @param __s C string to locate.
5262 * @param __pos Index of character to search from (default 0).
5263 * @return Index of start of first occurrence.
5265 * Starting from @a __pos, searches forward for the value of @a
5266 * __s within this string. If found, returns the index where
5267 * it begins. If not found, returns npos.
5270 find(const _CharT
* __s
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
5272 __glibcxx_requires_string(__s
);
5273 return this->find(__s
, __pos
, traits_type::length(__s
));
5277 * @brief Find position of a character.
5278 * @param __c Character to locate.
5279 * @param __pos Index of character to search from (default 0).
5280 * @return Index of first occurrence.
5282 * Starting from @a __pos, searches forward for @a __c within
5283 * this string. If found, returns the index where it was
5284 * found. If not found, returns npos.
5287 find(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
;
5289 #if __cplusplus >= 201703L
5291 * @brief Find position of a string_view.
5292 * @param __svt The object convertible to string_view to locate.
5293 * @param __pos Index of character to search from (default 0).
5294 * @return Index of start of first occurrence.
5296 template<typename _Tp
>
5297 _If_sv
<_Tp
, size_type
>
5298 find(const _Tp
& __svt
, size_type __pos
= 0) const
5299 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5301 __sv_type __sv
= __svt
;
5302 return this->find(__sv
.data(), __pos
, __sv
.size());
5307 * @brief Find last position of a string.
5308 * @param __str String to locate.
5309 * @param __pos Index of character to search back from (default end).
5310 * @return Index of start of last occurrence.
5312 * Starting from @a __pos, searches backward for value of @a
5313 * __str within this string. If found, returns the index where
5314 * it begins. If not found, returns npos.
5317 rfind(const basic_string
& __str
, size_type __pos
= npos
) const
5319 { return this->rfind(__str
.data(), __pos
, __str
.size()); }
5322 * @brief Find last position of a C substring.
5323 * @param __s C string to locate.
5324 * @param __pos Index of character to search back from.
5325 * @param __n Number of characters from s to search for.
5326 * @return Index of start of last occurrence.
5328 * Starting from @a __pos, searches backward for the first @a
5329 * __n characters in @a __s within this string. If found,
5330 * returns the index where it begins. If not found, returns
5334 rfind(const _CharT
* __s
, size_type __pos
, size_type __n
) const
5338 * @brief Find last position of a C string.
5339 * @param __s C string to locate.
5340 * @param __pos Index of character to start search at (default end).
5341 * @return Index of start of last occurrence.
5343 * Starting from @a __pos, searches backward for the value of
5344 * @a __s within this string. If found, returns the index
5345 * where it begins. If not found, returns npos.
5348 rfind(const _CharT
* __s
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
5350 __glibcxx_requires_string(__s
);
5351 return this->rfind(__s
, __pos
, traits_type::length(__s
));
5355 * @brief Find last position of a character.
5356 * @param __c Character to locate.
5357 * @param __pos Index of character to search back from (default end).
5358 * @return Index of last occurrence.
5360 * Starting from @a __pos, searches backward for @a __c within
5361 * this string. If found, returns the index where it was
5362 * found. If not found, returns npos.
5365 rfind(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
;
5367 #if __cplusplus >= 201703L
5369 * @brief Find last position of a string_view.
5370 * @param __svt The object convertible to string_view to locate.
5371 * @param __pos Index of character to search back from (default end).
5372 * @return Index of start of last occurrence.
5374 template<typename _Tp
>
5375 _If_sv
<_Tp
, size_type
>
5376 rfind(const _Tp
& __svt
, size_type __pos
= npos
) const
5377 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5379 __sv_type __sv
= __svt
;
5380 return this->rfind(__sv
.data(), __pos
, __sv
.size());
5385 * @brief Find position of a character of string.
5386 * @param __str String containing characters to locate.
5387 * @param __pos Index of character to search from (default 0).
5388 * @return Index of first occurrence.
5390 * Starting from @a __pos, searches forward for one of the
5391 * characters of @a __str within this string. If found,
5392 * returns the index where it was found. If not found, returns
5396 find_first_of(const basic_string
& __str
, size_type __pos
= 0) const
5398 { return this->find_first_of(__str
.data(), __pos
, __str
.size()); }
5401 * @brief Find position of a character of C substring.
5402 * @param __s String containing characters to locate.
5403 * @param __pos Index of character to search from.
5404 * @param __n Number of characters from s to search for.
5405 * @return Index of first occurrence.
5407 * Starting from @a __pos, searches forward for one of the
5408 * first @a __n characters of @a __s within this string. If
5409 * found, returns the index where it was found. If not found,
5413 find_first_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
5417 * @brief Find position of a character of C string.
5418 * @param __s String containing characters to locate.
5419 * @param __pos Index of character to search from (default 0).
5420 * @return Index of first occurrence.
5422 * Starting from @a __pos, searches forward for one of the
5423 * characters of @a __s within this string. If found, returns
5424 * the index where it was found. If not found, returns npos.
5427 find_first_of(const _CharT
* __s
, size_type __pos
= 0) const
5430 __glibcxx_requires_string(__s
);
5431 return this->find_first_of(__s
, __pos
, traits_type::length(__s
));
5435 * @brief Find position of a character.
5436 * @param __c Character to locate.
5437 * @param __pos Index of character to search from (default 0).
5438 * @return Index of first occurrence.
5440 * Starting from @a __pos, searches forward for the character
5441 * @a __c within this string. If found, returns the index
5442 * where it was found. If not found, returns npos.
5444 * Note: equivalent to find(__c, __pos).
5447 find_first_of(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
5448 { return this->find(__c
, __pos
); }
5450 #if __cplusplus >= 201703L
5452 * @brief Find position of a character of a string_view.
5453 * @param __svt An object convertible to string_view containing
5454 * characters to locate.
5455 * @param __pos Index of character to search from (default 0).
5456 * @return Index of first occurrence.
5458 template<typename _Tp
>
5459 _If_sv
<_Tp
, size_type
>
5460 find_first_of(const _Tp
& __svt
, size_type __pos
= 0) const
5461 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5463 __sv_type __sv
= __svt
;
5464 return this->find_first_of(__sv
.data(), __pos
, __sv
.size());
5469 * @brief Find last position of a character of string.
5470 * @param __str String containing characters to locate.
5471 * @param __pos Index of character to search back from (default end).
5472 * @return Index of last occurrence.
5474 * Starting from @a __pos, searches backward for one of the
5475 * characters of @a __str within this string. If found,
5476 * returns the index where it was found. If not found, returns
5480 find_last_of(const basic_string
& __str
, size_type __pos
= npos
) const
5482 { return this->find_last_of(__str
.data(), __pos
, __str
.size()); }
5485 * @brief Find last position of a character of C substring.
5486 * @param __s C string containing characters to locate.
5487 * @param __pos Index of character to search back from.
5488 * @param __n Number of characters from s to search for.
5489 * @return Index of last occurrence.
5491 * Starting from @a __pos, searches backward for one of the
5492 * first @a __n characters of @a __s within this string. If
5493 * found, returns the index where it was found. If not found,
5497 find_last_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
5501 * @brief Find last position of a character of C string.
5502 * @param __s C string containing characters to locate.
5503 * @param __pos Index of character to search back from (default end).
5504 * @return Index of last occurrence.
5506 * Starting from @a __pos, searches backward for one of the
5507 * characters of @a __s within this string. If found, returns
5508 * the index where it was found. If not found, returns npos.
5511 find_last_of(const _CharT
* __s
, size_type __pos
= npos
) const
5514 __glibcxx_requires_string(__s
);
5515 return this->find_last_of(__s
, __pos
, traits_type::length(__s
));
5519 * @brief Find last position of a character.
5520 * @param __c Character to locate.
5521 * @param __pos Index of character to search back from (default end).
5522 * @return Index of last occurrence.
5524 * Starting from @a __pos, searches backward for @a __c within
5525 * this string. If found, returns the index where it was
5526 * found. If not found, returns npos.
5528 * Note: equivalent to rfind(__c, __pos).
5531 find_last_of(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
5532 { return this->rfind(__c
, __pos
); }
5534 #if __cplusplus >= 201703L
5536 * @brief Find last position of a character of string.
5537 * @param __svt An object convertible to string_view containing
5538 * characters to locate.
5539 * @param __pos Index of character to search back from (default end).
5540 * @return Index of last occurrence.
5542 template<typename _Tp
>
5543 _If_sv
<_Tp
, size_type
>
5544 find_last_of(const _Tp
& __svt
, size_type __pos
= npos
) const
5545 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5547 __sv_type __sv
= __svt
;
5548 return this->find_last_of(__sv
.data(), __pos
, __sv
.size());
5553 * @brief Find position of a character not in string.
5554 * @param __str String containing characters to avoid.
5555 * @param __pos Index of character to search from (default 0).
5556 * @return Index of first occurrence.
5558 * Starting from @a __pos, searches forward for a character not contained
5559 * in @a __str within this string. If found, returns the index where it
5560 * was found. If not found, returns npos.
5563 find_first_not_of(const basic_string
& __str
, size_type __pos
= 0) const
5565 { return this->find_first_not_of(__str
.data(), __pos
, __str
.size()); }
5568 * @brief Find position of a character not in C substring.
5569 * @param __s C string containing characters to avoid.
5570 * @param __pos Index of character to search from.
5571 * @param __n Number of characters from __s to consider.
5572 * @return Index of first occurrence.
5574 * Starting from @a __pos, searches forward for a character not
5575 * contained in the first @a __n characters of @a __s within
5576 * this string. If found, returns the index where it was
5577 * found. If not found, returns npos.
5580 find_first_not_of(const _CharT
* __s
, size_type __pos
,
5581 size_type __n
) const _GLIBCXX_NOEXCEPT
;
5584 * @brief Find position of a character not in C string.
5585 * @param __s C string containing characters to avoid.
5586 * @param __pos Index of character to search from (default 0).
5587 * @return Index of first occurrence.
5589 * Starting from @a __pos, searches forward for a character not
5590 * contained in @a __s within this string. If found, returns
5591 * the index where it was found. If not found, returns npos.
5594 find_first_not_of(const _CharT
* __s
, size_type __pos
= 0) const
5597 __glibcxx_requires_string(__s
);
5598 return this->find_first_not_of(__s
, __pos
, traits_type::length(__s
));
5602 * @brief Find position of a different character.
5603 * @param __c Character to avoid.
5604 * @param __pos Index of character to search from (default 0).
5605 * @return Index of first occurrence.
5607 * Starting from @a __pos, searches forward for a character
5608 * other than @a __c within this string. If found, returns the
5609 * index where it was found. If not found, returns npos.
5612 find_first_not_of(_CharT __c
, size_type __pos
= 0) const
5615 #if __cplusplus >= 201703L
5617 * @brief Find position of a character not in a string_view.
5618 * @param __svt An object convertible to string_view containing
5619 * characters to avoid.
5620 * @param __pos Index of character to search from (default 0).
5621 * @return Index of first occurrence.
5623 template<typename _Tp
>
5624 _If_sv
<_Tp
, size_type
>
5625 find_first_not_of(const _Tp
& __svt
, size_type __pos
= 0) const
5626 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5628 __sv_type __sv
= __svt
;
5629 return this->find_first_not_of(__sv
.data(), __pos
, __sv
.size());
5634 * @brief Find last position of a character not in string.
5635 * @param __str String containing characters to avoid.
5636 * @param __pos Index of character to search back from (default end).
5637 * @return Index of last occurrence.
5639 * Starting from @a __pos, searches backward for a character
5640 * not contained in @a __str within this string. If found,
5641 * returns the index where it was found. If not found, returns
5645 find_last_not_of(const basic_string
& __str
, size_type __pos
= npos
) const
5647 { return this->find_last_not_of(__str
.data(), __pos
, __str
.size()); }
5650 * @brief Find last position of a character not in C substring.
5651 * @param __s C string containing characters to avoid.
5652 * @param __pos Index of character to search back from.
5653 * @param __n Number of characters from s to consider.
5654 * @return Index of last occurrence.
5656 * Starting from @a __pos, searches backward for a character not
5657 * contained in the first @a __n characters of @a __s within this string.
5658 * If found, returns the index where it was found. If not found,
5662 find_last_not_of(const _CharT
* __s
, size_type __pos
,
5663 size_type __n
) const _GLIBCXX_NOEXCEPT
;
5665 * @brief Find last position of a character not in C string.
5666 * @param __s C string containing characters to avoid.
5667 * @param __pos Index of character to search back from (default end).
5668 * @return Index of last occurrence.
5670 * Starting from @a __pos, searches backward for a character
5671 * not contained in @a __s within this string. If found,
5672 * returns the index where it was found. If not found, returns
5676 find_last_not_of(const _CharT
* __s
, size_type __pos
= npos
) const
5679 __glibcxx_requires_string(__s
);
5680 return this->find_last_not_of(__s
, __pos
, traits_type::length(__s
));
5684 * @brief Find last position of a different character.
5685 * @param __c Character to avoid.
5686 * @param __pos Index of character to search back from (default end).
5687 * @return Index of last occurrence.
5689 * Starting from @a __pos, searches backward for a character other than
5690 * @a __c within this string. If found, returns the index where it was
5691 * found. If not found, returns npos.
5694 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const
5697 #if __cplusplus >= 201703L
5699 * @brief Find last position of a character not in a string_view.
5700 * @param __svt An object convertible to string_view containing
5701 * characters to avoid.
5702 * @param __pos Index of character to search back from (default end).
5703 * @return Index of last occurrence.
5705 template<typename _Tp
>
5706 _If_sv
<_Tp
, size_type
>
5707 find_last_not_of(const _Tp
& __svt
, size_type __pos
= npos
) const
5708 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5710 __sv_type __sv
= __svt
;
5711 return this->find_last_not_of(__sv
.data(), __pos
, __sv
.size());
5716 * @brief Get a substring.
5717 * @param __pos Index of first character (default 0).
5718 * @param __n Number of characters in substring (default remainder).
5719 * @return The new string.
5720 * @throw std::out_of_range If __pos > size().
5722 * Construct and return a new string using the @a __n
5723 * characters starting at @a __pos. If the string is too
5724 * short, use the remainder of the characters. If @a __pos is
5725 * beyond the end of the string, out_of_range is thrown.
5728 substr(size_type __pos
= 0, size_type __n
= npos
) const
5729 { return basic_string(*this,
5730 _M_check(__pos
, "basic_string::substr"), __n
); }
5733 * @brief Compare to a string.
5734 * @param __str String to compare against.
5735 * @return Integer < 0, 0, or > 0.
5737 * Returns an integer < 0 if this string is ordered before @a
5738 * __str, 0 if their values are equivalent, or > 0 if this
5739 * string is ordered after @a __str. Determines the effective
5740 * length rlen of the strings to compare as the smallest of
5741 * size() and str.size(). The function then compares the two
5742 * strings by calling traits::compare(data(), str.data(),rlen).
5743 * If the result of the comparison is nonzero returns it,
5744 * otherwise the shorter one is ordered first.
5747 compare(const basic_string
& __str
) const
5749 const size_type __size
= this->size();
5750 const size_type __osize
= __str
.size();
5751 const size_type __len
= std::min(__size
, __osize
);
5753 int __r
= traits_type::compare(_M_data(), __str
.data(), __len
);
5755 __r
= _S_compare(__size
, __osize
);
5759 #if __cplusplus >= 201703L
5761 * @brief Compare to a string_view.
5762 * @param __svt An object convertible to string_view to compare against.
5763 * @return Integer < 0, 0, or > 0.
5765 template<typename _Tp
>
5767 compare(const _Tp
& __svt
) const
5768 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5770 __sv_type __sv
= __svt
;
5771 const size_type __size
= this->size();
5772 const size_type __osize
= __sv
.size();
5773 const size_type __len
= std::min(__size
, __osize
);
5775 int __r
= traits_type::compare(_M_data(), __sv
.data(), __len
);
5777 __r
= _S_compare(__size
, __osize
);
5782 * @brief Compare to a string_view.
5783 * @param __pos A position in the string to start comparing from.
5784 * @param __n The number of characters to compare.
5785 * @param __svt An object convertible to string_view to compare
5787 * @return Integer < 0, 0, or > 0.
5789 template<typename _Tp
>
5791 compare(size_type __pos
, size_type __n
, const _Tp
& __svt
) const
5792 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5794 __sv_type __sv
= __svt
;
5795 return __sv_type(*this).substr(__pos
, __n
).compare(__sv
);
5799 * @brief Compare to a string_view.
5800 * @param __pos1 A position in the string to start comparing from.
5801 * @param __n1 The number of characters to compare.
5802 * @param __svt An object convertible to string_view to compare
5804 * @param __pos2 A position in the string_view to start comparing from.
5805 * @param __n2 The number of characters to compare.
5806 * @return Integer < 0, 0, or > 0.
5808 template<typename _Tp
>
5810 compare(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
5811 size_type __pos2
, size_type __n2
= npos
) const
5812 noexcept(is_same
<_Tp
, __sv_type
>::value
)
5814 __sv_type __sv
= __svt
;
5815 return __sv_type(*this)
5816 .substr(__pos1
, __n1
).compare(__sv
.substr(__pos2
, __n2
));
5821 * @brief Compare substring to a string.
5822 * @param __pos Index of first character of substring.
5823 * @param __n Number of characters in substring.
5824 * @param __str String to compare against.
5825 * @return Integer < 0, 0, or > 0.
5827 * Form the substring of this string from the @a __n characters
5828 * starting at @a __pos. Returns an integer < 0 if the
5829 * substring is ordered before @a __str, 0 if their values are
5830 * equivalent, or > 0 if the substring is ordered after @a
5831 * __str. Determines the effective length rlen of the strings
5832 * to compare as the smallest of the length of the substring
5833 * and @a __str.size(). The function then compares the two
5834 * strings by calling
5835 * traits::compare(substring.data(),str.data(),rlen). If the
5836 * result of the comparison is nonzero returns it, otherwise
5837 * the shorter one is ordered first.
5840 compare(size_type __pos
, size_type __n
, const basic_string
& __str
) const;
5843 * @brief Compare substring to a substring.
5844 * @param __pos1 Index of first character of substring.
5845 * @param __n1 Number of characters in substring.
5846 * @param __str String to compare against.
5847 * @param __pos2 Index of first character of substring of str.
5848 * @param __n2 Number of characters in substring of str.
5849 * @return Integer < 0, 0, or > 0.
5851 * Form the substring of this string from the @a __n1
5852 * characters starting at @a __pos1. Form the substring of @a
5853 * __str from the @a __n2 characters starting at @a __pos2.
5854 * Returns an integer < 0 if this substring is ordered before
5855 * the substring of @a __str, 0 if their values are equivalent,
5856 * or > 0 if this substring is ordered after the substring of
5857 * @a __str. Determines the effective length rlen of the
5858 * strings to compare as the smallest of the lengths of the
5859 * substrings. The function then compares the two strings by
5861 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
5862 * If the result of the comparison is nonzero returns it,
5863 * otherwise the shorter one is ordered first.
5866 compare(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
5867 size_type __pos2
, size_type __n2
= npos
) const;
5870 * @brief Compare to a C string.
5871 * @param __s C string to compare against.
5872 * @return Integer < 0, 0, or > 0.
5874 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
5875 * their values are equivalent, or > 0 if this string is ordered after
5876 * @a __s. Determines the effective length rlen of the strings to
5877 * compare as the smallest of size() and the length of a string
5878 * constructed from @a __s. The function then compares the two strings
5879 * by calling traits::compare(data(),s,rlen). If the result of the
5880 * comparison is nonzero returns it, otherwise the shorter one is
5884 compare(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
;
5886 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5887 // 5 String::compare specification questionable
5889 * @brief Compare substring to a C string.
5890 * @param __pos Index of first character of substring.
5891 * @param __n1 Number of characters in substring.
5892 * @param __s C string to compare against.
5893 * @return Integer < 0, 0, or > 0.
5895 * Form the substring of this string from the @a __n1
5896 * characters starting at @a pos. Returns an integer < 0 if
5897 * the substring is ordered before @a __s, 0 if their values
5898 * are equivalent, or > 0 if the substring is ordered after @a
5899 * __s. Determines the effective length rlen of the strings to
5900 * compare as the smallest of the length of the substring and
5901 * the length of a string constructed from @a __s. The
5902 * function then compares the two string by calling
5903 * traits::compare(substring.data(),__s,rlen). If the result of
5904 * the comparison is nonzero returns it, otherwise the shorter
5905 * one is ordered first.
5908 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
) const;
5911 * @brief Compare substring against a character %array.
5912 * @param __pos Index of first character of substring.
5913 * @param __n1 Number of characters in substring.
5914 * @param __s character %array to compare against.
5915 * @param __n2 Number of characters of s.
5916 * @return Integer < 0, 0, or > 0.
5918 * Form the substring of this string from the @a __n1
5919 * characters starting at @a __pos. Form a string from the
5920 * first @a __n2 characters of @a __s. Returns an integer < 0
5921 * if this substring is ordered before the string from @a __s,
5922 * 0 if their values are equivalent, or > 0 if this substring
5923 * is ordered after the string from @a __s. Determines the
5924 * effective length rlen of the strings to compare as the
5925 * smallest of the length of the substring and @a __n2. The
5926 * function then compares the two strings by calling
5927 * traits::compare(substring.data(),s,rlen). If the result of
5928 * the comparison is nonzero returns it, otherwise the shorter
5929 * one is ordered first.
5931 * NB: s must have at least n2 characters, '\\0' has
5932 * no special meaning.
5935 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
,
5936 size_type __n2
) const;
5938 #if __cplusplus > 201703L
5940 starts_with(basic_string_view
<_CharT
, _Traits
> __x
) const noexcept
5941 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
5944 starts_with(_CharT __x
) const noexcept
5945 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
5948 starts_with(const _CharT
* __x
) const noexcept
5949 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
5952 ends_with(basic_string_view
<_CharT
, _Traits
> __x
) const noexcept
5953 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
5956 ends_with(_CharT __x
) const noexcept
5957 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
5960 ends_with(const _CharT
* __x
) const noexcept
5961 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
5964 # ifdef _GLIBCXX_TM_TS_INTERNAL
5966 ::_txnal_cow_string_C1_for_exceptions(void* that
, const char* s
,
5969 ::_txnal_cow_string_c_str(const void *that
);
5971 ::_txnal_cow_string_D1(void *that
);
5973 ::_txnal_cow_string_D1_commit(void *that
);
5976 #endif // !_GLIBCXX_USE_CXX11_ABI
5978 #if __cpp_deduction_guides >= 201606
5979 _GLIBCXX_BEGIN_NAMESPACE_CXX11
5980 template<typename _InputIterator
, typename _CharT
5981 = typename iterator_traits
<_InputIterator
>::value_type
,
5982 typename _Allocator
= allocator
<_CharT
>,
5983 typename
= _RequireInputIter
<_InputIterator
>,
5984 typename
= _RequireAllocator
<_Allocator
>>
5985 basic_string(_InputIterator
, _InputIterator
, _Allocator
= _Allocator())
5986 -> basic_string
<_CharT
, char_traits
<_CharT
>, _Allocator
>;
5988 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5989 // 3075. basic_string needs deduction guides from basic_string_view
5990 template<typename _CharT
, typename _Traits
,
5991 typename _Allocator
= allocator
<_CharT
>,
5992 typename
= _RequireAllocator
<_Allocator
>>
5993 basic_string(basic_string_view
<_CharT
, _Traits
>, const _Allocator
& = _Allocator())
5994 -> basic_string
<_CharT
, _Traits
, _Allocator
>;
5996 template<typename _CharT
, typename _Traits
,
5997 typename _Allocator
= allocator
<_CharT
>,
5998 typename
= _RequireAllocator
<_Allocator
>>
5999 basic_string(basic_string_view
<_CharT
, _Traits
>,
6000 typename basic_string
<_CharT
, _Traits
, _Allocator
>::size_type
,
6001 typename basic_string
<_CharT
, _Traits
, _Allocator
>::size_type
,
6002 const _Allocator
& = _Allocator())
6003 -> basic_string
<_CharT
, _Traits
, _Allocator
>;
6004 _GLIBCXX_END_NAMESPACE_CXX11
6009 * @brief Concatenate two strings.
6010 * @param __lhs First string.
6011 * @param __rhs Last string.
6012 * @return New string with value of @a __lhs followed by @a __rhs.
6014 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6015 basic_string
<_CharT
, _Traits
, _Alloc
>
6016 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6017 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6019 basic_string
<_CharT
, _Traits
, _Alloc
> __str(__lhs
);
6020 __str
.append(__rhs
);
6025 * @brief Concatenate C string and string.
6026 * @param __lhs First string.
6027 * @param __rhs Last string.
6028 * @return New string with value of @a __lhs followed by @a __rhs.
6030 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6031 basic_string
<_CharT
,_Traits
,_Alloc
>
6032 operator+(const _CharT
* __lhs
,
6033 const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
);
6036 * @brief Concatenate character and string.
6037 * @param __lhs First string.
6038 * @param __rhs Last string.
6039 * @return New string with @a __lhs followed by @a __rhs.
6041 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6042 basic_string
<_CharT
,_Traits
,_Alloc
>
6043 operator+(_CharT __lhs
, const basic_string
<_CharT
,_Traits
,_Alloc
>& __rhs
);
6046 * @brief Concatenate string and C string.
6047 * @param __lhs First string.
6048 * @param __rhs Last string.
6049 * @return New string with @a __lhs followed by @a __rhs.
6051 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6052 inline basic_string
<_CharT
, _Traits
, _Alloc
>
6053 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6054 const _CharT
* __rhs
)
6056 basic_string
<_CharT
, _Traits
, _Alloc
> __str(__lhs
);
6057 __str
.append(__rhs
);
6062 * @brief Concatenate string and character.
6063 * @param __lhs First string.
6064 * @param __rhs Last string.
6065 * @return New string with @a __lhs followed by @a __rhs.
6067 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6068 inline basic_string
<_CharT
, _Traits
, _Alloc
>
6069 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
, _CharT __rhs
)
6071 typedef basic_string
<_CharT
, _Traits
, _Alloc
> __string_type
;
6072 typedef typename
__string_type::size_type __size_type
;
6073 __string_type
__str(__lhs
);
6074 __str
.append(__size_type(1), __rhs
);
6078 #if __cplusplus >= 201103L
6079 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6080 inline basic_string
<_CharT
, _Traits
, _Alloc
>
6081 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
6082 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6083 { return std::move(__lhs
.append(__rhs
)); }
6085 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6086 inline basic_string
<_CharT
, _Traits
, _Alloc
>
6087 operator+(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6088 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
6089 { return std::move(__rhs
.insert(0, __lhs
)); }
6091 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6092 inline basic_string
<_CharT
, _Traits
, _Alloc
>
6093 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
6094 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
6096 const auto __size
= __lhs
.size() + __rhs
.size();
6097 const bool __cond
= (__size
> __lhs
.capacity()
6098 && __size
<= __rhs
.capacity());
6099 return __cond
? std::move(__rhs
.insert(0, __lhs
))
6100 : std::move(__lhs
.append(__rhs
));
6103 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6104 inline basic_string
<_CharT
, _Traits
, _Alloc
>
6105 operator+(const _CharT
* __lhs
,
6106 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
6107 { return std::move(__rhs
.insert(0, __lhs
)); }
6109 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6110 inline basic_string
<_CharT
, _Traits
, _Alloc
>
6111 operator+(_CharT __lhs
,
6112 basic_string
<_CharT
, _Traits
, _Alloc
>&& __rhs
)
6113 { return std::move(__rhs
.insert(0, 1, __lhs
)); }
6115 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6116 inline basic_string
<_CharT
, _Traits
, _Alloc
>
6117 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
6118 const _CharT
* __rhs
)
6119 { return std::move(__lhs
.append(__rhs
)); }
6121 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6122 inline basic_string
<_CharT
, _Traits
, _Alloc
>
6123 operator+(basic_string
<_CharT
, _Traits
, _Alloc
>&& __lhs
,
6125 { return std::move(__lhs
.append(1, __rhs
)); }
6130 * @brief Test equivalence of two strings.
6131 * @param __lhs First string.
6132 * @param __rhs Second string.
6133 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
6135 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6137 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6138 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6140 { return __lhs
.compare(__rhs
) == 0; }
6142 template<typename _CharT
>
6144 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
, bool>::__type
6145 operator==(const basic_string
<_CharT
>& __lhs
,
6146 const basic_string
<_CharT
>& __rhs
) _GLIBCXX_NOEXCEPT
6147 { return (__lhs
.size() == __rhs
.size()
6148 && !std::char_traits
<_CharT
>::compare(__lhs
.data(), __rhs
.data(),
6152 * @brief Test equivalence of C string and string.
6153 * @param __lhs C string.
6154 * @param __rhs String.
6155 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
6157 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6159 operator==(const _CharT
* __lhs
,
6160 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6161 { return __rhs
.compare(__lhs
) == 0; }
6164 * @brief Test equivalence of string and C string.
6165 * @param __lhs String.
6166 * @param __rhs C string.
6167 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
6169 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6171 operator==(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6172 const _CharT
* __rhs
)
6173 { return __lhs
.compare(__rhs
) == 0; }
6177 * @brief Test difference of two strings.
6178 * @param __lhs First string.
6179 * @param __rhs Second string.
6180 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
6182 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6184 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6185 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6187 { return !(__lhs
== __rhs
); }
6190 * @brief Test difference of C string and string.
6191 * @param __lhs C string.
6192 * @param __rhs String.
6193 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
6195 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6197 operator!=(const _CharT
* __lhs
,
6198 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6199 { return !(__lhs
== __rhs
); }
6202 * @brief Test difference of string and C string.
6203 * @param __lhs String.
6204 * @param __rhs C string.
6205 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
6207 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6209 operator!=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6210 const _CharT
* __rhs
)
6211 { return !(__lhs
== __rhs
); }
6215 * @brief Test if string precedes string.
6216 * @param __lhs First string.
6217 * @param __rhs Second string.
6218 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6220 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6222 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6223 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6225 { return __lhs
.compare(__rhs
) < 0; }
6228 * @brief Test if string precedes C string.
6229 * @param __lhs String.
6230 * @param __rhs C string.
6231 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6233 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6235 operator<(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6236 const _CharT
* __rhs
)
6237 { return __lhs
.compare(__rhs
) < 0; }
6240 * @brief Test if C string precedes string.
6241 * @param __lhs C string.
6242 * @param __rhs String.
6243 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6245 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6247 operator<(const _CharT
* __lhs
,
6248 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6249 { return __rhs
.compare(__lhs
) > 0; }
6253 * @brief Test if string follows string.
6254 * @param __lhs First string.
6255 * @param __rhs Second string.
6256 * @return True if @a __lhs follows @a __rhs. False otherwise.
6258 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6260 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6261 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6263 { return __lhs
.compare(__rhs
) > 0; }
6266 * @brief Test if string follows C string.
6267 * @param __lhs String.
6268 * @param __rhs C string.
6269 * @return True if @a __lhs follows @a __rhs. False otherwise.
6271 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6273 operator>(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6274 const _CharT
* __rhs
)
6275 { return __lhs
.compare(__rhs
) > 0; }
6278 * @brief Test if C string follows string.
6279 * @param __lhs C string.
6280 * @param __rhs String.
6281 * @return True if @a __lhs follows @a __rhs. False otherwise.
6283 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6285 operator>(const _CharT
* __lhs
,
6286 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6287 { return __rhs
.compare(__lhs
) < 0; }
6291 * @brief Test if string doesn't follow string.
6292 * @param __lhs First string.
6293 * @param __rhs Second string.
6294 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6296 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6298 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6299 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6301 { return __lhs
.compare(__rhs
) <= 0; }
6304 * @brief Test if string doesn't follow C string.
6305 * @param __lhs String.
6306 * @param __rhs C string.
6307 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6309 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6311 operator<=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6312 const _CharT
* __rhs
)
6313 { return __lhs
.compare(__rhs
) <= 0; }
6316 * @brief Test if C string doesn't follow string.
6317 * @param __lhs C string.
6318 * @param __rhs String.
6319 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6321 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6323 operator<=(const _CharT
* __lhs
,
6324 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6325 { return __rhs
.compare(__lhs
) >= 0; }
6329 * @brief Test if string doesn't precede string.
6330 * @param __lhs First string.
6331 * @param __rhs Second string.
6332 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6334 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6336 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6337 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6339 { return __lhs
.compare(__rhs
) >= 0; }
6342 * @brief Test if string doesn't precede C string.
6343 * @param __lhs String.
6344 * @param __rhs C string.
6345 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6347 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6349 operator>=(const basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6350 const _CharT
* __rhs
)
6351 { return __lhs
.compare(__rhs
) >= 0; }
6354 * @brief Test if C string doesn't precede string.
6355 * @param __lhs C string.
6356 * @param __rhs String.
6357 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6359 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6361 operator>=(const _CharT
* __lhs
,
6362 const basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6363 { return __rhs
.compare(__lhs
) <= 0; }
6366 * @brief Swap contents of two strings.
6367 * @param __lhs First string.
6368 * @param __rhs Second string.
6370 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
6372 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6374 swap(basic_string
<_CharT
, _Traits
, _Alloc
>& __lhs
,
6375 basic_string
<_CharT
, _Traits
, _Alloc
>& __rhs
)
6376 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs
.swap(__rhs
)))
6377 { __lhs
.swap(__rhs
); }
6381 * @brief Read stream into a string.
6382 * @param __is Input stream.
6383 * @param __str Buffer to store into.
6384 * @return Reference to the input stream.
6386 * Stores characters from @a __is into @a __str until whitespace is
6387 * found, the end of the stream is encountered, or str.max_size()
6388 * is reached. If is.width() is non-zero, that is the limit on the
6389 * number of characters stored into @a __str. Any previous
6390 * contents of @a __str are erased.
6392 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6393 basic_istream
<_CharT
, _Traits
>&
6394 operator>>(basic_istream
<_CharT
, _Traits
>& __is
,
6395 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
);
6398 basic_istream
<char>&
6399 operator>>(basic_istream
<char>& __is
, basic_string
<char>& __str
);
6402 * @brief Write string to a stream.
6403 * @param __os Output stream.
6404 * @param __str String to write out.
6405 * @return Reference to the output stream.
6407 * Output characters of @a __str into os following the same rules as for
6408 * writing a C string.
6410 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6411 inline basic_ostream
<_CharT
, _Traits
>&
6412 operator<<(basic_ostream
<_CharT
, _Traits
>& __os
,
6413 const basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
6415 // _GLIBCXX_RESOLVE_LIB_DEFECTS
6416 // 586. string inserter not a formatted function
6417 return __ostream_insert(__os
, __str
.data(), __str
.size());
6421 * @brief Read a line from stream into a string.
6422 * @param __is Input stream.
6423 * @param __str Buffer to store into.
6424 * @param __delim Character marking end of line.
6425 * @return Reference to the input stream.
6427 * Stores characters from @a __is into @a __str until @a __delim is
6428 * found, the end of the stream is encountered, or str.max_size()
6429 * is reached. Any previous contents of @a __str are erased. If
6430 * @a __delim is encountered, it is extracted but not stored into
6433 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6434 basic_istream
<_CharT
, _Traits
>&
6435 getline(basic_istream
<_CharT
, _Traits
>& __is
,
6436 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
);
6439 * @brief Read a line from stream into a string.
6440 * @param __is Input stream.
6441 * @param __str Buffer to store into.
6442 * @return Reference to the input stream.
6444 * Stores characters from is into @a __str until '\n' is
6445 * found, the end of the stream is encountered, or str.max_size()
6446 * is reached. Any previous contents of @a __str are erased. If
6447 * end of line is encountered, it is extracted but not stored into
6450 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6451 inline basic_istream
<_CharT
, _Traits
>&
6452 getline(basic_istream
<_CharT
, _Traits
>& __is
,
6453 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
6454 { return std::getline(__is
, __str
, __is
.widen('\n')); }
6456 #if __cplusplus >= 201103L
6457 /// Read a line from an rvalue stream into a string.
6458 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6459 inline basic_istream
<_CharT
, _Traits
>&
6460 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
6461 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
, _CharT __delim
)
6462 { return std::getline(__is
, __str
, __delim
); }
6464 /// Read a line from an rvalue stream into a string.
6465 template<typename _CharT
, typename _Traits
, typename _Alloc
>
6466 inline basic_istream
<_CharT
, _Traits
>&
6467 getline(basic_istream
<_CharT
, _Traits
>&& __is
,
6468 basic_string
<_CharT
, _Traits
, _Alloc
>& __str
)
6469 { return std::getline(__is
, __str
); }
6473 basic_istream
<char>&
6474 getline(basic_istream
<char>& __in
, basic_string
<char>& __str
,
6477 #ifdef _GLIBCXX_USE_WCHAR_T
6479 basic_istream
<wchar_t>&
6480 getline(basic_istream
<wchar_t>& __in
, basic_string
<wchar_t>& __str
,
6484 _GLIBCXX_END_NAMESPACE_VERSION
6487 #if __cplusplus >= 201103L
6489 #include <ext/string_conversions.h>
6491 namespace std
_GLIBCXX_VISIBILITY(default)
6493 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6494 _GLIBCXX_BEGIN_NAMESPACE_CXX11
6496 #if _GLIBCXX_USE_C99_STDLIB
6497 // 21.4 Numeric Conversions [string.conversions].
6499 stoi(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6500 { return __gnu_cxx::__stoa
<long, int>(&std::strtol
, "stoi", __str
.c_str(),
6504 stol(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6505 { return __gnu_cxx::__stoa(&std::strtol
, "stol", __str
.c_str(),
6508 inline unsigned long
6509 stoul(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6510 { return __gnu_cxx::__stoa(&std::strtoul
, "stoul", __str
.c_str(),
6514 stoll(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6515 { return __gnu_cxx::__stoa(&std::strtoll
, "stoll", __str
.c_str(),
6518 inline unsigned long long
6519 stoull(const string
& __str
, size_t* __idx
= 0, int __base
= 10)
6520 { return __gnu_cxx::__stoa(&std::strtoull
, "stoull", __str
.c_str(),
6523 // NB: strtof vs strtod.
6525 stof(const string
& __str
, size_t* __idx
= 0)
6526 { return __gnu_cxx::__stoa(&std::strtof
, "stof", __str
.c_str(), __idx
); }
6529 stod(const string
& __str
, size_t* __idx
= 0)
6530 { return __gnu_cxx::__stoa(&std::strtod
, "stod", __str
.c_str(), __idx
); }
6533 stold(const string
& __str
, size_t* __idx
= 0)
6534 { return __gnu_cxx::__stoa(&std::strtold
, "stold", __str
.c_str(), __idx
); }
6535 #endif // _GLIBCXX_USE_C99_STDLIB
6537 #if _GLIBCXX_USE_C99_STDIO
6538 // NB: (v)snprintf vs sprintf.
6542 to_string(int __val
)
6543 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, 4 * sizeof(int),
6547 to_string(unsigned __val
)
6548 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
6549 4 * sizeof(unsigned),
6553 to_string(long __val
)
6554 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, 4 * sizeof(long),
6558 to_string(unsigned long __val
)
6559 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
6560 4 * sizeof(unsigned long),
6564 to_string(long long __val
)
6565 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
6566 4 * sizeof(long long),
6570 to_string(unsigned long long __val
)
6571 { return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
,
6572 4 * sizeof(unsigned long long),
6576 to_string(float __val
)
6579 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
6580 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
6585 to_string(double __val
)
6588 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
6589 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
6594 to_string(long double __val
)
6597 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
6598 return __gnu_cxx::__to_xstring
<string
>(&std::vsnprintf
, __n
,
6601 #endif // _GLIBCXX_USE_C99_STDIO
6603 #if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
6605 stoi(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6606 { return __gnu_cxx::__stoa
<long, int>(&std::wcstol
, "stoi", __str
.c_str(),
6610 stol(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6611 { return __gnu_cxx::__stoa(&std::wcstol
, "stol", __str
.c_str(),
6614 inline unsigned long
6615 stoul(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6616 { return __gnu_cxx::__stoa(&std::wcstoul
, "stoul", __str
.c_str(),
6620 stoll(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6621 { return __gnu_cxx::__stoa(&std::wcstoll
, "stoll", __str
.c_str(),
6624 inline unsigned long long
6625 stoull(const wstring
& __str
, size_t* __idx
= 0, int __base
= 10)
6626 { return __gnu_cxx::__stoa(&std::wcstoull
, "stoull", __str
.c_str(),
6629 // NB: wcstof vs wcstod.
6631 stof(const wstring
& __str
, size_t* __idx
= 0)
6632 { return __gnu_cxx::__stoa(&std::wcstof
, "stof", __str
.c_str(), __idx
); }
6635 stod(const wstring
& __str
, size_t* __idx
= 0)
6636 { return __gnu_cxx::__stoa(&std::wcstod
, "stod", __str
.c_str(), __idx
); }
6639 stold(const wstring
& __str
, size_t* __idx
= 0)
6640 { return __gnu_cxx::__stoa(&std::wcstold
, "stold", __str
.c_str(), __idx
); }
6642 #ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6645 to_wstring(int __val
)
6646 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(int),
6650 to_wstring(unsigned __val
)
6651 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
6652 4 * sizeof(unsigned),
6656 to_wstring(long __val
)
6657 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, 4 * sizeof(long),
6661 to_wstring(unsigned long __val
)
6662 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
6663 4 * sizeof(unsigned long),
6667 to_wstring(long long __val
)
6668 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
6669 4 * sizeof(long long),
6673 to_wstring(unsigned long long __val
)
6674 { return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
,
6675 4 * sizeof(unsigned long long),
6679 to_wstring(float __val
)
6682 __gnu_cxx::__numeric_traits
<float>::__max_exponent10
+ 20;
6683 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
6688 to_wstring(double __val
)
6691 __gnu_cxx::__numeric_traits
<double>::__max_exponent10
+ 20;
6692 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
6697 to_wstring(long double __val
)
6700 __gnu_cxx::__numeric_traits
<long double>::__max_exponent10
+ 20;
6701 return __gnu_cxx::__to_xstring
<wstring
>(&std::vswprintf
, __n
,
6704 #endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6705 #endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
6707 _GLIBCXX_END_NAMESPACE_CXX11
6708 _GLIBCXX_END_NAMESPACE_VERSION
6713 #if __cplusplus >= 201103L
6715 #include <bits/functional_hash.h>
6717 namespace std
_GLIBCXX_VISIBILITY(default)
6719 _GLIBCXX_BEGIN_NAMESPACE_VERSION
6723 #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
6724 /// std::hash specialization for string.
6727 : public __hash_base
<size_t, string
>
6730 operator()(const string
& __s
) const noexcept
6731 { return std::_Hash_impl::hash(__s
.data(), __s
.length()); }
6735 struct __is_fast_hash
<hash
<string
>> : std::false_type
6738 #ifdef _GLIBCXX_USE_WCHAR_T
6739 /// std::hash specialization for wstring.
6741 struct hash
<wstring
>
6742 : public __hash_base
<size_t, wstring
>
6745 operator()(const wstring
& __s
) const noexcept
6746 { return std::_Hash_impl::hash(__s
.data(),
6747 __s
.length() * sizeof(wchar_t)); }
6751 struct __is_fast_hash
<hash
<wstring
>> : std::false_type
6754 #endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
6756 /// std::hash specialization for u16string.
6758 struct hash
<u16string
>
6759 : public __hash_base
<size_t, u16string
>
6762 operator()(const u16string
& __s
) const noexcept
6763 { return std::_Hash_impl::hash(__s
.data(),
6764 __s
.length() * sizeof(char16_t
)); }
6768 struct __is_fast_hash
<hash
<u16string
>> : std::false_type
6771 /// std::hash specialization for u32string.
6773 struct hash
<u32string
>
6774 : public __hash_base
<size_t, u32string
>
6777 operator()(const u32string
& __s
) const noexcept
6778 { return std::_Hash_impl::hash(__s
.data(),
6779 __s
.length() * sizeof(char32_t
)); }
6783 struct __is_fast_hash
<hash
<u32string
>> : std::false_type
6786 #if __cplusplus > 201103L
6788 #define __cpp_lib_string_udls 201304
6790 inline namespace literals
6792 inline namespace string_literals
6794 #pragma GCC diagnostic push
6795 #pragma GCC diagnostic ignored "-Wliteral-suffix"
6796 _GLIBCXX_DEFAULT_ABI_TAG
6797 inline basic_string
<char>
6798 operator""s(const char* __str
, size_t __len
)
6799 { return basic_string
<char>{__str
, __len
}; }
6801 #ifdef _GLIBCXX_USE_WCHAR_T
6802 _GLIBCXX_DEFAULT_ABI_TAG
6803 inline basic_string
<wchar_t>
6804 operator""s(const wchar_t* __str
, size_t __len
)
6805 { return basic_string
<wchar_t>{__str
, __len
}; }
6808 _GLIBCXX_DEFAULT_ABI_TAG
6809 inline basic_string
<char16_t
>
6810 operator""s(const char16_t
* __str
, size_t __len
)
6811 { return basic_string
<char16_t
>{__str
, __len
}; }
6813 _GLIBCXX_DEFAULT_ABI_TAG
6814 inline basic_string
<char32_t
>
6815 operator""s(const char32_t
* __str
, size_t __len
)
6816 { return basic_string
<char32_t
>{__str
, __len
}; }
6818 #pragma GCC diagnostic pop
6819 } // inline namespace string_literals
6820 } // inline namespace literals
6822 #endif // __cplusplus > 201103L
6824 _GLIBCXX_END_NAMESPACE_VERSION
6829 #endif /* _BASIC_STRING_H */