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