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