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