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