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
1431 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1432 // 3662. basic_string::append/assign(NTBS, pos, n) suboptimal
1433 /**
1434 * @brief Append a C substring.
1435 * @param __s The C string to append.
1436 * @param __pos The position in the C string to append from.
1437 * @param __n The number of characters to append.
1438 * @return Reference to this string.
1439 */
1440 basic_string&
1441 append(const _CharT* __s, size_type __pos, size_type __n)
1442 { return append(__sv_type(__s).substr(__pos, __n)); }
1443#endif // C++17
1444
1445 /**
1446 * @brief Append a single character.
1447 * @param __c Character to append.
1448 */
1449 void
1450 push_back(_CharT __c)
1451 {
1452 const size_type __len = 1 + this->size();
1453 if (__len > this->capacity() || _M_rep()->_M_is_shared())
1454 this->reserve(__len);
1455 traits_type::assign(_M_data()[this->size()], __c);
1456 _M_rep()->_M_set_length_and_sharable(__len);
1457 }
1458
1459 /**
1460 * @brief Set value to contents of another string.
1461 * @param __str Source string to use.
1462 * @return Reference to this string.
1463 */
1464 basic_string&
1465 assign(const basic_string& __str);
1466
1467#if __cplusplus >= 201103L
1468 /**
1469 * @brief Set value to contents of another string.
1470 * @param __str Source string to use.
1471 * @return Reference to this string.
1472 *
1473 * This function sets this string to the exact contents of @a __str.
1474 * @a __str is a valid, but unspecified string.
1475 */
1476 basic_string&
1477 assign(basic_string&& __str)
1478 noexcept(allocator_traits<_Alloc>::is_always_equal::value)
1479 {
1480 this->swap(__str);
1481 return *this;
1482 }
1483#endif // C++11
1484
1485 /**
1486 * @brief Set value to a substring of a string.
1487 * @param __str The string to use.
1488 * @param __pos Index of the first character of str.
1489 * @param __n Number of characters to use.
1490 * @return Reference to this string.
1491 * @throw std::out_of_range if @a pos is not a valid index.
1492 *
1493 * This function sets this string to the substring of @a __str
1494 * consisting of @a __n characters at @a __pos. If @a __n is
1495 * is larger than the number of available characters in @a
1496 * __str, the remainder of @a __str is used.
1497 */
1498 basic_string&
1499 assign(const basic_string& __str, size_type __pos, size_type __n = npos)
1500 { return this->assign(__str._M_data()
1501 + __str._M_check(__pos, "basic_string::assign"),
1502 __str._M_limit(__pos, __n)); }
1503
1504 /**
1505 * @brief Set value to a C substring.
1506 * @param __s The C string to use.
1507 * @param __n Number of characters to use.
1508 * @return Reference to this string.
1509 *
1510 * This function sets the value of this string to the first @a __n
1511 * characters of @a __s. If @a __n is is larger than the number of
1512 * available characters in @a __s, the remainder of @a __s is used.
1513 */
1514 basic_string&
1515 assign(const _CharT* __s, size_type __n);
1516
1517 /**
1518 * @brief Set value to contents of a C string.
1519 * @param __s The C string to use.
1520 * @return Reference to this string.
1521 *
1522 * This function sets the value of this string to the value of @a __s.
1523 * The data is copied, so there is no dependence on @a __s once the
1524 * function returns.
1525 */
1526 basic_string&
1527 assign(const _CharT* __s)
1528 {
1529 __glibcxx_requires_string(__s);
1530 return this->assign(__s, traits_type::length(__s));
1531 }
1532
1533 /**
1534 * @brief Set value to multiple characters.
1535 * @param __n Length of the resulting string.
1536 * @param __c The character to use.
1537 * @return Reference to this string.
1538 *
1539 * This function sets the value of this string to @a __n copies of
1540 * character @a __c.
1541 */
1542 basic_string&
1543 assign(size_type __n, _CharT __c)
1544 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
1545
1546 /**
1547 * @brief Set value to a range of characters.
1548 * @param __first Iterator referencing the first character to append.
1549 * @param __last Iterator marking the end of the range.
1550 * @return Reference to this string.
1551 *
1552 * Sets value of string to characters in the range [__first,__last).
1553 */
1554 template<class _InputIterator>
1555 basic_string&
1556 assign(_InputIterator __first, _InputIterator __last)
1557 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
1558
1559#if __glibcxx_containers_ranges // C++ >= 23
1560 /**
1561 * @brief Set value to a range of characters.
1562 * @since C++23
1563 */
1564 template<__detail::__container_compatible_range<_CharT> _Rg>
1565 basic_string&
1566 assign_range(_Rg&& __rg)
1567 {
1568 basic_string __s(from_range, std::forward<_Rg>(__rg),
1569 get_allocator());
1570 assign(std::move(__s));
1571 return *this;
1572 }
1573#endif
1574
1575#if __cplusplus >= 201103L
1576 /**
1577 * @brief Set value to an initializer_list of characters.
1578 * @param __l The initializer_list of characters to assign.
1579 * @return Reference to this string.
1580 */
1581 basic_string&
1582 assign(initializer_list<_CharT> __l)
1583 { return this->assign(__l.begin(), __l.size()); }
1584#endif // C++11
1585
1586#ifdef __glibcxx_string_view // >= C++17
1587 /**
1588 * @brief Set value from a string_view.
1589 * @param __svt The source object convertible to string_view.
1590 * @return Reference to this string.
1591 */
1592 template<typename _Tp>
1593 _If_sv<_Tp, basic_string&>
1594 assign(const _Tp& __svt)
1595 {
1596 __sv_type __sv = __svt;
1597 return this->assign(__sv.data(), __sv.size());
1598 }
1599
1600 /**
1601 * @brief Set value from a range of characters in a string_view.
1602 * @param __svt The source object convertible to string_view.
1603 * @param __pos The position in the string_view to assign from.
1604 * @param __n The number of characters to assign.
1605 * @return Reference to this string.
1606 */
1607 template<typename _Tp>
1608 _If_sv<_Tp, basic_string&>
1609 assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
1610 {
1611 __sv_type __sv = __svt;
1612 return assign(__sv.data()
1613 + std::__sv_check(__sv.size(), __pos, "basic_string::assign"),
1614 std::__sv_limit(__sv.size(), __pos, __n));
1615 }
1616
1617 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1618 // 3662. basic_string::append/assign(NTBS, pos, n) suboptimal
1619 /**
1620 * @brief Set value to a C substring.
1621 * @param __s The C string to use.
1622 * @param __pos The position in the C string to assign from.
1623 * @param __n Number of characters to use.
1624 * @return Reference to this string.
1625 */
1626 basic_string&
1627 assign(const _CharT* __s, size_type __pos, size_type __n)
1628 { return assign(__sv_type(__s).substr(__pos, __n)); }
1629#endif // C++17
1630
1631 /**
1632 * @brief Insert multiple characters.
1633 * @param __p Iterator referencing location in string to insert at.
1634 * @param __n Number of characters to insert
1635 * @param __c The character to insert.
1636 * @throw std::length_error If new length exceeds @c max_size().
1637 *
1638 * Inserts @a __n copies of character @a __c starting at the
1639 * position referenced by iterator @a __p. If adding
1640 * characters causes the length to exceed max_size(),
1641 * length_error is thrown. The value of the string doesn't
1642 * change if an error is thrown.
1643 */
1644 void
1645 insert(iterator __p, size_type __n, _CharT __c)
1646 { this->replace(__p, __p, __n, __c); }
1647
1648 /**
1649 * @brief Insert a range of characters.
1650 * @param __p Iterator referencing location in string to insert at.
1651 * @param __beg Start of range.
1652 * @param __end End of range.
1653 * @throw std::length_error If new length exceeds @c max_size().
1654 *
1655 * Inserts characters in range [__beg,__end). If adding
1656 * characters causes the length to exceed max_size(),
1657 * length_error is thrown. The value of the string doesn't
1658 * change if an error is thrown.
1659 */
1660 template<class _InputIterator>
1661 void
1662 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
1663 { this->replace(__p, __p, __beg, __end); }
1664
1665#if __glibcxx_containers_ranges // C++ >= 23
1666 /**
1667 * @brief Insert a range into the string.
1668 * @since C++23
1669 */
1670 template<__detail::__container_compatible_range<_CharT> _Rg>
1671 iterator
1672 insert_range(const_iterator __p, _Rg&& __rg)
1673 {
1674 auto __pos = __p - cbegin();
1675
1676 if constexpr (ranges::forward_range<_Rg>)
1677 if (ranges::empty(__rg))
1678 return begin() + __pos;
1679
1680 if (__p == cend())
1681 append_range(std::forward<_Rg>(__rg));
1682 else
1683 {
1684 basic_string __s(from_range, std::forward<_Rg>(__rg),
1685 get_allocator());
1686 insert(__pos, __s);
1687 }
1688 return begin() + __pos;
1689 }
1690#endif
1691
1692#if __cplusplus >= 201103L
1693 /**
1694 * @brief Insert an initializer_list of characters.
1695 * @param __p Iterator referencing location in string to insert at.
1696 * @param __l The initializer_list of characters to insert.
1697 * @throw std::length_error If new length exceeds @c max_size().
1698 */
1699 void
1700 insert(iterator __p, initializer_list<_CharT> __l)
1701 {
1702 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1703 this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
1704 }
1705#endif // C++11
1706
1707 /**
1708 * @brief Insert value of a string.
1709 * @param __pos1 Position in string to insert at.
1710 * @param __str The string to insert.
1711 * @return Reference to this string.
1712 * @throw std::length_error If new length exceeds @c max_size().
1713 *
1714 * Inserts value of @a __str starting at @a __pos1. If adding
1715 * characters causes the length to exceed max_size(),
1716 * length_error is thrown. The value of the string doesn't
1717 * change if an error is thrown.
1718 */
1719 basic_string&
1720 insert(size_type __pos1, const basic_string& __str)
1721 { return this->insert(__pos1, __str, size_type(0), __str.size()); }
1722
1723 /**
1724 * @brief Insert a substring.
1725 * @param __pos1 Position in string to insert at.
1726 * @param __str The string to insert.
1727 * @param __pos2 Start of characters in str to insert.
1728 * @param __n Number of characters to insert.
1729 * @return Reference to this string.
1730 * @throw std::length_error If new length exceeds @c max_size().
1731 * @throw std::out_of_range If @a pos1 > size() or
1732 * @a __pos2 > @a str.size().
1733 *
1734 * Starting at @a pos1, insert @a __n character of @a __str
1735 * beginning with @a __pos2. If adding characters causes the
1736 * length to exceed max_size(), length_error is thrown. If @a
1737 * __pos1 is beyond the end of this string or @a __pos2 is
1738 * beyond the end of @a __str, out_of_range is thrown. The
1739 * value of the string doesn't change if an error is thrown.
1740 */
1741 basic_string&
1742 insert(size_type __pos1, const basic_string& __str,
1743 size_type __pos2, size_type __n = npos)
1744 { return this->insert(__pos1, __str._M_data()
1745 + __str._M_check(__pos2, "basic_string::insert"),
1746 __str._M_limit(__pos2, __n)); }
1747
1748 /**
1749 * @brief Insert a C substring.
1750 * @param __pos Position in string to insert at.
1751 * @param __s The C string to insert.
1752 * @param __n The number of characters to insert.
1753 * @return Reference to this string.
1754 * @throw std::length_error If new length exceeds @c max_size().
1755 * @throw std::out_of_range If @a __pos is beyond the end of this
1756 * string.
1757 *
1758 * Inserts the first @a __n characters of @a __s starting at @a
1759 * __pos. If adding characters causes the length to exceed
1760 * max_size(), length_error is thrown. If @a __pos is beyond
1761 * end(), out_of_range is thrown. The value of the string
1762 * doesn't change if an error is thrown.
1763 */
1764 basic_string&
1765 insert(size_type __pos, const _CharT* __s, size_type __n);
1766
1767 /**
1768 * @brief Insert a C string.
1769 * @param __pos Position in string to insert at.
1770 * @param __s The C string to insert.
1771 * @return Reference to this string.
1772 * @throw std::length_error If new length exceeds @c max_size().
1773 * @throw std::out_of_range If @a pos is beyond the end of this
1774 * string.
1775 *
1776 * Inserts the first @a n characters of @a __s starting at @a __pos. If
1777 * adding characters causes the length to exceed max_size(),
1778 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
1779 * thrown. The value of the string doesn't change if an error is
1780 * thrown.
1781 */
1782 basic_string&
1783 insert(size_type __pos, const _CharT* __s)
1784 {
1785 __glibcxx_requires_string(__s);
1786 return this->insert(__pos, __s, traits_type::length(__s));
1787 }
1788
1789 /**
1790 * @brief Insert multiple characters.
1791 * @param __pos Index in string to insert at.
1792 * @param __n Number of characters to insert
1793 * @param __c The character to insert.
1794 * @return Reference to this string.
1795 * @throw std::length_error If new length exceeds @c max_size().
1796 * @throw std::out_of_range If @a __pos is beyond the end of this
1797 * string.
1798 *
1799 * Inserts @a __n copies of character @a __c starting at index
1800 * @a __pos. If adding characters causes the length to exceed
1801 * max_size(), length_error is thrown. If @a __pos > length(),
1802 * out_of_range is thrown. The value of the string doesn't
1803 * change if an error is thrown.
1804 */
1805 basic_string&
1806 insert(size_type __pos, size_type __n, _CharT __c)
1807 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1808 size_type(0), __n, __c); }
1809
1810 /**
1811 * @brief Insert one character.
1812 * @param __p Iterator referencing position in string to insert at.
1813 * @param __c The character to insert.
1814 * @return Iterator referencing newly inserted char.
1815 * @throw std::length_error If new length exceeds @c max_size().
1816 *
1817 * Inserts character @a __c at position referenced by @a __p.
1818 * If adding character causes the length to exceed max_size(),
1819 * length_error is thrown. If @a __p is beyond end of string,
1820 * out_of_range is thrown. The value of the string doesn't
1821 * change if an error is thrown.
1822 */
1823 iterator
1824 insert(iterator __p, _CharT __c)
1825 {
1826 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
1827 const size_type __pos = __p - _M_ibegin();
1828 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1829 _M_rep()->_M_set_leaked();
1830 return iterator(_M_data() + __pos);
1831 }
1832
1833#ifdef __glibcxx_string_view // >= C++17
1834 /**
1835 * @brief Insert a string_view.
1836 * @param __pos Position in string to insert at.
1837 * @param __svt The object convertible to string_view to insert.
1838 * @return Reference to this string.
1839 */
1840 template<typename _Tp>
1841 _If_sv<_Tp, basic_string&>
1842 insert(size_type __pos, const _Tp& __svt)
1843 {
1844 __sv_type __sv = __svt;
1845 return this->insert(__pos, __sv.data(), __sv.size());
1846 }
1847
1848 /**
1849 * @brief Insert a string_view.
1850 * @param __pos1 Position in string to insert at.
1851 * @param __svt The object convertible to string_view to insert from.
1852 * @param __pos2 Position in string_view to insert from.
1853 * @param __n The number of characters to insert.
1854 * @return Reference to this string.
1855 */
1856 template<typename _Tp>
1857 _If_sv<_Tp, basic_string&>
1858 insert(size_type __pos1, const _Tp& __svt,
1859 size_type __pos2, size_type __n = npos)
1860 {
1861 __sv_type __sv = __svt;
1862 return this->replace(__pos1, size_type(0), __sv.data()
1863 + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"),
1864 std::__sv_limit(__sv.size(), __pos2, __n));
1865 }
1866#endif // C++17
1867
1868 /**
1869 * @brief Remove characters.
1870 * @param __pos Index of first character to remove (default 0).
1871 * @param __n Number of characters to remove (default remainder).
1872 * @return Reference to this string.
1873 * @throw std::out_of_range If @a pos is beyond the end of this
1874 * string.
1875 *
1876 * Removes @a __n characters from this string starting at @a
1877 * __pos. The length of the string is reduced by @a __n. If
1878 * there are < @a __n characters to remove, the remainder of
1879 * the string is truncated. If @a __p is beyond end of string,
1880 * out_of_range is thrown. The value of the string doesn't
1881 * change if an error is thrown.
1882 */
1883 basic_string&
1884 erase(size_type __pos = 0, size_type __n = npos)
1885 {
1886 _M_mutate(_M_check(__pos, "basic_string::erase"),
1887 _M_limit(__pos, __n), size_type(0));
1888 return *this;
1889 }
1890
1891 /**
1892 * @brief Remove one character.
1893 * @param __position Iterator referencing the character to remove.
1894 * @return iterator referencing same location after removal.
1895 *
1896 * Removes the character at @a __position from this string. The value
1897 * of the string doesn't change if an error is thrown.
1898 */
1899 iterator
1900 erase(iterator __position)
1901 {
1902 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
1903 && __position < _M_iend());
1904 const size_type __pos = __position - _M_ibegin();
1905 _M_mutate(__pos, size_type(1), size_type(0));
1906 _M_rep()->_M_set_leaked();
1907 return iterator(_M_data() + __pos);
1908 }
1909
1910 /**
1911 * @brief Remove a range of characters.
1912 * @param __first Iterator referencing the first character to remove.
1913 * @param __last Iterator referencing the end of the range.
1914 * @return Iterator referencing location of first after removal.
1915 *
1916 * Removes the characters in the range [first,last) from this string.
1917 * The value of the string doesn't change if an error is thrown.
1918 */
1919 iterator
1920 erase(iterator __first, iterator __last);
1921
1922#if __cplusplus >= 201103L
1923 /**
1924 * @brief Remove the last character.
1925 *
1926 * The string must be non-empty.
1927 */
1928 void
1929 pop_back() // FIXME C++11: should be noexcept.
1930 {
1931 __glibcxx_assert(!empty());
1932 erase(size() - 1, 1);
1933 }
1934#endif // C++11
1935
1936 /**
1937 * @brief Replace characters with value from another string.
1938 * @param __pos Index of first character to replace.
1939 * @param __n Number of characters to be replaced.
1940 * @param __str String to insert.
1941 * @return Reference to this string.
1942 * @throw std::out_of_range If @a pos is beyond the end of this
1943 * string.
1944 * @throw std::length_error If new length exceeds @c max_size().
1945 *
1946 * Removes the characters in the range [__pos,__pos+__n) from
1947 * this string. In place, the value of @a __str is inserted.
1948 * If @a __pos is beyond end of string, out_of_range is thrown.
1949 * If the length of the result exceeds max_size(), length_error
1950 * is thrown. The value of the string doesn't change if an
1951 * error is thrown.
1952 */
1953 basic_string&
1954 replace(size_type __pos, size_type __n, const basic_string& __str)
1955 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1956
1957 /**
1958 * @brief Replace characters with value from another string.
1959 * @param __pos1 Index of first character to replace.
1960 * @param __n1 Number of characters to be replaced.
1961 * @param __str String to insert.
1962 * @param __pos2 Index of first character of str to use.
1963 * @param __n2 Number of characters from str to use.
1964 * @return Reference to this string.
1965 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
1966 * __str.size().
1967 * @throw std::length_error If new length exceeds @c max_size().
1968 *
1969 * Removes the characters in the range [__pos1,__pos1 + n) from this
1970 * string. In place, the value of @a __str is inserted. If @a __pos is
1971 * beyond end of string, out_of_range is thrown. If the length of the
1972 * result exceeds max_size(), length_error is thrown. The value of the
1973 * string doesn't change if an error is thrown.
1974 */
1975 basic_string&
1976 replace(size_type __pos1, size_type __n1, const basic_string& __str,
1977 size_type __pos2, size_type __n2 = npos)
1978 { return this->replace(__pos1, __n1, __str._M_data()
1979 + __str._M_check(__pos2, "basic_string::replace"),
1980 __str._M_limit(__pos2, __n2)); }
1981
1982 /**
1983 * @brief Replace characters with value of a C substring.
1984 * @param __pos Index of first character to replace.
1985 * @param __n1 Number of characters to be replaced.
1986 * @param __s C string to insert.
1987 * @param __n2 Number of characters from @a s to use.
1988 * @return Reference to this string.
1989 * @throw std::out_of_range If @a pos1 > size().
1990 * @throw std::length_error If new length exceeds @c max_size().
1991 *
1992 * Removes the characters in the range [__pos,__pos + __n1)
1993 * from this string. In place, the first @a __n2 characters of
1994 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
1995 * @a __pos is beyond end of string, out_of_range is thrown. If
1996 * the length of result exceeds max_size(), length_error is
1997 * thrown. The value of the string doesn't change if an error
1998 * is thrown.
1999 */
2000 basic_string&
2001 replace(size_type __pos, size_type __n1, const _CharT* __s,
2002 size_type __n2);
2003
2004 /**
2005 * @brief Replace characters with value of a C string.
2006 * @param __pos Index of first character to replace.
2007 * @param __n1 Number of characters to be replaced.
2008 * @param __s C string to insert.
2009 * @return Reference to this string.
2010 * @throw std::out_of_range If @a pos > size().
2011 * @throw std::length_error If new length exceeds @c max_size().
2012 *
2013 * Removes the characters in the range [__pos,__pos + __n1)
2014 * from this string. In place, the characters of @a __s are
2015 * inserted. If @a __pos is beyond end of string, out_of_range
2016 * is thrown. If the length of result exceeds max_size(),
2017 * length_error is thrown. The value of the string doesn't
2018 * change if an error is thrown.
2019 */
2020 basic_string&
2021 replace(size_type __pos, size_type __n1, const _CharT* __s)
2022 {
2023 __glibcxx_requires_string(__s);
2024 return this->replace(__pos, __n1, __s, traits_type::length(__s));
2025 }
2026
2027 /**
2028 * @brief Replace characters with multiple characters.
2029 * @param __pos Index of first character to replace.
2030 * @param __n1 Number of characters to be replaced.
2031 * @param __n2 Number of characters to insert.
2032 * @param __c Character to insert.
2033 * @return Reference to this string.
2034 * @throw std::out_of_range If @a __pos > size().
2035 * @throw std::length_error If new length exceeds @c max_size().
2036 *
2037 * Removes the characters in the range [pos,pos + n1) from this
2038 * string. In place, @a __n2 copies of @a __c are inserted.
2039 * If @a __pos is beyond end of string, out_of_range is thrown.
2040 * If the length of result exceeds max_size(), length_error is
2041 * thrown. The value of the string doesn't change if an error
2042 * is thrown.
2043 */
2044 basic_string&
2045 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
2046 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
2047 _M_limit(__pos, __n1), __n2, __c); }
2048
2049 /**
2050 * @brief Replace range of characters with string.
2051 * @param __i1 Iterator referencing start of range to replace.
2052 * @param __i2 Iterator referencing end of range to replace.
2053 * @param __str String value to insert.
2054 * @return Reference to this string.
2055 * @throw std::length_error If new length exceeds @c max_size().
2056 *
2057 * Removes the characters in the range [__i1,__i2). In place,
2058 * the value of @a __str is inserted. If the length of result
2059 * exceeds max_size(), length_error is thrown. The value of
2060 * the string doesn't change if an error is thrown.
2061 */
2062 basic_string&
2063 replace(iterator __i1, iterator __i2, const basic_string& __str)
2064 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
2065
2066 /**
2067 * @brief Replace range of characters with C substring.
2068 * @param __i1 Iterator referencing start of range to replace.
2069 * @param __i2 Iterator referencing end of range to replace.
2070 * @param __s C string value to insert.
2071 * @param __n Number of characters from s to insert.
2072 * @return Reference to this string.
2073 * @throw std::length_error If new length exceeds @c max_size().
2074 *
2075 * Removes the characters in the range [__i1,__i2). In place,
2076 * the first @a __n characters of @a __s are inserted. If the
2077 * length of result exceeds max_size(), length_error is thrown.
2078 * The value of the string doesn't change if an error is
2079 * thrown.
2080 */
2081 basic_string&
2082 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
2083 {
2084 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2085 && __i2 <= _M_iend());
2086 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
2087 }
2088
2089 /**
2090 * @brief Replace range of characters with C string.
2091 * @param __i1 Iterator referencing start of range to replace.
2092 * @param __i2 Iterator referencing end of range to replace.
2093 * @param __s C string value to insert.
2094 * @return Reference to this string.
2095 * @throw std::length_error If new length exceeds @c max_size().
2096 *
2097 * Removes the characters in the range [__i1,__i2). In place,
2098 * the characters of @a __s are inserted. If the length of
2099 * result exceeds max_size(), length_error is thrown. The
2100 * value of the string doesn't change if an error is thrown.
2101 */
2102 basic_string&
2103 replace(iterator __i1, iterator __i2, const _CharT* __s)
2104 {
2105 __glibcxx_requires_string(__s);
2106 return this->replace(__i1, __i2, __s, traits_type::length(__s));
2107 }
2108
2109 /**
2110 * @brief Replace range of characters with multiple characters
2111 * @param __i1 Iterator referencing start of range to replace.
2112 * @param __i2 Iterator referencing end of range to replace.
2113 * @param __n Number of characters to insert.
2114 * @param __c Character to insert.
2115 * @return Reference to this string.
2116 * @throw std::length_error If new length exceeds @c max_size().
2117 *
2118 * Removes the characters in the range [__i1,__i2). In place,
2119 * @a __n copies of @a __c are inserted. If the length of
2120 * result exceeds max_size(), length_error is thrown. The
2121 * value of the string doesn't change if an error is thrown.
2122 */
2123 basic_string&
2124 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
2125 {
2126 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2127 && __i2 <= _M_iend());
2128 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
2129 }
2130
2131 /**
2132 * @brief Replace range of characters with range.
2133 * @param __i1 Iterator referencing start of range to replace.
2134 * @param __i2 Iterator referencing end of range to replace.
2135 * @param __k1 Iterator referencing start of range to insert.
2136 * @param __k2 Iterator referencing end of range to insert.
2137 * @return Reference to this string.
2138 * @throw std::length_error If new length exceeds @c max_size().
2139 *
2140 * Removes the characters in the range [__i1,__i2). In place,
2141 * characters in the range [__k1,__k2) are inserted. If the
2142 * length of result exceeds max_size(), length_error is thrown.
2143 * The value of the string doesn't change if an error is
2144 * thrown.
2145 */
2146 template<class _InputIterator>
2147 basic_string&
2148 replace(iterator __i1, iterator __i2,
2149 _InputIterator __k1, _InputIterator __k2)
2150 {
2151 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2152 && __i2 <= _M_iend());
2153 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
2154 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
2155 }
2156
2157 // Specializations for the common case of pointer and iterator:
2158 // useful to avoid the overhead of temporary buffering in _M_replace.
2159 basic_string&
2160 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
2161 {
2162 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2163 && __i2 <= _M_iend());
2164 __glibcxx_requires_valid_range(__k1, __k2);
2165 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
2166 __k1, __k2 - __k1);
2167 }
2168
2169 basic_string&
2170 replace(iterator __i1, iterator __i2,
2171 const _CharT* __k1, const _CharT* __k2)
2172 {
2173 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2174 && __i2 <= _M_iend());
2175 __glibcxx_requires_valid_range(__k1, __k2);
2176 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
2177 __k1, __k2 - __k1);
2178 }
2179
2180 basic_string&
2181 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
2182 {
2183 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2184 && __i2 <= _M_iend());
2185 __glibcxx_requires_valid_range(__k1, __k2);
2186 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
2187 __k1.base(), __k2 - __k1);
2188 }
2189
2190 basic_string&
2191 replace(iterator __i1, iterator __i2,
2192 const_iterator __k1, const_iterator __k2)
2193 {
2194 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
2195 && __i2 <= _M_iend());
2196 __glibcxx_requires_valid_range(__k1, __k2);
2197 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
2198 __k1.base(), __k2 - __k1);
2199 }
2200
2201#if __glibcxx_containers_ranges // C++ >= 23
2202 /**
2203 * @brief Replace part of the string with a range.
2204 * @since C++23
2205 */
2206 template<__detail::__container_compatible_range<_CharT> _Rg>
2207 basic_string&
2208 replace_with_range(const_iterator __i1, const_iterator __i2, _Rg&& __rg)
2209 {
2210 if (__i1 == cend())
2211 append_range(std::forward<_Rg>(__rg));
2212 else
2213 {
2214 basic_string __s(from_range, std::forward<_Rg>(__rg),
2215 get_allocator());
2216 replace(__i1 - cbegin(), __i2 - __i1, __s);
2217 }
2218 return *this;
2219 }
2220#endif
2221
2222#if __cplusplus >= 201103L
2223 /**
2224 * @brief Replace range of characters with initializer_list.
2225 * @param __i1 Iterator referencing start of range to replace.
2226 * @param __i2 Iterator referencing end of range to replace.
2227 * @param __l The initializer_list of characters to insert.
2228 * @return Reference to this string.
2229 * @throw std::length_error If new length exceeds @c max_size().
2230 *
2231 * Removes the characters in the range [__i1,__i2). In place,
2232 * characters in the range [__k1,__k2) are inserted. If the
2233 * length of result exceeds max_size(), length_error is thrown.
2234 * The value of the string doesn't change if an error is
2235 * thrown.
2236 */
2237 basic_string& replace(iterator __i1, iterator __i2,
2238 initializer_list<_CharT> __l)
2239 { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
2240#endif // C++11
2241
2242#ifdef __glibcxx_string_view // >= C++17
2243 /**
2244 * @brief Replace range of characters with string_view.
2245 * @param __pos The position to replace at.
2246 * @param __n The number of characters to replace.
2247 * @param __svt The object convertible to string_view to insert.
2248 * @return Reference to this string.
2249 */
2250 template<typename _Tp>
2251 _If_sv<_Tp, basic_string&>
2252 replace(size_type __pos, size_type __n, const _Tp& __svt)
2253 {
2254 __sv_type __sv = __svt;
2255 return this->replace(__pos, __n, __sv.data(), __sv.size());
2256 }
2257
2258 /**
2259 * @brief Replace range of characters with string_view.
2260 * @param __pos1 The position to replace at.
2261 * @param __n1 The number of characters to replace.
2262 * @param __svt The object convertible to string_view to insert from.
2263 * @param __pos2 The position in the string_view to insert from.
2264 * @param __n2 The number of characters to insert.
2265 * @return Reference to this string.
2266 */
2267 template<typename _Tp>
2268 _If_sv<_Tp, basic_string&>
2269 replace(size_type __pos1, size_type __n1, const _Tp& __svt,
2270 size_type __pos2, size_type __n2 = npos)
2271 {
2272 __sv_type __sv = __svt;
2273 return this->replace(__pos1, __n1,
2274 __sv.data()
2275 + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"),
2276 std::__sv_limit(__sv.size(), __pos2, __n2));
2277 }
2278
2279 /**
2280 * @brief Replace range of characters with string_view.
2281 * @param __i1 An iterator referencing the start position
2282 * to replace at.
2283 * @param __i2 An iterator referencing the end position
2284 * for the replace.
2285 * @param __svt The object convertible to string_view to insert from.
2286 * @return Reference to this string.
2287 */
2288 template<typename _Tp>
2289 _If_sv<_Tp, basic_string&>
2290 replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
2291 {
2292 __sv_type __sv = __svt;
2293 return this->replace(__i1 - begin(), __i2 - __i1, __sv);
2294 }
2295#endif // C++17
2296
2297 private:
2298 template<class _Integer>
2299 basic_string&
2300 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
2301 _Integer __val, __true_type)
2302 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
2303
2304 template<class _InputIterator>
2305 basic_string&
2306 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
2307 _InputIterator __k2, __false_type);
2308
2309 basic_string&
2310 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
2311 _CharT __c);
2312
2313 basic_string&
2314 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
2315 size_type __n2);
2316
2317 // _S_construct_aux is used to implement the 21.3.1 para 15 which
2318 // requires special behaviour if _InIter is an integral type
2319 template<class _InIterator>
2320 static _CharT*
2321 _S_construct_aux(_InIterator __beg, _InIterator __end,
2322 const _Alloc& __a, __false_type)
2323 {
2324 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
2325 return _S_construct(__beg, __end, __a, _Tag());
2326 }
2327
2328 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2329 // 438. Ambiguity in the "do the right thing" clause
2330 template<class _Integer>
2331 static _CharT*
2332 _S_construct_aux(_Integer __beg, _Integer __end,
2333 const _Alloc& __a, __true_type)
2334 { return _S_construct_aux_2(static_cast<size_type>(__beg),
2335 __end, __a); }
2336
2337 static _CharT*
2338 _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
2339 { return _S_construct(__req, __c, __a); }
2340
2341 template<class _InIterator>
2342 static _CharT*
2343 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
2344 {
2345 typedef typename std::__is_integer<_InIterator>::__type _Integral;
2346 return _S_construct_aux(__beg, __end, __a, _Integral());
2347 }
2348
2349 // For Input Iterators, used in istreambuf_iterators, etc.
2350 template<class _InIterator>
2351 static _CharT*
2352 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
2353 input_iterator_tag);
2354
2355 // For forward_iterators up to random_access_iterators, used for
2356 // string::iterator, _CharT*, etc.
2357 template<class _FwdIterator>
2358 static _CharT*
2359 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
2360 forward_iterator_tag);
2361
2362 static _CharT*
2363 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
2364
2365 public:
2366
2367 /**
2368 * @brief Copy substring into C string.
2369 * @param __s C string to copy value into.
2370 * @param __n Number of characters to copy.
2371 * @param __pos Index of first character to copy.
2372 * @return Number of characters actually copied
2373 * @throw std::out_of_range If __pos > size().
2374 *
2375 * Copies up to @a __n characters starting at @a __pos into the
2376 * C string @a __s. If @a __pos is %greater than size(),
2377 * out_of_range is thrown.
2378 */
2379 size_type
2380 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
2381
2382 /**
2383 * @brief Swap contents with another string.
2384 * @param __s String to swap with.
2385 *
2386 * Exchanges the contents of this string with that of @a __s in constant
2387 * time.
2388 */
2389 void
2390 swap(basic_string& __s)
2391 _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value);
2392
2393 // String operations:
2394 /**
2395 * @brief Return const pointer to null-terminated contents.
2396 *
2397 * This is a handle to internal data. Do not modify or dire things may
2398 * happen.
2399 */
2400 const _CharT*
2401 c_str() const _GLIBCXX_NOEXCEPT
2402 { return _M_data(); }
2403
2404 /**
2405 * @brief Return const pointer to contents.
2406 *
2407 * This is a pointer to internal data. It is undefined to modify
2408 * the contents through the returned pointer. To get a pointer that
2409 * allows modifying the contents use @c &str[0] instead,
2410 * (or in C++17 the non-const @c str.data() overload).
2411 */
2412 const _CharT*
2413 data() const _GLIBCXX_NOEXCEPT
2414 { return _M_data(); }
2415
2416#if __cplusplus >= 201703L
2417 /**
2418 * @brief Return non-const pointer to contents.
2419 *
2420 * This is a pointer to the character sequence held by the string.
2421 * Modifying the characters in the sequence is allowed.
2422 *
2423 * The standard requires this function to be `noexcept` but for the
2424 * Copy-On-Write string implementation it can throw. This function
2425 * allows modifying the string contents directly, which means we
2426 * must copy-on-write to unshare it, which requires allocating memory.
2427 */
2428 _CharT*
2429 data() noexcept(false)
2430 {
2431 _M_leak();
2432 return _M_data();
2433 }
2434#endif
2435
2436 /**
2437 * @brief Return copy of allocator used to construct this string.
2438 */
2439 allocator_type
2440 get_allocator() const _GLIBCXX_NOEXCEPT
2441 { return _M_dataplus; }
2442
2443 /**
2444 * @brief Find position of a C substring.
2445 * @param __s C string to locate.
2446 * @param __pos Index of character to search from.
2447 * @param __n Number of characters from @a s to search for.
2448 * @return Index of start of first occurrence.
2449 *
2450 * Starting from @a __pos, searches forward for the first @a
2451 * __n characters in @a __s within this string. If found,
2452 * returns the index where it begins. If not found, returns
2453 * npos.
2454 */
2455 size_type
2456 find(const _CharT* __s, size_type __pos, size_type __n) const
2457 _GLIBCXX_NOEXCEPT;
2458
2459 /**
2460 * @brief Find position of a string.
2461 * @param __str String to locate.
2462 * @param __pos Index of character to search from (default 0).
2463 * @return Index of start of first occurrence.
2464 *
2465 * Starting from @a __pos, searches forward for value of @a __str within
2466 * this string. If found, returns the index where it begins. If not
2467 * found, returns npos.
2468 */
2469 size_type
2470 find(const basic_string& __str, size_type __pos = 0) const
2471 _GLIBCXX_NOEXCEPT
2472 { return this->find(__str.data(), __pos, __str.size()); }
2473
2474 /**
2475 * @brief Find position of a C string.
2476 * @param __s C string to locate.
2477 * @param __pos Index of character to search from (default 0).
2478 * @return Index of start of first occurrence.
2479 *
2480 * Starting from @a __pos, searches forward for the value of @a
2481 * __s within this string. If found, returns the index where
2482 * it begins. If not found, returns npos.
2483 */
2484 size_type
2485 find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
2486 {
2487 __glibcxx_requires_string(__s);
2488 return this->find(__s, __pos, traits_type::length(__s));
2489 }
2490
2491 /**
2492 * @brief Find position of a character.
2493 * @param __c Character to locate.
2494 * @param __pos Index of character to search from (default 0).
2495 * @return Index of first occurrence.
2496 *
2497 * Starting from @a __pos, searches forward for @a __c within
2498 * this string. If found, returns the index where it was
2499 * found. If not found, returns npos.
2500 */
2501 size_type
2502 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
2503
2504#ifdef __glibcxx_string_view // >= C++17
2505 /**
2506 * @brief Find position of a string_view.
2507 * @param __svt The object convertible to string_view to locate.
2508 * @param __pos Index of character to search from (default 0).
2509 * @return Index of start of first occurrence.
2510 */
2511 template<typename _Tp>
2512 _If_sv<_Tp, size_type>
2513 find(const _Tp& __svt, size_type __pos = 0) const
2514 noexcept(is_same<_Tp, __sv_type>::value)
2515 {
2516 __sv_type __sv = __svt;
2517 return this->find(__sv.data(), __pos, __sv.size());
2518 }
2519#endif // C++17
2520
2521 /**
2522 * @brief Find last position of a string.
2523 * @param __str String to locate.
2524 * @param __pos Index of character to search back from (default end).
2525 * @return Index of start of last occurrence.
2526 *
2527 * Starting from @a __pos, searches backward for value of @a
2528 * __str within this string. If found, returns the index where
2529 * it begins. If not found, returns npos.
2530 */
2531 size_type
2532 rfind(const basic_string& __str, size_type __pos = npos) const
2533 _GLIBCXX_NOEXCEPT
2534 { return this->rfind(__str.data(), __pos, __str.size()); }
2535
2536 /**
2537 * @brief Find last position of a C substring.
2538 * @param __s C string to locate.
2539 * @param __pos Index of character to search back from.
2540 * @param __n Number of characters from s to search for.
2541 * @return Index of start of last occurrence.
2542 *
2543 * Starting from @a __pos, searches backward for the first @a
2544 * __n characters in @a __s within this string. If found,
2545 * returns the index where it begins. If not found, returns
2546 * npos.
2547 */
2548 size_type
2549 rfind(const _CharT* __s, size_type __pos, size_type __n) const
2550 _GLIBCXX_NOEXCEPT;
2551
2552 /**
2553 * @brief Find last position of a C string.
2554 * @param __s C string to locate.
2555 * @param __pos Index of character to start search at (default end).
2556 * @return Index of start of last occurrence.
2557 *
2558 * Starting from @a __pos, searches backward for the value of
2559 * @a __s within this string. If found, returns the index
2560 * where it begins. If not found, returns npos.
2561 */
2562 size_type
2563 rfind(const _CharT* __s, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
2564 {
2565 __glibcxx_requires_string(__s);
2566 return this->rfind(__s, __pos, traits_type::length(__s));
2567 }
2568
2569 /**
2570 * @brief Find last position of a character.
2571 * @param __c Character to locate.
2572 * @param __pos Index of character to search back from (default end).
2573 * @return Index of last occurrence.
2574 *
2575 * Starting from @a __pos, searches backward for @a __c within
2576 * this string. If found, returns the index where it was
2577 * found. If not found, returns npos.
2578 */
2579 size_type
2580 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
2581
2582#ifdef __glibcxx_string_view // >= C++17
2583 /**
2584 * @brief Find last position of a string_view.
2585 * @param __svt The object convertible to string_view to locate.
2586 * @param __pos Index of character to search back from (default end).
2587 * @return Index of start of last occurrence.
2588 */
2589 template<typename _Tp>
2590 _If_sv<_Tp, size_type>
2591 rfind(const _Tp& __svt, size_type __pos = npos) const
2592 noexcept(is_same<_Tp, __sv_type>::value)
2593 {
2594 __sv_type __sv = __svt;
2595 return this->rfind(__sv.data(), __pos, __sv.size());
2596 }
2597#endif // C++17
2598
2599 /**
2600 * @brief Find position of a character of string.
2601 * @param __str String containing characters to locate.
2602 * @param __pos Index of character to search from (default 0).
2603 * @return Index of first occurrence.
2604 *
2605 * Starting from @a __pos, searches forward for one of the
2606 * characters of @a __str within this string. If found,
2607 * returns the index where it was found. If not found, returns
2608 * npos.
2609 */
2610 size_type
2611 find_first_of(const basic_string& __str, size_type __pos = 0) const
2612 _GLIBCXX_NOEXCEPT
2613 { return this->find_first_of(__str.data(), __pos, __str.size()); }
2614
2615 /**
2616 * @brief Find position of a character of C substring.
2617 * @param __s String containing characters to locate.
2618 * @param __pos Index of character to search from.
2619 * @param __n Number of characters from s to search for.
2620 * @return Index of first occurrence.
2621 *
2622 * Starting from @a __pos, searches forward for one of the
2623 * first @a __n characters of @a __s within this string. If
2624 * found, returns the index where it was found. If not found,
2625 * returns npos.
2626 */
2627 size_type
2628 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
2629 _GLIBCXX_NOEXCEPT;
2630
2631 /**
2632 * @brief Find position of a character of C string.
2633 * @param __s String containing characters to locate.
2634 * @param __pos Index of character to search from (default 0).
2635 * @return Index of first occurrence.
2636 *
2637 * Starting from @a __pos, searches forward for one of the
2638 * characters of @a __s within this string. If found, returns
2639 * the index where it was found. If not found, returns npos.
2640 */
2641 size_type
2642 find_first_of(const _CharT* __s, size_type __pos = 0) const
2643 _GLIBCXX_NOEXCEPT
2644 {
2645 __glibcxx_requires_string(__s);
2646 return this->find_first_of(__s, __pos, traits_type::length(__s));
2647 }
2648
2649 /**
2650 * @brief Find position of a character.
2651 * @param __c Character to locate.
2652 * @param __pos Index of character to search from (default 0).
2653 * @return Index of first occurrence.
2654 *
2655 * Starting from @a __pos, searches forward for the character
2656 * @a __c within this string. If found, returns the index
2657 * where it was found. If not found, returns npos.
2658 *
2659 * Note: equivalent to find(__c, __pos).
2660 */
2661 size_type
2662 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
2663 { return this->find(__c, __pos); }
2664
2665#ifdef __glibcxx_string_view // >= C++17
2666 /**
2667 * @brief Find position of a character of a string_view.
2668 * @param __svt An object convertible to string_view containing
2669 * characters to locate.
2670 * @param __pos Index of character to search from (default 0).
2671 * @return Index of first occurrence.
2672 */
2673 template<typename _Tp>
2674 _If_sv<_Tp, size_type>
2675 find_first_of(const _Tp& __svt, size_type __pos = 0) const
2676 noexcept(is_same<_Tp, __sv_type>::value)
2677 {
2678 __sv_type __sv = __svt;
2679 return this->find_first_of(__sv.data(), __pos, __sv.size());
2680 }
2681#endif // C++17
2682
2683 /**
2684 * @brief Find last position of a character of string.
2685 * @param __str String containing characters to locate.
2686 * @param __pos Index of character to search back from (default end).
2687 * @return Index of last occurrence.
2688 *
2689 * Starting from @a __pos, searches backward for one of the
2690 * characters of @a __str within this string. If found,
2691 * returns the index where it was found. If not found, returns
2692 * npos.
2693 */
2694 size_type
2695 find_last_of(const basic_string& __str, size_type __pos = npos) const
2696 _GLIBCXX_NOEXCEPT
2697 { return this->find_last_of(__str.data(), __pos, __str.size()); }
2698
2699 /**
2700 * @brief Find last position of a character of C substring.
2701 * @param __s C string containing characters to locate.
2702 * @param __pos Index of character to search back from.
2703 * @param __n Number of characters from s to search for.
2704 * @return Index of last occurrence.
2705 *
2706 * Starting from @a __pos, searches backward for one of the
2707 * first @a __n characters of @a __s within this string. If
2708 * found, returns the index where it was found. If not found,
2709 * returns npos.
2710 */
2711 size_type
2712 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
2713 _GLIBCXX_NOEXCEPT;
2714
2715 /**
2716 * @brief Find last position of a character of C string.
2717 * @param __s C string containing characters to locate.
2718 * @param __pos Index of character to search back from (default end).
2719 * @return Index of last occurrence.
2720 *
2721 * Starting from @a __pos, searches backward for one of the
2722 * characters of @a __s within this string. If found, returns
2723 * the index where it was found. If not found, returns npos.
2724 */
2725 size_type
2726 find_last_of(const _CharT* __s, size_type __pos = npos) const
2727 _GLIBCXX_NOEXCEPT
2728 {
2729 __glibcxx_requires_string(__s);
2730 return this->find_last_of(__s, __pos, traits_type::length(__s));
2731 }
2732
2733 /**
2734 * @brief Find last position of a character.
2735 * @param __c Character to locate.
2736 * @param __pos Index of character to search back from (default end).
2737 * @return Index of last occurrence.
2738 *
2739 * Starting from @a __pos, searches backward for @a __c within
2740 * this string. If found, returns the index where it was
2741 * found. If not found, returns npos.
2742 *
2743 * Note: equivalent to rfind(__c, __pos).
2744 */
2745 size_type
2746 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
2747 { return this->rfind(__c, __pos); }
2748
2749#ifdef __glibcxx_string_view // >= C++17
2750 /**
2751 * @brief Find last position of a character of string.
2752 * @param __svt An object convertible to string_view containing
2753 * characters to locate.
2754 * @param __pos Index of character to search back from (default end).
2755 * @return Index of last occurrence.
2756 */
2757 template<typename _Tp>
2758 _If_sv<_Tp, size_type>
2759 find_last_of(const _Tp& __svt, size_type __pos = npos) const
2760 noexcept(is_same<_Tp, __sv_type>::value)
2761 {
2762 __sv_type __sv = __svt;
2763 return this->find_last_of(__sv.data(), __pos, __sv.size());
2764 }
2765#endif // C++17
2766
2767 /**
2768 * @brief Find position of a character not in string.
2769 * @param __str String containing characters to avoid.
2770 * @param __pos Index of character to search from (default 0).
2771 * @return Index of first occurrence.
2772 *
2773 * Starting from @a __pos, searches forward for a character not contained
2774 * in @a __str within this string. If found, returns the index where it
2775 * was found. If not found, returns npos.
2776 */
2777 size_type
2778 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
2779 _GLIBCXX_NOEXCEPT
2780 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
2781
2782 /**
2783 * @brief Find position of a character not in C substring.
2784 * @param __s C string containing characters to avoid.
2785 * @param __pos Index of character to search from.
2786 * @param __n Number of characters from __s to consider.
2787 * @return Index of first occurrence.
2788 *
2789 * Starting from @a __pos, searches forward for a character not
2790 * contained in the first @a __n characters of @a __s within
2791 * this string. If found, returns the index where it was
2792 * found. If not found, returns npos.
2793 */
2794 size_type
2795 find_first_not_of(const _CharT* __s, size_type __pos,
2796 size_type __n) const _GLIBCXX_NOEXCEPT;
2797
2798 /**
2799 * @brief Find position of a character not in C string.
2800 * @param __s C string containing characters to avoid.
2801 * @param __pos Index of character to search from (default 0).
2802 * @return Index of first occurrence.
2803 *
2804 * Starting from @a __pos, searches forward for a character not
2805 * contained in @a __s within this string. If found, returns
2806 * the index where it was found. If not found, returns npos.
2807 */
2808 size_type
2809 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
2810 _GLIBCXX_NOEXCEPT
2811 {
2812 __glibcxx_requires_string(__s);
2813 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
2814 }
2815
2816 /**
2817 * @brief Find position of a different character.
2818 * @param __c Character to avoid.
2819 * @param __pos Index of character to search from (default 0).
2820 * @return Index of first occurrence.
2821 *
2822 * Starting from @a __pos, searches forward for a character
2823 * other than @a __c within this string. If found, returns the
2824 * index where it was found. If not found, returns npos.
2825 */
2826 size_type
2827 find_first_not_of(_CharT __c, size_type __pos = 0) const
2828 _GLIBCXX_NOEXCEPT;
2829
2830#ifdef __glibcxx_string_view // >= C++17
2831 /**
2832 * @brief Find position of a character not in a string_view.
2833 * @param __svt An object convertible to string_view containing
2834 * characters to avoid.
2835 * @param __pos Index of character to search from (default 0).
2836 * @return Index of first occurrence.
2837 */
2838 template<typename _Tp>
2839 _If_sv<_Tp, size_type>
2840 find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
2841 noexcept(is_same<_Tp, __sv_type>::value)
2842 {
2843 __sv_type __sv = __svt;
2844 return this->find_first_not_of(__sv.data(), __pos, __sv.size());
2845 }
2846#endif // C++17
2847
2848 /**
2849 * @brief Find last position of a character not in string.
2850 * @param __str String containing characters to avoid.
2851 * @param __pos Index of character to search back from (default end).
2852 * @return Index of last occurrence.
2853 *
2854 * Starting from @a __pos, searches backward for a character
2855 * not contained in @a __str within this string. If found,
2856 * returns the index where it was found. If not found, returns
2857 * npos.
2858 */
2859 size_type
2860 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
2861 _GLIBCXX_NOEXCEPT
2862 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
2863
2864 /**
2865 * @brief Find last position of a character not in C substring.
2866 * @param __s C string containing characters to avoid.
2867 * @param __pos Index of character to search back from.
2868 * @param __n Number of characters from s to consider.
2869 * @return Index of last occurrence.
2870 *
2871 * Starting from @a __pos, searches backward for a character not
2872 * contained in the first @a __n characters of @a __s within this string.
2873 * If found, returns the index where it was found. If not found,
2874 * returns npos.
2875 */
2876 size_type
2877 find_last_not_of(const _CharT* __s, size_type __pos,
2878 size_type __n) const _GLIBCXX_NOEXCEPT;
2879 /**
2880 * @brief Find last position of a character not in C string.
2881 * @param __s C string containing characters to avoid.
2882 * @param __pos Index of character to search back from (default end).
2883 * @return Index of last occurrence.
2884 *
2885 * Starting from @a __pos, searches backward for a character
2886 * not contained in @a __s within this string. If found,
2887 * returns the index where it was found. If not found, returns
2888 * npos.
2889 */
2890 size_type
2891 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
2892 _GLIBCXX_NOEXCEPT
2893 {
2894 __glibcxx_requires_string(__s);
2895 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
2896 }
2897
2898 /**
2899 * @brief Find last position of a different character.
2900 * @param __c Character to avoid.
2901 * @param __pos Index of character to search back from (default end).
2902 * @return Index of last occurrence.
2903 *
2904 * Starting from @a __pos, searches backward for a character other than
2905 * @a __c within this string. If found, returns the index where it was
2906 * found. If not found, returns npos.
2907 */
2908 size_type
2909 find_last_not_of(_CharT __c, size_type __pos = npos) const
2910 _GLIBCXX_NOEXCEPT;
2911
2912#ifdef __glibcxx_string_view // >= C++17
2913 /**
2914 * @brief Find last position of a character not in a string_view.
2915 * @param __svt An object convertible to string_view containing
2916 * characters to avoid.
2917 * @param __pos Index of character to search back from (default end).
2918 * @return Index of last occurrence.
2919 */
2920 template<typename _Tp>
2921 _If_sv<_Tp, size_type>
2922 find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
2923 noexcept(is_same<_Tp, __sv_type>::value)
2924 {
2925 __sv_type __sv = __svt;
2926 return this->find_last_not_of(__sv.data(), __pos, __sv.size());
2927 }
2928#endif // C++17
2929
2930 /**
2931 * @brief Get a substring.
2932 * @param __pos Index of first character (default 0).
2933 * @param __n Number of characters in substring (default remainder).
2934 * @return The new string.
2935 * @throw std::out_of_range If __pos > size().
2936 *
2937 * Construct and return a new string using the @a __n
2938 * characters starting at @a __pos. If the string is too
2939 * short, use the remainder of the characters. If @a __pos is
2940 * beyond the end of the string, out_of_range is thrown.
2941 */
2942 basic_string
2943 substr(size_type __pos = 0, size_type __n = npos) const
2944 { return basic_string(*this,
2945 _M_check(__pos, "basic_string::substr"), __n); }
2946
2947#ifdef __glibcxx_string_subview // >= C++26
2948 /**
2949 * @brief Get a subview.
2950 * @param __pos Index of first character (default 0).
2951 * @param __n Number of characters in subview (default remainder).
2952 * @return The subview.
2953 * @throw std::out_of_range If __pos > size().
2954 *
2955 * Construct and return a subview using the `__n` characters starting at
2956 * `__pos`. If the string is too short, use the remainder of the
2957 * characters. If `__pos` is beyond the end of the string, out_of_range
2958 * is thrown.
2959 */
2960 [[nodiscard]]
2961 constexpr basic_string_view<_CharT, _Traits>
2962 subview(size_type __pos = 0, size_type __n = npos) const
2963 { return __sv_type(*this).subview(__pos, __n); }
2964#endif
2965
2966 /**
2967 * @brief Compare to a string.
2968 * @param __str String to compare against.
2969 * @return Integer < 0, 0, or > 0.
2970 *
2971 * Returns an integer < 0 if this string is ordered before @a
2972 * __str, 0 if their values are equivalent, or > 0 if this
2973 * string is ordered after @a __str. Determines the effective
2974 * length rlen of the strings to compare as the smallest of
2975 * size() and str.size(). The function then compares the two
2976 * strings by calling traits::compare(data(), str.data(),rlen).
2977 * If the result of the comparison is nonzero returns it,
2978 * otherwise the shorter one is ordered first.
2979 */
2980 int
2981 compare(const basic_string& __str) const
2982 {
2983 const size_type __size = this->size();
2984 const size_type __osize = __str.size();
2985 const size_type __len = std::min(__size, __osize);
2986
2987 int __r = traits_type::compare(_M_data(), __str.data(), __len);
2988 if (!__r)
2989 __r = _S_compare(__size, __osize);
2990 return __r;
2991 }
2992
2993#ifdef __glibcxx_string_view // >= C++17
2994 /**
2995 * @brief Compare to a string_view.
2996 * @param __svt An object convertible to string_view to compare against.
2997 * @return Integer < 0, 0, or > 0.
2998 */
2999 template<typename _Tp>
3000 _If_sv<_Tp, int>
3001 compare(const _Tp& __svt) const
3002 noexcept(is_same<_Tp, __sv_type>::value)
3003 {
3004 __sv_type __sv = __svt;
3005 const size_type __size = this->size();
3006 const size_type __osize = __sv.size();
3007 const size_type __len = std::min(__size, __osize);
3008
3009 int __r = traits_type::compare(_M_data(), __sv.data(), __len);
3010 if (!__r)
3011 __r = _S_compare(__size, __osize);
3012 return __r;
3013 }
3014
3015 /**
3016 * @brief Compare to a string_view.
3017 * @param __pos A position in the string to start comparing from.
3018 * @param __n The number of characters to compare.
3019 * @param __svt An object convertible to string_view to compare
3020 * against.
3021 * @return Integer < 0, 0, or > 0.
3022 */
3023 template<typename _Tp>
3024 _If_sv<_Tp, int>
3025 compare(size_type __pos, size_type __n, const _Tp& __svt) const
3026 {
3027 __sv_type __sv = __svt;
3028 return __sv_type(*this).substr(__pos, __n).compare(__sv);
3029 }
3030
3031 /**
3032 * @brief Compare to a string_view.
3033 * @param __pos1 A position in the string to start comparing from.
3034 * @param __n1 The number of characters to compare.
3035 * @param __svt An object convertible to string_view to compare
3036 * against.
3037 * @param __pos2 A position in the string_view to start comparing from.
3038 * @param __n2 The number of characters to compare.
3039 * @return Integer < 0, 0, or > 0.
3040 */
3041 template<typename _Tp>
3042 _If_sv<_Tp, int>
3043 compare(size_type __pos1, size_type __n1, const _Tp& __svt,
3044 size_type __pos2, size_type __n2 = npos) const
3045 {
3046 __sv_type __sv = __svt;
3047 return __sv_type(*this)
3048 .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3049 }
3050#endif // C++17
3051
3052 /**
3053 * @brief Compare substring to a string.
3054 * @param __pos Index of first character of substring.
3055 * @param __n Number of characters in substring.
3056 * @param __str String to compare against.
3057 * @return Integer < 0, 0, or > 0.
3058 *
3059 * Form the substring of this string from the @a __n characters
3060 * starting at @a __pos. Returns an integer < 0 if the
3061 * substring is ordered before @a __str, 0 if their values are
3062 * equivalent, or > 0 if the substring is ordered after @a
3063 * __str. Determines the effective length rlen of the strings
3064 * to compare as the smallest of the length of the substring
3065 * and @a __str.size(). The function then compares the two
3066 * strings by calling
3067 * traits::compare(substring.data(),str.data(),rlen). If the
3068 * result of the comparison is nonzero returns it, otherwise
3069 * the shorter one is ordered first.
3070 */
3071 int
3072 compare(size_type __pos, size_type __n, const basic_string& __str) const
3073 {
3074 _M_check(__pos, "basic_string::compare");
3075 __n = _M_limit(__pos, __n);
3076 const size_type __osize = __str.size();
3077 const size_type __len = std::min(__n, __osize);
3078 int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
3079 if (!__r)
3080 __r = _S_compare(__n, __osize);
3081 return __r;
3082 }
3083
3084 /**
3085 * @brief Compare substring to a substring.
3086 * @param __pos1 Index of first character of substring.
3087 * @param __n1 Number of characters in substring.
3088 * @param __str String to compare against.
3089 * @param __pos2 Index of first character of substring of str.
3090 * @param __n2 Number of characters in substring of str.
3091 * @return Integer < 0, 0, or > 0.
3092 *
3093 * Form the substring of this string from the @a __n1
3094 * characters starting at @a __pos1. Form the substring of @a
3095 * __str from the @a __n2 characters starting at @a __pos2.
3096 * Returns an integer < 0 if this substring is ordered before
3097 * the substring of @a __str, 0 if their values are equivalent,
3098 * or > 0 if this substring is ordered after the substring of
3099 * @a __str. Determines the effective length rlen of the
3100 * strings to compare as the smallest of the lengths of the
3101 * substrings. The function then compares the two strings by
3102 * calling
3103 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
3104 * If the result of the comparison is nonzero returns it,
3105 * otherwise the shorter one is ordered first.
3106 */
3107 int
3108 compare(size_type __pos1, size_type __n1, const basic_string& __str,
3109 size_type __pos2, size_type __n2 = npos) const
3110 {
3111 _M_check(__pos1, "basic_string::compare");
3112 __str._M_check(__pos2, "basic_string::compare");
3113 __n1 = _M_limit(__pos1, __n1);
3114 __n2 = __str._M_limit(__pos2, __n2);
3115 const size_type __len = std::min(__n1, __n2);
3116 int __r = traits_type::compare(_M_data() + __pos1,
3117 __str.data() + __pos2, __len);
3118 if (!__r)
3119 __r = _S_compare(__n1, __n2);
3120 return __r;
3121 }
3122
3123 /**
3124 * @brief Compare to a C string.
3125 * @param __s C string to compare against.
3126 * @return Integer < 0, 0, or > 0.
3127 *
3128 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
3129 * their values are equivalent, or > 0 if this string is ordered after
3130 * @a __s. Determines the effective length rlen of the strings to
3131 * compare as the smallest of size() and the length of a string
3132 * constructed from @a __s. The function then compares the two strings
3133 * by calling traits::compare(data(),s,rlen). If the result of the
3134 * comparison is nonzero returns it, otherwise the shorter one is
3135 * ordered first.
3136 */
3137 int
3138 compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT
3139 {
3140 __glibcxx_requires_string(__s);
3141 const size_type __size = this->size();
3142 const size_type __osize = traits_type::length(__s);
3143 const size_type __len = std::min(__size, __osize);
3144 int __r = traits_type::compare(_M_data(), __s, __len);
3145 if (!__r)
3146 __r = _S_compare(__size, __osize);
3147 return __r;
3148 }
3149
3150 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3151 // 5 String::compare specification questionable
3152 /**
3153 * @brief Compare substring to a C string.
3154 * @param __pos Index of first character of substring.
3155 * @param __n1 Number of characters in substring.
3156 * @param __s C string to compare against.
3157 * @return Integer < 0, 0, or > 0.
3158 *
3159 * Form the substring of this string from the @a __n1
3160 * characters starting at @a pos. Returns an integer < 0 if
3161 * the substring is ordered before @a __s, 0 if their values
3162 * are equivalent, or > 0 if the substring is ordered after @a
3163 * __s. Determines the effective length rlen of the strings to
3164 * compare as the smallest of the length of the substring and
3165 * the length of a string constructed from @a __s. The
3166 * function then compares the two string by calling
3167 * traits::compare(substring.data(),__s,rlen). If the result of
3168 * the comparison is nonzero returns it, otherwise the shorter
3169 * one is ordered first.
3170 */
3171 int
3172 compare(size_type __pos, size_type __n1, const _CharT* __s) const
3173 {
3174 __glibcxx_requires_string(__s);
3175 _M_check(__pos, "basic_string::compare");
3176 __n1 = _M_limit(__pos, __n1);
3177 const size_type __osize = traits_type::length(__s);
3178 const size_type __len = std::min(__n1, __osize);
3179 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
3180 if (!__r)
3181 __r = _S_compare(__n1, __osize);
3182 return __r;
3183 }
3184
3185 /**
3186 * @brief Compare substring against a character %array.
3187 * @param __pos Index of first character of substring.
3188 * @param __n1 Number of characters in substring.
3189 * @param __s character %array to compare against.
3190 * @param __n2 Number of characters of s.
3191 * @return Integer < 0, 0, or > 0.
3192 *
3193 * Form the substring of this string from the @a __n1
3194 * characters starting at @a __pos. Form a string from the
3195 * first @a __n2 characters of @a __s. Returns an integer < 0
3196 * if this substring is ordered before the string from @a __s,
3197 * 0 if their values are equivalent, or > 0 if this substring
3198 * is ordered after the string from @a __s. Determines the
3199 * effective length rlen of the strings to compare as the
3200 * smallest of the length of the substring and @a __n2. The
3201 * function then compares the two strings by calling
3202 * traits::compare(substring.data(),s,rlen). If the result of
3203 * the comparison is nonzero returns it, otherwise the shorter
3204 * one is ordered first.
3205 *
3206 * NB: s must have at least n2 characters, &apos;\\0&apos; has
3207 * no special meaning.
3208 */
3209 int
3210 compare(size_type __pos, size_type __n1, const _CharT* __s,
3211 size_type __n2) const
3212 {
3213 __glibcxx_requires_string_len(__s, __n2);
3214 _M_check(__pos, "basic_string::compare");
3215 __n1 = _M_limit(__pos, __n1);
3216 const size_type __len = std::min(__n1, __n2);
3217 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
3218 if (!__r)
3219 __r = _S_compare(__n1, __n2);
3220 return __r;
3221 }
3222
3223#if __cplusplus >= 202002L
3224 bool
3225 starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
3226 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3227
3228 bool
3229 starts_with(_CharT __x) const noexcept
3230 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3231
3232 [[__gnu__::__nonnull__]]
3233 bool
3234 starts_with(const _CharT* __x) const noexcept
3235 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3236
3237 bool
3238 ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept
3239 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3240
3241 bool
3242 ends_with(_CharT __x) const noexcept
3243 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3244
3245 [[__gnu__::__nonnull__]]
3246 bool
3247 ends_with(const _CharT* __x) const noexcept
3248 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3249#endif // C++20
3250
3251#if __cplusplus > 202011L
3252 bool
3253 contains(basic_string_view<_CharT, _Traits> __x) const noexcept
3254 { return __sv_type(this->data(), this->size()).contains(__x); }
3255
3256 bool
3257 contains(_CharT __x) const noexcept
3258 { return __sv_type(this->data(), this->size()).contains(__x); }
3259
3260 [[__gnu__::__nonnull__]]
3261 bool
3262 contains(const _CharT* __x) const noexcept
3263 { return __sv_type(this->data(), this->size()).contains(__x); }
3264#endif // C++23
3265
3266# ifdef _GLIBCXX_TM_TS_INTERNAL
3267 friend void
3268 ::_txnal_cow_string_C1_for_exceptions(void* that, const char* s,
3269 void* exc);
3270 friend const char*
3271 ::_txnal_cow_string_c_str(const void *that);
3272 friend void
3273 ::_txnal_cow_string_D1(void *that);
3274 friend void
3275 ::_txnal_cow_string_D1_commit(void *that);
3276# endif
3277 };
3278
3279 template<typename _CharT, typename _Traits, typename _Alloc>
3280 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
3281 basic_string<_CharT, _Traits, _Alloc>::
3282 _Rep::_S_max_size = (((npos - sizeof(_Rep_base))/sizeof(_CharT)) - 1) / 4;
3283
3284 template<typename _CharT, typename _Traits, typename _Alloc>
3285 const _CharT
3286 basic_string<_CharT, _Traits, _Alloc>::
3287 _Rep::_S_terminal = _CharT();
3288
3289 template<typename _CharT, typename _Traits, typename _Alloc>
3290 const typename basic_string<_CharT, _Traits, _Alloc>::size_type
3291 basic_string<_CharT, _Traits, _Alloc>::npos;
3292
3293 // Linker sets _S_empty_rep_storage to all 0s (one reference, empty string)
3294 // at static init time (before static ctors are run).
3295 template<typename _CharT, typename _Traits, typename _Alloc>
3296 typename basic_string<_CharT, _Traits, _Alloc>::size_type
3297 basic_string<_CharT, _Traits, _Alloc>::_Rep::_S_empty_rep_storage[
3298 (sizeof(_Rep_base) + sizeof(_CharT) + sizeof(size_type) - 1) /
3299 sizeof(size_type)];
3300
3301 // NB: This is the special case for Input Iterators, used in
3302 // istreambuf_iterators, etc.
3303 // Input Iterators have a cost structure very different from
3304 // pointers, calling for a different coding style.
3305 template<typename _CharT, typename _Traits, typename _Alloc>
3306 template<typename _InIterator>
3307 _CharT*
3308 basic_string<_CharT, _Traits, _Alloc>::
3309 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
3310 input_iterator_tag)
3311 {
3312#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3313 if (__beg == __end && __a == _Alloc())
3314 return _S_empty_rep()._M_refdata();
3315#endif
3316 // Avoid reallocation for common case.
3317 _CharT __buf[128];
3318 size_type __len = 0;
3319 while (__beg != __end && __len < sizeof(__buf) / sizeof(_CharT))
3320 {
3321 __buf[__len++] = *__beg;
3322 ++__beg;
3323 }
3324 _Rep* __r = _Rep::_S_create(__len, size_type(0), __a);
3325 _M_copy(__r->_M_refdata(), __buf, __len);
3326 __try
3327 {
3328 while (__beg != __end)
3329 {
3330 if (__len == __r->_M_capacity)
3331 {
3332 // Allocate more space.
3333 _Rep* __another = _Rep::_S_create(__len + 1, __len, __a);
3334 _M_copy(__another->_M_refdata(), __r->_M_refdata(), __len);
3335 __r->_M_destroy(__a);
3336 __r = __another;
3337 }
3338 __r->_M_refdata()[__len++] = *__beg;
3339 ++__beg;
3340 }
3341 }
3342 __catch(...)
3343 {
3344 __r->_M_destroy(__a);
3345 __throw_exception_again;
3346 }
3347 __r->_M_set_length_and_sharable(__len);
3348 return __r->_M_refdata();
3349 }
3350
3351 template<typename _CharT, typename _Traits, typename _Alloc>
3352 template <typename _InIterator>
3353 _CharT*
3354 basic_string<_CharT, _Traits, _Alloc>::
3355 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
3356 forward_iterator_tag)
3357 {
3358#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3359 if (__beg == __end && __a == _Alloc())
3360 return _S_empty_rep()._M_refdata();
3361#endif
3362 // NB: Not required, but considered best practice.
3363 if (__gnu_cxx::__is_null_pointer(__beg) && __beg != __end)
3364 __throw_logic_error(__N("basic_string::_S_construct null not valid"));
3365
3366 const size_type __dnew = static_cast<size_type>(std::distance(__beg,
3367 __end));
3368 // Check for out_of_range and length_error exceptions.
3369 _Rep* __r = _Rep::_S_create(__dnew, size_type(0), __a);
3370 __try
3371 { _S_copy_chars(__r->_M_refdata(), __beg, __end); }
3372 __catch(...)
3373 {
3374 __r->_M_destroy(__a);
3375 __throw_exception_again;
3376 }
3377 __r->_M_set_length_and_sharable(__dnew);
3378 return __r->_M_refdata();
3379 }
3380
3381 template<typename _CharT, typename _Traits, typename _Alloc>
3382 _CharT*
3383 basic_string<_CharT, _Traits, _Alloc>::
3384 _S_construct(size_type __n, _CharT __c, const _Alloc& __a)
3385 {
3386#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3387 if (__n == 0 && __a == _Alloc())
3388 return _S_empty_rep()._M_refdata();
3389#endif
3390 // Check for out_of_range and length_error exceptions.
3391 _Rep* __r = _Rep::_S_create(__n, size_type(0), __a);
3392 if (__n)
3393 _M_assign(__r->_M_refdata(), __n, __c);
3394
3395 __r->_M_set_length_and_sharable(__n);
3396 return __r->_M_refdata();
3397 }
3398
3399 template<typename _CharT, typename _Traits, typename _Alloc>
3400 basic_string<_CharT, _Traits, _Alloc>::
3401 basic_string(const basic_string& __str, size_type __pos, const _Alloc& __a)
3402 : _M_dataplus(_S_construct(__str._M_data()
3403 + __str._M_check(__pos,
3404 "basic_string::basic_string"),
3405 __str._M_data() + __str._M_limit(__pos, npos)
3406 + __pos, __a), __a)
3407 { }
3408
3409 template<typename _CharT, typename _Traits, typename _Alloc>
3410 basic_string<_CharT, _Traits, _Alloc>::
3411 basic_string(const basic_string& __str, size_type __pos, size_type __n)
3412 : _M_dataplus(_S_construct(__str._M_data()
3413 + __str._M_check(__pos,
3414 "basic_string::basic_string"),
3415 __str._M_data() + __str._M_limit(__pos, __n)
3416 + __pos, _Alloc()), _Alloc())
3417 { }
3418
3419 template<typename _CharT, typename _Traits, typename _Alloc>
3420 basic_string<_CharT, _Traits, _Alloc>::
3421 basic_string(const basic_string& __str, size_type __pos,
3422 size_type __n, const _Alloc& __a)
3423 : _M_dataplus(_S_construct(__str._M_data()
3424 + __str._M_check(__pos,
3425 "basic_string::basic_string"),
3426 __str._M_data() + __str._M_limit(__pos, __n)
3427 + __pos, __a), __a)
3428 { }
3429
3430 template<typename _CharT, typename _Traits, typename _Alloc>
3431 basic_string<_CharT, _Traits, _Alloc>&
3432 basic_string<_CharT, _Traits, _Alloc>::
3433 assign(const basic_string& __str)
3434 {
3435 if (_M_rep() != __str._M_rep())
3436 {
3437 // XXX MT
3438 const allocator_type __a = this->get_allocator();
3439 _CharT* __tmp = __str._M_rep()->_M_grab(__a, __str.get_allocator());
3440 _M_rep()->_M_dispose(__a);
3441 _M_data(__tmp);
3442 }
3443 return *this;
3444 }
3445
3446 template<typename _CharT, typename _Traits, typename _Alloc>
3447 basic_string<_CharT, _Traits, _Alloc>&
3448 basic_string<_CharT, _Traits, _Alloc>::
3449 assign(const _CharT* __s, size_type __n)
3450 {
3451 __glibcxx_requires_string_len(__s, __n);
3452 _M_check_length(this->size(), __n, "basic_string::assign");
3453 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
3454 return _M_replace_safe(size_type(0), this->size(), __s, __n);
3455 else
3456 {
3457 // Work in-place.
3458 const size_type __pos = __s - _M_data();
3459 if (__pos >= __n)
3460 _M_copy(_M_data(), __s, __n);
3461 else if (__pos)
3462 _M_move(_M_data(), __s, __n);
3463 _M_rep()->_M_set_length_and_sharable(__n);
3464 return *this;
3465 }
3466 }
3467
3468 template<typename _CharT, typename _Traits, typename _Alloc>
3469 basic_string<_CharT, _Traits, _Alloc>&
3470 basic_string<_CharT, _Traits, _Alloc>::
3471 append(size_type __n, _CharT __c)
3472 {
3473 if (__n)
3474 {
3475 _M_check_length(size_type(0), __n, "basic_string::append");
3476 const size_type __len = __n + this->size();
3477 if (__len > this->capacity() || _M_rep()->_M_is_shared())
3478 this->reserve(__len);
3479 _M_assign(_M_data() + this->size(), __n, __c);
3480 _M_rep()->_M_set_length_and_sharable(__len);
3481 }
3482 return *this;
3483 }
3484
3485 template<typename _CharT, typename _Traits, typename _Alloc>
3486 basic_string<_CharT, _Traits, _Alloc>&
3487 basic_string<_CharT, _Traits, _Alloc>::
3488 append(const _CharT* __s, size_type __n)
3489 {
3490 __glibcxx_requires_string_len(__s, __n);
3491 if (__n)
3492 {
3493 _M_check_length(size_type(0), __n, "basic_string::append");
3494 const size_type __len = __n + this->size();
3495 if (__len > this->capacity() || _M_rep()->_M_is_shared())
3496 {
3497 if (_M_disjunct(__s))
3498 this->reserve(__len);
3499 else
3500 {
3501 const size_type __off = __s - _M_data();
3502 this->reserve(__len);
3503 __s = _M_data() + __off;
3504 }
3505 }
3506 _M_copy(_M_data() + this->size(), __s, __n);
3507 _M_rep()->_M_set_length_and_sharable(__len);
3508 }
3509 return *this;
3510 }
3511
3512 template<typename _CharT, typename _Traits, typename _Alloc>
3513 basic_string<_CharT, _Traits, _Alloc>&
3514 basic_string<_CharT, _Traits, _Alloc>::
3515 append(const basic_string& __str)
3516 {
3517 const size_type __size = __str.size();
3518 if (__size)
3519 {
3520 const size_type __len = __size + this->size();
3521 if (__len > this->capacity() || _M_rep()->_M_is_shared())
3522 this->reserve(__len);
3523 _M_copy(_M_data() + this->size(), __str._M_data(), __size);
3524 _M_rep()->_M_set_length_and_sharable(__len);
3525 }
3526 return *this;
3527 }
3528
3529 template<typename _CharT, typename _Traits, typename _Alloc>
3530 basic_string<_CharT, _Traits, _Alloc>&
3531 basic_string<_CharT, _Traits, _Alloc>::
3532 append(const basic_string& __str, size_type __pos, size_type __n)
3533 {
3534 __str._M_check(__pos, "basic_string::append");
3535 __n = __str._M_limit(__pos, __n);
3536 if (__n)
3537 {
3538 const size_type __len = __n + this->size();
3539 if (__len > this->capacity() || _M_rep()->_M_is_shared())
3540 this->reserve(__len);
3541 _M_copy(_M_data() + this->size(), __str._M_data() + __pos, __n);
3542 _M_rep()->_M_set_length_and_sharable(__len);
3543 }
3544 return *this;
3545 }
3546
3547 template<typename _CharT, typename _Traits, typename _Alloc>
3548 basic_string<_CharT, _Traits, _Alloc>&
3549 basic_string<_CharT, _Traits, _Alloc>::
3550 insert(size_type __pos, const _CharT* __s, size_type __n)
3551 {
3552 __glibcxx_requires_string_len(__s, __n);
3553 _M_check(__pos, "basic_string::insert");
3554 _M_check_length(size_type(0), __n, "basic_string::insert");
3555 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
3556 return _M_replace_safe(__pos, size_type(0), __s, __n);
3557 else
3558 {
3559 // Work in-place.
3560 const size_type __off = __s - _M_data();
3561 _M_mutate(__pos, 0, __n);
3562 __s = _M_data() + __off;
3563 _CharT* __p = _M_data() + __pos;
3564 if (__s + __n <= __p)
3565 _M_copy(__p, __s, __n);
3566 else if (__s >= __p)
3567 _M_copy(__p, __s + __n, __n);
3568 else
3569 {
3570 const size_type __nleft = __p - __s;
3571 _M_copy(__p, __s, __nleft);
3572 _M_copy(__p + __nleft, __p + __n, __n - __nleft);
3573 }
3574 return *this;
3575 }
3576 }
3577
3578 template<typename _CharT, typename _Traits, typename _Alloc>
3579 typename basic_string<_CharT, _Traits, _Alloc>::iterator
3580 basic_string<_CharT, _Traits, _Alloc>::
3581 erase(iterator __first, iterator __last)
3582 {
3583 _GLIBCXX_DEBUG_PEDASSERT(__first >= _M_ibegin() && __first <= __last
3584 && __last <= _M_iend());
3585
3586 // NB: This isn't just an optimization (bail out early when
3587 // there is nothing to do, really), it's also a correctness
3588 // issue vs MT, see libstdc++/40518.
3589 const size_type __size = __last - __first;
3590 if (__size)
3591 {
3592 const size_type __pos = __first - _M_ibegin();
3593 _M_mutate(__pos, __size, size_type(0));
3594 _M_rep()->_M_set_leaked();
3595 return iterator(_M_data() + __pos);
3596 }
3597 else
3598 return __first;
3599 }
3600
3601 template<typename _CharT, typename _Traits, typename _Alloc>
3602 basic_string<_CharT, _Traits, _Alloc>&
3603 basic_string<_CharT, _Traits, _Alloc>::
3604 replace(size_type __pos, size_type __n1, const _CharT* __s,
3605 size_type __n2)
3606 {
3607 __glibcxx_requires_string_len(__s, __n2);
3608 _M_check(__pos, "basic_string::replace");
3609 __n1 = _M_limit(__pos, __n1);
3610 _M_check_length(__n1, __n2, "basic_string::replace");
3611 bool __left;
3612 if (_M_disjunct(__s) || _M_rep()->_M_is_shared())
3613 return _M_replace_safe(__pos, __n1, __s, __n2);
3614 else if ((__left = __s + __n2 <= _M_data() + __pos)
3615 || _M_data() + __pos + __n1 <= __s)
3616 {
3617 // Work in-place: non-overlapping case.
3618 size_type __off = __s - _M_data();
3619 __left ? __off : (__off += __n2 - __n1);
3620 _M_mutate(__pos, __n1, __n2);
3621 _M_copy(_M_data() + __pos, _M_data() + __off, __n2);
3622 return *this;
3623 }
3624 else
3625 {
3626 // TODO: overlapping case.
3627 const basic_string __tmp(__s, __n2);
3628 return _M_replace_safe(__pos, __n1, __tmp._M_data(), __n2);
3629 }
3630 }
3631
3632 template<typename _CharT, typename _Traits, typename _Alloc>
3633 void
3634 basic_string<_CharT, _Traits, _Alloc>::_Rep::
3635 _M_destroy(const _Alloc& __a) throw ()
3636 {
3637 const size_type __size = sizeof(_Rep_base)
3638 + (this->_M_capacity + 1) * sizeof(_CharT);
3639 _Raw_bytes_alloc(__a).deallocate(reinterpret_cast<char*>(this), __size);
3640 }
3641
3642 template<typename _CharT, typename _Traits, typename _Alloc>
3643 void
3644 basic_string<_CharT, _Traits, _Alloc>::
3645 _M_leak_hard()
3646 {
3647 // No need to create a new copy of an empty string when a non-const
3648 // reference/pointer/iterator into it is obtained. Modifying the
3649 // trailing null character is undefined, so the ref/pointer/iterator
3650 // is effectively const anyway.
3651 if (this->empty())
3652 return;
3653
3654 if (_M_rep()->_M_is_shared())
3655 _M_mutate(0, 0, 0);
3656 _M_rep()->_M_set_leaked();
3657 }
3658
3659 template<typename _CharT, typename _Traits, typename _Alloc>
3660 void
3661 basic_string<_CharT, _Traits, _Alloc>::
3662 _M_mutate(size_type __pos, size_type __len1, size_type __len2)
3663 {
3664 const size_type __old_size = this->size();
3665 const size_type __new_size = __old_size + __len2 - __len1;
3666 const size_type __how_much = __old_size - __pos - __len1;
3667
3668 if (__new_size > this->capacity() || _M_rep()->_M_is_shared())
3669 {
3670 // Must reallocate.
3671 const allocator_type __a = get_allocator();
3672 _Rep* __r = _Rep::_S_create(__new_size, this->capacity(), __a);
3673
3674 if (__pos)
3675 _M_copy(__r->_M_refdata(), _M_data(), __pos);
3676 if (__how_much)
3677 _M_copy(__r->_M_refdata() + __pos + __len2,
3678 _M_data() + __pos + __len1, __how_much);
3679
3680 _M_rep()->_M_dispose(__a);
3681 _M_data(__r->_M_refdata());
3682 }
3683 else if (__how_much && __len1 != __len2)
3684 {
3685 // Work in-place.
3686 _M_move(_M_data() + __pos + __len2,
3687 _M_data() + __pos + __len1, __how_much);
3688 }
3689 _M_rep()->_M_set_length_and_sharable(__new_size);
3690 }
3691
3692 template<typename _CharT, typename _Traits, typename _Alloc>
3693 void
3694 basic_string<_CharT, _Traits, _Alloc>::
3695 reserve(size_type __res)
3696 {
3697 const size_type __capacity = capacity();
3698
3699 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3700 // 2968. Inconsistencies between basic_string reserve and
3701 // vector/unordered_map/unordered_set reserve functions
3702 // P0966 reserve should not shrink
3703 if (__res <= __capacity)
3704 {
3705 if (!_M_rep()->_M_is_shared())
3706 return;
3707
3708 // unshare, but keep same capacity
3709 __res = __capacity;
3710 }
3711
3712 const allocator_type __a = get_allocator();
3713 _CharT* __tmp = _M_rep()->_M_clone(__a, __res - this->size());
3714 _M_rep()->_M_dispose(__a);
3715 _M_data(__tmp);
3716 }
3717
3718 template<typename _CharT, typename _Traits, typename _Alloc>
3719 void
3720 basic_string<_CharT, _Traits, _Alloc>::
3721 swap(basic_string& __s)
3722 _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value)
3723 {
3724 if (_M_rep()->_M_is_leaked())
3725 _M_rep()->_M_set_sharable();
3726 if (__s._M_rep()->_M_is_leaked())
3727 __s._M_rep()->_M_set_sharable();
3728 if (this->get_allocator() == __s.get_allocator())
3729 {
3730 _CharT* __tmp = _M_data();
3731 _M_data(__s._M_data());
3732 __s._M_data(__tmp);
3733 }
3734 // The code below can usually be optimized away.
3735 else
3736 {
3737 const basic_string __tmp1(_M_ibegin(), _M_iend(),
3738 __s.get_allocator());
3739 const basic_string __tmp2(__s._M_ibegin(), __s._M_iend(),
3740 this->get_allocator());
3741 *this = __tmp2;
3742 __s = __tmp1;
3743 }
3744 }
3745
3746 template<typename _CharT, typename _Traits, typename _Alloc>
3747 typename basic_string<_CharT, _Traits, _Alloc>::_Rep*
3748 basic_string<_CharT, _Traits, _Alloc>::_Rep::
3749 _S_create(size_type __capacity, size_type __old_capacity,
3750 const _Alloc& __alloc)
3751 {
3752 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3753 // 83. String::npos vs. string::max_size()
3754 if (__capacity > _S_max_size)
3755 __throw_length_error(__N("basic_string::_S_create"));
3756
3757 // The standard places no restriction on allocating more memory
3758 // than is strictly needed within this layer at the moment or as
3759 // requested by an explicit application call to reserve(n).
3760
3761 // Many malloc implementations perform quite poorly when an
3762 // application attempts to allocate memory in a stepwise fashion
3763 // growing each allocation size by only 1 char. Additionally,
3764 // it makes little sense to allocate less linear memory than the
3765 // natural blocking size of the malloc implementation.
3766 // Unfortunately, we would need a somewhat low-level calculation
3767 // with tuned parameters to get this perfect for any particular
3768 // malloc implementation. Fortunately, generalizations about
3769 // common features seen among implementations seems to suffice.
3770
3771 // __pagesize need not match the actual VM page size for good
3772 // results in practice, thus we pick a common value on the low
3773 // side. __malloc_header_size is an estimate of the amount of
3774 // overhead per memory allocation (in practice seen N * sizeof
3775 // (void*) where N is 0, 2 or 4). According to folklore,
3776 // picking this value on the high side is better than
3777 // low-balling it (especially when this algorithm is used with
3778 // malloc implementations that allocate memory blocks rounded up
3779 // to a size which is a power of 2).
3780 const size_type __pagesize = 4096;
3781 const size_type __malloc_header_size = 4 * sizeof(void*);
3782
3783 // The below implements an exponential growth policy, necessary to
3784 // meet amortized linear time requirements of the library: see
3785 // http://gcc.gnu.org/ml/libstdc++/2001-07/msg00085.html.
3786 // It's active for allocations requiring an amount of memory above
3787 // system pagesize. This is consistent with the requirements of the
3788 // standard: http://gcc.gnu.org/ml/libstdc++/2001-07/msg00130.html
3789 if (__capacity > __old_capacity && __capacity < 2 * __old_capacity)
3790 __capacity = 2 * __old_capacity;
3791
3792 // NB: Need an array of char_type[__capacity], plus a terminating
3793 // null char_type() element, plus enough for the _Rep data structure.
3794 // Whew. Seemingly so needy, yet so elemental.
3795 size_type __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
3796
3797 const size_type __adj_size = __size + __malloc_header_size;
3798 if (__adj_size > __pagesize && __capacity > __old_capacity)
3799 {
3800 const size_type __extra = __pagesize - __adj_size % __pagesize;
3801 __capacity += __extra / sizeof(_CharT);
3802 // Never allocate a string bigger than _S_max_size.
3803 if (__capacity > _S_max_size)
3804 __capacity = _S_max_size;
3805 __size = (__capacity + 1) * sizeof(_CharT) + sizeof(_Rep);
3806 }
3807
3808 // NB: Might throw, but no worries about a leak, mate: _Rep()
3809 // does not throw.
3810 void* __place = _Raw_bytes_alloc(__alloc).allocate(__size);
3811 _Rep *__p = new (__place) _Rep;
3812 __p->_M_capacity = __capacity;
3813 // ABI compatibility - 3.4.x set in _S_create both
3814 // _M_refcount and _M_length. All callers of _S_create
3815 // in basic_string.tcc then set just _M_length.
3816 // In 4.0.x and later both _M_refcount and _M_length
3817 // are initialized in the callers, unfortunately we can
3818 // have 3.4.x compiled code with _S_create callers inlined
3819 // calling 4.0.x+ _S_create.
3820 __p->_M_set_sharable();
3821 return __p;
3822 }
3823
3824 template<typename _CharT, typename _Traits, typename _Alloc>
3825 _CharT*
3826 basic_string<_CharT, _Traits, _Alloc>::_Rep::
3827 _M_clone(const _Alloc& __alloc, size_type __res)
3828 {
3829 // Requested capacity of the clone.
3830 const size_type __requested_cap = this->_M_length + __res;
3831 _Rep* __r = _Rep::_S_create(__requested_cap, this->_M_capacity,
3832 __alloc);
3833 if (this->_M_length)
3834 _M_copy(__r->_M_refdata(), _M_refdata(), this->_M_length);
3835
3836 __r->_M_set_length_and_sharable(this->_M_length);
3837 return __r->_M_refdata();
3838 }
3839
3840 template<typename _CharT, typename _Traits, typename _Alloc>
3841 void
3842 basic_string<_CharT, _Traits, _Alloc>::
3843 resize(size_type __n, _CharT __c)
3844 {
3845 const size_type __size = this->size();
3846 _M_check_length(__size, __n, "basic_string::resize");
3847 if (__size < __n)
3848 this->append(__n - __size, __c);
3849 else if (__n < __size)
3850 this->erase(__n);
3851 // else nothing (in particular, avoid calling _M_mutate() unnecessarily.)
3852 }
3853
3854 template<typename _CharT, typename _Traits, typename _Alloc>
3855 template<typename _InputIterator>
3856 basic_string<_CharT, _Traits, _Alloc>&
3857 basic_string<_CharT, _Traits, _Alloc>::
3858 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
3859 _InputIterator __k2, __false_type)
3860 {
3861 __glibcxx_requires_valid_range(__k1, __k2);
3862 const basic_string __s(__k1, __k2);
3863 const size_type __n1 = __i2 - __i1;
3864 _M_check_length(__n1, __s.size(), "basic_string::_M_replace_dispatch");
3865 return _M_replace_safe(__i1 - _M_ibegin(), __n1, __s._M_data(),
3866 __s.size());
3867 }
3868
3869 template<typename _CharT, typename _Traits, typename _Alloc>
3870 basic_string<_CharT, _Traits, _Alloc>&
3871 basic_string<_CharT, _Traits, _Alloc>::
3872 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
3873 _CharT __c)
3874 {
3875 _M_check_length(__n1, __n2, "basic_string::_M_replace_aux");
3876 _M_mutate(__pos1, __n1, __n2);
3877 if (__n2)
3878 _M_assign(_M_data() + __pos1, __n2, __c);
3879 return *this;
3880 }
3881
3882 template<typename _CharT, typename _Traits, typename _Alloc>
3883 basic_string<_CharT, _Traits, _Alloc>&
3884 basic_string<_CharT, _Traits, _Alloc>::
3885 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
3886 size_type __n2)
3887 {
3888 _M_mutate(__pos1, __n1, __n2);
3889 if (__n2)
3890 _M_copy(_M_data() + __pos1, __s, __n2);
3891 return *this;
3892 }
3893
3894 template<typename _CharT, typename _Traits, typename _Alloc>
3895 void
3896 basic_string<_CharT, _Traits, _Alloc>::
3897 reserve()
3898 {
3899#if __cpp_exceptions
3900 if (length() < capacity() || _M_rep()->_M_is_shared())
3901 try
3902 {
3903 const allocator_type __a = get_allocator();
3904 _CharT* __tmp = _M_rep()->_M_clone(__a);
3905 _M_rep()->_M_dispose(__a);
3906 _M_data(__tmp);
3907 }
3908 catch (const __cxxabiv1::__forced_unwind&)
3909 { throw; }
3910 catch (...)
3911 { /* swallow the exception */ }
3912#endif
3913 }
3914
3915 template<typename _CharT, typename _Traits, typename _Alloc>
3916 typename basic_string<_CharT, _Traits, _Alloc>::size_type
3917 basic_string<_CharT, _Traits, _Alloc>::
3918 copy(_CharT* __s, size_type __n, size_type __pos) const
3919 {
3920 _M_check(__pos, "basic_string::copy");
3921 __n = _M_limit(__pos, __n);
3922 __glibcxx_requires_string_len(__s, __n);
3923 if (__n)
3924 _M_copy(__s, _M_data() + __pos, __n);
3925 // 21.3.5.7 par 3: do not append null. (good.)
3926 return __n;
3927 }
3928
3929#ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23
3930 template<typename _CharT, typename _Traits, typename _Alloc>
3931 template<typename _Operation>
3932 [[__gnu__::__always_inline__]]
3933 inline void
3934 basic_string<_CharT, _Traits, _Alloc>::
3935 __resize_and_overwrite(const size_type __n, _Operation __op)
3936 { resize_and_overwrite<_Operation&>(__n, __op); }
3937#endif
3938
3939#if __cplusplus >= 201103L
3940 template<typename _CharT, typename _Traits, typename _Alloc>
3941 template<typename _Operation>
3942 void
3943 basic_string<_CharT, _Traits, _Alloc>::
3944#ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23
3945 resize_and_overwrite(const size_type __n, _Operation __op)
3946#else
3947 __resize_and_overwrite(const size_type __n, _Operation __op)
3948#endif
3949 {
3950 const size_type __capacity = capacity();
3951 _CharT* __p;
3952 if (__n > __capacity || _M_rep()->_M_is_shared())
3953 this->reserve(__n);
3954 __p = _M_data();
3955 struct _Terminator {
3956 ~_Terminator() { _M_this->_M_rep()->_M_set_length_and_sharable(_M_r); }
3957 basic_string* _M_this;
3958 size_type _M_r;
3959 };
3960 _Terminator __term{this, 0};
3961 auto __r = std::move(__op)(__p + 0, __n + 0);
3962#ifdef __cpp_lib_concepts
3963 static_assert(ranges::__detail::__is_integer_like<decltype(__r)>);
3964#else
3965 static_assert(__gnu_cxx::__is_integer_nonstrict<decltype(__r)>::__value,
3966 "resize_and_overwrite operation must return an integer");
3967#endif
3968 _GLIBCXX_DEBUG_ASSERT(__r >= 0 && size_type(__r) <= __n);
3969 __term._M_r = size_type(__r);
3970 if (__term._M_r > __n)
3971 __builtin_unreachable();
3972 }
3973#endif // C++11
3974
3975
3976_GLIBCXX_END_NAMESPACE_VERSION
3977} // namespace std
3978#endif // ! _GLIBCXX_USE_CXX11_ABI
3979#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:2969
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:3108
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:1450
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:2485
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:2082
std::basic_string::assign
basic_string & assign(initializer_list< _CharT > __l)
Set value to an initializer_list of characters.
Definition cow_string.h:1582
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:2611
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:3581
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:2413
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:3435
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:2726
std::basic_string::compare
int compare(const _CharT *__s) const noexcept
Compare to a C string.
Definition cow_string.h:3138
std::basic_string::insert
void insert(iterator __p, initializer_list< _CharT > __l)
Insert an initializer_list of characters.
Definition cow_string.h:1700
std::basic_string::insert
basic_string & insert(size_type __pos1, const basic_string &__str)
Insert value of a string.
Definition cow_string.h:1720
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:2563
std::basic_string::substr
basic_string substr(size_type __pos=0, size_type __n=npos) const
Get a substring.
Definition cow_string.h:2943
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:1900
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:2470
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:2860
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:3072
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:2968
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:2981
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:3172
std::basic_string::substr
constexpr basic_string substr(size_type __pos=0, size_type __n=npos) const
Get a substring.
Definition basic_string.h:3523
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:3210
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:2063
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:2778
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:2891
std::basic_string::insert
void insert(iterator __p, size_type __n, _CharT __c)
Insert multiple characters.
Definition cow_string.h:1645
std::basic_string::assign
constexpr basic_string & assign(const basic_string &__str)
Set value to contents of another string.
Definition basic_string.h:1808
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:1742
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:2148
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:1477
std::basic_string::pop_back
void pop_back()
Remove the last character.
Definition cow_string.h:1929
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:3918
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:2695
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:2532
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:3171
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:3897
std::basic_string::data
_CharT * data() noexcept(false)
Return non-const pointer to contents.
Definition cow_string.h:2429
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:2746
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:3087
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:2237
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:3260
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:1556
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:1792
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:2662
std::basic_string::assign
basic_string & assign(size_type __n, _CharT __c)
Set value to multiple characters.
Definition cow_string.h:1543
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:2103
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:3348
std::basic_string::insert
basic_string & insert(size_type __pos, const _CharT *__s)
Insert a C string.
Definition cow_string.h:1783
std::basic_string::insert
constexpr iterator insert(const_iterator __p, size_type __n, _CharT __c)
Insert multiple characters.
Definition basic_string.h:2091
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:1499
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:2401
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:2021
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:2405
std::basic_string::get_allocator
allocator_type get_allocator() const noexcept
Return copy of allocator used to construct this string.
Definition cow_string.h:2440
std::basic_string::assign
basic_string & assign(const _CharT *__s)
Set value to contents of a C string.
Definition cow_string.h:1527
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:2045
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:2989
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:2491
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:2124
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:2962
std::basic_string::insert
void insert(iterator __p, _InputIterator __beg, _InputIterator __end)
Insert a range of characters.
Definition cow_string.h:1662
std::basic_string::compare
constexpr int compare(const basic_string &__str) const
Compare to a string.
Definition basic_string.h:3574
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:2642
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:1806
std::basic_string::erase
basic_string & erase(size_type __pos=0, size_type __n=npos)
Remove characters.
Definition cow_string.h:1884
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:1976
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:3721
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:1954
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:2809
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:1824
__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