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