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