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