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