1 // Definition of gcc4-compatible Copy-on-Write basic_string -*- C++ -*-
3 // Copyright (C) 1997-2023 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/cow_string.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{string}
29 * Defines the reference-counted COW string implementation.
33 #define _COW_STRING_H 1
35 #if ! _GLIBCXX_USE_CXX11_ABI
37 #include <ext/atomicity.h> // _Atomic_word, __is_single_threaded
39 namespace std
_GLIBCXX_VISIBILITY(default)
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
44 * @class basic_string basic_string.h <string>
45 * @brief Managing sequences of characters and character-like objects.
52 * @tparam _CharT Type of character
53 * @tparam _Traits Traits for character type, defaults to
54 * char_traits<_CharT>.
55 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
57 * Meets the requirements of a <a href="tables.html#65">container</a>, a
58 * <a href="tables.html#66">reversible container</a>, and a
59 * <a href="tables.html#67">sequence</a>. Of the
60 * <a href="tables.html#68">optional sequence requirements</a>, only
61 * @c push_back, @c at, and @c %array access are supported.
66 * Documentation? What's that?
67 * Nathan Myers <ncm@cantrip.org>.
69 * A string looks like this:
74 * [basic_string<char_type>] _M_capacity
75 * _M_dataplus _M_refcount
76 * _M_p ----------------> unnamed array of char_type
79 * Where the _M_p points to the first character in the string, and
80 * you cast it to a pointer-to-_Rep and subtract 1 to get a
81 * pointer to the header.
83 * This approach has the enormous advantage that a string object
84 * requires only one allocation. All the ugliness is confined
85 * within a single %pair of inline functions, which each compile to
86 * a single @a add instruction: _Rep::_M_data(), and
87 * string::_M_rep(); and the allocation function which gets a
88 * block of raw bytes and with room enough and constructs a _Rep
89 * object at the front.
91 * The reason you want _M_data pointing to the character %array and
92 * not the _Rep is so that the debugger can see the string
93 * contents. (Probably we should add a non-inline member to get
94 * the _Rep for the debugger to use, so users can check the actual
97 * Note that the _Rep object is a POD so that you can have a
98 * static <em>empty string</em> _Rep object already @a constructed before
99 * static constructors have run. The reference-count encoding is
100 * chosen so that a 0 indicates one reference, so you never try to
101 * destroy the empty-string _Rep object.
103 * All but the last paragraph is considered pretty conventional
104 * for a Copy-On-Write C++ string implementation.
106 // 21.3 Template class basic_string
107 template<typename _CharT
, typename _Traits
, typename _Alloc
>
110 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
111 rebind
<_CharT
>::other _CharT_alloc_type
;
112 typedef __gnu_cxx::__alloc_traits
<_CharT_alloc_type
> _CharT_alloc_traits
;
116 typedef _Traits traits_type
;
117 typedef typename
_Traits::char_type value_type
;
118 typedef _Alloc allocator_type
;
119 typedef typename
_CharT_alloc_traits::size_type size_type
;
120 typedef typename
_CharT_alloc_traits::difference_type difference_type
;
121 #if __cplusplus < 201103L
122 typedef typename
_CharT_alloc_type::reference reference
;
123 typedef typename
_CharT_alloc_type::const_reference const_reference
;
125 typedef value_type
& reference
;
126 typedef const value_type
& const_reference
;
128 typedef typename
_CharT_alloc_traits::pointer pointer
;
129 typedef typename
_CharT_alloc_traits::const_pointer const_pointer
;
130 typedef __gnu_cxx::__normal_iterator
<pointer
, basic_string
> iterator
;
131 typedef __gnu_cxx::__normal_iterator
<const_pointer
, basic_string
>
133 typedef std::reverse_iterator
<const_iterator
> const_reverse_iterator
;
134 typedef std::reverse_iterator
<iterator
> reverse_iterator
;
137 // type used for positions in insert, erase etc.
138 typedef iterator __const_iterator
;
141 // _Rep: string representation
143 // 1. String really contains _M_length + 1 characters: due to 21.3.4
144 // must be kept null-terminated.
145 // 2. _M_capacity >= _M_length
146 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
147 // 3. _M_refcount has three states:
148 // -1: leaked, one reference, no ref-copies allowed, non-const.
149 // 0: one reference, non-const.
150 // n>0: n + 1 references, operations require a lock, const.
151 // 4. All fields==0 is an empty string, given the extra storage
152 // beyond-the-end for a null terminator; thus, the shared
153 // empty string representation needs no constructor.
158 size_type _M_capacity
;
159 _Atomic_word _M_refcount
;
162 struct _Rep
: _Rep_base
165 typedef typename
__gnu_cxx::__alloc_traits
<_Alloc
>::template
166 rebind
<char>::other _Raw_bytes_alloc
;
168 // (Public) Data members:
170 // The maximum number of individual char_type elements of an
171 // individual string is determined by _S_max_size. This is the
172 // value that will be returned by max_size(). (Whereas npos
173 // is the maximum number of bytes the allocator can allocate.)
174 // If one was to divvy up the theoretical largest size string,
175 // with a terminating character and m _CharT elements, it'd
177 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
179 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
180 // In addition, this implementation quarters this amount.
181 static const size_type _S_max_size
;
182 static const _CharT _S_terminal
;
184 // The following storage is init'd to 0 by the linker, resulting
185 // (carefully) in an empty string with one reference.
186 static size_type _S_empty_rep_storage
[];
189 _S_empty_rep() _GLIBCXX_NOEXCEPT
191 // NB: Mild hack to avoid strict-aliasing warnings. Note that
192 // _S_empty_rep_storage is never modified and the punning should
193 // be reasonably safe in this case.
194 void* __p
= reinterpret_cast<void*>(&_S_empty_rep_storage
);
195 return *reinterpret_cast<_Rep
*>(__p
);
199 _M_is_leaked() const _GLIBCXX_NOEXCEPT
201 #if defined(__GTHREADS)
202 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
203 // so we need to use an atomic load. However, _M_is_leaked
204 // predicate does not change concurrently (i.e. the string is either
205 // leaked or not), so a relaxed load is enough.
206 return __atomic_load_n(&this->_M_refcount
, __ATOMIC_RELAXED
) < 0;
208 return this->_M_refcount
< 0;
213 _M_is_shared() const _GLIBCXX_NOEXCEPT
215 #if defined(__GTHREADS)
216 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
217 // so we need to use an atomic load. Another thread can drop last
218 // but one reference concurrently with this check, so we need this
219 // load to be acquire to synchronize with release fetch_and_add in
221 if (!__gnu_cxx::__is_single_threaded())
222 return __atomic_load_n(&this->_M_refcount
, __ATOMIC_ACQUIRE
) > 0;
224 return this->_M_refcount
> 0;
228 _M_set_leaked() _GLIBCXX_NOEXCEPT
229 { this->_M_refcount
= -1; }
232 _M_set_sharable() _GLIBCXX_NOEXCEPT
233 { this->_M_refcount
= 0; }
236 _M_set_length_and_sharable(size_type __n
) _GLIBCXX_NOEXCEPT
238 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
239 if (__builtin_expect(this != &_S_empty_rep(), false))
242 this->_M_set_sharable(); // One reference.
243 this->_M_length
= __n
;
244 traits_type::assign(this->_M_refdata()[__n
], _S_terminal
);
245 // grrr. (per 21.3.4)
246 // You cannot leave those LWG people alone for a second.
252 { return reinterpret_cast<_CharT
*>(this + 1); }
255 _M_grab(const _Alloc
& __alloc1
, const _Alloc
& __alloc2
)
257 return (!_M_is_leaked() && __alloc1
== __alloc2
)
258 ? _M_refcopy() : _M_clone(__alloc1
);
263 _S_create(size_type
, size_type
, const _Alloc
&);
266 _M_dispose(const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
268 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
269 if (__builtin_expect(this != &_S_empty_rep(), false))
272 // Be race-detector-friendly. For more info see bits/c++config.
273 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount
);
274 // Decrement of _M_refcount is acq_rel, because:
275 // - all but last decrements need to release to synchronize with
276 // the last decrement that will delete the object.
277 // - the last decrement needs to acquire to synchronize with
278 // all the previous decrements.
279 // - last but one decrement needs to release to synchronize with
280 // the acquire load in _M_is_shared that will conclude that
281 // the object is not shared anymore.
282 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount
,
285 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount
);
292 _M_destroy(const _Alloc
&) throw();
297 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
298 if (__builtin_expect(this != &_S_empty_rep(), false))
300 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount
, 1);
305 _M_clone(const _Alloc
&, size_type __res
= 0);
308 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
309 struct _Alloc_hider
: _Alloc
311 _Alloc_hider(_CharT
* __dat
, const _Alloc
& __a
) _GLIBCXX_NOEXCEPT
312 : _Alloc(__a
), _M_p(__dat
) { }
314 _CharT
* _M_p
; // The actual data.
318 // Data Members (public):
319 // NB: This is an unsigned type, and thus represents the maximum
320 // size that the allocator can hold.
321 /// Value returned by various member functions when they fail.
322 static const size_type npos
= static_cast<size_type
>(-1);
325 // Data Members (private):
326 mutable _Alloc_hider _M_dataplus
;
329 _M_data() const _GLIBCXX_NOEXCEPT
330 { return _M_dataplus
._M_p
; }
333 _M_data(_CharT
* __p
) _GLIBCXX_NOEXCEPT
334 { return (_M_dataplus
._M_p
= __p
); }
337 _M_rep() const _GLIBCXX_NOEXCEPT
338 { return &((reinterpret_cast<_Rep
*> (_M_data()))[-1]); }
340 // For the internal use we have functions similar to `begin'/`end'
341 // but they do not call _M_leak.
343 _M_ibegin() const _GLIBCXX_NOEXCEPT
344 { return iterator(_M_data()); }
347 _M_iend() const _GLIBCXX_NOEXCEPT
348 { return iterator(_M_data() + this->size()); }
351 _M_leak() // for use in begin() & non-const op[]
353 if (!_M_rep()->_M_is_leaked())
358 _M_check(size_type __pos
, const char* __s
) const
360 if (__pos
> this->size())
361 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
362 "this->size() (which is %zu)"),
363 __s
, __pos
, this->size());
368 _M_check_length(size_type __n1
, size_type __n2
, const char* __s
) const
370 if (this->max_size() - (this->size() - __n1
) < __n2
)
371 __throw_length_error(__N(__s
));
374 // NB: _M_limit doesn't check for a bad __pos value.
376 _M_limit(size_type __pos
, size_type __off
) const _GLIBCXX_NOEXCEPT
378 const bool __testoff
= __off
< this->size() - __pos
;
379 return __testoff
? __off
: this->size() - __pos
;
382 // True if _Rep and source do not overlap.
384 _M_disjunct(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
386 return (less
<const _CharT
*>()(__s
, _M_data())
387 || less
<const _CharT
*>()(_M_data() + this->size(), __s
));
390 // When __n = 1 way faster than the general multichar
391 // traits_type::copy/move/assign.
393 _M_copy(_CharT
* __d
, const _CharT
* __s
, size_type __n
) _GLIBCXX_NOEXCEPT
396 traits_type::assign(*__d
, *__s
);
398 traits_type::copy(__d
, __s
, __n
);
402 _M_move(_CharT
* __d
, const _CharT
* __s
, size_type __n
) _GLIBCXX_NOEXCEPT
405 traits_type::assign(*__d
, *__s
);
407 traits_type::move(__d
, __s
, __n
);
411 _M_assign(_CharT
* __d
, size_type __n
, _CharT __c
) _GLIBCXX_NOEXCEPT
414 traits_type::assign(*__d
, __c
);
416 traits_type::assign(__d
, __n
, __c
);
419 // _S_copy_chars is a separate template to permit specialization
420 // to optimize for the common case of pointers as iterators.
421 template<class _Iterator
>
423 _S_copy_chars(_CharT
* __p
, _Iterator __k1
, _Iterator __k2
)
425 for (; __k1
!= __k2
; ++__k1
, (void)++__p
)
426 traits_type::assign(*__p
, *__k1
); // These types are off.
430 _S_copy_chars(_CharT
* __p
, iterator __k1
, iterator __k2
) _GLIBCXX_NOEXCEPT
431 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
434 _S_copy_chars(_CharT
* __p
, const_iterator __k1
, const_iterator __k2
)
436 { _S_copy_chars(__p
, __k1
.base(), __k2
.base()); }
439 _S_copy_chars(_CharT
* __p
, _CharT
* __k1
, _CharT
* __k2
) _GLIBCXX_NOEXCEPT
440 { _M_copy(__p
, __k1
, __k2
- __k1
); }
443 _S_copy_chars(_CharT
* __p
, const _CharT
* __k1
, const _CharT
* __k2
)
445 { _M_copy(__p
, __k1
, __k2
- __k1
); }
448 _S_compare(size_type __n1
, size_type __n2
) _GLIBCXX_NOEXCEPT
450 const difference_type __d
= difference_type(__n1
- __n2
);
452 if (__d
> __gnu_cxx::__numeric_traits
<int>::__max
)
453 return __gnu_cxx::__numeric_traits
<int>::__max
;
454 else if (__d
< __gnu_cxx::__numeric_traits
<int>::__min
)
455 return __gnu_cxx::__numeric_traits
<int>::__min
;
461 _M_mutate(size_type __pos
, size_type __len1
, size_type __len2
);
467 _S_empty_rep() _GLIBCXX_NOEXCEPT
468 { return _Rep::_S_empty_rep(); }
470 #if __cplusplus >= 201703L
471 // A helper type for avoiding boiler-plate.
472 typedef basic_string_view
<_CharT
, _Traits
> __sv_type
;
474 template<typename _Tp
, typename _Res
>
475 using _If_sv
= enable_if_t
<
476 __and_
<is_convertible
<const _Tp
&, __sv_type
>,
477 __not_
<is_convertible
<const _Tp
*, const basic_string
*>>,
478 __not_
<is_convertible
<const _Tp
&, const _CharT
*>>>::value
,
481 // Allows an implicit conversion to __sv_type.
483 _S_to_string_view(__sv_type __svt
) noexcept
486 // Wraps a string_view by explicit conversion and thus
487 // allows to add an internal constructor that does not
488 // participate in overload resolution when a string_view
492 explicit __sv_wrapper(__sv_type __sv
) noexcept
: _M_sv(__sv
) { }
497 * @brief Only internally used: Construct string from a string view
499 * @param __svw string view wrapper.
500 * @param __a Allocator to use.
503 basic_string(__sv_wrapper __svw
, const _Alloc
& __a
)
504 : basic_string(__svw
._M_sv
.data(), __svw
._M_sv
.size(), __a
) { }
508 // Construct/copy/destroy:
509 // NB: We overload ctors in some cases instead of using default
510 // arguments, per 17.4.4.4 para. 2 item 2.
513 * @brief Default constructor creates an empty string.
516 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
518 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc())
520 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc())
525 * @brief Construct an empty string using allocator @a a.
528 basic_string(const _Alloc
& __a
)
529 : _M_dataplus(_S_construct(size_type(), _CharT(), __a
), __a
)
532 // NB: per LWG issue 42, semantics different from IS:
534 * @brief Construct string with copy of value of @a str.
535 * @param __str Source string.
537 basic_string(const basic_string
& __str
)
538 : _M_dataplus(__str
._M_rep()->_M_grab(_Alloc(__str
.get_allocator()),
539 __str
.get_allocator()),
540 __str
.get_allocator())
543 // _GLIBCXX_RESOLVE_LIB_DEFECTS
544 // 2583. no way to supply an allocator for basic_string(str, pos)
546 * @brief Construct string as copy of a substring.
547 * @param __str Source string.
548 * @param __pos Index of first character to copy from.
549 * @param __a Allocator to use.
551 basic_string(const basic_string
& __str
, size_type __pos
,
552 const _Alloc
& __a
= _Alloc());
555 * @brief Construct string as copy of a substring.
556 * @param __str Source string.
557 * @param __pos Index of first character to copy from.
558 * @param __n Number of characters to copy.
560 basic_string(const basic_string
& __str
, size_type __pos
,
563 * @brief Construct string as copy of a substring.
564 * @param __str Source string.
565 * @param __pos Index of first character to copy from.
566 * @param __n Number of characters to copy.
567 * @param __a Allocator to use.
569 basic_string(const basic_string
& __str
, size_type __pos
,
570 size_type __n
, const _Alloc
& __a
);
573 * @brief Construct string initialized by a character %array.
574 * @param __s Source character %array.
575 * @param __n Number of characters to copy.
576 * @param __a Allocator to use (default is default allocator).
578 * NB: @a __s must have at least @a __n characters, '\\0'
579 * has no special meaning.
581 basic_string(const _CharT
* __s
, size_type __n
,
582 const _Alloc
& __a
= _Alloc())
583 : _M_dataplus(_S_construct(__s
, __s
+ __n
, __a
), __a
)
587 * @brief Construct string as copy of a C string.
588 * @param __s Source C string.
589 * @param __a Allocator to use (default is default allocator).
591 #if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
592 // _GLIBCXX_RESOLVE_LIB_DEFECTS
593 // 3076. basic_string CTAD ambiguity
594 template<typename
= _RequireAllocator
<_Alloc
>>
596 basic_string(const _CharT
* __s
, const _Alloc
& __a
= _Alloc())
597 : _M_dataplus(_S_construct(__s
, __s
? __s
+ traits_type::length(__s
) :
598 __s
+ npos
, __a
), __a
)
602 * @brief Construct string as multiple characters.
603 * @param __n Number of characters.
604 * @param __c Character to use.
605 * @param __a Allocator to use (default is default allocator).
607 basic_string(size_type __n
, _CharT __c
, const _Alloc
& __a
= _Alloc())
608 : _M_dataplus(_S_construct(__n
, __c
, __a
), __a
)
611 #if __cplusplus >= 201103L
613 * @brief Move construct string.
614 * @param __str Source string.
616 * The newly-created string contains the exact contents of @a __str.
617 * @a __str is a valid, but unspecified string.
619 basic_string(basic_string
&& __str
) noexcept
620 : _M_dataplus(std::move(__str
._M_dataplus
))
622 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
623 // Make __str use the shared empty string rep.
624 __str
._M_data(_S_empty_rep()._M_refdata());
626 // Rather than allocate an empty string for the rvalue string,
627 // just share ownership with it by incrementing the reference count.
628 // If the rvalue string was the unique owner then there are exactly
630 if (_M_rep()->_M_is_shared())
631 __gnu_cxx::__atomic_add_dispatch(&_M_rep()->_M_refcount
, 1);
633 _M_rep()->_M_refcount
= 1;
638 * @brief Construct string from an initializer %list.
639 * @param __l std::initializer_list of characters.
640 * @param __a Allocator to use (default is default allocator).
642 basic_string(initializer_list
<_CharT
> __l
, const _Alloc
& __a
= _Alloc())
643 : _M_dataplus(_S_construct(__l
.begin(), __l
.end(), __a
), __a
)
646 basic_string(const basic_string
& __str
, const _Alloc
& __a
)
647 : _M_dataplus(__str
._M_rep()->_M_grab(__a
, __str
.get_allocator()), __a
)
650 basic_string(basic_string
&& __str
, const _Alloc
& __a
)
651 : _M_dataplus(__str
._M_data(), __a
)
653 if (__a
== __str
.get_allocator())
655 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
656 __str
._M_data(_S_empty_rep()._M_refdata());
658 __str
._M_data(_S_construct(size_type(), _CharT(), __a
));
662 _M_dataplus
._M_p
= _S_construct(__str
.begin(), __str
.end(), __a
);
666 #if __cplusplus >= 202100L
667 basic_string(nullptr_t
) = delete;
668 basic_string
& operator=(nullptr_t
) = delete;
672 * @brief Construct string as copy of a range.
673 * @param __beg Start of range.
674 * @param __end End of range.
675 * @param __a Allocator to use (default is default allocator).
677 template<class _InputIterator
>
678 basic_string(_InputIterator __beg
, _InputIterator __end
,
679 const _Alloc
& __a
= _Alloc())
680 : _M_dataplus(_S_construct(__beg
, __end
, __a
), __a
)
683 #if __cplusplus >= 201703L
685 * @brief Construct string from a substring of a string_view.
686 * @param __t Source object convertible to string view.
687 * @param __pos The index of the first character to copy from __t.
688 * @param __n The number of characters to copy from __t.
689 * @param __a Allocator to use.
691 template<typename _Tp
,
692 typename
= enable_if_t
<is_convertible_v
<const _Tp
&, __sv_type
>>>
693 basic_string(const _Tp
& __t
, size_type __pos
, size_type __n
,
694 const _Alloc
& __a
= _Alloc())
695 : basic_string(_S_to_string_view(__t
).substr(__pos
, __n
), __a
) { }
698 * @brief Construct string from a string_view.
699 * @param __t Source object convertible to string view.
700 * @param __a Allocator to use (default is default allocator).
702 template<typename _Tp
, typename
= _If_sv
<_Tp
, void>>
704 basic_string(const _Tp
& __t
, const _Alloc
& __a
= _Alloc())
705 : basic_string(__sv_wrapper(_S_to_string_view(__t
)), __a
) { }
709 * @brief Destroy the string instance.
711 ~basic_string() _GLIBCXX_NOEXCEPT
712 { _M_rep()->_M_dispose(this->get_allocator()); }
715 * @brief Assign the value of @a str to this string.
716 * @param __str Source string.
719 operator=(const basic_string
& __str
)
720 { return this->assign(__str
); }
723 * @brief Copy contents of @a s into this string.
724 * @param __s Source null-terminated string.
727 operator=(const _CharT
* __s
)
728 { return this->assign(__s
); }
731 * @brief Set value to string of length 1.
732 * @param __c Source character.
734 * Assigning to a character makes this string length 1 and
735 * (*this)[0] == @a c.
738 operator=(_CharT __c
)
740 this->assign(1, __c
);
744 #if __cplusplus >= 201103L
746 * @brief Move assign the value of @a str to this string.
747 * @param __str Source string.
749 * The contents of @a str are moved into this string (without copying).
750 * @a str is a valid, but unspecified string.
753 operator=(basic_string
&& __str
)
754 _GLIBCXX_NOEXCEPT_IF(allocator_traits
<_Alloc
>::is_always_equal::value
)
762 * @brief Set value to string constructed from initializer %list.
763 * @param __l std::initializer_list.
766 operator=(initializer_list
<_CharT
> __l
)
768 this->assign(__l
.begin(), __l
.size());
773 #if __cplusplus >= 201703L
775 * @brief Set value to string constructed from a string_view.
776 * @param __svt An object convertible to string_view.
778 template<typename _Tp
>
779 _If_sv
<_Tp
, basic_string
&>
780 operator=(const _Tp
& __svt
)
781 { return this->assign(__svt
); }
784 * @brief Convert to a string_view.
785 * @return A string_view.
787 operator __sv_type() const noexcept
788 { return __sv_type(data(), size()); }
793 * Returns a read/write iterator that points to the first character in
794 * the %string. Unshares the string.
797 begin() // FIXME C++11: should be noexcept.
800 return iterator(_M_data());
804 * Returns a read-only (constant) iterator that points to the first
805 * character in the %string.
808 begin() const _GLIBCXX_NOEXCEPT
809 { return const_iterator(_M_data()); }
812 * Returns a read/write iterator that points one past the last
813 * character in the %string. Unshares the string.
816 end() // FIXME C++11: should be noexcept.
819 return iterator(_M_data() + this->size());
823 * Returns a read-only (constant) iterator that points one past the
824 * last character in the %string.
827 end() const _GLIBCXX_NOEXCEPT
828 { return const_iterator(_M_data() + this->size()); }
831 * Returns a read/write reverse iterator that points to the last
832 * character in the %string. Iteration is done in reverse element
833 * order. Unshares the string.
836 rbegin() // FIXME C++11: should be noexcept.
837 { return reverse_iterator(this->end()); }
840 * Returns a read-only (constant) reverse iterator that points
841 * to the last character in the %string. Iteration is done in
842 * reverse element order.
844 const_reverse_iterator
845 rbegin() const _GLIBCXX_NOEXCEPT
846 { return const_reverse_iterator(this->end()); }
849 * Returns a read/write reverse iterator that points to one before the
850 * first character in the %string. Iteration is done in reverse
851 * element order. Unshares the string.
854 rend() // FIXME C++11: should be noexcept.
855 { return reverse_iterator(this->begin()); }
858 * Returns a read-only (constant) reverse iterator that points
859 * to one before the first character in the %string. Iteration
860 * is done in reverse element order.
862 const_reverse_iterator
863 rend() const _GLIBCXX_NOEXCEPT
864 { return const_reverse_iterator(this->begin()); }
866 #if __cplusplus >= 201103L
868 * Returns a read-only (constant) iterator that points to the first
869 * character in the %string.
872 cbegin() const noexcept
873 { return const_iterator(this->_M_data()); }
876 * Returns a read-only (constant) iterator that points one past the
877 * last character in the %string.
880 cend() const noexcept
881 { return const_iterator(this->_M_data() + this->size()); }
884 * Returns a read-only (constant) reverse iterator that points
885 * to the last character in the %string. Iteration is done in
886 * reverse element order.
888 const_reverse_iterator
889 crbegin() const noexcept
890 { return const_reverse_iterator(this->end()); }
893 * Returns a read-only (constant) reverse iterator that points
894 * to one before the first character in the %string. Iteration
895 * is done in reverse element order.
897 const_reverse_iterator
898 crend() const noexcept
899 { return const_reverse_iterator(this->begin()); }
905 /// Returns the number of characters in the string, not including any
906 /// null-termination.
908 size() const _GLIBCXX_NOEXCEPT
910 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 && __OPTIMIZE__
911 if (_S_empty_rep()._M_length
!= 0)
912 __builtin_unreachable();
914 return _M_rep()->_M_length
;
917 /// Returns the number of characters in the string, not including any
918 /// null-termination.
920 length() const _GLIBCXX_NOEXCEPT
923 /// Returns the size() of the largest possible %string.
925 max_size() const _GLIBCXX_NOEXCEPT
926 { return _Rep::_S_max_size
; }
929 * @brief Resizes the %string to the specified number of characters.
930 * @param __n Number of characters the %string should contain.
931 * @param __c Character to fill any new elements.
933 * This function will %resize the %string to the specified
934 * number of characters. If the number is smaller than the
935 * %string's current size the %string is truncated, otherwise
936 * the %string is extended and new elements are %set to @a __c.
939 resize(size_type __n
, _CharT __c
);
942 * @brief Resizes the %string to the specified number of characters.
943 * @param __n Number of characters the %string should contain.
945 * This function will resize the %string to the specified length. If
946 * the new size is smaller than the %string's current size the %string
947 * is truncated, otherwise the %string is extended and new characters
948 * are default-constructed. For basic types such as char, this means
952 resize(size_type __n
)
953 { this->resize(__n
, _CharT()); }
955 #if __cplusplus >= 201103L
956 #pragma GCC diagnostic push
957 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
958 /// A non-binding request to reduce capacity() to size().
960 shrink_to_fit() noexcept
962 #pragma GCC diagnostic pop
965 #ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23
966 /** Resize the string and call a function to fill it.
968 * @param __n The maximum size requested.
969 * @param __op A callable object that writes characters to the string.
971 * This is a low-level function that is easy to misuse, be careful.
973 * Calling `str.resize_and_overwrite(n, op)` will reserve at least `n`
974 * characters in `str`, evaluate `n2 = std::move(op)(str.data(), n)`,
975 * and finally set the string length to `n2` (adding a null terminator
976 * at the end). The function object `op` is allowed to write to the
977 * extra capacity added by the initial reserve operation, which is not
978 * allowed if you just call `str.reserve(n)` yourself.
980 * This can be used to efficiently fill a `string` buffer without the
981 * overhead of zero-initializing characters that will be overwritten
984 * The callable `op` must not access the string directly (only through
985 * the pointer passed as its first argument), must not write more than
986 * `n` characters to the string, must return a value no greater than `n`,
987 * and must ensure that all characters up to the returned length are
988 * valid after it returns (i.e. there must be no uninitialized values
989 * left in the string after the call, because accessing them would
990 * have undefined behaviour). If `op` exits by throwing an exception
991 * the behaviour is undefined.
995 template<typename _Operation
>
997 resize_and_overwrite(size_type __n
, _Operation __op
);
998 #endif // __glibcxx_string_resize_and_overwrite
1000 #if __cplusplus >= 201103L
1001 /// Non-standard version of resize_and_overwrite for C++11 and above.
1002 template<typename _Operation
>
1004 __resize_and_overwrite(size_type __n
, _Operation __op
);
1008 * Returns the total number of characters that the %string can hold
1009 * before needing to allocate more memory.
1012 capacity() const _GLIBCXX_NOEXCEPT
1013 { return _M_rep()->_M_capacity
; }
1016 * @brief Attempt to preallocate enough memory for specified number of
1018 * @param __res_arg Number of characters required.
1019 * @throw std::length_error If @a __res_arg exceeds @c max_size().
1021 * This function attempts to reserve enough memory for the
1022 * %string to hold the specified number of characters. If the
1023 * number requested is more than max_size(), length_error is
1026 * The advantage of this function is that if optimal code is a
1027 * necessity and the user can determine the string length that will be
1028 * required, the user can reserve the memory in %advance, and thus
1029 * prevent a possible reallocation of memory and copying of %string
1033 reserve(size_type __res_arg
);
1035 /// Equivalent to shrink_to_fit().
1036 #if __cplusplus > 201703L
1037 [[deprecated("use shrink_to_fit() instead")]]
1043 * Erases the string, making it empty.
1045 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
1047 clear() _GLIBCXX_NOEXCEPT
1049 if (_M_rep()->_M_is_shared())
1051 _M_rep()->_M_dispose(this->get_allocator());
1052 _M_data(_S_empty_rep()._M_refdata());
1055 _M_rep()->_M_set_length_and_sharable(0);
1058 // PR 56166: this should not throw.
1061 { _M_mutate(0, this->size(), 0); }
1065 * Returns true if the %string is empty. Equivalent to
1066 * <code>*this == ""</code>.
1068 _GLIBCXX_NODISCARD
bool
1069 empty() const _GLIBCXX_NOEXCEPT
1070 { return this->size() == 0; }
1074 * @brief Subscript access to the data contained in the %string.
1075 * @param __pos The index of the character to access.
1076 * @return Read-only (constant) reference to the character.
1078 * This operator allows for easy, array-style, data access.
1079 * Note that data access with this operator is unchecked and
1080 * out_of_range lookups are not defined. (For checked lookups
1084 operator[] (size_type __pos
) const _GLIBCXX_NOEXCEPT
1086 __glibcxx_assert(__pos
<= size());
1087 return _M_data()[__pos
];
1091 * @brief Subscript access to the data contained in the %string.
1092 * @param __pos The index of the character to access.
1093 * @return Read/write reference to the character.
1095 * This operator allows for easy, array-style, data access.
1096 * Note that data access with this operator is unchecked and
1097 * out_of_range lookups are not defined. (For checked lookups
1098 * see at().) Unshares the string.
1101 operator[](size_type __pos
)
1103 // Allow pos == size() both in C++98 mode, as v3 extension,
1104 // and in C++11 mode.
1105 __glibcxx_assert(__pos
<= size());
1106 // In pedantic mode be strict in C++98 mode.
1107 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus
>= 201103L || __pos
< size());
1109 return _M_data()[__pos
];
1113 * @brief Provides access to the data contained in the %string.
1114 * @param __n The index of the character to access.
1115 * @return Read-only (const) reference to the character.
1116 * @throw std::out_of_range If @a n is an invalid index.
1118 * This function provides for safer data access. The parameter is
1119 * first checked that it is in the range of the string. The function
1120 * throws out_of_range if the check fails.
1123 at(size_type __n
) const
1125 if (__n
>= this->size())
1126 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1127 "(which is %zu) >= this->size() "
1130 return _M_data()[__n
];
1134 * @brief Provides access to the data contained in the %string.
1135 * @param __n The index of the character to access.
1136 * @return Read/write reference to the character.
1137 * @throw std::out_of_range If @a n is an invalid index.
1139 * This function provides for safer data access. The parameter is
1140 * first checked that it is in the range of the string. The function
1141 * throws out_of_range if the check fails. Success results in
1142 * unsharing the string.
1148 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1149 "(which is %zu) >= this->size() "
1153 return _M_data()[__n
];
1156 #if __cplusplus >= 201103L
1158 * Returns a read/write reference to the data at the first
1159 * element of the %string.
1164 __glibcxx_assert(!empty());
1165 return operator[](0);
1169 * Returns a read-only (constant) reference to the data at the first
1170 * element of the %string.
1173 front() const noexcept
1175 __glibcxx_assert(!empty());
1176 return operator[](0);
1180 * Returns a read/write reference to the data at the last
1181 * element of the %string.
1186 __glibcxx_assert(!empty());
1187 return operator[](this->size() - 1);
1191 * Returns a read-only (constant) reference to the data at the
1192 * last element of the %string.
1195 back() const noexcept
1197 __glibcxx_assert(!empty());
1198 return operator[](this->size() - 1);
1204 * @brief Append a string to this string.
1205 * @param __str The string to append.
1206 * @return Reference to this string.
1209 operator+=(const basic_string
& __str
)
1210 { return this->append(__str
); }
1213 * @brief Append a C string.
1214 * @param __s The C string to append.
1215 * @return Reference to this string.
1218 operator+=(const _CharT
* __s
)
1219 { return this->append(__s
); }
1222 * @brief Append a character.
1223 * @param __c The character to append.
1224 * @return Reference to this string.
1227 operator+=(_CharT __c
)
1229 this->push_back(__c
);
1233 #if __cplusplus >= 201103L
1235 * @brief Append an initializer_list of characters.
1236 * @param __l The initializer_list of characters to be appended.
1237 * @return Reference to this string.
1240 operator+=(initializer_list
<_CharT
> __l
)
1241 { return this->append(__l
.begin(), __l
.size()); }
1244 #if __cplusplus >= 201703L
1246 * @brief Append a string_view.
1247 * @param __svt The object convertible to string_view to be appended.
1248 * @return Reference to this string.
1250 template<typename _Tp
>
1251 _If_sv
<_Tp
, basic_string
&>
1252 operator+=(const _Tp
& __svt
)
1253 { return this->append(__svt
); }
1257 * @brief Append a string to this string.
1258 * @param __str The string to append.
1259 * @return Reference to this string.
1262 append(const basic_string
& __str
);
1265 * @brief Append a substring.
1266 * @param __str The string to append.
1267 * @param __pos Index of the first character of str to append.
1268 * @param __n The number of characters to append.
1269 * @return Reference to this string.
1270 * @throw std::out_of_range if @a __pos is not a valid index.
1272 * This function appends @a __n characters from @a __str
1273 * starting at @a __pos to this string. If @a __n is is larger
1274 * than the number of available characters in @a __str, the
1275 * remainder of @a __str is appended.
1278 append(const basic_string
& __str
, size_type __pos
, size_type __n
= npos
);
1281 * @brief Append a C substring.
1282 * @param __s The C string to append.
1283 * @param __n The number of characters to append.
1284 * @return Reference to this string.
1287 append(const _CharT
* __s
, size_type __n
);
1290 * @brief Append a C string.
1291 * @param __s The C string to append.
1292 * @return Reference to this string.
1295 append(const _CharT
* __s
)
1297 __glibcxx_requires_string(__s
);
1298 return this->append(__s
, traits_type::length(__s
));
1302 * @brief Append multiple characters.
1303 * @param __n The number of characters to append.
1304 * @param __c The character to use.
1305 * @return Reference to this string.
1307 * Appends __n copies of __c to this string.
1310 append(size_type __n
, _CharT __c
);
1312 #if __cplusplus >= 201103L
1314 * @brief Append an initializer_list of characters.
1315 * @param __l The initializer_list of characters to append.
1316 * @return Reference to this string.
1319 append(initializer_list
<_CharT
> __l
)
1320 { return this->append(__l
.begin(), __l
.size()); }
1324 * @brief Append a range of characters.
1325 * @param __first Iterator referencing the first character to append.
1326 * @param __last Iterator marking the end of the range.
1327 * @return Reference to this string.
1329 * Appends characters in the range [__first,__last) to this string.
1331 template<class _InputIterator
>
1333 append(_InputIterator __first
, _InputIterator __last
)
1334 { return this->replace(_M_iend(), _M_iend(), __first
, __last
); }
1336 #if __cplusplus >= 201703L
1338 * @brief Append a string_view.
1339 * @param __svt The object convertible to string_view to be appended.
1340 * @return Reference to this string.
1342 template<typename _Tp
>
1343 _If_sv
<_Tp
, basic_string
&>
1344 append(const _Tp
& __svt
)
1346 __sv_type __sv
= __svt
;
1347 return this->append(__sv
.data(), __sv
.size());
1351 * @brief Append a range of characters from a string_view.
1352 * @param __svt The object convertible to string_view to be appended
1354 * @param __pos The position in the string_view to append from.
1355 * @param __n The number of characters to append from the string_view.
1356 * @return Reference to this string.
1358 template<typename _Tp
>
1359 _If_sv
<_Tp
, basic_string
&>
1360 append(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
1362 __sv_type __sv
= __svt
;
1363 return append(__sv
.data()
1364 + std::__sv_check(__sv
.size(), __pos
, "basic_string::append"),
1365 std::__sv_limit(__sv
.size(), __pos
, __n
));
1370 * @brief Append a single character.
1371 * @param __c Character to append.
1374 push_back(_CharT __c
)
1376 const size_type __len
= 1 + this->size();
1377 if (__len
> this->capacity() || _M_rep()->_M_is_shared())
1378 this->reserve(__len
);
1379 traits_type::assign(_M_data()[this->size()], __c
);
1380 _M_rep()->_M_set_length_and_sharable(__len
);
1384 * @brief Set value to contents of another string.
1385 * @param __str Source string to use.
1386 * @return Reference to this string.
1389 assign(const basic_string
& __str
);
1391 #if __cplusplus >= 201103L
1393 * @brief Set value to contents of another string.
1394 * @param __str Source string to use.
1395 * @return Reference to this string.
1397 * This function sets this string to the exact contents of @a __str.
1398 * @a __str is a valid, but unspecified string.
1401 assign(basic_string
&& __str
)
1402 noexcept(allocator_traits
<_Alloc
>::is_always_equal::value
)
1410 * @brief Set value to a substring of a string.
1411 * @param __str The string to use.
1412 * @param __pos Index of the first character of str.
1413 * @param __n Number of characters to use.
1414 * @return Reference to this string.
1415 * @throw std::out_of_range if @a pos is not a valid index.
1417 * This function sets this string to the substring of @a __str
1418 * consisting of @a __n characters at @a __pos. If @a __n is
1419 * is larger than the number of available characters in @a
1420 * __str, the remainder of @a __str is used.
1423 assign(const basic_string
& __str
, size_type __pos
, size_type __n
= npos
)
1424 { return this->assign(__str
._M_data()
1425 + __str
._M_check(__pos
, "basic_string::assign"),
1426 __str
._M_limit(__pos
, __n
)); }
1429 * @brief Set value to a C substring.
1430 * @param __s The C string to use.
1431 * @param __n Number of characters to use.
1432 * @return Reference to this string.
1434 * This function sets the value of this string to the first @a __n
1435 * characters of @a __s. If @a __n is is larger than the number of
1436 * available characters in @a __s, the remainder of @a __s is used.
1439 assign(const _CharT
* __s
, size_type __n
);
1442 * @brief Set value to contents of a C string.
1443 * @param __s The C string to use.
1444 * @return Reference to this string.
1446 * This function sets the value of this string to the value of @a __s.
1447 * The data is copied, so there is no dependence on @a __s once the
1451 assign(const _CharT
* __s
)
1453 __glibcxx_requires_string(__s
);
1454 return this->assign(__s
, traits_type::length(__s
));
1458 * @brief Set value to multiple characters.
1459 * @param __n Length of the resulting string.
1460 * @param __c The character to use.
1461 * @return Reference to this string.
1463 * This function sets the value of this string to @a __n copies of
1467 assign(size_type __n
, _CharT __c
)
1468 { return _M_replace_aux(size_type(0), this->size(), __n
, __c
); }
1471 * @brief Set value to a range of characters.
1472 * @param __first Iterator referencing the first character to append.
1473 * @param __last Iterator marking the end of the range.
1474 * @return Reference to this string.
1476 * Sets value of string to characters in the range [__first,__last).
1478 template<class _InputIterator
>
1480 assign(_InputIterator __first
, _InputIterator __last
)
1481 { return this->replace(_M_ibegin(), _M_iend(), __first
, __last
); }
1483 #if __cplusplus >= 201103L
1485 * @brief Set value to an initializer_list of characters.
1486 * @param __l The initializer_list of characters to assign.
1487 * @return Reference to this string.
1490 assign(initializer_list
<_CharT
> __l
)
1491 { return this->assign(__l
.begin(), __l
.size()); }
1494 #if __cplusplus >= 201703L
1496 * @brief Set value from a string_view.
1497 * @param __svt The source object convertible to string_view.
1498 * @return Reference to this string.
1500 template<typename _Tp
>
1501 _If_sv
<_Tp
, basic_string
&>
1502 assign(const _Tp
& __svt
)
1504 __sv_type __sv
= __svt
;
1505 return this->assign(__sv
.data(), __sv
.size());
1509 * @brief Set value from a range of characters in a string_view.
1510 * @param __svt The source object convertible to string_view.
1511 * @param __pos The position in the string_view to assign from.
1512 * @param __n The number of characters to assign.
1513 * @return Reference to this string.
1515 template<typename _Tp
>
1516 _If_sv
<_Tp
, basic_string
&>
1517 assign(const _Tp
& __svt
, size_type __pos
, size_type __n
= npos
)
1519 __sv_type __sv
= __svt
;
1520 return assign(__sv
.data()
1521 + std::__sv_check(__sv
.size(), __pos
, "basic_string::assign"),
1522 std::__sv_limit(__sv
.size(), __pos
, __n
));
1527 * @brief Insert multiple characters.
1528 * @param __p Iterator referencing location in string to insert at.
1529 * @param __n Number of characters to insert
1530 * @param __c The character to insert.
1531 * @throw std::length_error If new length exceeds @c max_size().
1533 * Inserts @a __n copies of character @a __c starting at the
1534 * position referenced by iterator @a __p. If adding
1535 * characters causes the length to exceed max_size(),
1536 * length_error is thrown. The value of the string doesn't
1537 * change if an error is thrown.
1540 insert(iterator __p
, size_type __n
, _CharT __c
)
1541 { this->replace(__p
, __p
, __n
, __c
); }
1544 * @brief Insert a range of characters.
1545 * @param __p Iterator referencing location in string to insert at.
1546 * @param __beg Start of range.
1547 * @param __end End of range.
1548 * @throw std::length_error If new length exceeds @c max_size().
1550 * Inserts characters in range [__beg,__end). If adding
1551 * characters causes the length to exceed max_size(),
1552 * length_error is thrown. The value of the string doesn't
1553 * change if an error is thrown.
1555 template<class _InputIterator
>
1557 insert(iterator __p
, _InputIterator __beg
, _InputIterator __end
)
1558 { this->replace(__p
, __p
, __beg
, __end
); }
1560 #if __cplusplus >= 201103L
1562 * @brief Insert an initializer_list of characters.
1563 * @param __p Iterator referencing location in string to insert at.
1564 * @param __l The initializer_list of characters to insert.
1565 * @throw std::length_error If new length exceeds @c max_size().
1568 insert(iterator __p
, initializer_list
<_CharT
> __l
)
1570 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
1571 this->insert(__p
- _M_ibegin(), __l
.begin(), __l
.size());
1576 * @brief Insert value of a string.
1577 * @param __pos1 Position in string to insert at.
1578 * @param __str The string to insert.
1579 * @return Reference to this string.
1580 * @throw std::length_error If new length exceeds @c max_size().
1582 * Inserts value of @a __str starting at @a __pos1. If adding
1583 * characters causes the length to exceed max_size(),
1584 * length_error is thrown. The value of the string doesn't
1585 * change if an error is thrown.
1588 insert(size_type __pos1
, const basic_string
& __str
)
1589 { return this->insert(__pos1
, __str
, size_type(0), __str
.size()); }
1592 * @brief Insert a substring.
1593 * @param __pos1 Position in string to insert at.
1594 * @param __str The string to insert.
1595 * @param __pos2 Start of characters in str to insert.
1596 * @param __n Number of characters to insert.
1597 * @return Reference to this string.
1598 * @throw std::length_error If new length exceeds @c max_size().
1599 * @throw std::out_of_range If @a pos1 > size() or
1600 * @a __pos2 > @a str.size().
1602 * Starting at @a pos1, insert @a __n character of @a __str
1603 * beginning with @a __pos2. If adding characters causes the
1604 * length to exceed max_size(), length_error is thrown. If @a
1605 * __pos1 is beyond the end of this string or @a __pos2 is
1606 * beyond the end of @a __str, out_of_range is thrown. The
1607 * value of the string doesn't change if an error is thrown.
1610 insert(size_type __pos1
, const basic_string
& __str
,
1611 size_type __pos2
, size_type __n
= npos
)
1612 { return this->insert(__pos1
, __str
._M_data()
1613 + __str
._M_check(__pos2
, "basic_string::insert"),
1614 __str
._M_limit(__pos2
, __n
)); }
1617 * @brief Insert a C substring.
1618 * @param __pos Position in string to insert at.
1619 * @param __s The C string to insert.
1620 * @param __n The number of characters to insert.
1621 * @return Reference to this string.
1622 * @throw std::length_error If new length exceeds @c max_size().
1623 * @throw std::out_of_range If @a __pos is beyond the end of this
1626 * Inserts the first @a __n characters of @a __s starting at @a
1627 * __pos. If adding characters causes the length to exceed
1628 * max_size(), length_error is thrown. If @a __pos is beyond
1629 * end(), out_of_range is thrown. The value of the string
1630 * doesn't change if an error is thrown.
1633 insert(size_type __pos
, const _CharT
* __s
, size_type __n
);
1636 * @brief Insert a C string.
1637 * @param __pos Position in string to insert at.
1638 * @param __s The C string to insert.
1639 * @return Reference to this string.
1640 * @throw std::length_error If new length exceeds @c max_size().
1641 * @throw std::out_of_range If @a pos is beyond the end of this
1644 * Inserts the first @a n characters of @a __s starting at @a __pos. If
1645 * adding characters causes the length to exceed max_size(),
1646 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
1647 * thrown. The value of the string doesn't change if an error is
1651 insert(size_type __pos
, const _CharT
* __s
)
1653 __glibcxx_requires_string(__s
);
1654 return this->insert(__pos
, __s
, traits_type::length(__s
));
1658 * @brief Insert multiple characters.
1659 * @param __pos Index in string to insert at.
1660 * @param __n Number of characters to insert
1661 * @param __c The character to insert.
1662 * @return Reference to this string.
1663 * @throw std::length_error If new length exceeds @c max_size().
1664 * @throw std::out_of_range If @a __pos is beyond the end of this
1667 * Inserts @a __n copies of character @a __c starting at index
1668 * @a __pos. If adding characters causes the length to exceed
1669 * max_size(), length_error is thrown. If @a __pos > length(),
1670 * out_of_range is thrown. The value of the string doesn't
1671 * change if an error is thrown.
1674 insert(size_type __pos
, size_type __n
, _CharT __c
)
1675 { return _M_replace_aux(_M_check(__pos
, "basic_string::insert"),
1676 size_type(0), __n
, __c
); }
1679 * @brief Insert one character.
1680 * @param __p Iterator referencing position in string to insert at.
1681 * @param __c The character to insert.
1682 * @return Iterator referencing newly inserted char.
1683 * @throw std::length_error If new length exceeds @c max_size().
1685 * Inserts character @a __c at position referenced by @a __p.
1686 * If adding character causes the length to exceed max_size(),
1687 * length_error is thrown. If @a __p is beyond end of string,
1688 * out_of_range is thrown. The value of the string doesn't
1689 * change if an error is thrown.
1692 insert(iterator __p
, _CharT __c
)
1694 _GLIBCXX_DEBUG_PEDASSERT(__p
>= _M_ibegin() && __p
<= _M_iend());
1695 const size_type __pos
= __p
- _M_ibegin();
1696 _M_replace_aux(__pos
, size_type(0), size_type(1), __c
);
1697 _M_rep()->_M_set_leaked();
1698 return iterator(_M_data() + __pos
);
1701 #if __cplusplus >= 201703L
1703 * @brief Insert a string_view.
1704 * @param __pos Position in string to insert at.
1705 * @param __svt The object convertible to string_view to insert.
1706 * @return Reference to this string.
1708 template<typename _Tp
>
1709 _If_sv
<_Tp
, basic_string
&>
1710 insert(size_type __pos
, const _Tp
& __svt
)
1712 __sv_type __sv
= __svt
;
1713 return this->insert(__pos
, __sv
.data(), __sv
.size());
1717 * @brief Insert a string_view.
1718 * @param __pos1 Position in string to insert at.
1719 * @param __svt The object convertible to string_view to insert from.
1720 * @param __pos2 Position in string_view to insert from.
1721 * @param __n The number of characters to insert.
1722 * @return Reference to this string.
1724 template<typename _Tp
>
1725 _If_sv
<_Tp
, basic_string
&>
1726 insert(size_type __pos1
, const _Tp
& __svt
,
1727 size_type __pos2
, size_type __n
= npos
)
1729 __sv_type __sv
= __svt
;
1730 return this->replace(__pos1
, size_type(0), __sv
.data()
1731 + std::__sv_check(__sv
.size(), __pos2
, "basic_string::insert"),
1732 std::__sv_limit(__sv
.size(), __pos2
, __n
));
1737 * @brief Remove characters.
1738 * @param __pos Index of first character to remove (default 0).
1739 * @param __n Number of characters to remove (default remainder).
1740 * @return Reference to this string.
1741 * @throw std::out_of_range If @a pos is beyond the end of this
1744 * Removes @a __n characters from this string starting at @a
1745 * __pos. The length of the string is reduced by @a __n. If
1746 * there are < @a __n characters to remove, the remainder of
1747 * the string is truncated. If @a __p is beyond end of string,
1748 * out_of_range is thrown. The value of the string doesn't
1749 * change if an error is thrown.
1752 erase(size_type __pos
= 0, size_type __n
= npos
)
1754 _M_mutate(_M_check(__pos
, "basic_string::erase"),
1755 _M_limit(__pos
, __n
), size_type(0));
1760 * @brief Remove one character.
1761 * @param __position Iterator referencing the character to remove.
1762 * @return iterator referencing same location after removal.
1764 * Removes the character at @a __position from this string. The value
1765 * of the string doesn't change if an error is thrown.
1768 erase(iterator __position
)
1770 _GLIBCXX_DEBUG_PEDASSERT(__position
>= _M_ibegin()
1771 && __position
< _M_iend());
1772 const size_type __pos
= __position
- _M_ibegin();
1773 _M_mutate(__pos
, size_type(1), size_type(0));
1774 _M_rep()->_M_set_leaked();
1775 return iterator(_M_data() + __pos
);
1779 * @brief Remove a range of characters.
1780 * @param __first Iterator referencing the first character to remove.
1781 * @param __last Iterator referencing the end of the range.
1782 * @return Iterator referencing location of first after removal.
1784 * Removes the characters in the range [first,last) from this string.
1785 * The value of the string doesn't change if an error is thrown.
1788 erase(iterator __first
, iterator __last
);
1790 #if __cplusplus >= 201103L
1792 * @brief Remove the last character.
1794 * The string must be non-empty.
1797 pop_back() // FIXME C++11: should be noexcept.
1799 __glibcxx_assert(!empty());
1800 erase(size() - 1, 1);
1805 * @brief Replace characters with value from another string.
1806 * @param __pos Index of first character to replace.
1807 * @param __n Number of characters to be replaced.
1808 * @param __str String to insert.
1809 * @return Reference to this string.
1810 * @throw std::out_of_range If @a pos is beyond the end of this
1812 * @throw std::length_error If new length exceeds @c max_size().
1814 * Removes the characters in the range [__pos,__pos+__n) from
1815 * this string. In place, the value of @a __str is inserted.
1816 * If @a __pos is beyond end of string, out_of_range is thrown.
1817 * If the length of the result exceeds max_size(), length_error
1818 * is thrown. The value of the string doesn't change if an
1822 replace(size_type __pos
, size_type __n
, const basic_string
& __str
)
1823 { return this->replace(__pos
, __n
, __str
._M_data(), __str
.size()); }
1826 * @brief Replace characters with value from another string.
1827 * @param __pos1 Index of first character to replace.
1828 * @param __n1 Number of characters to be replaced.
1829 * @param __str String to insert.
1830 * @param __pos2 Index of first character of str to use.
1831 * @param __n2 Number of characters from str to use.
1832 * @return Reference to this string.
1833 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
1835 * @throw std::length_error If new length exceeds @c max_size().
1837 * Removes the characters in the range [__pos1,__pos1 + n) from this
1838 * string. In place, the value of @a __str is inserted. If @a __pos is
1839 * beyond end of string, out_of_range is thrown. If the length of the
1840 * result exceeds max_size(), length_error is thrown. The value of the
1841 * string doesn't change if an error is thrown.
1844 replace(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
1845 size_type __pos2
, size_type __n2
= npos
)
1846 { return this->replace(__pos1
, __n1
, __str
._M_data()
1847 + __str
._M_check(__pos2
, "basic_string::replace"),
1848 __str
._M_limit(__pos2
, __n2
)); }
1851 * @brief Replace characters with value of a C substring.
1852 * @param __pos Index of first character to replace.
1853 * @param __n1 Number of characters to be replaced.
1854 * @param __s C string to insert.
1855 * @param __n2 Number of characters from @a s to use.
1856 * @return Reference to this string.
1857 * @throw std::out_of_range If @a pos1 > size().
1858 * @throw std::length_error If new length exceeds @c max_size().
1860 * Removes the characters in the range [__pos,__pos + __n1)
1861 * from this string. In place, the first @a __n2 characters of
1862 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
1863 * @a __pos is beyond end of string, out_of_range is thrown. If
1864 * the length of result exceeds max_size(), length_error is
1865 * thrown. The value of the string doesn't change if an error
1869 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
1873 * @brief Replace characters with value of a C string.
1874 * @param __pos Index of first character to replace.
1875 * @param __n1 Number of characters to be replaced.
1876 * @param __s C string to insert.
1877 * @return Reference to this string.
1878 * @throw std::out_of_range If @a pos > size().
1879 * @throw std::length_error If new length exceeds @c max_size().
1881 * Removes the characters in the range [__pos,__pos + __n1)
1882 * from this string. In place, the characters of @a __s are
1883 * inserted. If @a __pos is beyond end of string, out_of_range
1884 * is thrown. If the length of result exceeds max_size(),
1885 * length_error is thrown. The value of the string doesn't
1886 * change if an error is thrown.
1889 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
)
1891 __glibcxx_requires_string(__s
);
1892 return this->replace(__pos
, __n1
, __s
, traits_type::length(__s
));
1896 * @brief Replace characters with multiple characters.
1897 * @param __pos Index of first character to replace.
1898 * @param __n1 Number of characters to be replaced.
1899 * @param __n2 Number of characters to insert.
1900 * @param __c Character to insert.
1901 * @return Reference to this string.
1902 * @throw std::out_of_range If @a __pos > size().
1903 * @throw std::length_error If new length exceeds @c max_size().
1905 * Removes the characters in the range [pos,pos + n1) from this
1906 * string. In place, @a __n2 copies of @a __c are inserted.
1907 * If @a __pos is beyond end of string, out_of_range is thrown.
1908 * If the length of result exceeds max_size(), length_error is
1909 * thrown. The value of the string doesn't change if an error
1913 replace(size_type __pos
, size_type __n1
, size_type __n2
, _CharT __c
)
1914 { return _M_replace_aux(_M_check(__pos
, "basic_string::replace"),
1915 _M_limit(__pos
, __n1
), __n2
, __c
); }
1918 * @brief Replace range of characters with string.
1919 * @param __i1 Iterator referencing start of range to replace.
1920 * @param __i2 Iterator referencing end of range to replace.
1921 * @param __str String value to insert.
1922 * @return Reference to this string.
1923 * @throw std::length_error If new length exceeds @c max_size().
1925 * Removes the characters in the range [__i1,__i2). In place,
1926 * the value of @a __str is inserted. If the length of result
1927 * exceeds max_size(), length_error is thrown. The value of
1928 * the string doesn't change if an error is thrown.
1931 replace(iterator __i1
, iterator __i2
, const basic_string
& __str
)
1932 { return this->replace(__i1
, __i2
, __str
._M_data(), __str
.size()); }
1935 * @brief Replace range of characters with C substring.
1936 * @param __i1 Iterator referencing start of range to replace.
1937 * @param __i2 Iterator referencing end of range to replace.
1938 * @param __s C string value to insert.
1939 * @param __n Number of characters from s to insert.
1940 * @return Reference to this string.
1941 * @throw std::length_error If new length exceeds @c max_size().
1943 * Removes the characters in the range [__i1,__i2). In place,
1944 * the first @a __n characters of @a __s are inserted. If the
1945 * length of result exceeds max_size(), length_error is thrown.
1946 * The value of the string doesn't change if an error is
1950 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
, size_type __n
)
1952 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
1953 && __i2
<= _M_iend());
1954 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
, __s
, __n
);
1958 * @brief Replace range of characters with C string.
1959 * @param __i1 Iterator referencing start of range to replace.
1960 * @param __i2 Iterator referencing end of range to replace.
1961 * @param __s C string value to insert.
1962 * @return Reference to this string.
1963 * @throw std::length_error If new length exceeds @c max_size().
1965 * Removes the characters in the range [__i1,__i2). In place,
1966 * the characters of @a __s are inserted. If the length of
1967 * result exceeds max_size(), length_error is thrown. The
1968 * value of the string doesn't change if an error is thrown.
1971 replace(iterator __i1
, iterator __i2
, const _CharT
* __s
)
1973 __glibcxx_requires_string(__s
);
1974 return this->replace(__i1
, __i2
, __s
, traits_type::length(__s
));
1978 * @brief Replace range of characters with multiple characters
1979 * @param __i1 Iterator referencing start of range to replace.
1980 * @param __i2 Iterator referencing end of range to replace.
1981 * @param __n Number of characters to insert.
1982 * @param __c Character to insert.
1983 * @return Reference to this string.
1984 * @throw std::length_error If new length exceeds @c max_size().
1986 * Removes the characters in the range [__i1,__i2). In place,
1987 * @a __n copies of @a __c are inserted. If the length of
1988 * result exceeds max_size(), length_error is thrown. The
1989 * value of the string doesn't change if an error is thrown.
1992 replace(iterator __i1
, iterator __i2
, size_type __n
, _CharT __c
)
1994 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
1995 && __i2
<= _M_iend());
1996 return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __c
);
2000 * @brief Replace range of characters with range.
2001 * @param __i1 Iterator referencing start of range to replace.
2002 * @param __i2 Iterator referencing end of range to replace.
2003 * @param __k1 Iterator referencing start of range to insert.
2004 * @param __k2 Iterator referencing end of range to insert.
2005 * @return Reference to this string.
2006 * @throw std::length_error If new length exceeds @c max_size().
2008 * Removes the characters in the range [__i1,__i2). In place,
2009 * characters in the range [__k1,__k2) are inserted. If the
2010 * length of result exceeds max_size(), length_error is thrown.
2011 * The value of the string doesn't change if an error is
2014 template<class _InputIterator
>
2016 replace(iterator __i1
, iterator __i2
,
2017 _InputIterator __k1
, _InputIterator __k2
)
2019 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
2020 && __i2
<= _M_iend());
2021 __glibcxx_requires_valid_range(__k1
, __k2
);
2022 typedef typename
std::__is_integer
<_InputIterator
>::__type _Integral
;
2023 return _M_replace_dispatch(__i1
, __i2
, __k1
, __k2
, _Integral());
2026 // Specializations for the common case of pointer and iterator:
2027 // useful to avoid the overhead of temporary buffering in _M_replace.
2029 replace(iterator __i1
, iterator __i2
, _CharT
* __k1
, _CharT
* __k2
)
2031 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
2032 && __i2
<= _M_iend());
2033 __glibcxx_requires_valid_range(__k1
, __k2
);
2034 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
2039 replace(iterator __i1
, iterator __i2
,
2040 const _CharT
* __k1
, const _CharT
* __k2
)
2042 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
2043 && __i2
<= _M_iend());
2044 __glibcxx_requires_valid_range(__k1
, __k2
);
2045 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
2050 replace(iterator __i1
, iterator __i2
, iterator __k1
, iterator __k2
)
2052 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
2053 && __i2
<= _M_iend());
2054 __glibcxx_requires_valid_range(__k1
, __k2
);
2055 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
2056 __k1
.base(), __k2
- __k1
);
2060 replace(iterator __i1
, iterator __i2
,
2061 const_iterator __k1
, const_iterator __k2
)
2063 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1
&& __i1
<= __i2
2064 && __i2
<= _M_iend());
2065 __glibcxx_requires_valid_range(__k1
, __k2
);
2066 return this->replace(__i1
- _M_ibegin(), __i2
- __i1
,
2067 __k1
.base(), __k2
- __k1
);
2070 #if __cplusplus >= 201103L
2072 * @brief Replace range of characters with initializer_list.
2073 * @param __i1 Iterator referencing start of range to replace.
2074 * @param __i2 Iterator referencing end of range to replace.
2075 * @param __l The initializer_list of characters to insert.
2076 * @return Reference to this string.
2077 * @throw std::length_error If new length exceeds @c max_size().
2079 * Removes the characters in the range [__i1,__i2). In place,
2080 * characters in the range [__k1,__k2) are inserted. If the
2081 * length of result exceeds max_size(), length_error is thrown.
2082 * The value of the string doesn't change if an error is
2085 basic_string
& replace(iterator __i1
, iterator __i2
,
2086 initializer_list
<_CharT
> __l
)
2087 { return this->replace(__i1
, __i2
, __l
.begin(), __l
.end()); }
2090 #if __cplusplus >= 201703L
2092 * @brief Replace range of characters with string_view.
2093 * @param __pos The position to replace at.
2094 * @param __n The number of characters to replace.
2095 * @param __svt The object convertible to string_view to insert.
2096 * @return Reference to this string.
2098 template<typename _Tp
>
2099 _If_sv
<_Tp
, basic_string
&>
2100 replace(size_type __pos
, size_type __n
, const _Tp
& __svt
)
2102 __sv_type __sv
= __svt
;
2103 return this->replace(__pos
, __n
, __sv
.data(), __sv
.size());
2107 * @brief Replace range of characters with string_view.
2108 * @param __pos1 The position to replace at.
2109 * @param __n1 The number of characters to replace.
2110 * @param __svt The object convertible to string_view to insert from.
2111 * @param __pos2 The position in the string_view to insert from.
2112 * @param __n2 The number of characters to insert.
2113 * @return Reference to this string.
2115 template<typename _Tp
>
2116 _If_sv
<_Tp
, basic_string
&>
2117 replace(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
2118 size_type __pos2
, size_type __n2
= npos
)
2120 __sv_type __sv
= __svt
;
2121 return this->replace(__pos1
, __n1
,
2123 + std::__sv_check(__sv
.size(), __pos2
, "basic_string::replace"),
2124 std::__sv_limit(__sv
.size(), __pos2
, __n2
));
2128 * @brief Replace range of characters with string_view.
2129 * @param __i1 An iterator referencing the start position
2131 * @param __i2 An iterator referencing the end position
2133 * @param __svt The object convertible to string_view to insert from.
2134 * @return Reference to this string.
2136 template<typename _Tp
>
2137 _If_sv
<_Tp
, basic_string
&>
2138 replace(const_iterator __i1
, const_iterator __i2
, const _Tp
& __svt
)
2140 __sv_type __sv
= __svt
;
2141 return this->replace(__i1
- begin(), __i2
- __i1
, __sv
);
2146 template<class _Integer
>
2148 _M_replace_dispatch(iterator __i1
, iterator __i2
, _Integer __n
,
2149 _Integer __val
, __true_type
)
2150 { return _M_replace_aux(__i1
- _M_ibegin(), __i2
- __i1
, __n
, __val
); }
2152 template<class _InputIterator
>
2154 _M_replace_dispatch(iterator __i1
, iterator __i2
, _InputIterator __k1
,
2155 _InputIterator __k2
, __false_type
);
2158 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
2162 _M_replace_safe(size_type __pos1
, size_type __n1
, const _CharT
* __s
,
2165 // _S_construct_aux is used to implement the 21.3.1 para 15 which
2166 // requires special behaviour if _InIter is an integral type
2167 template<class _InIterator
>
2169 _S_construct_aux(_InIterator __beg
, _InIterator __end
,
2170 const _Alloc
& __a
, __false_type
)
2172 typedef typename iterator_traits
<_InIterator
>::iterator_category _Tag
;
2173 return _S_construct(__beg
, __end
, __a
, _Tag());
2176 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2177 // 438. Ambiguity in the "do the right thing" clause
2178 template<class _Integer
>
2180 _S_construct_aux(_Integer __beg
, _Integer __end
,
2181 const _Alloc
& __a
, __true_type
)
2182 { return _S_construct_aux_2(static_cast<size_type
>(__beg
),
2186 _S_construct_aux_2(size_type __req
, _CharT __c
, const _Alloc
& __a
)
2187 { return _S_construct(__req
, __c
, __a
); }
2189 template<class _InIterator
>
2191 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
)
2193 typedef typename
std::__is_integer
<_InIterator
>::__type _Integral
;
2194 return _S_construct_aux(__beg
, __end
, __a
, _Integral());
2197 // For Input Iterators, used in istreambuf_iterators, etc.
2198 template<class _InIterator
>
2200 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
,
2201 input_iterator_tag
);
2203 // For forward_iterators up to random_access_iterators, used for
2204 // string::iterator, _CharT*, etc.
2205 template<class _FwdIterator
>
2207 _S_construct(_FwdIterator __beg
, _FwdIterator __end
, const _Alloc
& __a
,
2208 forward_iterator_tag
);
2211 _S_construct(size_type __req
, _CharT __c
, const _Alloc
& __a
);
2216 * @brief Copy substring into C string.
2217 * @param __s C string to copy value into.
2218 * @param __n Number of characters to copy.
2219 * @param __pos Index of first character to copy.
2220 * @return Number of characters actually copied
2221 * @throw std::out_of_range If __pos > size().
2223 * Copies up to @a __n characters starting at @a __pos into the
2224 * C string @a __s. If @a __pos is %greater than size(),
2225 * out_of_range is thrown.
2228 copy(_CharT
* __s
, size_type __n
, size_type __pos
= 0) const;
2231 * @brief Swap contents with another string.
2232 * @param __s String to swap with.
2234 * Exchanges the contents of this string with that of @a __s in constant
2238 swap(basic_string
& __s
)
2239 _GLIBCXX_NOEXCEPT_IF(allocator_traits
<_Alloc
>::is_always_equal::value
);
2241 // String operations:
2243 * @brief Return const pointer to null-terminated contents.
2245 * This is a handle to internal data. Do not modify or dire things may
2249 c_str() const _GLIBCXX_NOEXCEPT
2250 { return _M_data(); }
2253 * @brief Return const pointer to contents.
2255 * This is a pointer to internal data. It is undefined to modify
2256 * the contents through the returned pointer. To get a pointer that
2257 * allows modifying the contents use @c &str[0] instead,
2258 * (or in C++17 the non-const @c str.data() overload).
2261 data() const _GLIBCXX_NOEXCEPT
2262 { return _M_data(); }
2264 #if __cplusplus >= 201703L
2266 * @brief Return non-const pointer to contents.
2268 * This is a pointer to the character sequence held by the string.
2269 * Modifying the characters in the sequence is allowed.
2280 * @brief Return copy of allocator used to construct this string.
2283 get_allocator() const _GLIBCXX_NOEXCEPT
2284 { return _M_dataplus
; }
2287 * @brief Find position of a C substring.
2288 * @param __s C string to locate.
2289 * @param __pos Index of character to search from.
2290 * @param __n Number of characters from @a s to search for.
2291 * @return Index of start of first occurrence.
2293 * Starting from @a __pos, searches forward for the first @a
2294 * __n characters in @a __s within this string. If found,
2295 * returns the index where it begins. If not found, returns
2299 find(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2303 * @brief Find position of a string.
2304 * @param __str String to locate.
2305 * @param __pos Index of character to search from (default 0).
2306 * @return Index of start of first occurrence.
2308 * Starting from @a __pos, searches forward for value of @a __str within
2309 * this string. If found, returns the index where it begins. If not
2310 * found, returns npos.
2313 find(const basic_string
& __str
, size_type __pos
= 0) const
2315 { return this->find(__str
.data(), __pos
, __str
.size()); }
2318 * @brief Find position of a C string.
2319 * @param __s C string to locate.
2320 * @param __pos Index of character to search from (default 0).
2321 * @return Index of start of first occurrence.
2323 * Starting from @a __pos, searches forward for the value of @a
2324 * __s within this string. If found, returns the index where
2325 * it begins. If not found, returns npos.
2328 find(const _CharT
* __s
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
2330 __glibcxx_requires_string(__s
);
2331 return this->find(__s
, __pos
, traits_type::length(__s
));
2335 * @brief Find position of a character.
2336 * @param __c Character to locate.
2337 * @param __pos Index of character to search from (default 0).
2338 * @return Index of first occurrence.
2340 * Starting from @a __pos, searches forward for @a __c within
2341 * this string. If found, returns the index where it was
2342 * found. If not found, returns npos.
2345 find(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
;
2347 #if __cplusplus >= 201703L
2349 * @brief Find position of a string_view.
2350 * @param __svt The object convertible to string_view to locate.
2351 * @param __pos Index of character to search from (default 0).
2352 * @return Index of start of first occurrence.
2354 template<typename _Tp
>
2355 _If_sv
<_Tp
, size_type
>
2356 find(const _Tp
& __svt
, size_type __pos
= 0) const
2357 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2359 __sv_type __sv
= __svt
;
2360 return this->find(__sv
.data(), __pos
, __sv
.size());
2365 * @brief Find last position of a string.
2366 * @param __str String to locate.
2367 * @param __pos Index of character to search back from (default end).
2368 * @return Index of start of last occurrence.
2370 * Starting from @a __pos, searches backward for value of @a
2371 * __str within this string. If found, returns the index where
2372 * it begins. If not found, returns npos.
2375 rfind(const basic_string
& __str
, size_type __pos
= npos
) const
2377 { return this->rfind(__str
.data(), __pos
, __str
.size()); }
2380 * @brief Find last position of a C substring.
2381 * @param __s C string to locate.
2382 * @param __pos Index of character to search back from.
2383 * @param __n Number of characters from s to search for.
2384 * @return Index of start of last occurrence.
2386 * Starting from @a __pos, searches backward for the first @a
2387 * __n characters in @a __s within this string. If found,
2388 * returns the index where it begins. If not found, returns
2392 rfind(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2396 * @brief Find last position of a C string.
2397 * @param __s C string to locate.
2398 * @param __pos Index of character to start search at (default end).
2399 * @return Index of start of last occurrence.
2401 * Starting from @a __pos, searches backward for the value of
2402 * @a __s within this string. If found, returns the index
2403 * where it begins. If not found, returns npos.
2406 rfind(const _CharT
* __s
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
2408 __glibcxx_requires_string(__s
);
2409 return this->rfind(__s
, __pos
, traits_type::length(__s
));
2413 * @brief Find last position of a character.
2414 * @param __c Character to locate.
2415 * @param __pos Index of character to search back from (default end).
2416 * @return Index of last occurrence.
2418 * Starting from @a __pos, searches backward for @a __c within
2419 * this string. If found, returns the index where it was
2420 * found. If not found, returns npos.
2423 rfind(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
;
2425 #if __cplusplus >= 201703L
2427 * @brief Find last position of a string_view.
2428 * @param __svt The object convertible to string_view to locate.
2429 * @param __pos Index of character to search back from (default end).
2430 * @return Index of start of last occurrence.
2432 template<typename _Tp
>
2433 _If_sv
<_Tp
, size_type
>
2434 rfind(const _Tp
& __svt
, size_type __pos
= npos
) const
2435 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2437 __sv_type __sv
= __svt
;
2438 return this->rfind(__sv
.data(), __pos
, __sv
.size());
2443 * @brief Find position of a character of string.
2444 * @param __str String containing characters to locate.
2445 * @param __pos Index of character to search from (default 0).
2446 * @return Index of first occurrence.
2448 * Starting from @a __pos, searches forward for one of the
2449 * characters of @a __str within this string. If found,
2450 * returns the index where it was found. If not found, returns
2454 find_first_of(const basic_string
& __str
, size_type __pos
= 0) const
2456 { return this->find_first_of(__str
.data(), __pos
, __str
.size()); }
2459 * @brief Find position of a character of C substring.
2460 * @param __s String containing characters to locate.
2461 * @param __pos Index of character to search from.
2462 * @param __n Number of characters from s to search for.
2463 * @return Index of first occurrence.
2465 * Starting from @a __pos, searches forward for one of the
2466 * first @a __n characters of @a __s within this string. If
2467 * found, returns the index where it was found. If not found,
2471 find_first_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2475 * @brief Find position of a character of C string.
2476 * @param __s String containing characters to locate.
2477 * @param __pos Index of character to search from (default 0).
2478 * @return Index of first occurrence.
2480 * Starting from @a __pos, searches forward for one of the
2481 * characters of @a __s within this string. If found, returns
2482 * the index where it was found. If not found, returns npos.
2485 find_first_of(const _CharT
* __s
, size_type __pos
= 0) const
2488 __glibcxx_requires_string(__s
);
2489 return this->find_first_of(__s
, __pos
, traits_type::length(__s
));
2493 * @brief Find position of a character.
2494 * @param __c Character to locate.
2495 * @param __pos Index of character to search from (default 0).
2496 * @return Index of first occurrence.
2498 * Starting from @a __pos, searches forward for the character
2499 * @a __c within this string. If found, returns the index
2500 * where it was found. If not found, returns npos.
2502 * Note: equivalent to find(__c, __pos).
2505 find_first_of(_CharT __c
, size_type __pos
= 0) const _GLIBCXX_NOEXCEPT
2506 { return this->find(__c
, __pos
); }
2508 #if __cplusplus >= 201703L
2510 * @brief Find position of a character of a string_view.
2511 * @param __svt An object convertible to string_view containing
2512 * characters to locate.
2513 * @param __pos Index of character to search from (default 0).
2514 * @return Index of first occurrence.
2516 template<typename _Tp
>
2517 _If_sv
<_Tp
, size_type
>
2518 find_first_of(const _Tp
& __svt
, size_type __pos
= 0) const
2519 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2521 __sv_type __sv
= __svt
;
2522 return this->find_first_of(__sv
.data(), __pos
, __sv
.size());
2527 * @brief Find last position of a character of string.
2528 * @param __str String containing characters to locate.
2529 * @param __pos Index of character to search back from (default end).
2530 * @return Index of last occurrence.
2532 * Starting from @a __pos, searches backward for one of the
2533 * characters of @a __str within this string. If found,
2534 * returns the index where it was found. If not found, returns
2538 find_last_of(const basic_string
& __str
, size_type __pos
= npos
) const
2540 { return this->find_last_of(__str
.data(), __pos
, __str
.size()); }
2543 * @brief Find last position of a character of C substring.
2544 * @param __s C string containing characters to locate.
2545 * @param __pos Index of character to search back from.
2546 * @param __n Number of characters from s to search for.
2547 * @return Index of last occurrence.
2549 * Starting from @a __pos, searches backward for one of the
2550 * first @a __n characters of @a __s within this string. If
2551 * found, returns the index where it was found. If not found,
2555 find_last_of(const _CharT
* __s
, size_type __pos
, size_type __n
) const
2559 * @brief Find last position of a character of C string.
2560 * @param __s C string containing characters to locate.
2561 * @param __pos Index of character to search back from (default end).
2562 * @return Index of last occurrence.
2564 * Starting from @a __pos, searches backward for one of the
2565 * characters of @a __s within this string. If found, returns
2566 * the index where it was found. If not found, returns npos.
2569 find_last_of(const _CharT
* __s
, size_type __pos
= npos
) const
2572 __glibcxx_requires_string(__s
);
2573 return this->find_last_of(__s
, __pos
, traits_type::length(__s
));
2577 * @brief Find last position of a character.
2578 * @param __c Character to locate.
2579 * @param __pos Index of character to search back from (default end).
2580 * @return Index of last occurrence.
2582 * Starting from @a __pos, searches backward for @a __c within
2583 * this string. If found, returns the index where it was
2584 * found. If not found, returns npos.
2586 * Note: equivalent to rfind(__c, __pos).
2589 find_last_of(_CharT __c
, size_type __pos
= npos
) const _GLIBCXX_NOEXCEPT
2590 { return this->rfind(__c
, __pos
); }
2592 #if __cplusplus >= 201703L
2594 * @brief Find last position of a character of string.
2595 * @param __svt An object convertible to string_view containing
2596 * characters to locate.
2597 * @param __pos Index of character to search back from (default end).
2598 * @return Index of last occurrence.
2600 template<typename _Tp
>
2601 _If_sv
<_Tp
, size_type
>
2602 find_last_of(const _Tp
& __svt
, size_type __pos
= npos
) const
2603 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2605 __sv_type __sv
= __svt
;
2606 return this->find_last_of(__sv
.data(), __pos
, __sv
.size());
2611 * @brief Find position of a character not in string.
2612 * @param __str String containing characters to avoid.
2613 * @param __pos Index of character to search from (default 0).
2614 * @return Index of first occurrence.
2616 * Starting from @a __pos, searches forward for a character not contained
2617 * in @a __str within this string. If found, returns the index where it
2618 * was found. If not found, returns npos.
2621 find_first_not_of(const basic_string
& __str
, size_type __pos
= 0) const
2623 { return this->find_first_not_of(__str
.data(), __pos
, __str
.size()); }
2626 * @brief Find position of a character not in C substring.
2627 * @param __s C string containing characters to avoid.
2628 * @param __pos Index of character to search from.
2629 * @param __n Number of characters from __s to consider.
2630 * @return Index of first occurrence.
2632 * Starting from @a __pos, searches forward for a character not
2633 * contained in the first @a __n characters of @a __s within
2634 * this string. If found, returns the index where it was
2635 * found. If not found, returns npos.
2638 find_first_not_of(const _CharT
* __s
, size_type __pos
,
2639 size_type __n
) const _GLIBCXX_NOEXCEPT
;
2642 * @brief Find position of a character not in C string.
2643 * @param __s C string containing characters to avoid.
2644 * @param __pos Index of character to search from (default 0).
2645 * @return Index of first occurrence.
2647 * Starting from @a __pos, searches forward for a character not
2648 * contained in @a __s within this string. If found, returns
2649 * the index where it was found. If not found, returns npos.
2652 find_first_not_of(const _CharT
* __s
, size_type __pos
= 0) const
2655 __glibcxx_requires_string(__s
);
2656 return this->find_first_not_of(__s
, __pos
, traits_type::length(__s
));
2660 * @brief Find position of a different character.
2661 * @param __c Character to avoid.
2662 * @param __pos Index of character to search from (default 0).
2663 * @return Index of first occurrence.
2665 * Starting from @a __pos, searches forward for a character
2666 * other than @a __c within this string. If found, returns the
2667 * index where it was found. If not found, returns npos.
2670 find_first_not_of(_CharT __c
, size_type __pos
= 0) const
2673 #if __cplusplus >= 201703L
2675 * @brief Find position of a character not in a string_view.
2676 * @param __svt An object convertible to string_view containing
2677 * characters to avoid.
2678 * @param __pos Index of character to search from (default 0).
2679 * @return Index of first occurrence.
2681 template<typename _Tp
>
2682 _If_sv
<_Tp
, size_type
>
2683 find_first_not_of(const _Tp
& __svt
, size_type __pos
= 0) const
2684 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2686 __sv_type __sv
= __svt
;
2687 return this->find_first_not_of(__sv
.data(), __pos
, __sv
.size());
2692 * @brief Find last position of a character not in string.
2693 * @param __str String containing characters to avoid.
2694 * @param __pos Index of character to search back from (default end).
2695 * @return Index of last occurrence.
2697 * Starting from @a __pos, searches backward for a character
2698 * not contained in @a __str within this string. If found,
2699 * returns the index where it was found. If not found, returns
2703 find_last_not_of(const basic_string
& __str
, size_type __pos
= npos
) const
2705 { return this->find_last_not_of(__str
.data(), __pos
, __str
.size()); }
2708 * @brief Find last position of a character not in C substring.
2709 * @param __s C string containing characters to avoid.
2710 * @param __pos Index of character to search back from.
2711 * @param __n Number of characters from s to consider.
2712 * @return Index of last occurrence.
2714 * Starting from @a __pos, searches backward for a character not
2715 * contained in the first @a __n characters of @a __s within this string.
2716 * If found, returns the index where it was found. If not found,
2720 find_last_not_of(const _CharT
* __s
, size_type __pos
,
2721 size_type __n
) const _GLIBCXX_NOEXCEPT
;
2723 * @brief Find last position of a character not in C string.
2724 * @param __s C string containing characters to avoid.
2725 * @param __pos Index of character to search back from (default end).
2726 * @return Index of last occurrence.
2728 * Starting from @a __pos, searches backward for a character
2729 * not contained in @a __s within this string. If found,
2730 * returns the index where it was found. If not found, returns
2734 find_last_not_of(const _CharT
* __s
, size_type __pos
= npos
) const
2737 __glibcxx_requires_string(__s
);
2738 return this->find_last_not_of(__s
, __pos
, traits_type::length(__s
));
2742 * @brief Find last position of a different character.
2743 * @param __c Character to avoid.
2744 * @param __pos Index of character to search back from (default end).
2745 * @return Index of last occurrence.
2747 * Starting from @a __pos, searches backward for a character other than
2748 * @a __c within this string. If found, returns the index where it was
2749 * found. If not found, returns npos.
2752 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const
2755 #if __cplusplus >= 201703L
2757 * @brief Find last position of a character not in a string_view.
2758 * @param __svt An object convertible to string_view containing
2759 * characters to avoid.
2760 * @param __pos Index of character to search back from (default end).
2761 * @return Index of last occurrence.
2763 template<typename _Tp
>
2764 _If_sv
<_Tp
, size_type
>
2765 find_last_not_of(const _Tp
& __svt
, size_type __pos
= npos
) const
2766 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2768 __sv_type __sv
= __svt
;
2769 return this->find_last_not_of(__sv
.data(), __pos
, __sv
.size());
2774 * @brief Get a substring.
2775 * @param __pos Index of first character (default 0).
2776 * @param __n Number of characters in substring (default remainder).
2777 * @return The new string.
2778 * @throw std::out_of_range If __pos > size().
2780 * Construct and return a new string using the @a __n
2781 * characters starting at @a __pos. If the string is too
2782 * short, use the remainder of the characters. If @a __pos is
2783 * beyond the end of the string, out_of_range is thrown.
2786 substr(size_type __pos
= 0, size_type __n
= npos
) const
2787 { return basic_string(*this,
2788 _M_check(__pos
, "basic_string::substr"), __n
); }
2791 * @brief Compare to a string.
2792 * @param __str String to compare against.
2793 * @return Integer < 0, 0, or > 0.
2795 * Returns an integer < 0 if this string is ordered before @a
2796 * __str, 0 if their values are equivalent, or > 0 if this
2797 * string is ordered after @a __str. Determines the effective
2798 * length rlen of the strings to compare as the smallest of
2799 * size() and str.size(). The function then compares the two
2800 * strings by calling traits::compare(data(), str.data(),rlen).
2801 * If the result of the comparison is nonzero returns it,
2802 * otherwise the shorter one is ordered first.
2805 compare(const basic_string
& __str
) const
2807 const size_type __size
= this->size();
2808 const size_type __osize
= __str
.size();
2809 const size_type __len
= std::min(__size
, __osize
);
2811 int __r
= traits_type::compare(_M_data(), __str
.data(), __len
);
2813 __r
= _S_compare(__size
, __osize
);
2817 #if __cplusplus >= 201703L
2819 * @brief Compare to a string_view.
2820 * @param __svt An object convertible to string_view to compare against.
2821 * @return Integer < 0, 0, or > 0.
2823 template<typename _Tp
>
2825 compare(const _Tp
& __svt
) const
2826 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2828 __sv_type __sv
= __svt
;
2829 const size_type __size
= this->size();
2830 const size_type __osize
= __sv
.size();
2831 const size_type __len
= std::min(__size
, __osize
);
2833 int __r
= traits_type::compare(_M_data(), __sv
.data(), __len
);
2835 __r
= _S_compare(__size
, __osize
);
2840 * @brief Compare to a string_view.
2841 * @param __pos A position in the string to start comparing from.
2842 * @param __n The number of characters to compare.
2843 * @param __svt An object convertible to string_view to compare
2845 * @return Integer < 0, 0, or > 0.
2847 template<typename _Tp
>
2849 compare(size_type __pos
, size_type __n
, const _Tp
& __svt
) const
2850 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2852 __sv_type __sv
= __svt
;
2853 return __sv_type(*this).substr(__pos
, __n
).compare(__sv
);
2857 * @brief Compare to a string_view.
2858 * @param __pos1 A position in the string to start comparing from.
2859 * @param __n1 The number of characters to compare.
2860 * @param __svt An object convertible to string_view to compare
2862 * @param __pos2 A position in the string_view to start comparing from.
2863 * @param __n2 The number of characters to compare.
2864 * @return Integer < 0, 0, or > 0.
2866 template<typename _Tp
>
2868 compare(size_type __pos1
, size_type __n1
, const _Tp
& __svt
,
2869 size_type __pos2
, size_type __n2
= npos
) const
2870 noexcept(is_same
<_Tp
, __sv_type
>::value
)
2872 __sv_type __sv
= __svt
;
2873 return __sv_type(*this)
2874 .substr(__pos1
, __n1
).compare(__sv
.substr(__pos2
, __n2
));
2879 * @brief Compare substring to a string.
2880 * @param __pos Index of first character of substring.
2881 * @param __n Number of characters in substring.
2882 * @param __str String to compare against.
2883 * @return Integer < 0, 0, or > 0.
2885 * Form the substring of this string from the @a __n characters
2886 * starting at @a __pos. Returns an integer < 0 if the
2887 * substring is ordered before @a __str, 0 if their values are
2888 * equivalent, or > 0 if the substring is ordered after @a
2889 * __str. Determines the effective length rlen of the strings
2890 * to compare as the smallest of the length of the substring
2891 * and @a __str.size(). The function then compares the two
2892 * strings by calling
2893 * traits::compare(substring.data(),str.data(),rlen). If the
2894 * result of the comparison is nonzero returns it, otherwise
2895 * the shorter one is ordered first.
2898 compare(size_type __pos
, size_type __n
, const basic_string
& __str
) const
2900 _M_check(__pos
, "basic_string::compare");
2901 __n
= _M_limit(__pos
, __n
);
2902 const size_type __osize
= __str
.size();
2903 const size_type __len
= std::min(__n
, __osize
);
2904 int __r
= traits_type::compare(_M_data() + __pos
, __str
.data(), __len
);
2906 __r
= _S_compare(__n
, __osize
);
2911 * @brief Compare substring to a substring.
2912 * @param __pos1 Index of first character of substring.
2913 * @param __n1 Number of characters in substring.
2914 * @param __str String to compare against.
2915 * @param __pos2 Index of first character of substring of str.
2916 * @param __n2 Number of characters in substring of str.
2917 * @return Integer < 0, 0, or > 0.
2919 * Form the substring of this string from the @a __n1
2920 * characters starting at @a __pos1. Form the substring of @a
2921 * __str from the @a __n2 characters starting at @a __pos2.
2922 * Returns an integer < 0 if this substring is ordered before
2923 * the substring of @a __str, 0 if their values are equivalent,
2924 * or > 0 if this substring is ordered after the substring of
2925 * @a __str. Determines the effective length rlen of the
2926 * strings to compare as the smallest of the lengths of the
2927 * substrings. The function then compares the two strings by
2929 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2930 * If the result of the comparison is nonzero returns it,
2931 * otherwise the shorter one is ordered first.
2934 compare(size_type __pos1
, size_type __n1
, const basic_string
& __str
,
2935 size_type __pos2
, size_type __n2
= npos
) const
2937 _M_check(__pos1
, "basic_string::compare");
2938 __str
._M_check(__pos2
, "basic_string::compare");
2939 __n1
= _M_limit(__pos1
, __n1
);
2940 __n2
= __str
._M_limit(__pos2
, __n2
);
2941 const size_type __len
= std::min(__n1
, __n2
);
2942 int __r
= traits_type::compare(_M_data() + __pos1
,
2943 __str
.data() + __pos2
, __len
);
2945 __r
= _S_compare(__n1
, __n2
);
2950 * @brief Compare to a C string.
2951 * @param __s C string to compare against.
2952 * @return Integer < 0, 0, or > 0.
2954 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
2955 * their values are equivalent, or > 0 if this string is ordered after
2956 * @a __s. Determines the effective length rlen of the strings to
2957 * compare as the smallest of size() and the length of a string
2958 * constructed from @a __s. The function then compares the two strings
2959 * by calling traits::compare(data(),s,rlen). If the result of the
2960 * comparison is nonzero returns it, otherwise the shorter one is
2964 compare(const _CharT
* __s
) const _GLIBCXX_NOEXCEPT
2966 __glibcxx_requires_string(__s
);
2967 const size_type __size
= this->size();
2968 const size_type __osize
= traits_type::length(__s
);
2969 const size_type __len
= std::min(__size
, __osize
);
2970 int __r
= traits_type::compare(_M_data(), __s
, __len
);
2972 __r
= _S_compare(__size
, __osize
);
2976 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2977 // 5 String::compare specification questionable
2979 * @brief Compare substring to a C string.
2980 * @param __pos Index of first character of substring.
2981 * @param __n1 Number of characters in substring.
2982 * @param __s C string to compare against.
2983 * @return Integer < 0, 0, or > 0.
2985 * Form the substring of this string from the @a __n1
2986 * characters starting at @a pos. Returns an integer < 0 if
2987 * the substring is ordered before @a __s, 0 if their values
2988 * are equivalent, or > 0 if the substring is ordered after @a
2989 * __s. Determines the effective length rlen of the strings to
2990 * compare as the smallest of the length of the substring and
2991 * the length of a string constructed from @a __s. The
2992 * function then compares the two string by calling
2993 * traits::compare(substring.data(),__s,rlen). If the result of
2994 * the comparison is nonzero returns it, otherwise the shorter
2995 * one is ordered first.
2998 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
) const
3000 __glibcxx_requires_string(__s
);
3001 _M_check(__pos
, "basic_string::compare");
3002 __n1
= _M_limit(__pos
, __n1
);
3003 const size_type __osize
= traits_type::length(__s
);
3004 const size_type __len
= std::min(__n1
, __osize
);
3005 int __r
= traits_type::compare(_M_data() + __pos
, __s
, __len
);
3007 __r
= _S_compare(__n1
, __osize
);
3012 * @brief Compare substring against a character %array.
3013 * @param __pos Index of first character of substring.
3014 * @param __n1 Number of characters in substring.
3015 * @param __s character %array to compare against.
3016 * @param __n2 Number of characters of s.
3017 * @return Integer < 0, 0, or > 0.
3019 * Form the substring of this string from the @a __n1
3020 * characters starting at @a __pos. Form a string from the
3021 * first @a __n2 characters of @a __s. Returns an integer < 0
3022 * if this substring is ordered before the string from @a __s,
3023 * 0 if their values are equivalent, or > 0 if this substring
3024 * is ordered after the string from @a __s. Determines the
3025 * effective length rlen of the strings to compare as the
3026 * smallest of the length of the substring and @a __n2. The
3027 * function then compares the two strings by calling
3028 * traits::compare(substring.data(),s,rlen). If the result of
3029 * the comparison is nonzero returns it, otherwise the shorter
3030 * one is ordered first.
3032 * NB: s must have at least n2 characters, '\\0' has
3033 * no special meaning.
3036 compare(size_type __pos
, size_type __n1
, const _CharT
* __s
,
3037 size_type __n2
) const
3039 __glibcxx_requires_string_len(__s
, __n2
);
3040 _M_check(__pos
, "basic_string::compare");
3041 __n1
= _M_limit(__pos
, __n1
);
3042 const size_type __len
= std::min(__n1
, __n2
);
3043 int __r
= traits_type::compare(_M_data() + __pos
, __s
, __len
);
3045 __r
= _S_compare(__n1
, __n2
);
3049 #if __cplusplus > 201703L
3051 starts_with(basic_string_view
<_CharT
, _Traits
> __x
) const noexcept
3052 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
3055 starts_with(_CharT __x
) const noexcept
3056 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
3058 [[__gnu__::__nonnull__
]]
3060 starts_with(const _CharT
* __x
) const noexcept
3061 { return __sv_type(this->data(), this->size()).starts_with(__x
); }
3064 ends_with(basic_string_view
<_CharT
, _Traits
> __x
) const noexcept
3065 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
3068 ends_with(_CharT __x
) const noexcept
3069 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
3071 [[__gnu__::__nonnull__
]]
3073 ends_with(const _CharT
* __x
) const noexcept
3074 { return __sv_type(this->data(), this->size()).ends_with(__x
); }
3077 #if __cplusplus > 202011L
3079 contains(basic_string_view
<_CharT
, _Traits
> __x
) const noexcept
3080 { return __sv_type(this->data(), this->size()).contains(__x
); }
3083 contains(_CharT __x
) const noexcept
3084 { return __sv_type(this->data(), this->size()).contains(__x
); }
3086 [[__gnu__::__nonnull__
]]
3088 contains(const _CharT
* __x
) const noexcept
3089 { return __sv_type(this->data(), this->size()).contains(__x
); }
3092 # ifdef _GLIBCXX_TM_TS_INTERNAL
3094 ::_txnal_cow_string_C1_for_exceptions(void* that
, const char* s
,
3097 ::_txnal_cow_string_c_str(const void *that
);
3099 ::_txnal_cow_string_D1(void *that
);
3101 ::_txnal_cow_string_D1_commit(void *that
);
3105 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3106 const typename basic_string
<_CharT
, _Traits
, _Alloc
>::size_type
3107 basic_string
<_CharT
, _Traits
, _Alloc
>::
3108 _Rep::_S_max_size
= (((npos
- sizeof(_Rep_base
))/sizeof(_CharT
)) - 1) / 4;
3110 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3112 basic_string
<_CharT
, _Traits
, _Alloc
>::
3113 _Rep::_S_terminal
= _CharT();
3115 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3116 const typename basic_string
<_CharT
, _Traits
, _Alloc
>::size_type
3117 basic_string
<_CharT
, _Traits
, _Alloc
>::npos
;
3119 // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string)
3120 // at static init time (before static ctors are run).
3121 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3122 typename basic_string
<_CharT
, _Traits
, _Alloc
>::size_type
3123 basic_string
<_CharT
, _Traits
, _Alloc
>::_Rep::_S_empty_rep_storage
[
3124 (sizeof(_Rep_base
) + sizeof(_CharT
) + sizeof(size_type
) - 1) /
3127 // NB: This is the special case for Input Iterators, used in
3128 // istreambuf_iterators, etc.
3129 // Input Iterators have a cost structure very different from
3130 // pointers, calling for a different coding style.
3131 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3132 template<typename _InIterator
>
3134 basic_string
<_CharT
, _Traits
, _Alloc
>::
3135 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
,
3138 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3139 if (__beg
== __end
&& __a
== _Alloc())
3140 return _S_empty_rep()._M_refdata();
3142 // Avoid reallocation for common case.
3144 size_type __len
= 0;
3145 while (__beg
!= __end
&& __len
< sizeof(__buf
) / sizeof(_CharT
))
3147 __buf
[__len
++] = *__beg
;
3150 _Rep
* __r
= _Rep::_S_create(__len
, size_type(0), __a
);
3151 _M_copy(__r
->_M_refdata(), __buf
, __len
);
3154 while (__beg
!= __end
)
3156 if (__len
== __r
->_M_capacity
)
3158 // Allocate more space.
3159 _Rep
* __another
= _Rep::_S_create(__len
+ 1, __len
, __a
);
3160 _M_copy(__another
->_M_refdata(), __r
->_M_refdata(), __len
);
3161 __r
->_M_destroy(__a
);
3164 __r
->_M_refdata()[__len
++] = *__beg
;
3170 __r
->_M_destroy(__a
);
3171 __throw_exception_again
;
3173 __r
->_M_set_length_and_sharable(__len
);
3174 return __r
->_M_refdata();
3177 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3178 template <typename _InIterator
>
3180 basic_string
<_CharT
, _Traits
, _Alloc
>::
3181 _S_construct(_InIterator __beg
, _InIterator __end
, const _Alloc
& __a
,
3182 forward_iterator_tag
)
3184 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3185 if (__beg
== __end
&& __a
== _Alloc())
3186 return _S_empty_rep()._M_refdata();
3188 // NB: Not required, but considered best practice.
3189 if (__gnu_cxx::__is_null_pointer(__beg
) && __beg
!= __end
)
3190 __throw_logic_error(__N("basic_string::_S_construct null not valid"));
3192 const size_type __dnew
= static_cast<size_type
>(std::distance(__beg
,
3194 // Check for out_of_range and length_error exceptions.
3195 _Rep
* __r
= _Rep::_S_create(__dnew
, size_type(0), __a
);
3197 { _S_copy_chars(__r
->_M_refdata(), __beg
, __end
); }
3200 __r
->_M_destroy(__a
);
3201 __throw_exception_again
;
3203 __r
->_M_set_length_and_sharable(__dnew
);
3204 return __r
->_M_refdata();
3207 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3209 basic_string
<_CharT
, _Traits
, _Alloc
>::
3210 _S_construct(size_type __n
, _CharT __c
, const _Alloc
& __a
)
3212 #if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3213 if (__n
== 0 && __a
== _Alloc())
3214 return _S_empty_rep()._M_refdata();
3216 // Check for out_of_range and length_error exceptions.
3217 _Rep
* __r
= _Rep::_S_create(__n
, size_type(0), __a
);
3219 _M_assign(__r
->_M_refdata(), __n
, __c
);
3221 __r
->_M_set_length_and_sharable(__n
);
3222 return __r
->_M_refdata();
3225 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3226 basic_string
<_CharT
, _Traits
, _Alloc
>::
3227 basic_string(const basic_string
& __str
, size_type __pos
, const _Alloc
& __a
)
3228 : _M_dataplus(_S_construct(__str
._M_data()
3229 + __str
._M_check(__pos
,
3230 "basic_string::basic_string"),
3231 __str
._M_data() + __str
._M_limit(__pos
, npos
)
3235 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3236 basic_string
<_CharT
, _Traits
, _Alloc
>::
3237 basic_string(const basic_string
& __str
, size_type __pos
, size_type __n
)
3238 : _M_dataplus(_S_construct(__str
._M_data()
3239 + __str
._M_check(__pos
,
3240 "basic_string::basic_string"),
3241 __str
._M_data() + __str
._M_limit(__pos
, __n
)
3242 + __pos
, _Alloc()), _Alloc())
3245 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3246 basic_string
<_CharT
, _Traits
, _Alloc
>::
3247 basic_string(const basic_string
& __str
, size_type __pos
,
3248 size_type __n
, const _Alloc
& __a
)
3249 : _M_dataplus(_S_construct(__str
._M_data()
3250 + __str
._M_check(__pos
,
3251 "basic_string::basic_string"),
3252 __str
._M_data() + __str
._M_limit(__pos
, __n
)
3256 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3257 basic_string
<_CharT
, _Traits
, _Alloc
>&
3258 basic_string
<_CharT
, _Traits
, _Alloc
>::
3259 assign(const basic_string
& __str
)
3261 if (_M_rep() != __str
._M_rep())
3264 const allocator_type __a
= this->get_allocator();
3265 _CharT
* __tmp
= __str
._M_rep()->_M_grab(__a
, __str
.get_allocator());
3266 _M_rep()->_M_dispose(__a
);
3272 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3273 basic_string
<_CharT
, _Traits
, _Alloc
>&
3274 basic_string
<_CharT
, _Traits
, _Alloc
>::
3275 assign(const _CharT
* __s
, size_type __n
)
3277 __glibcxx_requires_string_len(__s
, __n
);
3278 _M_check_length(this->size(), __n
, "basic_string::assign");
3279 if (_M_disjunct(__s
) || _M_rep()->_M_is_shared())
3280 return _M_replace_safe(size_type(0), this->size(), __s
, __n
);
3284 const size_type __pos
= __s
- _M_data();
3286 _M_copy(_M_data(), __s
, __n
);
3288 _M_move(_M_data(), __s
, __n
);
3289 _M_rep()->_M_set_length_and_sharable(__n
);
3294 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3295 basic_string
<_CharT
, _Traits
, _Alloc
>&
3296 basic_string
<_CharT
, _Traits
, _Alloc
>::
3297 append(size_type __n
, _CharT __c
)
3301 _M_check_length(size_type(0), __n
, "basic_string::append");
3302 const size_type __len
= __n
+ this->size();
3303 if (__len
> this->capacity() || _M_rep()->_M_is_shared())
3304 this->reserve(__len
);
3305 _M_assign(_M_data() + this->size(), __n
, __c
);
3306 _M_rep()->_M_set_length_and_sharable(__len
);
3311 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3312 basic_string
<_CharT
, _Traits
, _Alloc
>&
3313 basic_string
<_CharT
, _Traits
, _Alloc
>::
3314 append(const _CharT
* __s
, size_type __n
)
3316 __glibcxx_requires_string_len(__s
, __n
);
3319 _M_check_length(size_type(0), __n
, "basic_string::append");
3320 const size_type __len
= __n
+ this->size();
3321 if (__len
> this->capacity() || _M_rep()->_M_is_shared())
3323 if (_M_disjunct(__s
))
3324 this->reserve(__len
);
3327 const size_type __off
= __s
- _M_data();
3328 this->reserve(__len
);
3329 __s
= _M_data() + __off
;
3332 _M_copy(_M_data() + this->size(), __s
, __n
);
3333 _M_rep()->_M_set_length_and_sharable(__len
);
3338 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3339 basic_string
<_CharT
, _Traits
, _Alloc
>&
3340 basic_string
<_CharT
, _Traits
, _Alloc
>::
3341 append(const basic_string
& __str
)
3343 const size_type __size
= __str
.size();
3346 const size_type __len
= __size
+ this->size();
3347 if (__len
> this->capacity() || _M_rep()->_M_is_shared())
3348 this->reserve(__len
);
3349 _M_copy(_M_data() + this->size(), __str
._M_data(), __size
);
3350 _M_rep()->_M_set_length_and_sharable(__len
);
3355 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3356 basic_string
<_CharT
, _Traits
, _Alloc
>&
3357 basic_string
<_CharT
, _Traits
, _Alloc
>::
3358 append(const basic_string
& __str
, size_type __pos
, size_type __n
)
3360 __str
._M_check(__pos
, "basic_string::append");
3361 __n
= __str
._M_limit(__pos
, __n
);
3364 const size_type __len
= __n
+ this->size();
3365 if (__len
> this->capacity() || _M_rep()->_M_is_shared())
3366 this->reserve(__len
);
3367 _M_copy(_M_data() + this->size(), __str
._M_data() + __pos
, __n
);
3368 _M_rep()->_M_set_length_and_sharable(__len
);
3373 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3374 basic_string
<_CharT
, _Traits
, _Alloc
>&
3375 basic_string
<_CharT
, _Traits
, _Alloc
>::
3376 insert(size_type __pos
, const _CharT
* __s
, size_type __n
)
3378 __glibcxx_requires_string_len(__s
, __n
);
3379 _M_check(__pos
, "basic_string::insert");
3380 _M_check_length(size_type(0), __n
, "basic_string::insert");
3381 if (_M_disjunct(__s
) || _M_rep()->_M_is_shared())
3382 return _M_replace_safe(__pos
, size_type(0), __s
, __n
);
3386 const size_type __off
= __s
- _M_data();
3387 _M_mutate(__pos
, 0, __n
);
3388 __s
= _M_data() + __off
;
3389 _CharT
* __p
= _M_data() + __pos
;
3390 if (__s
+ __n
<= __p
)
3391 _M_copy(__p
, __s
, __n
);
3392 else if (__s
>= __p
)
3393 _M_copy(__p
, __s
+ __n
, __n
);
3396 const size_type __nleft
= __p
- __s
;
3397 _M_copy(__p
, __s
, __nleft
);
3398 _M_copy(__p
+ __nleft
, __p
+ __n
, __n
- __nleft
);
3404 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3405 typename basic_string
<_CharT
, _Traits
, _Alloc
>::iterator
3406 basic_string
<_CharT
, _Traits
, _Alloc
>::
3407 erase(iterator __first
, iterator __last
)
3409 _GLIBCXX_DEBUG_PEDASSERT(__first
>= _M_ibegin() && __first
<= __last
3410 && __last
<= _M_iend());
3412 // NB: This isn't just an optimization (bail out early when
3413 // there is nothing to do, really), it's also a correctness
3414 // issue vs MT, see libstdc++/40518.
3415 const size_type __size
= __last
- __first
;
3418 const size_type __pos
= __first
- _M_ibegin();
3419 _M_mutate(__pos
, __size
, size_type(0));
3420 _M_rep()->_M_set_leaked();
3421 return iterator(_M_data() + __pos
);
3427 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3428 basic_string
<_CharT
, _Traits
, _Alloc
>&
3429 basic_string
<_CharT
, _Traits
, _Alloc
>::
3430 replace(size_type __pos
, size_type __n1
, const _CharT
* __s
,
3433 __glibcxx_requires_string_len(__s
, __n2
);
3434 _M_check(__pos
, "basic_string::replace");
3435 __n1
= _M_limit(__pos
, __n1
);
3436 _M_check_length(__n1
, __n2
, "basic_string::replace");
3438 if (_M_disjunct(__s
) || _M_rep()->_M_is_shared())
3439 return _M_replace_safe(__pos
, __n1
, __s
, __n2
);
3440 else if ((__left
= __s
+ __n2
<= _M_data() + __pos
)
3441 || _M_data() + __pos
+ __n1
<= __s
)
3443 // Work in-place: non-overlapping case.
3444 size_type __off
= __s
- _M_data();
3445 __left
? __off
: (__off
+= __n2
- __n1
);
3446 _M_mutate(__pos
, __n1
, __n2
);
3447 _M_copy(_M_data() + __pos
, _M_data() + __off
, __n2
);
3452 // TODO: overlapping case.
3453 const basic_string
__tmp(__s
, __n2
);
3454 return _M_replace_safe(__pos
, __n1
, __tmp
._M_data(), __n2
);
3458 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3460 basic_string
<_CharT
, _Traits
, _Alloc
>::_Rep::
3461 _M_destroy(const _Alloc
& __a
) throw ()
3463 const size_type __size
= sizeof(_Rep_base
)
3464 + (this->_M_capacity
+ 1) * sizeof(_CharT
);
3465 _Raw_bytes_alloc(__a
).deallocate(reinterpret_cast<char*>(this), __size
);
3468 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3470 basic_string
<_CharT
, _Traits
, _Alloc
>::
3473 // No need to create a new copy of an empty string when a non-const
3474 // reference/pointer/iterator into it is obtained. Modifying the
3475 // trailing null character is undefined, so the ref/pointer/iterator
3476 // is effectively const anyway.
3480 if (_M_rep()->_M_is_shared())
3482 _M_rep()->_M_set_leaked();
3485 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3487 basic_string
<_CharT
, _Traits
, _Alloc
>::
3488 _M_mutate(size_type __pos
, size_type __len1
, size_type __len2
)
3490 const size_type __old_size
= this->size();
3491 const size_type __new_size
= __old_size
+ __len2
- __len1
;
3492 const size_type __how_much
= __old_size
- __pos
- __len1
;
3494 if (__new_size
> this->capacity() || _M_rep()->_M_is_shared())
3497 const allocator_type __a
= get_allocator();
3498 _Rep
* __r
= _Rep::_S_create(__new_size
, this->capacity(), __a
);
3501 _M_copy(__r
->_M_refdata(), _M_data(), __pos
);
3503 _M_copy(__r
->_M_refdata() + __pos
+ __len2
,
3504 _M_data() + __pos
+ __len1
, __how_much
);
3506 _M_rep()->_M_dispose(__a
);
3507 _M_data(__r
->_M_refdata());
3509 else if (__how_much
&& __len1
!= __len2
)
3512 _M_move(_M_data() + __pos
+ __len2
,
3513 _M_data() + __pos
+ __len1
, __how_much
);
3515 _M_rep()->_M_set_length_and_sharable(__new_size
);
3518 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3520 basic_string
<_CharT
, _Traits
, _Alloc
>::
3521 reserve(size_type __res
)
3523 const size_type __capacity
= capacity();
3525 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3526 // 2968. Inconsistencies between basic_string reserve and
3527 // vector/unordered_map/unordered_set reserve functions
3528 // P0966 reserve should not shrink
3529 if (__res
<= __capacity
)
3531 if (!_M_rep()->_M_is_shared())
3534 // unshare, but keep same capacity
3538 const allocator_type __a
= get_allocator();
3539 _CharT
* __tmp
= _M_rep()->_M_clone(__a
, __res
- this->size());
3540 _M_rep()->_M_dispose(__a
);
3544 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3546 basic_string
<_CharT
, _Traits
, _Alloc
>::
3547 swap(basic_string
& __s
)
3548 _GLIBCXX_NOEXCEPT_IF(allocator_traits
<_Alloc
>::is_always_equal::value
)
3550 if (_M_rep()->_M_is_leaked())
3551 _M_rep()->_M_set_sharable();
3552 if (__s
._M_rep()->_M_is_leaked())
3553 __s
._M_rep()->_M_set_sharable();
3554 if (this->get_allocator() == __s
.get_allocator())
3556 _CharT
* __tmp
= _M_data();
3557 _M_data(__s
._M_data());
3560 // The code below can usually be optimized away.
3563 const basic_string
__tmp1(_M_ibegin(), _M_iend(),
3564 __s
.get_allocator());
3565 const basic_string
__tmp2(__s
._M_ibegin(), __s
._M_iend(),
3566 this->get_allocator());
3572 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3573 typename basic_string
<_CharT
, _Traits
, _Alloc
>::_Rep
*
3574 basic_string
<_CharT
, _Traits
, _Alloc
>::_Rep::
3575 _S_create(size_type __capacity
, size_type __old_capacity
,
3576 const _Alloc
& __alloc
)
3578 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3579 // 83. String::npos vs. string::max_size()
3580 if (__capacity
> _S_max_size
)
3581 __throw_length_error(__N("basic_string::_S_create"));
3583 // The standard places no restriction on allocating more memory
3584 // than is strictly needed within this layer at the moment or as
3585 // requested by an explicit application call to reserve(n).
3587 // Many malloc implementations perform quite poorly when an
3588 // application attempts to allocate memory in a stepwise fashion
3589 // growing each allocation size by only 1 char. Additionally,
3590 // it makes little sense to allocate less linear memory than the
3591 // natural blocking size of the malloc implementation.
3592 // Unfortunately, we would need a somewhat low-level calculation
3593 // with tuned parameters to get this perfect for any particular
3594 // malloc implementation. Fortunately, generalizations about
3595 // common features seen among implementations seems to suffice.
3597 // __pagesize need not match the actual VM page size for good
3598 // results in practice, thus we pick a common value on the low
3599 // side. __malloc_header_size is an estimate of the amount of
3600 // overhead per memory allocation (in practice seen N * sizeof
3601 // (void*) where N is 0, 2 or 4). According to folklore,
3602 // picking this value on the high side is better than
3603 // low-balling it (especially when this algorithm is used with
3604 // malloc implementations that allocate memory blocks rounded up
3605 // to a size which is a power of 2).
3606 const size_type __pagesize
= 4096;
3607 const size_type __malloc_header_size
= 4 * sizeof(void*);
3609 // The below implements an exponential growth policy, necessary to
3610 // meet amortized linear time requirements of the library: see
3611 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
3612 // It's active for allocations requiring an amount of memory above
3613 // system pagesize. This is consistent with the requirements of the
3614 // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
3615 if (__capacity
> __old_capacity
&& __capacity
< 2 * __old_capacity
)
3616 __capacity
= 2 * __old_capacity
;
3618 // NB: Need an array of char_type[__capacity], plus a terminating
3619 // null char_type() element, plus enough for the _Rep data structure.
3620 // Whew. Seemingly so needy, yet so elemental.
3621 size_type __size
= (__capacity
+ 1) * sizeof(_CharT
) + sizeof(_Rep
);
3623 const size_type __adj_size
= __size
+ __malloc_header_size
;
3624 if (__adj_size
> __pagesize
&& __capacity
> __old_capacity
)
3626 const size_type __extra
= __pagesize
- __adj_size
% __pagesize
;
3627 __capacity
+= __extra
/ sizeof(_CharT
);
3628 // Never allocate a string bigger than _S_max_size.
3629 if (__capacity
> _S_max_size
)
3630 __capacity
= _S_max_size
;
3631 __size
= (__capacity
+ 1) * sizeof(_CharT
) + sizeof(_Rep
);
3634 // NB: Might throw, but no worries about a leak, mate: _Rep()
3636 void* __place
= _Raw_bytes_alloc(__alloc
).allocate(__size
);
3637 _Rep
*__p
= new (__place
) _Rep
;
3638 __p
->_M_capacity
= __capacity
;
3639 // ABI compatibility - 3.4.x set in _S_create both
3640 // _M_refcount and _M_length. All callers of _S_create
3641 // in basic_string.tcc then set just _M_length.
3642 // In 4.0.x and later both _M_refcount and _M_length
3643 // are initialized in the callers, unfortunately we can
3644 // have 3.4.x compiled code with _S_create callers inlined
3645 // calling 4.0.x+ _S_create.
3646 __p
->_M_set_sharable();
3650 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3652 basic_string
<_CharT
, _Traits
, _Alloc
>::_Rep::
3653 _M_clone(const _Alloc
& __alloc
, size_type __res
)
3655 // Requested capacity of the clone.
3656 const size_type __requested_cap
= this->_M_length
+ __res
;
3657 _Rep
* __r
= _Rep::_S_create(__requested_cap
, this->_M_capacity
,
3659 if (this->_M_length
)
3660 _M_copy(__r
->_M_refdata(), _M_refdata(), this->_M_length
);
3662 __r
->_M_set_length_and_sharable(this->_M_length
);
3663 return __r
->_M_refdata();
3666 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3668 basic_string
<_CharT
, _Traits
, _Alloc
>::
3669 resize(size_type __n
, _CharT __c
)
3671 const size_type __size
= this->size();
3672 _M_check_length(__size
, __n
, "basic_string::resize");
3674 this->append(__n
- __size
, __c
);
3675 else if (__n
< __size
)
3677 // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
3680 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3681 template<typename _InputIterator
>
3682 basic_string
<_CharT
, _Traits
, _Alloc
>&
3683 basic_string
<_CharT
, _Traits
, _Alloc
>::
3684 _M_replace_dispatch(iterator __i1
, iterator __i2
, _InputIterator __k1
,
3685 _InputIterator __k2
, __false_type
)
3687 const basic_string
__s(__k1
, __k2
);
3688 const size_type __n1
= __i2
- __i1
;
3689 _M_check_length(__n1
, __s
.size(), "basic_string::_M_replace_dispatch");
3690 return _M_replace_safe(__i1
- _M_ibegin(), __n1
, __s
._M_data(),
3694 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3695 basic_string
<_CharT
, _Traits
, _Alloc
>&
3696 basic_string
<_CharT
, _Traits
, _Alloc
>::
3697 _M_replace_aux(size_type __pos1
, size_type __n1
, size_type __n2
,
3700 _M_check_length(__n1
, __n2
, "basic_string::_M_replace_aux");
3701 _M_mutate(__pos1
, __n1
, __n2
);
3703 _M_assign(_M_data() + __pos1
, __n2
, __c
);
3707 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3708 basic_string
<_CharT
, _Traits
, _Alloc
>&
3709 basic_string
<_CharT
, _Traits
, _Alloc
>::
3710 _M_replace_safe(size_type __pos1
, size_type __n1
, const _CharT
* __s
,
3713 _M_mutate(__pos1
, __n1
, __n2
);
3715 _M_copy(_M_data() + __pos1
, __s
, __n2
);
3719 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3721 basic_string
<_CharT
, _Traits
, _Alloc
>::
3724 #if __cpp_exceptions
3725 if (length() < capacity() || _M_rep()->_M_is_shared())
3728 const allocator_type __a
= get_allocator();
3729 _CharT
* __tmp
= _M_rep()->_M_clone(__a
);
3730 _M_rep()->_M_dispose(__a
);
3733 catch (const __cxxabiv1::__forced_unwind
&)
3736 { /* swallow the exception */ }
3740 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3741 typename basic_string
<_CharT
, _Traits
, _Alloc
>::size_type
3742 basic_string
<_CharT
, _Traits
, _Alloc
>::
3743 copy(_CharT
* __s
, size_type __n
, size_type __pos
) const
3745 _M_check(__pos
, "basic_string::copy");
3746 __n
= _M_limit(__pos
, __n
);
3747 __glibcxx_requires_string_len(__s
, __n
);
3749 _M_copy(__s
, _M_data() + __pos
, __n
);
3750 // 21.3.5.7 par 3: do not append null. (good.)
3754 #ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23
3755 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3756 template<typename _Operation
>
3757 [[__gnu__::__always_inline__
]]
3759 basic_string
<_CharT
, _Traits
, _Alloc
>::
3760 __resize_and_overwrite(const size_type __n
, _Operation __op
)
3761 { resize_and_overwrite
<_Operation
&>(__n
, __op
); }
3764 #if __cplusplus >= 201103L
3765 template<typename _CharT
, typename _Traits
, typename _Alloc
>
3766 template<typename _Operation
>
3768 basic_string
<_CharT
, _Traits
, _Alloc
>::
3769 #ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23
3770 resize_and_overwrite(const size_type __n
, _Operation __op
)
3772 __resize_and_overwrite(const size_type __n
, _Operation __op
)
3775 const size_type __capacity
= capacity();
3777 if (__n
> __capacity
|| _M_rep()->_M_is_shared())
3780 struct _Terminator
{
3781 ~_Terminator() { _M_this
->_M_rep()->_M_set_length_and_sharable(_M_r
); }
3782 basic_string
* _M_this
;
3785 _Terminator __term
{this, 0};
3786 auto __r
= std::move(__op
)(__p
+ 0, __n
+ 0);
3787 #ifdef __cpp_lib_concepts
3788 static_assert(ranges::__detail::__is_integer_like
<decltype(__r
)>);
3790 static_assert(__gnu_cxx::__is_integer_nonstrict
<decltype(__r
)>::__value
,
3791 "resize_and_overwrite operation must return an integer");
3793 _GLIBCXX_DEBUG_ASSERT(__r
>= 0 && __r
<= __n
);
3794 __term
._M_r
= size_type(__r
);
3795 if (__term
._M_r
> __n
)
3796 __builtin_unreachable();
3801 _GLIBCXX_END_NAMESPACE_VERSION
3803 #endif // ! _GLIBCXX_USE_CXX11_ABI
3804 #endif // _COW_STRING_H