libstdc++
basic_string.h
Go to the documentation of this file.
1// Components for manipulating sequences of characters -*- C++ -*-
2
3// Copyright (C) 1997-2026 Free Software Foundation, Inc.
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file bits/basic_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
30//
31// ISO C++ 14882: 21 Strings library
32//
33
34#ifndef _BASIC_STRING_H
35#define _BASIC_STRING_H 1
36
37#ifdef _GLIBCXX_SYSHDR
38#pragma GCC system_header
39#endif
40
41#include <ext/alloc_traits.h>
42#include <debug/debug.h>
43
44#if __cplusplus >= 201103L
45#include <initializer_list>
46#endif
47
48#include <bits/version.h>
49
50#ifdef __glibcxx_string_view // >= C++17
51# include <string_view>
52#endif
53
54#if __glibcxx_containers_ranges // C++ >= 23
55# include <bits/ranges_algobase.h> // ranges::copy
56# include <bits/ranges_util.h> // ranges::subrange
57#endif
58
59#if __glibcxx_to_string >= 202306L // C++ >= 26
60# include <charconv>
61#endif
62
63#if ! _GLIBCXX_USE_CXX11_ABI
64# include "cow_string.h"
65#else
66
67namespace std _GLIBCXX_VISIBILITY(default)
68{
69_GLIBCXX_BEGIN_NAMESPACE_VERSION
70_GLIBCXX_BEGIN_NAMESPACE_CXX11
71
72 /**
73 * @class basic_string basic_string.h <string>
74 * @brief Managing sequences of characters and character-like objects.
75 *
76 * @ingroup strings
77 * @ingroup sequences
78 * @headerfile string
79 * @since C++98
80 *
81 * @tparam _CharT Type of character
82 * @tparam _Traits Traits for character type, defaults to
83 * char_traits<_CharT>.
84 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
85 *
86 * Meets the requirements of a <a href="tables.html#65">container</a>, a
87 * <a href="tables.html#66">reversible container</a>, and a
88 * <a href="tables.html#67">sequence</a>. Of the
89 * <a href="tables.html#68">optional sequence requirements</a>, only
90 * @c push_back, @c at, and @c %array access are supported.
91 */
92 template<typename _CharT, typename _Traits, typename _Alloc>
94 {
95#if __cplusplus >= 202002L
96 static_assert(is_trivially_copyable_v<_CharT>
97 && is_trivially_default_constructible_v<_CharT>
98 && is_standard_layout_v<_CharT>);
99 static_assert(is_same_v<_CharT, typename _Traits::char_type>);
100 static_assert(is_same_v<_CharT, typename _Alloc::value_type>);
101 using _Char_alloc_type = _Alloc;
102#else
104 rebind<_CharT>::other _Char_alloc_type;
105#endif
106
108
109 // Types:
110 public:
111 typedef _Traits traits_type;
112 typedef typename _Traits::char_type value_type;
113 typedef _Char_alloc_type allocator_type;
114 typedef typename _Alloc_traits::size_type size_type;
115 typedef typename _Alloc_traits::difference_type difference_type;
116 typedef typename _Alloc_traits::reference reference;
117 typedef typename _Alloc_traits::const_reference const_reference;
118 typedef typename _Alloc_traits::pointer pointer;
119 typedef typename _Alloc_traits::const_pointer const_pointer;
120 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
121 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
122 const_iterator;
123 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
124 typedef std::reverse_iterator<iterator> reverse_iterator;
125
126 /// Value returned by various member functions when they fail.
127 static const size_type npos = static_cast<size_type>(-1);
128
129 protected:
130 // type used for positions in insert, erase etc.
131#if __cplusplus < 201103L
132 typedef iterator __const_iterator;
133#else
134 typedef const_iterator __const_iterator;
135#endif
136
137 private:
138 // For ABI reasons this must remain, though unused.
139 static _GLIBCXX20_CONSTEXPR pointer
140 _S_allocate(_Char_alloc_type& __a, size_type __n)
141 { return _Alloc_traits::allocate(__a, __n); }
142
143 struct _Alloc_result { pointer __ptr; size_type __count; };
144
145 static _GLIBCXX20_CONSTEXPR _Alloc_result
146 _S_allocate_at_least(_Char_alloc_type& __a, size_type __n)
147 {
148 _Alloc_result __r;
149#ifdef __glibcxx_allocate_at_least // C++23
150 auto [__ptr, __count] = _Alloc_traits::allocate_at_least(__a, __n);
151 __r.__ptr = __ptr;
152 __r.__count = __count;
153#else
154 __r.__ptr = _Alloc_traits::allocate(__a, __n);
155 __r.__count = __n;
156#endif
157#if __glibcxx_constexpr_string >= 201907L
158 // std::char_traits begins the lifetime of characters,
159 // but custom traits might not, so do it here.
160 if constexpr (!is_same_v<_Traits, char_traits<_CharT>>)
161 if (std::__is_constant_evaluated())
162 // Begin the lifetime of characters in allocated storage.
163 for (size_type __i = 0; __i < __r.__count; ++__i)
164 std::construct_at(__builtin_addressof(__r.__ptr[__i]));
165#endif
166 return __r;
167 }
168
169#ifdef __glibcxx_string_view // >= C++17
170 // A helper type for avoiding boiler-plate.
171 typedef basic_string_view<_CharT, _Traits> __sv_type;
172
173 template<typename _Tp, typename _Res>
174 using _If_sv = enable_if_t<
175 __and_<is_convertible<const _Tp&, __sv_type>,
176 __not_<is_convertible<const _Tp*, const basic_string*>>,
177 __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
178 _Res>;
179
180 // Allows an implicit conversion to __sv_type.
181 _GLIBCXX20_CONSTEXPR
182 static __sv_type
183 _S_to_string_view(__sv_type __svt) noexcept
184 { return __svt; }
185
186 // Wraps a string_view by explicit conversion and thus
187 // allows to add an internal constructor that does not
188 // participate in overload resolution when a string_view
189 // is provided.
190 struct __sv_wrapper
191 {
192 _GLIBCXX20_CONSTEXPR explicit
193 __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
194
195 __sv_type _M_sv;
196 };
197
198 /**
199 * @brief Only internally used: Construct string from a string view
200 * wrapper.
201 * @param __svw string view wrapper.
202 * @param __a Allocator to use.
203 */
204 _GLIBCXX20_CONSTEXPR
205 explicit
206 basic_string(__sv_wrapper __svw, const _Alloc& __a)
207 : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
208#endif
209
210 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
211 struct _Alloc_hider : allocator_type // TODO check __is_final
212 {
213#if __cplusplus < 201103L
214 _Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc())
215 : allocator_type(__a), _M_p(__dat) { }
216#else
217 _GLIBCXX20_CONSTEXPR
218 _Alloc_hider(pointer __dat, const _Alloc& __a)
219 : allocator_type(__a), _M_p(__dat) { }
220
221 _GLIBCXX20_CONSTEXPR
222 _Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc())
223 : allocator_type(std::move(__a)), _M_p(__dat) { }
224#endif
225
226 pointer _M_p; // The actual data.
227 };
228
229 _Alloc_hider _M_dataplus;
230 size_type _M_string_length;
231
232 enum { _S_local_capacity = 15 / sizeof(_CharT) };
233
234 union
235 {
236 _CharT _M_local_buf[_S_local_capacity + 1];
237 size_type _M_allocated_capacity;
238 };
239
240 _GLIBCXX20_CONSTEXPR
241 void
242 _M_data(pointer __p)
243 { _M_dataplus._M_p = __p; }
244
245 _GLIBCXX20_CONSTEXPR
246 void
247 _M_length(size_type __length)
248 { _M_string_length = __length; }
249
250 _GLIBCXX20_CONSTEXPR
251 pointer
252 _M_data() const
253 { return _M_dataplus._M_p; }
254
255 _GLIBCXX20_CONSTEXPR
256 pointer
257 _M_local_data()
258 {
259#if __cplusplus >= 201103L
260 return std::pointer_traits<pointer>::pointer_to(*_M_local_buf);
261#else
262 return pointer(_M_local_buf);
263#endif
264 }
265
266 _GLIBCXX20_CONSTEXPR
267 const_pointer
268 _M_local_data() const
269 {
270#if __cplusplus >= 201103L
271 return std::pointer_traits<const_pointer>::pointer_to(*_M_local_buf);
272#else
273 return const_pointer(_M_local_buf);
274#endif
275 }
276
277 _GLIBCXX20_CONSTEXPR
278 void
279 _M_capacity(size_type __capacity)
280 { _M_allocated_capacity = __capacity; }
281
282 _GLIBCXX20_CONSTEXPR
283 void
284 _M_set_length(size_type __n)
285 {
286 traits_type::assign(_M_data()[__n], _CharT());
287 _M_length(__n);
288 }
289
290 _GLIBCXX20_CONSTEXPR
291 bool
292 _M_is_local() const
293 {
294 if (_M_data() == _M_local_data())
295 {
296 if (_M_string_length > _S_local_capacity)
297 __builtin_unreachable();
298 return true;
299 }
300 return false;
301 }
302
303 // Create & Destroy
304 _GLIBCXX20_CONSTEXPR
305 _Alloc_result
306 _M_create_plus(size_type __new_capacity, size_type __old_capacity);
307
308 __attribute__((__always_inline__))
309 _GLIBCXX20_CONSTEXPR
310 void
311 _M_create_and_place(size_type __new_capacity, size_type __old_capacity)
312 {
313 _Alloc_result __r = _M_create_plus(__new_capacity, __old_capacity);
314 _M_data(__r.__ptr);
315 _M_capacity(__r.__count - 1); // Leave room for NUL.
316 }
317
318 // This must remain for ABI stability though unused.
319 _GLIBCXX20_CONSTEXPR
320 pointer
321 _M_create(size_type&, size_type);
322
323 _GLIBCXX20_CONSTEXPR
324 void
325 _M_dispose()
326 {
327 if (!_M_is_local())
328 _M_destroy(_M_allocated_capacity);
329 }
330
331 _GLIBCXX20_CONSTEXPR
332 void
333 _M_destroy(size_type __size) throw()
334 { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); }
335
336#if __cplusplus < 201103L || defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
337 // _M_construct_aux is used to implement the 21.3.1 para 15 which
338 // requires special behaviour if _InIterator is an integral type
339 template<typename _InIterator>
340 void
341 _M_construct_aux(_InIterator __beg, _InIterator __end,
342 std::__false_type)
343 {
344 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
345 _M_construct(__beg, __end, _Tag());
346 }
347
348 // _GLIBCXX_RESOLVE_LIB_DEFECTS
349 // 438. Ambiguity in the "do the right thing" clause
350 template<typename _Integer>
351 void
352 _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
353 { _M_construct_aux_2(static_cast<size_type>(__beg), __end); }
354
355 void
356 _M_construct_aux_2(size_type __req, _CharT __c)
357 { _M_construct(__req, __c); }
358#endif
359
360 // For Input Iterators, used in istreambuf_iterators, etc.
361 template<typename _InIterator>
362 _GLIBCXX20_CONSTEXPR
363 void
364 _M_construct(_InIterator __beg, _InIterator __end,
365 std::input_iterator_tag);
366
367 // For forward_iterators up to random_access_iterators, used for
368 // string::iterator, _CharT*, etc.
369 template<typename _FwdIterator>
370 _GLIBCXX20_CONSTEXPR
371 void
372 _M_construct(_FwdIterator __beg, _FwdIterator __end,
373 std::forward_iterator_tag);
374
375 _GLIBCXX20_CONSTEXPR
376 void
377 _M_construct(size_type __req, _CharT __c);
378
379 // Construct using block of memory of known size.
380 // If _Terminated is true assume that source is already 0 terminated.
381 template<bool _Terminated>
382 _GLIBCXX20_CONSTEXPR
383 void
384 _M_construct(const _CharT *__c, size_type __n);
385
386#if __cplusplus >= 202302L
387 constexpr void
388 _M_construct(basic_string&& __str, size_type __pos, size_type __n);
389#endif
390
391 _GLIBCXX20_CONSTEXPR
392 allocator_type&
393 _M_get_allocator()
394 { return _M_dataplus; }
395
396 _GLIBCXX20_CONSTEXPR
397 const allocator_type&
398 _M_get_allocator() const
399 { return _M_dataplus; }
400
401 // Ensure that _M_local_buf is the active member of the union.
402 __attribute__((__always_inline__))
403 _GLIBCXX14_CONSTEXPR
404 void
405 _M_init_local_buf() _GLIBCXX_NOEXCEPT
406 {
407#if __glibcxx_is_constant_evaluated
408 if (std::is_constant_evaluated())
409 for (size_type __i = 0; __i <= _S_local_capacity; ++__i)
410 _M_local_buf[__i] = _CharT();
411#endif
412 }
413
414 __attribute__((__always_inline__))
415 _GLIBCXX14_CONSTEXPR
416 pointer
417 _M_use_local_data() _GLIBCXX_NOEXCEPT
418 {
419#if __cpp_lib_is_constant_evaluated
420 _M_init_local_buf();
421#endif
422 return _M_local_data();
423 }
424
425 private:
426
427#ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
428 // The explicit instantiations in misc-inst.cc require this due to
429 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
430 template<typename _Tp, bool _Requires =
431 !__are_same<_Tp, _CharT*>::__value
432 && !__are_same<_Tp, const _CharT*>::__value
433 && !__are_same<_Tp, iterator>::__value
434 && !__are_same<_Tp, const_iterator>::__value>
435 struct __enable_if_not_native_iterator
436 { typedef basic_string& __type; };
437 template<typename _Tp>
438 struct __enable_if_not_native_iterator<_Tp, false> { };
439#endif
440
441 _GLIBCXX20_CONSTEXPR
442 size_type
443 _M_check(size_type __pos, const char* __s) const
444 {
445 if (__pos > this->size())
446 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
447 "this->size() (which is %zu)"),
448 __s, (size_t)__pos, (size_t)this->size());
449 return __pos;
450 }
451
452 _GLIBCXX20_CONSTEXPR
453 void
454 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
455 {
456 if (this->max_size() - (this->size() - __n1) < __n2)
457 __throw_length_error(__N(__s));
458 }
459
460
461 // NB: _M_limit doesn't check for a bad __pos value.
462 _GLIBCXX20_CONSTEXPR
463 size_type
464 _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
465 {
466 const bool __testoff = __off < this->size() - __pos;
467 return __testoff ? __off : this->size() - __pos;
468 }
469
470 // True if _Rep and source do not overlap.
471 bool
472 _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
473 {
474 return (less<const _CharT*>()(__s, _M_data())
475 || less<const _CharT*>()(_M_data() + this->size(), __s));
476 }
477
478 // When __n = 1 way faster than the general multichar
479 // traits_type::copy/move/assign.
480 _GLIBCXX20_CONSTEXPR
481 static void
482 _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
483 {
484 if (__n == 1)
485 traits_type::assign(*__d, *__s);
486 else
487 traits_type::copy(__d, __s, __n);
488 }
489
490 _GLIBCXX20_CONSTEXPR
491 static void
492 _S_move(_CharT* __d, const _CharT* __s, size_type __n)
493 {
494 if (__n == 1)
495 traits_type::assign(*__d, *__s);
496 else
497 traits_type::move(__d, __s, __n);
498 }
499
500 _GLIBCXX20_CONSTEXPR
501 static void
502 _S_assign(_CharT* __d, size_type __n, _CharT __c)
503 {
504 if (__n == 1)
505 traits_type::assign(*__d, __c);
506 else
507 traits_type::assign(__d, __n, __c);
508 }
509
510#pragma GCC diagnostic push
511#pragma GCC diagnostic ignored "-Wc++17-extensions"
512 // _S_copy_chars is a separate template to permit specialization
513 // to optimize for the common case of pointers as iterators.
514 template<class _Iterator>
515 _GLIBCXX20_CONSTEXPR
516 static void
517 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
518 {
519#if __cplusplus >= 201103L
520 using _IterBase = decltype(std::__niter_base(__k1));
521 if constexpr (__or_<is_same<_IterBase, _CharT*>,
522 is_same<_IterBase, const _CharT*>>::value)
523 _S_copy(__p, std::__niter_base(__k1), __k2 - __k1);
524#if __cpp_lib_concepts
525 else if constexpr (requires {
526 requires contiguous_iterator<_Iterator>;
527 { std::to_address(__k1) }
528 -> convertible_to<const _CharT*>;
529 })
530 {
531 const auto __d = __k2 - __k1;
532 (void) (__k1 + __d); // See P3349R1
533 _S_copy(__p, std::to_address(__k1), static_cast<size_type>(__d));
534 }
535#endif
536 else
537#endif
538 for (; __k1 != __k2; ++__k1, (void)++__p)
539 traits_type::assign(*__p, static_cast<_CharT>(*__k1));
540 }
541#pragma GCC diagnostic pop
542
543#if __cplusplus < 201103L || defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
544 static void
545 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2)
546 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
547
548 static void
549 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
550 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
551
552 static void
553 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2)
554 { _S_copy(__p, __k1, __k2 - __k1); }
555
556 static void
557 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
558 { _S_copy(__p, __k1, __k2 - __k1); }
559#endif
560
561#if __glibcxx_containers_ranges // C++ >= 23
562 // pre: __n == ranges::distance(__rg). __p+[0,__n) is a valid range.
563 template<typename _Rg>
564 static constexpr void
565 _S_copy_range(pointer __p, _Rg&& __rg, size_type __n)
566 {
567 if constexpr (requires {
568 requires ranges::contiguous_range<_Rg>;
569 { ranges::data(std::forward<_Rg>(__rg)) }
570 -> convertible_to<const _CharT*>;
571 })
572 _S_copy(__p, ranges::data(std::forward<_Rg>(__rg)), __n);
573 else
574 {
575 auto __first = ranges::begin(__rg);
576 const auto __last = ranges::end(__rg);
577 for (; __first != __last; ++__first)
578 traits_type::assign(*__p++, static_cast<_CharT>(*__first));
579 }
580 }
581#endif
582
583 _GLIBCXX20_CONSTEXPR
584 static int
585 _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
586 {
587 const difference_type __d = difference_type(__n1 - __n2);
588
589 if (__d > __gnu_cxx::__numeric_traits<int>::__max)
590 return __gnu_cxx::__numeric_traits<int>::__max;
591 else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
592 return __gnu_cxx::__numeric_traits<int>::__min;
593 else
594 return int(__d);
595 }
596
597 _GLIBCXX20_CONSTEXPR
598 void
599 _M_assign(const basic_string&);
600
601 _GLIBCXX20_CONSTEXPR
602 void
603 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
604 size_type __len2);
605
606 _GLIBCXX20_CONSTEXPR
607 void
608 _M_erase(size_type __pos, size_type __n);
609
610 public:
611 // Construct/copy/destroy:
612 // NB: We overload ctors in some cases instead of using default
613 // arguments, per 17.4.4.4 para. 2 item 2.
614
615 /**
616 * @brief Default constructor creates an empty string.
617 */
618 _GLIBCXX20_CONSTEXPR
620 _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
621#if __cpp_concepts && __glibcxx_type_trait_variable_templates
622 requires is_default_constructible_v<_Alloc>
623#endif
624 : _M_dataplus(_M_local_data())
625 {
626 _M_init_local_buf();
627 _M_set_length(0);
628 }
629
630 /**
631 * @brief Construct an empty string using allocator @a a.
632 */
633 _GLIBCXX20_CONSTEXPR
634 explicit
635 basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
636 : _M_dataplus(_M_local_data(), __a)
637 {
638 _M_init_local_buf();
639 _M_set_length(0);
640 }
641
642 /**
643 * @brief Construct string with copy of value of @a __str.
644 * @param __str Source string.
645 */
646 _GLIBCXX20_CONSTEXPR
648 : _M_dataplus(_M_local_data(),
649 _Alloc_traits::_S_select_on_copy(__str._M_get_allocator()))
650 {
651 _M_construct<true>(__str._M_data(), __str.length());
652 }
653
654 // _GLIBCXX_RESOLVE_LIB_DEFECTS
655 // 2583. no way to supply an allocator for basic_string(str, pos)
656 /**
657 * @brief Construct string as copy of a substring.
658 * @param __str Source string.
659 * @param __pos Index of first character to copy from.
660 * @param __a Allocator to use.
661 */
662 _GLIBCXX20_CONSTEXPR
663 basic_string(const basic_string& __str, size_type __pos,
664 const _Alloc& __a = _Alloc())
665 : _M_dataplus(_M_local_data(), __a)
666 {
667 const _CharT* __start = __str._M_data()
668 + __str._M_check(__pos, "basic_string::basic_string");
669 _M_construct(__start, __start + __str._M_limit(__pos, npos),
671 }
672
673 /**
674 * @brief Construct string as copy of a substring.
675 * @param __str Source string.
676 * @param __pos Index of first character to copy from.
677 * @param __n Number of characters to copy.
678 */
679 _GLIBCXX20_CONSTEXPR
680 basic_string(const basic_string& __str, size_type __pos,
681 size_type __n)
682 : _M_dataplus(_M_local_data())
683 {
684 const _CharT* __start = __str._M_data()
685 + __str._M_check(__pos, "basic_string::basic_string");
686 _M_construct(__start, __start + __str._M_limit(__pos, __n),
688 }
689
690 /**
691 * @brief Construct string as copy of a substring.
692 * @param __str Source string.
693 * @param __pos Index of first character to copy from.
694 * @param __n Number of characters to copy.
695 * @param __a Allocator to use.
696 */
697 _GLIBCXX20_CONSTEXPR
698 basic_string(const basic_string& __str, size_type __pos,
699 size_type __n, const _Alloc& __a)
700 : _M_dataplus(_M_local_data(), __a)
701 {
702 const _CharT* __start
703 = __str._M_data() + __str._M_check(__pos, "string::string");
704 _M_construct(__start, __start + __str._M_limit(__pos, __n),
706 }
707
708#if __cplusplus >= 202302L
709 _GLIBCXX20_CONSTEXPR
710 basic_string(basic_string&& __str, size_type __pos,
711 const _Alloc& __a = _Alloc())
712 : _M_dataplus(_M_local_data(), __a)
713 {
714 __pos = __str._M_check(__pos, "string::string");
715 _M_construct(std::move(__str), __pos, __str.length() - __pos);
716 }
717
718 _GLIBCXX20_CONSTEXPR
719 basic_string(basic_string&& __str, size_type __pos, size_type __n,
720 const _Alloc& __a = _Alloc())
721 : _M_dataplus(_M_local_data(), __a)
722 {
723 __pos = __str._M_check(__pos, "string::string");
724 _M_construct(std::move(__str), __pos, __str._M_limit(__pos, __n));
725 }
726#endif // C++23
727
728 /**
729 * @brief Construct string initialized by a character %array.
730 * @param __s Source character %array.
731 * @param __n Number of characters to copy.
732 * @param __a Allocator to use (default is default allocator).
733 *
734 * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
735 * has no special meaning.
736 */
737 _GLIBCXX20_CONSTEXPR
738 basic_string(const _CharT* __s, size_type __n,
739 const _Alloc& __a = _Alloc())
740 : _M_dataplus(_M_local_data(), __a)
741 {
742 // NB: Not required, but considered best practice.
743 if (__s == 0 && __n > 0)
744 std::__throw_logic_error(__N("basic_string: "
745 "construction from null is not valid"));
746 _M_construct(__s, __s + __n, std::forward_iterator_tag());
747 }
748
749 /**
750 * @brief Construct string as copy of a C string.
751 * @param __s Source C string.
752 * @param __a Allocator to use (default is default allocator).
753 */
754#if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
755 // _GLIBCXX_RESOLVE_LIB_DEFECTS
756 // 3076. basic_string CTAD ambiguity
757 template<typename = _RequireAllocator<_Alloc>>
758#endif
759 _GLIBCXX20_CONSTEXPR
760 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
761 : _M_dataplus(_M_local_data(), __a)
762 {
763 // NB: Not required, but considered best practice.
764 if (__s == 0)
765 std::__throw_logic_error(__N("basic_string: "
766 "construction from null is not valid"));
767 const _CharT* __end = __s + traits_type::length(__s);
768 _M_construct(__s, __end, forward_iterator_tag());
769 }
770
771 /**
772 * @brief Construct string as multiple characters.
773 * @param __n Number of characters.
774 * @param __c Character to use.
775 * @param __a Allocator to use (default is default allocator).
776 */
777#if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
778 // _GLIBCXX_RESOLVE_LIB_DEFECTS
779 // 3076. basic_string CTAD ambiguity
780 template<typename = _RequireAllocator<_Alloc>>
781#endif
782 _GLIBCXX20_CONSTEXPR
783 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
784 : _M_dataplus(_M_local_data(), __a)
785 { _M_construct(__n, __c); }
786
787#if __cplusplus >= 201103L
788 /**
789 * @brief Move construct string.
790 * @param __str Source string.
791 *
792 * The newly-created string contains the exact contents of @a __str.
793 * @a __str is a valid, but unspecified string.
794 */
795 _GLIBCXX20_CONSTEXPR
796 basic_string(basic_string&& __str) noexcept
797 : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator()))
798 {
799 if (__str._M_is_local())
800 {
801 _M_init_local_buf();
802 traits_type::copy(_M_local_buf, __str._M_local_buf,
803 __str.length() + 1);
804 }
805 else
806 {
807 _M_data(__str._M_data());
808 _M_capacity(__str._M_allocated_capacity);
809 }
810
811 // Must use _M_length() here not _M_set_length() because
812 // basic_stringbuf relies on writing into unallocated capacity so
813 // we mess up the contents if we put a '\0' in the string.
814 _M_length(__str.length());
815 __str._M_data(__str._M_use_local_data());
816 __str._M_set_length(0);
817 }
818
819#if __glibcxx_containers_ranges // C++ >= 23
820 /**
821 * @brief Construct a string from a range.
822 * @since C++23
823 */
824 template<__detail::__container_compatible_range<_CharT> _Rg>
825 constexpr
826 basic_string(from_range_t, _Rg&& __rg, const _Alloc& __a = _Alloc())
827 : basic_string(__a)
828 {
830 {
831 const auto __n = static_cast<size_type>(ranges::distance(__rg));
832 reserve(__n);
833 _S_copy_range(_M_data(), std::forward<_Rg>(__rg), __n);
834 _M_set_length(__n);
835 }
836 else
837 {
838 auto __first = ranges::begin(__rg);
839 const auto __last = ranges::end(__rg);
840 for (; __first != __last; ++__first)
841 push_back(*__first);
842 }
843 }
844#endif
845
846 /**
847 * @brief Construct string from an initializer %list.
848 * @param __l std::initializer_list of characters.
849 * @param __a Allocator to use (default is default allocator).
850 */
851 _GLIBCXX20_CONSTEXPR
852 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
853 : _M_dataplus(_M_local_data(), __a)
854 { _M_construct(__l.begin(), __l.end(), std::forward_iterator_tag()); }
855
856 _GLIBCXX20_CONSTEXPR
857 basic_string(const basic_string& __str, const _Alloc& __a)
858 : _M_dataplus(_M_local_data(), __a)
859 { _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag()); }
860
861 _GLIBCXX20_CONSTEXPR
862 basic_string(basic_string&& __str, const _Alloc& __a)
863 noexcept(_Alloc_traits::_S_always_equal())
864 : _M_dataplus(_M_local_data(), __a)
865 {
866 if (__str._M_is_local())
867 {
868 _M_init_local_buf();
869 traits_type::copy(_M_local_buf, __str._M_local_buf,
870 __str.length() + 1);
871 _M_length(__str.length());
872 __str._M_set_length(0);
873 }
874 else if (_Alloc_traits::_S_always_equal()
875 || __str.get_allocator() == __a)
876 {
877 _M_data(__str._M_data());
878 _M_length(__str.length());
879 _M_capacity(__str._M_allocated_capacity);
880 __str._M_data(__str._M_use_local_data());
881 __str._M_set_length(0);
882 }
883 else
884 _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag());
885 }
886#endif // C++11
887
888#if __cplusplus >= 202100L
889 basic_string(nullptr_t) = delete;
890 basic_string& operator=(nullptr_t) = delete;
891#endif // C++23
892
893 /**
894 * @brief Construct string as copy of a range.
895 * @param __beg Start of range.
896 * @param __end End of range.
897 * @param __a Allocator to use (default is default allocator).
898 */
899#if __cplusplus >= 201103L
900 template<typename _InputIterator,
901 typename = std::_RequireInputIter<_InputIterator>>
902#else
903 template<typename _InputIterator>
904#endif
905 _GLIBCXX20_CONSTEXPR
906 basic_string(_InputIterator __beg, _InputIterator __end,
907 const _Alloc& __a = _Alloc())
908 : _M_dataplus(_M_local_data(), __a), _M_string_length(0)
909 {
910#if __cplusplus >= 201103L
911 _M_construct(__beg, __end, std::__iterator_category(__beg));
912#else
913 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
914 _M_construct_aux(__beg, __end, _Integral());
915#endif
916 }
917
918#ifdef __glibcxx_string_view // >= C++17
919 /**
920 * @brief Construct string from a substring of a string_view.
921 * @param __t Source object convertible to string view.
922 * @param __pos The index of the first character to copy from __t.
923 * @param __n The number of characters to copy from __t.
924 * @param __a Allocator to use.
925 */
926 template<typename _Tp,
928 _GLIBCXX20_CONSTEXPR
929 basic_string(const _Tp& __t, size_type __pos, size_type __n,
930 const _Alloc& __a = _Alloc())
931 : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
932
933 /**
934 * @brief Construct string from a string_view.
935 * @param __t Source object convertible to string view.
936 * @param __a Allocator to use (default is default allocator).
937 */
938 template<typename _Tp, typename = _If_sv<_Tp, void>>
939 _GLIBCXX20_CONSTEXPR
940 explicit
941 basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
942 : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
943#endif // C++17
944
945 /**
946 * @brief Destroy the string instance.
947 */
948 _GLIBCXX20_CONSTEXPR
950 { _M_dispose(); }
951
952 /**
953 * @brief Assign the value of @a str to this string.
954 * @param __str Source string.
955 */
956 _GLIBCXX20_CONSTEXPR
959 {
960 return this->assign(__str);
961 }
962
963 /**
964 * @brief Copy contents of @a s into this string.
965 * @param __s Source null-terminated string.
966 */
967 _GLIBCXX20_CONSTEXPR
969 operator=(const _CharT* __s)
970 { return this->assign(__s); }
971
972 /**
973 * @brief Set value to string of length 1.
974 * @param __c Source character.
975 *
976 * Assigning to a character makes this string length 1 and
977 * (*this)[0] == @a c.
978 */
979 _GLIBCXX20_CONSTEXPR
981 operator=(_CharT __c)
982 {
983 this->assign(1, __c);
984 return *this;
985 }
986
987#if __cplusplus >= 201103L
988 /**
989 * @brief Move assign the value of @a str to this string.
990 * @param __str Source string.
991 *
992 * The contents of @a str are moved into this string (without copying).
993 * @a str is a valid, but unspecified string.
994 */
995 // _GLIBCXX_RESOLVE_LIB_DEFECTS
996 // 2063. Contradictory requirements for string move assignment
997 _GLIBCXX20_CONSTEXPR
1000 noexcept(_Alloc_traits::_S_nothrow_move())
1001 {
1002 const bool __equal_allocs = _Alloc_traits::_S_always_equal()
1003 || _M_get_allocator() == __str._M_get_allocator();
1004 if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
1005 && !__equal_allocs)
1006 {
1007 // Destroy existing storage before replacing allocator.
1008 _M_destroy(_M_allocated_capacity);
1009 _M_data(_M_local_data());
1010 _M_set_length(0);
1011 }
1012 // Replace allocator if POCMA is true.
1013 std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator());
1014
1015 if (__str._M_is_local())
1016 {
1017 // We've always got room for a short string, just copy it
1018 // (unless this is a self-move, because that would violate the
1019 // char_traits::copy precondition that the ranges don't overlap).
1020 if (__builtin_expect(std::__addressof(__str) != this, true))
1021 {
1022 if (__str.size())
1023 this->_S_copy(_M_data(), __str._M_data(), __str.size());
1024 _M_set_length(__str.size());
1025 }
1026 }
1027 else if (_Alloc_traits::_S_propagate_on_move_assign() || __equal_allocs)
1028 {
1029 // Just move the allocated pointer, our allocator can free it.
1030 pointer __data = nullptr;
1031 size_type __capacity;
1032 if (!_M_is_local())
1033 {
1034 if (__equal_allocs)
1035 {
1036 // __str can reuse our existing storage.
1037 __data = _M_data();
1038 __capacity = _M_allocated_capacity;
1039 }
1040 else // __str can't use it, so free it.
1041 _M_destroy(_M_allocated_capacity);
1042 }
1043
1044 _M_data(__str._M_data());
1045 _M_length(__str.length());
1046 _M_capacity(__str._M_allocated_capacity);
1047 if (__data)
1048 {
1049 __str._M_data(__data);
1050 __str._M_capacity(__capacity);
1051 }
1052 else
1053 __str._M_data(__str._M_use_local_data());
1054 }
1055 else // Need to do a deep copy
1056 _M_assign(__str);
1057 __str.clear();
1058 return *this;
1059 }
1060
1061 /**
1062 * @brief Set value to string constructed from initializer %list.
1063 * @param __l std::initializer_list.
1064 */
1065 _GLIBCXX20_CONSTEXPR
1068 {
1069 this->assign(__l.begin(), __l.size());
1070 return *this;
1071 }
1072#endif // C++11
1073
1074#ifdef __glibcxx_string_view // >= C++17
1075 /**
1076 * @brief Set value to string constructed from a string_view.
1077 * @param __svt An object convertible to string_view.
1078 */
1079 template<typename _Tp>
1080 _GLIBCXX20_CONSTEXPR
1081 _If_sv<_Tp, basic_string&>
1082 operator=(const _Tp& __svt)
1083 { return this->assign(__svt); }
1084
1085 /**
1086 * @brief Convert to a string_view.
1087 * @return A string_view.
1088 */
1089 _GLIBCXX20_CONSTEXPR
1090 operator __sv_type() const noexcept
1091 { return __sv_type(data(), size()); }
1092#endif // C++17
1093
1094 // Iterators:
1095 /**
1096 * Returns a read/write iterator that points to the first character in
1097 * the %string.
1098 */
1099 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1100 iterator
1101 begin() _GLIBCXX_NOEXCEPT
1102 { return iterator(_M_data()); }
1103
1104 /**
1105 * Returns a read-only (constant) iterator that points to the first
1106 * character in the %string.
1107 */
1108 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1109 const_iterator
1110 begin() const _GLIBCXX_NOEXCEPT
1111 { return const_iterator(_M_data()); }
1112
1113 /**
1114 * Returns a read/write iterator that points one past the last
1115 * character in the %string.
1116 */
1117 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1118 iterator
1119 end() _GLIBCXX_NOEXCEPT
1120 { return iterator(_M_data() + this->size()); }
1121
1122 /**
1123 * Returns a read-only (constant) iterator that points one past the
1124 * last character in the %string.
1125 */
1126 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1127 const_iterator
1128 end() const _GLIBCXX_NOEXCEPT
1129 { return const_iterator(_M_data() + this->size()); }
1130
1131 /**
1132 * Returns a read/write reverse iterator that points to the last
1133 * character in the %string. Iteration is done in reverse element
1134 * order.
1135 */
1136 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1138 rbegin() _GLIBCXX_NOEXCEPT
1139 { return reverse_iterator(this->end()); }
1140
1141 /**
1142 * Returns a read-only (constant) reverse iterator that points
1143 * to the last character in the %string. Iteration is done in
1144 * reverse element order.
1145 */
1146 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1147 const_reverse_iterator
1148 rbegin() const _GLIBCXX_NOEXCEPT
1149 { return const_reverse_iterator(this->end()); }
1150
1151 /**
1152 * Returns a read/write reverse iterator that points to one before the
1153 * first character in the %string. Iteration is done in reverse
1154 * element order.
1155 */
1156 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1158 rend() _GLIBCXX_NOEXCEPT
1159 { return reverse_iterator(this->begin()); }
1160
1161 /**
1162 * Returns a read-only (constant) reverse iterator that points
1163 * to one before the first character in the %string. Iteration
1164 * is done in reverse element order.
1165 */
1166 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1167 const_reverse_iterator
1168 rend() const _GLIBCXX_NOEXCEPT
1169 { return const_reverse_iterator(this->begin()); }
1170
1171#if __cplusplus >= 201103L
1172 /**
1173 * Returns a read-only (constant) iterator that points to the first
1174 * character in the %string.
1175 */
1176 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1177 const_iterator
1178 cbegin() const noexcept
1179 { return const_iterator(this->_M_data()); }
1180
1181 /**
1182 * Returns a read-only (constant) iterator that points one past the
1183 * last character in the %string.
1184 */
1185 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1186 const_iterator
1187 cend() const noexcept
1188 { return const_iterator(this->_M_data() + this->size()); }
1189
1190 /**
1191 * Returns a read-only (constant) reverse iterator that points
1192 * to the last character in the %string. Iteration is done in
1193 * reverse element order.
1194 */
1195 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1196 const_reverse_iterator
1197 crbegin() const noexcept
1198 { return const_reverse_iterator(this->end()); }
1199
1200 /**
1201 * Returns a read-only (constant) reverse iterator that points
1202 * to one before the first character in the %string. Iteration
1203 * is done in reverse element order.
1204 */
1205 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1206 const_reverse_iterator
1207 crend() const noexcept
1208 { return const_reverse_iterator(this->begin()); }
1209#endif
1210
1211 public:
1212 // Capacity:
1213 /// Returns the number of characters in the string, not including any
1214 /// null-termination.
1215 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1216 size_type
1217 size() const _GLIBCXX_NOEXCEPT
1218 {
1219 size_type __sz = _M_string_length;
1220 if (__sz > max_size ())
1221 __builtin_unreachable();
1222 return __sz;
1223 }
1224
1225 /// Returns the number of characters in the string, not including any
1226 /// null-termination.
1227 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1228 size_type
1229 length() const _GLIBCXX_NOEXCEPT
1230 { return size(); }
1231
1232 /// Returns the size() of the largest possible %string.
1233 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1234 size_type
1235 max_size() const _GLIBCXX_NOEXCEPT
1236 {
1237 const size_t __diffmax
1238 = __gnu_cxx::__numeric_traits<ptrdiff_t>::__max / sizeof(_CharT);
1239 const size_t __allocmax = _Alloc_traits::max_size(_M_get_allocator());
1240 return (std::min)(__diffmax, __allocmax) - 1;
1241 }
1242
1243 /**
1244 * @brief Resizes the %string to the specified number of characters.
1245 * @param __n Number of characters the %string should contain.
1246 * @param __c Character to fill any new elements.
1247 *
1248 * This function will %resize the %string to the specified
1249 * number of characters. If the number is smaller than the
1250 * %string's current size the %string is truncated, otherwise
1251 * the %string is extended and new elements are %set to @a __c.
1252 */
1253 _GLIBCXX20_CONSTEXPR
1254 void
1255 resize(size_type __n, _CharT __c);
1256
1257 /**
1258 * @brief Resizes the %string to the specified number of characters.
1259 * @param __n Number of characters the %string should contain.
1260 *
1261 * This function will resize the %string to the specified length. If
1262 * the new size is smaller than the %string's current size the %string
1263 * is truncated, otherwise the %string is extended and new characters
1264 * are default-constructed. For basic types such as char, this means
1265 * setting them to 0.
1266 */
1267 _GLIBCXX20_CONSTEXPR
1268 void
1269 resize(size_type __n)
1270 { this->resize(__n, _CharT()); }
1271
1272#if __cplusplus >= 201103L
1273#pragma GCC diagnostic push
1274#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1275 /// A non-binding request to reduce capacity() to size().
1276 _GLIBCXX20_CONSTEXPR
1277 void
1278 shrink_to_fit() noexcept
1279 { reserve(); }
1280#pragma GCC diagnostic pop
1281#endif
1282
1283#ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23
1284 /** Resize the string and call a function to fill it.
1285 *
1286 * @param __n The maximum size requested.
1287 * @param __op A callable object that writes characters to the string.
1288 *
1289 * This is a low-level function that is easy to misuse, be careful.
1290 *
1291 * Calling `str.resize_and_overwrite(n, op)` will reserve at least `n`
1292 * characters in `str`, evaluate `n2 = std::move(op)(str.data(), n)`,
1293 * and finally set the string length to `n2` (adding a null terminator
1294 * at the end). The function object `op` is allowed to write to the
1295 * extra capacity added by the initial reserve operation, which is not
1296 * allowed if you just call `str.reserve(n)` yourself.
1297 *
1298 * This can be used to efficiently fill a `string` buffer without the
1299 * overhead of zero-initializing characters that will be overwritten
1300 * anyway.
1301 *
1302 * The callable `op` must not access the string directly (only through
1303 * the pointer passed as its first argument), must not write more than
1304 * `n` characters to the string, must return a value no greater than `n`,
1305 * and must ensure that all characters up to the returned length are
1306 * valid after it returns (i.e. there must be no uninitialized values
1307 * left in the string after the call, because accessing them would
1308 * have undefined behaviour). If `op` exits by throwing an exception
1309 * the behaviour is undefined.
1310 *
1311 * @since C++23
1312 */
1313 template<typename _Operation>
1314 constexpr void
1315 resize_and_overwrite(size_type __n, _Operation __op);
1316#endif
1317
1318#if __cplusplus >= 201103L
1319 /// Non-standard version of resize_and_overwrite for C++11 and above.
1320 template<typename _Operation>
1321 _GLIBCXX20_CONSTEXPR void
1322 __resize_and_overwrite(size_type __n, _Operation __op);
1323#endif
1324
1325 /**
1326 * Returns the total number of characters that the %string can hold
1327 * before needing to allocate more memory.
1328 */
1329 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1330 size_type
1331 capacity() const _GLIBCXX_NOEXCEPT
1332 {
1333 size_t __sz = _M_is_local() ? size_type(_S_local_capacity)
1334 : _M_allocated_capacity;
1335 if (__sz < _S_local_capacity || __sz > max_size ())
1336 __builtin_unreachable();
1337 return __sz;
1338 }
1339
1340 /**
1341 * @brief Attempt to preallocate enough memory for specified number of
1342 * characters.
1343 * @param __res_arg Number of characters required.
1344 * @throw std::length_error If @a __res_arg exceeds @c max_size().
1345 *
1346 * This function attempts to reserve enough memory for the
1347 * %string to hold the specified number of characters. If the
1348 * number requested is more than max_size(), length_error is
1349 * thrown.
1350 *
1351 * The advantage of this function is that if optimal code is a
1352 * necessity and the user can determine the string length that will be
1353 * required, the user can reserve the memory in %advance, and thus
1354 * prevent a possible reallocation of memory and copying of %string
1355 * data.
1356 */
1357 _GLIBCXX20_CONSTEXPR
1358 void
1359 reserve(size_type __res_arg);
1360
1361 /**
1362 * Equivalent to shrink_to_fit().
1363 */
1364#if __cplusplus >= 202002L
1365 [[deprecated("use shrink_to_fit() instead")]]
1366#endif
1367 _GLIBCXX20_CONSTEXPR
1368 void
1370
1371 /**
1372 * Erases the string, making it empty.
1373 */
1374 _GLIBCXX20_CONSTEXPR
1375 void
1376 clear() _GLIBCXX_NOEXCEPT
1377 { _M_set_length(0); }
1378
1379 /**
1380 * Returns true if the %string is empty. Equivalent to
1381 * <code>*this == ""</code>.
1382 */
1383 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1384 bool
1385 empty() const _GLIBCXX_NOEXCEPT
1386 { return _M_string_length == 0; }
1387
1388 // Element access:
1389 /**
1390 * @brief Subscript access to the data contained in the %string.
1391 * @param __pos The index of the character to access.
1392 * @return Read-only (constant) reference to the character.
1393 *
1394 * This operator allows for easy, array-style, data access.
1395 * Note that data access with this operator is unchecked and
1396 * out_of_range lookups are not defined. (For checked lookups
1397 * see at().)
1398 */
1399 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1400 const_reference
1401 operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
1402 {
1403 __glibcxx_assert(__pos <= size());
1404 return _M_data()[__pos];
1405 }
1406
1407 /**
1408 * @brief Subscript access to the data contained in the %string.
1409 * @param __pos The index of the character to access.
1410 * @return Read/write reference to the character.
1411 *
1412 * This operator allows for easy, array-style, data access.
1413 * Note that data access with this operator is unchecked and
1414 * out_of_range lookups are not defined. (For checked lookups
1415 * see at().)
1416 */
1417 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1418 reference
1419 operator[](size_type __pos)
1420 {
1421 // Allow pos == size() both in C++98 mode, as v3 extension,
1422 // and in C++11 mode.
1423 __glibcxx_assert(__pos <= size());
1424 // In pedantic mode be strict in C++98 mode.
1425 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
1426 return _M_data()[__pos];
1427 }
1428
1429 /**
1430 * @brief Provides access to the data contained in the %string.
1431 * @param __n The index of the character to access.
1432 * @return Read-only (const) reference to the character.
1433 * @throw std::out_of_range If @a n is an invalid index.
1434 *
1435 * This function provides for safer data access. The parameter is
1436 * first checked that it is in the range of the string. The function
1437 * throws out_of_range if the check fails.
1438 */
1439 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1440 const_reference
1441 at(size_type __n) const
1442 {
1443 if (__n >= this->size())
1444 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1445 "(which is %zu) >= this->size() "
1446 "(which is %zu)"),
1447 __n, this->size());
1448 return _M_data()[__n];
1449 }
1450
1451 /**
1452 * @brief Provides access to the data contained in the %string.
1453 * @param __n The index of the character to access.
1454 * @return Read/write reference to the character.
1455 * @throw std::out_of_range If @a n is an invalid index.
1456 *
1457 * This function provides for safer data access. The parameter is
1458 * first checked that it is in the range of the string. The function
1459 * throws out_of_range if the check fails.
1460 */
1461 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1462 reference
1463 at(size_type __n)
1464 {
1465 if (__n >= size())
1466 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1467 "(which is %zu) >= this->size() "
1468 "(which is %zu)"),
1469 __n, this->size());
1470 return _M_data()[__n];
1471 }
1472
1473#if __cplusplus >= 201103L
1474 /**
1475 * Returns a read/write reference to the data at the first
1476 * element of the %string.
1477 */
1478 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1479 reference
1480 front() noexcept
1481 {
1482 __glibcxx_assert(!empty());
1483 return operator[](0);
1484 }
1485
1486 /**
1487 * Returns a read-only (constant) reference to the data at the first
1488 * element of the %string.
1489 */
1490 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1491 const_reference
1492 front() const noexcept
1493 {
1494 __glibcxx_assert(!empty());
1495 return operator[](0);
1496 }
1497
1498 /**
1499 * Returns a read/write reference to the data at the last
1500 * element of the %string.
1501 */
1502 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1503 reference
1504 back() noexcept
1505 {
1506 __glibcxx_assert(!empty());
1507 return operator[](this->size() - 1);
1508 }
1509
1510 /**
1511 * Returns a read-only (constant) reference to the data at the
1512 * last element of the %string.
1513 */
1514 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1515 const_reference
1516 back() const noexcept
1517 {
1518 __glibcxx_assert(!empty());
1519 return operator[](this->size() - 1);
1520 }
1521#endif
1522
1523 // Modifiers:
1524 /**
1525 * @brief Append a string to this string.
1526 * @param __str The string to append.
1527 * @return Reference to this string.
1528 */
1529 _GLIBCXX20_CONSTEXPR
1532 { return this->append(__str); }
1533
1534 /**
1535 * @brief Append a C string.
1536 * @param __s The C string to append.
1537 * @return Reference to this string.
1538 */
1539 _GLIBCXX20_CONSTEXPR
1541 operator+=(const _CharT* __s)
1542 { return this->append(__s); }
1543
1544 /**
1545 * @brief Append a character.
1546 * @param __c The character to append.
1547 * @return Reference to this string.
1548 */
1549 _GLIBCXX20_CONSTEXPR
1551 operator+=(_CharT __c)
1552 {
1553 this->push_back(__c);
1554 return *this;
1555 }
1556
1557#if __cplusplus >= 201103L
1558 /**
1559 * @brief Append an initializer_list of characters.
1560 * @param __l The initializer_list of characters to be appended.
1561 * @return Reference to this string.
1562 */
1563 _GLIBCXX20_CONSTEXPR
1566 { return this->append(__l.begin(), __l.size()); }
1567#endif // C++11
1568
1569#ifdef __glibcxx_string_view // >= C++17
1570 /**
1571 * @brief Append a string_view.
1572 * @param __svt An object convertible to string_view to be appended.
1573 * @return Reference to this string.
1574 */
1575 template<typename _Tp>
1576 _GLIBCXX20_CONSTEXPR
1577 _If_sv<_Tp, basic_string&>
1578 operator+=(const _Tp& __svt)
1579 { return this->append(__svt); }
1580#endif // C++17
1581
1582 /**
1583 * @brief Append a string to this string.
1584 * @param __str The string to append.
1585 * @return Reference to this string.
1586 */
1587 _GLIBCXX20_CONSTEXPR
1589 append(const basic_string& __str)
1590 { return this->append(__str._M_data(), __str.size()); }
1591
1592 /**
1593 * @brief Append a substring.
1594 * @param __str The string to append.
1595 * @param __pos Index of the first character of str to append.
1596 * @param __n The number of characters to append.
1597 * @return Reference to this string.
1598 * @throw std::out_of_range if @a __pos is not a valid index.
1599 *
1600 * This function appends @a __n characters from @a __str
1601 * starting at @a __pos to this string. If @a __n is is larger
1602 * than the number of available characters in @a __str, the
1603 * remainder of @a __str is appended.
1604 */
1605 _GLIBCXX20_CONSTEXPR
1607 append(const basic_string& __str, size_type __pos, size_type __n = npos)
1608 { return this->append(__str._M_data()
1609 + __str._M_check(__pos, "basic_string::append"),
1610 __str._M_limit(__pos, __n)); }
1611
1612 /**
1613 * @brief Append a C substring.
1614 * @param __s The C string to append.
1615 * @param __n The number of characters to append.
1616 * @return Reference to this string.
1617 */
1618 _GLIBCXX20_CONSTEXPR
1620 append(const _CharT* __s, size_type __n)
1621 {
1622 __glibcxx_requires_string_len(__s, __n);
1623 _M_check_length(size_type(0), __n, "basic_string::append");
1624 return _M_append(__s, __n);
1625 }
1626
1627 /**
1628 * @brief Append a C string.
1629 * @param __s The C string to append.
1630 * @return Reference to this string.
1631 */
1632 _GLIBCXX20_CONSTEXPR
1634 append(const _CharT* __s)
1635 {
1636 __glibcxx_requires_string(__s);
1637 const size_type __n = traits_type::length(__s);
1638 _M_check_length(size_type(0), __n, "basic_string::append");
1639 return _M_append(__s, __n);
1640 }
1641
1642 /**
1643 * @brief Append multiple characters.
1644 * @param __n The number of characters to append.
1645 * @param __c The character to use.
1646 * @return Reference to this string.
1647 *
1648 * Appends __n copies of __c to this string.
1649 */
1650 _GLIBCXX20_CONSTEXPR
1652 append(size_type __n, _CharT __c)
1653 { return _M_replace_aux(this->size(), size_type(0), __n, __c); }
1654
1655#if __glibcxx_containers_ranges // C++ >= 23
1656 /**
1657 * @brief Append a range to the string.
1658 * @param __rg A range of values that are convertible to `value_type`.
1659 * @since C++23
1660 *
1661 * The range `__rg` is allowed to overlap with `*this`.
1662 */
1663 template<__detail::__container_compatible_range<_CharT> _Rg>
1664 constexpr basic_string&
1665 append_range(_Rg&& __rg)
1666 {
1667 // N.B. __rg may overlap with *this, so we must copy from __rg before
1668 // existing elements or iterators referring to *this are invalidated.
1669 // e.g. in s.append_range(views::concat(s, str)), rg overlaps s.
1671 {
1672 const auto __len = size_type(ranges::distance(__rg));
1673
1674 // Don't care if this addition wraps around, we check it below:
1675 const size_type __newlen = size() + __len;
1676
1677 if ((capacity() - size()) >= __len)
1678 _S_copy_range(_M_data() + size(), std::forward<_Rg>(__rg),
1679 __len);
1680 else
1681 {
1682 _M_check_length(0, __len, "basic_string::append_range");
1683 basic_string __s(_M_get_allocator());
1684 __s.reserve(__newlen);
1685 _S_copy_range(__s._M_data() + size(), std::forward<_Rg>(__rg),
1686 __len);
1687 _S_copy(__s._M_data(), _M_data(), size());
1688 if (!_M_is_local())
1689 _M_destroy(_M_allocated_capacity);
1690 _M_data(__s._M_data());
1691 _M_capacity(__s._M_allocated_capacity);
1692 __s._M_data(__s._M_local_data());
1693 __s._M_length(0);
1694 }
1695 _M_set_length(__newlen); // adds null-terminator
1696 }
1697 else
1698 {
1699 basic_string __s(from_range, std::forward<_Rg>(__rg),
1700 _M_get_allocator());
1701 append(__s);
1702 }
1703 return *this;
1704 }
1705#endif
1706
1707#if __cplusplus >= 201103L
1708 /**
1709 * @brief Append an initializer_list of characters.
1710 * @param __l The initializer_list of characters to append.
1711 * @return Reference to this string.
1712 */
1713 _GLIBCXX20_CONSTEXPR
1716 { return this->append(__l.begin(), __l.size()); }
1717#endif // C++11
1718
1719 /**
1720 * @brief Append a range of characters.
1721 * @param __first Iterator referencing the first character to append.
1722 * @param __last Iterator marking the end of the range.
1723 * @return Reference to this string.
1724 *
1725 * Appends characters in the range [__first,__last) to this string.
1726 */
1727#if __cplusplus >= 201103L
1728 template<class _InputIterator,
1729 typename = std::_RequireInputIter<_InputIterator>>
1730 _GLIBCXX20_CONSTEXPR
1731#else
1732 template<class _InputIterator>
1733#endif
1735 append(_InputIterator __first, _InputIterator __last)
1736 { return this->replace(end(), end(), __first, __last); }
1737
1738#ifdef __glibcxx_string_view
1739 /**
1740 * @brief Append a string_view.
1741 * @param __svt An object convertible to string_view to be appended.
1742 * @return Reference to this string.
1743 */
1744 template<typename _Tp>
1745 _GLIBCXX20_CONSTEXPR
1746 _If_sv<_Tp, basic_string&>
1747 append(const _Tp& __svt)
1748 {
1749 __sv_type __sv = __svt;
1750 return this->append(__sv.data(), __sv.size());
1751 }
1752
1753 /**
1754 * @brief Append a range of characters from a string_view.
1755 * @param __svt An object convertible to string_view to be appended from.
1756 * @param __pos The position in the string_view to append from.
1757 * @param __n The number of characters to append from the string_view.
1758 * @return Reference to this string.
1759 */
1760 template<typename _Tp>
1761 _GLIBCXX20_CONSTEXPR
1762 _If_sv<_Tp, basic_string&>
1763 append(const _Tp& __svt, size_type __pos, size_type __n = npos)
1764 {
1765 __sv_type __sv = __svt;
1766 return _M_append(__sv.data()
1767 + std::__sv_check(__sv.size(), __pos, "basic_string::append"),
1768 std::__sv_limit(__sv.size(), __pos, __n));
1769 }
1770#endif // C++17
1771
1772 /**
1773 * @brief Append a single character.
1774 * @param __c Character to append.
1775 */
1776 _GLIBCXX20_CONSTEXPR
1777 void
1778 push_back(_CharT __c)
1779 {
1780 const size_type __size = this->size();
1781 if (__size + 1 > this->capacity())
1782 this->_M_mutate(__size, size_type(0), 0, size_type(1));
1783 traits_type::assign(this->_M_data()[__size], __c);
1784 this->_M_set_length(__size + 1);
1785 }
1786
1787 /**
1788 * @brief Set value to contents of another string.
1789 * @param __str Source string to use.
1790 * @return Reference to this string.
1791 */
1792 _GLIBCXX20_CONSTEXPR
1794 assign(const basic_string& __str)
1795 {
1796#if __cplusplus >= 201103L
1797 if (_Alloc_traits::_S_propagate_on_copy_assign())
1798 {
1799 if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
1800 && _M_get_allocator() != __str._M_get_allocator())
1801 {
1802 // Propagating allocator cannot free existing storage so must
1803 // deallocate it before replacing current allocator.
1804 if (__str.size() <= _S_local_capacity)
1805 {
1806 _M_destroy(_M_allocated_capacity);
1807 _M_data(_M_use_local_data());
1808 _M_set_length(0);
1809 }
1810 else
1811 {
1812 const auto __len = __str.size();
1813 auto __alloc = __str._M_get_allocator();
1814 // If this allocation throws there are no effects:
1815 auto __r = _S_allocate_at_least(__alloc, __len + 1);
1816 _M_destroy(_M_allocated_capacity);
1817 _M_data(__r.__ptr);
1818 _M_capacity(__r.__count - 1);
1819 _M_set_length(__len);
1820 }
1821 }
1822 std::__alloc_on_copy(_M_get_allocator(), __str._M_get_allocator());
1823 }
1824#endif
1825 this->_M_assign(__str);
1826 return *this;
1827 }
1828
1829#if __cplusplus >= 201103L
1830 /**
1831 * @brief Set value to contents of another string.
1832 * @param __str Source string to use.
1833 * @return Reference to this string.
1834 *
1835 * This function sets this string to the exact contents of @a __str.
1836 * @a __str is a valid, but unspecified string.
1837 */
1838 _GLIBCXX20_CONSTEXPR
1841 noexcept(_Alloc_traits::_S_nothrow_move())
1842 {
1843 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1844 // 2063. Contradictory requirements for string move assignment
1845 return *this = std::move(__str);
1846 }
1847#endif // C++11
1848
1849 /**
1850 * @brief Set value to a substring of a string.
1851 * @param __str The string to use.
1852 * @param __pos Index of the first character of str.
1853 * @param __n Number of characters to use.
1854 * @return Reference to this string.
1855 * @throw std::out_of_range if @a pos is not a valid index.
1856 *
1857 * This function sets this string to the substring of @a __str
1858 * consisting of @a __n characters at @a __pos. If @a __n is
1859 * is larger than the number of available characters in @a
1860 * __str, the remainder of @a __str is used.
1861 */
1862 _GLIBCXX20_CONSTEXPR
1864 assign(const basic_string& __str, size_type __pos, size_type __n = npos)
1865 { return _M_replace(size_type(0), this->size(), __str._M_data()
1866 + __str._M_check(__pos, "basic_string::assign"),
1867 __str._M_limit(__pos, __n)); }
1868
1869 /**
1870 * @brief Set value to a C substring.
1871 * @param __s The C string to use.
1872 * @param __n Number of characters to use.
1873 * @return Reference to this string.
1874 *
1875 * This function sets the value of this string to the first @a __n
1876 * characters of @a __s. If @a __n is is larger than the number of
1877 * available characters in @a __s, the remainder of @a __s is used.
1878 */
1879 _GLIBCXX20_CONSTEXPR
1881 assign(const _CharT* __s, size_type __n)
1882 {
1883 __glibcxx_requires_string_len(__s, __n);
1884 return _M_replace(size_type(0), this->size(), __s, __n);
1885 }
1886
1887 /**
1888 * @brief Set value to contents of a C string.
1889 * @param __s The C string to use.
1890 * @return Reference to this string.
1891 *
1892 * This function sets the value of this string to the value of @a __s.
1893 * The data is copied, so there is no dependence on @a __s once the
1894 * function returns.
1895 */
1896 _GLIBCXX20_CONSTEXPR
1898 assign(const _CharT* __s)
1899 {
1900 __glibcxx_requires_string(__s);
1901 return _M_replace(size_type(0), this->size(), __s,
1902 traits_type::length(__s));
1903 }
1904
1905 /**
1906 * @brief Set value to multiple characters.
1907 * @param __n Length of the resulting string.
1908 * @param __c The character to use.
1909 * @return Reference to this string.
1910 *
1911 * This function sets the value of this string to @a __n copies of
1912 * character @a __c.
1913 */
1914 _GLIBCXX20_CONSTEXPR
1916 assign(size_type __n, _CharT __c)
1917 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
1918
1919 /**
1920 * @brief Set value to a range of characters.
1921 * @param __first Iterator referencing the first character to append.
1922 * @param __last Iterator marking the end of the range.
1923 * @return Reference to this string.
1924 *
1925 * Sets value of string to characters in the range [__first,__last).
1926 */
1927#if __cplusplus >= 201103L
1928#pragma GCC diagnostic push
1929#pragma GCC diagnostic ignored "-Wc++17-extensions"
1930 template<class _InputIterator,
1931 typename = std::_RequireInputIter<_InputIterator>>
1932 _GLIBCXX20_CONSTEXPR
1934 assign(_InputIterator __first, _InputIterator __last)
1935 {
1936 using _IterTraits = iterator_traits<_InputIterator>;
1937 if constexpr (is_pointer<decltype(std::__niter_base(__first))>::value
1938 && is_same<typename _IterTraits::value_type,
1939 _CharT>::value)
1940 {
1941 __glibcxx_requires_valid_range(__first, __last);
1942 return _M_replace(size_type(0), size(),
1943 std::__niter_base(__first), __last - __first);
1944 }
1945#if __cplusplus >= 202002L
1946 else if constexpr (contiguous_iterator<_InputIterator>
1947 && is_same_v<iter_value_t<_InputIterator>,
1948 _CharT>)
1949 {
1950 __glibcxx_requires_valid_range(__first, __last);
1951 return _M_replace(size_type(0), size(),
1952 std::to_address(__first), __last - __first);
1953 }
1954#endif
1955 else
1956 return *this = basic_string(__first, __last, get_allocator());
1957 }
1958#pragma GCC diagnostic pop
1959#else
1960 template<class _InputIterator>
1962 assign(_InputIterator __first, _InputIterator __last)
1963 { return this->replace(begin(), end(), __first, __last); }
1964#endif
1965
1966#if __glibcxx_containers_ranges // C++ >= 23
1967 /**
1968 * @brief Assign a range to the string.
1969 * @param __rg A range of values that are convertible to `value_type`.
1970 * @since C++23
1971 *
1972 * The range `__rg` is allowed to overlap with `*this`.
1973 */
1974 template<__detail::__container_compatible_range<_CharT> _Rg>
1975 constexpr basic_string&
1976 assign_range(_Rg&& __rg)
1977 {
1978 basic_string __s(from_range, std::forward<_Rg>(__rg),
1979 _M_get_allocator());
1980 assign(std::move(__s));
1981 return *this;
1982 }
1983#endif
1984
1985#if __cplusplus >= 201103L
1986 /**
1987 * @brief Set value to an initializer_list of characters.
1988 * @param __l The initializer_list of characters to assign.
1989 * @return Reference to this string.
1990 */
1991 _GLIBCXX20_CONSTEXPR
1994 {
1995 // The initializer_list array cannot alias the characters in *this
1996 // so we don't need to use replace to that case.
1997 const size_type __n = __l.size();
1998 if (__n > capacity())
1999 *this = basic_string(__l.begin(), __l.end(), get_allocator());
2000 else
2001 {
2002 if (__n)
2003 _S_copy(_M_data(), __l.begin(), __n);
2004 _M_set_length(__n);
2005 }
2006 return *this;
2007 }
2008#endif // C++11
2009
2010#ifdef __glibcxx_string_view // >= C++17
2011 /**
2012 * @brief Set value from a string_view.
2013 * @param __svt The source object convertible to string_view.
2014 * @return Reference to this string.
2015 */
2016 template<typename _Tp>
2017 _GLIBCXX20_CONSTEXPR
2018 _If_sv<_Tp, basic_string&>
2019 assign(const _Tp& __svt)
2020 {
2021 __sv_type __sv = __svt;
2022 return this->assign(__sv.data(), __sv.size());
2023 }
2024
2025 /**
2026 * @brief Set value from a range of characters in a string_view.
2027 * @param __svt The source object convertible to string_view.
2028 * @param __pos The position in the string_view to assign from.
2029 * @param __n The number of characters to assign.
2030 * @return Reference to this string.
2031 */
2032 template<typename _Tp>
2033 _GLIBCXX20_CONSTEXPR
2034 _If_sv<_Tp, basic_string&>
2035 assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
2036 {
2037 __sv_type __sv = __svt;
2038 return _M_replace(size_type(0), this->size(),
2039 __sv.data()
2040 + std::__sv_check(__sv.size(), __pos, "basic_string::assign"),
2041 std::__sv_limit(__sv.size(), __pos, __n));
2042 }
2043#endif // C++17
2044
2045#if __cplusplus >= 201103L
2046 /**
2047 * @brief Insert multiple characters.
2048 * @param __p Const_iterator referencing location in string to
2049 * insert at.
2050 * @param __n Number of characters to insert
2051 * @param __c The character to insert.
2052 * @return Iterator referencing the first inserted char.
2053 * @throw std::length_error If new length exceeds @c max_size().
2054 *
2055 * Inserts @a __n copies of character @a __c starting at the
2056 * position referenced by iterator @a __p. If adding
2057 * characters causes the length to exceed max_size(),
2058 * length_error is thrown. The value of the string doesn't
2059 * change if an error is thrown.
2060 */
2061 _GLIBCXX20_CONSTEXPR
2062 iterator
2063 insert(const_iterator __p, size_type __n, _CharT __c)
2064 {
2065 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
2066 const size_type __pos = __p - begin();
2067 this->replace(__p, __p, __n, __c);
2068 return iterator(this->_M_data() + __pos);
2069 }
2070#else
2071 /**
2072 * @brief Insert multiple characters.
2073 * @param __p Iterator referencing location in string to insert at.
2074 * @param __n Number of characters to insert
2075 * @param __c The character to insert.
2076 * @throw std::length_error If new length exceeds @c max_size().
2077 *
2078 * Inserts @a __n copies of character @a __c starting at the
2079 * position referenced by iterator @a __p. If adding
2080 * characters causes the length to exceed max_size(),
2081 * length_error is thrown. The value of the string doesn't
2082 * change if an error is thrown.
2083 */
2084 void
2085 insert(iterator __p, size_type __n, _CharT __c)
2086 { this->replace(__p, __p, __n, __c); }
2087#endif
2088
2089#if __cplusplus >= 201103L
2090 /**
2091 * @brief Insert a range of characters.
2092 * @param __p Const_iterator referencing location in string to
2093 * insert at.
2094 * @param __beg Start of range.
2095 * @param __end End of range.
2096 * @return Iterator referencing the first inserted char.
2097 * @throw std::length_error If new length exceeds @c max_size().
2098 *
2099 * Inserts characters in range [beg,end). If adding characters
2100 * causes the length to exceed max_size(), length_error is
2101 * thrown. The value of the string doesn't change if an error
2102 * is thrown.
2103 */
2104 template<class _InputIterator,
2105 typename = std::_RequireInputIter<_InputIterator>>
2106 _GLIBCXX20_CONSTEXPR
2107 iterator
2108 insert(const_iterator __p, _InputIterator __beg, _InputIterator __end)
2109 {
2110 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
2111 const size_type __pos = __p - begin();
2112 this->replace(__p, __p, __beg, __end);
2113 return iterator(this->_M_data() + __pos);
2114 }
2115#else
2116 /**
2117 * @brief Insert a range of characters.
2118 * @param __p Iterator referencing location in string to insert at.
2119 * @param __beg Start of range.
2120 * @param __end End of range.
2121 * @throw std::length_error If new length exceeds @c max_size().
2122 *
2123 * Inserts characters in range [__beg,__end). If adding
2124 * characters causes the length to exceed max_size(),
2125 * length_error is thrown. The value of the string doesn't
2126 * change if an error is thrown.
2127 */
2128 template<class _InputIterator>
2129 void
2130 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
2131 { this->replace(__p, __p, __beg, __end); }
2132#endif
2133
2134#if __glibcxx_containers_ranges // C++ >= 23
2135 /**
2136 * @brief Insert a range into the string.
2137 * @param __rg A range of values that are convertible to `value_type`.
2138 * @since C++23
2139 *
2140 * The range `__rg` is allowed to overlap with `*this`.
2141 */
2142 template<__detail::__container_compatible_range<_CharT> _Rg>
2143 constexpr iterator
2144 insert_range(const_iterator __p, _Rg&& __rg)
2145 {
2146 auto __pos = __p - cbegin();
2147
2148 if constexpr (ranges::forward_range<_Rg>)
2149 if (ranges::empty(__rg))
2150 return begin() + __pos;
2151
2152
2153 if (__p == cend())
2154 append_range(std::forward<_Rg>(__rg));
2155 else
2156 {
2157 basic_string __s(from_range, std::forward<_Rg>(__rg),
2158 _M_get_allocator());
2159 insert(__pos, __s);
2160 }
2161 return begin() + __pos;
2162 }
2163#endif
2164
2165#if __cplusplus >= 201103L
2166 /**
2167 * @brief Insert an initializer_list of characters.
2168 * @param __p Iterator referencing location in string to insert at.
2169 * @param __l The initializer_list of characters to insert.
2170 * @throw std::length_error If new length exceeds @c max_size().
2171 */
2172 _GLIBCXX20_CONSTEXPR
2173 iterator
2174 insert(const_iterator __p, initializer_list<_CharT> __l)
2175 { return this->insert(__p, __l.begin(), __l.end()); }
2176
2177#ifdef _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
2178 // See PR libstdc++/83328
2179 void
2181 {
2182 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
2183 this->insert(__p - begin(), __l.begin(), __l.size());
2184 }
2185#endif
2186#endif // C++11
2187
2188 /**
2189 * @brief Insert value of a string.
2190 * @param __pos1 Position in string to insert at.
2191 * @param __str The string to insert.
2192 * @return Reference to this string.
2193 * @throw std::length_error If new length exceeds @c max_size().
2194 *
2195 * Inserts value of @a __str starting at @a __pos1. If adding
2196 * characters causes the length to exceed max_size(),
2197 * length_error is thrown. The value of the string doesn't
2198 * change if an error is thrown.
2199 */
2200 _GLIBCXX20_CONSTEXPR
2202 insert(size_type __pos1, const basic_string& __str)
2203 { return this->replace(__pos1, size_type(0),
2204 __str._M_data(), __str.size()); }
2205
2206 /**
2207 * @brief Insert a substring.
2208 * @param __pos1 Position in string to insert at.
2209 * @param __str The string to insert.
2210 * @param __pos2 Start of characters in str to insert.
2211 * @param __n Number of characters to insert.
2212 * @return Reference to this string.
2213 * @throw std::length_error If new length exceeds @c max_size().
2214 * @throw std::out_of_range If @a pos1 > size() or
2215 * @a __pos2 > @a str.size().
2216 *
2217 * Starting at @a pos1, insert @a __n character of @a __str
2218 * beginning with @a __pos2. If adding characters causes the
2219 * length to exceed max_size(), length_error is thrown. If @a
2220 * __pos1 is beyond the end of this string or @a __pos2 is
2221 * beyond the end of @a __str, out_of_range is thrown. The
2222 * value of the string doesn't change if an error is thrown.
2223 */
2224 _GLIBCXX20_CONSTEXPR
2226 insert(size_type __pos1, const basic_string& __str,
2227 size_type __pos2, size_type __n = npos)
2228 { return this->replace(__pos1, size_type(0), __str._M_data()
2229 + __str._M_check(__pos2, "basic_string::insert"),
2230 __str._M_limit(__pos2, __n)); }
2231
2232 /**
2233 * @brief Insert a C substring.
2234 * @param __pos Position in string to insert at.
2235 * @param __s The C string to insert.
2236 * @param __n The number of characters to insert.
2237 * @return Reference to this string.
2238 * @throw std::length_error If new length exceeds @c max_size().
2239 * @throw std::out_of_range If @a __pos is beyond the end of this
2240 * string.
2241 *
2242 * Inserts the first @a __n characters of @a __s starting at @a
2243 * __pos. If adding characters causes the length to exceed
2244 * max_size(), length_error is thrown. If @a __pos is beyond
2245 * end(), out_of_range is thrown. The value of the string
2246 * doesn't change if an error is thrown.
2247 */
2248 _GLIBCXX20_CONSTEXPR
2250 insert(size_type __pos, const _CharT* __s, size_type __n)
2251 { return this->replace(__pos, size_type(0), __s, __n); }
2252
2253 /**
2254 * @brief Insert a C string.
2255 * @param __pos Position in string to insert at.
2256 * @param __s The C string to insert.
2257 * @return Reference to this string.
2258 * @throw std::length_error If new length exceeds @c max_size().
2259 * @throw std::out_of_range If @a pos is beyond the end of this
2260 * string.
2261 *
2262 * Inserts the first @a n characters of @a __s starting at @a __pos. If
2263 * adding characters causes the length to exceed max_size(),
2264 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
2265 * thrown. The value of the string doesn't change if an error is
2266 * thrown.
2267 */
2268 _GLIBCXX20_CONSTEXPR
2270 insert(size_type __pos, const _CharT* __s)
2271 {
2272 __glibcxx_requires_string(__s);
2273 return this->replace(__pos, size_type(0), __s,
2274 traits_type::length(__s));
2275 }
2276
2277 /**
2278 * @brief Insert multiple characters.
2279 * @param __pos Index in string to insert at.
2280 * @param __n Number of characters to insert
2281 * @param __c The character to insert.
2282 * @return Reference to this string.
2283 * @throw std::length_error If new length exceeds @c max_size().
2284 * @throw std::out_of_range If @a __pos is beyond the end of this
2285 * string.
2286 *
2287 * Inserts @a __n copies of character @a __c starting at index
2288 * @a __pos. If adding characters causes the length to exceed
2289 * max_size(), length_error is thrown. If @a __pos > length(),
2290 * out_of_range is thrown. The value of the string doesn't
2291 * change if an error is thrown.
2292 */
2293 _GLIBCXX20_CONSTEXPR
2295 insert(size_type __pos, size_type __n, _CharT __c)
2296 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
2297 size_type(0), __n, __c); }
2298
2299 /**
2300 * @brief Insert one character.
2301 * @param __p Iterator referencing position in string to insert at.
2302 * @param __c The character to insert.
2303 * @return Iterator referencing newly inserted char.
2304 * @throw std::length_error If new length exceeds @c max_size().
2305 *
2306 * Inserts character @a __c at position referenced by @a __p.
2307 * If adding character causes the length to exceed max_size(),
2308 * length_error is thrown. If @a __p is beyond end of string,
2309 * out_of_range is thrown. The value of the string doesn't
2310 * change if an error is thrown.
2311 */
2312 _GLIBCXX20_CONSTEXPR
2313 iterator
2314 insert(__const_iterator __p, _CharT __c)
2315 {
2316 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
2317 const size_type __pos = __p - begin();
2318 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
2319 return iterator(_M_data() + __pos);
2320 }
2321
2322#ifdef __glibcxx_string_view // >= C++17
2323 /**
2324 * @brief Insert a string_view.
2325 * @param __pos Position in string to insert at.
2326 * @param __svt The object convertible to string_view to insert.
2327 * @return Reference to this string.
2328 */
2329 template<typename _Tp>
2330 _GLIBCXX20_CONSTEXPR
2331 _If_sv<_Tp, basic_string&>
2332 insert(size_type __pos, const _Tp& __svt)
2333 {
2334 __sv_type __sv = __svt;
2335 return this->insert(__pos, __sv.data(), __sv.size());
2336 }
2337
2338 /**
2339 * @brief Insert a string_view.
2340 * @param __pos1 Position in string to insert at.
2341 * @param __svt The object convertible to string_view to insert from.
2342 * @param __pos2 Start of characters in str to insert.
2343 * @param __n The number of characters to insert.
2344 * @return Reference to this string.
2345 */
2346 template<typename _Tp>
2347 _GLIBCXX20_CONSTEXPR
2348 _If_sv<_Tp, basic_string&>
2349 insert(size_type __pos1, const _Tp& __svt,
2350 size_type __pos2, size_type __n = npos)
2351 {
2352 __sv_type __sv = __svt;
2353 return this->replace(__pos1, size_type(0),
2354 __sv.data()
2355 + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"),
2356 std::__sv_limit(__sv.size(), __pos2, __n));
2357 }
2358#endif // C++17
2359
2360 /**
2361 * @brief Remove characters.
2362 * @param __pos Index of first character to remove (default 0).
2363 * @param __n Number of characters to remove (default remainder).
2364 * @return Reference to this string.
2365 * @throw std::out_of_range If @a pos is beyond the end of this
2366 * string.
2367 *
2368 * Removes @a __n characters from this string starting at @a
2369 * __pos. The length of the string is reduced by @a __n. If
2370 * there are < @a __n characters to remove, the remainder of
2371 * the string is truncated. If @a __p is beyond end of string,
2372 * out_of_range is thrown. The value of the string doesn't
2373 * change if an error is thrown.
2374 */
2375 _GLIBCXX20_CONSTEXPR
2377 erase(size_type __pos = 0, size_type __n = npos)
2378 {
2379 _M_check(__pos, "basic_string::erase");
2380 if (__n == npos)
2381 this->_M_set_length(__pos);
2382 else if (__n != 0)
2383 this->_M_erase(__pos, _M_limit(__pos, __n));
2384 return *this;
2385 }
2386
2387 /**
2388 * @brief Remove one character.
2389 * @param __position Iterator referencing the character to remove.
2390 * @return iterator referencing same location after removal.
2391 *
2392 * Removes the character at @a __position from this string. The value
2393 * of the string doesn't change if an error is thrown.
2394 */
2395 _GLIBCXX20_CONSTEXPR
2396 iterator
2397 erase(__const_iterator __position)
2398 {
2399 _GLIBCXX_DEBUG_PEDASSERT(__position >= begin()
2400 && __position < end());
2401 const size_type __pos = __position - begin();
2402 this->_M_erase(__pos, size_type(1));
2403 return iterator(_M_data() + __pos);
2404 }
2405
2406 /**
2407 * @brief Remove a range of characters.
2408 * @param __first Iterator referencing the first character to remove.
2409 * @param __last Iterator referencing the end of the range.
2410 * @return Iterator referencing location of first after removal.
2411 *
2412 * Removes the characters in the range [first,last) from this string.
2413 * The value of the string doesn't change if an error is thrown.
2414 */
2415 _GLIBCXX20_CONSTEXPR
2416 iterator
2417 erase(__const_iterator __first, __const_iterator __last)
2418 {
2419 _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last
2420 && __last <= end());
2421 const size_type __pos = __first - begin();
2422 if (__last == end())
2423 this->_M_set_length(__pos);
2424 else
2425 this->_M_erase(__pos, __last - __first);
2426 return iterator(this->_M_data() + __pos);
2427 }
2428
2429#if __cplusplus >= 201103L
2430 /**
2431 * @brief Remove the last character.
2432 *
2433 * The string must be non-empty.
2434 */
2435 _GLIBCXX20_CONSTEXPR
2436 void
2437 pop_back() noexcept
2438 {
2439 __glibcxx_assert(!empty());
2440 _M_erase(size() - 1, 1);
2441 }
2442#endif // C++11
2443
2444 /**
2445 * @brief Replace characters with value from another string.
2446 * @param __pos Index of first character to replace.
2447 * @param __n Number of characters to be replaced.
2448 * @param __str String to insert.
2449 * @return Reference to this string.
2450 * @throw std::out_of_range If @a pos is beyond the end of this
2451 * string.
2452 * @throw std::length_error If new length exceeds @c max_size().
2453 *
2454 * Removes the characters in the range [__pos,__pos+__n) from
2455 * this string. In place, the value of @a __str is inserted.
2456 * If @a __pos is beyond end of string, out_of_range is thrown.
2457 * If the length of the result exceeds max_size(), length_error
2458 * is thrown. The value of the string doesn't change if an
2459 * error is thrown.
2460 */
2461 _GLIBCXX20_CONSTEXPR
2463 replace(size_type __pos, size_type __n, const basic_string& __str)
2464 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
2465
2466 /**
2467 * @brief Replace characters with value from another string.
2468 * @param __pos1 Index of first character to replace.
2469 * @param __n1 Number of characters to be replaced.
2470 * @param __str String to insert.
2471 * @param __pos2 Index of first character of str to use.
2472 * @param __n2 Number of characters from str to use.
2473 * @return Reference to this string.
2474 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
2475 * __str.size().
2476 * @throw std::length_error If new length exceeds @c max_size().
2477 *
2478 * Removes the characters in the range [__pos1,__pos1 + n) from this
2479 * string. In place, the value of @a __str is inserted. If @a __pos is
2480 * beyond end of string, out_of_range is thrown. If the length of the
2481 * result exceeds max_size(), length_error is thrown. The value of the
2482 * string doesn't change if an error is thrown.
2483 */
2484 _GLIBCXX20_CONSTEXPR
2486 replace(size_type __pos1, size_type __n1, const basic_string& __str,
2487 size_type __pos2, size_type __n2 = npos)
2488 { return this->replace(__pos1, __n1, __str._M_data()
2489 + __str._M_check(__pos2, "basic_string::replace"),
2490 __str._M_limit(__pos2, __n2)); }
2491
2492 /**
2493 * @brief Replace characters with value of a C substring.
2494 * @param __pos Index of first character to replace.
2495 * @param __n1 Number of characters to be replaced.
2496 * @param __s C string to insert.
2497 * @param __n2 Number of characters from @a s to use.
2498 * @return Reference to this string.
2499 * @throw std::out_of_range If @a pos1 > size().
2500 * @throw std::length_error If new length exceeds @c max_size().
2501 *
2502 * Removes the characters in the range [__pos,__pos + __n1)
2503 * from this string. In place, the first @a __n2 characters of
2504 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
2505 * @a __pos is beyond end of string, out_of_range is thrown. If
2506 * the length of result exceeds max_size(), length_error is
2507 * thrown. The value of the string doesn't change if an error
2508 * is thrown.
2509 */
2510 _GLIBCXX20_CONSTEXPR
2512 replace(size_type __pos, size_type __n1, const _CharT* __s,
2513 size_type __n2)
2514 {
2515 __glibcxx_requires_string_len(__s, __n2);
2516 return _M_replace(_M_check(__pos, "basic_string::replace"),
2517 _M_limit(__pos, __n1), __s, __n2);
2518 }
2519
2520 /**
2521 * @brief Replace characters with value of a C string.
2522 * @param __pos Index of first character to replace.
2523 * @param __n1 Number of characters to be replaced.
2524 * @param __s C string to insert.
2525 * @return Reference to this string.
2526 * @throw std::out_of_range If @a pos > size().
2527 * @throw std::length_error If new length exceeds @c max_size().
2528 *
2529 * Removes the characters in the range [__pos,__pos + __n1)
2530 * from this string. In place, the characters of @a __s are
2531 * inserted. If @a __pos is beyond end of string, out_of_range
2532 * is thrown. If the length of result exceeds max_size(),
2533 * length_error is thrown. The value of the string doesn't
2534 * change if an error is thrown.
2535 */
2536 _GLIBCXX20_CONSTEXPR
2538 replace(size_type __pos, size_type __n1, const _CharT* __s)
2539 {
2540 __glibcxx_requires_string(__s);
2541 return this->replace(__pos, __n1, __s, traits_type::length(__s));
2542 }
2543
2544 /**
2545 * @brief Replace characters with multiple characters.
2546 * @param __pos Index of first character to replace.
2547 * @param __n1 Number of characters to be replaced.
2548 * @param __n2 Number of characters to insert.
2549 * @param __c Character to insert.
2550 * @return Reference to this string.
2551 * @throw std::out_of_range If @a __pos > size().
2552 * @throw std::length_error If new length exceeds @c max_size().
2553 *
2554 * Removes the characters in the range [pos,pos + n1) from this
2555 * string. In place, @a __n2 copies of @a __c are inserted.
2556 * If @a __pos is beyond end of string, out_of_range is thrown.
2557 * If the length of result exceeds max_size(), length_error is
2558 * thrown. The value of the string doesn't change if an error
2559 * is thrown.
2560 */
2561 _GLIBCXX20_CONSTEXPR
2563 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
2564 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
2565 _M_limit(__pos, __n1), __n2, __c); }
2566
2567 /**
2568 * @brief Replace range of characters with string.
2569 * @param __i1 Iterator referencing start of range to replace.
2570 * @param __i2 Iterator referencing end of range to replace.
2571 * @param __str String value to insert.
2572 * @return Reference to this string.
2573 * @throw std::length_error If new length exceeds @c max_size().
2574 *
2575 * Removes the characters in the range [__i1,__i2). In place,
2576 * the value of @a __str is inserted. If the length of result
2577 * exceeds max_size(), length_error is thrown. The value of
2578 * the string doesn't change if an error is thrown.
2579 */
2580 _GLIBCXX20_CONSTEXPR
2582 replace(__const_iterator __i1, __const_iterator __i2,
2583 const basic_string& __str)
2584 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
2585
2586 /**
2587 * @brief Replace range of characters with C substring.
2588 * @param __i1 Iterator referencing start of range to replace.
2589 * @param __i2 Iterator referencing end of range to replace.
2590 * @param __s C string value to insert.
2591 * @param __n Number of characters from s to insert.
2592 * @return Reference to this string.
2593 * @throw std::length_error If new length exceeds @c max_size().
2594 *
2595 * Removes the characters in the range [__i1,__i2). In place,
2596 * the first @a __n characters of @a __s are inserted. If the
2597 * length of result exceeds max_size(), length_error is thrown.
2598 * The value of the string doesn't change if an error is
2599 * thrown.
2600 */
2601 _GLIBCXX20_CONSTEXPR
2603 replace(__const_iterator __i1, __const_iterator __i2,
2604 const _CharT* __s, size_type __n)
2605 {
2606 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2607 && __i2 <= end());
2608 return this->replace(__i1 - begin(), __i2 - __i1, __s, __n);
2609 }
2610
2611 /**
2612 * @brief Replace range of characters with C string.
2613 * @param __i1 Iterator referencing start of range to replace.
2614 * @param __i2 Iterator referencing end of range to replace.
2615 * @param __s C string value to insert.
2616 * @return Reference to this string.
2617 * @throw std::length_error If new length exceeds @c max_size().
2618 *
2619 * Removes the characters in the range [__i1,__i2). In place,
2620 * the characters of @a __s are inserted. If the length of
2621 * result exceeds max_size(), length_error is thrown. The
2622 * value of the string doesn't change if an error is thrown.
2623 */
2624 _GLIBCXX20_CONSTEXPR
2626 replace(__const_iterator __i1, __const_iterator __i2, const _CharT* __s)
2627 {
2628 __glibcxx_requires_string(__s);
2629 return this->replace(__i1, __i2, __s, traits_type::length(__s));
2630 }
2631
2632 /**
2633 * @brief Replace range of characters with multiple characters
2634 * @param __i1 Iterator referencing start of range to replace.
2635 * @param __i2 Iterator referencing end of range to replace.
2636 * @param __n Number of characters to insert.
2637 * @param __c Character to insert.
2638 * @return Reference to this string.
2639 * @throw std::length_error If new length exceeds @c max_size().
2640 *
2641 * Removes the characters in the range [__i1,__i2). In place,
2642 * @a __n copies of @a __c are inserted. If the length of
2643 * result exceeds max_size(), length_error is thrown. The
2644 * value of the string doesn't change if an error is thrown.
2645 */
2646 _GLIBCXX20_CONSTEXPR
2648 replace(__const_iterator __i1, __const_iterator __i2, size_type __n,
2649 _CharT __c)
2650 {
2651 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2652 && __i2 <= end());
2653 return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __c);
2654 }
2655
2656 /**
2657 * @brief Replace range of characters with range.
2658 * @param __i1 Iterator referencing start of range to replace.
2659 * @param __i2 Iterator referencing end of range to replace.
2660 * @param __k1 Iterator referencing start of range to insert.
2661 * @param __k2 Iterator referencing end of range to insert.
2662 * @return Reference to this string.
2663 * @throw std::length_error If new length exceeds @c max_size().
2664 *
2665 * Removes the characters in the range [__i1,__i2). In place,
2666 * characters in the range [__k1,__k2) are inserted. If the
2667 * length of result exceeds max_size(), length_error is thrown.
2668 * The value of the string doesn't change if an error is
2669 * thrown.
2670 */
2671#if __cplusplus >= 201103L
2672 template<class _InputIterator,
2673 typename = std::_RequireInputIter<_InputIterator>>
2674 _GLIBCXX20_CONSTEXPR
2676 replace(const_iterator __i1, const_iterator __i2,
2677 _InputIterator __k1, _InputIterator __k2)
2678 {
2679 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2680 && __i2 <= end());
2681 __glibcxx_requires_valid_range(__k1, __k2);
2682 return this->_M_replace_dispatch(__i1, __i2, __k1, __k2,
2683 std::__false_type());
2684 }
2685#else
2686 template<class _InputIterator>
2687#ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
2688 typename __enable_if_not_native_iterator<_InputIterator>::__type
2689#else
2691#endif
2692 replace(iterator __i1, iterator __i2,
2693 _InputIterator __k1, _InputIterator __k2)
2694 {
2695 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2696 && __i2 <= end());
2697 __glibcxx_requires_valid_range(__k1, __k2);
2698 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
2699 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
2700 }
2701#endif
2702
2703 // Specializations for the common case of pointer and iterator:
2704 // useful to avoid the overhead of temporary buffering in _M_replace.
2705 _GLIBCXX20_CONSTEXPR
2707 replace(__const_iterator __i1, __const_iterator __i2,
2708 _CharT* __k1, _CharT* __k2)
2709 {
2710 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2711 && __i2 <= end());
2712 __glibcxx_requires_valid_range(__k1, __k2);
2713 return this->replace(__i1 - begin(), __i2 - __i1,
2714 __k1, __k2 - __k1);
2715 }
2716
2717 _GLIBCXX20_CONSTEXPR
2719 replace(__const_iterator __i1, __const_iterator __i2,
2720 const _CharT* __k1, const _CharT* __k2)
2721 {
2722 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2723 && __i2 <= end());
2724 __glibcxx_requires_valid_range(__k1, __k2);
2725 return this->replace(__i1 - begin(), __i2 - __i1,
2726 __k1, __k2 - __k1);
2727 }
2728
2729 _GLIBCXX20_CONSTEXPR
2731 replace(__const_iterator __i1, __const_iterator __i2,
2732 iterator __k1, iterator __k2)
2733 {
2734 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2735 && __i2 <= end());
2736 __glibcxx_requires_valid_range(__k1, __k2);
2737 return this->replace(__i1 - begin(), __i2 - __i1,
2738 __k1.base(), __k2 - __k1);
2739 }
2740
2741 _GLIBCXX20_CONSTEXPR
2743 replace(__const_iterator __i1, __const_iterator __i2,
2744 const_iterator __k1, const_iterator __k2)
2745 {
2746 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2747 && __i2 <= end());
2748 __glibcxx_requires_valid_range(__k1, __k2);
2749 return this->replace(__i1 - begin(), __i2 - __i1,
2750 __k1.base(), __k2 - __k1);
2751 }
2752
2753#if __glibcxx_containers_ranges // C++ >= 23
2754 /**
2755 * @brief Replace part of the string with a range.
2756 * @param __rg A range of values that are convertible to `value_type`.
2757 * @since C++23
2758 *
2759 * The range `__rg` is allowed to overlap with `*this`.
2760 */
2761 template<__detail::__container_compatible_range<_CharT> _Rg>
2762 constexpr basic_string&
2763 replace_with_range(const_iterator __i1, const_iterator __i2, _Rg&& __rg)
2764 {
2765 if (__i1 == cend())
2766 append_range(std::forward<_Rg>(__rg));
2767 else
2768 {
2769 basic_string __s(from_range, std::forward<_Rg>(__rg),
2770 _M_get_allocator());
2771 replace(__i1, __i2, __s);
2772 }
2773 return *this;
2774 }
2775#endif
2776
2777#if __cplusplus >= 201103L
2778 /**
2779 * @brief Replace range of characters with initializer_list.
2780 * @param __i1 Iterator referencing start of range to replace.
2781 * @param __i2 Iterator referencing end of range to replace.
2782 * @param __l The initializer_list of characters to insert.
2783 * @return Reference to this string.
2784 * @throw std::length_error If new length exceeds @c max_size().
2785 *
2786 * Removes the characters in the range [__i1,__i2). In place,
2787 * characters in the range [__k1,__k2) are inserted. If the
2788 * length of result exceeds max_size(), length_error is thrown.
2789 * The value of the string doesn't change if an error is
2790 * thrown.
2791 */
2792 _GLIBCXX20_CONSTEXPR
2793 basic_string& replace(const_iterator __i1, const_iterator __i2,
2795 { return this->replace(__i1, __i2, __l.begin(), __l.size()); }
2796#endif // C++11
2797
2798#ifdef __glibcxx_string_view // >= C++17
2799 /**
2800 * @brief Replace range of characters with string_view.
2801 * @param __pos The position to replace at.
2802 * @param __n The number of characters to replace.
2803 * @param __svt The object convertible to string_view to insert.
2804 * @return Reference to this string.
2805 */
2806 template<typename _Tp>
2807 _GLIBCXX20_CONSTEXPR
2808 _If_sv<_Tp, basic_string&>
2809 replace(size_type __pos, size_type __n, const _Tp& __svt)
2810 {
2811 __sv_type __sv = __svt;
2812 return this->replace(__pos, __n, __sv.data(), __sv.size());
2813 }
2814
2815 /**
2816 * @brief Replace range of characters with string_view.
2817 * @param __pos1 The position to replace at.
2818 * @param __n1 The number of characters to replace.
2819 * @param __svt The object convertible to string_view to insert from.
2820 * @param __pos2 The position in the string_view to insert from.
2821 * @param __n2 The number of characters to insert.
2822 * @return Reference to this string.
2823 */
2824 template<typename _Tp>
2825 _GLIBCXX20_CONSTEXPR
2826 _If_sv<_Tp, basic_string&>
2827 replace(size_type __pos1, size_type __n1, const _Tp& __svt,
2828 size_type __pos2, size_type __n2 = npos)
2829 {
2830 __sv_type __sv = __svt;
2831 return this->replace(__pos1, __n1,
2832 __sv.data()
2833 + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"),
2834 std::__sv_limit(__sv.size(), __pos2, __n2));
2835 }
2836
2837 /**
2838 * @brief Replace range of characters with string_view.
2839 * @param __i1 An iterator referencing the start position
2840 to replace at.
2841 * @param __i2 An iterator referencing the end position
2842 for the replace.
2843 * @param __svt The object convertible to string_view to insert from.
2844 * @return Reference to this string.
2845 */
2846 template<typename _Tp>
2847 _GLIBCXX20_CONSTEXPR
2848 _If_sv<_Tp, basic_string&>
2849 replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
2850 {
2851 __sv_type __sv = __svt;
2852 return this->replace(__i1 - begin(), __i2 - __i1, __sv);
2853 }
2854#endif // C++17
2855
2856 private:
2857 template<class _Integer>
2858 _GLIBCXX20_CONSTEXPR
2860 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
2861 _Integer __n, _Integer __val, __true_type)
2862 { return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __val); }
2863
2864 template<class _InputIterator>
2865 _GLIBCXX20_CONSTEXPR
2867 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
2868 _InputIterator __k1, _InputIterator __k2,
2869 __false_type);
2870
2871 _GLIBCXX20_CONSTEXPR
2873 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
2874 _CharT __c);
2875
2876 __attribute__((__noinline__, __noclone__, __cold__)) void
2877 _M_replace_cold(pointer __p, size_type __len1, const _CharT* __s,
2878 const size_type __len2, const size_type __how_much);
2879
2880 _GLIBCXX20_CONSTEXPR
2882 _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
2883 const size_type __len2);
2884
2885 _GLIBCXX20_CONSTEXPR
2887 _M_append(const _CharT* __s, size_type __n);
2888
2889 public:
2890
2891 /**
2892 * @brief Copy substring into C string.
2893 * @param __s C string to copy value into.
2894 * @param __n Number of characters to copy.
2895 * @param __pos Index of first character to copy.
2896 * @return Number of characters actually copied
2897 * @throw std::out_of_range If __pos > size().
2898 *
2899 * Copies up to @a __n characters starting at @a __pos into the
2900 * C string @a __s. If @a __pos is %greater than size(),
2901 * out_of_range is thrown.
2902 */
2903 _GLIBCXX20_CONSTEXPR
2904 size_type
2905 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
2906
2907 /**
2908 * @brief Swap contents with another string.
2909 * @param __s String to swap with.
2910 *
2911 * Exchanges the contents of this string with that of @a __s in constant
2912 * time.
2913 */
2914 _GLIBCXX20_CONSTEXPR
2915 void
2916 swap(basic_string& __s) _GLIBCXX_NOEXCEPT;
2917
2918 // String operations:
2919 /**
2920 * @brief Return const pointer to null-terminated contents.
2921 *
2922 * This is a handle to internal data. Do not modify or dire things may
2923 * happen.
2924 */
2925 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2926 const _CharT*
2927 c_str() const _GLIBCXX_NOEXCEPT
2928 { return _M_data(); }
2929
2930 /**
2931 * @brief Return const pointer to contents.
2932 *
2933 * This is a pointer to internal data. It is undefined to modify
2934 * the contents through the returned pointer. To get a pointer that
2935 * allows modifying the contents use @c &str[0] instead,
2936 * (or in C++17 the non-const @c str.data() overload).
2937 */
2938 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2939 const _CharT*
2940 data() const _GLIBCXX_NOEXCEPT
2941 { return _M_data(); }
2942
2943#if __cplusplus >= 201703L
2944 /**
2945 * @brief Return non-const pointer to contents.
2946 *
2947 * This is a pointer to the character sequence held by the string.
2948 * Modifying the characters in the sequence is allowed.
2949 */
2950 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2951 _CharT*
2952 data() noexcept
2953 { return _M_data(); }
2954#endif
2955
2956 /**
2957 * @brief Return copy of allocator used to construct this string.
2958 */
2959 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2960 allocator_type
2961 get_allocator() const _GLIBCXX_NOEXCEPT
2962 { return _M_get_allocator(); }
2963
2964 /**
2965 * @brief Find position of a C substring.
2966 * @param __s C string to locate.
2967 * @param __pos Index of character to search from.
2968 * @param __n Number of characters from @a s to search for.
2969 * @return Index of start of first occurrence.
2970 *
2971 * Starting from @a __pos, searches forward for the first @a
2972 * __n characters in @a __s within this string. If found,
2973 * returns the index where it begins. If not found, returns
2974 * npos.
2975 */
2976 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2977 size_type
2978 find(const _CharT* __s, size_type __pos, size_type __n) const
2979 _GLIBCXX_NOEXCEPT;
2980
2981 /**
2982 * @brief Find position of a string.
2983 * @param __str String to locate.
2984 * @param __pos Index of character to search from (default 0).
2985 * @return Index of start of first occurrence.
2986 *
2987 * Starting from @a __pos, searches forward for value of @a __str within
2988 * this string. If found, returns the index where it begins. If not
2989 * found, returns npos.
2990 */
2991 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2992 size_type
2993 find(const basic_string& __str, size_type __pos = 0) const
2994 _GLIBCXX_NOEXCEPT
2995 { return this->find(__str.data(), __pos, __str.size()); }
2996
2997#ifdef __glibcxx_string_view // >= C++17
2998 /**
2999 * @brief Find position of a string_view.
3000 * @param __svt The object convertible to string_view to locate.
3001 * @param __pos Index of character to search from (default 0).
3002 * @return Index of start of first occurrence.
3003 */
3004 template<typename _Tp>
3005 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3006 _If_sv<_Tp, size_type>
3007 find(const _Tp& __svt, size_type __pos = 0) const
3008 noexcept(is_same<_Tp, __sv_type>::value)
3009 {
3010 __sv_type __sv = __svt;
3011 return this->find(__sv.data(), __pos, __sv.size());
3012 }
3013#endif // C++17
3014
3015 /**
3016 * @brief Find position of a C string.
3017 * @param __s C string to locate.
3018 * @param __pos Index of character to search from (default 0).
3019 * @return Index of start of first occurrence.
3020 *
3021 * Starting from @a __pos, searches forward for the value of @a
3022 * __s within this string. If found, returns the index where
3023 * it begins. If not found, returns npos.
3024 */
3025 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3026 size_type
3027 find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
3028 {
3029 __glibcxx_requires_string(__s);
3030 return this->find(__s, __pos, traits_type::length(__s));
3031 }
3032
3033 /**
3034 * @brief Find position of a character.
3035 * @param __c Character to locate.
3036 * @param __pos Index of character to search from (default 0).
3037 * @return Index of first occurrence.
3038 *
3039 * Starting from @a __pos, searches forward for @a __c within
3040 * this string. If found, returns the index where it was
3041 * found. If not found, returns npos.
3042 */
3043 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3044 size_type
3045 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
3046
3047 /**
3048 * @brief Find last position of a string.
3049 * @param __str String to locate.
3050 * @param __pos Index of character to search back from (default end).
3051 * @return Index of start of last occurrence.
3052 *
3053 * Starting from @a __pos, searches backward for value of @a
3054 * __str within this string. If found, returns the index where
3055 * it begins. If not found, returns npos.
3056 */
3057 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3058 size_type
3059 rfind(const basic_string& __str, size_type __pos = npos) const
3060 _GLIBCXX_NOEXCEPT
3061 { return this->rfind(__str.data(), __pos, __str.size()); }
3062
3063#ifdef __glibcxx_string_view // >= C++17
3064 /**
3065 * @brief Find last position of a string_view.
3066 * @param __svt The object convertible to string_view to locate.
3067 * @param __pos Index of character to search back from (default end).
3068 * @return Index of start of last occurrence.
3069 */
3070 template<typename _Tp>
3071 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3072 _If_sv<_Tp, size_type>
3073 rfind(const _Tp& __svt, size_type __pos = npos) const
3075 {
3076 __sv_type __sv = __svt;
3077 return this->rfind(__sv.data(), __pos, __sv.size());
3078 }
3079#endif // C++17
3080
3081 /**
3082 * @brief Find last position of a C substring.
3083 * @param __s C string to locate.
3084 * @param __pos Index of character to search back from.
3085 * @param __n Number of characters from s to search for.
3086 * @return Index of start of last occurrence.
3087 *
3088 * Starting from @a __pos, searches backward for the first @a
3089 * __n characters in @a __s within this string. If found,
3090 * returns the index where it begins. If not found, returns
3091 * npos.
3092 */
3093 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3094 size_type
3095 rfind(const _CharT* __s, size_type __pos, size_type __n) const
3096 _GLIBCXX_NOEXCEPT;
3097
3098 /**
3099 * @brief Find last position of a C string.
3100 * @param __s C string to locate.
3101 * @param __pos Index of character to start search at (default end).
3102 * @return Index of start of last occurrence.
3103 *
3104 * Starting from @a __pos, searches backward for the value of
3105 * @a __s within this string. If found, returns the index
3106 * where it begins. If not found, returns npos.
3107 */
3108 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3109 size_type
3110 rfind(const _CharT* __s, size_type __pos = npos) const
3111 {
3112 __glibcxx_requires_string(__s);
3113 return this->rfind(__s, __pos, traits_type::length(__s));
3114 }
3115
3116 /**
3117 * @brief Find last position of a character.
3118 * @param __c Character to locate.
3119 * @param __pos Index of character to search back from (default end).
3120 * @return Index of last occurrence.
3121 *
3122 * Starting from @a __pos, searches backward for @a __c within
3123 * this string. If found, returns the index where it was
3124 * found. If not found, returns npos.
3125 */
3126 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3127 size_type
3128 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
3129
3130 /**
3131 * @brief Find position of a character of string.
3132 * @param __str String containing characters to locate.
3133 * @param __pos Index of character to search from (default 0).
3134 * @return Index of first occurrence.
3135 *
3136 * Starting from @a __pos, searches forward for one of the
3137 * characters of @a __str within this string. If found,
3138 * returns the index where it was found. If not found, returns
3139 * npos.
3140 */
3141 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3142 size_type
3143 find_first_of(const basic_string& __str, size_type __pos = 0) const
3144 _GLIBCXX_NOEXCEPT
3145 { return this->find_first_of(__str.data(), __pos, __str.size()); }
3146
3147#ifdef __glibcxx_string_view // >= C++17
3148 /**
3149 * @brief Find position of a character of a string_view.
3150 * @param __svt An object convertible to string_view containing
3151 * characters to locate.
3152 * @param __pos Index of character to search from (default 0).
3153 * @return Index of first occurrence.
3154 */
3155 template<typename _Tp>
3156 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3157 _If_sv<_Tp, size_type>
3158 find_first_of(const _Tp& __svt, size_type __pos = 0) const
3159 noexcept(is_same<_Tp, __sv_type>::value)
3160 {
3161 __sv_type __sv = __svt;
3162 return this->find_first_of(__sv.data(), __pos, __sv.size());
3163 }
3164#endif // C++17
3165
3166 /**
3167 * @brief Find position of a character of C substring.
3168 * @param __s String containing characters to locate.
3169 * @param __pos Index of character to search from.
3170 * @param __n Number of characters from s to search for.
3171 * @return Index of first occurrence.
3172 *
3173 * Starting from @a __pos, searches forward for one of the
3174 * first @a __n characters of @a __s within this string. If
3175 * found, returns the index where it was found. If not found,
3176 * returns npos.
3177 */
3178 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3179 size_type
3180 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
3181 _GLIBCXX_NOEXCEPT;
3182
3183 /**
3184 * @brief Find position of a character of C string.
3185 * @param __s String containing characters to locate.
3186 * @param __pos Index of character to search from (default 0).
3187 * @return Index of first occurrence.
3188 *
3189 * Starting from @a __pos, searches forward for one of the
3190 * characters of @a __s within this string. If found, returns
3191 * the index where it was found. If not found, returns npos.
3192 */
3193 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3194 size_type
3195 find_first_of(const _CharT* __s, size_type __pos = 0) const
3196 _GLIBCXX_NOEXCEPT
3197 {
3198 __glibcxx_requires_string(__s);
3199 return this->find_first_of(__s, __pos, traits_type::length(__s));
3200 }
3201
3202 /**
3203 * @brief Find position of a character.
3204 * @param __c Character to locate.
3205 * @param __pos Index of character to search from (default 0).
3206 * @return Index of first occurrence.
3207 *
3208 * Starting from @a __pos, searches forward for the character
3209 * @a __c within this string. If found, returns the index
3210 * where it was found. If not found, returns npos.
3211 *
3212 * Note: equivalent to find(__c, __pos).
3213 */
3214 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3215 size_type
3216 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
3217 { return this->find(__c, __pos); }
3218
3219 /**
3220 * @brief Find last position of a character of string.
3221 * @param __str String containing characters to locate.
3222 * @param __pos Index of character to search back from (default end).
3223 * @return Index of last occurrence.
3224 *
3225 * Starting from @a __pos, searches backward for one of the
3226 * characters of @a __str within this string. If found,
3227 * returns the index where it was found. If not found, returns
3228 * npos.
3229 */
3230 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3231 size_type
3232 find_last_of(const basic_string& __str, size_type __pos = npos) const
3233 _GLIBCXX_NOEXCEPT
3234 { return this->find_last_of(__str.data(), __pos, __str.size()); }
3235
3236#ifdef __glibcxx_string_view // >= C++17
3237 /**
3238 * @brief Find last position of a character of string.
3239 * @param __svt An object convertible to string_view containing
3240 * characters to locate.
3241 * @param __pos Index of character to search back from (default end).
3242 * @return Index of last occurrence.
3243 */
3244 template<typename _Tp>
3245 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3246 _If_sv<_Tp, size_type>
3247 find_last_of(const _Tp& __svt, size_type __pos = npos) const
3249 {
3250 __sv_type __sv = __svt;
3251 return this->find_last_of(__sv.data(), __pos, __sv.size());
3252 }
3253#endif // C++17
3254
3255 /**
3256 * @brief Find last position of a character of C substring.
3257 * @param __s C string containing characters to locate.
3258 * @param __pos Index of character to search back from.
3259 * @param __n Number of characters from s to search for.
3260 * @return Index of last occurrence.
3261 *
3262 * Starting from @a __pos, searches backward for one of the
3263 * first @a __n characters of @a __s within this string. If
3264 * found, returns the index where it was found. If not found,
3265 * returns npos.
3266 */
3267 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3268 size_type
3269 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
3270 _GLIBCXX_NOEXCEPT;
3271
3272 /**
3273 * @brief Find last position of a character of C string.
3274 * @param __s C string containing characters to locate.
3275 * @param __pos Index of character to search back from (default end).
3276 * @return Index of last occurrence.
3277 *
3278 * Starting from @a __pos, searches backward for one of the
3279 * characters of @a __s within this string. If found, returns
3280 * the index where it was found. If not found, returns npos.
3281 */
3282 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3283 size_type
3284 find_last_of(const _CharT* __s, size_type __pos = npos) const
3285 _GLIBCXX_NOEXCEPT
3286 {
3287 __glibcxx_requires_string(__s);
3288 return this->find_last_of(__s, __pos, traits_type::length(__s));
3289 }
3290
3291 /**
3292 * @brief Find last position of a character.
3293 * @param __c Character to locate.
3294 * @param __pos Index of character to search back from (default end).
3295 * @return Index of last occurrence.
3296 *
3297 * Starting from @a __pos, searches backward for @a __c within
3298 * this string. If found, returns the index where it was
3299 * found. If not found, returns npos.
3300 *
3301 * Note: equivalent to rfind(__c, __pos).
3302 */
3303 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3304 size_type
3305 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
3306 { return this->rfind(__c, __pos); }
3307
3308 /**
3309 * @brief Find position of a character not in string.
3310 * @param __str String containing characters to avoid.
3311 * @param __pos Index of character to search from (default 0).
3312 * @return Index of first occurrence.
3313 *
3314 * Starting from @a __pos, searches forward for a character not contained
3315 * in @a __str within this string. If found, returns the index where it
3316 * was found. If not found, returns npos.
3317 */
3318 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3319 size_type
3320 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
3321 _GLIBCXX_NOEXCEPT
3322 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
3323
3324#ifdef __glibcxx_string_view // >= C++17
3325 /**
3326 * @brief Find position of a character not in a string_view.
3327 * @param __svt A object convertible to string_view containing
3328 * characters to avoid.
3329 * @param __pos Index of character to search from (default 0).
3330 * @return Index of first occurrence.
3331 */
3332 template<typename _Tp>
3333 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3334 _If_sv<_Tp, size_type>
3335 find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
3336 noexcept(is_same<_Tp, __sv_type>::value)
3337 {
3338 __sv_type __sv = __svt;
3339 return this->find_first_not_of(__sv.data(), __pos, __sv.size());
3340 }
3341#endif // C++17
3342
3343 /**
3344 * @brief Find position of a character not in C substring.
3345 * @param __s C string containing characters to avoid.
3346 * @param __pos Index of character to search from.
3347 * @param __n Number of characters from __s to consider.
3348 * @return Index of first occurrence.
3349 *
3350 * Starting from @a __pos, searches forward for a character not
3351 * contained in the first @a __n characters of @a __s within
3352 * this string. If found, returns the index where it was
3353 * found. If not found, returns npos.
3354 */
3355 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3356 size_type
3357 find_first_not_of(const _CharT* __s, size_type __pos,
3358 size_type __n) const _GLIBCXX_NOEXCEPT;
3359
3360 /**
3361 * @brief Find position of a character not in C string.
3362 * @param __s C string containing characters to avoid.
3363 * @param __pos Index of character to search from (default 0).
3364 * @return Index of first occurrence.
3365 *
3366 * Starting from @a __pos, searches forward for a character not
3367 * contained in @a __s within this string. If found, returns
3368 * the index where it was found. If not found, returns npos.
3369 */
3370 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3371 size_type
3372 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
3373 _GLIBCXX_NOEXCEPT
3374 {
3375 __glibcxx_requires_string(__s);
3376 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
3377 }
3378
3379 /**
3380 * @brief Find position of a different character.
3381 * @param __c Character to avoid.
3382 * @param __pos Index of character to search from (default 0).
3383 * @return Index of first occurrence.
3384 *
3385 * Starting from @a __pos, searches forward for a character
3386 * other than @a __c within this string. If found, returns the
3387 * index where it was found. If not found, returns npos.
3388 */
3389 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3390 size_type
3391 find_first_not_of(_CharT __c, size_type __pos = 0) const
3392 _GLIBCXX_NOEXCEPT;
3393
3394 /**
3395 * @brief Find last position of a character not in string.
3396 * @param __str String containing characters to avoid.
3397 * @param __pos Index of character to search back from (default end).
3398 * @return Index of last occurrence.
3399 *
3400 * Starting from @a __pos, searches backward for a character
3401 * not contained in @a __str within this string. If found,
3402 * returns the index where it was found. If not found, returns
3403 * npos.
3404 */
3405 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3406 size_type
3407 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
3408 _GLIBCXX_NOEXCEPT
3409 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
3410
3411#ifdef __glibcxx_string_view // >= C++17
3412 /**
3413 * @brief Find last position of a character not in a string_view.
3414 * @param __svt An object convertible to string_view containing
3415 * characters to avoid.
3416 * @param __pos Index of character to search back from (default end).
3417 * @return Index of last occurrence.
3418 */
3419 template<typename _Tp>
3420 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3421 _If_sv<_Tp, size_type>
3422 find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
3424 {
3425 __sv_type __sv = __svt;
3426 return this->find_last_not_of(__sv.data(), __pos, __sv.size());
3427 }
3428#endif // C++17
3429
3430 /**
3431 * @brief Find last position of a character not in C substring.
3432 * @param __s C string containing characters to avoid.
3433 * @param __pos Index of character to search back from.
3434 * @param __n Number of characters from s to consider.
3435 * @return Index of last occurrence.
3436 *
3437 * Starting from @a __pos, searches backward for a character not
3438 * contained in the first @a __n characters of @a __s within this string.
3439 * If found, returns the index where it was found. If not found,
3440 * returns npos.
3441 */
3442 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3443 size_type
3444 find_last_not_of(const _CharT* __s, size_type __pos,
3445 size_type __n) const _GLIBCXX_NOEXCEPT;
3446 /**
3447 * @brief Find last position of a character not in C string.
3448 * @param __s C string containing characters to avoid.
3449 * @param __pos Index of character to search back from (default end).
3450 * @return Index of last occurrence.
3451 *
3452 * Starting from @a __pos, searches backward for a character
3453 * not contained in @a __s within this string. If found,
3454 * returns the index where it was found. If not found, returns
3455 * npos.
3456 */
3457 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3458 size_type
3459 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
3460 _GLIBCXX_NOEXCEPT
3461 {
3462 __glibcxx_requires_string(__s);
3463 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
3464 }
3465
3466 /**
3467 * @brief Find last position of a different character.
3468 * @param __c Character to avoid.
3469 * @param __pos Index of character to search back from (default end).
3470 * @return Index of last occurrence.
3471 *
3472 * Starting from @a __pos, searches backward for a character other than
3473 * @a __c within this string. If found, returns the index where it was
3474 * found. If not found, returns npos.
3475 */
3476 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3477 size_type
3478 find_last_not_of(_CharT __c, size_type __pos = npos) const
3479 _GLIBCXX_NOEXCEPT;
3480
3481 /**
3482 * @brief Get a substring.
3483 * @param __pos Index of first character (default 0).
3484 * @param __n Number of characters in substring (default remainder).
3485 * @return The new string.
3486 * @throw std::out_of_range If __pos > size().
3487 *
3488 * Construct and return a new string using the @a __n
3489 * characters starting at @a __pos. If the string is too
3490 * short, use the remainder of the characters. If @a __pos is
3491 * beyond the end of the string, out_of_range is thrown.
3492 */
3493 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3495 substr(size_type __pos = 0, size_type __n = npos) const
3496 { return basic_string(*this,
3497 _M_check(__pos, "basic_string::substr"), __n); }
3498
3499#if __cplusplus >= 202302L
3500 _GLIBCXX_NODISCARD
3501 constexpr basic_string
3502 substr(size_type __pos = 0) &&
3503 { return basic_string(std::move(*this), __pos); }
3504
3505 _GLIBCXX_NODISCARD
3506 constexpr basic_string
3507 substr(size_type __pos, size_type __n) &&
3508 { return basic_string(std::move(*this), __pos, __n); }
3509#endif // C++23
3510
3511#ifdef __glibcxx_string_subview // >= C++26
3512 /**
3513 * @brief Get a subview.
3514 * @param __pos Index of first character (default 0).
3515 * @param __n Number of characters in subview (default remainder).
3516 * @return The subview.
3517 * @throw std::out_of_range If __pos > size().
3518 *
3519 * Construct and return a subview using the `__n` characters starting at
3520 * `__pos`. If the string is too short, use the remainder of the
3521 * characters. If `__pos` is beyond the end of the string, out_of_range
3522 * is thrown.
3523 */
3524 [[nodiscard]]
3525 constexpr basic_string_view<_CharT, _Traits>
3526 subview(size_type __pos = 0, size_type __n = npos) const
3527 { return __sv_type(*this).subview(__pos, __n); }
3528#endif
3529
3530 /**
3531 * @brief Compare to a string.
3532 * @param __str String to compare against.
3533 * @return Integer < 0, 0, or > 0.
3534 *
3535 * Returns an integer < 0 if this string is ordered before @a
3536 * __str, 0 if their values are equivalent, or > 0 if this
3537 * string is ordered after @a __str. Determines the effective
3538 * length rlen of the strings to compare as the smallest of
3539 * size() and str.size(). The function then compares the two
3540 * strings by calling traits::compare(data(), str.data(),rlen).
3541 * If the result of the comparison is nonzero returns it,
3542 * otherwise the shorter one is ordered first.
3543 */
3544 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3545 int
3546 compare(const basic_string& __str) const
3547 {
3548 const size_type __size = this->size();
3549 const size_type __osize = __str.size();
3550 const size_type __len = std::min(__size, __osize);
3551
3552 int __r = traits_type::compare(_M_data(), __str.data(), __len);
3553 if (!__r)
3554 __r = _S_compare(__size, __osize);
3555 return __r;
3556 }
3557
3558#ifdef __glibcxx_string_view // >= C++17
3559 /**
3560 * @brief Compare to a string_view.
3561 * @param __svt An object convertible to string_view to compare against.
3562 * @return Integer < 0, 0, or > 0.
3563 */
3564 template<typename _Tp>
3565 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3566 _If_sv<_Tp, int>
3567 compare(const _Tp& __svt) const
3569 {
3570 __sv_type __sv = __svt;
3571 const size_type __size = this->size();
3572 const size_type __osize = __sv.size();
3573 const size_type __len = std::min(__size, __osize);
3574
3575 int __r = traits_type::compare(_M_data(), __sv.data(), __len);
3576 if (!__r)
3577 __r = _S_compare(__size, __osize);
3578 return __r;
3579 }
3580
3581 /**
3582 * @brief Compare to a string_view.
3583 * @param __pos A position in the string to start comparing from.
3584 * @param __n The number of characters to compare.
3585 * @param __svt An object convertible to string_view to compare
3586 * against.
3587 * @return Integer < 0, 0, or > 0.
3588 */
3589 template<typename _Tp>
3590 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3591 _If_sv<_Tp, int>
3592 compare(size_type __pos, size_type __n, const _Tp& __svt) const
3593 {
3594 __sv_type __sv = __svt;
3595 return __sv_type(*this).substr(__pos, __n).compare(__sv);
3596 }
3597
3598 /**
3599 * @brief Compare to a string_view.
3600 * @param __pos1 A position in the string to start comparing from.
3601 * @param __n1 The number of characters to compare.
3602 * @param __svt An object convertible to string_view to compare
3603 * against.
3604 * @param __pos2 A position in the string_view to start comparing from.
3605 * @param __n2 The number of characters to compare.
3606 * @return Integer < 0, 0, or > 0.
3607 */
3608 template<typename _Tp>
3609 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3610 _If_sv<_Tp, int>
3611 compare(size_type __pos1, size_type __n1, const _Tp& __svt,
3612 size_type __pos2, size_type __n2 = npos) const
3613 {
3614 __sv_type __sv = __svt;
3615 return __sv_type(*this)
3616 .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3617 }
3618#endif // C++17
3619
3620 /**
3621 * @brief Compare substring to a string.
3622 * @param __pos Index of first character of substring.
3623 * @param __n Number of characters in substring.
3624 * @param __str String to compare against.
3625 * @return Integer < 0, 0, or > 0.
3626 *
3627 * Form the substring of this string from the @a __n characters
3628 * starting at @a __pos. Returns an integer < 0 if the
3629 * substring is ordered before @a __str, 0 if their values are
3630 * equivalent, or > 0 if the substring is ordered after @a
3631 * __str. Determines the effective length rlen of the strings
3632 * to compare as the smallest of the length of the substring
3633 * and @a __str.size(). The function then compares the two
3634 * strings by calling
3635 * traits::compare(substring.data(),str.data(),rlen). If the
3636 * result of the comparison is nonzero returns it, otherwise
3637 * the shorter one is ordered first.
3638 */
3639 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3640 int
3641 compare(size_type __pos, size_type __n, const basic_string& __str) const
3642 {
3643 _M_check(__pos, "basic_string::compare");
3644 __n = _M_limit(__pos, __n);
3645 const size_type __osize = __str.size();
3646 const size_type __len = std::min(__n, __osize);
3647 int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
3648 if (!__r)
3649 __r = _S_compare(__n, __osize);
3650 return __r;
3651 }
3652
3653 /**
3654 * @brief Compare substring to a substring.
3655 * @param __pos1 Index of first character of substring.
3656 * @param __n1 Number of characters in substring.
3657 * @param __str String to compare against.
3658 * @param __pos2 Index of first character of substring of str.
3659 * @param __n2 Number of characters in substring of str.
3660 * @return Integer < 0, 0, or > 0.
3661 *
3662 * Form the substring of this string from the @a __n1
3663 * characters starting at @a __pos1. Form the substring of @a
3664 * __str from the @a __n2 characters starting at @a __pos2.
3665 * Returns an integer < 0 if this substring is ordered before
3666 * the substring of @a __str, 0 if their values are equivalent,
3667 * or > 0 if this substring is ordered after the substring of
3668 * @a __str. Determines the effective length rlen of the
3669 * strings to compare as the smallest of the lengths of the
3670 * substrings. The function then compares the two strings by
3671 * calling
3672 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
3673 * If the result of the comparison is nonzero returns it,
3674 * otherwise the shorter one is ordered first.
3675 */
3676 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3677 int
3678 compare(size_type __pos1, size_type __n1, const basic_string& __str,
3679 size_type __pos2, size_type __n2 = npos) const
3680 {
3681 _M_check(__pos1, "basic_string::compare");
3682 __str._M_check(__pos2, "basic_string::compare");
3683 __n1 = _M_limit(__pos1, __n1);
3684 __n2 = __str._M_limit(__pos2, __n2);
3685 const size_type __len = std::min(__n1, __n2);
3686 int __r = traits_type::compare(_M_data() + __pos1,
3687 __str.data() + __pos2, __len);
3688 if (!__r)
3689 __r = _S_compare(__n1, __n2);
3690 return __r;
3691 }
3692
3693 /**
3694 * @brief Compare to a C string.
3695 * @param __s C string to compare against.
3696 * @return Integer < 0, 0, or > 0.
3697 *
3698 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
3699 * their values are equivalent, or > 0 if this string is ordered after
3700 * @a __s. Determines the effective length rlen of the strings to
3701 * compare as the smallest of size() and the length of a string
3702 * constructed from @a __s. The function then compares the two strings
3703 * by calling traits::compare(data(),s,rlen). If the result of the
3704 * comparison is nonzero returns it, otherwise the shorter one is
3705 * ordered first.
3706 */
3707 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3708 int
3709 compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT
3710 {
3711 __glibcxx_requires_string(__s);
3712 const size_type __size = this->size();
3713 const size_type __osize = traits_type::length(__s);
3714 const size_type __len = std::min(__size, __osize);
3715 int __r = traits_type::compare(_M_data(), __s, __len);
3716 if (!__r)
3717 __r = _S_compare(__size, __osize);
3718 return __r;
3719 }
3720
3721 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3722 // 5 String::compare specification questionable
3723 /**
3724 * @brief Compare substring to a C string.
3725 * @param __pos Index of first character of substring.
3726 * @param __n1 Number of characters in substring.
3727 * @param __s C string to compare against.
3728 * @return Integer < 0, 0, or > 0.
3729 *
3730 * Form the substring of this string from the @a __n1
3731 * characters starting at @a pos. Returns an integer < 0 if
3732 * the substring is ordered before @a __s, 0 if their values
3733 * are equivalent, or > 0 if the substring is ordered after @a
3734 * __s. Determines the effective length rlen of the strings to
3735 * compare as the smallest of the length of the substring and
3736 * the length of a string constructed from @a __s. The
3737 * function then compares the two string by calling
3738 * traits::compare(substring.data(),__s,rlen). If the result of
3739 * the comparison is nonzero returns it, otherwise the shorter
3740 * one is ordered first.
3741 */
3742 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3743 int
3744 compare(size_type __pos, size_type __n1, const _CharT* __s) const
3745 {
3746 __glibcxx_requires_string(__s);
3747 _M_check(__pos, "basic_string::compare");
3748 __n1 = _M_limit(__pos, __n1);
3749 const size_type __osize = traits_type::length(__s);
3750 const size_type __len = std::min(__n1, __osize);
3751 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
3752 if (!__r)
3753 __r = _S_compare(__n1, __osize);
3754 return __r;
3755 }
3756
3757 /**
3758 * @brief Compare substring against a character %array.
3759 * @param __pos Index of first character of substring.
3760 * @param __n1 Number of characters in substring.
3761 * @param __s character %array to compare against.
3762 * @param __n2 Number of characters of s.
3763 * @return Integer < 0, 0, or > 0.
3764 *
3765 * Form the substring of this string from the @a __n1
3766 * characters starting at @a __pos. Form a string from the
3767 * first @a __n2 characters of @a __s. Returns an integer < 0
3768 * if this substring is ordered before the string from @a __s,
3769 * 0 if their values are equivalent, or > 0 if this substring
3770 * is ordered after the string from @a __s. Determines the
3771 * effective length rlen of the strings to compare as the
3772 * smallest of the length of the substring and @a __n2. The
3773 * function then compares the two strings by calling
3774 * traits::compare(substring.data(),s,rlen). If the result of
3775 * the comparison is nonzero returns it, otherwise the shorter
3776 * one is ordered first.
3777 *
3778 * NB: s must have at least n2 characters, &apos;\\0&apos; has
3779 * no special meaning.
3780 */
3781 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3782 int
3783 compare(size_type __pos, size_type __n1, const _CharT* __s,
3784 size_type __n2) const
3785 {
3786 __glibcxx_requires_string_len(__s, __n2);
3787 _M_check(__pos, "basic_string::compare");
3788 __n1 = _M_limit(__pos, __n1);
3789 const size_type __len = std::min(__n1, __n2);
3790 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
3791 if (!__r)
3792 __r = _S_compare(__n1, __n2);
3793 return __r;
3794 }
3795
3796#if __cplusplus >= 202002L
3797 [[nodiscard]]
3798 constexpr bool
3799 starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
3800 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3801
3802 [[nodiscard]]
3803 constexpr bool
3804 starts_with(_CharT __x) const noexcept
3805 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3806
3807 [[nodiscard, __gnu__::__nonnull__]]
3808 constexpr bool
3809 starts_with(const _CharT* __x) const noexcept
3810 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3811
3812 [[nodiscard]]
3813 constexpr bool
3814 ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept
3815 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3816
3817 [[nodiscard]]
3818 constexpr bool
3819 ends_with(_CharT __x) const noexcept
3820 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3821
3822 [[nodiscard, __gnu__::__nonnull__]]
3823 constexpr bool
3824 ends_with(const _CharT* __x) const noexcept
3825 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3826#endif // C++20
3827
3828#if __cplusplus > 202002L
3829 [[nodiscard]]
3830 constexpr bool
3831 contains(basic_string_view<_CharT, _Traits> __x) const noexcept
3832 { return __sv_type(this->data(), this->size()).contains(__x); }
3833
3834 [[nodiscard]]
3835 constexpr bool
3836 contains(_CharT __x) const noexcept
3837 { return __sv_type(this->data(), this->size()).contains(__x); }
3838
3839 [[nodiscard, __gnu__::__nonnull__]]
3840 constexpr bool
3841 contains(const _CharT* __x) const noexcept
3842 { return __sv_type(this->data(), this->size()).contains(__x); }
3843#endif // C++23
3844
3845 // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
3846 template<typename, typename, typename> friend class basic_stringbuf;
3847 };
3848_GLIBCXX_END_NAMESPACE_CXX11
3849_GLIBCXX_END_NAMESPACE_VERSION
3850} // namespace std
3851#endif // _GLIBCXX_USE_CXX11_ABI
3852
3853namespace std _GLIBCXX_VISIBILITY(default)
3854{
3855_GLIBCXX_BEGIN_NAMESPACE_VERSION
3856
3857#if __cpp_deduction_guides >= 201606
3858_GLIBCXX_BEGIN_NAMESPACE_CXX11
3859 template<typename _InputIterator, typename _CharT
3861 typename _Allocator = allocator<_CharT>,
3862 typename = _RequireInputIter<_InputIterator>,
3863 typename = _RequireAllocator<_Allocator>>
3864 basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
3866
3867 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3868 // 3075. basic_string needs deduction guides from basic_string_view
3869 template<typename _CharT, typename _Traits,
3870 typename _Allocator = allocator<_CharT>,
3871 typename = _RequireAllocator<_Allocator>>
3872 basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
3874
3875 template<typename _CharT, typename _Traits,
3876 typename _Allocator = allocator<_CharT>,
3877 typename = _RequireAllocator<_Allocator>>
3879 typename basic_string<_CharT, _Traits, _Allocator>::size_type,
3880 typename basic_string<_CharT, _Traits, _Allocator>::size_type,
3881 const _Allocator& = _Allocator())
3883
3884#if __glibcxx_containers_ranges // C++ >= 23
3885 template<ranges::input_range _Rg,
3886 typename _Allocator = allocator<ranges::range_value_t<_Rg>>>
3887 basic_string(from_range_t, _Rg&&, _Allocator = _Allocator())
3890 _Allocator>;
3891#endif
3892_GLIBCXX_END_NAMESPACE_CXX11
3893#endif
3894
3895 template<typename _Str>
3896 _GLIBCXX20_CONSTEXPR
3897 inline _Str
3898 __str_concat(typename _Str::value_type const* __lhs,
3899 typename _Str::size_type __lhs_len,
3900 typename _Str::value_type const* __rhs,
3901 typename _Str::size_type __rhs_len,
3902 typename _Str::allocator_type const& __a)
3903 {
3904 typedef typename _Str::allocator_type allocator_type;
3905 typedef __gnu_cxx::__alloc_traits<allocator_type> _Alloc_traits;
3906 _Str __str(_Alloc_traits::_S_select_on_copy(__a));
3907 __str.reserve(__lhs_len + __rhs_len);
3908 __str.append(__lhs, __lhs_len);
3909 __str.append(__rhs, __rhs_len);
3910 return __str;
3911 }
3912
3913 // operator+
3914 /**
3915 * @brief Concatenate two strings.
3916 * @param __lhs First string.
3917 * @param __rhs Last string.
3918 * @return New string with value of @a __lhs followed by @a __rhs.
3919 */
3920 template<typename _CharT, typename _Traits, typename _Alloc>
3921 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3925 {
3927 return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(),
3928 __rhs.c_str(), __rhs.size(),
3929 __lhs.get_allocator());
3930 }
3931
3932 /**
3933 * @brief Concatenate C string and string.
3934 * @param __lhs First string.
3935 * @param __rhs Last string.
3936 * @return New string with value of @a __lhs followed by @a __rhs.
3937 */
3938 template<typename _CharT, typename _Traits, typename _Alloc>
3939 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3940 inline basic_string<_CharT,_Traits,_Alloc>
3941 operator+(const _CharT* __lhs,
3943 {
3944 __glibcxx_requires_string(__lhs);
3946 return std::__str_concat<_Str>(__lhs, _Traits::length(__lhs),
3947 __rhs.c_str(), __rhs.size(),
3948 __rhs.get_allocator());
3949 }
3950
3951 /**
3952 * @brief Concatenate character and string.
3953 * @param __lhs First string.
3954 * @param __rhs Last string.
3955 * @return New string with @a __lhs followed by @a __rhs.
3956 */
3957 template<typename _CharT, typename _Traits, typename _Alloc>
3958 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3959 inline basic_string<_CharT,_Traits,_Alloc>
3961 {
3963 return std::__str_concat<_Str>(__builtin_addressof(__lhs), 1,
3964 __rhs.c_str(), __rhs.size(),
3965 __rhs.get_allocator());
3966 }
3967
3968 /**
3969 * @brief Concatenate string and C string.
3970 * @param __lhs First string.
3971 * @param __rhs Last string.
3972 * @return New string with @a __lhs followed by @a __rhs.
3973 */
3974 template<typename _CharT, typename _Traits, typename _Alloc>
3975 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3976 inline basic_string<_CharT, _Traits, _Alloc>
3978 const _CharT* __rhs)
3979 {
3980 __glibcxx_requires_string(__rhs);
3982 return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(),
3983 __rhs, _Traits::length(__rhs),
3984 __lhs.get_allocator());
3985 }
3986 /**
3987 * @brief Concatenate string and character.
3988 * @param __lhs First string.
3989 * @param __rhs Last string.
3990 * @return New string with @a __lhs followed by @a __rhs.
3991 */
3992 template<typename _CharT, typename _Traits, typename _Alloc>
3993 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3994 inline basic_string<_CharT, _Traits, _Alloc>
3996 {
3998 return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(),
3999 __builtin_addressof(__rhs), 1,
4000 __lhs.get_allocator());
4001 }
4002
4003#if __cplusplus >= 201103L
4004 template<typename _CharT, typename _Traits, typename _Alloc>
4005 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4006 inline basic_string<_CharT, _Traits, _Alloc>
4007 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
4008 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4009 { return std::move(__lhs.append(__rhs)); }
4010
4011 template<typename _CharT, typename _Traits, typename _Alloc>
4012 _GLIBCXX20_CONSTEXPR
4016 { return std::move(__rhs.insert(0, __lhs)); }
4017
4018 template<typename _CharT, typename _Traits, typename _Alloc>
4019 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4023 {
4024#pragma GCC diagnostic push
4025#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
4026 // Return value must use __lhs.get_allocator(), but if __rhs has equal
4027 // allocator then we can choose which parameter to modify in-place.
4028 bool __use_rhs = false;
4030 __use_rhs = true;
4031 else if (__lhs.get_allocator() == __rhs.get_allocator())
4032 __use_rhs = true;
4033 if (__use_rhs)
4034 {
4035 const auto __size = __lhs.size() + __rhs.size();
4036 if (__size > __lhs.capacity() && __size <= __rhs.capacity())
4037 return std::move(__rhs.insert(0, __lhs));
4038 }
4039 return std::move(__lhs.append(__rhs));
4040#pragma GCC diagnostic pop
4041 }
4042
4043 template<typename _CharT, typename _Traits, typename _Alloc>
4044 _GLIBCXX_NODISCARD _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4046 operator+(const _CharT* __lhs,
4048 { return std::move(__rhs.insert(0, __lhs)); }
4049
4050 template<typename _CharT, typename _Traits, typename _Alloc>
4051 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4053 operator+(_CharT __lhs,
4055 { return std::move(__rhs.insert(0, 1, __lhs)); }
4056
4057 template<typename _CharT, typename _Traits, typename _Alloc>
4058 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4061 const _CharT* __rhs)
4062 { return std::move(__lhs.append(__rhs)); }
4063
4064 template<typename _CharT, typename _Traits, typename _Alloc>
4065 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4068 _CharT __rhs)
4069 { return std::move(__lhs.append(1, __rhs)); }
4070#endif
4071
4072#if __glibcxx_string_view >= 202403L
4073 // const string & + string_view
4074 template<typename _CharT, typename _Traits, typename _Alloc>
4075 [[nodiscard]]
4078 type_identity_t<basic_string_view<_CharT, _Traits>> __rhs)
4079 {
4081 return std::__str_concat<_Str>(__lhs.data(), __lhs.size(),
4082 __rhs.data(), __rhs.size(),
4083 __lhs.get_allocator());
4084 }
4085
4086 // string && + string_view
4087 template<typename _CharT, typename _Traits, typename _Alloc>
4088 [[nodiscard]]
4091 type_identity_t<basic_string_view<_CharT, _Traits>> __rhs)
4092 {
4093 return std::move(__lhs.append(__rhs));
4094 }
4095
4096 // string_view + const string &
4097 template<typename _CharT, typename _Traits, typename _Alloc>
4098 [[nodiscard]]
4100 operator+(type_identity_t<basic_string_view<_CharT, _Traits>> __lhs,
4102 {
4104 return std::__str_concat<_Str>(__lhs.data(), __lhs.size(),
4105 __rhs.data(), __rhs.size(),
4106 __rhs.get_allocator());
4107 }
4108
4109 // string_view + string &&
4110 template<typename _CharT, typename _Traits, typename _Alloc>
4111 [[nodiscard]]
4113 operator+(type_identity_t<basic_string_view<_CharT, _Traits>> __lhs,
4115 {
4116 return std::move(__rhs.insert(0, __lhs));
4117 }
4118#endif
4119
4120 // operator ==
4121 /**
4122 * @brief Test equivalence of two strings.
4123 * @param __lhs First string.
4124 * @param __rhs Second string.
4125 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
4126 */
4127 template<typename _CharT, typename _Traits, typename _Alloc>
4128 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4129 inline bool
4132 _GLIBCXX_NOEXCEPT
4133 {
4134 return __lhs.size() == __rhs.size()
4135 && !_Traits::compare(__lhs.data(), __rhs.data(), __lhs.size());
4136 }
4137
4138 /**
4139 * @brief Test equivalence of string and C string.
4140 * @param __lhs String.
4141 * @param __rhs C string.
4142 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
4143 */
4144 template<typename _CharT, typename _Traits, typename _Alloc>
4145 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4146 inline bool
4148 const _CharT* __rhs)
4149 {
4150 return __lhs.size() == _Traits::length(__rhs)
4151 && !_Traits::compare(__lhs.data(), __rhs, __lhs.size());
4152 }
4153
4154#if __cpp_lib_three_way_comparison
4155 /**
4156 * @brief Three-way comparison of a string and a C string.
4157 * @param __lhs A string.
4158 * @param __rhs A null-terminated string.
4159 * @return A value indicating whether `__lhs` is less than, equal to,
4160 * greater than, or incomparable with `__rhs`.
4161 */
4162 template<typename _CharT, typename _Traits, typename _Alloc>
4163 [[nodiscard]]
4164 constexpr auto
4165 operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4166 const basic_string<_CharT, _Traits, _Alloc>& __rhs) noexcept
4167 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
4168 { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); }
4169
4170 /**
4171 * @brief Three-way comparison of a string and a C string.
4172 * @param __lhs A string.
4173 * @param __rhs A null-terminated string.
4174 * @return A value indicating whether `__lhs` is less than, equal to,
4175 * greater than, or incomparable with `__rhs`.
4176 */
4177 template<typename _CharT, typename _Traits, typename _Alloc>
4178 [[nodiscard]]
4179 constexpr auto
4180 operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4181 const _CharT* __rhs) noexcept
4182 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
4183 { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); }
4184#else
4185 /**
4186 * @brief Test equivalence of C string and string.
4187 * @param __lhs C string.
4188 * @param __rhs String.
4189 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
4190 */
4191 template<typename _CharT, typename _Traits, typename _Alloc>
4192 _GLIBCXX_NODISCARD
4193 inline bool
4194 operator==(const _CharT* __lhs,
4195 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4196 { return __rhs == __lhs; }
4197
4198 // operator !=
4199 /**
4200 * @brief Test difference of two strings.
4201 * @param __lhs First string.
4202 * @param __rhs Second string.
4203 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
4204 */
4205 template<typename _CharT, typename _Traits, typename _Alloc>
4206 _GLIBCXX_NODISCARD
4207 inline bool
4208 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4210 _GLIBCXX_NOEXCEPT
4211 { return !(__lhs == __rhs); }
4212
4213 /**
4214 * @brief Test difference of C string and string.
4215 * @param __lhs C string.
4216 * @param __rhs String.
4217 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
4218 */
4219 template<typename _CharT, typename _Traits, typename _Alloc>
4220 _GLIBCXX_NODISCARD
4221 inline bool
4222 operator!=(const _CharT* __lhs,
4224 { return !(__rhs == __lhs); }
4225
4226 /**
4227 * @brief Test difference of string and C string.
4228 * @param __lhs String.
4229 * @param __rhs C string.
4230 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
4231 */
4232 template<typename _CharT, typename _Traits, typename _Alloc>
4233 _GLIBCXX_NODISCARD
4234 inline bool
4235 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4236 const _CharT* __rhs)
4237 { return !(__lhs == __rhs); }
4238
4239 // operator <
4240 /**
4241 * @brief Test if string precedes string.
4242 * @param __lhs First string.
4243 * @param __rhs Second string.
4244 * @return True if @a __lhs precedes @a __rhs. False otherwise.
4245 */
4246 template<typename _CharT, typename _Traits, typename _Alloc>
4247 _GLIBCXX_NODISCARD
4248 inline bool
4249 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4251 _GLIBCXX_NOEXCEPT
4252 { return __lhs.compare(__rhs) < 0; }
4253
4254 /**
4255 * @brief Test if string precedes C string.
4256 * @param __lhs String.
4257 * @param __rhs C string.
4258 * @return True if @a __lhs precedes @a __rhs. False otherwise.
4259 */
4260 template<typename _CharT, typename _Traits, typename _Alloc>
4261 _GLIBCXX_NODISCARD
4262 inline bool
4263 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4264 const _CharT* __rhs)
4265 { return __lhs.compare(__rhs) < 0; }
4266
4267 /**
4268 * @brief Test if C string precedes string.
4269 * @param __lhs C string.
4270 * @param __rhs String.
4271 * @return True if @a __lhs precedes @a __rhs. False otherwise.
4272 */
4273 template<typename _CharT, typename _Traits, typename _Alloc>
4274 _GLIBCXX_NODISCARD
4275 inline bool
4276 operator<(const _CharT* __lhs,
4278 { return __rhs.compare(__lhs) > 0; }
4279
4280 // operator >
4281 /**
4282 * @brief Test if string follows string.
4283 * @param __lhs First string.
4284 * @param __rhs Second string.
4285 * @return True if @a __lhs follows @a __rhs. False otherwise.
4286 */
4287 template<typename _CharT, typename _Traits, typename _Alloc>
4288 _GLIBCXX_NODISCARD
4289 inline bool
4290 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4292 _GLIBCXX_NOEXCEPT
4293 { return __lhs.compare(__rhs) > 0; }
4294
4295 /**
4296 * @brief Test if string follows C string.
4297 * @param __lhs String.
4298 * @param __rhs C string.
4299 * @return True if @a __lhs follows @a __rhs. False otherwise.
4300 */
4301 template<typename _CharT, typename _Traits, typename _Alloc>
4302 _GLIBCXX_NODISCARD
4303 inline bool
4304 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4305 const _CharT* __rhs)
4306 { return __lhs.compare(__rhs) > 0; }
4307
4308 /**
4309 * @brief Test if C string follows string.
4310 * @param __lhs C string.
4311 * @param __rhs String.
4312 * @return True if @a __lhs follows @a __rhs. False otherwise.
4313 */
4314 template<typename _CharT, typename _Traits, typename _Alloc>
4315 _GLIBCXX_NODISCARD
4316 inline bool
4317 operator>(const _CharT* __lhs,
4319 { return __rhs.compare(__lhs) < 0; }
4320
4321 // operator <=
4322 /**
4323 * @brief Test if string doesn't follow string.
4324 * @param __lhs First string.
4325 * @param __rhs Second string.
4326 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
4327 */
4328 template<typename _CharT, typename _Traits, typename _Alloc>
4329 _GLIBCXX_NODISCARD
4330 inline bool
4331 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4333 _GLIBCXX_NOEXCEPT
4334 { return __lhs.compare(__rhs) <= 0; }
4335
4336 /**
4337 * @brief Test if string doesn't follow C string.
4338 * @param __lhs String.
4339 * @param __rhs C string.
4340 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
4341 */
4342 template<typename _CharT, typename _Traits, typename _Alloc>
4343 _GLIBCXX_NODISCARD
4344 inline bool
4345 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4346 const _CharT* __rhs)
4347 { return __lhs.compare(__rhs) <= 0; }
4348
4349 /**
4350 * @brief Test if C string doesn't follow string.
4351 * @param __lhs C string.
4352 * @param __rhs String.
4353 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
4354 */
4355 template<typename _CharT, typename _Traits, typename _Alloc>
4356 _GLIBCXX_NODISCARD
4357 inline bool
4358 operator<=(const _CharT* __lhs,
4360 { return __rhs.compare(__lhs) >= 0; }
4361
4362 // operator >=
4363 /**
4364 * @brief Test if string doesn't precede string.
4365 * @param __lhs First string.
4366 * @param __rhs Second string.
4367 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
4368 */
4369 template<typename _CharT, typename _Traits, typename _Alloc>
4370 _GLIBCXX_NODISCARD
4371 inline bool
4372 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4374 _GLIBCXX_NOEXCEPT
4375 { return __lhs.compare(__rhs) >= 0; }
4376
4377 /**
4378 * @brief Test if string doesn't precede C string.
4379 * @param __lhs String.
4380 * @param __rhs C string.
4381 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
4382 */
4383 template<typename _CharT, typename _Traits, typename _Alloc>
4384 _GLIBCXX_NODISCARD
4385 inline bool
4386 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4387 const _CharT* __rhs)
4388 { return __lhs.compare(__rhs) >= 0; }
4389
4390 /**
4391 * @brief Test if C string doesn't precede string.
4392 * @param __lhs C string.
4393 * @param __rhs String.
4394 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
4395 */
4396 template<typename _CharT, typename _Traits, typename _Alloc>
4397 _GLIBCXX_NODISCARD
4398 inline bool
4399 operator>=(const _CharT* __lhs,
4401 { return __rhs.compare(__lhs) <= 0; }
4402#endif // three-way comparison
4403
4404 /**
4405 * @brief Swap contents of two strings.
4406 * @param __lhs First string.
4407 * @param __rhs Second string.
4408 *
4409 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
4410 */
4411 template<typename _CharT, typename _Traits, typename _Alloc>
4412 _GLIBCXX20_CONSTEXPR
4413 inline void
4416 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
4417 { __lhs.swap(__rhs); }
4418
4419
4420 /**
4421 * @brief Read stream into a string.
4422 * @param __is Input stream.
4423 * @param __str Buffer to store into.
4424 * @return Reference to the input stream.
4425 *
4426 * Stores characters from @a __is into @a __str until whitespace is
4427 * found, the end of the stream is encountered, or str.max_size()
4428 * is reached. If is.width() is non-zero, that is the limit on the
4429 * number of characters stored into @a __str. Any previous
4430 * contents of @a __str are erased.
4431 */
4432 template<typename _CharT, typename _Traits, typename _Alloc>
4433 basic_istream<_CharT, _Traits>&
4434 operator>>(basic_istream<_CharT, _Traits>& __is,
4435 basic_string<_CharT, _Traits, _Alloc>& __str);
4436
4437 template<>
4438 basic_istream<char>&
4440
4441 /**
4442 * @brief Write string to a stream.
4443 * @param __os Output stream.
4444 * @param __str String to write out.
4445 * @return Reference to the output stream.
4446 *
4447 * Output characters of @a __str into os following the same rules as for
4448 * writing a C string.
4449 */
4450 template<typename _CharT, typename _Traits, typename _Alloc>
4454 {
4455 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4456 // 586. string inserter not a formatted function
4457 return __ostream_insert(__os, __str.data(), __str.size());
4458 }
4459
4460 /**
4461 * @brief Read a line from stream into a string.
4462 * @param __is Input stream.
4463 * @param __str Buffer to store into.
4464 * @param __delim Character marking end of line.
4465 * @return Reference to the input stream.
4466 *
4467 * Stores characters from @a __is into @a __str until @a __delim is
4468 * found, the end of the stream is encountered, or str.max_size()
4469 * is reached. Any previous contents of @a __str are erased. If
4470 * @a __delim is encountered, it is extracted but not stored into
4471 * @a __str.
4472 */
4473 template<typename _CharT, typename _Traits, typename _Alloc>
4474 basic_istream<_CharT, _Traits>&
4475 getline(basic_istream<_CharT, _Traits>& __is,
4476 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
4477
4478 /**
4479 * @brief Read a line from stream into a string.
4480 * @param __is Input stream.
4481 * @param __str Buffer to store into.
4482 * @return Reference to the input stream.
4483 *
4484 * Stores characters from is into @a __str until &apos;\n&apos; is
4485 * found, the end of the stream is encountered, or str.max_size()
4486 * is reached. Any previous contents of @a __str are erased. If
4487 * end of line is encountered, it is extracted but not stored into
4488 * @a __str.
4489 */
4490 template<typename _CharT, typename _Traits, typename _Alloc>
4491 inline basic_istream<_CharT, _Traits>&
4494 { return std::getline(__is, __str, __is.widen('\n')); }
4495
4496#if __cplusplus >= 201103L
4497 /// Read a line from an rvalue stream into a string.
4498 template<typename _CharT, typename _Traits, typename _Alloc>
4499 inline basic_istream<_CharT, _Traits>&
4501 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
4502 { return std::getline(__is, __str, __delim); }
4503
4504 /// Read a line from an rvalue stream into a string.
4505 template<typename _CharT, typename _Traits, typename _Alloc>
4506 inline basic_istream<_CharT, _Traits>&
4510#endif
4511
4512 template<>
4513 basic_istream<char>&
4514 getline(basic_istream<char>& __in, basic_string<char>& __str,
4515 char __delim);
4516
4517#ifdef _GLIBCXX_USE_WCHAR_T
4518 template<>
4519 basic_istream<wchar_t>&
4520 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
4521 wchar_t __delim);
4522#endif
4523
4524_GLIBCXX_END_NAMESPACE_VERSION
4525} // namespace
4526
4527#if __cplusplus >= 201103L
4528
4529#include <ext/string_conversions.h>
4530#include <bits/charconv.h>
4531
4532namespace std _GLIBCXX_VISIBILITY(default)
4533{
4534_GLIBCXX_BEGIN_NAMESPACE_VERSION
4535_GLIBCXX_BEGIN_NAMESPACE_CXX11
4536
4537 // 21.4 Numeric Conversions [string.conversions].
4538 inline int
4539 stoi(const string& __str, size_t* __idx = 0, int __base = 10)
4540 { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
4541 __idx, __base); }
4542
4543 inline long
4544 stol(const string& __str, size_t* __idx = 0, int __base = 10)
4545 { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
4546 __idx, __base); }
4547
4548 inline unsigned long
4549 stoul(const string& __str, size_t* __idx = 0, int __base = 10)
4550 { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
4551 __idx, __base); }
4552
4553#if _GLIBCXX_USE_C99_STDLIB
4554 inline long long
4555 stoll(const string& __str, size_t* __idx = 0, int __base = 10)
4556 { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
4557 __idx, __base); }
4558
4559 inline unsigned long long
4560 stoull(const string& __str, size_t* __idx = 0, int __base = 10)
4561 { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
4562 __idx, __base); }
4563#elif __LONG_WIDTH__ == __LONG_LONG_WIDTH__
4564 inline long long
4565 stoll(const string& __str, size_t* __idx = 0, int __base = 10)
4566 { return std::stol(__str, __idx, __base); }
4567
4568 inline unsigned long long
4569 stoull(const string& __str, size_t* __idx = 0, int __base = 10)
4570 { return std::stoul(__str, __idx, __base); }
4571#endif
4572
4573 inline double
4574 stod(const string& __str, size_t* __idx = 0)
4575 { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
4576
4577#if _GLIBCXX_HAVE_STRTOF
4578 // NB: strtof vs strtod.
4579 inline float
4580 stof(const string& __str, size_t* __idx = 0)
4581 { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
4582#else
4583 inline float
4584 stof(const string& __str, size_t* __idx = 0)
4585 {
4586 double __d = std::stod(__str, __idx);
4587 if (__builtin_isfinite(__d) && __d != 0.0)
4588 {
4589 double __abs_d = __builtin_fabs(__d);
4590 if (__abs_d < __FLT_MIN__ || __abs_d > __FLT_MAX__)
4591 {
4592 errno = ERANGE;
4593 std::__throw_out_of_range("stof");
4594 }
4595 }
4596 return __d;
4597 }
4598#endif
4599
4600#if _GLIBCXX_HAVE_STRTOLD && ! _GLIBCXX_HAVE_BROKEN_STRTOLD
4601 inline long double
4602 stold(const string& __str, size_t* __idx = 0)
4603 { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
4604#elif __DBL_MANT_DIG__ == __LDBL_MANT_DIG__
4605 inline long double
4606 stold(const string& __str, size_t* __idx = 0)
4607 { return std::stod(__str, __idx); }
4608#endif
4609
4610 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4611 // DR 1261. Insufficent overloads for to_string / to_wstring
4612
4613 _GLIBCXX_NODISCARD
4614 inline string
4615 to_string(int __val)
4616#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32
4617 noexcept // any 32-bit value fits in the SSO buffer
4618#endif
4619 {
4620 const bool __neg = __val < 0;
4621 const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val;
4622 const auto __len = __detail::__to_chars_len(__uval);
4623 string __str;
4624 __str.__resize_and_overwrite(__neg + __len, [=](char* __p, size_t __n) {
4625 __p[0] = '-';
4626 __detail::__to_chars_10_impl(__p + (int)__neg, __len, __uval);
4627 return __n;
4628 });
4629 return __str;
4630 }
4631
4632 _GLIBCXX_NODISCARD
4633 inline string
4634 to_string(unsigned __val)
4635#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32
4636 noexcept // any 32-bit value fits in the SSO buffer
4637#endif
4638 {
4639 const auto __len = __detail::__to_chars_len(__val);
4640 string __str;
4641 __str.__resize_and_overwrite(__len, [__val](char* __p, size_t __n) {
4642 __detail::__to_chars_10_impl(__p, __n, __val);
4643 return __n;
4644 });
4645 return __str;
4646 }
4647
4648 _GLIBCXX_NODISCARD
4649 inline string
4650 to_string(long __val)
4651#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32
4652 noexcept // any 32-bit value fits in the SSO buffer
4653#endif
4654 {
4655 const bool __neg = __val < 0;
4656 const unsigned long __uval = __neg ? (unsigned long)~__val + 1ul : __val;
4657 const auto __len = __detail::__to_chars_len(__uval);
4658 string __str;
4659 __str.__resize_and_overwrite(__neg + __len, [=](char* __p, size_t __n) {
4660 __p[0] = '-';
4661 __detail::__to_chars_10_impl(__p + (int)__neg, __len, __uval);
4662 return __n;
4663 });
4664 return __str;
4665 }
4666
4667 _GLIBCXX_NODISCARD
4668 inline string
4669 to_string(unsigned long __val)
4670#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32
4671 noexcept // any 32-bit value fits in the SSO buffer
4672#endif
4673 {
4674 const auto __len = __detail::__to_chars_len(__val);
4675 string __str;
4676 __str.__resize_and_overwrite(__len, [__val](char* __p, size_t __n) {
4677 __detail::__to_chars_10_impl(__p, __n, __val);
4678 return __n;
4679 });
4680 return __str;
4681 }
4682
4683 _GLIBCXX_NODISCARD
4684 inline string
4685 to_string(long long __val)
4686 {
4687 const bool __neg = __val < 0;
4688 const unsigned long long __uval
4689 = __neg ? (unsigned long long)~__val + 1ull : __val;
4690 const auto __len = __detail::__to_chars_len(__uval);
4691 string __str;
4692 __str.__resize_and_overwrite(__neg + __len, [=](char* __p, size_t __n) {
4693 __p[0] = '-';
4694 __detail::__to_chars_10_impl(__p + (int)__neg, __len, __uval);
4695 return __n;
4696 });
4697 return __str;
4698 }
4699
4700 _GLIBCXX_NODISCARD
4701 inline string
4702 to_string(unsigned long long __val)
4703 {
4704 const auto __len = __detail::__to_chars_len(__val);
4705 string __str;
4706 __str.__resize_and_overwrite(__len, [__val](char* __p, size_t __n) {
4707 __detail::__to_chars_10_impl(__p, __n, __val);
4708 return __n;
4709 });
4710 return __str;
4711 }
4712
4713#if __glibcxx_to_string >= 202306L // C++ >= 26
4714
4715 [[nodiscard]]
4716 inline string
4717 to_string(float __val)
4718 {
4719 string __str;
4720 size_t __len = 15;
4721 do {
4722 __str.resize_and_overwrite(__len,
4723 [__val, &__len] (char* __p, size_t __n) {
4724 auto [__end, __err] = std::to_chars(__p, __p + __n, __val);
4725 if (__err == errc{}) [[likely]]
4726 return __end - __p;
4727 __len *= 2;
4728 return __p - __p;;
4729 });
4730 } while (__str.empty());
4731 return __str;
4732 }
4733
4734 [[nodiscard]]
4735 inline string
4736 to_string(double __val)
4737 {
4738 string __str;
4739 size_t __len = 15;
4740 do {
4741 __str.resize_and_overwrite(__len,
4742 [__val, &__len] (char* __p, size_t __n) {
4743 auto [__end, __err] = std::to_chars(__p, __p + __n, __val);
4744 if (__err == errc{}) [[likely]]
4745 return __end - __p;
4746 __len *= 2;
4747 return __p - __p;;
4748 });
4749 } while (__str.empty());
4750 return __str;
4751 }
4752
4753 [[nodiscard]]
4754 inline string
4755 to_string(long double __val)
4756 {
4757 string __str;
4758 size_t __len = 15;
4759 do {
4760 __str.resize_and_overwrite(__len,
4761 [__val, &__len] (char* __p, size_t __n) {
4762 auto [__end, __err] = std::to_chars(__p, __p + __n, __val);
4763 if (__err == errc{}) [[likely]]
4764 return __end - __p;
4765 __len *= 2;
4766 return __p - __p;;
4767 });
4768 } while (__str.empty());
4769 return __str;
4770 }
4771#elif _GLIBCXX_USE_C99_STDIO
4772#pragma GCC diagnostic push
4773#pragma GCC diagnostic ignored "-Wsuggest-attribute=format"
4774 // NB: (v)snprintf vs sprintf.
4775
4776 _GLIBCXX_NODISCARD
4777 inline string
4778 to_string(float __val)
4779 {
4780 const int __n =
4781 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
4782 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
4783 "%f", __val);
4784 }
4785
4786 _GLIBCXX_NODISCARD
4787 inline string
4788 to_string(double __val)
4789 {
4790 const int __n =
4791 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
4792 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
4793 "%f", __val);
4794 }
4795
4796 _GLIBCXX_NODISCARD
4797 inline string
4798 to_string(long double __val)
4799 {
4800 const int __n =
4801 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
4802 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
4803 "%Lf", __val);
4804 }
4805#pragma GCC diagnostic pop
4806#endif // _GLIBCXX_USE_C99_STDIO
4807
4808#if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
4809 inline int
4810 stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
4811 { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
4812 __idx, __base); }
4813
4814 inline long
4815 stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
4816 { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
4817 __idx, __base); }
4818
4819 inline unsigned long
4820 stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
4821 { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
4822 __idx, __base); }
4823
4824 inline long long
4825 stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
4826 { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
4827 __idx, __base); }
4828
4829 inline unsigned long long
4830 stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
4831 { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
4832 __idx, __base); }
4833
4834 // NB: wcstof vs wcstod.
4835 inline float
4836 stof(const wstring& __str, size_t* __idx = 0)
4837 { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
4838
4839 inline double
4840 stod(const wstring& __str, size_t* __idx = 0)
4841 { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
4842
4843 inline long double
4844 stold(const wstring& __str, size_t* __idx = 0)
4845 { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
4846#endif
4847
4848#ifdef _GLIBCXX_USE_WCHAR_T
4849#pragma GCC diagnostic push
4850#pragma GCC diagnostic ignored "-Wc++17-extensions"
4851 _GLIBCXX20_CONSTEXPR
4852 inline void
4853 __to_wstring_numeric(const char* __s, int __len, wchar_t* __wout)
4854 {
4855 // This condition is true if exec-charset and wide-exec-charset share the
4856 // same values for the ASCII subset or the EBCDIC invariant character set.
4857 if constexpr (wchar_t('0') == L'0' && wchar_t('-') == L'-'
4858 && wchar_t('.') == L'.' && wchar_t('e') == L'e')
4859 {
4860 for (int __i = 0; __i < __len; ++__i)
4861 __wout[__i] = (wchar_t) __s[__i];
4862 }
4863 else
4864 {
4865 wchar_t __wc[256];
4866 for (int __i = '0'; __i <= '9'; ++__i)
4867 __wc[__i] = L'0' + __i;
4868 __wc['.'] = L'.';
4869 __wc['+'] = L'+';
4870 __wc['-'] = L'-';
4871 __wc['a'] = L'a';
4872 __wc['b'] = L'b';
4873 __wc['c'] = L'c';
4874 __wc['d'] = L'd';
4875 __wc['e'] = L'e';
4876 __wc['f'] = L'f';
4877 __wc['i'] = L'i'; // for "inf"
4878 __wc['n'] = L'n'; // for "nan" and "inf"
4879 __wc['p'] = L'p'; // for hexfloats "0x1p1"
4880 __wc['x'] = L'x';
4881 __wc['A'] = L'A';
4882 __wc['B'] = L'B';
4883 __wc['C'] = L'C';
4884 __wc['D'] = L'D';
4885 __wc['E'] = L'E';
4886 __wc['F'] = L'F';
4887 __wc['I'] = L'I';
4888 __wc['N'] = L'N';
4889 __wc['P'] = L'P';
4890 __wc['X'] = L'X';
4891
4892 for (int __i = 0; __i < __len; ++__i)
4893 __wout[__i] = __wc[(int)__s[__i]];
4894 }
4895 }
4896
4897#if __glibcxx_constexpr_string >= 201907L
4898 constexpr
4899#endif
4900 inline wstring
4901#ifdef __glibcxx_string_view // >= C++17
4902 __to_wstring_numeric(string_view __s)
4903#else
4904 __to_wstring_numeric(const string& __s)
4905#endif
4906 {
4907 if constexpr (wchar_t('0') == L'0' && wchar_t('-') == L'-'
4908 && wchar_t('.') == L'.' && wchar_t('e') == L'e')
4909 return wstring(__s.data(), __s.data() + __s.size());
4910 else
4911 {
4912 wstring __ws;
4913 auto __f = __s.data();
4914 __ws.__resize_and_overwrite(__s.size(),
4915 [__f] (wchar_t* __to, int __n) {
4916 std::__to_wstring_numeric(__f, __n, __to);
4917 return __n;
4918 });
4919 return __ws;
4920 }
4921 }
4922#pragma GCC diagnostic pop
4923
4924 _GLIBCXX_NODISCARD
4925 inline wstring
4926 to_wstring(int __val)
4927 { return std::__to_wstring_numeric(std::to_string(__val)); }
4928
4929 _GLIBCXX_NODISCARD
4930 inline wstring
4931 to_wstring(unsigned __val)
4932 { return std::__to_wstring_numeric(std::to_string(__val)); }
4933
4934 _GLIBCXX_NODISCARD
4935 inline wstring
4936 to_wstring(long __val)
4937 { return std::__to_wstring_numeric(std::to_string(__val)); }
4938
4939 _GLIBCXX_NODISCARD
4940 inline wstring
4941 to_wstring(unsigned long __val)
4942 { return std::__to_wstring_numeric(std::to_string(__val)); }
4943
4944 _GLIBCXX_NODISCARD
4945 inline wstring
4946 to_wstring(long long __val)
4947 { return std::__to_wstring_numeric(std::to_string(__val)); }
4948
4949 _GLIBCXX_NODISCARD
4950 inline wstring
4951 to_wstring(unsigned long long __val)
4952 { return std::__to_wstring_numeric(std::to_string(__val)); }
4953
4954#if __glibcxx_to_string || _GLIBCXX_USE_C99_STDIO
4955 _GLIBCXX_NODISCARD
4956 inline wstring
4957 to_wstring(float __val)
4958 { return std::__to_wstring_numeric(std::to_string(__val)); }
4959
4960 _GLIBCXX_NODISCARD
4961 inline wstring
4962 to_wstring(double __val)
4963 { return std::__to_wstring_numeric(std::to_string(__val)); }
4964
4965 _GLIBCXX_NODISCARD
4966 inline wstring
4967 to_wstring(long double __val)
4968 { return std::__to_wstring_numeric(std::to_string(__val)); }
4969#endif
4970#endif // _GLIBCXX_USE_WCHAR_T
4971
4972_GLIBCXX_END_NAMESPACE_CXX11
4973_GLIBCXX_END_NAMESPACE_VERSION
4974} // namespace
4975
4976#endif /* C++11 */
4977
4978#if __cplusplus >= 201103L
4979
4980#include <bits/functional_hash.h>
4981
4982namespace std _GLIBCXX_VISIBILITY(default)
4983{
4984_GLIBCXX_BEGIN_NAMESPACE_VERSION
4985
4986 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4987 // 3705. Hashability shouldn't depend on basic_string's allocator
4988
4989 template<typename _CharT, typename _Alloc,
4990 typename _StrT = basic_string<_CharT, char_traits<_CharT>, _Alloc>>
4991 struct __str_hash_base
4992 : public __hash_base<size_t, _StrT>
4993 {
4994 [[__nodiscard__]]
4995 size_t
4996 operator()(const _StrT& __s) const noexcept
4997 { return _Hash_impl::hash(__s.data(), __s.length() * sizeof(_CharT)); }
4998 };
4999
5000#ifndef _GLIBCXX_COMPATIBILITY_CXX0X
5001 /// std::hash specialization for string.
5002 template<typename _Alloc>
5003 struct hash<basic_string<char, char_traits<char>, _Alloc>>
5004 : public __str_hash_base<char, _Alloc>
5005 { };
5006
5007 /// std::hash specialization for wstring.
5008 template<typename _Alloc>
5009 struct hash<basic_string<wchar_t, char_traits<wchar_t>, _Alloc>>
5010 : public __str_hash_base<wchar_t, _Alloc>
5011 { };
5012
5013 template<typename _Alloc>
5014 struct __is_fast_hash<hash<basic_string<wchar_t, char_traits<wchar_t>,
5015 _Alloc>>>
5017 { };
5018#endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
5019
5020#ifdef _GLIBCXX_USE_CHAR8_T
5021 /// std::hash specialization for u8string.
5022 template<typename _Alloc>
5023 struct hash<basic_string<char8_t, char_traits<char8_t>, _Alloc>>
5024 : public __str_hash_base<char8_t, _Alloc>
5025 { };
5026#endif
5027
5028 /// std::hash specialization for u16string.
5029 template<typename _Alloc>
5030 struct hash<basic_string<char16_t, char_traits<char16_t>, _Alloc>>
5031 : public __str_hash_base<char16_t, _Alloc>
5032 { };
5033
5034 /// std::hash specialization for u32string.
5035 template<typename _Alloc>
5036 struct hash<basic_string<char32_t, char_traits<char32_t>, _Alloc>>
5037 : public __str_hash_base<char32_t, _Alloc>
5038 { };
5039
5040#if ! _GLIBCXX_INLINE_VERSION
5041 // PR libstdc++/105907 - __is_fast_hash affects unordered container ABI.
5042 template<> struct __is_fast_hash<hash<string>> : std::false_type { };
5043 template<> struct __is_fast_hash<hash<wstring>> : std::false_type { };
5044 template<> struct __is_fast_hash<hash<u16string>> : std::false_type { };
5045 template<> struct __is_fast_hash<hash<u32string>> : std::false_type { };
5046#ifdef _GLIBCXX_USE_CHAR8_T
5047 template<> struct __is_fast_hash<hash<u8string>> : std::false_type { };
5048#endif
5049#else
5050 // For versioned namespace, assume every std::hash<basic_string<>> is slow.
5051 template<typename _CharT, typename _Traits, typename _Alloc>
5052 struct __is_fast_hash<hash<basic_string<_CharT, _Traits, _Alloc>>>
5054 { };
5055#endif
5056
5057#ifdef __glibcxx_string_udls // C++ >= 14
5058 inline namespace literals
5059 {
5060 inline namespace string_literals
5061 {
5062#pragma GCC diagnostic push
5063#pragma GCC diagnostic ignored "-Wliteral-suffix"
5064
5065#if __glibcxx_constexpr_string >= 201907L
5066# define _GLIBCXX_STRING_CONSTEXPR constexpr
5067#else
5068# define _GLIBCXX_STRING_CONSTEXPR
5069#endif
5070
5071 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
5072 inline basic_string<char>
5073 operator""s(const char* __str, size_t __len)
5074 { return basic_string<char>{__str, __len}; }
5075
5076 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
5077 inline basic_string<wchar_t>
5078 operator""s(const wchar_t* __str, size_t __len)
5079 { return basic_string<wchar_t>{__str, __len}; }
5080
5081#ifdef _GLIBCXX_USE_CHAR8_T
5082 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
5083 inline basic_string<char8_t>
5084 operator""s(const char8_t* __str, size_t __len)
5085 { return basic_string<char8_t>{__str, __len}; }
5086#endif
5087
5088 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
5089 inline basic_string<char16_t>
5090 operator""s(const char16_t* __str, size_t __len)
5091 { return basic_string<char16_t>{__str, __len}; }
5092
5093 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
5094 inline basic_string<char32_t>
5095 operator""s(const char32_t* __str, size_t __len)
5096 { return basic_string<char32_t>{__str, __len}; }
5097
5098#undef _GLIBCXX_STRING_CONSTEXPR
5099#pragma GCC diagnostic pop
5100 } // inline namespace string_literals
5101 } // inline namespace literals
5102#endif // __glibcxx_string_udls
5103
5104#ifdef __glibcxx_variant // >= C++17
5105 namespace __detail::__variant
5106 {
5107 template<typename> struct _Never_valueless_alt; // see <variant>
5108
5109 // Provide the strong exception-safety guarantee when emplacing a
5110 // basic_string into a variant, but only if moving the string cannot throw.
5111 template<typename _Tp, typename _Traits, typename _Alloc>
5112 struct _Never_valueless_alt<std::basic_string<_Tp, _Traits, _Alloc>>
5113 : __and_<
5114 is_nothrow_move_constructible<std::basic_string<_Tp, _Traits, _Alloc>>,
5115 is_nothrow_move_assignable<std::basic_string<_Tp, _Traits, _Alloc>>
5116 >::type
5117 { };
5118 } // namespace __detail::__variant
5119#endif // C++17
5120
5121_GLIBCXX_END_NAMESPACE_VERSION
5122} // namespace std
5123
5124#endif // C++11
5125
5126#endif /* _BASIC_STRING_H */
constexpr complex< _Tp > operator+(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x plus y.
Definition complex:374
constexpr _Tp * to_address(_Tp *__ptr) noexcept
Obtain address referenced by a pointer to an object.
Definition ptr_traits.h:234
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
Definition type_traits:2967
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition type_traits:122
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
Definition move.h:72
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
basic_string< char > string
A string of char.
Definition stringfwd.h:79
basic_string< char32_t > u32string
A string of char32_t.
Definition stringfwd.h:94
basic_string< char16_t > u16string
A string of char16_t.
Definition stringfwd.h:91
basic_string< wchar_t > wstring
A string of wchar_t.
Definition stringfwd.h:82
ISO C++ entities toplevel namespace is std.
basic_istream< _CharT, _Traits > & getline(basic_istream< _CharT, _Traits > &__is, basic_string< _CharT, _Traits, _Alloc > &__str, _CharT __delim)
Read a line from stream into a string.
constexpr auto size(const _Container &__cont) noexcept(noexcept(__cont.size())) -> decltype(__cont.size())
Return the size of a container.
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition bitset:1658
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition bitset:1754
ISO C++ inline namespace for literal suffixes.
initializer_list
char_type widen(char __c) const
Widens characters.
Definition basic_ios.h:465
Template class basic_istream.
Definition istream:67
Template class basic_ostream.
Definition ostream.h:67
A non-owning reference to a string.
Definition string_view:113
Primary class template hash.
is_pointer
Definition type_traits:651
is_nothrow_default_constructible
Definition type_traits:1352
static constexpr allocation_result< pointer, size_type > allocate_at_least(_Char_alloc_type &__a, size_type __n)
The standard allocator, as per C++03 [20.4.1].
Definition allocator.h:134
Managing sequences of characters and character-like objects.
constexpr const _CharT * c_str() const noexcept
Return const pointer to null-terminated contents.
constexpr basic_string(_InputIterator __beg, _InputIterator __end, const _Alloc &__a=_Alloc())
Construct string as copy of a range.
constexpr basic_string(const _Alloc &__a) noexcept
Construct an empty string using allocator a.
constexpr size_type find_first_not_of(_CharT __c, size_type __pos=0) const noexcept
Find position of a different character.
constexpr basic_string & append(const basic_string &__str, size_type __pos, size_type __n=npos)
Append a substring.
constexpr basic_string() noexcept(/*conditional */)
Default constructor creates an empty string.
constexpr reference at(size_type __n)
Provides access to the data contained in the string.
constexpr basic_string & replace(size_type __pos, size_type __n1, const _CharT *__s)
Replace characters with value of a C string.
constexpr size_type find(const _CharT *__s, size_type __pos=0) const noexcept
Find position of a C string.
constexpr basic_string & replace(__const_iterator __i1, __const_iterator __i2, const basic_string &__str)
Replace range of characters with string.
constexpr size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
constexpr basic_string(const _CharT *__s, const _Alloc &__a=_Alloc())
Construct string as copy of a C string.
constexpr const_iterator begin() const noexcept
constexpr void clear() noexcept
constexpr size_type find_last_not_of(const basic_string &__str, size_type __pos=npos) const noexcept
constexpr basic_string & operator=(const _CharT *__s)
Copy contents of s into this string.
constexpr const_reverse_iterator rend() const noexcept
constexpr void reserve(size_type __res_arg)
Attempt to preallocate enough memory for specified number of characters.
constexpr size_type find(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find position of a C substring.
constexpr basic_string & assign(const _CharT *__s, size_type __n)
Set value to a C substring.
constexpr basic_string & replace(__const_iterator __i1, __const_iterator __i2, size_type __n, _CharT __c)
Replace range of characters with multiple characters.
constexpr const _CharT * data() const noexcept
Return const pointer to contents.
constexpr void shrink_to_fit() noexcept
A non-binding request to reduce capacity() to size().
constexpr size_type find_first_not_of(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find position of a character not in C substring.
constexpr basic_string substr(size_type __pos=0, size_type __n=npos) const
Get a substring.
constexpr size_type find_first_of(const _CharT *__s, size_type __pos=0) const noexcept
Find position of a character of C string.
constexpr int compare(size_type __pos, size_type __n, const basic_string &__str) const
Compare substring to a string.
constexpr int compare(size_type __pos1, size_type __n1, const basic_string &__str, size_type __pos2, size_type __n2=npos) const
Compare substring to a substring.
constexpr size_type rfind(_CharT __c, size_type __pos=npos) const noexcept
Find last position of a character.
constexpr reference back() noexcept
constexpr size_type find_first_of(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find position of a character of C substring.
constexpr basic_string(const basic_string &__str, size_type __pos, const _Alloc &__a=_Alloc())
Construct string as copy of a substring.
constexpr basic_string(const basic_string &__str, size_type __pos, size_type __n)
Construct string as copy of a substring.
constexpr basic_string & replace(const_iterator __i1, const_iterator __i2, initializer_list< _CharT > __l)
Replace range of characters with initializer_list.
constexpr basic_string & assign(initializer_list< _CharT > __l)
Set value to an initializer_list of characters.
constexpr size_type rfind(const _CharT *__s, size_type __pos=npos) const
Find last position of a C string.
constexpr size_type find_first_not_of(const _CharT *__s, size_type __pos=0) const noexcept
Find position of a character not in C string.
constexpr basic_string & operator+=(const _CharT *__s)
Append a C string.
constexpr basic_string & assign(const basic_string &__str)
Set value to contents of another string.
constexpr basic_string(const basic_string &__str)
Construct string with copy of value of __str.
constexpr basic_string & append(initializer_list< _CharT > __l)
Append an initializer_list of characters.
constexpr basic_string(size_type __n, _CharT __c, const _Alloc &__a=_Alloc())
Construct string as multiple characters.
constexpr basic_string & append(size_type __n, _CharT __c)
Append multiple characters.
constexpr size_type length() const noexcept
Returns the number of characters in the string, not including any null-termination.
constexpr const_reverse_iterator crbegin() const noexcept
constexpr basic_string(const basic_string &__str, size_type __pos, size_type __n, const _Alloc &__a)
Construct string as copy of a substring.
constexpr size_type find_last_of(_CharT __c, size_type __pos=npos) const noexcept
Find last position of a character.
constexpr basic_string & replace(size_type __pos, size_type __n1, const _CharT *__s, size_type __n2)
Replace characters with value of a C substring.
constexpr basic_string & assign(basic_string &&__str) noexcept(_Alloc_traits::_S_nothrow_move())
Set value to contents of another string.
constexpr basic_string & replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
Replace characters with multiple characters.
constexpr basic_string & append(_InputIterator __first, _InputIterator __last)
Append a range of characters.
constexpr const_reverse_iterator rbegin() const noexcept
constexpr size_type copy(_CharT *__s, size_type __n, size_type __pos=0) const
Copy substring into C string.
constexpr const_iterator end() const noexcept
constexpr basic_string & insert(size_type __pos, const _CharT *__s, size_type __n)
Insert a C substring.
constexpr 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.
constexpr const_iterator cbegin() const noexcept
constexpr size_type rfind(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find last position of a C substring.
constexpr iterator erase(__const_iterator __first, __const_iterator __last)
Remove a range of characters.
constexpr size_type find_first_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character of string.
constexpr const_reference at(size_type __n) const
Provides access to the data contained in the string.
constexpr reference operator[](size_type __pos)
Subscript access to the data contained in the string.
constexpr basic_string & assign(const basic_string &__str, size_type __pos, size_type __n=npos)
Set value to a substring of a string.
constexpr reverse_iterator rend() noexcept
constexpr void resize(size_type __n, _CharT __c)
Resizes the string to the specified number of characters.
constexpr iterator insert(const_iterator __p, _InputIterator __beg, _InputIterator __end)
Insert a range of characters.
constexpr void reserve()
constexpr size_type find_last_not_of(_CharT __c, size_type __pos=npos) const noexcept
Find last position of a different character.
constexpr basic_string & insert(size_type __pos1, const basic_string &__str)
Insert value of a string.
constexpr iterator insert(__const_iterator __p, _CharT __c)
Insert one character.
constexpr ~basic_string()
Destroy the string instance.
constexpr basic_string & operator=(basic_string &&__str) noexcept(_Alloc_traits::_S_nothrow_move())
Move assign the value of str to this string.
constexpr void __resize_and_overwrite(size_type __n, _Operation __op)
Non-standard version of resize_and_overwrite for C++11 and above.
constexpr size_type rfind(const basic_string &__str, size_type __pos=npos) const noexcept
constexpr basic_string & append(const basic_string &__str)
Append a string to this string.
constexpr basic_string & operator+=(initializer_list< _CharT > __l)
Append an initializer_list of characters.
constexpr const_reference back() const noexcept
constexpr basic_string & operator=(const basic_string &__str)
Assign the value of str to this string.
constexpr size_type find_last_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character of string.
constexpr reference front() noexcept
constexpr iterator end() noexcept
constexpr basic_string & append(const _CharT *__s)
Append a C string.
constexpr iterator begin() noexcept
constexpr basic_string & replace(__const_iterator __i1, __const_iterator __i2, const _CharT *__s, size_type __n)
Replace range of characters with C substring.
constexpr size_type find(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a string.
constexpr basic_string & replace(__const_iterator __i1, __const_iterator __i2, const _CharT *__s)
Replace range of characters with C string.
constexpr basic_string & insert(size_type __pos, size_type __n, _CharT __c)
Insert multiple characters.
constexpr void push_back(_CharT __c)
Append a single character.
constexpr reverse_iterator rbegin() noexcept
constexpr basic_string & operator=(initializer_list< _CharT > __l)
Set value to string constructed from initializer list.
constexpr void resize(size_type __n)
Resizes the string to the specified number of characters.
constexpr size_type find_first_not_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character not in string.
constexpr basic_string & assign(const _CharT *__s)
Set value to contents of a C string.
constexpr basic_string & insert(size_type __pos1, const basic_string &__str, size_type __pos2, size_type __n=npos)
Insert a substring.
constexpr basic_string & assign(size_type __n, _CharT __c)
Set value to multiple characters.
constexpr iterator insert(const_iterator __p, size_type __n, _CharT __c)
Insert multiple characters.
constexpr size_type find_last_of(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find last position of a character of C substring.
constexpr size_type capacity() const noexcept
constexpr basic_string & insert(size_type __pos, const _CharT *__s)
Insert a C string.
constexpr bool empty() const noexcept
constexpr 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.
constexpr int compare(const _CharT *__s) const noexcept
Compare to a C string.
static const size_type npos
constexpr basic_string & erase(size_type __pos=0, size_type __n=npos)
Remove characters.
constexpr size_type find_first_of(_CharT __c, size_type __pos=0) const noexcept
Find position of a character.
constexpr basic_string & replace(const_iterator __i1, const_iterator __i2, _InputIterator __k1, _InputIterator __k2)
Replace range of characters with range.
constexpr iterator erase(__const_iterator __position)
Remove one character.
constexpr allocator_type get_allocator() const noexcept
Return copy of allocator used to construct this string.
constexpr size_type find(_CharT __c, size_type __pos=0) const noexcept
Find position of a character.
constexpr basic_string & replace(size_type __pos, size_type __n, const basic_string &__str)
Replace characters with value from another string.
constexpr basic_string & assign(_InputIterator __first, _InputIterator __last)
Set value to a range of characters.
constexpr const_reverse_iterator crend() const noexcept
constexpr basic_string & operator+=(_CharT __c)
Append a character.
constexpr basic_string(const _CharT *__s, size_type __n, const _Alloc &__a=_Alloc())
Construct string initialized by a character array.
constexpr const_reference front() const noexcept
constexpr basic_string_view< _CharT, _Traits > subview(size_type __pos=0, size_type __n=npos) const
Get a subview.
constexpr int compare(const basic_string &__str) const
Compare to a string.
constexpr int compare(size_type __pos, size_type __n1, const _CharT *__s, size_type __n2) const
Compare substring against a character array.
constexpr size_type find_last_not_of(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find last position of a character not in C substring.
constexpr basic_string(initializer_list< _CharT > __l, const _Alloc &__a=_Alloc())
Construct string from an initializer list.
constexpr _CharT * data() noexcept
Return non-const pointer to contents.
constexpr basic_string & append(const _CharT *__s, size_type __n)
Append a C substring.
constexpr size_type max_size() const noexcept
Returns the size() of the largest possible string.
constexpr void swap(basic_string &__s) noexcept
Swap contents with another string.
constexpr basic_string(basic_string &&__str) noexcept
Move construct string.
constexpr const_reference operator[](size_type __pos) const noexcept
Subscript access to the data contained in the string.
constexpr void pop_back() noexcept
Remove the last character.
constexpr int compare(size_type __pos, size_type __n1, const _CharT *__s) const
Compare substring to a C string.
constexpr basic_string & operator+=(const basic_string &__str)
Append a string to this string.
constexpr iterator insert(const_iterator __p, initializer_list< _CharT > __l)
Insert an initializer_list of characters.
constexpr const_iterator cend() const noexcept
constexpr basic_string & operator=(_CharT __c)
Set value to string of length 1.
constexpr size_type find_last_of(const _CharT *__s, size_type __pos=npos) const noexcept
Find last position of a character of C string.
Basis for explicit traits specializations.
Traits class for iterators.
Forward iterators support a superset of input iterator operations.
Common iterator class.
Uniform interface to C++98 and C++11 allocators.
static constexpr pointer allocate(_Char_alloc_type &__a, size_type __n)
static constexpr void deallocate(_Char_alloc_type &__a, pointer __p, size_type __n)
static constexpr size_type max_size(const _Char_alloc_type &__a) noexcept
[range.sized] The sized_range concept.
A range for which ranges::begin returns an input iterator.
A range for which ranges::begin returns a forward iterator.