libstdc++: cow_string.h Source File

libstdc++

cow_string.h
Go to the documentation of this file.
1// Definition of gcc4-compatible Copy-on-Write basic_string -*- C++ -*-
2
3// Copyright (C) 1997-2026 Free Software Foundation, Inc.
4//
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)
9// any later version.
10
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.
15
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.
19
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/>.
24
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}
28 *
29 * Defines the reference-counted COW string implementation.
30 */
31
32#ifndef _COW_STRING_H
33#define _COW_STRING_H 1
34
35#if ! _GLIBCXX_USE_CXX11_ABI
36
37#include <ext/atomicity.h> // _Atomic_word, __is_single_threaded
38
39namespace std _GLIBCXX_VISIBILITY(default)
40{
41_GLIBCXX_BEGIN_NAMESPACE_VERSION
42
43 /**
44 * @class basic_string basic_string.h <string>
45 * @brief Managing sequences of characters and character-like objects.
46 *
47 * @ingroup strings
48 * @ingroup sequences
49 * @headerfile string
50 * @since C++98
51 *
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>.
56 *
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.
62 *
63 * @doctodo
64 *
65 *
66 * Documentation? What's that?
67 * Nathan Myers <ncm@cantrip.org>.
68 *
69 * A string looks like this:
70 *
71 * @code
72 * [_Rep]
73 * _M_length
74 * [basic_string<char_type>] _M_capacity
75 * _M_dataplus _M_refcount
76 * _M_p ----------------> unnamed array of char_type
77 * @endcode
78 *
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.
82 *
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.
90 *
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
95 * string length.)
96 *
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.
102 *
103 * All but the last paragraph is considered pretty conventional
104 * for a Copy-On-Write C++ string implementation.
105 */
106 // 21.3 Template class basic_string
107 template<typename _CharT, typename _Traits, typename _Alloc>
108 class basic_string
109 {
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;
113
114 // Types:
115 public:
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;
124#else
125 typedef value_type& reference;
126 typedef const value_type& const_reference;
127#endif
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>
132 const_iterator;
133 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
134 typedef std::reverse_iterator<iterator> reverse_iterator;
135
136 protected:
137 // type used for positions in insert, erase etc.
138 typedef iterator __const_iterator;
139
140 private:
141 // _Rep: string representation
142 // Invariants:
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.
154
155 struct _Rep_base
156 {
157 size_type _M_length;
158 size_type _M_capacity;
159 _Atomic_word _M_refcount;
160 };
161
162 struct _Rep : _Rep_base
163 {
164 // Types:
165 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
166 rebind<char>::other _Raw_bytes_alloc;
167
168 // (Public) Data members:
169
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
176 // look like this:
177 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
178 // Solving for m:
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;
183
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[];
187
188 static _Rep&
189 _S_empty_rep() _GLIBCXX_NOEXCEPT
190 {
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);
196 }
197
198 bool
199 _M_is_leaked() const _GLIBCXX_NOEXCEPT
200 {
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;
207#else
208 return this->_M_refcount < 0;
209#endif
210 }
211
212 bool
213 _M_is_shared() const _GLIBCXX_NOEXCEPT
214 {
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
220 // _M_dispose.
221 if (!__gnu_cxx::__is_single_threaded())
222 return __atomic_load_n(&this->_M_refcount, __ATOMIC_ACQUIRE) > 0;
223#endif
224 return this->_M_refcount > 0;
225 }
226
227 void
228 _M_set_leaked() _GLIBCXX_NOEXCEPT
229 { this->_M_refcount = -1; }
230
231 void
232 _M_set_sharable() _GLIBCXX_NOEXCEPT
233 { this->_M_refcount = 0; }
234
235 void
236 _M_set_length_and_sharable(size_type __n) _GLIBCXX_NOEXCEPT
237 {
238#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
239 if (__builtin_expect(this != &_S_empty_rep(), false))
240#endif
241 {
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.
247 }
248 }
249
250 _CharT*
251 _M_refdata() throw()
252 { return reinterpret_cast<_CharT*>(this + 1); }
253
254 _CharT*
255 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
256 {
257 return (!_M_is_leaked() && __alloc1 == __alloc2)
258 ? _M_refcopy() : _M_clone(__alloc1);
259 }
260
261 // Create & Destroy
262 static _Rep*
263 _S_create(size_type, size_type, const _Alloc&);
264
265 void
266 _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPT
267 {
268#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
269 if (__builtin_expect(this != &_S_empty_rep(), false))
270#endif
271 {
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,
283 -1) <= 0)
284 {
285 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount);
286 _M_destroy(__a);
287 }
288 }
289 } // XXX MT
290
291 void
292 _M_destroy(const _Alloc&) throw();
293
294 _CharT*
295 _M_refcopy() throw()
296 {
297#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
298 if (__builtin_expect(this != &_S_empty_rep(), false))
299#endif
300 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
301 return _M_refdata();
302 } // XXX MT
303
304 _CharT*
305 _M_clone(const _Alloc&, size_type __res = 0);
306 };
307
308 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
309 struct _Alloc_hider : _Alloc
310 {
311 _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT
312 : _Alloc(__a), _M_p(__dat) { }
313
314 _CharT* _M_p; // The actual data.
315 };
316
317 public:
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);
323
324 private:
325 // Data Members (private):
326 mutable _Alloc_hider _M_dataplus;
327
328 _CharT*
329 _M_data() const _GLIBCXX_NOEXCEPT
330 { return _M_dataplus._M_p; }
331
332 _CharT*
333 _M_data(_CharT* __p) _GLIBCXX_NOEXCEPT
334 { return (_M_dataplus._M_p = __p); }
335
336 _Rep*
337 _M_rep() const _GLIBCXX_NOEXCEPT
338 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
339
340 // For the internal use we have functions similar to `begin'/`end'
341 // but they do not call _M_leak.
342 iterator
343 _M_ibegin() const _GLIBCXX_NOEXCEPT
344 { return iterator(_M_data()); }
345
346 iterator
347 _M_iend() const _GLIBCXX_NOEXCEPT
348 { return iterator(_M_data() + this->size()); }
349
350 void
351 _M_leak() // for use in begin() & non-const op[]
352 {
353 if (!_M_rep()->_M_is_leaked())
354 _M_leak_hard();
355 }
356
357 size_type
358 _M_check(size_type __pos, const char* __s) const
359 {
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());
364 return __pos;
365 }
366
367 void
368 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
369 {
370 if (this->max_size() - (this->size() - __n1) < __n2)
371 __throw_length_error(__N(__s));
372 }
373
374 // NB: _M_limit doesn't check for a bad __pos value.
375 size_type
376 _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
377 {
378 const bool __testoff = __off < this->size() - __pos;
379 return __testoff ? __off : this->size() - __pos;
380 }
381
382 // True if _Rep and source do not overlap.
383 bool
384 _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
385 {
386 return (less<const _CharT*>()(__s, _M_data())
387 || less<const _CharT*>()(_M_data() + this->size(), __s));
388 }
389
390 // When __n = 1 way faster than the general multichar
391 // traits_type::copy/move/assign.
392 static void
393 _M_copy(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
394 {
395 if (__n == 1)
396 traits_type::assign(*__d, *__s);
397 else
398 traits_type::copy(__d, __s, __n);
399 }
400
401 static void
402 _M_move(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
403 {
404 if (__n == 1)
405 traits_type::assign(*__d, *__s);
406 else
407 traits_type::move(__d, __s, __n);
408 }
409
410 static void
411 _M_assign(_CharT* __d, size_type __n, _CharT __c) _GLIBCXX_NOEXCEPT
412 {
413 if (__n == 1)
414 traits_type::assign(*__d, __c);
415 else
416 traits_type::assign(__d, __n, __c);
417 }
418
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>
422 static void
423 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
424 {
425 for (; __k1 != __k2; ++__k1, (void)++__p)
426 traits_type::assign(*__p, static_cast<_CharT>(*__k1));
427 }
428
429 static void
430 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
431 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
432
433 static void
434 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
435 _GLIBCXX_NOEXCEPT
436 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
437
438 static void
439 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
440 { _M_copy(__p, __k1, __k2 - __k1); }
441
442 static void
443 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
444 _GLIBCXX_NOEXCEPT
445 { _M_copy(__p, __k1, __k2 - __k1); }
446
447 static int
448 _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
449 {
450 const difference_type __d = difference_type(__n1 - __n2);
451
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;
456 else
457 return int(__d);
458 }
459
460 void
461 _M_mutate(size_type __pos, size_type __len1, size_type __len2);
462
463 void
464 _M_leak_hard();
465
466 static _Rep&
467 _S_empty_rep() _GLIBCXX_NOEXCEPT
468 { return _Rep::_S_empty_rep(); }
469
470#ifdef __glibcxx_string_view // >= C++17
471 // A helper type for avoiding boiler-plate.
472 typedef basic_string_view<_CharT, _Traits> __sv_type;
473
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,
479 _Res>;
480
481 // Allows an implicit conversion to __sv_type.
482 static __sv_type
483 _S_to_string_view(__sv_type __svt) noexcept
484 { return __svt; }
485
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
489 // is provided.
490 struct __sv_wrapper
491 {
492 explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
493 __sv_type _M_sv;
494 };
495
496 /**
497 * @brief Only internally used: Construct string from a string view
498 * wrapper.
499 * @param __svw string view wrapper.
500 * @param __a Allocator to use.
501 */
502 explicit
503 basic_string(__sv_wrapper __svw, const _Alloc& __a)
504 : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
505#endif
506
507 public:
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.
511
512 /**
513 * @brief Default constructor creates an empty string.
514 */
515 basic_string()
516#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
517 _GLIBCXX_NOEXCEPT
518#endif
519#if __cpp_concepts && __glibcxx_type_trait_variable_templates
520 requires is_default_constructible_v<_Alloc>
521#endif
522#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
523 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc())
524#else
525 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc())
526#endif
527 { }
528
529 /**
530 * @brief Construct an empty string using allocator @a a.
531 */
532 explicit
533 basic_string(const _Alloc& __a)
534 : _M_dataplus(_S_construct(size_type(), _CharT(), __a), __a)
535 { }
536
537 // NB: per LWG issue 42, semantics different from IS:
538 /**
539 * @brief Construct string with copy of value of @a str.
540 * @param __str Source string.
541 */
542 basic_string(const basic_string& __str)
543 : _M_dataplus(__str._M_rep()->_M_grab(_Alloc(__str.get_allocator()),
544 __str.get_allocator()),
545 __str.get_allocator())
546 { }
547
548 // _GLIBCXX_RESOLVE_LIB_DEFECTS
549 // 2583. no way to supply an allocator for basic_string(str, pos)
550 /**
551 * @brief Construct string as copy of a substring.
552 * @param __str Source string.
553 * @param __pos Index of first character to copy from.
554 * @param __a Allocator to use.
555 */
556 basic_string(const basic_string& __str, size_type __pos,
557 const _Alloc& __a = _Alloc());
558
559 /**
560 * @brief Construct string as copy of a substring.
561 * @param __str Source string.
562 * @param __pos Index of first character to copy from.
563 * @param __n Number of characters to copy.
564 */
565 basic_string(const basic_string& __str, size_type __pos,
566 size_type __n);
567 /**
568 * @brief Construct string as copy of a substring.
569 * @param __str Source string.
570 * @param __pos Index of first character to copy from.
571 * @param __n Number of characters to copy.
572 * @param __a Allocator to use.
573 */
574 basic_string(const basic_string& __str, size_type __pos,
575 size_type __n, const _Alloc& __a);
576
577 /**
578 * @brief Construct string initialized by a character %array.
579 * @param __s Source character %array.
580 * @param __n Number of characters to copy.
581 * @param __a Allocator to use (default is default allocator).
582 *
583 * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
584 * has no special meaning.
585 */
586 basic_string(const _CharT* __s, size_type __n,
587 const _Alloc& __a = _Alloc())
588 : _M_dataplus(_S_construct(__s, __s + __n, __a), __a)
589 { }
590
591 /**
592 * @brief Construct string as copy of a C string.
593 * @param __s Source C string.
594 * @param __a Allocator to use (default is default allocator).
595 */
596#if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
597 // _GLIBCXX_RESOLVE_LIB_DEFECTS
598 // 3076. basic_string CTAD ambiguity
599 template<typename = _RequireAllocator<_Alloc>>
600#endif
601 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
602 : _M_dataplus(_S_construct(__s, __s ? __s + traits_type::length(__s) :
603 __s + npos, __a), __a)
604 { }
605
606 /**
607 * @brief Construct string as multiple characters.
608 * @param __n Number of characters.
609 * @param __c Character to use.
610 * @param __a Allocator to use (default is default allocator).
611 */
612 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
613 : _M_dataplus(_S_construct(__n, __c, __a), __a)
614 { }
615
616#if __cplusplus >= 201103L
617 /**
618 * @brief Move construct string.
619 * @param __str Source string.
620 *
621 * The newly-created string contains the exact contents of @a __str.
622 * @a __str is a valid, but unspecified string.
623 */
624 basic_string(basic_string&& __str) noexcept
625 : _M_dataplus(std::move(__str._M_dataplus))
626 {
627#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
628 // Make __str use the shared empty string rep.
629 __str._M_data(_S_empty_rep()._M_refdata());
630#else
631 // Rather than allocate an empty string for the rvalue string,
632 // just share ownership with it by incrementing the reference count.
633 // If the rvalue string was the unique owner then there are exactly
634 // two owners now.
635 if (_M_rep()->_M_is_shared())
636 __gnu_cxx::__atomic_add_dispatch(&_M_rep()->_M_refcount, 1);
637 else
638 _M_rep()->_M_refcount = 1;
639#endif
640 }
641
642#if __glibcxx_containers_ranges // C++ >= 23
643 /**
644 * @brief Construct a string from a range.
645 * @since C++23
646 */
647 template<__detail::__container_compatible_range<_CharT> _Rg>
648 basic_string(from_range_t, _Rg&& __rg, const _Alloc& __a = _Alloc())
649 : basic_string(__a)
650 {
651 if constexpr (ranges::forward_range<_Rg> || ranges::sized_range<_Rg>)
652 {
653 const auto __n = static_cast<size_type>(ranges::distance(__rg));
654 if (__n == 0)
655 return;
656
657 reserve(__n);
658 pointer __p = _M_data();
659 if constexpr (requires {
660 requires ranges::contiguous_range<_Rg>;
661 { ranges::data(std::forward<_Rg>(__rg)) }
662 -> convertible_to<const _CharT*>;
663 })
664 _M_copy(__p, ranges::data(std::forward<_Rg>(__rg)), __n);
665 else
666 {
667 auto __first = ranges::begin(__rg);
668 const auto __last = ranges::end(__rg);
669 for (; __first != __last; ++__first)
670 traits_type::assign(*__p++, static_cast<_CharT>(*__first));
671 }
672 _M_rep()->_M_set_length_and_sharable(__n);
673 }
674 else
675 {
676 auto __first = ranges::begin(__rg);
677 const auto __last = ranges::end(__rg);
678 for (; __first != __last; ++__first)
679 push_back(*__first);
680 }
681 }
682#endif
683
684 /**
685 * @brief Construct string from an initializer %list.
686 * @param __l std::initializer_list of characters.
687 * @param __a Allocator to use (default is default allocator).
688 */
689 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
690 : _M_dataplus(_S_construct(__l.begin(), __l.end(), __a), __a)
691 { }
692
693 basic_string(const basic_string& __str, const _Alloc& __a)
694 : _M_dataplus(__str._M_rep()->_M_grab(__a, __str.get_allocator()), __a)
695 { }
696
697 basic_string(basic_string&& __str, const _Alloc& __a)
698 : _M_dataplus(__str._M_data(), __a)
699 {
700 if (__a == __str.get_allocator())
701 {
702#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
703 __str._M_data(_S_empty_rep()._M_refdata());
704#else
705 __str._M_data(_S_construct(size_type(), _CharT(), __a));
706#endif
707 }
708 else
709 _M_dataplus._M_p = _S_construct(__str.begin(), __str.end(), __a);
710 }
711#endif // C++11
712
713#if __cplusplus >= 202100L
714 basic_string(nullptr_t) = delete;
715 basic_string& operator=(nullptr_t) = delete;
716#endif // C++23
717
718 /**
719 * @brief Construct string as copy of a range.
720 * @param __beg Start of range.
721 * @param __end End of range.
722 * @param __a Allocator to use (default is default allocator).
723 */
724 template<class _InputIterator>
725 basic_string(_InputIterator __beg, _InputIterator __end,
726 const _Alloc& __a = _Alloc())
727 : _M_dataplus(_S_construct(__beg, __end, __a), __a)
728 { }
729
730#ifdef __glibcxx_string_view // >= C++17
731 /**
732 * @brief Construct string from a substring of a string_view.
733 * @param __t Source object convertible to string view.
734 * @param __pos The index of the first character to copy from __t.
735 * @param __n The number of characters to copy from __t.
736 * @param __a Allocator to use.
737 */
738 template<typename _Tp,
739 typename = enable_if_t<is_convertible_v<const _Tp&, __sv_type>>>
740 basic_string(const _Tp& __t, size_type __pos, size_type __n,
741 const _Alloc& __a = _Alloc())
742 : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
743
744 /**
745 * @brief Construct string from a string_view.
746 * @param __t Source object convertible to string view.
747 * @param __a Allocator to use (default is default allocator).
748 */
749 template<typename _Tp, typename = _If_sv<_Tp, void>>
750 explicit
751 basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
752 : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
753#endif // C++17
754
755 /**
756 * @brief Destroy the string instance.
757 */
758 ~basic_string() _GLIBCXX_NOEXCEPT
759 { _M_rep()->_M_dispose(this->get_allocator()); }
760
761 /**
762 * @brief Assign the value of @a str to this string.
763 * @param __str Source string.
764 */
765 basic_string&
766 operator=(const basic_string& __str)
767 { return this->assign(__str); }
768
769 /**
770 * @brief Copy contents of @a s into this string.
771 * @param __s Source null-terminated string.
772 */
773 basic_string&
774 operator=(const _CharT* __s)
775 { return this->assign(__s); }
776
777 /**
778 * @brief Set value to string of length 1.
779 * @param __c Source character.
780 *
781 * Assigning to a character makes this string length 1 and
782 * (*this)[0] == @a c.
783 */
784 basic_string&
785 operator=(_CharT __c)
786 {
787 this->assign(1, __c);
788 return *this;
789 }
790
791#if __cplusplus >= 201103L
792 /**
793 * @brief Move assign the value of @a str to this string.
794 * @param __str Source string.
795 *
796 * The contents of @a str are moved into this string (without copying).
797 * @a str is a valid, but unspecified string.
798 */
799 basic_string&
800 operator=(basic_string&& __str)
801 _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value)
802 {
803 // NB: DR 1204.
804 this->swap(__str);
805 return *this;
806 }
807
808 /**
809 * @brief Set value to string constructed from initializer %list.
810 * @param __l std::initializer_list.
811 */
812 basic_string&
813 operator=(initializer_list<_CharT> __l)
814 {
815 this->assign(__l.begin(), __l.size());
816 return *this;
817 }
818#endif // C++11
819
820#ifdef __glibcxx_string_view // >= C++17
821 /**
822 * @brief Set value to string constructed from a string_view.
823 * @param __svt An object convertible to string_view.
824 */
825 template<typename _Tp>
826 _If_sv<_Tp, basic_string&>
827 operator=(const _Tp& __svt)
828 { return this->assign(__svt); }
829
830 /**
831 * @brief Convert to a string_view.
832 * @return A string_view.
833 */
834 operator __sv_type() const noexcept
835 { return __sv_type(data(), size()); }
836#endif // C++17
837
838 // Iterators:
839 /**
840 * Returns a read/write iterator that points to the first character in
841 * the %string. Unshares the string.
842 */
843 iterator
844 begin() // FIXME C++11: should be noexcept.
845 {
846 _M_leak();
847 return iterator(_M_data());
848 }
849
850 /**
851 * Returns a read-only (constant) iterator that points to the first
852 * character in the %string.
853 */
854 const_iterator
855 begin() const _GLIBCXX_NOEXCEPT
856 { return const_iterator(_M_data()); }
857
858 /**
859 * Returns a read/write iterator that points one past the last
860 * character in the %string. Unshares the string.
861 */
862 iterator
863 end() // FIXME C++11: should be noexcept.
864 {
865 _M_leak();
866 return iterator(_M_data() + this->size());
867 }
868
869 /**
870 * Returns a read-only (constant) iterator that points one past the
871 * last character in the %string.
872 */
873 const_iterator
874 end() const _GLIBCXX_NOEXCEPT
875 { return const_iterator(_M_data() + this->size()); }
876
877 /**
878 * Returns a read/write reverse iterator that points to the last
879 * character in the %string. Iteration is done in reverse element
880 * order. Unshares the string.
881 */
882 reverse_iterator
883 rbegin() // FIXME C++11: should be noexcept.
884 { return reverse_iterator(this->end()); }
885
886 /**
887 * Returns a read-only (constant) reverse iterator that points
888 * to the last character in the %string. Iteration is done in
889 * reverse element order.
890 */
891 const_reverse_iterator
892 rbegin() const _GLIBCXX_NOEXCEPT
893 { return const_reverse_iterator(this->end()); }
894
895 /**
896 * Returns a read/write reverse iterator that points to one before the
897 * first character in the %string. Iteration is done in reverse
898 * element order. Unshares the string.
899 */
900 reverse_iterator
901 rend() // FIXME C++11: should be noexcept.
902 { return reverse_iterator(this->begin()); }
903
904 /**
905 * Returns a read-only (constant) reverse iterator that points
906 * to one before the first character in the %string. Iteration
907 * is done in reverse element order.
908 */
909 const_reverse_iterator
910 rend() const _GLIBCXX_NOEXCEPT
911 { return const_reverse_iterator(this->begin()); }
912
913#if __cplusplus >= 201103L
914 /**
915 * Returns a read-only (constant) iterator that points to the first
916 * character in the %string.
917 */
918 const_iterator
919 cbegin() const noexcept
920 { return const_iterator(this->_M_data()); }
921
922 /**
923 * Returns a read-only (constant) iterator that points one past the
924 * last character in the %string.
925 */
926 const_iterator
927 cend() const noexcept
928 { return const_iterator(this->_M_data() + this->size()); }
929
930 /**
931 * Returns a read-only (constant) reverse iterator that points
932 * to the last character in the %string. Iteration is done in
933 * reverse element order.
934 */
935 const_reverse_iterator
936 crbegin() const noexcept
937 { return const_reverse_iterator(this->end()); }
938
939 /**
940 * Returns a read-only (constant) reverse iterator that points
941 * to one before the first character in the %string. Iteration
942 * is done in reverse element order.
943 */
944 const_reverse_iterator
945 crend() const noexcept
946 { return const_reverse_iterator(this->begin()); }
947#endif
948
949 public:
950 // Capacity:
951
952 /// Returns the number of characters in the string, not including any
953 /// null-termination.
954 size_type
955 size() const _GLIBCXX_NOEXCEPT
956 {
957#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0 && __OPTIMIZE__
958 if (_S_empty_rep()._M_length != 0)
959 __builtin_unreachable();
960#endif
961 return _M_rep()->_M_length;
962 }
963
964 /// Returns the number of characters in the string, not including any
965 /// null-termination.
966 size_type
967 length() const _GLIBCXX_NOEXCEPT
968 { return size(); }
969
970 /// Returns the size() of the largest possible %string.
971 size_type
972 max_size() const _GLIBCXX_NOEXCEPT
973 { return _Rep::_S_max_size; }
974
975 /**
976 * @brief Resizes the %string to the specified number of characters.
977 * @param __n Number of characters the %string should contain.
978 * @param __c Character to fill any new elements.
979 *
980 * This function will %resize the %string to the specified
981 * number of characters. If the number is smaller than the
982 * %string's current size the %string is truncated, otherwise
983 * the %string is extended and new elements are %set to @a __c.
984 */
985 void
986 resize(size_type __n, _CharT __c);
987
988 /**
989 * @brief Resizes the %string to the specified number of characters.
990 * @param __n Number of characters the %string should contain.
991 *
992 * This function will resize the %string to the specified length. If
993 * the new size is smaller than the %string's current size the %string
994 * is truncated, otherwise the %string is extended and new characters
995 * are default-constructed. For basic types such as char, this means
996 * setting them to 0.
997 */
998 void
999 resize(size_type __n)
1000 { this->resize(__n, _CharT()); }
1001
1002#if __cplusplus >= 201103L
1003#pragma GCC diagnostic push
1004#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1005 /// A non-binding request to reduce capacity() to size().
1006 void
1007 shrink_to_fit() noexcept
1008 { reserve(); }
1009#pragma GCC diagnostic pop
1010#endif
1011
1012#ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23
1013 /** Resize the string and call a function to fill it.
1014 *
1015 * @param __n The maximum size requested.
1016 * @param __op A callable object that writes characters to the string.
1017 *
1018 * This is a low-level function that is easy to misuse, be careful.
1019 *
1020 * Calling `str.resize_and_overwrite(n, op)` will reserve at least `n`
1021 * characters in `str`, evaluate `n2 = std::move(op)(str.data(), n)`,
1022 * and finally set the string length to `n2` (adding a null terminator
1023 * at the end). The function object `op` is allowed to write to the
1024 * extra capacity added by the initial reserve operation, which is not
1025 * allowed if you just call `str.reserve(n)` yourself.
1026 *
1027 * This can be used to efficiently fill a `string` buffer without the
1028 * overhead of zero-initializing characters that will be overwritten
1029 * anyway.
1030 *
1031 * The callable `op` must not access the string directly (only through
1032 * the pointer passed as its first argument), must not write more than
1033 * `n` characters to the string, must return a value no greater than `n`,
1034 * and must ensure that all characters up to the returned length are
1035 * valid after it returns (i.e. there must be no uninitialized values
1036 * left in the string after the call, because accessing them would
1037 * have undefined behaviour). If `op` exits by throwing an exception
1038 * the behaviour is undefined.
1039 *
1040 * @since C++23
1041 */
1042 template<typename _Operation>
1043 void
1044 resize_and_overwrite(size_type __n, _Operation __op);
1045#endif // __glibcxx_string_resize_and_overwrite
1046
1047#if __cplusplus >= 201103L
1048 /// Non-standard version of resize_and_overwrite for C++11 and above.
1049 template<typename _Operation>
1050 void
1051 __resize_and_overwrite(size_type __n, _Operation __op);
1052#endif
1053
1054 /**
1055 * Returns the total number of characters that the %string can hold
1056 * before needing to allocate more memory.
1057 */
1058 size_type
1059 capacity() const _GLIBCXX_NOEXCEPT
1060 { return _M_rep()->_M_capacity; }
1061
1062 /**
1063 * @brief Attempt to preallocate enough memory for specified number of
1064 * characters.
1065 * @param __res_arg Number of characters required.
1066 * @throw std::length_error If @a __res_arg exceeds @c max_size().
1067 *
1068 * This function attempts to reserve enough memory for the
1069 * %string to hold the specified number of characters. If the
1070 * number requested is more than max_size(), length_error is
1071 * thrown.
1072 *
1073 * The advantage of this function is that if optimal code is a
1074 * necessity and the user can determine the string length that will be
1075 * required, the user can reserve the memory in %advance, and thus
1076 * prevent a possible reallocation of memory and copying of %string
1077 * data.
1078 */
1079 void
1080 reserve(size_type __res_arg);
1081
1082 /// Equivalent to shrink_to_fit().
1083#if __cplusplus >= 202002L
1084 [[deprecated("use shrink_to_fit() instead")]]
1085#endif
1086 void
1087 reserve();
1088
1089 /**
1090 * Erases the string, making it empty.
1091 */
1092#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
1093 void
1094 clear() _GLIBCXX_NOEXCEPT
1095 {
1096 if (_M_rep()->_M_is_shared())
1097 {
1098 _M_rep()->_M_dispose(this->get_allocator());
1099 _M_data(_S_empty_rep()._M_refdata());
1100 }
1101 else
1102 _M_rep()->_M_set_length_and_sharable(0);
1103 }
1104#else
1105 // PR 56166: this should not throw.
1106 void
1107 clear()
1108 { _M_mutate(0, this->size(), 0); }
1109#endif
1110
1111 /**
1112 * Returns true if the %string is empty. Equivalent to
1113 * <code>*this == ""</code>.
1114 */
1115 _GLIBCXX_NODISCARD bool
1116 empty() const _GLIBCXX_NOEXCEPT
1117 { return this->size() == 0; }
1118
1119 // Element access:
1120 /**
1121 * @brief Subscript access to the data contained in the %string.
1122 * @param __pos The index of the character to access.
1123 * @return Read-only (constant) reference to the character.
1124 *
1125 * This operator allows for easy, array-style, data access.
1126 * Note that data access with this operator is unchecked and
1127 * out_of_range lookups are not defined. (For checked lookups
1128 * see at().)
1129 */
1130 const_reference
1131 operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
1132 {
1133 __glibcxx_assert(__pos <= size());
1134 return _M_data()[__pos];
1135 }
1136
1137 /**
1138 * @brief Subscript access to the data contained in the %string.
1139 * @param __pos The index of the character to access.
1140 * @return Read/write reference to the character.
1141 *
1142 * This operator allows for easy, array-style, data access.
1143 * Note that data access with this operator is unchecked and
1144 * out_of_range lookups are not defined. (For checked lookups
1145 * see at().) Unshares the string.
1146 */
1147 reference
1148 operator[](size_type __pos)
1149 {
1150 // Allow pos == size() both in C++98 mode, as v3 extension,
1151 // and in C++11 mode.
1152 __glibcxx_assert(__pos <= size());
1153 // In pedantic mode be strict in C++98 mode.
1154 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
1155 _M_leak();
1156 return _M_data()[__pos];
1157 }
1158
1159 /**
1160 * @brief Provides access to the data contained in the %string.
1161 * @param __n The index of the character to access.
1162 * @return Read-only (const) reference to the character.
1163 * @throw std::out_of_range If @a n is an invalid index.
1164 *
1165 * This function provides for safer data access. The parameter is
1166 * first checked that it is in the range of the string. The function
1167 * throws out_of_range if the check fails.
1168 */
1169 const_reference
1170 at(size_type __n) const
1171 {
1172 if (__n >= this->size())
1173 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1174 "(which is %zu) >= this->size() "
1175 "(which is %zu)"),
1176 __n, this->size());
1177 return _M_data()[__n];
1178 }
1179
1180 /**
1181 * @brief Provides access to the data contained in the %string.
1182 * @param __n The index of the character to access.
1183 * @return Read/write reference to the character.
1184 * @throw std::out_of_range If @a n is an invalid index.
1185 *
1186 * This function provides for safer data access. The parameter is
1187 * first checked that it is in the range of the string. The function
1188 * throws out_of_range if the check fails. Success results in
1189 * unsharing the string.
1190 */
1191 reference
1192 at(size_type __n)
1193 {
1194 if (__n >= size())
1195 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1196 "(which is %zu) >= this->size() "
1197 "(which is %zu)"),
1198 __n, this->size());
1199 _M_leak();
1200 return _M_data()[__n];
1201 }
1202
1203#if __cplusplus >= 201103L
1204 /**
1205 * Returns a read/write reference to the data at the first
1206 * element of the %string.
1207 */
1208 reference
1209 front()
1210 {
1211 __glibcxx_assert(!empty());
1212 return operator[](0);
1213 }
1214
1215 /**
1216 * Returns a read-only (constant) reference to the data at the first
1217 * element of the %string.
1218 */
1219 const_reference
1220 front() const noexcept
1221 {
1222 __glibcxx_assert(!empty());
1223 return operator[](0);
1224 }
1225
1226 /**
1227 * Returns a read/write reference to the data at the last
1228 * element of the %string.
1229 */
1230 reference
1231 back()
1232 {
1233 __glibcxx_assert(!empty());
1234 return operator[](this->size() - 1);
1235 }
1236
1237 /**
1238 * Returns a read-only (constant) reference to the data at the
1239 * last element of the %string.
1240 */
1241 const_reference
1242 back() const noexcept
1243 {
1244 __glibcxx_assert(!empty());
1245 return operator[](this->size() - 1);
1246 }
1247#endif
1248
1249 // Modifiers:
1250 /**
1251 * @brief Append a string to this string.
1252 * @param __str The string to append.
1253 * @return Reference to this string.
1254 */
1255 basic_string&
1256 operator+=(const basic_string& __str)
1257 { return this->append(__str); }
1258
1259 /**
1260 * @brief Append a C string.
1261 * @param __s The C string to append.
1262 * @return Reference to this string.
1263 */
1264 basic_string&
1265 operator+=(const _CharT* __s)
1266 { return this->append(__s); }
1267
1268 /**
1269 * @brief Append a character.
1270 * @param __c The character to append.
1271 * @return Reference to this string.
1272 */
1273 basic_string&
1274 operator+=(_CharT __c)
1275 {
1276 this->push_back(__c);
1277 return *this;
1278 }
1279
1280#if __cplusplus >= 201103L
1281 /**
1282 * @brief Append an initializer_list of characters.
1283 * @param __l The initializer_list of characters to be appended.
1284 * @return Reference to this string.
1285 */
1286 basic_string&
1287 operator+=(initializer_list<_CharT> __l)
1288 { return this->append(__l.begin(), __l.size()); }
1289#endif // C++11
1290
1291#ifdef __glibcxx_string_view // >= C++17
1292 /**
1293 * @brief Append a string_view.
1294 * @param __svt The object convertible to string_view to be appended.
1295 * @return Reference to this string.
1296 */
1297 template<typename _Tp>
1298 _If_sv<_Tp, basic_string&>
1299 operator+=(const _Tp& __svt)
1300 { return this->append(__svt); }
1301#endif // C++17
1302
1303 /**
1304 * @brief Append a string to this string.
1305 * @param __str The string to append.
1306 * @return Reference to this string.
1307 */
1308 basic_string&
1309 append(const basic_string& __str);
1310
1311 /**
1312 * @brief Append a substring.
1313 * @param __str The string to append.
1314 * @param __pos Index of the first character of str to append.
1315 * @param __n The number of characters to append.
1316 * @return Reference to this string.
1317 * @throw std::out_of_range if @a __pos is not a valid index.
1318 *
1319 * This function appends @a __n characters from @a __str
1320 * starting at @a __pos to this string. If @a __n is is larger
1321 * than the number of available characters in @a __str, the
1322 * remainder of @a __str is appended.
1323 */
1324 basic_string&
1325 append(const basic_string& __str, size_type __pos, size_type __n = npos);
1326
1327 /**
1328 * @brief Append a C substring.
1329 * @param __s The C string to append.
1330 * @param __n The number of characters to append.
1331 * @return Reference to this string.
1332 */
1333 basic_string&
1334 append(const _CharT* __s, size_type __n);
1335
1336 /**
1337 * @brief Append a C string.
1338 * @param __s The C string to append.
1339 * @return Reference to this string.
1340 */
1341 basic_string&
1342 append(const _CharT* __s)
1343 {
1344 __glibcxx_requires_string(__s);
1345 return this->append(__s, traits_type::length(__s));
1346 }
1347
1348 /**
1349 * @brief Append multiple characters.
1350 * @param __n The number of characters to append.
1351 * @param __c The character to use.
1352 * @return Reference to this string.
1353 *
1354 * Appends __n copies of __c to this string.
1355 */
1356 basic_string&
1357 append(size_type __n, _CharT __c);
1358
1359#if __glibcxx_containers_ranges // C++ >= 23
1360 /**
1361 * @brief Append a range to the string.
1362 * @since C++23
1363 */
1364 template<__detail::__container_compatible_range<_CharT> _Rg>
1365 basic_string&
1366 append_range(_Rg&& __rg)
1367 {
1368 basic_string __s(from_range, std::forward<_Rg>(__rg),
1369 get_allocator());
1370 append(__s);
1371 return *this;
1372 }
1373#endif
1374
1375#if __cplusplus >= 201103L
1376 /**
1377 * @brief Append an initializer_list of characters.
1378 * @param __l The initializer_list of characters to append.
1379 * @return Reference to this string.
1380 */
1381 basic_string&
1382 append(initializer_list<_CharT> __l)
1383 { return this->append(__l.begin(), __l.size()); }
1384#endif // C++11
1385
1386 /**
1387 * @brief Append a range of characters.
1388 * @param __first Iterator referencing the first character to append.
1389 * @param __last Iterator marking the end of the range.
1390 * @return Reference to this string.
1391 *
1392 * Appends characters in the range [__first,__last) to this string.
1393 */
1394 template<class _InputIterator>
1395 basic_string&
1396 append(_InputIterator __first, _InputIterator __last)
1397 { return this->replace(_M_iend(), _M_iend(), __first, __last); }
1398
1399#ifdef __glibcxx_string_view // >= C++17
1400 /**
1401 * @brief Append a string_view.
1402 * @param __svt The object convertible to string_view to be appended.
1403 * @return Reference to this string.
1404 */
1405 template<typename _Tp>
1406 _If_sv<_Tp, basic_string&>
1407 append(const _Tp& __svt)
1408 {
1409 __sv_type __sv = __svt;
1410 return this->append(__sv.data(), __sv.size());
1411 }
1412
1413 /**
1414 * @brief Append a range of characters from a string_view.
1415 * @param __svt The object convertible to string_view to be appended
1416 * from.
1417 * @param __pos The position in the string_view to append from.
1418 * @param __n The number of characters to append from the string_view.
1419 * @return Reference to this string.
1420 */
1421 template<typename _Tp>
1422 _If_sv<_Tp, basic_string&>
1423 append(const _Tp& __svt, size_type __pos, size_type __n = npos)
1424 {
1425 __sv_type __sv = __svt;
1426 return append(__sv.data()
1427 + std::__sv_check(__sv.size(), __pos, "basic_string::append"),
1428 std::__sv_limit(__sv.size(), __pos, __n));
1429 }
1430#endif // C++17
1431
1432 /**
1433 * @brief Append a single character.
1434 * @param __c Character to append.
1435 */
1436 void
1437 push_back(_CharT __c)
1438 {
1439 const size_type __len = 1 + this->size();
1440 if (__len > this->capacity() || _M_rep()->_M_is_shared())
1441 this->reserve(__len);
1442 traits_type::assign(_M_data()[this->size()], __c);
1443 _M_rep()->_M_set_length_and_sharable(__len);
1444 }
1445
1446 /**
1447 * @brief Set value to contents of another string.
1448 * @param __str Source string to use.
1449 * @return Reference to this string.
1450 */
1451 basic_string&
1452 assign(const basic_string& __str);
1453
1454#if __cplusplus >= 201103L
1455 /**
1456 * @brief Set value to contents of another string.
1457 * @param __str Source string to use.
1458 * @return Reference to this string.
1459 *
1460 * This function sets this string to the exact contents of @a __str.
1461 * @a __str is a valid, but unspecified string.
1462 */
1463 basic_string&
1464 assign(basic_string&& __str)
1465 noexcept(allocator_traits<_Alloc>::is_always_equal::value)
1466 {
1467 this->swap(__str);
1468 return *this;
1469 }
1470#endif // C++11
1471
1472 /**
1473 * @brief Set value to a substring of a string.
1474 * @param __str The string to use.
1475 * @param __pos Index of the first character of str.
1476 * @param __n Number of characters to use.
1477 * @return Reference to this string.
1478 * @throw std::out_of_range if @a pos is not a valid index.
1479 *
1480 * This function sets this string to the substring of @a __str
1481 * consisting of @a __n characters at @a __pos. If @a __n is
1482 * is larger than the number of available characters in @a
1483 * __str, the remainder of @a __str is used.
1484 */
1485 basic_string&
1486 assign(const basic_string& __str, size_type __pos, size_type __n = npos)
1487 { return this->assign(__str._M_data()
1488 + __str._M_check(__pos, "basic_string::assign"),
1489 __str._M_limit(__pos, __n)); }
1490
1491 /**
1492 * @brief Set value to a C substring.
1493 * @param __s The C string to use.
1494 * @param __n Number of characters to use.
1495 * @return Reference to this string.
1496 *
1497 * This function sets the value of this string to the first @a __n
1498 * characters of @a __s. If @a __n is is larger than the number of
1499 * available characters in @a __s, the remainder of @a __s is used.
1500 */
1501 basic_string&
1502 assign(const _CharT* __s, size_type __n);
1503
1504 /**
1505 * @brief Set value to contents of a C string.
1506 * @param __s The C string to use.
1507 * @return Reference to this string.
1508 *
1509 * This function sets the value of this string to the value of @a __s.
1510 * The data is copied, so there is no dependence on @a __s once the
1511 * function returns.
1512 */
1513 basic_string&
1514 assign(const _CharT* __s)
1515 {
1516 __glibcxx_requires_string(__s);
1517 return this->assign(__s, traits_type::length(__s));
1518 }
1519
1520 /**
1521 * @brief Set value to multiple characters.
1522 * @param __n Length of the resulting string.
1523 * @param __c The character to use.
1524 * @return Reference to this string.
1525 *
1526 * This function sets the value of this string to @a __n copies of
1527 * character @a __c.
1528 */
1529 basic_string&
1530 assign(size_type __n, _CharT __c)
1531 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
1532
1533 /**
1534 * @brief Set value to a range of characters.
1535 * @param __first Iterator referencing the first character to append.
1536 * @param __last Iterator marking the end of the range.
1537 * @return Reference to this string.
1538 *
1539 * Sets value of string to characters in the range [__first,__last).
1540 */
1541 template<class _InputIterator>
1542 basic_string&
1543 assign(_InputIterator __first, _InputIterator __last)
1544 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
1545
1546#if __glibcxx_containers_ranges // C++ >= 23
1547 /**
1548 * @brief Set value to a range of characters.
1549 * @since C++23
1550 */
1551 template<__detail::__container_compatible_range<_CharT> _Rg>
1552 basic_string&
1553 assign_range(_Rg&& __rg)
1554 {
1555 basic_string __s(from_range, std::forward<_Rg>(__rg),
1556 get_allocator());
1557 assign(std::move(__s));
1558 return *this;
1559 }
1560#endif
1561
1562#if __cplusplus >= 201103L
1563 /**
1564 * @brief Set value to an initializer_list of characters.
1565 * @param __l The initializer_list of characters to assign.
1566 * @return Reference to this string.
1567 */
1568 basic_string&
1569 assign(initializer_list<_CharT> __l)
1570 { return this->assign(__l.begin(), __l.size()); }
1571#endif // C++11
1572
1573#ifdef __glibcxx_string_view // >= C++17
1574 /**
1575 * @brief Set value from a string_view.
1576 * @param __svt The source object convertible to string_view.
1577 * @return Reference to this string.
1578 */
1579 template<typename _Tp>
1580 _If_sv<_Tp, basic_string&>
1581 assign(const _Tp& __svt)
1582 {
1583 __sv_type __sv = __svt;
1584 return this->assign(__sv.data(), __sv.size());
1585 }
1586
1587 /**
1588 * @brief Set value from a range of characters in a string_view.
1589 * @param __svt The source object convertible to string_view.
1590 * @param __pos The position in the string_view to assign from.
1591 * @param __n The number of characters to assign.
1592 * @return Reference to this string.
1593 */
1594 template<typename _Tp>
1595 _If_sv<_Tp, basic_string&>
1596 assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
1597 {
1598 __sv_type __sv = __svt;
1599 return assign(__sv.data()
1600 + std::__sv_check(__sv.size(), __pos, "basic_string::assign"),
1601 std::__sv_limit(__sv.size(), __pos, __n));
1602 }
1603#endif // C++17
1604
1605 /**
1606 * @brief Insert multiple characters.
1607 * @param __p Iterator referencing location in string to insert at.
1608 * @param __n Number of characters to insert
1609 * @param __c The character to insert.
1610 * @throw std::length_error If new length exceeds @c max_size().
1611 *
1612 * Inserts @a __n copies of character @a __c starting at the
1613 * position referenced by iterator @a __p. If adding
1614 * characters causes the length to exceed max_size(),
1615 * length_error is thrown. The value of the string doesn't
1616 * change if an error is thrown.
1617 */
1618 void
1619 insert(iterator __p, size_type __n, _CharT __c)
1620 { this->replace(__p, __p, __n, __c); }
1621
1622 /**
1623 * @brief Insert a range of characters.
1624 * @param __p Iterator referencing location in string to insert at.
1625 * @param __beg Start of range.
1626 * @param __end End of range.
1627 * @throw std::length_error If new length exceeds @c max_size().
1628 *
1629 * Inserts characters in range [__beg,__end). If adding
1630 * characters causes the length to exceed max_size(),
1631 * length_error is thrown. The value of the string doesn't
1632 * change if an error is thrown.
1633 */
1634 template<class _InputIterator>
1635 void
1636 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
1637 { this->replace(__p, __p, __beg, __end); }
1638
1639#if __glibcxx_containers_ranges // C++ >= 23
1640 /**
1641 * @brief Insert a range into the string.
1642 * @since C++23
1643 */
1644 template<__detail::__container_compatible_range<_CharT> _Rg>
1645 iterator
1646 insert_range(const_iterator __p, _Rg&& __rg)
1647 {
1648 auto __pos = __p - cbegin();
1649
1650 if constexpr (ranges::forward_range<_Rg>)
1651 if (ranges::empty(__rg))
1652 return begin() + __pos;
1653
1654 if (__p == cend())
1655 append_range(std::forward<_Rg>(__rg));
1656 else
1657 {
1658 basic_string __s(from_range, std::forward<_Rg>(__rg),
1659 get_allocator());
1660 insert(__pos, __s);
1661 }
1662 return begin() + __pos;
1663 }
1664#endif
1665
1666#if __cplusplus >= 201103L
1667 /**
1668 * @brief Insert an initializer_list of characters.
1669 * @param __p Iterator referencing location in string to insert at.
1670 * @param __l The initializer_list of characters to insert.
1671 * @throw std::length_error If new length exceeds @c max_size().
1672 */
1673 void
1674 insert(iterator __p, initializer_list<_CharT> __l)
1675 {
1676 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1677 this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
1678 }
1679#endif // C++11
1680
1681 /**
1682 * @brief Insert value of a string.
1683 * @param __pos1 Position in string to insert at.
1684 * @param __str The string to insert.
1685 * @return Reference to this string.
1686 * @throw std::length_error If new length exceeds @c max_size().
1687 *
1688 * Inserts value of @a __str starting at @a __pos1. If adding
1689 * characters causes the length to exceed max_size(),
1690 * length_error is thrown. The value of the string doesn't
1691 * change if an error is thrown.
1692 */
1693 basic_string&
1694 insert(size_type __pos1, const basic_string& __str)
1695 { return this->insert(__pos1, __str, size_type(0), __str.size()); }
1696
1697 /**
1698 * @brief Insert a substring.
1699 * @param __pos1 Position in string to insert at.
1700 * @param __str The string to insert.
1701 * @param __pos2 Start of characters in str to insert.
1702 * @param __n Number of characters to insert.
1703 * @return Reference to this string.
1704 * @throw std::length_error If new length exceeds @c max_size().
1705 * @throw std::out_of_range If @a pos1 > size() or
1706 * @a __pos2 > @a str.size().
1707 *
1708 * Starting at @a pos1, insert @a __n character of @a __str
1709 * beginning with @a __pos2. If adding characters causes the
1710 * length to exceed max_size(), length_error is thrown. If @a
1711 * __pos1 is beyond the end of this string or @a __pos2 is
1712 * beyond the end of @a __str, out_of_range is thrown. The
1713 * value of the string doesn't change if an error is thrown.
1714 */
1715 basic_string&
1716 insert(size_type __pos1, const basic_string& __str,
1717 size_type __pos2, size_type __n = npos)
1718 { return this->insert(__pos1, __str._M_data()
1719 + __str._M_check(__pos2, "basic_string::insert"),
1720 __str._M_limit(__pos2, __n)); }
1721
1722 /**
1723 * @brief Insert a C substring.
1724 * @param __pos Position in string to insert at.
1725 * @param __s The C string to insert.
1726 * @param __n The number of characters to insert.
1727 * @return Reference to this string.
1728 * @throw std::length_error If new length exceeds @c max_size().
1729 * @throw std::out_of_range If @a __pos is beyond the end of this
1730 * string.
1731 *
1732 * Inserts the first @a __n characters of @a __s starting at @a
1733 * __pos. If adding characters causes the length to exceed
1734 * max_size(), length_error is thrown. If @a __pos is beyond
1735 * end(), out_of_range is thrown. The value of the string
1736 * doesn't change if an error is thrown.
1737 */
1738 basic_string&
1739 insert(size_type __pos, const _CharT* __s, size_type __n);
1740
1741 /**
1742 * @brief Insert a C string.
1743 * @param __pos Position in string to insert at.
1744 * @param __s The C string to insert.
1745 * @return Reference to this string.
1746 * @throw std::length_error If new length exceeds @c max_size().
1747 * @throw std::out_of_range If @a pos is beyond the end of this
1748 * string.
1749 *
1750 * Inserts the first @a n characters of @a __s starting at @a __pos. If
1751 * adding characters causes the length to exceed max_size(),
1752 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
1753 * thrown. The value of the string doesn't change if an error is
1754 * thrown.
1755 */
1756 basic_string&
1757 insert(size_type __pos, const _CharT* __s)
1758 {
1759 __glibcxx_requires_string(__s);
1760 return this->insert(__pos, __s, traits_type::length(__s));
1761 }
1762
1763 /**
1764 * @brief Insert multiple characters.
1765 * @param __pos Index in string to insert at.
1766 * @param __n Number of characters to insert
1767 * @param __c The character to insert.
1768 * @return Reference to this string.
1769 * @throw std::length_error If new length exceeds @c max_size().
1770 * @throw std::out_of_range If @a __pos is beyond the end of this
1771 * string.
1772 *
1773 * Inserts @a __n copies of character @a __c starting at index
1774 * @a __pos. If adding characters causes the length to exceed
1775 * max_size(), length_error is thrown. If @a __pos > length(),
1776 * out_of_range is thrown. The value of the string doesn't
1777 * change if an error is thrown.
1778 */
1779 basic_string&
1780 insert(size_type __pos, size_type __n, _CharT __c)
1781 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1782 size_type(0), __n, __c); }
1783
1784 /**
1785 * @brief Insert one character.
1786 * @param __p Iterator referencing position in string to insert at.
1787 * @param __c The character to insert.
1788 * @return Iterator referencing newly inserted char.
1789 * @throw std::length_error If new length exceeds @c max_size().
1790 *
1791 * Inserts character @a __c at position referenced by @a __p.
1792 * If adding character causes the length to exceed max_size(),
1793 * length_error is thrown. If @a __p is beyond end of string,
1794 * out_of_range is thrown. The value of the string doesn't
1795 * change if an error is thrown.
1796 */
1797 iterator
1798 insert(iterator __p, _CharT __c)
1799 {
1800 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1801 const size_type __pos = __p - _M_ibegin();
1802 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1803 _M_rep()->_M_set_leaked();
1804 return iterator(_M_data() + __pos);
1805 }
1806
1807#ifdef __glibcxx_string_view // >= C++17
1808 /**
1809 * @brief Insert a string_view.
1810 * @param __pos Position in string to insert at.
1811 * @param __svt The object convertible to string_view to insert.
1812 * @return Reference to this string.
1813 */
1814 template<typename _Tp>
1815 _If_sv<_Tp, basic_string&>
1816 insert(size_type __pos, const _Tp& __svt)
1817 {
1818 __sv_type __sv = __svt;
1819 return this->insert(__pos, __sv.data(), __sv.size());
1820 }
1821
1822 /**
1823 * @brief Insert a string_view.
1824 * @param __pos1 Position in string to insert at.
1825 * @param __svt The object convertible to string_view to insert from.
1826 * @param __pos2 Position in string_view to insert from.
1827 * @param __n The number of characters to insert.
1828 * @return Reference to this string.
1829 */
1830 template<typename _Tp>
1831 _If_sv<_Tp, basic_string&>
1832 insert(size_type __pos1, const _Tp& __svt,
1833 size_type __pos2, size_type __n = npos)
1834 {
1835 __sv_type __sv = __svt;
1836 return this->replace(__pos1, size_type(0), __sv.data()
1837 + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"),
1838 std::__sv_limit(__sv.size(), __pos2, __n));
1839 }
1840#endif // C++17
1841
1842 /**
1843 * @brief Remove characters.
1844 * @param __pos Index of first character to remove (default 0).
1845 * @param __n Number of characters to remove (default remainder).
1846 * @return Reference to this string.
1847 * @throw std::out_of_range If @a pos is beyond the end of this
1848 * string.
1849 *
1850 * Removes @a __n characters from this string starting at @a
1851 * __pos. The length of the string is reduced by @a __n. If
1852 * there are < @a __n characters to remove, the remainder of
1853 * the string is truncated. If @a __p is beyond end of string,
1854 * out_of_range is thrown. The value of the string doesn't
1855 * change if an error is thrown.
1856 */
1857 basic_string&
1858 erase(size_type __pos = 0, size_type __n = npos)
1859 {
1860 _M_mutate(_M_check(__pos, "basic_string::erase"),
1861 _M_limit(__pos, __n), size_type(0));
1862 return *this;
1863 }
1864
1865 /**
1866 * @brief Remove one character.
1867 * @param __position Iterator referencing the character to remove.
1868 * @return iterator referencing same location after removal.
1869 *
1870 * Removes the character at @a __position from this string. The value
1871 * of the string doesn't change if an error is thrown.
1872 */
1873 iterator
1874 erase(iterator __position)
1875 {
1876 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1877 && __position < _M_iend());
1878 const size_type __pos = __position - _M_ibegin();
1879 _M_mutate(__pos, size_type(1), size_type(0));
1880 _M_rep()->_M_set_leaked();
1881 return iterator(_M_data() + __pos);
1882 }
1883
1884 /**
1885 * @brief Remove a range of characters.
1886 * @param __first Iterator referencing the first character to remove.
1887 * @param __last Iterator referencing the end of the range.
1888 * @return Iterator referencing location of first after removal.
1889 *
1890 * Removes the characters in the range [first,last) from this string.
1891 * The value of the string doesn't change if an error is thrown.
1892 */
1893 iterator
1894 erase(iterator __first, iterator __last);
1895
1896#if __cplusplus >= 201103L
1897 /**
1898 * @brief Remove the last character.
1899 *
1900 * The string must be non-empty.
1901 */
1902 void
1903 pop_back() // FIXME C++11: should be noexcept.
1904 {
1905 __glibcxx_assert(!empty());
1906 erase(size() - 1, 1);
1907 }
1908#endif // C++11
1909
1910 /**
1911 * @brief Replace characters with value from another string.
1912 * @param __pos Index of first character to replace.
1913 * @param __n Number of characters to be replaced.
1914 * @param __str String to insert.
1915 * @return Reference to this string.
1916 * @throw std::out_of_range If @a pos is beyond the end of this
1917 * string.
1918 * @throw std::length_error If new length exceeds @c max_size().
1919 *
1920 * Removes the characters in the range [__pos,__pos+__n) from
1921 * this string. In place, the value of @a __str is inserted.
1922 * If @a __pos is beyond end of string, out_of_range is thrown.
1923 * If the length of the result exceeds max_size(), length_error
1924 * is thrown. The value of the string doesn't change if an
1925 * error is thrown.
1926 */
1927 basic_string&
1928 replace(size_type __pos, size_type __n, const basic_string& __str)
1929 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1930
1931 /**
1932 * @brief Replace characters with value from another string.
1933 * @param __pos1 Index of first character to replace.
1934 * @param __n1 Number of characters to be replaced.
1935 * @param __str String to insert.
1936 * @param __pos2 Index of first character of str to use.
1937 * @param __n2 Number of characters from str to use.
1938 * @return Reference to this string.
1939 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
1940 * __str.size().
1941 * @throw std::length_error If new length exceeds @c max_size().
1942 *
1943 * Removes the characters in the range [__pos1,__pos1 + n) from this
1944 * string. In place, the value of @a __str is inserted. If @a __pos is
1945 * beyond end of string, out_of_range is thrown. If the length of the
1946 * result exceeds max_size(), length_error is thrown. The value of the
1947 * string doesn't change if an error is thrown.
1948 */
1949 basic_string&
1950 replace(size_type __pos1, size_type __n1, const basic_string& __str,
1951 size_type __pos2, size_type __n2 = npos)
1952 { return this->replace(__pos1, __n1, __str._M_data()
1953 + __str._M_check(__pos2, "basic_string::replace"),
1954 __str._M_limit(__pos2, __n2)); }
1955
1956 /**
1957 * @brief Replace characters with value of a C substring.
1958 * @param __pos Index of first character to replace.
1959 * @param __n1 Number of characters to be replaced.
1960 * @param __s C string to insert.
1961 * @param __n2 Number of characters from @a s to use.
1962 * @return Reference to this string.
1963 * @throw std::out_of_range If @a pos1 > size().
1964 * @throw std::length_error If new length exceeds @c max_size().
1965 *
1966 * Removes the characters in the range [__pos,__pos + __n1)
1967 * from this string. In place, the first @a __n2 characters of
1968 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
1969 * @a __pos is beyond end of string, out_of_range is thrown. If
1970 * the length of result exceeds max_size(), length_error is
1971 * thrown. The value of the string doesn't change if an error
1972 * is thrown.
1973 */
1974 basic_string&
1975 replace(size_type __pos, size_type __n1, const _CharT* __s,
1976 size_type __n2);
1977
1978 /**
1979 * @brief Replace characters with value of a C string.
1980 * @param __pos Index of first character to replace.
1981 * @param __n1 Number of characters to be replaced.
1982 * @param __s C string to insert.
1983 * @return Reference to this string.
1984 * @throw std::out_of_range If @a pos > size().
1985 * @throw std::length_error If new length exceeds @c max_size().
1986 *
1987 * Removes the characters in the range [__pos,__pos + __n1)
1988 * from this string. In place, the characters of @a __s are
1989 * inserted. If @a __pos is beyond end of string, out_of_range
1990 * is thrown. If the length of result exceeds max_size(),
1991 * length_error is thrown. The value of the string doesn't
1992 * change if an error is thrown.
1993 */
1994 basic_string&
1995 replace(size_type __pos, size_type __n1, const _CharT* __s)
1996 {
1997 __glibcxx_requires_string(__s);
1998 return this->replace(__pos, __n1, __s, traits_type::length(__s));
1999 }
2000
2001 /**
2002 * @brief Replace characters with multiple characters.
2003 * @param __pos Index of first character to replace.
2004 * @param __n1 Number of characters to be replaced.
2005 * @param __n2 Number of characters to insert.
2006 * @param __c Character to insert.
2007 * @return Reference to this string.
2008 * @throw std::out_of_range If @a __pos > size().
2009 * @throw std::length_error If new length exceeds @c max_size().
2010 *
2011 * Removes the characters in the range [pos,pos + n1) from this
2012 * string. In place, @a __n2 copies of @a __c are inserted.
2013 * If @a __pos is beyond end of string, out_of_range is thrown.
2014 * If the length of result exceeds max_size(), length_error is
2015 * thrown. The value of the string doesn't change if an error
2016 * is thrown.
2017 */
2018 basic_string&
2019 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
2020 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
2021 _M_limit(__pos, __n1), __n2, __c); }
2022
2023 /**
2024 * @brief Replace range of characters with string.
2025 * @param __i1 Iterator referencing start of range to replace.
2026 * @param __i2 Iterator referencing end of range to replace.
2027 * @param __str String value to insert.
2028 * @return Reference to this string.
2029 * @throw std::length_error If new length exceeds @c max_size().
2030 *
2031 * Removes the characters in the range [__i1,__i2). In place,
2032 * the value of @a __str is inserted. If the length of result
2033 * exceeds max_size(), length_error is thrown. The value of
2034 * the string doesn't change if an error is thrown.
2035 */
2036 basic_string&
2037 replace(iterator __i1, iterator __i2, const basic_string& __str)
2038 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
2039
2040 /**
2041 * @brief Replace range of characters with C substring.
2042 * @param __i1 Iterator referencing start of range to replace.
2043 * @param __i2 Iterator referencing end of range to replace.
2044 * @param __s C string value to insert.
2045 * @param __n Number of characters from s to insert.
2046 * @return Reference to this string.
2047 * @throw std::length_error If new length exceeds @c max_size().
2048 *
2049 * Removes the characters in the range [__i1,__i2). In place,
2050 * the first @a __n characters of @a __s are inserted. If the
2051 * length of result exceeds max_size(), length_error is thrown.
2052 * The value of the string doesn't change if an error is
2053 * thrown.
2054 */
2055 basic_string&
2056 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
2057 {
2058 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2059 && __i2 <= _M_iend());
2060 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
2061 }
2062
2063 /**
2064 * @brief Replace range of characters with C string.
2065 * @param __i1 Iterator referencing start of range to replace.
2066 * @param __i2 Iterator referencing end of range to replace.
2067 * @param __s C string value to insert.
2068 * @return Reference to this string.
2069 * @throw std::length_error If new length exceeds @c max_size().
2070 *
2071 * Removes the characters in the range [__i1,__i2). In place,
2072 * the characters of @a __s are inserted. If the length of
2073 * result exceeds max_size(), length_error is thrown. The
2074 * value of the string doesn't change if an error is thrown.
2075 */
2076 basic_string&
2077 replace(iterator __i1, iterator __i2, const _CharT* __s)
2078 {
2079 __glibcxx_requires_string(__s);
2080 return this->replace(__i1, __i2, __s, traits_type::length(__s));
2081 }
2082
2083 /**
2084 * @brief Replace range of characters with multiple characters
2085 * @param __i1 Iterator referencing start of range to replace.
2086 * @param __i2 Iterator referencing end of range to replace.
2087 * @param __n Number of characters to insert.
2088 * @param __c Character to insert.
2089 * @return Reference to this string.
2090 * @throw std::length_error If new length exceeds @c max_size().
2091 *
2092 * Removes the characters in the range [__i1,__i2). In place,
2093 * @a __n copies of @a __c are inserted. If the length of
2094 * result exceeds max_size(), length_error is thrown. The
2095 * value of the string doesn't change if an error is thrown.
2096 */
2097 basic_string&
2098 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
2099 {
2100 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2101 && __i2 <= _M_iend());
2102 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
2103 }
2104
2105 /**
2106 * @brief Replace range of characters with range.
2107 * @param __i1 Iterator referencing start of range to replace.
2108 * @param __i2 Iterator referencing end of range to replace.
2109 * @param __k1 Iterator referencing start of range to insert.
2110 * @param __k2 Iterator referencing end of range to insert.
2111 * @return Reference to this string.
2112 * @throw std::length_error If new length exceeds @c max_size().
2113 *
2114 * Removes the characters in the range [__i1,__i2). In place,
2115 * characters in the range [__k1,__k2) are inserted. If the
2116 * length of result exceeds max_size(), length_error is thrown.
2117 * The value of the string doesn't change if an error is
2118 * thrown.
2119 */
2120 template<class _InputIterator>
2121 basic_string&
2122 replace(iterator __i1, iterator __i2,
2123 _InputIterator __k1, _InputIterator __k2)
2124 {
2125 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2126 && __i2 <= _M_iend());
2127 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
2128 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
2129 }
2130
2131 // Specializations for the common case of pointer and iterator:
2132 // useful to avoid the overhead of temporary buffering in _M_replace.
2133 basic_string&
2134 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
2135 {
2136 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2137 && __i2 <= _M_iend());
2138 __glibcxx_requires_valid_range(__k1, __k2);
2139 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
2140 __k1, __k2 - __k1);
2141 }
2142
2143 basic_string&
2144 replace(iterator __i1, iterator __i2,
2145 const _CharT* __k1, const _CharT* __k2)
2146 {
2147 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2148 && __i2 <= _M_iend());
2149 __glibcxx_requires_valid_range(__k1, __k2);
2150 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
2151 __k1, __k2 - __k1);
2152 }
2153
2154 basic_string&
2155 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
2156 {
2157 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2158 && __i2 <= _M_iend());
2159 __glibcxx_requires_valid_range(__k1, __k2);
2160 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
2161 __k1.base(), __k2 - __k1);
2162 }
2163
2164 basic_string&
2165 replace(iterator __i1, iterator __i2,
2166 const_iterator __k1, const_iterator __k2)
2167 {
2168 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2169 && __i2 <= _M_iend());
2170 __glibcxx_requires_valid_range(__k1, __k2);
2171 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
2172 __k1.base(), __k2 - __k1);
2173 }
2174
2175#if __glibcxx_containers_ranges // C++ >= 23
2176 /**
2177 * @brief Replace part of the string with a range.
2178 * @since C++23
2179 */
2180 template<__detail::__container_compatible_range<_CharT> _Rg>
2181 basic_string&
2182 replace_with_range(const_iterator __i1, const_iterator __i2, _Rg&& __rg)
2183 {
2184 if (__i1 == cend())
2185 append_range(std::forward<_Rg>(__rg));
2186 else
2187 {
2188 basic_string __s(from_range, std::forward<_Rg>(__rg),
2189 get_allocator());
2190 replace(__i1 - cbegin(), __i2 - __i1, __s);
2191 }
2192 return *this;
2193 }
2194#endif
2195
2196#if __cplusplus >= 201103L
2197 /**
2198 * @brief Replace range of characters with initializer_list.
2199 * @param __i1 Iterator referencing start of range to replace.
2200 * @param __i2 Iterator referencing end of range to replace.
2201 * @param __l The initializer_list of characters to insert.
2202 * @return Reference to this string.
2203 * @throw std::length_error If new length exceeds @c max_size().
2204 *
2205 * Removes the characters in the range [__i1,__i2). In place,
2206 * characters in the range [__k1,__k2) are inserted. If the
2207 * length of result exceeds max_size(), length_error is thrown.
2208 * The value of the string doesn't change if an error is
2209 * thrown.
2210 */
2211 basic_string& replace(iterator __i1, iterator __i2,
2212 initializer_list<_CharT> __l)
2213 { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
2214#endif // C++11
2215
2216#ifdef __glibcxx_string_view // >= C++17
2217 /**
2218 * @brief Replace range of characters with string_view.
2219 * @param __pos The position to replace at.
2220 * @param __n The number of characters to replace.
2221 * @param __svt The object convertible to string_view to insert.
2222 * @return Reference to this string.
2223 */
2224 template<typename _Tp>
2225 _If_sv<_Tp, basic_string&>
2226 replace(size_type __pos, size_type __n, const _Tp& __svt)
2227 {
2228 __sv_type __sv = __svt;
2229 return this->replace(__pos, __n, __sv.data(), __sv.size());
2230 }
2231
2232 /**
2233 * @brief Replace range of characters with string_view.
2234 * @param __pos1 The position to replace at.
2235 * @param __n1 The number of characters to replace.
2236 * @param __svt The object convertible to string_view to insert from.
2237 * @param __pos2 The position in the string_view to insert from.
2238 * @param __n2 The number of characters to insert.
2239 * @return Reference to this string.
2240 */
2241 template<typename _Tp>
2242 _If_sv<_Tp, basic_string&>
2243 replace(size_type __pos1, size_type __n1, const _Tp& __svt,
2244 size_type __pos2, size_type __n2 = npos)
2245 {
2246 __sv_type __sv = __svt;
2247 return this->replace(__pos1, __n1,
2248 __sv.data()
2249 + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"),
2250 std::__sv_limit(__sv.size(), __pos2, __n2));
2251 }
2252
2253 /**
2254 * @brief Replace range of characters with string_view.
2255 * @param __i1 An iterator referencing the start position
2256 * to replace at.
2257 * @param __i2 An iterator referencing the end position
2258 * for the replace.
2259 * @param __svt The object convertible to string_view to insert from.
2260 * @return Reference to this string.
2261 */
2262 template<typename _Tp>
2263 _If_sv<_Tp, basic_string&>
2264 replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
2265 {
2266 __sv_type __sv = __svt;
2267 return this->replace(__i1 - begin(), __i2 - __i1, __sv);
2268 }
2269#endif // C++17
2270
2271 private:
2272 template<class _Integer>
2273 basic_string&
2274 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
2275 _Integer __val, __true_type)
2276 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
2277
2278 template<class _InputIterator>
2279 basic_string&
2280 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
2281 _InputIterator __k2, __false_type);
2282
2283 basic_string&
2284 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
2285 _CharT __c);
2286
2287 basic_string&
2288 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
2289 size_type __n2);
2290
2291 // _S_construct_aux is used to implement the 21.3.1 para 15 which
2292 // requires special behaviour if _InIter is an integral type
2293 template<class _InIterator>
2294 static _CharT*
2295 _S_construct_aux(_InIterator __beg, _InIterator __end,
2296 const _Alloc& __a, __false_type)
2297 {
2298 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
2299 return _S_construct(__beg, __end, __a, _Tag());
2300 }
2301
2302 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2303 // 438. Ambiguity in the "do the right thing" clause
2304 template<class _Integer>
2305 static _CharT*
2306 _S_construct_aux(_Integer __beg, _Integer __end,
2307 const _Alloc& __a, __true_type)
2308 { return _S_construct_aux_2(static_cast<size_type>(__beg),
2309 __end, __a); }
2310
2311 static _CharT*
2312 _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
2313 { return _S_construct(__req, __c, __a); }
2314
2315 template<class _InIterator>
2316 static _CharT*
2317 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
2318 {
2319 typedef typename std::__is_integer<_InIterator>::__type _Integral;
2320 return _S_construct_aux(__beg, __end, __a, _Integral());
2321 }
2322
2323 // For Input Iterators, used in istreambuf_iterators, etc.
2324 template<class _InIterator>
2325 static _CharT*
2326 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
2327 input_iterator_tag);
2328
2329 // For forward_iterators up to random_access_iterators, used for
2330 // string::iterator, _CharT*, etc.
2331 template<class _FwdIterator>
2332 static _CharT*
2333 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
2334 forward_iterator_tag);
2335
2336 static _CharT*
2337 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
2338
2339 public:
2340
2341 /**
2342 * @brief Copy substring into C string.
2343 * @param __s C string to copy value into.
2344 * @param __n Number of characters to copy.
2345 * @param __pos Index of first character to copy.
2346 * @return Number of characters actually copied
2347 * @throw std::out_of_range If __pos > size().
2348 *
2349 * Copies up to @a __n characters starting at @a __pos into the
2350 * C string @a __s. If @a __pos is %greater than size(),
2351 * out_of_range is thrown.
2352 */
2353 size_type
2354 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
2355
2356 /**
2357 * @brief Swap contents with another string.
2358 * @param __s String to swap with.
2359 *
2360 * Exchanges the contents of this string with that of @a __s in constant
2361 * time.
2362 */
2363 void
2364 swap(basic_string& __s)
2365 _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value);
2366
2367 // String operations:
2368 /**
2369 * @brief Return const pointer to null-terminated contents.
2370 *
2371 * This is a handle to internal data. Do not modify or dire things may
2372 * happen.
2373 */
2374 const _CharT*
2375 c_str() const _GLIBCXX_NOEXCEPT
2376 { return _M_data(); }
2377
2378 /**
2379 * @brief Return const pointer to contents.
2380 *
2381 * This is a pointer to internal data. It is undefined to modify
2382 * the contents through the returned pointer. To get a pointer that
2383 * allows modifying the contents use @c &str[0] instead,
2384 * (or in C++17 the non-const @c str.data() overload).
2385 */
2386 const _CharT*
2387 data() const _GLIBCXX_NOEXCEPT
2388 { return _M_data(); }
2389
2390#if __cplusplus >= 201703L
2391 /**
2392 * @brief Return non-const pointer to contents.
2393 *
2394 * This is a pointer to the character sequence held by the string.
2395 * Modifying the characters in the sequence is allowed.
2396 *
2397 * The standard requires this function to be `noexcept` but for the
2398 * Copy-On-Write string implementation it can throw. This function
2399 * allows modifying the string contents directly, which means we
2400 * must copy-on-write to unshare it, which requires allocating memory.
2401 */
2402 _CharT*
2403 data() noexcept(false)
2404 {
2405 _M_leak();
2406 return _M_data();
2407 }
2408#endif
2409
2410 /**
2411 * @brief Return copy of allocator used to construct this string.
2412 */
2413 allocator_type
2414 get_allocator() const _GLIBCXX_NOEXCEPT
2415 { return _M_dataplus; }
2416
2417 /**
2418 * @brief Find position of a C substring.
2419 * @param __s C string to locate.
2420 * @param __pos Index of character to search from.
2421 * @param __n Number of characters from @a s to search for.
2422 * @return Index of start of first occurrence.
2423 *
2424 * Starting from @a __pos, searches forward for the first @a
2425 * __n characters in @a __s within this string. If found,
2426 * returns the index where it begins. If not found, returns
2427 * npos.
2428 */
2429 size_type
2430 find(const _CharT* __s, size_type __pos, size_type __n) const
2431 _GLIBCXX_NOEXCEPT;
2432
2433 /**
2434 * @brief Find position of a string.
2435 * @param __str String to locate.
2436 * @param __pos Index of character to search from (default 0).
2437 * @return Index of start of first occurrence.
2438 *
2439 * Starting from @a __pos, searches forward for value of @a __str within
2440 * this string. If found, returns the index where it begins. If not
2441 * found, returns npos.
2442 */
2443 size_type
2444 find(const basic_string& __str, size_type __pos = 0) const
2445 _GLIBCXX_NOEXCEPT
2446 { return this->find(__str.data(), __pos, __str.size()); }
2447
2448 /**
2449 * @brief Find position of a C string.
2450 * @param __s C string to locate.
2451 * @param __pos Index of character to search from (default 0).
2452 * @return Index of start of first occurrence.
2453 *
2454 * Starting from @a __pos, searches forward for the value of @a
2455 * __s within this string. If found, returns the index where
2456 * it begins. If not found, returns npos.
2457 */
2458 size_type
2459 find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
2460 {
2461 __glibcxx_requires_string(__s);
2462 return this->find(__s, __pos, traits_type::length(__s));
2463 }
2464
2465 /**
2466 * @brief Find position of a character.
2467 * @param __c Character to locate.
2468 * @param __pos Index of character to search from (default 0).
2469 * @return Index of first occurrence.
2470 *
2471 * Starting from @a __pos, searches forward for @a __c within
2472 * this string. If found, returns the index where it was
2473 * found. If not found, returns npos.
2474 */
2475 size_type
2476 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
2477
2478#ifdef __glibcxx_string_view // >= C++17
2479 /**
2480 * @brief Find position of a string_view.
2481 * @param __svt The object convertible to string_view to locate.
2482 * @param __pos Index of character to search from (default 0).
2483 * @return Index of start of first occurrence.
2484 */
2485 template<typename _Tp>
2486 _If_sv<_Tp, size_type>
2487 find(const _Tp& __svt, size_type __pos = 0) const
2488 noexcept(is_same<_Tp, __sv_type>::value)
2489 {
2490 __sv_type __sv = __svt;
2491 return this->find(__sv.data(), __pos, __sv.size());
2492 }
2493#endif // C++17
2494
2495 /**
2496 * @brief Find last position of a string.
2497 * @param __str String to locate.
2498 * @param __pos Index of character to search back from (default end).
2499 * @return Index of start of last occurrence.
2500 *
2501 * Starting from @a __pos, searches backward for value of @a
2502 * __str within this string. If found, returns the index where
2503 * it begins. If not found, returns npos.
2504 */
2505 size_type
2506 rfind(const basic_string& __str, size_type __pos = npos) const
2507 _GLIBCXX_NOEXCEPT
2508 { return this->rfind(__str.data(), __pos, __str.size()); }
2509
2510 /**
2511 * @brief Find last position of a C substring.
2512 * @param __s C string to locate.
2513 * @param __pos Index of character to search back from.
2514 * @param __n Number of characters from s to search for.
2515 * @return Index of start of last occurrence.
2516 *
2517 * Starting from @a __pos, searches backward for the first @a
2518 * __n characters in @a __s within this string. If found,
2519 * returns the index where it begins. If not found, returns
2520 * npos.
2521 */
2522 size_type
2523 rfind(const _CharT* __s, size_type __pos, size_type __n) const
2524 _GLIBCXX_NOEXCEPT;
2525
2526 /**
2527 * @brief Find last position of a C string.
2528 * @param __s C string to locate.
2529 * @param __pos Index of character to start search at (default end).
2530 * @return Index of start of last occurrence.
2531 *
2532 * Starting from @a __pos, searches backward for the value of
2533 * @a __s within this string. If found, returns the index
2534 * where it begins. If not found, returns npos.
2535 */
2536 size_type
2537 rfind(const _CharT* __s, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
2538 {
2539 __glibcxx_requires_string(__s);
2540 return this->rfind(__s, __pos, traits_type::length(__s));
2541 }
2542
2543 /**
2544 * @brief Find last position of a character.
2545 * @param __c Character to locate.
2546 * @param __pos Index of character to search back from (default end).
2547 * @return Index of last occurrence.
2548 *
2549 * Starting from @a __pos, searches backward for @a __c within
2550 * this string. If found, returns the index where it was
2551 * found. If not found, returns npos.
2552 */
2553 size_type
2554 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
2555
2556#ifdef __glibcxx_string_view // >= C++17
2557 /**
2558 * @brief Find last position of a string_view.
2559 * @param __svt The object convertible to string_view to locate.
2560 * @param __pos Index of character to search back from (default end).
2561 * @return Index of start of last occurrence.
2562 */
2563 template<typename _Tp>
2564 _If_sv<_Tp, size_type>
2565 rfind(const _Tp& __svt, size_type __pos = npos) const
2566 noexcept(is_same<_Tp, __sv_type>::value)
2567 {
2568 __sv_type __sv = __svt;
2569 return this->rfind(__sv.data(), __pos, __sv.size());
2570 }
2571#endif // C++17
2572
2573 /**
2574 * @brief Find position of a character of string.
2575 * @param __str String containing characters to locate.
2576 * @param __pos Index of character to search from (default 0).
2577 * @return Index of first occurrence.
2578 *
2579 * Starting from @a __pos, searches forward for one of the
2580 * characters of @a __str within this string. If found,
2581 * returns the index where it was found. If not found, returns
2582 * npos.
2583 */
2584 size_type
2585 find_first_of(const basic_string& __str, size_type __pos = 0) const
2586 _GLIBCXX_NOEXCEPT
2587 { return this->find_first_of(__str.data(), __pos, __str.size()); }
2588
2589 /**
2590 * @brief Find position of a character of C substring.
2591 * @param __s String containing characters to locate.
2592 * @param __pos Index of character to search from.
2593 * @param __n Number of characters from s to search for.
2594 * @return Index of first occurrence.
2595 *
2596 * Starting from @a __pos, searches forward for one of the
2597 * first @a __n characters of @a __s within this string. If
2598 * found, returns the index where it was found. If not found,
2599 * returns npos.
2600 */
2601 size_type
2602 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
2603 _GLIBCXX_NOEXCEPT;
2604
2605 /**
2606 * @brief Find position of a character of C string.
2607 * @param __s String containing characters to locate.
2608 * @param __pos Index of character to search from (default 0).
2609 * @return Index of first occurrence.
2610 *
2611 * Starting from @a __pos, searches forward for one of the
2612 * characters of @a __s within this string. If found, returns
2613 * the index where it was found. If not found, returns npos.
2614 */
2615 size_type
2616 find_first_of(const _CharT* __s, size_type __pos = 0) const
2617 _GLIBCXX_NOEXCEPT
2618 {
2619 __glibcxx_requires_string(__s);
2620 return this->find_first_of(__s, __pos, traits_type::length(__s));
2621 }
2622
2623 /**
2624 * @brief Find position of a character.
2625 * @param __c Character to locate.
2626 * @param __pos Index of character to search from (default 0).
2627 * @return Index of first occurrence.
2628 *
2629 * Starting from @a __pos, searches forward for the character
2630 * @a __c within this string. If found, returns the index
2631 * where it was found. If not found, returns npos.
2632 *
2633 * Note: equivalent to find(__c, __pos).
2634 */
2635 size_type
2636 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
2637 { return this->find(__c, __pos); }
2638
2639#ifdef __glibcxx_string_view // >= C++17
2640 /**
2641 * @brief Find position of a character of a string_view.
2642 * @param __svt An object convertible to string_view containing
2643 * characters to locate.
2644 * @param __pos Index of character to search from (default 0).
2645 * @return Index of first occurrence.
2646 */
2647 template<typename _Tp>
2648 _If_sv<_Tp, size_type>
2649 find_first_of(const _Tp& __svt, size_type __pos = 0) const
2650 noexcept(is_same<_Tp, __sv_type>::value)
2651 {
2652 __sv_type __sv = __svt;
2653 return this->find_first_of(__sv.data(), __pos, __sv.size());
2654 }
2655#endif // C++17
2656
2657 /**
2658 * @brief Find last position of a character of string.
2659 * @param __str String containing characters to locate.
2660 * @param __pos Index of character to search back from (default end).
2661 * @return Index of last occurrence.
2662 *
2663 * Starting from @a __pos, searches backward for one of the
2664 * characters of @a __str within this string. If found,
2665 * returns the index where it was found. If not found, returns
2666 * npos.
2667 */
2668 size_type
2669 find_last_of(const basic_string& __str, size_type __pos = npos) const
2670 _GLIBCXX_NOEXCEPT
2671 { return this->find_last_of(__str.data(), __pos, __str.size()); }
2672
2673 /**
2674 * @brief Find last position of a character of C substring.
2675 * @param __s C string containing characters to locate.
2676 * @param __pos Index of character to search back from.
2677 * @param __n Number of characters from s to search for.
2678 * @return Index of last occurrence.
2679 *
2680 * Starting from @a __pos, searches backward for one of the
2681 * first @a __n characters of @a __s within this string. If
2682 * found, returns the index where it was found. If not found,
2683 * returns npos.
2684 */
2685 size_type
2686 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
2687 _GLIBCXX_NOEXCEPT;
2688
2689 /**
2690 * @brief Find last position of a character of C string.
2691 * @param __s C string containing characters to locate.
2692 * @param __pos Index of character to search back from (default end).
2693 * @return Index of last occurrence.
2694 *
2695 * Starting from @a __pos, searches backward for one of the
2696 * characters of @a __s within this string. If found, returns
2697 * the index where it was found. If not found, returns npos.
2698 */
2699 size_type
2700 find_last_of(const _CharT* __s, size_type __pos = npos) const
2701 _GLIBCXX_NOEXCEPT
2702 {
2703 __glibcxx_requires_string(__s);
2704 return this->find_last_of(__s, __pos, traits_type::length(__s));
2705 }
2706
2707 /**
2708 * @brief Find last position of a character.
2709 * @param __c Character to locate.
2710 * @param __pos Index of character to search back from (default end).
2711 * @return Index of last occurrence.
2712 *
2713 * Starting from @a __pos, searches backward for @a __c within
2714 * this string. If found, returns the index where it was
2715 * found. If not found, returns npos.
2716 *
2717 * Note: equivalent to rfind(__c, __pos).
2718 */
2719 size_type
2720 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
2721 { return this->rfind(__c, __pos); }
2722
2723#ifdef __glibcxx_string_view // >= C++17
2724 /**
2725 * @brief Find last position of a character of string.
2726 * @param __svt An object convertible to string_view containing
2727 * characters to locate.
2728 * @param __pos Index of character to search back from (default end).
2729 * @return Index of last occurrence.
2730 */
2731 template<typename _Tp>
2732 _If_sv<_Tp, size_type>
2733 find_last_of(const _Tp& __svt, size_type __pos = npos) const
2734 noexcept(is_same<_Tp, __sv_type>::value)
2735 {
2736 __sv_type __sv = __svt;
2737 return this->find_last_of(__sv.data(), __pos, __sv.size());
2738 }
2739#endif // C++17
2740
2741 /**
2742 * @brief Find position of a character not in string.
2743 * @param __str String containing characters to avoid.
2744 * @param __pos Index of character to search from (default 0).
2745 * @return Index of first occurrence.
2746 *
2747 * Starting from @a __pos, searches forward for a character not contained
2748 * in @a __str within this string. If found, returns the index where it
2749 * was found. If not found, returns npos.
2750 */
2751 size_type
2752 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
2753 _GLIBCXX_NOEXCEPT
2754 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
2755
2756 /**
2757 * @brief Find position of a character not in C substring.
2758 * @param __s C string containing characters to avoid.
2759 * @param __pos Index of character to search from.
2760 * @param __n Number of characters from __s to consider.
2761 * @return Index of first occurrence.
2762 *
2763 * Starting from @a __pos, searches forward for a character not
2764 * contained in the first @a __n characters of @a __s within
2765 * this string. If found, returns the index where it was
2766 * found. If not found, returns npos.
2767 */
2768 size_type
2769 find_first_not_of(const _CharT* __s, size_type __pos,
2770 size_type __n) const _GLIBCXX_NOEXCEPT;
2771
2772 /**
2773 * @brief Find position of a character not in C string.
2774 * @param __s C string containing characters to avoid.
2775 * @param __pos Index of character to search from (default 0).
2776 * @return Index of first occurrence.
2777 *
2778 * Starting from @a __pos, searches forward for a character not
2779 * contained in @a __s within this string. If found, returns
2780 * the index where it was found. If not found, returns npos.
2781 */
2782 size_type
2783 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
2784 _GLIBCXX_NOEXCEPT
2785 {
2786 __glibcxx_requires_string(__s);
2787 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
2788 }
2789
2790 /**
2791 * @brief Find position of a different character.
2792 * @param __c Character to avoid.
2793 * @param __pos Index of character to search from (default 0).
2794 * @return Index of first occurrence.
2795 *
2796 * Starting from @a __pos, searches forward for a character
2797 * other than @a __c within this string. If found, returns the
2798 * index where it was found. If not found, returns npos.
2799 */
2800 size_type
2801 find_first_not_of(_CharT __c, size_type __pos = 0) const
2802 _GLIBCXX_NOEXCEPT;
2803
2804#ifdef __glibcxx_string_view // >= C++17
2805 /**
2806 * @brief Find position of a character not in a string_view.
2807 * @param __svt An object convertible to string_view containing
2808 * characters to avoid.
2809 * @param __pos Index of character to search from (default 0).
2810 * @return Index of first occurrence.
2811 */
2812 template<typename _Tp>
2813 _If_sv<_Tp, size_type>
2814 find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
2815 noexcept(is_same<_Tp, __sv_type>::value)
2816 {
2817 __sv_type __sv = __svt;
2818 return this->find_first_not_of(__sv.data(), __pos, __sv.size());
2819 }
2820#endif // C++17
2821
2822 /**
2823 * @brief Find last position of a character not in string.
2824 * @param __str String containing characters to avoid.
2825 * @param __pos Index of character to search back from (default end).
2826 * @return Index of last occurrence.
2827 *
2828 * Starting from @a __pos, searches backward for a character
2829 * not contained in @a __str within this string. If found,
2830 * returns the index where it was found. If not found, returns
2831 * npos.
2832 */
2833 size_type
2834 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
2835 _GLIBCXX_NOEXCEPT
2836 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
2837
2838 /**
2839 * @brief Find last position of a character not in C substring.
2840 * @param __s C string containing characters to avoid.
2841 * @param __pos Index of character to search back from.
2842 * @param __n Number of characters from s to consider.
2843 * @return Index of last occurrence.
2844 *
2845 * Starting from @a __pos, searches backward for a character not
2846 * contained in the first @a __n characters of @a __s within this string.
2847 * If found, returns the index where it was found. If not found,
2848 * returns npos.
2849 */
2850 size_type
2851 find_last_not_of(const _CharT* __s, size_type __pos,
2852 size_type __n) const _GLIBCXX_NOEXCEPT;
2853 /**
2854 * @brief Find last position of a character not in C string.
2855 * @param __s C string containing characters to avoid.
2856 * @param __pos Index of character to search back from (default end).
2857 * @return Index of last occurrence.
2858 *
2859 * Starting from @a __pos, searches backward for a character
2860 * not contained in @a __s within this string. If found,
2861 * returns the index where it was found. If not found, returns
2862 * npos.
2863 */
2864 size_type
2865 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
2866 _GLIBCXX_NOEXCEPT
2867 {
2868 __glibcxx_requires_string(__s);
2869 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
2870 }
2871
2872 /**
2873 * @brief Find last position of a different character.
2874 * @param __c Character to avoid.
2875 * @param __pos Index of character to search back from (default end).
2876 * @return Index of last occurrence.
2877 *
2878 * Starting from @a __pos, searches backward for a character other than
2879 * @a __c within this string. If found, returns the index where it was
2880 * found. If not found, returns npos.
2881 */
2882 size_type
2883 find_last_not_of(_CharT __c, size_type __pos = npos) const
2884 _GLIBCXX_NOEXCEPT;
2885
2886#ifdef __glibcxx_string_view // >= C++17
2887 /**
2888 * @brief Find last position of a character not in a string_view.
2889 * @param __svt An object convertible to string_view containing
2890 * characters to avoid.
2891 * @param __pos Index of character to search back from (default end).
2892 * @return Index of last occurrence.
2893 */
2894 template<typename _Tp>
2895 _If_sv<_Tp, size_type>
2896 find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
2897 noexcept(is_same<_Tp, __sv_type>::value)
2898 {
2899 __sv_type __sv = __svt;
2900 return this->find_last_not_of(__sv.data(), __pos, __sv.size());
2901 }
2902#endif // C++17
2903
2904 /**
2905 * @brief Get a substring.
2906 * @param __pos Index of first character (default 0).
2907 * @param __n Number of characters in substring (default remainder).
2908 * @return The new string.
2909 * @throw std::out_of_range If __pos > size().
2910 *
2911 * Construct and return a new string using the @a __n
2912 * characters starting at @a __pos. If the string is too
2913 * short, use the remainder of the characters. If @a __pos is
2914 * beyond the end of the string, out_of_range is thrown.
2915 */
2916 basic_string
2917 substr(size_type __pos = 0, size_type __n = npos) const
2918 { return basic_string(*this,
2919 _M_check(__pos, "basic_string::substr"), __n); }
2920
2921#ifdef __glibcxx_string_subview // >= C++26
2922 /**
2923 * @brief Get a subview.
2924 * @param __pos Index of first character (default 0).
2925 * @param __n Number of characters in subview (default remainder).
2926 * @return The subview.
2927 * @throw std::out_of_range If __pos > size().
2928 *
2929 * Construct and return a subview using the `__n` characters starting at
2930 * `__pos`. If the string is too short, use the remainder of the
2931 * characters. If `__pos` is beyond the end of the string, out_of_range
2932 * is thrown.
2933 */
2934 [[nodiscard]]
2935 constexpr basic_string_view<_CharT, _Traits>
2936 subview(size_type __pos = 0, size_type __n = npos) const
2937 { return __sv_type(*this).subview(__pos, __n); }
2938#endif
2939
2940 /**
2941 * @brief Compare to a string.
2942 * @param __str String to compare against.
2943 * @return Integer < 0, 0, or > 0.
2944 *
2945 * Returns an integer < 0 if this string is ordered before @a
2946 * __str, 0 if their values are equivalent, or > 0 if this
2947 * string is ordered after @a __str. Determines the effective
2948 * length rlen of the strings to compare as the smallest of
2949 * size() and str.size(). The function then compares the two
2950 * strings by calling traits::compare(data(), str.data(),rlen).
2951 * If the result of the comparison is nonzero returns it,
2952 * otherwise the shorter one is ordered first.
2953 */
2954 int
2955 compare(const basic_string& __str) const
2956 {
2957 const size_type __size = this->size();
2958 const size_type __osize = __str.size();
2959 const size_type __len = std::min(__size, __osize);
2960
2961 int __r = traits_type::compare(_M_data(), __str.data(), __len);
2962 if (!__r)
2963 __r = _S_compare(__size, __osize);
2964 return __r;
2965 }
2966
2967#ifdef __glibcxx_string_view // >= C++17
2968 /**
2969 * @brief Compare to a string_view.
2970 * @param __svt An object convertible to string_view to compare against.
2971 * @return Integer < 0, 0, or > 0.
2972 */
2973 template<typename _Tp>
2974 _If_sv<_Tp, int>
2975 compare(const _Tp& __svt) const
2976 noexcept(is_same<_Tp, __sv_type>::value)
2977 {
2978 __sv_type __sv = __svt;
2979 const size_type __size = this->size();
2980 const size_type __osize = __sv.size();
2981 const size_type __len = std::min(__size, __osize);
2982
2983 int __r = traits_type::compare(_M_data(), __sv.data(), __len);
2984 if (!__r)
2985 __r = _S_compare(__size, __osize);
2986 return __r;
2987 }
2988
2989 /**
2990 * @brief Compare to a string_view.
2991 * @param __pos A position in the string to start comparing from.
2992 * @param __n The number of characters to compare.
2993 * @param __svt An object convertible to string_view to compare
2994 * against.
2995 * @return Integer < 0, 0, or > 0.
2996 */
2997 template<typename _Tp>
2998 _If_sv<_Tp, int>
2999 compare(size_type __pos, size_type __n, const _Tp& __svt) const
3000 {
3001 __sv_type __sv = __svt;
3002 return __sv_type(*this).substr(__pos, __n).compare(__sv);
3003 }
3004
3005 /**
3006 * @brief Compare to a string_view.
3007 * @param __pos1 A position in the string to start comparing from.
3008 * @param __n1 The number of characters to compare.
3009 * @param __svt An object convertible to string_view to compare
3010 * against.
3011 * @param __pos2 A position in the string_view to start comparing from.
3012 * @param __n2 The number of characters to compare.
3013 * @return Integer < 0, 0, or > 0.
3014 */
3015 template<typename _Tp>
3016 _If_sv<_Tp, int>
3017 compare(size_type __pos1, size_type __n1, const _Tp& __svt,
3018 size_type __pos2, size_type __n2 = npos) const
3019 {
3020 __sv_type __sv = __svt;
3021 return __sv_type(*this)
3022 .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3023 }
3024#endif // C++17
3025
3026 /**
3027 * @brief Compare substring to a string.
3028 * @param __pos Index of first character of substring.
3029 * @param __n Number of characters in substring.
3030 * @param __str String to compare against.
3031 * @return Integer < 0, 0, or > 0.
3032 *
3033 * Form the substring of this string from the @a __n characters
3034 * starting at @a __pos. Returns an integer < 0 if the
3035 * substring is ordered before @a __str, 0 if their values are
3036 * equivalent, or > 0 if the substring is ordered after @a
3037 * __str. Determines the effective length rlen of the strings
3038 * to compare as the smallest of the length of the substring
3039 * and @a __str.size(). The function then compares the two
3040 * strings by calling
3041 * traits::compare(substring.data(),str.data(),rlen). If the
3042 * result of the comparison is nonzero returns it, otherwise
3043 * the shorter one is ordered first.
3044 */
3045 int
3046 compare(size_type __pos, size_type __n, const basic_string& __str) const
3047 {
3048 _M_check(__pos, "basic_string::compare");
3049 __n = _M_limit(__pos, __n);
3050 const size_type __osize = __str.size();
3051 const size_type __len = std::min(__n, __osize);
3052 int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
3053 if (!__r)
3054 __r = _S_compare(__n, __osize);
3055 return __r;
3056 }
3057
3058 /**
3059 * @brief Compare substring to a substring.
3060 * @param __pos1 Index of first character of substring.
3061 * @param __n1 Number of characters in substring.
3062 * @param __str String to compare against.
3063 * @param __pos2 Index of first character of substring of str.
3064 * @param __n2 Number of characters in substring of str.
3065 * @return Integer < 0, 0, or > 0.
3066 *
3067 * Form the substring of this string from the @a __n1
3068 * characters starting at @a __pos1. Form the substring of @a
3069 * __str from the @a __n2 characters starting at @a __pos2.
3070 * Returns an integer < 0 if this substring is ordered before
3071 * the substring of @a __str, 0 if their values are equivalent,
3072 * or > 0 if this substring is ordered after the substring of
3073 * @a __str. Determines the effective length rlen of the
3074 * strings to compare as the smallest of the lengths of the
3075 * substrings. The function then compares the two strings by
3076 * calling
3077 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
3078 * If the result of the comparison is nonzero returns it,
3079 * otherwise the shorter one is ordered first.
3080 */
3081 int
3082 compare(size_type __pos1, size_type __n1, const basic_string& __str,
3083 size_type __pos2, size_type __n2 = npos) const
3084 {
3085 _M_check(__pos1, "basic_string::compare");
3086 __str._M_check(__pos2, "basic_string::compare");
3087 __n1 = _M_limit(__pos1, __n1);
3088 __n2 = __str._M_limit(__pos2, __n2);
3089 const size_type __len = std::min(__n1, __n2);
3090 int __r = traits_type::compare(_M_data() + __pos1,
3091 __str.data() + __pos2, __len);
3092 if (!__r)
3093 __r = _S_compare(__n1, __n2);
3094 return __r;
3095 }
3096
3097 /**
3098 * @brief Compare to a C string.
3099 * @param __s C string to compare against.
3100 * @return Integer < 0, 0, or > 0.
3101 *
3102 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
3103 * their values are equivalent, or > 0 if this string is ordered after
3104 * @a __s. Determines the effective length rlen of the strings to
3105 * compare as the smallest of size() and the length of a string
3106 * constructed from @a __s. The function then compares the two strings
3107 * by calling traits::compare(data(),s,rlen). If the result of the
3108 * comparison is nonzero returns it, otherwise the shorter one is
3109 * ordered first.
3110 */
3111 int
3112 compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT
3113 {
3114 __glibcxx_requires_string(__s);
3115 const size_type __size = this->size();
3116 const size_type __osize = traits_type::length(__s);
3117 const size_type __len = std::min(__size, __osize);
3118 int __r = traits_type::compare(_M_data(), __s, __len);
3119 if (!__r)
3120 __r = _S_compare(__size, __osize);
3121 return __r;
3122 }
3123
3124 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3125 // 5 String::compare specification questionable
3126 /**
3127 * @brief Compare substring to a C string.
3128 * @param __pos Index of first character of substring.
3129 * @param __n1 Number of characters in substring.
3130 * @param __s C string to compare against.
3131 * @return Integer < 0, 0, or > 0.
3132 *
3133 * Form the substring of this string from the @a __n1
3134 * characters starting at @a pos. Returns an integer < 0 if
3135 * the substring is ordered before @a __s, 0 if their values
3136 * are equivalent, or > 0 if the substring is ordered after @a
3137 * __s. Determines the effective length rlen of the strings to
3138 * compare as the smallest of the length of the substring and
3139 * the length of a string constructed from @a __s. The
3140 * function then compares the two string by calling
3141 * traits::compare(substring.data(),__s,rlen). If the result of
3142 * the comparison is nonzero returns it, otherwise the shorter
3143 * one is ordered first.
3144 */
3145 int
3146 compare(size_type __pos, size_type __n1, const _CharT* __s) const
3147 {
3148 __glibcxx_requires_string(__s);
3149 _M_check(__pos, "basic_string::compare");
3150 __n1 = _M_limit(__pos, __n1);
3151 const size_type __osize = traits_type::length(__s);
3152 const size_type __len = std::min(__n1, __osize);
3153 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
3154 if (!__r)
3155 __r = _S_compare(__n1, __osize);
3156 return __r;
3157 }
3158
3159 /**
3160 * @brief Compare substring against a character %array.
3161 * @param __pos Index of first character of substring.
3162 * @param __n1 Number of characters in substring.
3163 * @param __s character %array to compare against.
3164 * @param __n2 Number of characters of s.
3165 * @return Integer < 0, 0, or > 0.
3166 *
3167 * Form the substring of this string from the @a __n1
3168 * characters starting at @a __pos. Form a string from the
3169 * first @a __n2 characters of @a __s. Returns an integer < 0
3170 * if this substring is ordered before the string from @a __s,
3171 * 0 if their values are equivalent, or > 0 if this substring
3172 * is ordered after the string from @a __s. Determines the
3173 * effective length rlen of the strings to compare as the
3174 * smallest of the length of the substring and @a __n2. The
3175 * function then compares the two strings by calling
3176 * traits::compare(substring.data(),s,rlen). If the result of
3177 * the comparison is nonzero returns it, otherwise the shorter
3178 * one is ordered first.
3179 *
3180 * NB: s must have at least n2 characters, &apos;\\0&apos; has
3181 * no special meaning.
3182 */
3183 int
3184 compare(size_type __pos, size_type __n1, const _CharT* __s,
3185 size_type __n2) const
3186 {
3187 __glibcxx_requires_string_len(__s, __n2);
3188 _M_check(__pos, "basic_string::compare");
3189 __n1 = _M_limit(__pos, __n1);
3190 const size_type __len = std::min(__n1, __n2);
3191 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
3192 if (!__r)
3193 __r = _S_compare(__n1, __n2);
3194 return __r;
3195 }
3196
3197#if __cplusplus >= 202002L
3198 bool
3199 starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
3200 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3201
3202 bool
3203 starts_with(_CharT __x) const noexcept
3204 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3205
3206 [[__gnu__::__nonnull__]]
3207 bool
3208 starts_with(const _CharT* __x) const noexcept
3209 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3210
3211 bool
3212 ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept
3213 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3214
3215 bool
3216 ends_with(_CharT __x) const noexcept
3217 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3218
3219 [[__gnu__::__nonnull__]]
3220 bool
3221 ends_with(const _CharT* __x) const noexcept
3222 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3223#endif // C++20
3224
3225#if __cplusplus > 202011L
3226 bool
3227 contains(basic_string_view<_CharT, _Traits> __x) const noexcept
3228 { return __sv_type(this->data(), this->size()).contains(__x); }
3229
3230 bool
3231 contains(_CharT __x) const noexcept
3232 { return __sv_type(this->data(), this->size()).contains(__x); }
3233
3234 [[__gnu__::__nonnull__]]
3235 bool
3236 contains(const _CharT* __x) const noexcept
3237 { return __sv_type(this->data(), this->size()).contains(__x); }
3238#endif // C++23
3239
3240# ifdef _GLIBCXX_TM_TS_INTERNAL
3241 friend void
3242 ::_txnal_cow_string_C1_for_exceptions(void* that, const char* s,
3243 void* exc);
3244 friend const char*
3245 ::_txnal_cow_string_c_str(const void *that);
3246 friend void
3247 ::_txnal_cow_string_D1(void *that);
3248 friend void
3249 ::_txnal_cow_string_D1_commit(void *that);
3250# endif
3251 };
3252
3253 template<typename _CharT, typename _Traits, typename _Alloc>
3254 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
3255 basic_string<_CharT, _Traits, _Alloc>::
3256 _Rep::_S_max_size = (((npos - sizeof(_Rep_base))/sizeof(_CharT)) - 1) / 4;
3257
3258 template<typename _CharT, typename _Traits, typename _Alloc>
3259 const _CharT
3260 basic_string<_CharT, _Traits, _Alloc>::
3261 _Rep::_S_terminal = _CharT();
3262
3263 template<typename _CharT, typename _Traits, typename _Alloc>
3264 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
3265 basic_string<_CharT, _Traits, _Alloc>::npos;
3266
3267 // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string)
3268 // at static init time (before static ctors are run).
3269 template<typename _CharT, typename _Traits, typename _Alloc>
3270 typename basic_string<_CharT, _Traits, _Alloc>::size_type
3271 basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[
3272 (sizeof(_Rep_base) + sizeof(_CharT) + sizeof(size_type) - 1) /
3273 sizeof(size_type)];
3274
3275 // NB: This is the special case for Input Iterators, used in
3276 // istreambuf_iterators, etc.
3277 // Input Iterators have a cost structure very different from
3278 // pointers, calling for a different coding style.
3279 template<typename _CharT, typename _Traits, typename _Alloc>
3280 template<typename _InIterator>
3281 _CharT*
3282 basic_string<_CharT, _Traits, _Alloc>::
3283 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
3284 input_iterator_tag)
3285 {
3286#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3287 if (__beg == __end && __a == _Alloc())
3288 return _S_empty_rep()._M_refdata();
3289#endif
3290 // Avoid reallocation for common case.
3291 _CharT __buf[128];
3292 size_type __len = 0;
3293 while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
3294 {
3295 __buf[__len++] = *__beg;
3296 ++__beg;
3297 }
3298 _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
3299 _M_copy(__r->_M_refdata(), __buf, __len);
3300 __try
3301 {
3302 while (__beg != __end)
3303 {
3304 if (__len == __r->_M_capacity)
3305 {
3306 // Allocate more space.
3307 _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
3308 _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
3309 __r->_M_destroy(__a);
3310 __r = __another;
3311 }
3312 __r->_M_refdata()[__len++] = *__beg;
3313 ++__beg;
3314 }
3315 }
3316 __catch(...)
3317 {
3318 __r->_M_destroy(__a);
3319 __throw_exception_again;
3320 }
3321 __r->_M_set_length_and_sharable(__len);
3322 return __r->_M_refdata();
3323 }
3324
3325 template<typename _CharT, typename _Traits, typename _Alloc>
3326 template <typename _InIterator>
3327 _CharT*
3328 basic_string<_CharT, _Traits, _Alloc>::
3329 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
3330 forward_iterator_tag)
3331 {
3332#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3333 if (__beg == __end && __a == _Alloc())
3334 return _S_empty_rep()._M_refdata();
3335#endif
3336 // NB: Not required, but considered best practice.
3337 if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end)
3338 __throw_logic_error(__N("basic_string::_S_construct null not valid"));
3339
3340 const size_type __dnew = static_cast<size_type>(std::distance(__beg,
3341 __end));
3342 // Check for out_of_range and length_error exceptions.
3343 _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
3344 __try
3345 { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
3346 __catch(...)
3347 {
3348 __r->_M_destroy(__a);
3349 __throw_exception_again;
3350 }
3351 __r->_M_set_length_and_sharable(__dnew);
3352 return __r->_M_refdata();
3353 }
3354
3355 template<typename _CharT, typename _Traits, typename _Alloc>
3356 _CharT*
3357 basic_string<_CharT, _Traits, _Alloc>::
3358 _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
3359 {
3360#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3361 if (__n == 0 && __a == _Alloc())
3362 return _S_empty_rep()._M_refdata();
3363#endif
3364 // Check for out_of_range and length_error exceptions.
3365 _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
3366 if (__n)
3367 _M_assign(__r->_M_refdata(), __n, __c);
3368
3369 __r->_M_set_length_and_sharable(__n);
3370 return __r->_M_refdata();
3371 }
3372
3373 template<typename _CharT, typename _Traits, typename _Alloc>
3374 basic_string<_CharT, _Traits, _Alloc>::
3375 basic_string(const basic_string& __str, size_type __pos, const _Alloc& __a)
3376 : _M_dataplus(_S_construct(__str._M_data()
3377 + __str._M_check(__pos,
3378 "basic_string::basic_string"),
3379 __str._M_data() + __str._M_limit(__pos, npos)
3380 + __pos, __a), __a)
3381 { }
3382
3383 template<typename _CharT, typename _Traits, typename _Alloc>
3384 basic_string<_CharT, _Traits, _Alloc>::
3385 basic_string(const basic_string& __str, size_type __pos, size_type __n)
3386 : _M_dataplus(_S_construct(__str._M_data()
3387 + __str._M_check(__pos,
3388 "basic_string::basic_string"),
3389 __str._M_data() + __str._M_limit(__pos, __n)
3390 + __pos, _Alloc()), _Alloc())
3391 { }
3392
3393 template<typename _CharT, typename _Traits, typename _Alloc>
3394 basic_string<_CharT, _Traits, _Alloc>::
3395 basic_string(const basic_string& __str, size_type __pos,
3396 size_type __n, const _Alloc& __a)
3397 : _M_dataplus(_S_construct(__str._M_data()
3398 + __str._M_check(__pos,
3399 "basic_string::basic_string"),
3400 __str._M_data() + __str._M_limit(__pos, __n)
3401 + __pos, __a), __a)
3402 { }
3403
3404 template<typename _CharT, typename _Traits, typename _Alloc>
3405 basic_string<_CharT, _Traits, _Alloc>&
3406 basic_string<_CharT, _Traits, _Alloc>::
3407 assign(const basic_string& __str)
3408 {
3409 if (_M_rep() != __str._M_rep())
3410 {
3411 // XXX MT
3412 const allocator_type __a = this->get_allocator();
3413 _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
3414 _M_rep()->_M_dispose(__a);
3415 _M_data(__tmp);
3416 }
3417 return *this;
3418 }
3419
3420 template<typename _CharT, typename _Traits, typename _Alloc>
3421 basic_string<_CharT, _Traits, _Alloc>&
3422 basic_string<_CharT, _Traits, _Alloc>::
3423 assign(const _CharT* __s, size_type __n)
3424 {
3425 __glibcxx_requires_string_len(__s, __n);
3426 _M_check_length(this->size(), __n, "basic_string::assign");
3427 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
3428 return _M_replace_safe(size_type(0), this->size(), __s, __n);
3429 else
3430 {
3431 // Work in-place.
3432 const size_type __pos = __s - _M_data();
3433 if (__pos >= __n)
3434 _M_copy(_M_data(), __s, __n);
3435 else if (__pos)
3436 _M_move(_M_data(), __s, __n);
3437 _M_rep()->_M_set_length_and_sharable(__n);
3438 return *this;
3439 }
3440 }
3441
3442 template<typename _CharT, typename _Traits, typename _Alloc>
3443 basic_string<_CharT, _Traits, _Alloc>&
3444 basic_string<_CharT, _Traits, _Alloc>::
3445 append(size_type __n, _CharT __c)
3446 {
3447 if (__n)
3448 {
3449 _M_check_length(size_type(0), __n, "basic_string::append");
3450 const size_type __len = __n + this->size();
3451 if (__len > this->capacity() || _M_rep()->_M_is_shared())
3452 this->reserve(__len);
3453 _M_assign(_M_data() + this->size(), __n, __c);
3454 _M_rep()->_M_set_length_and_sharable(__len);
3455 }
3456 return *this;
3457 }
3458
3459 template<typename _CharT, typename _Traits, typename _Alloc>
3460 basic_string<_CharT, _Traits, _Alloc>&
3461 basic_string<_CharT, _Traits, _Alloc>::
3462 append(const _CharT* __s, size_type __n)
3463 {
3464 __glibcxx_requires_string_len(__s, __n);
3465 if (__n)
3466 {
3467 _M_check_length(size_type(0), __n, "basic_string::append");
3468 const size_type __len = __n + this->size();
3469 if (__len > this->capacity() || _M_rep()->_M_is_shared())
3470 {
3471 if (_M_disjunct(__s))
3472 this->reserve(__len);
3473 else
3474 {
3475 const size_type __off = __s - _M_data();
3476 this->reserve(__len);
3477 __s = _M_data() + __off;
3478 }
3479 }
3480 _M_copy(_M_data() + this->size(), __s, __n);
3481 _M_rep()->_M_set_length_and_sharable(__len);
3482 }
3483 return *this;
3484 }
3485
3486 template<typename _CharT, typename _Traits, typename _Alloc>
3487 basic_string<_CharT, _Traits, _Alloc>&
3488 basic_string<_CharT, _Traits, _Alloc>::
3489 append(const basic_string& __str)
3490 {
3491 const size_type __size = __str.size();
3492 if (__size)
3493 {
3494 const size_type __len = __size + this->size();
3495 if (__len > this->capacity() || _M_rep()->_M_is_shared())
3496 this->reserve(__len);
3497 _M_copy(_M_data() + this->size(), __str._M_data(), __size);
3498 _M_rep()->_M_set_length_and_sharable(__len);
3499 }
3500 return *this;
3501 }
3502
3503 template<typename _CharT, typename _Traits, typename _Alloc>
3504 basic_string<_CharT, _Traits, _Alloc>&
3505 basic_string<_CharT, _Traits, _Alloc>::
3506 append(const basic_string& __str, size_type __pos, size_type __n)
3507 {
3508 __str._M_check(__pos, "basic_string::append");
3509 __n = __str._M_limit(__pos, __n);
3510 if (__n)
3511 {
3512 const size_type __len = __n + this->size();
3513 if (__len > this->capacity() || _M_rep()->_M_is_shared())
3514 this->reserve(__len);
3515 _M_copy(_M_data() + this->size(), __str._M_data() + __pos, __n);
3516 _M_rep()->_M_set_length_and_sharable(__len);
3517 }
3518 return *this;
3519 }
3520
3521 template<typename _CharT, typename _Traits, typename _Alloc>
3522 basic_string<_CharT, _Traits, _Alloc>&
3523 basic_string<_CharT, _Traits, _Alloc>::
3524 insert(size_type __pos, const _CharT* __s, size_type __n)
3525 {
3526 __glibcxx_requires_string_len(__s, __n);
3527 _M_check(__pos, "basic_string::insert");
3528 _M_check_length(size_type(0), __n, "basic_string::insert");
3529 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
3530 return _M_replace_safe(__pos, size_type(0), __s, __n);
3531 else
3532 {
3533 // Work in-place.
3534 const size_type __off = __s - _M_data();
3535 _M_mutate(__pos, 0, __n);
3536 __s = _M_data() + __off;
3537 _CharT* __p = _M_data() + __pos;
3538 if (__s + __n <= __p)
3539 _M_copy(__p, __s, __n);
3540 else if (__s >= __p)
3541 _M_copy(__p, __s + __n, __n);
3542 else
3543 {
3544 const size_type __nleft = __p - __s;
3545 _M_copy(__p, __s, __nleft);
3546 _M_copy(__p + __nleft, __p + __n, __n - __nleft);
3547 }
3548 return *this;
3549 }
3550 }
3551
3552 template<typename _CharT, typename _Traits, typename _Alloc>
3553 typename basic_string<_CharT, _Traits, _Alloc>::iterator
3554 basic_string<_CharT, _Traits, _Alloc>::
3555 erase(iterator __first, iterator __last)
3556 {
3557 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
3558 && __last <= _M_iend());
3559
3560 // NB: This isn't just an optimization (bail out early when
3561 // there is nothing to do, really), it's also a correctness
3562 // issue vs MT, see libstdc++/40518.
3563 const size_type __size = __last - __first;
3564 if (__size)
3565 {
3566 const size_type __pos = __first - _M_ibegin();
3567 _M_mutate(__pos, __size, size_type(0));
3568 _M_rep()->_M_set_leaked();
3569 return iterator(_M_data() + __pos);
3570 }
3571 else
3572 return __first;
3573 }
3574
3575 template<typename _CharT, typename _Traits, typename _Alloc>
3576 basic_string<_CharT, _Traits, _Alloc>&
3577 basic_string<_CharT, _Traits, _Alloc>::
3578 replace(size_type __pos, size_type __n1, const _CharT* __s,
3579 size_type __n2)
3580 {
3581 __glibcxx_requires_string_len(__s, __n2);
3582 _M_check(__pos, "basic_string::replace");
3583 __n1 = _M_limit(__pos, __n1);
3584 _M_check_length(__n1, __n2, "basic_string::replace");
3585 bool __left;
3586 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
3587 return _M_replace_safe(__pos, __n1, __s, __n2);
3588 else if ((__left = __s + __n2 <= _M_data() + __pos)
3589 || _M_data() + __pos + __n1 <= __s)
3590 {
3591 // Work in-place: non-overlapping case.
3592 size_type __off = __s - _M_data();
3593 __left ? __off : (__off += __n2 - __n1);
3594 _M_mutate(__pos, __n1, __n2);
3595 _M_copy(_M_data() + __pos, _M_data() + __off, __n2);
3596 return *this;
3597 }
3598 else
3599 {
3600 // TODO: overlapping case.
3601 const basic_string __tmp(__s, __n2);
3602 return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
3603 }
3604 }
3605
3606 template<typename _CharT, typename _Traits, typename _Alloc>
3607 void
3608 basic_string<_CharT, _Traits, _Alloc>::_Rep::
3609 _M_destroy(const _Alloc& __a) throw ()
3610 {
3611 const size_type __size = sizeof(_Rep_base)
3612 + (this->_M_capacity + 1) * sizeof(_CharT);
3613 _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
3614 }
3615
3616 template<typename _CharT, typename _Traits, typename _Alloc>
3617 void
3618 basic_string<_CharT, _Traits, _Alloc>::
3619 _M_leak_hard()
3620 {
3621 // No need to create a new copy of an empty string when a non-const
3622 // reference/pointer/iterator into it is obtained. Modifying the
3623 // trailing null character is undefined, so the ref/pointer/iterator
3624 // is effectively const anyway.
3625 if (this->empty())
3626 return;
3627
3628 if (_M_rep()->_M_is_shared())
3629 _M_mutate(0, 0, 0);
3630 _M_rep()->_M_set_leaked();
3631 }
3632
3633 template<typename _CharT, typename _Traits, typename _Alloc>
3634 void
3635 basic_string<_CharT, _Traits, _Alloc>::
3636 _M_mutate(size_type __pos, size_type __len1, size_type __len2)
3637 {
3638 const size_type __old_size = this->size();
3639 const size_type __new_size = __old_size + __len2 - __len1;
3640 const size_type __how_much = __old_size - __pos - __len1;
3641
3642 if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
3643 {
3644 // Must reallocate.
3645 const allocator_type __a = get_allocator();
3646 _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
3647
3648 if (__pos)
3649 _M_copy(__r->_M_refdata(), _M_data(), __pos);
3650 if (__how_much)
3651 _M_copy(__r->_M_refdata() + __pos + __len2,
3652 _M_data() + __pos + __len1, __how_much);
3653
3654 _M_rep()->_M_dispose(__a);
3655 _M_data(__r->_M_refdata());
3656 }
3657 else if (__how_much && __len1 != __len2)
3658 {
3659 // Work in-place.
3660 _M_move(_M_data() + __pos + __len2,
3661 _M_data() + __pos + __len1, __how_much);
3662 }
3663 _M_rep()->_M_set_length_and_sharable(__new_size);
3664 }
3665
3666 template<typename _CharT, typename _Traits, typename _Alloc>
3667 void
3668 basic_string<_CharT, _Traits, _Alloc>::
3669 reserve(size_type __res)
3670 {
3671 const size_type __capacity = capacity();
3672
3673 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3674 // 2968. Inconsistencies between basic_string reserve and
3675 // vector/unordered_map/unordered_set reserve functions
3676 // P0966 reserve should not shrink
3677 if (__res <= __capacity)
3678 {
3679 if (!_M_rep()->_M_is_shared())
3680 return;
3681
3682 // unshare, but keep same capacity
3683 __res = __capacity;
3684 }
3685
3686 const allocator_type __a = get_allocator();
3687 _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
3688 _M_rep()->_M_dispose(__a);
3689 _M_data(__tmp);
3690 }
3691
3692 template<typename _CharT, typename _Traits, typename _Alloc>
3693 void
3694 basic_string<_CharT, _Traits, _Alloc>::
3695 swap(basic_string& __s)
3696 _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value)
3697 {
3698 if (_M_rep()->_M_is_leaked())
3699 _M_rep()->_M_set_sharable();
3700 if (__s._M_rep()->_M_is_leaked())
3701 __s._M_rep()->_M_set_sharable();
3702 if (this->get_allocator() == __s.get_allocator())
3703 {
3704 _CharT* __tmp = _M_data();
3705 _M_data(__s._M_data());
3706 __s._M_data(__tmp);
3707 }
3708 // The code below can usually be optimized away.
3709 else
3710 {
3711 const basic_string __tmp1(_M_ibegin(), _M_iend(),
3712 __s.get_allocator());
3713 const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
3714 this->get_allocator());
3715 *this = __tmp2;
3716 __s = __tmp1;
3717 }
3718 }
3719
3720 template<typename _CharT, typename _Traits, typename _Alloc>
3721 typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
3722 basic_string<_CharT, _Traits, _Alloc>::_Rep::
3723 _S_create(size_type __capacity, size_type __old_capacity,
3724 const _Alloc& __alloc)
3725 {
3726 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3727 // 83. String::npos vs. string::max_size()
3728 if (__capacity > _S_max_size)
3729 __throw_length_error(__N("basic_string::_S_create"));
3730
3731 // The standard places no restriction on allocating more memory
3732 // than is strictly needed within this layer at the moment or as
3733 // requested by an explicit application call to reserve(n).
3734
3735 // Many malloc implementations perform quite poorly when an
3736 // application attempts to allocate memory in a stepwise fashion
3737 // growing each allocation size by only 1 char. Additionally,
3738 // it makes little sense to allocate less linear memory than the
3739 // natural blocking size of the malloc implementation.
3740 // Unfortunately, we would need a somewhat low-level calculation
3741 // with tuned parameters to get this perfect for any particular
3742 // malloc implementation. Fortunately, generalizations about
3743 // common features seen among implementations seems to suffice.
3744
3745 // __pagesize need not match the actual VM page size for good
3746 // results in practice, thus we pick a common value on the low
3747 // side. __malloc_header_size is an estimate of the amount of
3748 // overhead per memory allocation (in practice seen N * sizeof
3749 // (void*) where N is 0, 2 or 4). According to folklore,
3750 // picking this value on the high side is better than
3751 // low-balling it (especially when this algorithm is used with
3752 // malloc implementations that allocate memory blocks rounded up
3753 // to a size which is a power of 2).
3754 const size_type __pagesize = 4096;
3755 const size_type __malloc_header_size = 4 * sizeof(void*);
3756
3757 // The below implements an exponential growth policy, necessary to
3758 // meet amortized linear time requirements of the library: see
3759 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
3760 // It's active for allocations requiring an amount of memory above
3761 // system pagesize. This is consistent with the requirements of the
3762 // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
3763 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
3764 __capacity = 2 * __old_capacity;
3765
3766 // NB: Need an array of char_type[__capacity], plus a terminating
3767 // null char_type() element, plus enough for the _Rep data structure.
3768 // Whew. Seemingly so needy, yet so elemental.
3769 size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
3770
3771 const size_type __adj_size = __size + __malloc_header_size;
3772 if (__adj_size > __pagesize && __capacity > __old_capacity)
3773 {
3774 const size_type __extra = __pagesize - __adj_size % __pagesize;
3775 __capacity += __extra / sizeof(_CharT);
3776 // Never allocate a string bigger than _S_max_size.
3777 if (__capacity > _S_max_size)
3778 __capacity = _S_max_size;
3779 __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
3780 }
3781
3782 // NB: Might throw, but no worries about a leak, mate: _Rep()
3783 // does not throw.
3784 void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
3785 _Rep *__p = new (__place) _Rep;
3786 __p->_M_capacity = __capacity;
3787 // ABI compatibility - 3.4.x set in _S_create both
3788 // _M_refcount and _M_length. All callers of _S_create
3789 // in basic_string.tcc then set just _M_length.
3790 // In 4.0.x and later both _M_refcount and _M_length
3791 // are initialized in the callers, unfortunately we can
3792 // have 3.4.x compiled code with _S_create callers inlined
3793 // calling 4.0.x+ _S_create.
3794 __p->_M_set_sharable();
3795 return __p;
3796 }
3797
3798 template<typename _CharT, typename _Traits, typename _Alloc>
3799 _CharT*
3800 basic_string<_CharT, _Traits, _Alloc>::_Rep::
3801 _M_clone(const _Alloc& __alloc, size_type __res)
3802 {
3803 // Requested capacity of the clone.
3804 const size_type __requested_cap = this->_M_length + __res;
3805 _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
3806 __alloc);
3807 if (this->_M_length)
3808 _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
3809
3810 __r->_M_set_length_and_sharable(this->_M_length);
3811 return __r->_M_refdata();
3812 }
3813
3814 template<typename _CharT, typename _Traits, typename _Alloc>
3815 void
3816 basic_string<_CharT, _Traits, _Alloc>::
3817 resize(size_type __n, _CharT __c)
3818 {
3819 const size_type __size = this->size();
3820 _M_check_length(__size, __n, "basic_string::resize");
3821 if (__size < __n)
3822 this->append(__n - __size, __c);
3823 else if (__n < __size)
3824 this->erase(__n);
3825 // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
3826 }
3827
3828 template<typename _CharT, typename _Traits, typename _Alloc>
3829 template<typename _InputIterator>
3830 basic_string<_CharT, _Traits, _Alloc>&
3831 basic_string<_CharT, _Traits, _Alloc>::
3832 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
3833 _InputIterator __k2, __false_type)
3834 {
3835 __glibcxx_requires_valid_range(__k1, __k2);
3836 const basic_string __s(__k1, __k2);
3837 const size_type __n1 = __i2 - __i1;
3838 _M_check_length(__n1, __s.size(), "basic_string::_M_replace_dispatch");
3839 return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
3840 __s.size());
3841 }
3842
3843 template<typename _CharT, typename _Traits, typename _Alloc>
3844 basic_string<_CharT, _Traits, _Alloc>&
3845 basic_string<_CharT, _Traits, _Alloc>::
3846 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
3847 _CharT __c)
3848 {
3849 _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
3850 _M_mutate(__pos1, __n1, __n2);
3851 if (__n2)
3852 _M_assign(_M_data() + __pos1, __n2, __c);
3853 return *this;
3854 }
3855
3856 template<typename _CharT, typename _Traits, typename _Alloc>
3857 basic_string<_CharT, _Traits, _Alloc>&
3858 basic_string<_CharT, _Traits, _Alloc>::
3859 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
3860 size_type __n2)
3861 {
3862 _M_mutate(__pos1, __n1, __n2);
3863 if (__n2)
3864 _M_copy(_M_data() + __pos1, __s, __n2);
3865 return *this;
3866 }
3867
3868 template<typename _CharT, typename _Traits, typename _Alloc>
3869 void
3870 basic_string<_CharT, _Traits, _Alloc>::
3871 reserve()
3872 {
3873#if __cpp_exceptions
3874 if (length() < capacity() || _M_rep()->_M_is_shared())
3875 try
3876 {
3877 const allocator_type __a = get_allocator();
3878 _CharT* __tmp = _M_rep()->_M_clone(__a);
3879 _M_rep()->_M_dispose(__a);
3880 _M_data(__tmp);
3881 }
3882 catch (const __cxxabiv1::__forced_unwind&)
3883 { throw; }
3884 catch (...)
3885 { /* swallow the exception */ }
3886#endif
3887 }
3888
3889 template<typename _CharT, typename _Traits, typename _Alloc>
3890 typename basic_string<_CharT, _Traits, _Alloc>::size_type
3891 basic_string<_CharT, _Traits, _Alloc>::
3892 copy(_CharT* __s, size_type __n, size_type __pos) const
3893 {
3894 _M_check(__pos, "basic_string::copy");
3895 __n = _M_limit(__pos, __n);
3896 __glibcxx_requires_string_len(__s, __n);
3897 if (__n)
3898 _M_copy(__s, _M_data() + __pos, __n);
3899 // 21.3.5.7 par 3: do not append null. (good.)
3900 return __n;
3901 }
3902
3903#ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23
3904 template<typename _CharT, typename _Traits, typename _Alloc>
3905 template<typename _Operation>
3906 [[__gnu__::__always_inline__]]
3907 inline void
3908 basic_string<_CharT, _Traits, _Alloc>::
3909 __resize_and_overwrite(const size_type __n, _Operation __op)
3910 { resize_and_overwrite<_Operation&>(__n, __op); }
3911#endif
3912
3913#if __cplusplus >= 201103L
3914 template<typename _CharT, typename _Traits, typename _Alloc>
3915 template<typename _Operation>
3916 void
3917 basic_string<_CharT, _Traits, _Alloc>::
3918#ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23
3919 resize_and_overwrite(const size_type __n, _Operation __op)
3920#else
3921 __resize_and_overwrite(const size_type __n, _Operation __op)
3922#endif
3923 {
3924 const size_type __capacity = capacity();
3925 _CharT* __p;
3926 if (__n > __capacity || _M_rep()->_M_is_shared())
3927 this->reserve(__n);
3928 __p = _M_data();
3929 struct _Terminator {
3930 ~_Terminator() { _M_this->_M_rep()->_M_set_length_and_sharable(_M_r); }
3931 basic_string* _M_this;
3932 size_type _M_r;
3933 };
3934 _Terminator __term{this, 0};
3935 auto __r = std::move(__op)(__p + 0, __n + 0);
3936#ifdef __cpp_lib_concepts
3937 static_assert(ranges::__detail::__is_integer_like<decltype(__r)>);
3938#else
3939 static_assert(__gnu_cxx::__is_integer_nonstrict<decltype(__r)>::__value,
3940 "resize_and_overwrite operation must return an integer");
3941#endif
3942 _GLIBCXX_DEBUG_ASSERT(__r >= 0 && size_type(__r) <= __n);
3943 __term._M_r = size_type(__r);
3944 if (__term._M_r > __n)
3945 __builtin_unreachable();
3946 }
3947#endif // C++11
3948
3949
3950_GLIBCXX_END_NAMESPACE_VERSION
3951} // namespace std
3952#endif // ! _GLIBCXX_USE_CXX11_ABI
3953#endif // _COW_STRING_H
atomicity.h
std::enable_if_t
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
Definition type_traits:2967
std::move
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
std::forward
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
std::min
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition stl_algobase.h:232
std
ISO C++ entities toplevel namespace is std.
std::distance
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
Definition stl_iterator_base_funcs.h:177
std::empty
constexpr auto empty(const _Container &__cont) noexcept(noexcept(__cont.empty())) -> decltype(__cont.empty())
Return whether a container is empty.
Definition range_access.h:318
std::size
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
Definition range_access.h:294
std::initializer_list
initializer_list
Definition initializer_list:51
std::basic_string_view
A non-owning reference to a string.
Definition string_view:114
std::is_same
is_same
Definition type_traits:1649
std::size_type
std::allocator_traits::allocate
static constexpr pointer allocate(_Alloc &__a, size_type __n)
Allocate memory.
Definition bits/alloc_traits.h:380
std::basic_string
Managing sequences of characters and character-like objects.
Definition basic_string.h:94
std::basic_string::compare
int compare(size_type __pos1, size_type __n1, const basic_string &__str, size_type __pos2, size_type __n2=npos) const
Compare substring to a substring.
Definition cow_string.h:3082
std::basic_string::crbegin
const_reverse_iterator crbegin() const noexcept
Definition cow_string.h:936
std::basic_string::insert
basic_string & insert(size_type __pos, const _CharT *__s, size_type __n)
Insert a C substring.
std::basic_string::swap
void swap(basic_string &__s) noexcept(/*conditional */)
Swap contents with another string.
std::basic_string::operator=
basic_string & operator=(const _CharT *__s)
Copy contents of s into this string.
Definition cow_string.h:774
std::basic_string::push_back
void push_back(_CharT __c)
Append a single character.
Definition cow_string.h:1437
std::basic_string::cend
const_iterator cend() const noexcept
Definition cow_string.h:927
std::basic_string::operator+=
basic_string & operator+=(initializer_list< _CharT > __l)
Append an initializer_list of characters.
Definition cow_string.h:1287
std::basic_string::find
size_type find(const _CharT *__s, size_type __pos=0) const noexcept
Find position of a C string.
Definition cow_string.h:2459
std::basic_string::basic_string
constexpr basic_string() noexcept(/*conditional */)
Default constructor creates an empty string.
Definition basic_string.h:619
std::basic_string::basic_string
basic_string(_InputIterator __beg, _InputIterator __end, const _Alloc &__a=_Alloc())
Construct string as copy of a range.
Definition cow_string.h:725
std::basic_string::replace
basic_string & replace(iterator __i1, iterator __i2, const _CharT *__s, size_type __n)
Replace range of characters with C substring.
Definition cow_string.h:2056
std::basic_string::assign
basic_string & assign(initializer_list< _CharT > __l)
Set value to an initializer_list of characters.
Definition cow_string.h:1569
std::basic_string::append
basic_string & append(size_type __n, _CharT __c)
Append multiple characters.
std::basic_string::find_first_of
size_type find_first_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character of string.
Definition cow_string.h:2585
std::basic_string::size
constexpr size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
Definition basic_string.h:1217
std::basic_string::erase
iterator erase(iterator __first, iterator __last)
Remove a range of characters.
Definition cow_string.h:3555
std::basic_string::clear
constexpr void clear() noexcept
Definition basic_string.h:1376
std::basic_string::data
const _CharT * data() const noexcept
Return const pointer to contents.
Definition cow_string.h:2387
std::basic_string::find_last_of
size_type find_last_of(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find last position of a character of C substring.
std::basic_string::basic_string
basic_string(const _Alloc &__a)
Construct an empty string using allocator a.
Definition cow_string.h:533
std::basic_string::find_last_not_of
constexpr size_type find_last_not_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character not in string.
Definition basic_string.h:3407
std::basic_string::find_last_of
size_type find_last_of(const _CharT *__s, size_type __pos=npos) const noexcept
Find last position of a character of C string.
Definition cow_string.h:2700
std::basic_string::compare
int compare(const _CharT *__s) const noexcept
Compare to a C string.
Definition cow_string.h:3112
std::basic_string::insert
void insert(iterator __p, initializer_list< _CharT > __l)
Insert an initializer_list of characters.
Definition cow_string.h:1674
std::basic_string::insert
basic_string & insert(size_type __pos1, const basic_string &__str)
Insert value of a string.
Definition cow_string.h:1694
std::basic_string::__resize_and_overwrite
void __resize_and_overwrite(size_type __n, _Operation __op)
Non-standard version of resize_and_overwrite for C++11 and above.
std::basic_string::rfind
size_type rfind(const _CharT *__s, size_type __pos=npos) const noexcept
Find last position of a C string.
Definition cow_string.h:2537
std::basic_string::substr
basic_string substr(size_type __pos=0, size_type __n=npos) const
Get a substring.
Definition cow_string.h:2917
std::basic_string::reserve
void reserve(size_type __res_arg)
Attempt to preallocate enough memory for specified number of characters.
std::basic_string::erase
iterator erase(iterator __position)
Remove one character.
Definition cow_string.h:1874
std::basic_string::find
constexpr size_type find(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find position of a C substring.
Definition basic_string.tcc:702
std::basic_string::find
size_type find(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a string.
Definition cow_string.h:2444
std::basic_string::find_last_not_of
size_type find_last_not_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character not in string.
Definition cow_string.h:2834
std::basic_string::copy
size_type copy(_CharT *__s, size_type __n, size_type __pos=0) const
Copy substring into C string.
std::basic_string::compare
int compare(size_type __pos, size_type __n, const basic_string &__str) const
Compare substring to a string.
Definition cow_string.h:3046
std::basic_string::basic_string
basic_string() noexcept
Default constructor creates an empty string.
Definition cow_string.h:515
std::basic_string::data
constexpr const _CharT * data() const noexcept
Return const pointer to contents.
Definition basic_string.h:2940
std::basic_string::basic_string
basic_string(const basic_string &__str)
Construct string with copy of value of str.
Definition cow_string.h:542
std::basic_string::compare
int compare(const basic_string &__str) const
Compare to a string.
Definition cow_string.h:2955
std::basic_string::compare
int compare(size_type __pos, size_type __n1, const _CharT *__s) const
Compare substring to a C string.
Definition cow_string.h:3146
std::basic_string::substr
constexpr basic_string substr(size_type __pos=0, size_type __n=npos) const
Get a substring.
Definition basic_string.h:3495
std::basic_string::assign
basic_string & assign(const _CharT *__s, size_type __n)
Set value to a C substring.
std::basic_string::compare
int compare(size_type __pos, size_type __n1, const _CharT *__s, size_type __n2) const
Compare substring against a character array.
Definition cow_string.h:3184
std::basic_string::rend
reverse_iterator rend()
Definition cow_string.h:901
std::basic_string::replace
basic_string & replace(iterator __i1, iterator __i2, const basic_string &__str)
Replace range of characters with string.
Definition cow_string.h:2037
std::basic_string::basic_string
basic_string(const basic_string &__str, size_type __pos, const _Alloc &__a=_Alloc())
Construct string as copy of a substring.
std::basic_string::basic_string
basic_string(const basic_string &__str, size_type __pos, size_type __n)
Construct string as copy of a substring.
std::basic_string::find_first_not_of
size_type find_first_not_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character not in string.
Definition cow_string.h:2752
std::basic_string::front
const_reference front() const noexcept
Definition cow_string.h:1220
std::basic_string::find_last_not_of
size_type find_last_not_of(const _CharT *__s, size_type __pos=npos) const noexcept
Find last position of a character not in C string.
Definition cow_string.h:2865
std::basic_string::insert
void insert(iterator __p, size_type __n, _CharT __c)
Insert multiple characters.
Definition cow_string.h:1619
std::basic_string::assign
constexpr basic_string & assign(const basic_string &__str)
Set value to contents of another string.
Definition basic_string.h:1794
std::basic_string::rbegin
reverse_iterator rbegin()
Definition cow_string.h:883
std::basic_string::insert
basic_string & insert(size_type __pos1, const basic_string &__str, size_type __pos2, size_type __n=npos)
Insert a substring.
Definition cow_string.h:1716
std::basic_string::basic_string
basic_string(initializer_list< _CharT > __l, const _Alloc &__a=_Alloc())
Construct string from an initializer list.
Definition cow_string.h:689
std::basic_string< _CharT >::length
constexpr size_type length() const noexcept
Definition basic_string.h:1229
std::basic_string::front
reference front()
Definition cow_string.h:1209
std::basic_string::basic_string
basic_string(const basic_string &__str, size_type __pos, size_type __n, const _Alloc &__a)
Construct string as copy of a substring.
std::basic_string::replace
basic_string & replace(iterator __i1, iterator __i2, _InputIterator __k1, _InputIterator __k2)
Replace range of characters with range.
Definition cow_string.h:2122
std::basic_string::assign
basic_string & assign(basic_string &&__str) noexcept(allocator_traits< _Alloc >::is_always_equal::value)
Set value to contents of another string.
Definition cow_string.h:1464
std::basic_string::pop_back
void pop_back()
Remove the last character.
Definition cow_string.h:1903
std::basic_string::basic_string
basic_string(basic_string &&__str) noexcept
Move construct string.
Definition cow_string.h:624
std::basic_string::copy
constexpr size_type copy(_CharT *__s, size_type __n, size_type __pos=0) const
Copy substring into C string.
Definition cow_string.h:3892
std::basic_string::length
size_type length() const noexcept
Returns the number of characters in the string, not including any null-termination.
Definition cow_string.h:967
std::basic_string::find_last_of
size_type find_last_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character of string.
Definition cow_string.h:2669
std::basic_string::cbegin
constexpr const_iterator cbegin() const noexcept
Definition basic_string.h:1178
std::basic_string::find_first_not_of
size_type find_first_not_of(_CharT __c, size_type __pos=0) const noexcept
Find position of a different character.
std::basic_string::operator+=
basic_string & operator+=(const basic_string &__str)
Append a string to this string.
Definition cow_string.h:1256
std::basic_string::size
size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
Definition cow_string.h:955
std::basic_string::rfind
size_type rfind(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a string.
Definition cow_string.h:2506
std::basic_string::find_first_of
constexpr size_type find_first_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character of string.
Definition basic_string.h:3143
std::basic_string::operator+=
basic_string & operator+=(const _CharT *__s)
Append a C string.
Definition cow_string.h:1265
std::basic_string::basic_string
basic_string(size_type __n, _CharT __c, const _Alloc &__a=_Alloc())
Construct string as multiple characters.
Definition cow_string.h:612
std::basic_string::shrink_to_fit
void shrink_to_fit() noexcept
A non-binding request to reduce capacity() to size().
Definition cow_string.h:1007
std::basic_string::resize
void resize(size_type __n, _CharT __c)
Resizes the string to the specified number of characters.
std::basic_string::reserve
constexpr void reserve()
Definition cow_string.h:3871
std::basic_string::data
_CharT * data() noexcept(false)
Return non-const pointer to contents.
Definition cow_string.h:2403
std::basic_string::find
size_type find(_CharT __c, size_type __pos=0) const noexcept
Find position of a character.
std::basic_string::at
const_reference at(size_type __n) const
Provides access to the data contained in the string.
Definition cow_string.h:1170
std::basic_string::back
const_reference back() const noexcept
Definition cow_string.h:1242
std::basic_string::rend
const_reverse_iterator rend() const noexcept
Definition cow_string.h:910
std::basic_string::end
const_iterator end() const noexcept
Definition cow_string.h:874
std::basic_string::__resize_and_overwrite
constexpr void __resize_and_overwrite(size_type __n, _Operation __op)
Non-standard version of resize_and_overwrite for C++11 and above.
std::basic_string::replace
basic_string & replace(size_type __pos, size_type __n1, const _CharT *__s, size_type __n2)
Replace characters with value of a C substring.
std::basic_string::find_last_of
size_type find_last_of(_CharT __c, size_type __pos=npos) const noexcept
Find last position of a character.
Definition cow_string.h:2720
std::basic_string::begin
iterator begin()
Definition cow_string.h:844
std::basic_string::rfind
constexpr size_type rfind(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a string.
Definition basic_string.h:3059
std::basic_string::append
constexpr basic_string & append(const basic_string &__str)
Append a string to this string.
Definition basic_string.h:1589
std::basic_string::begin
const_iterator begin() const noexcept
Definition cow_string.h:855
std::basic_string::operator=
basic_string & operator=(basic_string &&__str) noexcept(/*conditional */)
Move assign the value of str to this string.
Definition cow_string.h:800
std::basic_string::replace
basic_string & replace(iterator __i1, iterator __i2, initializer_list< _CharT > __l)
Replace range of characters with initializer_list.
Definition cow_string.h:2211
std::basic_string::operator+=
basic_string & operator+=(_CharT __c)
Append a character.
Definition cow_string.h:1274
std::basic_string::crend
const_reverse_iterator crend() const noexcept
Definition cow_string.h:945
std::basic_string::operator=
basic_string & operator=(const basic_string &__str)
Assign the value of str to this string.
Definition cow_string.h:766
std::basic_string::operator=
basic_string & operator=(_CharT __c)
Set value to string of length 1.
Definition cow_string.h:785
std::basic_string::resize
void resize(size_type __n)
Resizes the string to the specified number of characters.
Definition cow_string.h:999
std::basic_string::rbegin
const_reverse_iterator rbegin() const noexcept
Definition cow_string.h:892
std::basic_string::find_last_of
constexpr size_type find_last_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character of string.
Definition basic_string.h:3232
std::basic_string::find_last_not_of
size_type find_last_not_of(_CharT __c, size_type __pos=npos) const noexcept
Find last position of a different character.
std::basic_string< _CharT >::end
constexpr iterator end() noexcept
Definition basic_string.h:1119
std::basic_string< _CharT >::begin
constexpr iterator begin() noexcept
Definition basic_string.h:1101
std::basic_string::end
iterator end()
Definition cow_string.h:863
std::basic_string::append
basic_string & append(const _CharT *__s, size_type __n)
Append a C substring.
std::basic_string::assign
basic_string & assign(_InputIterator __first, _InputIterator __last)
Set value to a range of characters.
Definition cow_string.h:1543
std::basic_string::append
basic_string & append(initializer_list< _CharT > __l)
Append an initializer_list of characters.
Definition cow_string.h:1382
std::basic_string::append
basic_string & append(const basic_string &__str, size_type __pos, size_type __n=npos)
Append a substring.
std::basic_string::append
basic_string & append(_InputIterator __first, _InputIterator __last)
Append a range of characters.
Definition cow_string.h:1396
std::basic_string::assign
basic_string & assign(const basic_string &__str)
Set value to contents of another string.
std::basic_string::basic_string
basic_string(const _CharT *__s, const _Alloc &__a=_Alloc())
Construct string as copy of a C string.
Definition cow_string.h:601
std::basic_string::push_back
constexpr void push_back(_CharT __c)
Append a single character.
Definition basic_string.h:1778
std::basic_string::clear
void clear() noexcept
Definition cow_string.h:1094
std::basic_string::find_first_of
size_type find_first_of(_CharT __c, size_type __pos=0) const noexcept
Find position of a character.
Definition cow_string.h:2636
std::basic_string::assign
basic_string & assign(size_type __n, _CharT __c)
Set value to multiple characters.
Definition cow_string.h:1530
std::basic_string::replace
basic_string & replace(iterator __i1, iterator __i2, const _CharT *__s)
Replace range of characters with C string.
Definition cow_string.h:2077
std::basic_string::empty
bool empty() const noexcept
Definition cow_string.h:1116
std::basic_string::find_first_not_of
constexpr size_type find_first_not_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character not in string.
Definition basic_string.h:3320
std::basic_string::insert
basic_string & insert(size_type __pos, const _CharT *__s)
Insert a C string.
Definition cow_string.h:1757
std::basic_string::insert
constexpr iterator insert(const_iterator __p, size_type __n, _CharT __c)
Insert multiple characters.
Definition basic_string.h:2063
std::basic_string::assign
basic_string & assign(const basic_string &__str, size_type __pos, size_type __n=npos)
Set value to a substring of a string.
Definition cow_string.h:1486
std::basic_string::capacity
constexpr size_type capacity() const noexcept
Definition basic_string.h:1331
std::basic_string::c_str
const _CharT * c_str() const noexcept
Return const pointer to null-terminated contents.
Definition cow_string.h:2375
std::basic_string::back
reference back()
Definition cow_string.h:1231
std::basic_string::replace
basic_string & replace(size_type __pos, size_type __n1, const _CharT *__s)
Replace characters with value of a C string.
Definition cow_string.h:1995
std::basic_string::empty
constexpr bool empty() const noexcept
Definition basic_string.h:1385
std::basic_string::find
size_type find(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find position of a C substring.
std::basic_string::npos
static const size_type npos
Value returned by various member functions when they fail.
Definition basic_string.h:127
std::basic_string::erase
constexpr basic_string & erase(size_type __pos=0, size_type __n=npos)
Remove characters.
Definition basic_string.h:2377
std::basic_string::get_allocator
allocator_type get_allocator() const noexcept
Return copy of allocator used to construct this string.
Definition cow_string.h:2414
std::basic_string::assign
basic_string & assign(const _CharT *__s)
Set value to contents of a C string.
Definition cow_string.h:1514
std::basic_string::append
basic_string & append(const basic_string &__str)
Append a string to this string.
std::basic_string::find_first_not_of
size_type find_first_not_of(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find position of a character not in C substring.
std::basic_string::replace
basic_string & replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
Replace characters with multiple characters.
Definition cow_string.h:2019
std::basic_string::cbegin
const_iterator cbegin() const noexcept
Definition cow_string.h:919
std::basic_string< _CharT >::get_allocator
constexpr allocator_type get_allocator() const noexcept
Definition basic_string.h:2961
std::basic_string::replace
constexpr basic_string & replace(size_type __pos, size_type __n, const basic_string &__str)
Replace characters with value from another string.
Definition basic_string.h:2463
std::basic_string::replace
basic_string & replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
Replace range of characters with multiple characters.
Definition cow_string.h:2098
std::basic_string::operator=
basic_string & operator=(initializer_list< _CharT > __l)
Set value to string constructed from initializer list.
Definition cow_string.h:813
std::basic_string::~basic_string
~basic_string() noexcept
Destroy the string instance.
Definition cow_string.h:758
std::basic_string::capacity
size_type capacity() const noexcept
Definition cow_string.h:1059
std::basic_string::find_first_of
size_type find_first_of(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find position of a character of C substring.
std::basic_string::subview
constexpr basic_string_view< _CharT, _Traits > subview(size_type __pos=0, size_type __n=npos) const
Get a subview.
Definition cow_string.h:2936
std::basic_string::insert
void insert(iterator __p, _InputIterator __beg, _InputIterator __end)
Insert a range of characters.
Definition cow_string.h:1636
std::basic_string::compare
constexpr int compare(const basic_string &__str) const
Compare to a string.
Definition basic_string.h:3546
std::basic_string::find_first_of
size_type find_first_of(const _CharT *__s, size_type __pos=0) const noexcept
Find position of a character of C string.
Definition cow_string.h:2616
std::basic_string::rfind
size_type rfind(_CharT __c, size_type __pos=npos) const noexcept
Find last position of a character.
std::basic_string::max_size
size_type max_size() const noexcept
Returns the size() of the largest possible string.
Definition cow_string.h:972
std::basic_string::operator[]
reference operator[](size_type __pos)
Subscript access to the data contained in the string.
Definition cow_string.h:1148
std::basic_string::insert
basic_string & insert(size_type __pos, size_type __n, _CharT __c)
Insert multiple characters.
Definition cow_string.h:1780
std::basic_string::erase
basic_string & erase(size_type __pos=0, size_type __n=npos)
Remove characters.
Definition cow_string.h:1858
std::basic_string::replace
basic_string & replace(size_type __pos1, size_type __n1, const basic_string &__str, size_type __pos2, size_type __n2=npos)
Replace characters with value from another string.
Definition cow_string.h:1950
std::basic_string::max_size
constexpr size_type max_size() const noexcept
Returns the size() of the largest possible string.
Definition basic_string.h:1235
std::basic_string::basic_string
basic_string(const _CharT *__s, size_type __n, const _Alloc &__a=_Alloc())
Construct string initialized by a character array.
Definition cow_string.h:586
std::basic_string::swap
constexpr void swap(basic_string &__s) noexcept
Swap contents with another string.
Definition cow_string.h:3695
std::basic_string::append
basic_string & append(const _CharT *__s)
Append a C string.
Definition cow_string.h:1342
std::basic_string::operator[]
constexpr const_reference operator[](size_type __pos) const noexcept
Subscript access to the data contained in the string.
Definition basic_string.h:1401
std::basic_string::replace
basic_string & replace(size_type __pos, size_type __n, const basic_string &__str)
Replace characters with value from another string.
Definition cow_string.h:1928
std::basic_string::rfind
size_type rfind(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find last position of a C substring.
std::basic_string::operator+=
constexpr basic_string & operator+=(const basic_string &__str)
Append a string to this string.
Definition basic_string.h:1531
std::basic_string::find_first_not_of
size_type find_first_not_of(const _CharT *__s, size_type __pos=0) const noexcept
Find position of a character not in C string.
Definition cow_string.h:2783
std::basic_string::find_last_not_of
size_type find_last_not_of(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find last position of a character not in C substring.
std::basic_string::cend
constexpr const_iterator cend() const noexcept
Definition basic_string.h:1187
std::basic_string::at
reference at(size_type __n)
Provides access to the data contained in the string.
Definition cow_string.h:1192
std::basic_string::insert
iterator insert(iterator __p, _CharT __c)
Insert one character.
Definition cow_string.h:1798
__cxxabiv1::__forced_unwind
Thrown as part of forced unwinding.
Definition cxxabi_forced.h:51
std::input_iterator_tag
Marking input iterators.
Definition stl_iterator_base_types.h:95
std::forward_iterator_tag
Forward iterators support a superset of input iterator operations.
Definition stl_iterator_base_types.h:101
std::iterator
Common iterator class.
Definition stl_iterator_base_types.h:133
std::ranges::sized_range
[range.sized] The sized_range concept.
Definition ranges_base.h:553
std::ranges::forward_range
A range for which ranges::begin returns a forward iterator.
Definition ranges_base.h:624
std::ranges::contiguous_range
A range for which ranges::begin returns a contiguous iterator.
Definition ranges_base.h:639