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