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