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