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