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