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 if constexpr (requires {
831 requires is_same_v<ranges::iterator_t<_Rg>, _SbufIter>;
832 requires is_same_v<ranges::sentinel_t<_Rg>, _SbufIter> ||
833 is_same_v<ranges::sentinel_t<_Rg>, default_sentinel_t>; })
834 {
835 _M_construct(ranges::begin(__rg), _SbufIter(),
836 input_iterator_tag{});
837 }
838 else if constexpr (ranges::forward_range<_Rg>
839 || ranges::sized_range<_Rg>)
840 {
841 const auto __n = static_cast<size_type>(ranges::distance(__rg));
842 reserve(__n);
843 _S_copy_range(_M_data(), std::forward<_Rg>(__rg), __n);
844 _M_set_length(__n);
845 }
846 else
847 {
848 auto __first = ranges::begin(__rg);
849 const auto __last = ranges::end(__rg);
850 for (; __first != __last; ++__first)
851 push_back(*__first);
852 }
853 }
854#endif
855
856 /**
857 * @brief Construct string from an initializer %list.
858 * @param __l std::initializer_list of characters.
859 * @param __a Allocator to use (default is default allocator).
860 */
861 _GLIBCXX20_CONSTEXPR
862 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
863 : _M_dataplus(_M_local_data(), __a)
864 { _M_construct(__l.begin(), __l.end(), std::forward_iterator_tag()); }
865
866 _GLIBCXX20_CONSTEXPR
867 basic_string(const basic_string& __str, const _Alloc& __a)
868 : _M_dataplus(_M_local_data(), __a)
869 { _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag()); }
870
871 _GLIBCXX20_CONSTEXPR
872 basic_string(basic_string&& __str, const _Alloc& __a)
873 noexcept(_Alloc_traits::_S_always_equal())
874 : _M_dataplus(_M_local_data(), __a)
875 {
876 if (__str._M_is_local())
877 {
878 _M_init_local_buf();
879 traits_type::copy(_M_local_buf, __str._M_local_buf,
880 __str.length() + 1);
881 _M_length(__str.length());
882 __str._M_set_length(0);
883 }
884 else if (_Alloc_traits::_S_always_equal()
885 || __str.get_allocator() == __a)
886 {
887 _M_data(__str._M_data());
888 _M_length(__str.length());
889 _M_capacity(__str._M_allocated_capacity);
890 __str._M_data(__str._M_use_local_data());
891 __str._M_set_length(0);
892 }
893 else
894 _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag());
895 }
896#endif // C++11
897
898#if __cplusplus >= 202100L
899 basic_string(nullptr_t) = delete;
900 basic_string& operator=(nullptr_t) = delete;
901#endif // C++23
902
903 /**
904 * @brief Construct string as copy of a range.
905 * @param __beg Start of range.
906 * @param __end End of range.
907 * @param __a Allocator to use (default is default allocator).
908 */
909#if __cplusplus >= 201103L
910 template<typename _InputIterator,
911 typename = std::_RequireInputIter<_InputIterator>>
912#else
913 template<typename _InputIterator>
914#endif
915 _GLIBCXX20_CONSTEXPR
916 basic_string(_InputIterator __beg, _InputIterator __end,
917 const _Alloc& __a = _Alloc())
918 : _M_dataplus(_M_local_data(), __a), _M_string_length(0)
919 {
920#if __cplusplus >= 201103L
921 _M_construct(__beg, __end, std::__iterator_category(__beg));
922#else
923 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
924 _M_construct_aux(__beg, __end, _Integral());
925#endif
926 }
927
928#ifdef __glibcxx_string_view // >= C++17
929 /**
930 * @brief Construct string from a substring of a string_view.
931 * @param __t Source object convertible to string view.
932 * @param __pos The index of the first character to copy from __t.
933 * @param __n The number of characters to copy from __t.
934 * @param __a Allocator to use.
935 */
936 template<typename _Tp,
938 _GLIBCXX20_CONSTEXPR
939 basic_string(const _Tp& __t, size_type __pos, size_type __n,
940 const _Alloc& __a = _Alloc())
941 : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
942
943 /**
944 * @brief Construct string from a string_view.
945 * @param __t Source object convertible to string view.
946 * @param __a Allocator to use (default is default allocator).
947 */
948 template<typename _Tp, typename = _If_sv<_Tp, void>>
949 _GLIBCXX20_CONSTEXPR
950 explicit
951 basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
952 : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
953#endif // C++17
954
955 /**
956 * @brief Destroy the string instance.
957 */
958 _GLIBCXX20_CONSTEXPR
960 { _M_dispose(); }
961
962 /**
963 * @brief Assign the value of @a str to this string.
964 * @param __str Source string.
965 */
966 _GLIBCXX20_CONSTEXPR
969 {
970 return this->assign(__str);
971 }
972
973 /**
974 * @brief Copy contents of @a s into this string.
975 * @param __s Source null-terminated string.
976 */
977 _GLIBCXX20_CONSTEXPR
979 operator=(const _CharT* __s)
980 { return this->assign(__s); }
981
982 /**
983 * @brief Set value to string of length 1.
984 * @param __c Source character.
985 *
986 * Assigning to a character makes this string length 1 and
987 * (*this)[0] == @a c.
988 */
989 _GLIBCXX20_CONSTEXPR
991 operator=(_CharT __c)
992 {
993 this->assign(1, __c);
994 return *this;
995 }
996
997#if __cplusplus >= 201103L
998 /**
999 * @brief Move assign the value of @a str to this string.
1000 * @param __str Source string.
1001 *
1002 * The contents of @a str are moved into this string (without copying).
1003 * @a str is a valid, but unspecified string.
1004 */
1005 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1006 // 2063. Contradictory requirements for string move assignment
1007 _GLIBCXX20_CONSTEXPR
1010 noexcept(_Alloc_traits::_S_nothrow_move())
1011 {
1012 const bool __equal_allocs = _Alloc_traits::_S_always_equal()
1013 || _M_get_allocator() == __str._M_get_allocator();
1014 if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
1015 && !__equal_allocs)
1016 {
1017 // Destroy existing storage before replacing allocator.
1018 _M_destroy(_M_allocated_capacity);
1019 _M_data(_M_local_data());
1020 _M_set_length(0);
1021 }
1022 // Replace allocator if POCMA is true.
1023 std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator());
1024
1025 if (__str._M_is_local())
1026 {
1027 // We've always got room for a short string, just copy it
1028 // (unless this is a self-move, because that would violate the
1029 // char_traits::copy precondition that the ranges don't overlap).
1030 if (__builtin_expect(std::__addressof(__str) != this, true))
1031 {
1032 if (__str.size())
1033 this->_S_copy(_M_data(), __str._M_data(), __str.size());
1034 _M_set_length(__str.size());
1035 }
1036 }
1037 else if (_Alloc_traits::_S_propagate_on_move_assign() || __equal_allocs)
1038 {
1039 // Just move the allocated pointer, our allocator can free it.
1040 pointer __data = nullptr;
1041 size_type __capacity;
1042 if (!_M_is_local())
1043 {
1044 if (__equal_allocs)
1045 {
1046 // __str can reuse our existing storage.
1047 __data = _M_data();
1048 __capacity = _M_allocated_capacity;
1049 }
1050 else // __str can't use it, so free it.
1051 _M_destroy(_M_allocated_capacity);
1052 }
1053
1054 _M_data(__str._M_data());
1055 _M_length(__str.length());
1056 _M_capacity(__str._M_allocated_capacity);
1057 if (__data)
1058 {
1059 __str._M_data(__data);
1060 __str._M_capacity(__capacity);
1061 }
1062 else
1063 __str._M_data(__str._M_use_local_data());
1064 }
1065 else // Need to do a deep copy
1066 _M_assign(__str);
1067 __str.clear();
1068 return *this;
1069 }
1070
1071 /**
1072 * @brief Set value to string constructed from initializer %list.
1073 * @param __l std::initializer_list.
1074 */
1075 _GLIBCXX20_CONSTEXPR
1078 {
1079 this->assign(__l.begin(), __l.size());
1080 return *this;
1081 }
1082#endif // C++11
1083
1084#ifdef __glibcxx_string_view // >= C++17
1085 /**
1086 * @brief Set value to string constructed from a string_view.
1087 * @param __svt An object convertible to string_view.
1088 */
1089 template<typename _Tp>
1090 _GLIBCXX20_CONSTEXPR
1091 _If_sv<_Tp, basic_string&>
1092 operator=(const _Tp& __svt)
1093 { return this->assign(__svt); }
1094
1095 /**
1096 * @brief Convert to a string_view.
1097 * @return A string_view.
1098 */
1099 _GLIBCXX20_CONSTEXPR
1100 operator __sv_type() const noexcept
1101 { return __sv_type(data(), size()); }
1102#endif // C++17
1103
1104 // Iterators:
1105 /**
1106 * Returns a read/write iterator that points to the first character in
1107 * the %string.
1108 */
1109 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1110 iterator
1111 begin() _GLIBCXX_NOEXCEPT
1112 { return iterator(_M_data()); }
1113
1114 /**
1115 * Returns a read-only (constant) iterator that points to the first
1116 * character in the %string.
1117 */
1118 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1119 const_iterator
1120 begin() const _GLIBCXX_NOEXCEPT
1121 { return const_iterator(_M_data()); }
1122
1123 /**
1124 * Returns a read/write iterator that points one past the last
1125 * character in the %string.
1126 */
1127 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1128 iterator
1129 end() _GLIBCXX_NOEXCEPT
1130 { return iterator(_M_data() + this->size()); }
1131
1132 /**
1133 * Returns a read-only (constant) iterator that points one past the
1134 * last character in the %string.
1135 */
1136 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1137 const_iterator
1138 end() const _GLIBCXX_NOEXCEPT
1139 { return const_iterator(_M_data() + this->size()); }
1140
1141 /**
1142 * Returns a read/write reverse iterator that points to the last
1143 * character in the %string. Iteration is done in reverse element
1144 * order.
1145 */
1146 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1148 rbegin() _GLIBCXX_NOEXCEPT
1149 { return reverse_iterator(this->end()); }
1150
1151 /**
1152 * Returns a read-only (constant) reverse iterator that points
1153 * to the last character in the %string. Iteration is done in
1154 * reverse element order.
1155 */
1156 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1157 const_reverse_iterator
1158 rbegin() const _GLIBCXX_NOEXCEPT
1159 { return const_reverse_iterator(this->end()); }
1160
1161 /**
1162 * Returns a read/write reverse iterator that points to one before the
1163 * first character in the %string. Iteration is done in reverse
1164 * element order.
1165 */
1166 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1168 rend() _GLIBCXX_NOEXCEPT
1169 { return reverse_iterator(this->begin()); }
1170
1171 /**
1172 * Returns a read-only (constant) reverse iterator that points
1173 * to one before the first character in the %string. Iteration
1174 * is done in reverse element order.
1175 */
1176 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1177 const_reverse_iterator
1178 rend() const _GLIBCXX_NOEXCEPT
1179 { return const_reverse_iterator(this->begin()); }
1180
1181#if __cplusplus >= 201103L
1182 /**
1183 * Returns a read-only (constant) iterator that points to the first
1184 * character in the %string.
1185 */
1186 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1187 const_iterator
1188 cbegin() const noexcept
1189 { return const_iterator(this->_M_data()); }
1190
1191 /**
1192 * Returns a read-only (constant) iterator that points one past the
1193 * last character in the %string.
1194 */
1195 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1196 const_iterator
1197 cend() const noexcept
1198 { return const_iterator(this->_M_data() + this->size()); }
1199
1200 /**
1201 * Returns a read-only (constant) reverse iterator that points
1202 * to the last character in the %string. Iteration is done in
1203 * reverse element order.
1204 */
1205 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1206 const_reverse_iterator
1207 crbegin() const noexcept
1208 { return const_reverse_iterator(this->end()); }
1209
1210 /**
1211 * Returns a read-only (constant) reverse iterator that points
1212 * to one before the first character in the %string. Iteration
1213 * is done in reverse element order.
1214 */
1215 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1216 const_reverse_iterator
1217 crend() const noexcept
1218 { return const_reverse_iterator(this->begin()); }
1219#endif
1220
1221 public:
1222 // Capacity:
1223 /// Returns the number of characters in the string, not including any
1224 /// null-termination.
1225 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1226 size_type
1227 size() const _GLIBCXX_NOEXCEPT
1228 {
1229 size_type __sz = _M_string_length;
1230 if (__sz > max_size ())
1231 __builtin_unreachable();
1232 return __sz;
1233 }
1234
1235 /// Returns the number of characters in the string, not including any
1236 /// null-termination.
1237 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1238 size_type
1239 length() const _GLIBCXX_NOEXCEPT
1240 { return size(); }
1241
1242 /// Returns the size() of the largest possible %string.
1243 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1244 size_type
1245 max_size() const _GLIBCXX_NOEXCEPT
1246 {
1247 const size_t __diffmax
1248 = __gnu_cxx::__numeric_traits<ptrdiff_t>::__max / sizeof(_CharT);
1249 const size_t __allocmax = _Alloc_traits::max_size(_M_get_allocator());
1250 return (std::min)(__diffmax, __allocmax) - 1;
1251 }
1252
1253 /**
1254 * @brief Resizes the %string to the specified number of characters.
1255 * @param __n Number of characters the %string should contain.
1256 * @param __c Character to fill any new elements.
1257 *
1258 * This function will %resize the %string to the specified
1259 * number of characters. If the number is smaller than the
1260 * %string's current size the %string is truncated, otherwise
1261 * the %string is extended and new elements are %set to @a __c.
1262 */
1263 _GLIBCXX20_CONSTEXPR
1264 void
1265 resize(size_type __n, _CharT __c);
1266
1267 /**
1268 * @brief Resizes the %string to the specified number of characters.
1269 * @param __n Number of characters the %string should contain.
1270 *
1271 * This function will resize the %string to the specified length. If
1272 * the new size is smaller than the %string's current size the %string
1273 * is truncated, otherwise the %string is extended and new characters
1274 * are default-constructed. For basic types such as char, this means
1275 * setting them to 0.
1276 */
1277 _GLIBCXX20_CONSTEXPR
1278 void
1279 resize(size_type __n)
1280 { this->resize(__n, _CharT()); }
1281
1282#if __cplusplus >= 201103L
1283#pragma GCC diagnostic push
1284#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1285 /// A non-binding request to reduce capacity() to size().
1286 _GLIBCXX20_CONSTEXPR
1287 void
1288 shrink_to_fit() noexcept
1289 { reserve(); }
1290#pragma GCC diagnostic pop
1291#endif
1292
1293#ifdef __glibcxx_string_resize_and_overwrite // C++ >= 23
1294 /** Resize the string and call a function to fill it.
1295 *
1296 * @param __n The maximum size requested.
1297 * @param __op A callable object that writes characters to the string.
1298 *
1299 * This is a low-level function that is easy to misuse, be careful.
1300 *
1301 * Calling `str.resize_and_overwrite(n, op)` will reserve at least `n`
1302 * characters in `str`, evaluate `n2 = std::move(op)(str.data(), n)`,
1303 * and finally set the string length to `n2` (adding a null terminator
1304 * at the end). The function object `op` is allowed to write to the
1305 * extra capacity added by the initial reserve operation, which is not
1306 * allowed if you just call `str.reserve(n)` yourself.
1307 *
1308 * This can be used to efficiently fill a `string` buffer without the
1309 * overhead of zero-initializing characters that will be overwritten
1310 * anyway.
1311 *
1312 * The callable `op` must not access the string directly (only through
1313 * the pointer passed as its first argument), must not write more than
1314 * `n` characters to the string, must return a value no greater than `n`,
1315 * and must ensure that all characters up to the returned length are
1316 * valid after it returns (i.e. there must be no uninitialized values
1317 * left in the string after the call, because accessing them would
1318 * have undefined behaviour). If `op` exits by throwing an exception
1319 * the behaviour is undefined.
1320 *
1321 * @since C++23
1322 */
1323 template<typename _Operation>
1324 constexpr void
1325 resize_and_overwrite(size_type __n, _Operation __op);
1326#endif
1327
1328#if __cplusplus >= 201103L
1329 /// Non-standard version of resize_and_overwrite for C++11 and above.
1330 template<typename _Operation>
1331 _GLIBCXX20_CONSTEXPR void
1332 __resize_and_overwrite(size_type __n, _Operation __op);
1333#endif
1334
1335 /**
1336 * Returns the total number of characters that the %string can hold
1337 * before needing to allocate more memory.
1338 */
1339 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1340 size_type
1341 capacity() const _GLIBCXX_NOEXCEPT
1342 {
1343 size_t __sz = _M_is_local() ? size_type(_S_local_capacity)
1344 : _M_allocated_capacity;
1345 if (__sz < _S_local_capacity || __sz > max_size ())
1346 __builtin_unreachable();
1347 return __sz;
1348 }
1349
1350 /**
1351 * @brief Attempt to preallocate enough memory for specified number of
1352 * characters.
1353 * @param __res_arg Number of characters required.
1354 * @throw std::length_error If @a __res_arg exceeds @c max_size().
1355 *
1356 * This function attempts to reserve enough memory for the
1357 * %string to hold the specified number of characters. If the
1358 * number requested is more than max_size(), length_error is
1359 * thrown.
1360 *
1361 * The advantage of this function is that if optimal code is a
1362 * necessity and the user can determine the string length that will be
1363 * required, the user can reserve the memory in %advance, and thus
1364 * prevent a possible reallocation of memory and copying of %string
1365 * data.
1366 */
1367 _GLIBCXX20_CONSTEXPR
1368 void
1369 reserve(size_type __res_arg);
1370
1371 /**
1372 * Equivalent to shrink_to_fit().
1373 */
1374#if __cplusplus >= 202002L
1375 [[deprecated("use shrink_to_fit() instead")]]
1376#endif
1377 _GLIBCXX20_CONSTEXPR
1378 void
1380
1381 /**
1382 * Erases the string, making it empty.
1383 */
1384 _GLIBCXX20_CONSTEXPR
1385 void
1386 clear() _GLIBCXX_NOEXCEPT
1387 { _M_set_length(0); }
1388
1389 /**
1390 * Returns true if the %string is empty. Equivalent to
1391 * <code>*this == ""</code>.
1392 */
1393 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1394 bool
1395 empty() const _GLIBCXX_NOEXCEPT
1396 { return _M_string_length == 0; }
1397
1398 // Element access:
1399 /**
1400 * @brief Subscript access to the data contained in the %string.
1401 * @param __pos The index of the character to access.
1402 * @return Read-only (constant) reference to the character.
1403 *
1404 * This operator allows for easy, array-style, data access.
1405 * Note that data access with this operator is unchecked and
1406 * out_of_range lookups are not defined. (For checked lookups
1407 * see at().)
1408 */
1409 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1410 const_reference
1411 operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
1412 {
1413 __glibcxx_assert(__pos <= size());
1414 return _M_data()[__pos];
1415 }
1416
1417 /**
1418 * @brief Subscript access to the data contained in the %string.
1419 * @param __pos The index of the character to access.
1420 * @return Read/write reference to the character.
1421 *
1422 * This operator allows for easy, array-style, data access.
1423 * Note that data access with this operator is unchecked and
1424 * out_of_range lookups are not defined. (For checked lookups
1425 * see at().)
1426 */
1427 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1428 reference
1429 operator[](size_type __pos)
1430 {
1431 // Allow pos == size() both in C++98 mode, as v3 extension,
1432 // and in C++11 mode.
1433 __glibcxx_assert(__pos <= size());
1434 // In pedantic mode be strict in C++98 mode.
1435 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
1436 return _M_data()[__pos];
1437 }
1438
1439 /**
1440 * @brief Provides access to the data contained in the %string.
1441 * @param __n The index of the character to access.
1442 * @return Read-only (const) reference to the character.
1443 * @throw std::out_of_range If @a n is an invalid index.
1444 *
1445 * This function provides for safer data access. The parameter is
1446 * first checked that it is in the range of the string. The function
1447 * throws out_of_range if the check fails.
1448 */
1449 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1450 const_reference
1451 at(size_type __n) const
1452 {
1453 if (__n >= this->size())
1454 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1455 "(which is %zu) >= this->size() "
1456 "(which is %zu)"),
1457 __n, this->size());
1458 return _M_data()[__n];
1459 }
1460
1461 /**
1462 * @brief Provides access to the data contained in the %string.
1463 * @param __n The index of the character to access.
1464 * @return Read/write reference to the character.
1465 * @throw std::out_of_range If @a n is an invalid index.
1466 *
1467 * This function provides for safer data access. The parameter is
1468 * first checked that it is in the range of the string. The function
1469 * throws out_of_range if the check fails.
1470 */
1471 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1472 reference
1473 at(size_type __n)
1474 {
1475 if (__n >= size())
1476 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1477 "(which is %zu) >= this->size() "
1478 "(which is %zu)"),
1479 __n, this->size());
1480 return _M_data()[__n];
1481 }
1482
1483#if __cplusplus >= 201103L
1484 /**
1485 * Returns a read/write reference to the data at the first
1486 * element of the %string.
1487 */
1488 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1489 reference
1490 front() noexcept
1491 {
1492 __glibcxx_assert(!empty());
1493 return operator[](0);
1494 }
1495
1496 /**
1497 * Returns a read-only (constant) reference to the data at the first
1498 * element of the %string.
1499 */
1500 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1501 const_reference
1502 front() const noexcept
1503 {
1504 __glibcxx_assert(!empty());
1505 return operator[](0);
1506 }
1507
1508 /**
1509 * Returns a read/write reference to the data at the last
1510 * element of the %string.
1511 */
1512 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1513 reference
1514 back() noexcept
1515 {
1516 __glibcxx_assert(!empty());
1517 return operator[](this->size() - 1);
1518 }
1519
1520 /**
1521 * Returns a read-only (constant) reference to the data at the
1522 * last element of the %string.
1523 */
1524 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1525 const_reference
1526 back() const noexcept
1527 {
1528 __glibcxx_assert(!empty());
1529 return operator[](this->size() - 1);
1530 }
1531#endif
1532
1533 // Modifiers:
1534 /**
1535 * @brief Append a string to this string.
1536 * @param __str The string to append.
1537 * @return Reference to this string.
1538 */
1539 _GLIBCXX20_CONSTEXPR
1542 { return this->append(__str); }
1543
1544 /**
1545 * @brief Append a C string.
1546 * @param __s The C string to append.
1547 * @return Reference to this string.
1548 */
1549 _GLIBCXX20_CONSTEXPR
1551 operator+=(const _CharT* __s)
1552 { return this->append(__s); }
1553
1554 /**
1555 * @brief Append a character.
1556 * @param __c The character to append.
1557 * @return Reference to this string.
1558 */
1559 _GLIBCXX20_CONSTEXPR
1561 operator+=(_CharT __c)
1562 {
1563 this->push_back(__c);
1564 return *this;
1565 }
1566
1567#if __cplusplus >= 201103L
1568 /**
1569 * @brief Append an initializer_list of characters.
1570 * @param __l The initializer_list of characters to be appended.
1571 * @return Reference to this string.
1572 */
1573 _GLIBCXX20_CONSTEXPR
1576 { return this->append(__l.begin(), __l.size()); }
1577#endif // C++11
1578
1579#ifdef __glibcxx_string_view // >= C++17
1580 /**
1581 * @brief Append a string_view.
1582 * @param __svt An object convertible to string_view to be appended.
1583 * @return Reference to this string.
1584 */
1585 template<typename _Tp>
1586 _GLIBCXX20_CONSTEXPR
1587 _If_sv<_Tp, basic_string&>
1588 operator+=(const _Tp& __svt)
1589 { return this->append(__svt); }
1590#endif // C++17
1591
1592 /**
1593 * @brief Append a string to this string.
1594 * @param __str The string to append.
1595 * @return Reference to this string.
1596 */
1597 _GLIBCXX20_CONSTEXPR
1599 append(const basic_string& __str)
1600 { return this->append(__str._M_data(), __str.size()); }
1601
1602 /**
1603 * @brief Append a substring.
1604 * @param __str The string to append.
1605 * @param __pos Index of the first character of str to append.
1606 * @param __n The number of characters to append.
1607 * @return Reference to this string.
1608 * @throw std::out_of_range if @a __pos is not a valid index.
1609 *
1610 * This function appends @a __n characters from @a __str
1611 * starting at @a __pos to this string. If @a __n is is larger
1612 * than the number of available characters in @a __str, the
1613 * remainder of @a __str is appended.
1614 */
1615 _GLIBCXX20_CONSTEXPR
1617 append(const basic_string& __str, size_type __pos, size_type __n = npos)
1618 { return this->append(__str._M_data()
1619 + __str._M_check(__pos, "basic_string::append"),
1620 __str._M_limit(__pos, __n)); }
1621
1622 /**
1623 * @brief Append a C substring.
1624 * @param __s The C string to append.
1625 * @param __n The number of characters to append.
1626 * @return Reference to this string.
1627 */
1628 _GLIBCXX20_CONSTEXPR
1630 append(const _CharT* __s, size_type __n)
1631 {
1632 __glibcxx_requires_string_len(__s, __n);
1633 _M_check_length(size_type(0), __n, "basic_string::append");
1634 return _M_append(__s, __n);
1635 }
1636
1637 /**
1638 * @brief Append a C string.
1639 * @param __s The C string to append.
1640 * @return Reference to this string.
1641 */
1642 _GLIBCXX20_CONSTEXPR
1644 append(const _CharT* __s)
1645 {
1646 __glibcxx_requires_string(__s);
1647 const size_type __n = traits_type::length(__s);
1648 _M_check_length(size_type(0), __n, "basic_string::append");
1649 return _M_append(__s, __n);
1650 }
1651
1652 /**
1653 * @brief Append multiple characters.
1654 * @param __n The number of characters to append.
1655 * @param __c The character to use.
1656 * @return Reference to this string.
1657 *
1658 * Appends __n copies of __c to this string.
1659 */
1660 _GLIBCXX20_CONSTEXPR
1662 append(size_type __n, _CharT __c)
1663 { return _M_replace_aux(this->size(), size_type(0), __n, __c); }
1664
1665#if __glibcxx_containers_ranges // C++ >= 23
1666 /**
1667 * @brief Append a range to the string.
1668 * @param __rg A range of values that are convertible to `value_type`.
1669 * @since C++23
1670 *
1671 * The range `__rg` is allowed to overlap with `*this`.
1672 */
1673 template<__detail::__container_compatible_range<_CharT> _Rg>
1674 constexpr basic_string&
1675 append_range(_Rg&& __rg)
1676 {
1677 // N.B. __rg may overlap with *this, so we must copy from __rg before
1678 // existing elements or iterators referring to *this are invalidated.
1679 // e.g. in s.append_range(views::concat(s, str)), rg overlaps s.
1681 {
1682 const auto __len = size_type(ranges::distance(__rg));
1683
1684 // Don't care if this addition wraps around, we check it below:
1685 const size_type __newlen = size() + __len;
1686
1687 if ((capacity() - size()) >= __len)
1688 _S_copy_range(_M_data() + size(), std::forward<_Rg>(__rg),
1689 __len);
1690 else
1691 {
1692 _M_check_length(0, __len, "basic_string::append_range");
1693 basic_string __s(_M_get_allocator());
1694 __s.reserve(__newlen);
1695 _S_copy_range(__s._M_data() + size(), std::forward<_Rg>(__rg),
1696 __len);
1697 _S_copy(__s._M_data(), _M_data(), size());
1698 if (!_M_is_local())
1699 _M_destroy(_M_allocated_capacity);
1700 _M_data(__s._M_data());
1701 _M_capacity(__s._M_allocated_capacity);
1702 __s._M_data(__s._M_local_data());
1703 __s._M_length(0);
1704 }
1705 _M_set_length(__newlen); // adds null-terminator
1706 }
1707 else
1708 {
1709 basic_string __s(from_range, std::forward<_Rg>(__rg),
1710 _M_get_allocator());
1711 append(__s);
1712 }
1713 return *this;
1714 }
1715#endif
1716
1717#if __cplusplus >= 201103L
1718 /**
1719 * @brief Append an initializer_list of characters.
1720 * @param __l The initializer_list of characters to append.
1721 * @return Reference to this string.
1722 */
1723 _GLIBCXX20_CONSTEXPR
1726 { return this->append(__l.begin(), __l.size()); }
1727#endif // C++11
1728
1729 /**
1730 * @brief Append a range of characters.
1731 * @param __first Iterator referencing the first character to append.
1732 * @param __last Iterator marking the end of the range.
1733 * @return Reference to this string.
1734 *
1735 * Appends characters in the range [__first,__last) to this string.
1736 */
1737#if __cplusplus >= 201103L
1738 template<class _InputIterator,
1739 typename = std::_RequireInputIter<_InputIterator>>
1740 _GLIBCXX20_CONSTEXPR
1741#else
1742 template<class _InputIterator>
1743#endif
1745 append(_InputIterator __first, _InputIterator __last)
1746 { return this->replace(end(), end(), __first, __last); }
1747
1748#ifdef __glibcxx_string_view
1749 /**
1750 * @brief Append a string_view.
1751 * @param __svt An object convertible to string_view to be appended.
1752 * @return Reference to this string.
1753 */
1754 template<typename _Tp>
1755 _GLIBCXX20_CONSTEXPR
1756 _If_sv<_Tp, basic_string&>
1757 append(const _Tp& __svt)
1758 {
1759 __sv_type __sv = __svt;
1760 return this->append(__sv.data(), __sv.size());
1761 }
1762
1763 /**
1764 * @brief Append a range of characters from a string_view.
1765 * @param __svt An object convertible to string_view to be appended from.
1766 * @param __pos The position in the string_view to append from.
1767 * @param __n The number of characters to append from the string_view.
1768 * @return Reference to this string.
1769 */
1770 template<typename _Tp>
1771 _GLIBCXX20_CONSTEXPR
1772 _If_sv<_Tp, basic_string&>
1773 append(const _Tp& __svt, size_type __pos, size_type __n = npos)
1774 {
1775 __sv_type __sv = __svt;
1776 return _M_append(__sv.data()
1777 + std::__sv_check(__sv.size(), __pos, "basic_string::append"),
1778 std::__sv_limit(__sv.size(), __pos, __n));
1779 }
1780
1781 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1782 // 3662. basic_string::append/assign(NTBS, pos, n) suboptimal
1783 /**
1784 * @brief Append a C substring.
1785 * @param __s The C string to append.
1786 * @param __pos The position in the C string to append from.
1787 * @param __n The number of characters to append.
1788 * @return Reference to this string.
1789 */
1790 _GLIBCXX20_CONSTEXPR
1792 append(const _CharT* __s, size_type __pos, size_type __n)
1793 { return append(__sv_type(__s).substr(__pos, __n)); }
1794#endif // C++17
1795
1796 /**
1797 * @brief Append a single character.
1798 * @param __c Character to append.
1799 */
1800 _GLIBCXX20_CONSTEXPR
1801 void
1802 push_back(_CharT __c)
1803 {
1804 const size_type __size = this->size();
1805 if (__size + 1 > this->capacity())
1806 this->_M_mutate(__size, size_type(0), 0, size_type(1));
1807 traits_type::assign(this->_M_data()[__size], __c);
1808 this->_M_set_length(__size + 1);
1809 }
1810
1811 /**
1812 * @brief Set value to contents of another string.
1813 * @param __str Source string to use.
1814 * @return Reference to this string.
1815 */
1816 _GLIBCXX20_CONSTEXPR
1818 assign(const basic_string& __str)
1819 {
1820#if __cplusplus >= 201103L
1821 if (_Alloc_traits::_S_propagate_on_copy_assign())
1822 {
1823 if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
1824 && _M_get_allocator() != __str._M_get_allocator())
1825 {
1826 // Propagating allocator cannot free existing storage so must
1827 // deallocate it before replacing current allocator.
1828 if (__str.size() <= _S_local_capacity)
1829 {
1830 _M_destroy(_M_allocated_capacity);
1831 _M_data(_M_use_local_data());
1832 _M_set_length(0);
1833 }
1834 else
1835 {
1836 const auto __len = __str.size();
1837 auto __alloc = __str._M_get_allocator();
1838 // If this allocation throws there are no effects:
1839 auto __r = _S_allocate_at_least(__alloc, __len + 1);
1840 _M_destroy(_M_allocated_capacity);
1841 _M_data(__r.__ptr);
1842 _M_capacity(__r.__count - 1);
1843 _M_set_length(__len);
1844 }
1845 }
1846 std::__alloc_on_copy(_M_get_allocator(), __str._M_get_allocator());
1847 }
1848#endif
1849 this->_M_assign(__str);
1850 return *this;
1851 }
1852
1853#if __cplusplus >= 201103L
1854 /**
1855 * @brief Set value to contents of another string.
1856 * @param __str Source string to use.
1857 * @return Reference to this string.
1858 *
1859 * This function sets this string to the exact contents of @a __str.
1860 * @a __str is a valid, but unspecified string.
1861 */
1862 _GLIBCXX20_CONSTEXPR
1865 noexcept(_Alloc_traits::_S_nothrow_move())
1866 {
1867 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1868 // 2063. Contradictory requirements for string move assignment
1869 return *this = std::move(__str);
1870 }
1871#endif // C++11
1872
1873 /**
1874 * @brief Set value to a substring of a string.
1875 * @param __str The string to use.
1876 * @param __pos Index of the first character of str.
1877 * @param __n Number of characters to use.
1878 * @return Reference to this string.
1879 * @throw std::out_of_range if @a pos is not a valid index.
1880 *
1881 * This function sets this string to the substring of @a __str
1882 * consisting of @a __n characters at @a __pos. If @a __n is
1883 * is larger than the number of available characters in @a
1884 * __str, the remainder of @a __str is used.
1885 */
1886 _GLIBCXX20_CONSTEXPR
1888 assign(const basic_string& __str, size_type __pos, size_type __n = npos)
1889 { return _M_replace(size_type(0), this->size(), __str._M_data()
1890 + __str._M_check(__pos, "basic_string::assign"),
1891 __str._M_limit(__pos, __n)); }
1892
1893 /**
1894 * @brief Set value to a C substring.
1895 * @param __s The C string to use.
1896 * @param __n Number of characters to use.
1897 * @return Reference to this string.
1898 *
1899 * This function sets the value of this string to the first @a __n
1900 * characters of @a __s. If @a __n is is larger than the number of
1901 * available characters in @a __s, the remainder of @a __s is used.
1902 */
1903 _GLIBCXX20_CONSTEXPR
1905 assign(const _CharT* __s, size_type __n)
1906 {
1907 __glibcxx_requires_string_len(__s, __n);
1908 return _M_replace(size_type(0), this->size(), __s, __n);
1909 }
1910
1911 /**
1912 * @brief Set value to contents of a C string.
1913 * @param __s The C string to use.
1914 * @return Reference to this string.
1915 *
1916 * This function sets the value of this string to the value of @a __s.
1917 * The data is copied, so there is no dependence on @a __s once the
1918 * function returns.
1919 */
1920 _GLIBCXX20_CONSTEXPR
1922 assign(const _CharT* __s)
1923 {
1924 __glibcxx_requires_string(__s);
1925 return _M_replace(size_type(0), this->size(), __s,
1926 traits_type::length(__s));
1927 }
1928
1929 /**
1930 * @brief Set value to multiple characters.
1931 * @param __n Length of the resulting string.
1932 * @param __c The character to use.
1933 * @return Reference to this string.
1934 *
1935 * This function sets the value of this string to @a __n copies of
1936 * character @a __c.
1937 */
1938 _GLIBCXX20_CONSTEXPR
1940 assign(size_type __n, _CharT __c)
1941 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
1942
1943 /**
1944 * @brief Set value to a range of characters.
1945 * @param __first Iterator referencing the first character to append.
1946 * @param __last Iterator marking the end of the range.
1947 * @return Reference to this string.
1948 *
1949 * Sets value of string to characters in the range [__first,__last).
1950 */
1951#if __cplusplus >= 201103L
1952#pragma GCC diagnostic push
1953#pragma GCC diagnostic ignored "-Wc++17-extensions"
1954 template<class _InputIterator,
1955 typename = std::_RequireInputIter<_InputIterator>>
1956 _GLIBCXX20_CONSTEXPR
1958 assign(_InputIterator __first, _InputIterator __last)
1959 {
1960 using _IterTraits = iterator_traits<_InputIterator>;
1961 if constexpr (is_pointer<decltype(std::__niter_base(__first))>::value
1962 && is_same<typename _IterTraits::value_type,
1963 _CharT>::value)
1964 {
1965 __glibcxx_requires_valid_range(__first, __last);
1966 return _M_replace(size_type(0), size(),
1967 std::__niter_base(__first), __last - __first);
1968 }
1969#if __cplusplus >= 202002L
1970 else if constexpr (contiguous_iterator<_InputIterator>
1971 && is_same_v<iter_value_t<_InputIterator>,
1972 _CharT>)
1973 {
1974 __glibcxx_requires_valid_range(__first, __last);
1975 return _M_replace(size_type(0), size(),
1976 std::to_address(__first), __last - __first);
1977 }
1978#endif
1979 else
1980 return *this = basic_string(__first, __last, get_allocator());
1981 }
1982#pragma GCC diagnostic pop
1983#else
1984 template<class _InputIterator>
1986 assign(_InputIterator __first, _InputIterator __last)
1987 { return this->replace(begin(), end(), __first, __last); }
1988#endif
1989
1990#if __glibcxx_containers_ranges // C++ >= 23
1991 /**
1992 * @brief Assign a range to the string.
1993 * @param __rg A range of values that are convertible to `value_type`.
1994 * @since C++23
1995 *
1996 * The range `__rg` is allowed to overlap with `*this`.
1997 */
1998 template<__detail::__container_compatible_range<_CharT> _Rg>
1999 constexpr basic_string&
2000 assign_range(_Rg&& __rg)
2001 {
2002 basic_string __s(from_range, std::forward<_Rg>(__rg),
2003 _M_get_allocator());
2004 assign(std::move(__s));
2005 return *this;
2006 }
2007#endif
2008
2009#if __cplusplus >= 201103L
2010 /**
2011 * @brief Set value to an initializer_list of characters.
2012 * @param __l The initializer_list of characters to assign.
2013 * @return Reference to this string.
2014 */
2015 _GLIBCXX20_CONSTEXPR
2018 {
2019 // The initializer_list array cannot alias the characters in *this
2020 // so we don't need to use replace to that case.
2021 const size_type __n = __l.size();
2022 if (__n > capacity())
2023 *this = basic_string(__l.begin(), __l.end(), get_allocator());
2024 else
2025 {
2026 if (__n)
2027 _S_copy(_M_data(), __l.begin(), __n);
2028 _M_set_length(__n);
2029 }
2030 return *this;
2031 }
2032#endif // C++11
2033
2034#ifdef __glibcxx_string_view // >= C++17
2035 /**
2036 * @brief Set value from a string_view.
2037 * @param __svt The source object convertible to string_view.
2038 * @return Reference to this string.
2039 */
2040 template<typename _Tp>
2041 _GLIBCXX20_CONSTEXPR
2042 _If_sv<_Tp, basic_string&>
2043 assign(const _Tp& __svt)
2044 {
2045 __sv_type __sv = __svt;
2046 return this->assign(__sv.data(), __sv.size());
2047 }
2048
2049 /**
2050 * @brief Set value from a range of characters in a string_view.
2051 * @param __svt The source object convertible to string_view.
2052 * @param __pos The position in the string_view to assign from.
2053 * @param __n The number of characters to assign.
2054 * @return Reference to this string.
2055 */
2056 template<typename _Tp>
2057 _GLIBCXX20_CONSTEXPR
2058 _If_sv<_Tp, basic_string&>
2059 assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
2060 {
2061 __sv_type __sv = __svt;
2062 return _M_replace(size_type(0), this->size(),
2063 __sv.data()
2064 + std::__sv_check(__sv.size(), __pos, "basic_string::assign"),
2065 std::__sv_limit(__sv.size(), __pos, __n));
2066 }
2067
2068 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2069 // 3662. basic_string::append/assign(NTBS, pos, n) suboptimal
2070 /**
2071 * @brief Set value to a C substring.
2072 * @param __s The C string to use.
2073 * @param __pos The position in the C string to assign from.
2074 * @param __n Number of characters to use.
2075 * @return Reference to this string.
2076 */
2077 _GLIBCXX20_CONSTEXPR
2079 assign(const _CharT* __s, size_type __pos, size_type __n)
2080 { return assign(__sv_type(__s).substr(__pos, __n)); }
2081#endif // C++17
2082
2083#if __cplusplus >= 201103L
2084 /**
2085 * @brief Insert multiple characters.
2086 * @param __p Const_iterator referencing location in string to
2087 * insert at.
2088 * @param __n Number of characters to insert
2089 * @param __c The character to insert.
2090 * @return Iterator referencing the first inserted char.
2091 * @throw std::length_error If new length exceeds @c max_size().
2092 *
2093 * Inserts @a __n copies of character @a __c starting at the
2094 * position referenced by iterator @a __p. If adding
2095 * characters causes the length to exceed max_size(),
2096 * length_error is thrown. The value of the string doesn't
2097 * change if an error is thrown.
2098 */
2099 _GLIBCXX20_CONSTEXPR
2100 iterator
2101 insert(const_iterator __p, size_type __n, _CharT __c)
2102 {
2103 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
2104 const size_type __pos = __p - begin();
2105 this->replace(__p, __p, __n, __c);
2106 return iterator(this->_M_data() + __pos);
2107 }
2108#else
2109 /**
2110 * @brief Insert multiple characters.
2111 * @param __p Iterator referencing location in string to insert at.
2112 * @param __n Number of characters to insert
2113 * @param __c The character to insert.
2114 * @throw std::length_error If new length exceeds @c max_size().
2115 *
2116 * Inserts @a __n copies of character @a __c starting at the
2117 * position referenced by iterator @a __p. If adding
2118 * characters causes the length to exceed max_size(),
2119 * length_error is thrown. The value of the string doesn't
2120 * change if an error is thrown.
2121 */
2122 void
2123 insert(iterator __p, size_type __n, _CharT __c)
2124 { this->replace(__p, __p, __n, __c); }
2125#endif
2126
2127#if __cplusplus >= 201103L
2128 /**
2129 * @brief Insert a range of characters.
2130 * @param __p Const_iterator referencing location in string to
2131 * insert at.
2132 * @param __beg Start of range.
2133 * @param __end End of range.
2134 * @return Iterator referencing the first inserted char.
2135 * @throw std::length_error If new length exceeds @c max_size().
2136 *
2137 * Inserts characters in range [beg,end). If adding characters
2138 * causes the length to exceed max_size(), length_error is
2139 * thrown. The value of the string doesn't change if an error
2140 * is thrown.
2141 */
2142 template<class _InputIterator,
2143 typename = std::_RequireInputIter<_InputIterator>>
2144 _GLIBCXX20_CONSTEXPR
2145 iterator
2146 insert(const_iterator __p, _InputIterator __beg, _InputIterator __end)
2147 {
2148 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
2149 const size_type __pos = __p - begin();
2150 this->replace(__p, __p, __beg, __end);
2151 return iterator(this->_M_data() + __pos);
2152 }
2153#else
2154 /**
2155 * @brief Insert a range of characters.
2156 * @param __p Iterator referencing location in string to insert at.
2157 * @param __beg Start of range.
2158 * @param __end End of range.
2159 * @throw std::length_error If new length exceeds @c max_size().
2160 *
2161 * Inserts characters in range [__beg,__end). If adding
2162 * characters causes the length to exceed max_size(),
2163 * length_error is thrown. The value of the string doesn't
2164 * change if an error is thrown.
2165 */
2166 template<class _InputIterator>
2167 void
2168 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
2169 { this->replace(__p, __p, __beg, __end); }
2170#endif
2171
2172#if __glibcxx_containers_ranges // C++ >= 23
2173 /**
2174 * @brief Insert a range into the string.
2175 * @param __rg A range of values that are convertible to `value_type`.
2176 * @since C++23
2177 *
2178 * The range `__rg` is allowed to overlap with `*this`.
2179 */
2180 template<__detail::__container_compatible_range<_CharT> _Rg>
2181 constexpr iterator
2182 insert_range(const_iterator __p, _Rg&& __rg)
2183 {
2184 auto __pos = __p - cbegin();
2185
2186 if constexpr (ranges::forward_range<_Rg>)
2187 if (ranges::empty(__rg))
2188 return begin() + __pos;
2189
2190
2191 if (__p == cend())
2192 append_range(std::forward<_Rg>(__rg));
2193 else
2194 {
2195 basic_string __s(from_range, std::forward<_Rg>(__rg),
2196 _M_get_allocator());
2197 insert(__pos, __s);
2198 }
2199 return begin() + __pos;
2200 }
2201#endif
2202
2203#if __cplusplus >= 201103L
2204 /**
2205 * @brief Insert an initializer_list of characters.
2206 * @param __p Iterator referencing location in string to insert at.
2207 * @param __l The initializer_list of characters to insert.
2208 * @throw std::length_error If new length exceeds @c max_size().
2209 */
2210 _GLIBCXX20_CONSTEXPR
2211 iterator
2212 insert(const_iterator __p, initializer_list<_CharT> __l)
2213 { return this->insert(__p, __l.begin(), __l.end()); }
2214
2215#ifdef _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
2216 // See PR libstdc++/83328
2217 void
2219 {
2220 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
2221 this->insert(__p - begin(), __l.begin(), __l.size());
2222 }
2223#endif
2224#endif // C++11
2225
2226 /**
2227 * @brief Insert value of a string.
2228 * @param __pos1 Position in string to insert at.
2229 * @param __str The string to insert.
2230 * @return Reference to this string.
2231 * @throw std::length_error If new length exceeds @c max_size().
2232 *
2233 * Inserts value of @a __str starting at @a __pos1. If adding
2234 * characters causes the length to exceed max_size(),
2235 * length_error is thrown. The value of the string doesn't
2236 * change if an error is thrown.
2237 */
2238 _GLIBCXX20_CONSTEXPR
2240 insert(size_type __pos1, const basic_string& __str)
2241 { return this->replace(__pos1, size_type(0),
2242 __str._M_data(), __str.size()); }
2243
2244 /**
2245 * @brief Insert a substring.
2246 * @param __pos1 Position in string to insert at.
2247 * @param __str The string to insert.
2248 * @param __pos2 Start of characters in str to insert.
2249 * @param __n Number of characters to insert.
2250 * @return Reference to this string.
2251 * @throw std::length_error If new length exceeds @c max_size().
2252 * @throw std::out_of_range If @a pos1 > size() or
2253 * @a __pos2 > @a str.size().
2254 *
2255 * Starting at @a pos1, insert @a __n character of @a __str
2256 * beginning with @a __pos2. If adding characters causes the
2257 * length to exceed max_size(), length_error is thrown. If @a
2258 * __pos1 is beyond the end of this string or @a __pos2 is
2259 * beyond the end of @a __str, out_of_range is thrown. The
2260 * value of the string doesn't change if an error is thrown.
2261 */
2262 _GLIBCXX20_CONSTEXPR
2264 insert(size_type __pos1, const basic_string& __str,
2265 size_type __pos2, size_type __n = npos)
2266 { return this->replace(__pos1, size_type(0), __str._M_data()
2267 + __str._M_check(__pos2, "basic_string::insert"),
2268 __str._M_limit(__pos2, __n)); }
2269
2270 /**
2271 * @brief Insert a C substring.
2272 * @param __pos Position in string to insert at.
2273 * @param __s The C string to insert.
2274 * @param __n The number of characters to insert.
2275 * @return Reference to this string.
2276 * @throw std::length_error If new length exceeds @c max_size().
2277 * @throw std::out_of_range If @a __pos is beyond the end of this
2278 * string.
2279 *
2280 * Inserts the first @a __n characters of @a __s starting at @a
2281 * __pos. If adding characters causes the length to exceed
2282 * max_size(), length_error is thrown. If @a __pos is beyond
2283 * end(), out_of_range is thrown. The value of the string
2284 * doesn't change if an error is thrown.
2285 */
2286 _GLIBCXX20_CONSTEXPR
2288 insert(size_type __pos, const _CharT* __s, size_type __n)
2289 { return this->replace(__pos, size_type(0), __s, __n); }
2290
2291 /**
2292 * @brief Insert a C string.
2293 * @param __pos Position in string to insert at.
2294 * @param __s The C string to insert.
2295 * @return Reference to this string.
2296 * @throw std::length_error If new length exceeds @c max_size().
2297 * @throw std::out_of_range If @a pos is beyond the end of this
2298 * string.
2299 *
2300 * Inserts the first @a n characters of @a __s starting at @a __pos. If
2301 * adding characters causes the length to exceed max_size(),
2302 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
2303 * thrown. The value of the string doesn't change if an error is
2304 * thrown.
2305 */
2306 _GLIBCXX20_CONSTEXPR
2308 insert(size_type __pos, const _CharT* __s)
2309 {
2310 __glibcxx_requires_string(__s);
2311 return this->replace(__pos, size_type(0), __s,
2312 traits_type::length(__s));
2313 }
2314
2315 /**
2316 * @brief Insert multiple characters.
2317 * @param __pos Index in string to insert at.
2318 * @param __n Number of characters to insert
2319 * @param __c The character to insert.
2320 * @return Reference to this string.
2321 * @throw std::length_error If new length exceeds @c max_size().
2322 * @throw std::out_of_range If @a __pos is beyond the end of this
2323 * string.
2324 *
2325 * Inserts @a __n copies of character @a __c starting at index
2326 * @a __pos. If adding characters causes the length to exceed
2327 * max_size(), length_error is thrown. If @a __pos > length(),
2328 * out_of_range is thrown. The value of the string doesn't
2329 * change if an error is thrown.
2330 */
2331 _GLIBCXX20_CONSTEXPR
2333 insert(size_type __pos, size_type __n, _CharT __c)
2334 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
2335 size_type(0), __n, __c); }
2336
2337 /**
2338 * @brief Insert one character.
2339 * @param __p Iterator referencing position in string to insert at.
2340 * @param __c The character to insert.
2341 * @return Iterator referencing newly inserted char.
2342 * @throw std::length_error If new length exceeds @c max_size().
2343 *
2344 * Inserts character @a __c at position referenced by @a __p.
2345 * If adding character causes the length to exceed max_size(),
2346 * length_error is thrown. If @a __p is beyond end of string,
2347 * out_of_range is thrown. The value of the string doesn't
2348 * change if an error is thrown.
2349 */
2350 _GLIBCXX20_CONSTEXPR
2351 iterator
2352 insert(__const_iterator __p, _CharT __c)
2353 {
2354 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
2355 const size_type __pos = __p - begin();
2356 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
2357 return iterator(_M_data() + __pos);
2358 }
2359
2360#ifdef __glibcxx_string_view // >= C++17
2361 /**
2362 * @brief Insert a string_view.
2363 * @param __pos Position in string to insert at.
2364 * @param __svt The object convertible to string_view to insert.
2365 * @return Reference to this string.
2366 */
2367 template<typename _Tp>
2368 _GLIBCXX20_CONSTEXPR
2369 _If_sv<_Tp, basic_string&>
2370 insert(size_type __pos, const _Tp& __svt)
2371 {
2372 __sv_type __sv = __svt;
2373 return this->insert(__pos, __sv.data(), __sv.size());
2374 }
2375
2376 /**
2377 * @brief Insert a string_view.
2378 * @param __pos1 Position in string to insert at.
2379 * @param __svt The object convertible to string_view to insert from.
2380 * @param __pos2 Start of characters in str to insert.
2381 * @param __n The number of characters to insert.
2382 * @return Reference to this string.
2383 */
2384 template<typename _Tp>
2385 _GLIBCXX20_CONSTEXPR
2386 _If_sv<_Tp, basic_string&>
2387 insert(size_type __pos1, const _Tp& __svt,
2388 size_type __pos2, size_type __n = npos)
2389 {
2390 __sv_type __sv = __svt;
2391 return this->replace(__pos1, size_type(0),
2392 __sv.data()
2393 + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"),
2394 std::__sv_limit(__sv.size(), __pos2, __n));
2395 }
2396#endif // C++17
2397
2398 /**
2399 * @brief Remove characters.
2400 * @param __pos Index of first character to remove (default 0).
2401 * @param __n Number of characters to remove (default remainder).
2402 * @return Reference to this string.
2403 * @throw std::out_of_range If @a pos is beyond the end of this
2404 * string.
2405 *
2406 * Removes @a __n characters from this string starting at @a
2407 * __pos. The length of the string is reduced by @a __n. If
2408 * there are < @a __n characters to remove, the remainder of
2409 * the string is truncated. If @a __p is beyond end of string,
2410 * out_of_range is thrown. The value of the string doesn't
2411 * change if an error is thrown.
2412 */
2413 _GLIBCXX20_CONSTEXPR
2415 erase(size_type __pos = 0, size_type __n = npos)
2416 {
2417 _M_check(__pos, "basic_string::erase");
2418 if (__n == npos)
2419 this->_M_set_length(__pos);
2420 else if (__n != 0)
2421 this->_M_erase(__pos, _M_limit(__pos, __n));
2422 return *this;
2423 }
2424
2425 /**
2426 * @brief Remove one character.
2427 * @param __position Iterator referencing the character to remove.
2428 * @return iterator referencing same location after removal.
2429 *
2430 * Removes the character at @a __position from this string. The value
2431 * of the string doesn't change if an error is thrown.
2432 */
2433 _GLIBCXX20_CONSTEXPR
2434 iterator
2435 erase(__const_iterator __position)
2436 {
2437 _GLIBCXX_DEBUG_PEDASSERT(__position >= begin()
2438 && __position < end());
2439 const size_type __pos = __position - begin();
2440 this->_M_erase(__pos, size_type(1));
2441 return iterator(_M_data() + __pos);
2442 }
2443
2444 /**
2445 * @brief Remove a range of characters.
2446 * @param __first Iterator referencing the first character to remove.
2447 * @param __last Iterator referencing the end of the range.
2448 * @return Iterator referencing location of first after removal.
2449 *
2450 * Removes the characters in the range [first,last) from this string.
2451 * The value of the string doesn't change if an error is thrown.
2452 */
2453 _GLIBCXX20_CONSTEXPR
2454 iterator
2455 erase(__const_iterator __first, __const_iterator __last)
2456 {
2457 _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last
2458 && __last <= end());
2459 const size_type __pos = __first - begin();
2460 if (__last == end())
2461 this->_M_set_length(__pos);
2462 else
2463 this->_M_erase(__pos, __last - __first);
2464 return iterator(this->_M_data() + __pos);
2465 }
2466
2467#if __cplusplus >= 201103L
2468 /**
2469 * @brief Remove the last character.
2470 *
2471 * The string must be non-empty.
2472 */
2473 _GLIBCXX20_CONSTEXPR
2474 void
2475 pop_back() noexcept
2476 {
2477 __glibcxx_assert(!empty());
2478 _M_erase(size() - 1, 1);
2479 }
2480#endif // C++11
2481
2482 /**
2483 * @brief Replace characters with value from another string.
2484 * @param __pos Index of first character to replace.
2485 * @param __n Number of characters to be replaced.
2486 * @param __str String to insert.
2487 * @return Reference to this string.
2488 * @throw std::out_of_range If @a pos is beyond the end of this
2489 * string.
2490 * @throw std::length_error If new length exceeds @c max_size().
2491 *
2492 * Removes the characters in the range [__pos,__pos+__n) from
2493 * this string. In place, the value of @a __str is inserted.
2494 * If @a __pos is beyond end of string, out_of_range is thrown.
2495 * If the length of the result exceeds max_size(), length_error
2496 * is thrown. The value of the string doesn't change if an
2497 * error is thrown.
2498 */
2499 _GLIBCXX20_CONSTEXPR
2501 replace(size_type __pos, size_type __n, const basic_string& __str)
2502 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
2503
2504 /**
2505 * @brief Replace characters with value from another string.
2506 * @param __pos1 Index of first character to replace.
2507 * @param __n1 Number of characters to be replaced.
2508 * @param __str String to insert.
2509 * @param __pos2 Index of first character of str to use.
2510 * @param __n2 Number of characters from str to use.
2511 * @return Reference to this string.
2512 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
2513 * __str.size().
2514 * @throw std::length_error If new length exceeds @c max_size().
2515 *
2516 * Removes the characters in the range [__pos1,__pos1 + n) from this
2517 * string. In place, the value of @a __str is inserted. If @a __pos is
2518 * beyond end of string, out_of_range is thrown. If the length of the
2519 * result exceeds max_size(), length_error is thrown. The value of the
2520 * string doesn't change if an error is thrown.
2521 */
2522 _GLIBCXX20_CONSTEXPR
2524 replace(size_type __pos1, size_type __n1, const basic_string& __str,
2525 size_type __pos2, size_type __n2 = npos)
2526 { return this->replace(__pos1, __n1, __str._M_data()
2527 + __str._M_check(__pos2, "basic_string::replace"),
2528 __str._M_limit(__pos2, __n2)); }
2529
2530 /**
2531 * @brief Replace characters with value of a C substring.
2532 * @param __pos Index of first character to replace.
2533 * @param __n1 Number of characters to be replaced.
2534 * @param __s C string to insert.
2535 * @param __n2 Number of characters from @a s to use.
2536 * @return Reference to this string.
2537 * @throw std::out_of_range If @a pos1 > size().
2538 * @throw std::length_error If new length exceeds @c max_size().
2539 *
2540 * Removes the characters in the range [__pos,__pos + __n1)
2541 * from this string. In place, the first @a __n2 characters of
2542 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
2543 * @a __pos is beyond end of string, out_of_range is thrown. If
2544 * the length of result exceeds max_size(), length_error is
2545 * thrown. The value of the string doesn't change if an error
2546 * is thrown.
2547 */
2548 _GLIBCXX20_CONSTEXPR
2550 replace(size_type __pos, size_type __n1, const _CharT* __s,
2551 size_type __n2)
2552 {
2553 __glibcxx_requires_string_len(__s, __n2);
2554 return _M_replace(_M_check(__pos, "basic_string::replace"),
2555 _M_limit(__pos, __n1), __s, __n2);
2556 }
2557
2558 /**
2559 * @brief Replace characters with value of a C string.
2560 * @param __pos Index of first character to replace.
2561 * @param __n1 Number of characters to be replaced.
2562 * @param __s C string to insert.
2563 * @return Reference to this string.
2564 * @throw std::out_of_range If @a pos > size().
2565 * @throw std::length_error If new length exceeds @c max_size().
2566 *
2567 * Removes the characters in the range [__pos,__pos + __n1)
2568 * from this string. In place, the characters of @a __s are
2569 * inserted. If @a __pos is beyond end of string, out_of_range
2570 * is thrown. If the length of result exceeds max_size(),
2571 * length_error is thrown. The value of the string doesn't
2572 * change if an error is thrown.
2573 */
2574 _GLIBCXX20_CONSTEXPR
2576 replace(size_type __pos, size_type __n1, const _CharT* __s)
2577 {
2578 __glibcxx_requires_string(__s);
2579 return this->replace(__pos, __n1, __s, traits_type::length(__s));
2580 }
2581
2582 /**
2583 * @brief Replace characters with multiple characters.
2584 * @param __pos Index of first character to replace.
2585 * @param __n1 Number of characters to be replaced.
2586 * @param __n2 Number of characters to insert.
2587 * @param __c Character to insert.
2588 * @return Reference to this string.
2589 * @throw std::out_of_range If @a __pos > size().
2590 * @throw std::length_error If new length exceeds @c max_size().
2591 *
2592 * Removes the characters in the range [pos,pos + n1) from this
2593 * string. In place, @a __n2 copies of @a __c are inserted.
2594 * If @a __pos is beyond end of string, out_of_range is thrown.
2595 * If the length of result exceeds max_size(), length_error is
2596 * thrown. The value of the string doesn't change if an error
2597 * is thrown.
2598 */
2599 _GLIBCXX20_CONSTEXPR
2601 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
2602 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
2603 _M_limit(__pos, __n1), __n2, __c); }
2604
2605 /**
2606 * @brief Replace range of characters with string.
2607 * @param __i1 Iterator referencing start of range to replace.
2608 * @param __i2 Iterator referencing end of range to replace.
2609 * @param __str String value to insert.
2610 * @return Reference to this string.
2611 * @throw std::length_error If new length exceeds @c max_size().
2612 *
2613 * Removes the characters in the range [__i1,__i2). In place,
2614 * the value of @a __str is inserted. If the length of result
2615 * exceeds max_size(), length_error is thrown. The value of
2616 * the string doesn't change if an error is thrown.
2617 */
2618 _GLIBCXX20_CONSTEXPR
2620 replace(__const_iterator __i1, __const_iterator __i2,
2621 const basic_string& __str)
2622 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
2623
2624 /**
2625 * @brief Replace range of characters with C substring.
2626 * @param __i1 Iterator referencing start of range to replace.
2627 * @param __i2 Iterator referencing end of range to replace.
2628 * @param __s C string value to insert.
2629 * @param __n Number of characters from s to insert.
2630 * @return Reference to this string.
2631 * @throw std::length_error If new length exceeds @c max_size().
2632 *
2633 * Removes the characters in the range [__i1,__i2). In place,
2634 * the first @a __n characters of @a __s are inserted. If the
2635 * length of result exceeds max_size(), length_error is thrown.
2636 * The value of the string doesn't change if an error is
2637 * thrown.
2638 */
2639 _GLIBCXX20_CONSTEXPR
2641 replace(__const_iterator __i1, __const_iterator __i2,
2642 const _CharT* __s, size_type __n)
2643 {
2644 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2645 && __i2 <= end());
2646 return this->replace(__i1 - begin(), __i2 - __i1, __s, __n);
2647 }
2648
2649 /**
2650 * @brief Replace range of characters with C string.
2651 * @param __i1 Iterator referencing start of range to replace.
2652 * @param __i2 Iterator referencing end of range to replace.
2653 * @param __s C string value to insert.
2654 * @return Reference to this string.
2655 * @throw std::length_error If new length exceeds @c max_size().
2656 *
2657 * Removes the characters in the range [__i1,__i2). In place,
2658 * the characters of @a __s are inserted. If the length of
2659 * result exceeds max_size(), length_error is thrown. The
2660 * value of the string doesn't change if an error is thrown.
2661 */
2662 _GLIBCXX20_CONSTEXPR
2664 replace(__const_iterator __i1, __const_iterator __i2, const _CharT* __s)
2665 {
2666 __glibcxx_requires_string(__s);
2667 return this->replace(__i1, __i2, __s, traits_type::length(__s));
2668 }
2669
2670 /**
2671 * @brief Replace range of characters with multiple characters
2672 * @param __i1 Iterator referencing start of range to replace.
2673 * @param __i2 Iterator referencing end of range to replace.
2674 * @param __n Number of characters to insert.
2675 * @param __c Character to insert.
2676 * @return Reference to this string.
2677 * @throw std::length_error If new length exceeds @c max_size().
2678 *
2679 * Removes the characters in the range [__i1,__i2). In place,
2680 * @a __n copies of @a __c are inserted. If the length of
2681 * result exceeds max_size(), length_error is thrown. The
2682 * value of the string doesn't change if an error is thrown.
2683 */
2684 _GLIBCXX20_CONSTEXPR
2686 replace(__const_iterator __i1, __const_iterator __i2, size_type __n,
2687 _CharT __c)
2688 {
2689 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2690 && __i2 <= end());
2691 return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __c);
2692 }
2693
2694 /**
2695 * @brief Replace range of characters with range.
2696 * @param __i1 Iterator referencing start of range to replace.
2697 * @param __i2 Iterator referencing end of range to replace.
2698 * @param __k1 Iterator referencing start of range to insert.
2699 * @param __k2 Iterator referencing end of range to insert.
2700 * @return Reference to this string.
2701 * @throw std::length_error If new length exceeds @c max_size().
2702 *
2703 * Removes the characters in the range [__i1,__i2). In place,
2704 * characters in the range [__k1,__k2) are inserted. If the
2705 * length of result exceeds max_size(), length_error is thrown.
2706 * The value of the string doesn't change if an error is
2707 * thrown.
2708 */
2709#if __cplusplus >= 201103L
2710 template<class _InputIterator,
2711 typename = std::_RequireInputIter<_InputIterator>>
2712 _GLIBCXX20_CONSTEXPR
2714 replace(const_iterator __i1, const_iterator __i2,
2715 _InputIterator __k1, _InputIterator __k2)
2716 {
2717 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2718 && __i2 <= end());
2719 __glibcxx_requires_valid_range(__k1, __k2);
2720 return this->_M_replace_dispatch(__i1, __i2, __k1, __k2,
2721 std::__false_type());
2722 }
2723#else
2724 template<class _InputIterator>
2725#ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
2726 typename __enable_if_not_native_iterator<_InputIterator>::__type
2727#else
2729#endif
2730 replace(iterator __i1, iterator __i2,
2731 _InputIterator __k1, _InputIterator __k2)
2732 {
2733 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2734 && __i2 <= end());
2735 __glibcxx_requires_valid_range(__k1, __k2);
2736 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
2737 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
2738 }
2739#endif
2740
2741 // Specializations for the common case of pointer and iterator:
2742 // useful to avoid the overhead of temporary buffering in _M_replace.
2743 _GLIBCXX20_CONSTEXPR
2745 replace(__const_iterator __i1, __const_iterator __i2,
2746 _CharT* __k1, _CharT* __k2)
2747 {
2748 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2749 && __i2 <= end());
2750 __glibcxx_requires_valid_range(__k1, __k2);
2751 return this->replace(__i1 - begin(), __i2 - __i1,
2752 __k1, __k2 - __k1);
2753 }
2754
2755 _GLIBCXX20_CONSTEXPR
2757 replace(__const_iterator __i1, __const_iterator __i2,
2758 const _CharT* __k1, const _CharT* __k2)
2759 {
2760 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2761 && __i2 <= end());
2762 __glibcxx_requires_valid_range(__k1, __k2);
2763 return this->replace(__i1 - begin(), __i2 - __i1,
2764 __k1, __k2 - __k1);
2765 }
2766
2767 _GLIBCXX20_CONSTEXPR
2769 replace(__const_iterator __i1, __const_iterator __i2,
2770 iterator __k1, iterator __k2)
2771 {
2772 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2773 && __i2 <= end());
2774 __glibcxx_requires_valid_range(__k1, __k2);
2775 return this->replace(__i1 - begin(), __i2 - __i1,
2776 __k1.base(), __k2 - __k1);
2777 }
2778
2779 _GLIBCXX20_CONSTEXPR
2781 replace(__const_iterator __i1, __const_iterator __i2,
2782 const_iterator __k1, const_iterator __k2)
2783 {
2784 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2785 && __i2 <= end());
2786 __glibcxx_requires_valid_range(__k1, __k2);
2787 return this->replace(__i1 - begin(), __i2 - __i1,
2788 __k1.base(), __k2 - __k1);
2789 }
2790
2791#if __glibcxx_containers_ranges // C++ >= 23
2792 /**
2793 * @brief Replace part of the string with a range.
2794 * @param __rg A range of values that are convertible to `value_type`.
2795 * @since C++23
2796 *
2797 * The range `__rg` is allowed to overlap with `*this`.
2798 */
2799 template<__detail::__container_compatible_range<_CharT> _Rg>
2800 constexpr basic_string&
2801 replace_with_range(const_iterator __i1, const_iterator __i2, _Rg&& __rg)
2802 {
2803 if (__i1 == cend())
2804 append_range(std::forward<_Rg>(__rg));
2805 else
2806 {
2807 basic_string __s(from_range, std::forward<_Rg>(__rg),
2808 _M_get_allocator());
2809 replace(__i1, __i2, __s);
2810 }
2811 return *this;
2812 }
2813#endif
2814
2815#if __cplusplus >= 201103L
2816 /**
2817 * @brief Replace range of characters with initializer_list.
2818 * @param __i1 Iterator referencing start of range to replace.
2819 * @param __i2 Iterator referencing end of range to replace.
2820 * @param __l The initializer_list of characters to insert.
2821 * @return Reference to this string.
2822 * @throw std::length_error If new length exceeds @c max_size().
2823 *
2824 * Removes the characters in the range [__i1,__i2). In place,
2825 * characters in the range [__k1,__k2) are inserted. If the
2826 * length of result exceeds max_size(), length_error is thrown.
2827 * The value of the string doesn't change if an error is
2828 * thrown.
2829 */
2830 _GLIBCXX20_CONSTEXPR
2831 basic_string& replace(const_iterator __i1, const_iterator __i2,
2833 { return this->replace(__i1, __i2, __l.begin(), __l.size()); }
2834#endif // C++11
2835
2836#ifdef __glibcxx_string_view // >= C++17
2837 /**
2838 * @brief Replace range of characters with string_view.
2839 * @param __pos The position to replace at.
2840 * @param __n The number of characters to replace.
2841 * @param __svt The object convertible to string_view to insert.
2842 * @return Reference to this string.
2843 */
2844 template<typename _Tp>
2845 _GLIBCXX20_CONSTEXPR
2846 _If_sv<_Tp, basic_string&>
2847 replace(size_type __pos, size_type __n, const _Tp& __svt)
2848 {
2849 __sv_type __sv = __svt;
2850 return this->replace(__pos, __n, __sv.data(), __sv.size());
2851 }
2852
2853 /**
2854 * @brief Replace range of characters with string_view.
2855 * @param __pos1 The position to replace at.
2856 * @param __n1 The number of characters to replace.
2857 * @param __svt The object convertible to string_view to insert from.
2858 * @param __pos2 The position in the string_view to insert from.
2859 * @param __n2 The number of characters to insert.
2860 * @return Reference to this string.
2861 */
2862 template<typename _Tp>
2863 _GLIBCXX20_CONSTEXPR
2864 _If_sv<_Tp, basic_string&>
2865 replace(size_type __pos1, size_type __n1, const _Tp& __svt,
2866 size_type __pos2, size_type __n2 = npos)
2867 {
2868 __sv_type __sv = __svt;
2869 return this->replace(__pos1, __n1,
2870 __sv.data()
2871 + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"),
2872 std::__sv_limit(__sv.size(), __pos2, __n2));
2873 }
2874
2875 /**
2876 * @brief Replace range of characters with string_view.
2877 * @param __i1 An iterator referencing the start position
2878 to replace at.
2879 * @param __i2 An iterator referencing the end position
2880 for the replace.
2881 * @param __svt The object convertible to string_view to insert from.
2882 * @return Reference to this string.
2883 */
2884 template<typename _Tp>
2885 _GLIBCXX20_CONSTEXPR
2886 _If_sv<_Tp, basic_string&>
2887 replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
2888 {
2889 __sv_type __sv = __svt;
2890 return this->replace(__i1 - begin(), __i2 - __i1, __sv);
2891 }
2892#endif // C++17
2893
2894 private:
2895 template<class _Integer>
2896 _GLIBCXX20_CONSTEXPR
2898 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
2899 _Integer __n, _Integer __val, __true_type)
2900 { return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __val); }
2901
2902 template<class _InputIterator>
2903 _GLIBCXX20_CONSTEXPR
2905 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
2906 _InputIterator __k1, _InputIterator __k2,
2907 __false_type);
2908
2909 _GLIBCXX20_CONSTEXPR
2911 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
2912 _CharT __c);
2913
2914 __attribute__((__noinline__, __noclone__, __cold__)) void
2915 _M_replace_cold(pointer __p, size_type __len1, const _CharT* __s,
2916 const size_type __len2, const size_type __how_much);
2917
2918 _GLIBCXX20_CONSTEXPR
2920 _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
2921 const size_type __len2);
2922
2923 _GLIBCXX20_CONSTEXPR
2925 _M_append(const _CharT* __s, size_type __n);
2926
2927 public:
2928
2929 /**
2930 * @brief Copy substring into C string.
2931 * @param __s C string to copy value into.
2932 * @param __n Number of characters to copy.
2933 * @param __pos Index of first character to copy.
2934 * @return Number of characters actually copied
2935 * @throw std::out_of_range If __pos > size().
2936 *
2937 * Copies up to @a __n characters starting at @a __pos into the
2938 * C string @a __s. If @a __pos is %greater than size(),
2939 * out_of_range is thrown.
2940 */
2941 _GLIBCXX20_CONSTEXPR
2942 size_type
2943 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
2944
2945 /**
2946 * @brief Swap contents with another string.
2947 * @param __s String to swap with.
2948 *
2949 * Exchanges the contents of this string with that of @a __s in constant
2950 * time.
2951 */
2952 _GLIBCXX20_CONSTEXPR
2953 void
2954 swap(basic_string& __s) _GLIBCXX_NOEXCEPT;
2955
2956 // String operations:
2957 /**
2958 * @brief Return const pointer to null-terminated contents.
2959 *
2960 * This is a handle to internal data. Do not modify or dire things may
2961 * happen.
2962 */
2963 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2964 const _CharT*
2965 c_str() const _GLIBCXX_NOEXCEPT
2966 { return _M_data(); }
2967
2968 /**
2969 * @brief Return const pointer to contents.
2970 *
2971 * This is a pointer to internal data. It is undefined to modify
2972 * the contents through the returned pointer. To get a pointer that
2973 * allows modifying the contents use @c &str[0] instead,
2974 * (or in C++17 the non-const @c str.data() overload).
2975 */
2976 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2977 const _CharT*
2978 data() const _GLIBCXX_NOEXCEPT
2979 { return _M_data(); }
2980
2981#if __cplusplus >= 201703L
2982 /**
2983 * @brief Return non-const pointer to contents.
2984 *
2985 * This is a pointer to the character sequence held by the string.
2986 * Modifying the characters in the sequence is allowed.
2987 */
2988 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2989 _CharT*
2990 data() noexcept
2991 { return _M_data(); }
2992#endif
2993
2994 /**
2995 * @brief Return copy of allocator used to construct this string.
2996 */
2997 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
2998 allocator_type
2999 get_allocator() const _GLIBCXX_NOEXCEPT
3000 { return _M_get_allocator(); }
3001
3002 /**
3003 * @brief Find position of a C substring.
3004 * @param __s C string to locate.
3005 * @param __pos Index of character to search from.
3006 * @param __n Number of characters from @a s to search for.
3007 * @return Index of start of first occurrence.
3008 *
3009 * Starting from @a __pos, searches forward for the first @a
3010 * __n characters in @a __s within this string. If found,
3011 * returns the index where it begins. If not found, returns
3012 * npos.
3013 */
3014 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3015 size_type
3016 find(const _CharT* __s, size_type __pos, size_type __n) const
3017 _GLIBCXX_NOEXCEPT;
3018
3019 /**
3020 * @brief Find position of a string.
3021 * @param __str String to locate.
3022 * @param __pos Index of character to search from (default 0).
3023 * @return Index of start of first occurrence.
3024 *
3025 * Starting from @a __pos, searches forward for value of @a __str within
3026 * this string. If found, returns the index where it begins. If not
3027 * found, returns npos.
3028 */
3029 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3030 size_type
3031 find(const basic_string& __str, size_type __pos = 0) const
3032 _GLIBCXX_NOEXCEPT
3033 { return this->find(__str.data(), __pos, __str.size()); }
3034
3035#ifdef __glibcxx_string_view // >= C++17
3036 /**
3037 * @brief Find position of a string_view.
3038 * @param __svt The object convertible to string_view to locate.
3039 * @param __pos Index of character to search from (default 0).
3040 * @return Index of start of first occurrence.
3041 */
3042 template<typename _Tp>
3043 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3044 _If_sv<_Tp, size_type>
3045 find(const _Tp& __svt, size_type __pos = 0) const
3046 noexcept(is_same<_Tp, __sv_type>::value)
3047 {
3048 __sv_type __sv = __svt;
3049 return this->find(__sv.data(), __pos, __sv.size());
3050 }
3051#endif // C++17
3052
3053 /**
3054 * @brief Find position of a C string.
3055 * @param __s C string to locate.
3056 * @param __pos Index of character to search from (default 0).
3057 * @return Index of start of first occurrence.
3058 *
3059 * Starting from @a __pos, searches forward for the value of @a
3060 * __s within this string. If found, returns the index where
3061 * it begins. If not found, returns npos.
3062 */
3063 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3064 size_type
3065 find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
3066 {
3067 __glibcxx_requires_string(__s);
3068 return this->find(__s, __pos, traits_type::length(__s));
3069 }
3070
3071 /**
3072 * @brief Find position of a character.
3073 * @param __c Character to locate.
3074 * @param __pos Index of character to search from (default 0).
3075 * @return Index of first occurrence.
3076 *
3077 * Starting from @a __pos, searches forward for @a __c within
3078 * this string. If found, returns the index where it was
3079 * found. If not found, returns npos.
3080 */
3081 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3082 size_type
3083 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
3084
3085 /**
3086 * @brief Find last position of a string.
3087 * @param __str String to locate.
3088 * @param __pos Index of character to search back from (default end).
3089 * @return Index of start of last occurrence.
3090 *
3091 * Starting from @a __pos, searches backward for value of @a
3092 * __str within this string. If found, returns the index where
3093 * it begins. If not found, returns npos.
3094 */
3095 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3096 size_type
3097 rfind(const basic_string& __str, size_type __pos = npos) const
3098 _GLIBCXX_NOEXCEPT
3099 { return this->rfind(__str.data(), __pos, __str.size()); }
3100
3101#ifdef __glibcxx_string_view // >= C++17
3102 /**
3103 * @brief Find last position of a string_view.
3104 * @param __svt The object convertible to string_view to locate.
3105 * @param __pos Index of character to search back from (default end).
3106 * @return Index of start of last occurrence.
3107 */
3108 template<typename _Tp>
3109 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3110 _If_sv<_Tp, size_type>
3111 rfind(const _Tp& __svt, size_type __pos = npos) const
3113 {
3114 __sv_type __sv = __svt;
3115 return this->rfind(__sv.data(), __pos, __sv.size());
3116 }
3117#endif // C++17
3118
3119 /**
3120 * @brief Find last position of a C substring.
3121 * @param __s C string to locate.
3122 * @param __pos Index of character to search back from.
3123 * @param __n Number of characters from s to search for.
3124 * @return Index of start of last occurrence.
3125 *
3126 * Starting from @a __pos, searches backward for the first @a
3127 * __n characters in @a __s within this string. If found,
3128 * returns the index where it begins. If not found, returns
3129 * npos.
3130 */
3131 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3132 size_type
3133 rfind(const _CharT* __s, size_type __pos, size_type __n) const
3134 _GLIBCXX_NOEXCEPT;
3135
3136 /**
3137 * @brief Find last position of a C string.
3138 * @param __s C string to locate.
3139 * @param __pos Index of character to start search at (default end).
3140 * @return Index of start of last occurrence.
3141 *
3142 * Starting from @a __pos, searches backward for the value of
3143 * @a __s within this string. If found, returns the index
3144 * where it begins. If not found, returns npos.
3145 */
3146 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3147 size_type
3148 rfind(const _CharT* __s, size_type __pos = npos) const
3149 {
3150 __glibcxx_requires_string(__s);
3151 return this->rfind(__s, __pos, traits_type::length(__s));
3152 }
3153
3154 /**
3155 * @brief Find last position of a character.
3156 * @param __c Character to locate.
3157 * @param __pos Index of character to search back from (default end).
3158 * @return Index of last occurrence.
3159 *
3160 * Starting from @a __pos, searches backward for @a __c within
3161 * this string. If found, returns the index where it was
3162 * found. If not found, returns npos.
3163 */
3164 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3165 size_type
3166 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
3167
3168 /**
3169 * @brief Find position of a character of string.
3170 * @param __str String containing characters to locate.
3171 * @param __pos Index of character to search from (default 0).
3172 * @return Index of first occurrence.
3173 *
3174 * Starting from @a __pos, searches forward for one of the
3175 * characters of @a __str within this string. If found,
3176 * returns the index where it was found. If not found, returns
3177 * npos.
3178 */
3179 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3180 size_type
3181 find_first_of(const basic_string& __str, size_type __pos = 0) const
3182 _GLIBCXX_NOEXCEPT
3183 { return this->find_first_of(__str.data(), __pos, __str.size()); }
3184
3185#ifdef __glibcxx_string_view // >= C++17
3186 /**
3187 * @brief Find position of a character of a string_view.
3188 * @param __svt An object convertible to string_view containing
3189 * characters to locate.
3190 * @param __pos Index of character to search from (default 0).
3191 * @return Index of first occurrence.
3192 */
3193 template<typename _Tp>
3194 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3195 _If_sv<_Tp, size_type>
3196 find_first_of(const _Tp& __svt, size_type __pos = 0) const
3197 noexcept(is_same<_Tp, __sv_type>::value)
3198 {
3199 __sv_type __sv = __svt;
3200 return this->find_first_of(__sv.data(), __pos, __sv.size());
3201 }
3202#endif // C++17
3203
3204 /**
3205 * @brief Find position of a character of C substring.
3206 * @param __s String containing characters to locate.
3207 * @param __pos Index of character to search from.
3208 * @param __n Number of characters from s to search for.
3209 * @return Index of first occurrence.
3210 *
3211 * Starting from @a __pos, searches forward for one of the
3212 * first @a __n characters of @a __s within this string. If
3213 * found, returns the index where it was found. If not found,
3214 * returns npos.
3215 */
3216 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3217 size_type
3218 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
3219 _GLIBCXX_NOEXCEPT;
3220
3221 /**
3222 * @brief Find position of a character of C string.
3223 * @param __s String containing characters to locate.
3224 * @param __pos Index of character to search from (default 0).
3225 * @return Index of first occurrence.
3226 *
3227 * Starting from @a __pos, searches forward for one of the
3228 * characters of @a __s within this string. If found, returns
3229 * the index where it was found. If not found, returns npos.
3230 */
3231 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3232 size_type
3233 find_first_of(const _CharT* __s, size_type __pos = 0) const
3234 _GLIBCXX_NOEXCEPT
3235 {
3236 __glibcxx_requires_string(__s);
3237 return this->find_first_of(__s, __pos, traits_type::length(__s));
3238 }
3239
3240 /**
3241 * @brief Find position of a character.
3242 * @param __c Character to locate.
3243 * @param __pos Index of character to search from (default 0).
3244 * @return Index of first occurrence.
3245 *
3246 * Starting from @a __pos, searches forward for the character
3247 * @a __c within this string. If found, returns the index
3248 * where it was found. If not found, returns npos.
3249 *
3250 * Note: equivalent to find(__c, __pos).
3251 */
3252 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3253 size_type
3254 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
3255 { return this->find(__c, __pos); }
3256
3257 /**
3258 * @brief Find last position of a character of string.
3259 * @param __str String containing characters to locate.
3260 * @param __pos Index of character to search back from (default end).
3261 * @return Index of last occurrence.
3262 *
3263 * Starting from @a __pos, searches backward for one of the
3264 * characters of @a __str within this string. If found,
3265 * returns the index where it was found. If not found, returns
3266 * npos.
3267 */
3268 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3269 size_type
3270 find_last_of(const basic_string& __str, size_type __pos = npos) const
3271 _GLIBCXX_NOEXCEPT
3272 { return this->find_last_of(__str.data(), __pos, __str.size()); }
3273
3274#ifdef __glibcxx_string_view // >= C++17
3275 /**
3276 * @brief Find last position of a character of string.
3277 * @param __svt An object convertible to string_view containing
3278 * characters to locate.
3279 * @param __pos Index of character to search back from (default end).
3280 * @return Index of last occurrence.
3281 */
3282 template<typename _Tp>
3283 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3284 _If_sv<_Tp, size_type>
3285 find_last_of(const _Tp& __svt, size_type __pos = npos) const
3287 {
3288 __sv_type __sv = __svt;
3289 return this->find_last_of(__sv.data(), __pos, __sv.size());
3290 }
3291#endif // C++17
3292
3293 /**
3294 * @brief Find last position of a character of C substring.
3295 * @param __s C string containing characters to locate.
3296 * @param __pos Index of character to search back from.
3297 * @param __n Number of characters from s to search for.
3298 * @return Index of last occurrence.
3299 *
3300 * Starting from @a __pos, searches backward for one of the
3301 * first @a __n characters of @a __s within this string. If
3302 * found, returns the index where it was found. If not found,
3303 * returns npos.
3304 */
3305 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3306 size_type
3307 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
3308 _GLIBCXX_NOEXCEPT;
3309
3310 /**
3311 * @brief Find last position of a character of C string.
3312 * @param __s C string containing characters to locate.
3313 * @param __pos Index of character to search back from (default end).
3314 * @return Index of last occurrence.
3315 *
3316 * Starting from @a __pos, searches backward for one of the
3317 * characters of @a __s within this string. If found, returns
3318 * the index where it was found. If not found, returns npos.
3319 */
3320 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3321 size_type
3322 find_last_of(const _CharT* __s, size_type __pos = npos) const
3323 _GLIBCXX_NOEXCEPT
3324 {
3325 __glibcxx_requires_string(__s);
3326 return this->find_last_of(__s, __pos, traits_type::length(__s));
3327 }
3328
3329 /**
3330 * @brief Find last position of a character.
3331 * @param __c Character to locate.
3332 * @param __pos Index of character to search back from (default end).
3333 * @return Index of last occurrence.
3334 *
3335 * Starting from @a __pos, searches backward for @a __c within
3336 * this string. If found, returns the index where it was
3337 * found. If not found, returns npos.
3338 *
3339 * Note: equivalent to rfind(__c, __pos).
3340 */
3341 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3342 size_type
3343 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
3344 { return this->rfind(__c, __pos); }
3345
3346 /**
3347 * @brief Find position of a character not in string.
3348 * @param __str String containing characters to avoid.
3349 * @param __pos Index of character to search from (default 0).
3350 * @return Index of first occurrence.
3351 *
3352 * Starting from @a __pos, searches forward for a character not contained
3353 * in @a __str within this string. If found, returns the index where it
3354 * was found. If not found, returns npos.
3355 */
3356 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3357 size_type
3358 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
3359 _GLIBCXX_NOEXCEPT
3360 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
3361
3362#ifdef __glibcxx_string_view // >= C++17
3363 /**
3364 * @brief Find position of a character not in a string_view.
3365 * @param __svt A object convertible to string_view containing
3366 * characters to avoid.
3367 * @param __pos Index of character to search from (default 0).
3368 * @return Index of first occurrence.
3369 */
3370 template<typename _Tp>
3371 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3372 _If_sv<_Tp, size_type>
3373 find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
3374 noexcept(is_same<_Tp, __sv_type>::value)
3375 {
3376 __sv_type __sv = __svt;
3377 return this->find_first_not_of(__sv.data(), __pos, __sv.size());
3378 }
3379#endif // C++17
3380
3381 /**
3382 * @brief Find position of a character not in C substring.
3383 * @param __s C string containing characters to avoid.
3384 * @param __pos Index of character to search from.
3385 * @param __n Number of characters from __s to consider.
3386 * @return Index of first occurrence.
3387 *
3388 * Starting from @a __pos, searches forward for a character not
3389 * contained in the first @a __n characters of @a __s within
3390 * this string. If found, returns the index where it was
3391 * found. If not found, returns npos.
3392 */
3393 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3394 size_type
3395 find_first_not_of(const _CharT* __s, size_type __pos,
3396 size_type __n) const _GLIBCXX_NOEXCEPT;
3397
3398 /**
3399 * @brief Find position of a character not in C string.
3400 * @param __s C string containing characters to avoid.
3401 * @param __pos Index of character to search from (default 0).
3402 * @return Index of first occurrence.
3403 *
3404 * Starting from @a __pos, searches forward for a character not
3405 * contained in @a __s within this string. If found, returns
3406 * the index where it was found. If not found, returns npos.
3407 */
3408 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3409 size_type
3410 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
3411 _GLIBCXX_NOEXCEPT
3412 {
3413 __glibcxx_requires_string(__s);
3414 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
3415 }
3416
3417 /**
3418 * @brief Find position of a different character.
3419 * @param __c Character to avoid.
3420 * @param __pos Index of character to search from (default 0).
3421 * @return Index of first occurrence.
3422 *
3423 * Starting from @a __pos, searches forward for a character
3424 * other than @a __c within this string. If found, returns the
3425 * index where it was found. If not found, returns npos.
3426 */
3427 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3428 size_type
3429 find_first_not_of(_CharT __c, size_type __pos = 0) const
3430 _GLIBCXX_NOEXCEPT;
3431
3432 /**
3433 * @brief Find last position of a character not in string.
3434 * @param __str String containing characters to avoid.
3435 * @param __pos Index of character to search back from (default end).
3436 * @return Index of last occurrence.
3437 *
3438 * Starting from @a __pos, searches backward for a character
3439 * not contained in @a __str within this string. If found,
3440 * returns the index where it was found. If not found, returns
3441 * npos.
3442 */
3443 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3444 size_type
3445 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
3446 _GLIBCXX_NOEXCEPT
3447 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
3448
3449#ifdef __glibcxx_string_view // >= C++17
3450 /**
3451 * @brief Find last position of a character not in a string_view.
3452 * @param __svt An object convertible to string_view containing
3453 * characters to avoid.
3454 * @param __pos Index of character to search back from (default end).
3455 * @return Index of last occurrence.
3456 */
3457 template<typename _Tp>
3458 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3459 _If_sv<_Tp, size_type>
3460 find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
3462 {
3463 __sv_type __sv = __svt;
3464 return this->find_last_not_of(__sv.data(), __pos, __sv.size());
3465 }
3466#endif // C++17
3467
3468 /**
3469 * @brief Find last position of a character not in C substring.
3470 * @param __s C string containing characters to avoid.
3471 * @param __pos Index of character to search back from.
3472 * @param __n Number of characters from s to consider.
3473 * @return Index of last occurrence.
3474 *
3475 * Starting from @a __pos, searches backward for a character not
3476 * contained in the first @a __n characters of @a __s within this string.
3477 * If found, returns the index where it was found. If not found,
3478 * returns npos.
3479 */
3480 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3481 size_type
3482 find_last_not_of(const _CharT* __s, size_type __pos,
3483 size_type __n) const _GLIBCXX_NOEXCEPT;
3484 /**
3485 * @brief Find last position of a character not in C string.
3486 * @param __s C string containing characters to avoid.
3487 * @param __pos Index of character to search back from (default end).
3488 * @return Index of last occurrence.
3489 *
3490 * Starting from @a __pos, searches backward for a character
3491 * not contained in @a __s within this string. If found,
3492 * returns the index where it was found. If not found, returns
3493 * npos.
3494 */
3495 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3496 size_type
3497 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
3498 _GLIBCXX_NOEXCEPT
3499 {
3500 __glibcxx_requires_string(__s);
3501 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
3502 }
3503
3504 /**
3505 * @brief Find last position of a different character.
3506 * @param __c Character to avoid.
3507 * @param __pos Index of character to search back from (default end).
3508 * @return Index of last occurrence.
3509 *
3510 * Starting from @a __pos, searches backward for a character other than
3511 * @a __c within this string. If found, returns the index where it was
3512 * found. If not found, returns npos.
3513 */
3514 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3515 size_type
3516 find_last_not_of(_CharT __c, size_type __pos = npos) const
3517 _GLIBCXX_NOEXCEPT;
3518
3519 /**
3520 * @brief Get a substring.
3521 * @param __pos Index of first character (default 0).
3522 * @param __n Number of characters in substring (default remainder).
3523 * @return The new string.
3524 * @throw std::out_of_range If __pos > size().
3525 *
3526 * Construct and return a new string using the @a __n
3527 * characters starting at @a __pos. If the string is too
3528 * short, use the remainder of the characters. If @a __pos is
3529 * beyond the end of the string, out_of_range is thrown.
3530 */
3531 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3533 substr(size_type __pos = 0, size_type __n = npos) const
3534 { return basic_string(*this,
3535 _M_check(__pos, "basic_string::substr"), __n); }
3536
3537#if __cplusplus >= 202302L
3538 _GLIBCXX_NODISCARD
3539 constexpr basic_string
3540 substr(size_type __pos = 0) &&
3541 { return basic_string(std::move(*this), __pos); }
3542
3543 _GLIBCXX_NODISCARD
3544 constexpr basic_string
3545 substr(size_type __pos, size_type __n) &&
3546 { return basic_string(std::move(*this), __pos, __n); }
3547#endif // C++23
3548
3549#ifdef __glibcxx_string_subview // >= C++26
3550 /**
3551 * @brief Get a subview.
3552 * @param __pos Index of first character (default 0).
3553 * @param __n Number of characters in subview (default remainder).
3554 * @return The subview.
3555 * @throw std::out_of_range If __pos > size().
3556 *
3557 * Construct and return a subview using the `__n` characters starting at
3558 * `__pos`. If the string is too short, use the remainder of the
3559 * characters. If `__pos` is beyond the end of the string, out_of_range
3560 * is thrown.
3561 */
3562 [[nodiscard]]
3563 constexpr basic_string_view<_CharT, _Traits>
3564 subview(size_type __pos = 0, size_type __n = npos) const
3565 { return __sv_type(*this).subview(__pos, __n); }
3566#endif
3567
3568 /**
3569 * @brief Compare to a string.
3570 * @param __str String to compare against.
3571 * @return Integer < 0, 0, or > 0.
3572 *
3573 * Returns an integer < 0 if this string is ordered before @a
3574 * __str, 0 if their values are equivalent, or > 0 if this
3575 * string is ordered after @a __str. Determines the effective
3576 * length rlen of the strings to compare as the smallest of
3577 * size() and str.size(). The function then compares the two
3578 * strings by calling traits::compare(data(), str.data(),rlen).
3579 * If the result of the comparison is nonzero returns it,
3580 * otherwise the shorter one is ordered first.
3581 */
3582 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3583 int
3584 compare(const basic_string& __str) const
3585 {
3586 const size_type __size = this->size();
3587 const size_type __osize = __str.size();
3588 const size_type __len = std::min(__size, __osize);
3589
3590 int __r = traits_type::compare(_M_data(), __str.data(), __len);
3591 if (!__r)
3592 __r = _S_compare(__size, __osize);
3593 return __r;
3594 }
3595
3596#ifdef __glibcxx_string_view // >= C++17
3597 /**
3598 * @brief Compare to a string_view.
3599 * @param __svt An object convertible to string_view to compare against.
3600 * @return Integer < 0, 0, or > 0.
3601 */
3602 template<typename _Tp>
3603 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3604 _If_sv<_Tp, int>
3605 compare(const _Tp& __svt) const
3607 {
3608 __sv_type __sv = __svt;
3609 const size_type __size = this->size();
3610 const size_type __osize = __sv.size();
3611 const size_type __len = std::min(__size, __osize);
3612
3613 int __r = traits_type::compare(_M_data(), __sv.data(), __len);
3614 if (!__r)
3615 __r = _S_compare(__size, __osize);
3616 return __r;
3617 }
3618
3619 /**
3620 * @brief Compare to a string_view.
3621 * @param __pos A position in the string to start comparing from.
3622 * @param __n The number of characters to compare.
3623 * @param __svt An object convertible to string_view to compare
3624 * against.
3625 * @return Integer < 0, 0, or > 0.
3626 */
3627 template<typename _Tp>
3628 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3629 _If_sv<_Tp, int>
3630 compare(size_type __pos, size_type __n, const _Tp& __svt) const
3631 {
3632 __sv_type __sv = __svt;
3633 return __sv_type(*this).substr(__pos, __n).compare(__sv);
3634 }
3635
3636 /**
3637 * @brief Compare to a string_view.
3638 * @param __pos1 A position in the string to start comparing from.
3639 * @param __n1 The number of characters to compare.
3640 * @param __svt An object convertible to string_view to compare
3641 * against.
3642 * @param __pos2 A position in the string_view to start comparing from.
3643 * @param __n2 The number of characters to compare.
3644 * @return Integer < 0, 0, or > 0.
3645 */
3646 template<typename _Tp>
3647 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3648 _If_sv<_Tp, int>
3649 compare(size_type __pos1, size_type __n1, const _Tp& __svt,
3650 size_type __pos2, size_type __n2 = npos) const
3651 {
3652 __sv_type __sv = __svt;
3653 return __sv_type(*this)
3654 .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3655 }
3656#endif // C++17
3657
3658 /**
3659 * @brief Compare substring to a string.
3660 * @param __pos Index of first character of substring.
3661 * @param __n Number of characters in substring.
3662 * @param __str String to compare against.
3663 * @return Integer < 0, 0, or > 0.
3664 *
3665 * Form the substring of this string from the @a __n characters
3666 * starting at @a __pos. Returns an integer < 0 if the
3667 * substring is ordered before @a __str, 0 if their values are
3668 * equivalent, or > 0 if the substring is ordered after @a
3669 * __str. Determines the effective length rlen of the strings
3670 * to compare as the smallest of the length of the substring
3671 * and @a __str.size(). The function then compares the two
3672 * strings by calling
3673 * traits::compare(substring.data(),str.data(),rlen). If the
3674 * result of the comparison is nonzero returns it, otherwise
3675 * the shorter one is ordered first.
3676 */
3677 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3678 int
3679 compare(size_type __pos, size_type __n, const basic_string& __str) const
3680 {
3681 _M_check(__pos, "basic_string::compare");
3682 __n = _M_limit(__pos, __n);
3683 const size_type __osize = __str.size();
3684 const size_type __len = std::min(__n, __osize);
3685 int __r = traits_type::compare(_M_data() + __pos, __str.data(), __len);
3686 if (!__r)
3687 __r = _S_compare(__n, __osize);
3688 return __r;
3689 }
3690
3691 /**
3692 * @brief Compare substring to a substring.
3693 * @param __pos1 Index of first character of substring.
3694 * @param __n1 Number of characters in substring.
3695 * @param __str String to compare against.
3696 * @param __pos2 Index of first character of substring of str.
3697 * @param __n2 Number of characters in substring of str.
3698 * @return Integer < 0, 0, or > 0.
3699 *
3700 * Form the substring of this string from the @a __n1
3701 * characters starting at @a __pos1. Form the substring of @a
3702 * __str from the @a __n2 characters starting at @a __pos2.
3703 * Returns an integer < 0 if this substring is ordered before
3704 * the substring of @a __str, 0 if their values are equivalent,
3705 * or > 0 if this substring is ordered after the substring of
3706 * @a __str. Determines the effective length rlen of the
3707 * strings to compare as the smallest of the lengths of the
3708 * substrings. The function then compares the two strings by
3709 * calling
3710 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
3711 * If the result of the comparison is nonzero returns it,
3712 * otherwise the shorter one is ordered first.
3713 */
3714 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3715 int
3716 compare(size_type __pos1, size_type __n1, const basic_string& __str,
3717 size_type __pos2, size_type __n2 = npos) const
3718 {
3719 _M_check(__pos1, "basic_string::compare");
3720 __str._M_check(__pos2, "basic_string::compare");
3721 __n1 = _M_limit(__pos1, __n1);
3722 __n2 = __str._M_limit(__pos2, __n2);
3723 const size_type __len = std::min(__n1, __n2);
3724 int __r = traits_type::compare(_M_data() + __pos1,
3725 __str.data() + __pos2, __len);
3726 if (!__r)
3727 __r = _S_compare(__n1, __n2);
3728 return __r;
3729 }
3730
3731 /**
3732 * @brief Compare to a C string.
3733 * @param __s C string to compare against.
3734 * @return Integer < 0, 0, or > 0.
3735 *
3736 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
3737 * their values are equivalent, or > 0 if this string is ordered after
3738 * @a __s. Determines the effective length rlen of the strings to
3739 * compare as the smallest of size() and the length of a string
3740 * constructed from @a __s. The function then compares the two strings
3741 * by calling traits::compare(data(),s,rlen). If the result of the
3742 * comparison is nonzero returns it, otherwise the shorter one is
3743 * ordered first.
3744 */
3745 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3746 int
3747 compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT
3748 {
3749 __glibcxx_requires_string(__s);
3750 const size_type __size = this->size();
3751 const size_type __osize = traits_type::length(__s);
3752 const size_type __len = std::min(__size, __osize);
3753 int __r = traits_type::compare(_M_data(), __s, __len);
3754 if (!__r)
3755 __r = _S_compare(__size, __osize);
3756 return __r;
3757 }
3758
3759 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3760 // 5 String::compare specification questionable
3761 /**
3762 * @brief Compare substring to a C string.
3763 * @param __pos Index of first character of substring.
3764 * @param __n1 Number of characters in substring.
3765 * @param __s C string to compare against.
3766 * @return Integer < 0, 0, or > 0.
3767 *
3768 * Form the substring of this string from the @a __n1
3769 * characters starting at @a pos. Returns an integer < 0 if
3770 * the substring is ordered before @a __s, 0 if their values
3771 * are equivalent, or > 0 if the substring is ordered after @a
3772 * __s. Determines the effective length rlen of the strings to
3773 * compare as the smallest of the length of the substring and
3774 * the length of a string constructed from @a __s. The
3775 * function then compares the two string by calling
3776 * traits::compare(substring.data(),__s,rlen). If the result of
3777 * the comparison is nonzero returns it, otherwise the shorter
3778 * one is ordered first.
3779 */
3780 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3781 int
3782 compare(size_type __pos, size_type __n1, const _CharT* __s) const
3783 {
3784 __glibcxx_requires_string(__s);
3785 _M_check(__pos, "basic_string::compare");
3786 __n1 = _M_limit(__pos, __n1);
3787 const size_type __osize = traits_type::length(__s);
3788 const size_type __len = std::min(__n1, __osize);
3789 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
3790 if (!__r)
3791 __r = _S_compare(__n1, __osize);
3792 return __r;
3793 }
3794
3795 /**
3796 * @brief Compare substring against a character %array.
3797 * @param __pos Index of first character of substring.
3798 * @param __n1 Number of characters in substring.
3799 * @param __s character %array to compare against.
3800 * @param __n2 Number of characters of s.
3801 * @return Integer < 0, 0, or > 0.
3802 *
3803 * Form the substring of this string from the @a __n1
3804 * characters starting at @a __pos. Form a string from the
3805 * first @a __n2 characters of @a __s. Returns an integer < 0
3806 * if this substring is ordered before the string from @a __s,
3807 * 0 if their values are equivalent, or > 0 if this substring
3808 * is ordered after the string from @a __s. Determines the
3809 * effective length rlen of the strings to compare as the
3810 * smallest of the length of the substring and @a __n2. The
3811 * function then compares the two strings by calling
3812 * traits::compare(substring.data(),s,rlen). If the result of
3813 * the comparison is nonzero returns it, otherwise the shorter
3814 * one is ordered first.
3815 *
3816 * NB: s must have at least n2 characters, &apos;\\0&apos; has
3817 * no special meaning.
3818 */
3819 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3820 int
3821 compare(size_type __pos, size_type __n1, const _CharT* __s,
3822 size_type __n2) const
3823 {
3824 __glibcxx_requires_string_len(__s, __n2);
3825 _M_check(__pos, "basic_string::compare");
3826 __n1 = _M_limit(__pos, __n1);
3827 const size_type __len = std::min(__n1, __n2);
3828 int __r = traits_type::compare(_M_data() + __pos, __s, __len);
3829 if (!__r)
3830 __r = _S_compare(__n1, __n2);
3831 return __r;
3832 }
3833
3834#if __cplusplus >= 202002L
3835 [[nodiscard]]
3836 constexpr bool
3837 starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
3838 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3839
3840 [[nodiscard]]
3841 constexpr bool
3842 starts_with(_CharT __x) const noexcept
3843 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3844
3845 [[nodiscard, __gnu__::__nonnull__]]
3846 constexpr bool
3847 starts_with(const _CharT* __x) const noexcept
3848 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3849
3850 [[nodiscard]]
3851 constexpr bool
3852 ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept
3853 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3854
3855 [[nodiscard]]
3856 constexpr bool
3857 ends_with(_CharT __x) const noexcept
3858 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3859
3860 [[nodiscard, __gnu__::__nonnull__]]
3861 constexpr bool
3862 ends_with(const _CharT* __x) const noexcept
3863 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3864#endif // C++20
3865
3866#if __cplusplus > 202002L
3867 [[nodiscard]]
3868 constexpr bool
3869 contains(basic_string_view<_CharT, _Traits> __x) const noexcept
3870 { return __sv_type(this->data(), this->size()).contains(__x); }
3871
3872 [[nodiscard]]
3873 constexpr bool
3874 contains(_CharT __x) const noexcept
3875 { return __sv_type(this->data(), this->size()).contains(__x); }
3876
3877 [[nodiscard, __gnu__::__nonnull__]]
3878 constexpr bool
3879 contains(const _CharT* __x) const noexcept
3880 { return __sv_type(this->data(), this->size()).contains(__x); }
3881#endif // C++23
3882
3883 // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
3884 template<typename, typename, typename> friend class basic_stringbuf;
3885 };
3886_GLIBCXX_END_NAMESPACE_CXX11
3887_GLIBCXX_END_NAMESPACE_VERSION
3888} // namespace std
3889#endif // _GLIBCXX_USE_CXX11_ABI
3890
3891namespace std _GLIBCXX_VISIBILITY(default)
3892{
3893_GLIBCXX_BEGIN_NAMESPACE_VERSION
3894
3895#if __cpp_deduction_guides >= 201606
3896_GLIBCXX_BEGIN_NAMESPACE_CXX11
3897 template<typename _InputIterator, typename _CharT
3899 typename _Allocator = allocator<_CharT>,
3900 typename = _RequireInputIter<_InputIterator>,
3901 typename = _RequireAllocator<_Allocator>>
3902 basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
3904
3905 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3906 // 3075. basic_string needs deduction guides from basic_string_view
3907 template<typename _CharT, typename _Traits,
3908 typename _Allocator = allocator<_CharT>,
3909 typename = _RequireAllocator<_Allocator>>
3910 basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
3912
3913 template<typename _CharT, typename _Traits,
3914 typename _Allocator = allocator<_CharT>,
3915 typename = _RequireAllocator<_Allocator>>
3917 typename basic_string<_CharT, _Traits, _Allocator>::size_type,
3918 typename basic_string<_CharT, _Traits, _Allocator>::size_type,
3919 const _Allocator& = _Allocator())
3921
3922#if __glibcxx_containers_ranges // C++ >= 23
3923 template<ranges::input_range _Rg,
3924 typename _Allocator = allocator<ranges::range_value_t<_Rg>>>
3925 basic_string(from_range_t, _Rg&&, _Allocator = _Allocator())
3928 _Allocator>;
3929#endif
3930_GLIBCXX_END_NAMESPACE_CXX11
3931#endif
3932
3933 template<typename _Str>
3934 _GLIBCXX20_CONSTEXPR
3935 inline _Str
3936 __str_concat(typename _Str::value_type const* __lhs,
3937 typename _Str::size_type __lhs_len,
3938 typename _Str::value_type const* __rhs,
3939 typename _Str::size_type __rhs_len,
3940 typename _Str::allocator_type const& __a)
3941 {
3942 typedef typename _Str::allocator_type allocator_type;
3943 typedef __gnu_cxx::__alloc_traits<allocator_type> _Alloc_traits;
3944 _Str __str(_Alloc_traits::_S_select_on_copy(__a));
3945 __str.reserve(__lhs_len + __rhs_len);
3946 __str.append(__lhs, __lhs_len);
3947 __str.append(__rhs, __rhs_len);
3948 return __str;
3949 }
3950
3951 // operator+
3952 /**
3953 * @brief Concatenate two strings.
3954 * @param __lhs First string.
3955 * @param __rhs Last string.
3956 * @return New string with value of @a __lhs followed by @a __rhs.
3957 */
3958 template<typename _CharT, typename _Traits, typename _Alloc>
3959 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3963 {
3965 return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(),
3966 __rhs.c_str(), __rhs.size(),
3967 __lhs.get_allocator());
3968 }
3969
3970 /**
3971 * @brief Concatenate C string and string.
3972 * @param __lhs First string.
3973 * @param __rhs Last string.
3974 * @return New string with value of @a __lhs followed by @a __rhs.
3975 */
3976 template<typename _CharT, typename _Traits, typename _Alloc>
3977 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3978 inline basic_string<_CharT,_Traits,_Alloc>
3979 operator+(const _CharT* __lhs,
3981 {
3982 __glibcxx_requires_string(__lhs);
3984 return std::__str_concat<_Str>(__lhs, _Traits::length(__lhs),
3985 __rhs.c_str(), __rhs.size(),
3986 __rhs.get_allocator());
3987 }
3988
3989 /**
3990 * @brief Concatenate character and string.
3991 * @param __lhs First string.
3992 * @param __rhs Last string.
3993 * @return New string with @a __lhs followed by @a __rhs.
3994 */
3995 template<typename _CharT, typename _Traits, typename _Alloc>
3996 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
3997 inline basic_string<_CharT,_Traits,_Alloc>
3999 {
4001 return std::__str_concat<_Str>(__builtin_addressof(__lhs), 1,
4002 __rhs.c_str(), __rhs.size(),
4003 __rhs.get_allocator());
4004 }
4005
4006 /**
4007 * @brief Concatenate string and C string.
4008 * @param __lhs First string.
4009 * @param __rhs Last string.
4010 * @return New string with @a __lhs followed by @a __rhs.
4011 */
4012 template<typename _CharT, typename _Traits, typename _Alloc>
4013 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4014 inline basic_string<_CharT, _Traits, _Alloc>
4016 const _CharT* __rhs)
4017 {
4018 __glibcxx_requires_string(__rhs);
4020 return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(),
4021 __rhs, _Traits::length(__rhs),
4022 __lhs.get_allocator());
4023 }
4024 /**
4025 * @brief Concatenate string and character.
4026 * @param __lhs First string.
4027 * @param __rhs Last string.
4028 * @return New string with @a __lhs followed by @a __rhs.
4029 */
4030 template<typename _CharT, typename _Traits, typename _Alloc>
4031 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4032 inline basic_string<_CharT, _Traits, _Alloc>
4034 {
4036 return std::__str_concat<_Str>(__lhs.c_str(), __lhs.size(),
4037 __builtin_addressof(__rhs), 1,
4038 __lhs.get_allocator());
4039 }
4040
4041#if __cplusplus >= 201103L
4042 template<typename _CharT, typename _Traits, typename _Alloc>
4043 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4044 inline basic_string<_CharT, _Traits, _Alloc>
4045 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
4046 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4047 { return std::move(__lhs.append(__rhs)); }
4048
4049 template<typename _CharT, typename _Traits, typename _Alloc>
4050 _GLIBCXX20_CONSTEXPR
4054 { return std::move(__rhs.insert(0, __lhs)); }
4055
4056 template<typename _CharT, typename _Traits, typename _Alloc>
4057 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4061 {
4062#pragma GCC diagnostic push
4063#pragma GCC diagnostic ignored "-Wc++17-extensions" // if constexpr
4064 // Return value must use __lhs.get_allocator(), but if __rhs has equal
4065 // allocator then we can choose which parameter to modify in-place.
4066 bool __use_rhs = false;
4068 __use_rhs = true;
4069 else if (__lhs.get_allocator() == __rhs.get_allocator())
4070 __use_rhs = true;
4071 if (__use_rhs)
4072 {
4073 const auto __size = __lhs.size() + __rhs.size();
4074 if (__size > __lhs.capacity() && __size <= __rhs.capacity())
4075 return std::move(__rhs.insert(0, __lhs));
4076 }
4077 return std::move(__lhs.append(__rhs));
4078#pragma GCC diagnostic pop
4079 }
4080
4081 template<typename _CharT, typename _Traits, typename _Alloc>
4082 _GLIBCXX_NODISCARD _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4084 operator+(const _CharT* __lhs,
4086 { return std::move(__rhs.insert(0, __lhs)); }
4087
4088 template<typename _CharT, typename _Traits, typename _Alloc>
4089 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4091 operator+(_CharT __lhs,
4093 { return std::move(__rhs.insert(0, 1, __lhs)); }
4094
4095 template<typename _CharT, typename _Traits, typename _Alloc>
4096 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4099 const _CharT* __rhs)
4100 { return std::move(__lhs.append(__rhs)); }
4101
4102 template<typename _CharT, typename _Traits, typename _Alloc>
4103 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4106 _CharT __rhs)
4107 { return std::move(__lhs.append(1, __rhs)); }
4108#endif
4109
4110#if __glibcxx_string_view >= 202403L
4111 // const string & + string_view
4112 template<typename _CharT, typename _Traits, typename _Alloc>
4113 [[nodiscard]]
4116 type_identity_t<basic_string_view<_CharT, _Traits>> __rhs)
4117 {
4119 return std::__str_concat<_Str>(__lhs.data(), __lhs.size(),
4120 __rhs.data(), __rhs.size(),
4121 __lhs.get_allocator());
4122 }
4123
4124 // string && + string_view
4125 template<typename _CharT, typename _Traits, typename _Alloc>
4126 [[nodiscard]]
4129 type_identity_t<basic_string_view<_CharT, _Traits>> __rhs)
4130 {
4131 return std::move(__lhs.append(__rhs));
4132 }
4133
4134 // string_view + const string &
4135 template<typename _CharT, typename _Traits, typename _Alloc>
4136 [[nodiscard]]
4138 operator+(type_identity_t<basic_string_view<_CharT, _Traits>> __lhs,
4140 {
4142 return std::__str_concat<_Str>(__lhs.data(), __lhs.size(),
4143 __rhs.data(), __rhs.size(),
4144 __rhs.get_allocator());
4145 }
4146
4147 // string_view + string &&
4148 template<typename _CharT, typename _Traits, typename _Alloc>
4149 [[nodiscard]]
4151 operator+(type_identity_t<basic_string_view<_CharT, _Traits>> __lhs,
4153 {
4154 return std::move(__rhs.insert(0, __lhs));
4155 }
4156#endif
4157
4158 // operator ==
4159 /**
4160 * @brief Test equivalence of two strings.
4161 * @param __lhs First string.
4162 * @param __rhs Second string.
4163 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
4164 */
4165 template<typename _CharT, typename _Traits, typename _Alloc>
4166 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4167 inline bool
4170 _GLIBCXX_NOEXCEPT
4171 {
4172 return __lhs.size() == __rhs.size()
4173 && !_Traits::compare(__lhs.data(), __rhs.data(), __lhs.size());
4174 }
4175
4176 /**
4177 * @brief Test equivalence of string and C string.
4178 * @param __lhs String.
4179 * @param __rhs C string.
4180 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
4181 */
4182 template<typename _CharT, typename _Traits, typename _Alloc>
4183 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
4184 inline bool
4186 const _CharT* __rhs)
4187 {
4188 return __lhs.size() == _Traits::length(__rhs)
4189 && !_Traits::compare(__lhs.data(), __rhs, __lhs.size());
4190 }
4191
4192#if __cpp_lib_three_way_comparison
4193 /**
4194 * @brief Three-way comparison of a string and a C string.
4195 * @param __lhs A string.
4196 * @param __rhs A null-terminated string.
4197 * @return A value indicating whether `__lhs` is less than, equal to,
4198 * greater than, or incomparable with `__rhs`.
4199 */
4200 template<typename _CharT, typename _Traits, typename _Alloc>
4201 [[nodiscard]]
4202 constexpr auto
4203 operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4204 const basic_string<_CharT, _Traits, _Alloc>& __rhs) noexcept
4205 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
4206 { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); }
4207
4208 /**
4209 * @brief Three-way comparison of a string and a C string.
4210 * @param __lhs A string.
4211 * @param __rhs A null-terminated string.
4212 * @return A value indicating whether `__lhs` is less than, equal to,
4213 * greater than, or incomparable with `__rhs`.
4214 */
4215 template<typename _CharT, typename _Traits, typename _Alloc>
4216 [[nodiscard]]
4217 constexpr auto
4218 operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4219 const _CharT* __rhs) noexcept
4220 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
4221 { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); }
4222#else
4223 /**
4224 * @brief Test equivalence of C string and string.
4225 * @param __lhs C string.
4226 * @param __rhs String.
4227 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
4228 */
4229 template<typename _CharT, typename _Traits, typename _Alloc>
4230 _GLIBCXX_NODISCARD
4231 inline bool
4232 operator==(const _CharT* __lhs,
4233 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
4234 { return __rhs == __lhs; }
4235
4236 // operator !=
4237 /**
4238 * @brief Test difference of two strings.
4239 * @param __lhs First string.
4240 * @param __rhs Second string.
4241 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
4242 */
4243 template<typename _CharT, typename _Traits, typename _Alloc>
4244 _GLIBCXX_NODISCARD
4245 inline bool
4246 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4248 _GLIBCXX_NOEXCEPT
4249 { return !(__lhs == __rhs); }
4250
4251 /**
4252 * @brief Test difference of C string and string.
4253 * @param __lhs C string.
4254 * @param __rhs String.
4255 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
4256 */
4257 template<typename _CharT, typename _Traits, typename _Alloc>
4258 _GLIBCXX_NODISCARD
4259 inline bool
4260 operator!=(const _CharT* __lhs,
4262 { return !(__rhs == __lhs); }
4263
4264 /**
4265 * @brief Test difference of string and C string.
4266 * @param __lhs String.
4267 * @param __rhs C string.
4268 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
4269 */
4270 template<typename _CharT, typename _Traits, typename _Alloc>
4271 _GLIBCXX_NODISCARD
4272 inline bool
4273 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4274 const _CharT* __rhs)
4275 { return !(__lhs == __rhs); }
4276
4277 // operator <
4278 /**
4279 * @brief Test if string precedes string.
4280 * @param __lhs First string.
4281 * @param __rhs Second string.
4282 * @return True if @a __lhs precedes @a __rhs. False otherwise.
4283 */
4284 template<typename _CharT, typename _Traits, typename _Alloc>
4285 _GLIBCXX_NODISCARD
4286 inline bool
4287 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4289 _GLIBCXX_NOEXCEPT
4290 { return __lhs.compare(__rhs) < 0; }
4291
4292 /**
4293 * @brief Test if string precedes C string.
4294 * @param __lhs String.
4295 * @param __rhs C string.
4296 * @return True if @a __lhs precedes @a __rhs. False otherwise.
4297 */
4298 template<typename _CharT, typename _Traits, typename _Alloc>
4299 _GLIBCXX_NODISCARD
4300 inline bool
4301 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4302 const _CharT* __rhs)
4303 { return __lhs.compare(__rhs) < 0; }
4304
4305 /**
4306 * @brief Test if C string precedes string.
4307 * @param __lhs C string.
4308 * @param __rhs String.
4309 * @return True if @a __lhs precedes @a __rhs. False otherwise.
4310 */
4311 template<typename _CharT, typename _Traits, typename _Alloc>
4312 _GLIBCXX_NODISCARD
4313 inline bool
4314 operator<(const _CharT* __lhs,
4316 { return __rhs.compare(__lhs) > 0; }
4317
4318 // operator >
4319 /**
4320 * @brief Test if string follows string.
4321 * @param __lhs First string.
4322 * @param __rhs Second string.
4323 * @return True if @a __lhs follows @a __rhs. False otherwise.
4324 */
4325 template<typename _CharT, typename _Traits, typename _Alloc>
4326 _GLIBCXX_NODISCARD
4327 inline bool
4328 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4330 _GLIBCXX_NOEXCEPT
4331 { return __lhs.compare(__rhs) > 0; }
4332
4333 /**
4334 * @brief Test if string follows C string.
4335 * @param __lhs String.
4336 * @param __rhs C string.
4337 * @return True if @a __lhs follows @a __rhs. False otherwise.
4338 */
4339 template<typename _CharT, typename _Traits, typename _Alloc>
4340 _GLIBCXX_NODISCARD
4341 inline bool
4342 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4343 const _CharT* __rhs)
4344 { return __lhs.compare(__rhs) > 0; }
4345
4346 /**
4347 * @brief Test if C string follows string.
4348 * @param __lhs C string.
4349 * @param __rhs String.
4350 * @return True if @a __lhs follows @a __rhs. False otherwise.
4351 */
4352 template<typename _CharT, typename _Traits, typename _Alloc>
4353 _GLIBCXX_NODISCARD
4354 inline bool
4355 operator>(const _CharT* __lhs,
4357 { return __rhs.compare(__lhs) < 0; }
4358
4359 // operator <=
4360 /**
4361 * @brief Test if string doesn't follow string.
4362 * @param __lhs First string.
4363 * @param __rhs Second string.
4364 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
4365 */
4366 template<typename _CharT, typename _Traits, typename _Alloc>
4367 _GLIBCXX_NODISCARD
4368 inline bool
4369 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4371 _GLIBCXX_NOEXCEPT
4372 { return __lhs.compare(__rhs) <= 0; }
4373
4374 /**
4375 * @brief Test if string doesn't follow C string.
4376 * @param __lhs String.
4377 * @param __rhs C string.
4378 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
4379 */
4380 template<typename _CharT, typename _Traits, typename _Alloc>
4381 _GLIBCXX_NODISCARD
4382 inline bool
4383 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4384 const _CharT* __rhs)
4385 { return __lhs.compare(__rhs) <= 0; }
4386
4387 /**
4388 * @brief Test if C string doesn't follow string.
4389 * @param __lhs C string.
4390 * @param __rhs String.
4391 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
4392 */
4393 template<typename _CharT, typename _Traits, typename _Alloc>
4394 _GLIBCXX_NODISCARD
4395 inline bool
4396 operator<=(const _CharT* __lhs,
4398 { return __rhs.compare(__lhs) >= 0; }
4399
4400 // operator >=
4401 /**
4402 * @brief Test if string doesn't precede string.
4403 * @param __lhs First string.
4404 * @param __rhs Second string.
4405 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
4406 */
4407 template<typename _CharT, typename _Traits, typename _Alloc>
4408 _GLIBCXX_NODISCARD
4409 inline bool
4410 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4412 _GLIBCXX_NOEXCEPT
4413 { return __lhs.compare(__rhs) >= 0; }
4414
4415 /**
4416 * @brief Test if string doesn't precede C string.
4417 * @param __lhs String.
4418 * @param __rhs C string.
4419 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
4420 */
4421 template<typename _CharT, typename _Traits, typename _Alloc>
4422 _GLIBCXX_NODISCARD
4423 inline bool
4424 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
4425 const _CharT* __rhs)
4426 { return __lhs.compare(__rhs) >= 0; }
4427
4428 /**
4429 * @brief Test if C string doesn't precede string.
4430 * @param __lhs C string.
4431 * @param __rhs String.
4432 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
4433 */
4434 template<typename _CharT, typename _Traits, typename _Alloc>
4435 _GLIBCXX_NODISCARD
4436 inline bool
4437 operator>=(const _CharT* __lhs,
4439 { return __rhs.compare(__lhs) <= 0; }
4440#endif // three-way comparison
4441
4442 /**
4443 * @brief Swap contents of two strings.
4444 * @param __lhs First string.
4445 * @param __rhs Second string.
4446 *
4447 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
4448 */
4449 template<typename _CharT, typename _Traits, typename _Alloc>
4450 _GLIBCXX20_CONSTEXPR
4451 inline void
4454 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
4455 { __lhs.swap(__rhs); }
4456
4457
4458 /**
4459 * @brief Read stream into a string.
4460 * @param __is Input stream.
4461 * @param __str Buffer to store into.
4462 * @return Reference to the input stream.
4463 *
4464 * Stores characters from @a __is into @a __str until whitespace is
4465 * found, the end of the stream is encountered, or str.max_size()
4466 * is reached. If is.width() is non-zero, that is the limit on the
4467 * number of characters stored into @a __str. Any previous
4468 * contents of @a __str are erased.
4469 */
4470 template<typename _CharT, typename _Traits, typename _Alloc>
4471 basic_istream<_CharT, _Traits>&
4472 operator>>(basic_istream<_CharT, _Traits>& __is,
4473 basic_string<_CharT, _Traits, _Alloc>& __str);
4474
4475 template<>
4476 basic_istream<char>&
4478
4479 /**
4480 * @brief Write string to a stream.
4481 * @param __os Output stream.
4482 * @param __str String to write out.
4483 * @return Reference to the output stream.
4484 *
4485 * Output characters of @a __str into os following the same rules as for
4486 * writing a C string.
4487 */
4488 template<typename _CharT, typename _Traits, typename _Alloc>
4492 {
4493 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4494 // 586. string inserter not a formatted function
4495 return __ostream_insert(__os, __str.data(), __str.size());
4496 }
4497
4498 /**
4499 * @brief Read a line from stream into a string.
4500 * @param __is Input stream.
4501 * @param __str Buffer to store into.
4502 * @param __delim Character marking end of line.
4503 * @return Reference to the input stream.
4504 *
4505 * Stores characters from @a __is into @a __str until @a __delim is
4506 * found, the end of the stream is encountered, or str.max_size()
4507 * is reached. Any previous contents of @a __str are erased. If
4508 * @a __delim is encountered, it is extracted but not stored into
4509 * @a __str.
4510 */
4511 template<typename _CharT, typename _Traits, typename _Alloc>
4512 basic_istream<_CharT, _Traits>&
4513 getline(basic_istream<_CharT, _Traits>& __is,
4514 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
4515
4516 /**
4517 * @brief Read a line from stream into a string.
4518 * @param __is Input stream.
4519 * @param __str Buffer to store into.
4520 * @return Reference to the input stream.
4521 *
4522 * Stores characters from is into @a __str until &apos;\n&apos; is
4523 * found, the end of the stream is encountered, or str.max_size()
4524 * is reached. Any previous contents of @a __str are erased. If
4525 * end of line is encountered, it is extracted but not stored into
4526 * @a __str.
4527 */
4528 template<typename _CharT, typename _Traits, typename _Alloc>
4529 inline basic_istream<_CharT, _Traits>&
4532 { return std::getline(__is, __str, __is.widen('\n')); }
4533
4534#if __cplusplus >= 201103L
4535 /// Read a line from an rvalue stream into a string.
4536 template<typename _CharT, typename _Traits, typename _Alloc>
4537 inline basic_istream<_CharT, _Traits>&
4539 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
4540 { return std::getline(__is, __str, __delim); }
4541
4542 /// Read a line from an rvalue stream into a string.
4543 template<typename _CharT, typename _Traits, typename _Alloc>
4544 inline basic_istream<_CharT, _Traits>&
4548#endif
4549
4550 template<>
4551 basic_istream<char>&
4552 getline(basic_istream<char>& __in, basic_string<char>& __str,
4553 char __delim);
4554
4555#ifdef _GLIBCXX_USE_WCHAR_T
4556 template<>
4557 basic_istream<wchar_t>&
4558 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
4559 wchar_t __delim);
4560#endif
4561
4562_GLIBCXX_END_NAMESPACE_VERSION
4563} // namespace
4564
4565#if __cplusplus >= 201103L
4566
4567#include <ext/string_conversions.h>
4568#include <bits/charconv.h>
4569
4570namespace std _GLIBCXX_VISIBILITY(default)
4571{
4572_GLIBCXX_BEGIN_NAMESPACE_VERSION
4573_GLIBCXX_BEGIN_NAMESPACE_CXX11
4574
4575 // 21.4 Numeric Conversions [string.conversions].
4576 inline int
4577 stoi(const string& __str, size_t* __idx = 0, int __base = 10)
4578 { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
4579 __idx, __base); }
4580
4581 inline long
4582 stol(const string& __str, size_t* __idx = 0, int __base = 10)
4583 { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
4584 __idx, __base); }
4585
4586 inline unsigned long
4587 stoul(const string& __str, size_t* __idx = 0, int __base = 10)
4588 { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
4589 __idx, __base); }
4590
4591#if _GLIBCXX_USE_C99_STDLIB
4592 inline long long
4593 stoll(const string& __str, size_t* __idx = 0, int __base = 10)
4594 { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
4595 __idx, __base); }
4596
4597 inline unsigned long long
4598 stoull(const string& __str, size_t* __idx = 0, int __base = 10)
4599 { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
4600 __idx, __base); }
4601#elif __LONG_WIDTH__ == __LONG_LONG_WIDTH__
4602 inline long long
4603 stoll(const string& __str, size_t* __idx = 0, int __base = 10)
4604 { return std::stol(__str, __idx, __base); }
4605
4606 inline unsigned long long
4607 stoull(const string& __str, size_t* __idx = 0, int __base = 10)
4608 { return std::stoul(__str, __idx, __base); }
4609#endif
4610
4611 inline double
4612 stod(const string& __str, size_t* __idx = 0)
4613 { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
4614
4615#if _GLIBCXX_HAVE_STRTOF
4616 // NB: strtof vs strtod.
4617 inline float
4618 stof(const string& __str, size_t* __idx = 0)
4619 { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
4620#else
4621 inline float
4622 stof(const string& __str, size_t* __idx = 0)
4623 {
4624 double __d = std::stod(__str, __idx);
4625 if (__builtin_isfinite(__d) && __d != 0.0)
4626 {
4627 double __abs_d = __builtin_fabs(__d);
4628 if (__abs_d < __FLT_MIN__ || __abs_d > __FLT_MAX__)
4629 {
4630 errno = ERANGE;
4631 std::__throw_out_of_range("stof");
4632 }
4633 }
4634 return __d;
4635 }
4636#endif
4637
4638#if _GLIBCXX_HAVE_STRTOLD && ! _GLIBCXX_HAVE_BROKEN_STRTOLD
4639 inline long double
4640 stold(const string& __str, size_t* __idx = 0)
4641 { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
4642#elif __DBL_MANT_DIG__ == __LDBL_MANT_DIG__
4643 inline long double
4644 stold(const string& __str, size_t* __idx = 0)
4645 { return std::stod(__str, __idx); }
4646#endif
4647
4648#if __glibcxx_constexpr_string >= 202511L
4649# define _GLIBCXX_TO_STRING_CONSTEXPR constexpr
4650#else
4651# define _GLIBCXX_TO_STRING_CONSTEXPR inline
4652#endif
4653
4654 // _GLIBCXX_RESOLVE_LIB_DEFECTS
4655 // DR 1261. Insufficient overloads for to_string / to_wstring
4656
4657 _GLIBCXX_NODISCARD
4658 _GLIBCXX_TO_STRING_CONSTEXPR string
4659 to_string(int __val)
4660#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32
4661 noexcept // any 32-bit value fits in the SSO buffer
4662#endif
4663 {
4664 const bool __neg = __val < 0;
4665 const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val;
4666 const auto __len = __detail::__to_chars_len(__uval);
4667 string __str;
4668 __str.__resize_and_overwrite(__neg + __len, [=](char* __p, size_t __n) {
4669 __p[0] = '-';
4670 __detail::__to_chars_10_impl(__p + (int)__neg, __len, __uval);
4671 return __n;
4672 });
4673 return __str;
4674 }
4675
4676 _GLIBCXX_NODISCARD
4677 _GLIBCXX_TO_STRING_CONSTEXPR string
4678 to_string(unsigned __val)
4679#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32
4680 noexcept // any 32-bit value fits in the SSO buffer
4681#endif
4682 {
4683 const auto __len = __detail::__to_chars_len(__val);
4684 string __str;
4685 __str.__resize_and_overwrite(__len, [__val](char* __p, size_t __n) {
4686 __detail::__to_chars_10_impl(__p, __n, __val);
4687 return __n;
4688 });
4689 return __str;
4690 }
4691
4692 _GLIBCXX_NODISCARD
4693 _GLIBCXX_TO_STRING_CONSTEXPR string
4694 to_string(long __val)
4695#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32
4696 noexcept // any 32-bit value fits in the SSO buffer
4697#endif
4698 {
4699 const bool __neg = __val < 0;
4700 const unsigned long __uval = __neg ? (unsigned long)~__val + 1ul : __val;
4701 const auto __len = __detail::__to_chars_len(__uval);
4702 string __str;
4703 __str.__resize_and_overwrite(__neg + __len, [=](char* __p, size_t __n) {
4704 __p[0] = '-';
4705 __detail::__to_chars_10_impl(__p + (int)__neg, __len, __uval);
4706 return __n;
4707 });
4708 return __str;
4709 }
4710
4711 _GLIBCXX_NODISCARD
4712 _GLIBCXX_TO_STRING_CONSTEXPR string
4713 to_string(unsigned long __val)
4714#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32
4715 noexcept // any 32-bit value fits in the SSO buffer
4716#endif
4717 {
4718 const auto __len = __detail::__to_chars_len(__val);
4719 string __str;
4720 __str.__resize_and_overwrite(__len, [__val](char* __p, size_t __n) {
4721 __detail::__to_chars_10_impl(__p, __n, __val);
4722 return __n;
4723 });
4724 return __str;
4725 }
4726
4727 _GLIBCXX_NODISCARD
4728 _GLIBCXX_TO_STRING_CONSTEXPR string
4729 to_string(long long __val)
4730 {
4731 const bool __neg = __val < 0;
4732 const unsigned long long __uval
4733 = __neg ? (unsigned long long)~__val + 1ull : __val;
4734 const auto __len = __detail::__to_chars_len(__uval);
4735 string __str;
4736 __str.__resize_and_overwrite(__neg + __len, [=](char* __p, size_t __n) {
4737 __p[0] = '-';
4738 __detail::__to_chars_10_impl(__p + (int)__neg, __len, __uval);
4739 return __n;
4740 });
4741 return __str;
4742 }
4743
4744 _GLIBCXX_NODISCARD
4745 _GLIBCXX_TO_STRING_CONSTEXPR string
4746 to_string(unsigned long long __val)
4747 {
4748 const auto __len = __detail::__to_chars_len(__val);
4749 string __str;
4750 __str.__resize_and_overwrite(__len, [__val](char* __p, size_t __n) {
4751 __detail::__to_chars_10_impl(__p, __n, __val);
4752 return __n;
4753 });
4754 return __str;
4755 }
4756
4757#if __glibcxx_to_string >= 202306L // C++ >= 26
4758
4759 [[nodiscard]]
4760 inline string
4761 to_string(float __val)
4762 {
4763 string __str;
4764 size_t __len = 15;
4765 do {
4766 __str.resize_and_overwrite(__len,
4767 [__val, &__len] (char* __p, size_t __n) {
4768 auto [__end, __err] = std::to_chars(__p, __p + __n, __val);
4769 if (__err == errc{}) [[likely]]
4770 return __end - __p;
4771 __len *= 2;
4772 return __p - __p;;
4773 });
4774 } while (__str.empty());
4775 return __str;
4776 }
4777
4778 [[nodiscard]]
4779 inline string
4780 to_string(double __val)
4781 {
4782 string __str;
4783 size_t __len = 15;
4784 do {
4785 __str.resize_and_overwrite(__len,
4786 [__val, &__len] (char* __p, size_t __n) {
4787 auto [__end, __err] = std::to_chars(__p, __p + __n, __val);
4788 if (__err == errc{}) [[likely]]
4789 return __end - __p;
4790 __len *= 2;
4791 return __p - __p;;
4792 });
4793 } while (__str.empty());
4794 return __str;
4795 }
4796
4797 [[nodiscard]]
4798 inline string
4799 to_string(long double __val)
4800 {
4801 string __str;
4802 size_t __len = 15;
4803 do {
4804 __str.resize_and_overwrite(__len,
4805 [__val, &__len] (char* __p, size_t __n) {
4806 auto [__end, __err] = std::to_chars(__p, __p + __n, __val);
4807 if (__err == errc{}) [[likely]]
4808 return __end - __p;
4809 __len *= 2;
4810 return __p - __p;;
4811 });
4812 } while (__str.empty());
4813 return __str;
4814 }
4815#elif _GLIBCXX_USE_C99_STDIO
4816#pragma GCC diagnostic push
4817#pragma GCC diagnostic ignored "-Wsuggest-attribute=format"
4818 // NB: (v)snprintf vs sprintf.
4819
4820 _GLIBCXX_NODISCARD
4821 inline string
4822 to_string(float __val)
4823 {
4824 const int __n =
4825 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
4826 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
4827 "%f", __val);
4828 }
4829
4830 _GLIBCXX_NODISCARD
4831 inline string
4832 to_string(double __val)
4833 {
4834 const int __n =
4835 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
4836 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
4837 "%f", __val);
4838 }
4839
4840 _GLIBCXX_NODISCARD
4841 inline string
4842 to_string(long double __val)
4843 {
4844 const int __n =
4845 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
4846 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
4847 "%Lf", __val);
4848 }
4849#pragma GCC diagnostic pop
4850#endif // _GLIBCXX_USE_C99_STDIO
4851
4852#if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
4853 inline int
4854 stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
4855 { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
4856 __idx, __base); }
4857
4858 inline long
4859 stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
4860 { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
4861 __idx, __base); }
4862
4863 inline unsigned long
4864 stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
4865 { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
4866 __idx, __base); }
4867
4868 inline long long
4869 stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
4870 { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
4871 __idx, __base); }
4872
4873 inline unsigned long long
4874 stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
4875 { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
4876 __idx, __base); }
4877
4878 // NB: wcstof vs wcstod.
4879 inline float
4880 stof(const wstring& __str, size_t* __idx = 0)
4881 { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
4882
4883 inline double
4884 stod(const wstring& __str, size_t* __idx = 0)
4885 { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
4886
4887 inline long double
4888 stold(const wstring& __str, size_t* __idx = 0)
4889 { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
4890#endif
4891
4892#ifdef _GLIBCXX_USE_WCHAR_T
4893#pragma GCC diagnostic push
4894#pragma GCC diagnostic ignored "-Wc++17-extensions"
4895 _GLIBCXX20_CONSTEXPR
4896 inline void
4897 __to_wstring_numeric(const char* __s, int __len, wchar_t* __wout)
4898 {
4899 // This condition is true if exec-charset and wide-exec-charset share the
4900 // same values for the ASCII subset or the EBCDIC invariant character set.
4901 if constexpr (wchar_t('0') == L'0' && wchar_t('-') == L'-'
4902 && wchar_t('.') == L'.' && wchar_t('e') == L'e')
4903 {
4904 for (int __i = 0; __i < __len; ++__i)
4905 __wout[__i] = (wchar_t) __s[__i];
4906 }
4907 else
4908 {
4909 wchar_t __wc[256];
4910 for (int __i = '0'; __i <= '9'; ++__i)
4911 __wc[__i] = L'0' + __i;
4912 __wc['.'] = L'.';
4913 __wc['+'] = L'+';
4914 __wc['-'] = L'-';
4915 __wc['a'] = L'a';
4916 __wc['b'] = L'b';
4917 __wc['c'] = L'c';
4918 __wc['d'] = L'd';
4919 __wc['e'] = L'e';
4920 __wc['f'] = L'f';
4921 __wc['i'] = L'i'; // for "inf"
4922 __wc['n'] = L'n'; // for "nan" and "inf"
4923 __wc['p'] = L'p'; // for hexfloats "0x1p1"
4924 __wc['x'] = L'x';
4925 __wc['A'] = L'A';
4926 __wc['B'] = L'B';
4927 __wc['C'] = L'C';
4928 __wc['D'] = L'D';
4929 __wc['E'] = L'E';
4930 __wc['F'] = L'F';
4931 __wc['I'] = L'I';
4932 __wc['N'] = L'N';
4933 __wc['P'] = L'P';
4934 __wc['X'] = L'X';
4935
4936 for (int __i = 0; __i < __len; ++__i)
4937 __wout[__i] = __wc[(int)__s[__i]];
4938 }
4939 }
4940
4941#if __glibcxx_constexpr_string >= 201907L
4942 constexpr
4943#endif
4944 inline wstring
4945#ifdef __glibcxx_string_view // >= C++17
4946 __to_wstring_numeric(string_view __s)
4947#else
4948 __to_wstring_numeric(const string& __s)
4949#endif
4950 {
4951 if constexpr (wchar_t('0') == L'0' && wchar_t('-') == L'-'
4952 && wchar_t('.') == L'.' && wchar_t('e') == L'e')
4953 return wstring(__s.data(), __s.data() + __s.size());
4954 else
4955 {
4956 wstring __ws;
4957 auto __f = __s.data();
4958 __ws.__resize_and_overwrite(__s.size(),
4959 [__f] (wchar_t* __to, int __n) {
4960 std::__to_wstring_numeric(__f, __n, __to);
4961 return __n;
4962 });
4963 return __ws;
4964 }
4965 }
4966#pragma GCC diagnostic pop
4967
4968 _GLIBCXX_NODISCARD
4969 _GLIBCXX_TO_STRING_CONSTEXPR wstring
4970 to_wstring(int __val)
4971 { return std::__to_wstring_numeric(std::to_string(__val)); }
4972
4973 _GLIBCXX_NODISCARD
4974 _GLIBCXX_TO_STRING_CONSTEXPR wstring
4975 to_wstring(unsigned __val)
4976 { return std::__to_wstring_numeric(std::to_string(__val)); }
4977
4978 _GLIBCXX_NODISCARD
4979 _GLIBCXX_TO_STRING_CONSTEXPR wstring
4980 to_wstring(long __val)
4981 { return std::__to_wstring_numeric(std::to_string(__val)); }
4982
4983 _GLIBCXX_NODISCARD
4984 _GLIBCXX_TO_STRING_CONSTEXPR wstring
4985 to_wstring(unsigned long __val)
4986 { return std::__to_wstring_numeric(std::to_string(__val)); }
4987
4988 _GLIBCXX_NODISCARD
4989 _GLIBCXX_TO_STRING_CONSTEXPR wstring
4990 to_wstring(long long __val)
4991 { return std::__to_wstring_numeric(std::to_string(__val)); }
4992
4993 _GLIBCXX_NODISCARD
4994 _GLIBCXX_TO_STRING_CONSTEXPR wstring
4995 to_wstring(unsigned long long __val)
4996 { return std::__to_wstring_numeric(std::to_string(__val)); }
4997
4998#if __glibcxx_to_string || _GLIBCXX_USE_C99_STDIO
4999 _GLIBCXX_NODISCARD
5000 inline wstring
5001 to_wstring(float __val)
5002 { return std::__to_wstring_numeric(std::to_string(__val)); }
5003
5004 _GLIBCXX_NODISCARD
5005 inline wstring
5006 to_wstring(double __val)
5007 { return std::__to_wstring_numeric(std::to_string(__val)); }
5008
5009 _GLIBCXX_NODISCARD
5010 inline wstring
5011 to_wstring(long double __val)
5012 { return std::__to_wstring_numeric(std::to_string(__val)); }
5013#endif
5014#endif // _GLIBCXX_USE_WCHAR_T
5015#undef _GLIBCXX_TO_STRING_CONSTEXPR
5016
5017_GLIBCXX_END_NAMESPACE_CXX11
5018_GLIBCXX_END_NAMESPACE_VERSION
5019} // namespace
5020
5021#endif /* C++11 */
5022
5023#if __cplusplus >= 201103L
5024
5025#include <bits/functional_hash.h>
5026
5027namespace std _GLIBCXX_VISIBILITY(default)
5028{
5029_GLIBCXX_BEGIN_NAMESPACE_VERSION
5030
5031 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5032 // 3705. Hashability shouldn't depend on basic_string's allocator
5033
5034 template<typename _CharT, typename _Alloc,
5035 typename _StrT = basic_string<_CharT, char_traits<_CharT>, _Alloc>>
5036 struct __str_hash_base
5037 : public __hash_base<size_t, _StrT>
5038 {
5039 [[__nodiscard__]]
5040 size_t
5041 operator()(const _StrT& __s) const noexcept
5042 { return _Hash_impl::hash(__s.data(), __s.length() * sizeof(_CharT)); }
5043 };
5044
5045#ifndef _GLIBCXX_COMPATIBILITY_CXX0X
5046 /// std::hash specialization for string.
5047 template<typename _Alloc>
5048 struct hash<basic_string<char, char_traits<char>, _Alloc>>
5049 : public __str_hash_base<char, _Alloc>
5050 { };
5051
5052 /// std::hash specialization for wstring.
5053 template<typename _Alloc>
5054 struct hash<basic_string<wchar_t, char_traits<wchar_t>, _Alloc>>
5055 : public __str_hash_base<wchar_t, _Alloc>
5056 { };
5057
5058 template<typename _Alloc>
5059 struct __is_fast_hash<hash<basic_string<wchar_t, char_traits<wchar_t>,
5060 _Alloc>>>
5062 { };
5063#endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
5064
5065#ifdef _GLIBCXX_USE_CHAR8_T
5066 /// std::hash specialization for u8string.
5067 template<typename _Alloc>
5068 struct hash<basic_string<char8_t, char_traits<char8_t>, _Alloc>>
5069 : public __str_hash_base<char8_t, _Alloc>
5070 { };
5071#endif
5072
5073 /// std::hash specialization for u16string.
5074 template<typename _Alloc>
5075 struct hash<basic_string<char16_t, char_traits<char16_t>, _Alloc>>
5076 : public __str_hash_base<char16_t, _Alloc>
5077 { };
5078
5079 /// std::hash specialization for u32string.
5080 template<typename _Alloc>
5081 struct hash<basic_string<char32_t, char_traits<char32_t>, _Alloc>>
5082 : public __str_hash_base<char32_t, _Alloc>
5083 { };
5084
5085#if ! _GLIBCXX_INLINE_VERSION
5086 // PR libstdc++/105907 - __is_fast_hash affects unordered container ABI.
5087 template<> struct __is_fast_hash<hash<string>> : std::false_type { };
5088 template<> struct __is_fast_hash<hash<wstring>> : std::false_type { };
5089 template<> struct __is_fast_hash<hash<u16string>> : std::false_type { };
5090 template<> struct __is_fast_hash<hash<u32string>> : std::false_type { };
5091#ifdef _GLIBCXX_USE_CHAR8_T
5092 template<> struct __is_fast_hash<hash<u8string>> : std::false_type { };
5093#endif
5094#else
5095 // For versioned namespace, assume every std::hash<basic_string<>> is slow.
5096 template<typename _CharT, typename _Traits, typename _Alloc>
5097 struct __is_fast_hash<hash<basic_string<_CharT, _Traits, _Alloc>>>
5099 { };
5100#endif
5101
5102#ifdef __glibcxx_string_udls // C++ >= 14
5103 inline namespace literals
5104 {
5105 inline namespace string_literals
5106 {
5107#pragma GCC diagnostic push
5108#pragma GCC diagnostic ignored "-Wliteral-suffix"
5109
5110#if __glibcxx_constexpr_string >= 201907L
5111# define _GLIBCXX_STRING_CONSTEXPR constexpr
5112#else
5113# define _GLIBCXX_STRING_CONSTEXPR
5114#endif
5115
5116 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
5117 inline basic_string<char>
5118 operator""s(const char* __str, size_t __len)
5119 { return basic_string<char>{__str, __len}; }
5120
5121 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
5122 inline basic_string<wchar_t>
5123 operator""s(const wchar_t* __str, size_t __len)
5124 { return basic_string<wchar_t>{__str, __len}; }
5125
5126#ifdef _GLIBCXX_USE_CHAR8_T
5127 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
5128 inline basic_string<char8_t>
5129 operator""s(const char8_t* __str, size_t __len)
5130 { return basic_string<char8_t>{__str, __len}; }
5131#endif
5132
5133 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
5134 inline basic_string<char16_t>
5135 operator""s(const char16_t* __str, size_t __len)
5136 { return basic_string<char16_t>{__str, __len}; }
5137
5138 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
5139 inline basic_string<char32_t>
5140 operator""s(const char32_t* __str, size_t __len)
5141 { return basic_string<char32_t>{__str, __len}; }
5142
5143#undef _GLIBCXX_STRING_CONSTEXPR
5144#pragma GCC diagnostic pop
5145 } // inline namespace string_literals
5146 } // inline namespace literals
5147#endif // __glibcxx_string_udls
5148
5149#ifdef __glibcxx_variant // >= C++17
5150 namespace __detail::__variant
5151 {
5152 template<typename> struct _Never_valueless_alt; // see <variant>
5153
5154 // Provide the strong exception-safety guarantee when emplacing a
5155 // basic_string into a variant, but only if moving the string cannot throw.
5156 template<typename _Tp, typename _Traits, typename _Alloc>
5157 struct _Never_valueless_alt<std::basic_string<_Tp, _Traits, _Alloc>>
5158 : __and_<
5159 is_nothrow_move_constructible<std::basic_string<_Tp, _Traits, _Alloc>>,
5160 is_nothrow_move_assignable<std::basic_string<_Tp, _Traits, _Alloc>>
5161 >::type
5162 { };
5163 } // namespace __detail::__variant
5164#endif // C++17
5165
5166_GLIBCXX_END_NAMESPACE_VERSION
5167} // namespace std
5168
5169#endif // C++11
5170
5171#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:2971
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition type_traits:124
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:1702
std::basic_ostream< _CharT, _Traits > & operator<<(std::basic_ostream< _CharT, _Traits > &__os, const bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition bitset:1801
ISO C++ inline namespace for literal suffixes.
initializer_list
Template class basic_istream.
Definition istream:73
A non-owning reference to a string.
Definition string_view:115
Primary class template hash.
is_pointer
Definition type_traits:653
is_nothrow_default_constructible
Definition type_traits:1354
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
char_type widen(char __c) const
Widens characters.
Definition basic_ios.h:465
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.
Template class basic_ostream.
Definition ostream.h:72
Provides input iterator semantics for streambufs.
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.