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