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