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