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