libstdc++
basic_string.h
Go to the documentation of this file.
1// Components for manipulating sequences of characters -*- C++ -*-
2
3// Copyright (C) 1997-2020 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
52namespace std _GLIBCXX_VISIBILITY(default)
53{
54_GLIBCXX_BEGIN_NAMESPACE_VERSION
55
56#if _GLIBCXX_USE_CXX11_ABI
57_GLIBCXX_BEGIN_NAMESPACE_CXX11
58 /**
59 * @class basic_string basic_string.h <string>
60 * @brief Managing sequences of characters and character-like objects.
61 *
62 * @ingroup strings
63 * @ingroup sequences
64 *
65 * @tparam _CharT Type of character
66 * @tparam _Traits Traits for character type, defaults to
67 * char_traits<_CharT>.
68 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
69 *
70 * Meets the requirements of a <a href="tables.html#65">container</a>, a
71 * <a href="tables.html#66">reversible container</a>, and a
72 * <a href="tables.html#67">sequence</a>. Of the
73 * <a href="tables.html#68">optional sequence requirements</a>, only
74 * @c push_back, @c at, and @c %array access are supported.
75 */
76 template<typename _CharT, typename _Traits, typename _Alloc>
77 class basic_string
78 {
80 rebind<_CharT>::other _Char_alloc_type;
82
83 // Types:
84 public:
85 typedef _Traits traits_type;
86 typedef typename _Traits::char_type value_type;
87 typedef _Char_alloc_type allocator_type;
88 typedef typename _Alloc_traits::size_type size_type;
89 typedef typename _Alloc_traits::difference_type difference_type;
90 typedef typename _Alloc_traits::reference reference;
91 typedef typename _Alloc_traits::const_reference const_reference;
92 typedef typename _Alloc_traits::pointer pointer;
93 typedef typename _Alloc_traits::const_pointer const_pointer;
94 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
95 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
96 const_iterator;
97 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
98 typedef std::reverse_iterator<iterator> reverse_iterator;
99
100 /// Value returned by various member functions when they fail.
101 static const size_type npos = static_cast<size_type>(-1);
102
103 protected:
104 // type used for positions in insert, erase etc.
105#if __cplusplus < 201103L
106 typedef iterator __const_iterator;
107#else
108 typedef const_iterator __const_iterator;
109#endif
110
111 private:
112#if __cplusplus >= 201703L
113 // A helper type for avoiding boiler-plate.
114 typedef basic_string_view<_CharT, _Traits> __sv_type;
115
116 template<typename _Tp, typename _Res>
117 using _If_sv = enable_if_t<
118 __and_<is_convertible<const _Tp&, __sv_type>,
119 __not_<is_convertible<const _Tp*, const basic_string*>>,
120 __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
121 _Res>;
122
123 // Allows an implicit conversion to __sv_type.
124 static __sv_type
125 _S_to_string_view(__sv_type __svt) noexcept
126 { return __svt; }
127
128 // Wraps a string_view by explicit conversion and thus
129 // allows to add an internal constructor that does not
130 // participate in overload resolution when a string_view
131 // is provided.
132 struct __sv_wrapper
133 {
134 explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
135 __sv_type _M_sv;
136 };
137
138 /**
139 * @brief Only internally used: Construct string from a string view
140 * wrapper.
141 * @param __svw string view wrapper.
142 * @param __a Allocator to use.
143 */
144 explicit
145 basic_string(__sv_wrapper __svw, const _Alloc& __a)
146 : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
147#endif
148
149 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
150 struct _Alloc_hider : allocator_type // TODO check __is_final
151 {
152#if __cplusplus < 201103L
153 _Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc())
154 : allocator_type(__a), _M_p(__dat) { }
155#else
156 _Alloc_hider(pointer __dat, const _Alloc& __a)
157 : allocator_type(__a), _M_p(__dat) { }
158
159 _Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc())
160 : allocator_type(std::move(__a)), _M_p(__dat) { }
161#endif
162
163 pointer _M_p; // The actual data.
164 };
165
166 _Alloc_hider _M_dataplus;
167 size_type _M_string_length;
168
169 enum { _S_local_capacity = 15 / sizeof(_CharT) };
170
171 union
172 {
173 _CharT _M_local_buf[_S_local_capacity + 1];
174 size_type _M_allocated_capacity;
175 };
176
177 void
178 _M_data(pointer __p)
179 { _M_dataplus._M_p = __p; }
180
181 void
182 _M_length(size_type __length)
183 { _M_string_length = __length; }
184
185 pointer
186 _M_data() const
187 { return _M_dataplus._M_p; }
188
189 pointer
190 _M_local_data()
191 {
192#if __cplusplus >= 201103L
193 return std::pointer_traits<pointer>::pointer_to(*_M_local_buf);
194#else
195 return pointer(_M_local_buf);
196#endif
197 }
198
199 const_pointer
200 _M_local_data() const
201 {
202#if __cplusplus >= 201103L
204#else
205 return const_pointer(_M_local_buf);
206#endif
207 }
208
209 void
210 _M_capacity(size_type __capacity)
211 { _M_allocated_capacity = __capacity; }
212
213 void
214 _M_set_length(size_type __n)
215 {
216 _M_length(__n);
217 traits_type::assign(_M_data()[__n], _CharT());
218 }
219
220 bool
221 _M_is_local() const
222 { return _M_data() == _M_local_data(); }
223
224 // Create & Destroy
225 pointer
226 _M_create(size_type&, size_type);
227
228 void
229 _M_dispose()
230 {
231 if (!_M_is_local())
232 _M_destroy(_M_allocated_capacity);
233 }
234
235 void
236 _M_destroy(size_type __size) throw()
237 { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); }
238
239 // _M_construct_aux is used to implement the 21.3.1 para 15 which
240 // requires special behaviour if _InIterator is an integral type
241 template<typename _InIterator>
242 void
243 _M_construct_aux(_InIterator __beg, _InIterator __end,
244 std::__false_type)
245 {
246 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
247 _M_construct(__beg, __end, _Tag());
248 }
249
250 // _GLIBCXX_RESOLVE_LIB_DEFECTS
251 // 438. Ambiguity in the "do the right thing" clause
252 template<typename _Integer>
253 void
254 _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
255 { _M_construct_aux_2(static_cast<size_type>(__beg), __end); }
256
257 void
258 _M_construct_aux_2(size_type __req, _CharT __c)
259 { _M_construct(__req, __c); }
260
261 template<typename _InIterator>
262 void
263 _M_construct(_InIterator __beg, _InIterator __end)
264 {
265 typedef typename std::__is_integer<_InIterator>::__type _Integral;
266 _M_construct_aux(__beg, __end, _Integral());
267 }
268
269 // For Input Iterators, used in istreambuf_iterators, etc.
270 template<typename _InIterator>
271 void
272 _M_construct(_InIterator __beg, _InIterator __end,
274
275 // For forward_iterators up to random_access_iterators, used for
276 // string::iterator, _CharT*, etc.
277 template<typename _FwdIterator>
278 void
279 _M_construct(_FwdIterator __beg, _FwdIterator __end,
281
282 void
283 _M_construct(size_type __req, _CharT __c);
284
285 allocator_type&
286 _M_get_allocator()
287 { return _M_dataplus; }
288
289 const allocator_type&
290 _M_get_allocator() const
291 { return _M_dataplus; }
292
293 private:
294
295#ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
296 // The explicit instantiations in misc-inst.cc require this due to
297 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
298 template<typename _Tp, bool _Requires =
299 !__are_same<_Tp, _CharT*>::__value
300 && !__are_same<_Tp, const _CharT*>::__value
301 && !__are_same<_Tp, iterator>::__value
302 && !__are_same<_Tp, const_iterator>::__value>
303 struct __enable_if_not_native_iterator
304 { typedef basic_string& __type; };
305 template<typename _Tp>
306 struct __enable_if_not_native_iterator<_Tp, false> { };
307#endif
308
309 size_type
310 _M_check(size_type __pos, const char* __s) const
311 {
312 if (__pos > this->size())
313 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
314 "this->size() (which is %zu)"),
315 __s, __pos, this->size());
316 return __pos;
317 }
318
319 void
320 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
321 {
322 if (this->max_size() - (this->size() - __n1) < __n2)
323 __throw_length_error(__N(__s));
324 }
325
326
327 // NB: _M_limit doesn't check for a bad __pos value.
328 size_type
329 _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
330 {
331 const bool __testoff = __off < this->size() - __pos;
332 return __testoff ? __off : this->size() - __pos;
333 }
334
335 // True if _Rep and source do not overlap.
336 bool
337 _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
338 {
339 return (less<const _CharT*>()(__s, _M_data())
340 || less<const _CharT*>()(_M_data() + this->size(), __s));
341 }
342
343 // When __n = 1 way faster than the general multichar
344 // traits_type::copy/move/assign.
345 static void
346 _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
347 {
348 if (__n == 1)
349 traits_type::assign(*__d, *__s);
350 else
351 traits_type::copy(__d, __s, __n);
352 }
353
354 static void
355 _S_move(_CharT* __d, const _CharT* __s, size_type __n)
356 {
357 if (__n == 1)
358 traits_type::assign(*__d, *__s);
359 else
360 traits_type::move(__d, __s, __n);
361 }
362
363 static void
364 _S_assign(_CharT* __d, size_type __n, _CharT __c)
365 {
366 if (__n == 1)
367 traits_type::assign(*__d, __c);
368 else
369 traits_type::assign(__d, __n, __c);
370 }
371
372 // _S_copy_chars is a separate template to permit specialization
373 // to optimize for the common case of pointers as iterators.
374 template<class _Iterator>
375 static void
376 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
377 {
378 for (; __k1 != __k2; ++__k1, (void)++__p)
379 traits_type::assign(*__p, *__k1); // These types are off.
380 }
381
382 static void
383 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
384 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
385
386 static void
387 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
388 _GLIBCXX_NOEXCEPT
389 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
390
391 static void
392 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
393 { _S_copy(__p, __k1, __k2 - __k1); }
394
395 static void
396 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
397 _GLIBCXX_NOEXCEPT
398 { _S_copy(__p, __k1, __k2 - __k1); }
399
400 static int
401 _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
402 {
403 const difference_type __d = difference_type(__n1 - __n2);
404
405 if (__d > __gnu_cxx::__numeric_traits<int>::__max)
406 return __gnu_cxx::__numeric_traits<int>::__max;
407 else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
408 return __gnu_cxx::__numeric_traits<int>::__min;
409 else
410 return int(__d);
411 }
412
413 void
414 _M_assign(const basic_string&);
415
416 void
417 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
418 size_type __len2);
419
420 void
421 _M_erase(size_type __pos, size_type __n);
422
423 public:
424 // Construct/copy/destroy:
425 // NB: We overload ctors in some cases instead of using default
426 // arguments, per 17.4.4.4 para. 2 item 2.
427
428 /**
429 * @brief Default constructor creates an empty string.
430 */
432 _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
433 : _M_dataplus(_M_local_data())
434 { _M_set_length(0); }
435
436 /**
437 * @brief Construct an empty string using allocator @a a.
438 */
439 explicit
440 basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
441 : _M_dataplus(_M_local_data(), __a)
442 { _M_set_length(0); }
443
444 /**
445 * @brief Construct string with copy of value of @a __str.
446 * @param __str Source string.
447 */
448 basic_string(const basic_string& __str)
449 : _M_dataplus(_M_local_data(),
450 _Alloc_traits::_S_select_on_copy(__str._M_get_allocator()))
451 { _M_construct(__str._M_data(), __str._M_data() + __str.length()); }
452
453 // _GLIBCXX_RESOLVE_LIB_DEFECTS
454 // 2583. no way to supply an allocator for basic_string(str, pos)
455 /**
456 * @brief Construct string as copy of a substring.
457 * @param __str Source string.
458 * @param __pos Index of first character to copy from.
459 * @param __a Allocator to use.
460 */
461 basic_string(const basic_string& __str, size_type __pos,
462 const _Alloc& __a = _Alloc())
463 : _M_dataplus(_M_local_data(), __a)
464 {
465 const _CharT* __start = __str._M_data()
466 + __str._M_check(__pos, "basic_string::basic_string");
467 _M_construct(__start, __start + __str._M_limit(__pos, npos));
468 }
469
470 /**
471 * @brief Construct string as copy of a substring.
472 * @param __str Source string.
473 * @param __pos Index of first character to copy from.
474 * @param __n Number of characters to copy.
475 */
476 basic_string(const basic_string& __str, size_type __pos,
477 size_type __n)
478 : _M_dataplus(_M_local_data())
479 {
480 const _CharT* __start = __str._M_data()
481 + __str._M_check(__pos, "basic_string::basic_string");
482 _M_construct(__start, __start + __str._M_limit(__pos, __n));
483 }
484
485 /**
486 * @brief Construct string as copy of a substring.
487 * @param __str Source string.
488 * @param __pos Index of first character to copy from.
489 * @param __n Number of characters to copy.
490 * @param __a Allocator to use.
491 */
492 basic_string(const basic_string& __str, size_type __pos,
493 size_type __n, const _Alloc& __a)
494 : _M_dataplus(_M_local_data(), __a)
495 {
496 const _CharT* __start
497 = __str._M_data() + __str._M_check(__pos, "string::string");
498 _M_construct(__start, __start + __str._M_limit(__pos, __n));
499 }
500
501 /**
502 * @brief Construct string initialized by a character %array.
503 * @param __s Source character %array.
504 * @param __n Number of characters to copy.
505 * @param __a Allocator to use (default is default allocator).
506 *
507 * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
508 * has no special meaning.
509 */
510 basic_string(const _CharT* __s, size_type __n,
511 const _Alloc& __a = _Alloc())
512 : _M_dataplus(_M_local_data(), __a)
513 { _M_construct(__s, __s + __n); }
514
515 /**
516 * @brief Construct string as copy of a C string.
517 * @param __s Source C string.
518 * @param __a Allocator to use (default is default allocator).
519 */
520#if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
521 // _GLIBCXX_RESOLVE_LIB_DEFECTS
522 // 3076. basic_string CTAD ambiguity
523 template<typename = _RequireAllocator<_Alloc>>
524#endif
525 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
526 : _M_dataplus(_M_local_data(), __a)
527 { _M_construct(__s, __s ? __s + traits_type::length(__s) : __s+npos); }
528
529 /**
530 * @brief Construct string as multiple characters.
531 * @param __n Number of characters.
532 * @param __c Character to use.
533 * @param __a Allocator to use (default is default allocator).
534 */
535#if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
536 // _GLIBCXX_RESOLVE_LIB_DEFECTS
537 // 3076. basic_string CTAD ambiguity
538 template<typename = _RequireAllocator<_Alloc>>
539#endif
540 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
541 : _M_dataplus(_M_local_data(), __a)
542 { _M_construct(__n, __c); }
543
544#if __cplusplus >= 201103L
545 /**
546 * @brief Move construct string.
547 * @param __str Source string.
548 *
549 * The newly-created string contains the exact contents of @a __str.
550 * @a __str is a valid, but unspecified string.
551 **/
552 basic_string(basic_string&& __str) noexcept
553 : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator()))
554 {
555 if (__str._M_is_local())
556 {
557 traits_type::copy(_M_local_buf, __str._M_local_buf,
558 _S_local_capacity + 1);
559 }
560 else
561 {
562 _M_data(__str._M_data());
563 _M_capacity(__str._M_allocated_capacity);
564 }
565
566 // Must use _M_length() here not _M_set_length() because
567 // basic_stringbuf relies on writing into unallocated capacity so
568 // we mess up the contents if we put a '\0' in the string.
569 _M_length(__str.length());
570 __str._M_data(__str._M_local_data());
571 __str._M_set_length(0);
572 }
573
574 /**
575 * @brief Construct string from an initializer %list.
576 * @param __l std::initializer_list of characters.
577 * @param __a Allocator to use (default is default allocator).
578 */
579 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
580 : _M_dataplus(_M_local_data(), __a)
581 { _M_construct(__l.begin(), __l.end()); }
582
583 basic_string(const basic_string& __str, const _Alloc& __a)
584 : _M_dataplus(_M_local_data(), __a)
585 { _M_construct(__str.begin(), __str.end()); }
586
587 basic_string(basic_string&& __str, const _Alloc& __a)
588 noexcept(_Alloc_traits::_S_always_equal())
589 : _M_dataplus(_M_local_data(), __a)
590 {
591 if (__str._M_is_local())
592 {
593 traits_type::copy(_M_local_buf, __str._M_local_buf,
594 _S_local_capacity + 1);
595 _M_length(__str.length());
596 __str._M_set_length(0);
597 }
598 else if (_Alloc_traits::_S_always_equal()
599 || __str.get_allocator() == __a)
600 {
601 _M_data(__str._M_data());
602 _M_length(__str.length());
603 _M_capacity(__str._M_allocated_capacity);
604 __str._M_data(__str._M_local_buf);
605 __str._M_set_length(0);
606 }
607 else
608 _M_construct(__str.begin(), __str.end());
609 }
610
611#endif // C++11
612
613 /**
614 * @brief Construct string as copy of a range.
615 * @param __beg Start of range.
616 * @param __end End of range.
617 * @param __a Allocator to use (default is default allocator).
618 */
619#if __cplusplus >= 201103L
620 template<typename _InputIterator,
621 typename = std::_RequireInputIter<_InputIterator>>
622#else
623 template<typename _InputIterator>
624#endif
625 basic_string(_InputIterator __beg, _InputIterator __end,
626 const _Alloc& __a = _Alloc())
627 : _M_dataplus(_M_local_data(), __a)
628 { _M_construct(__beg, __end); }
629
630#if __cplusplus >= 201703L
631 /**
632 * @brief Construct string from a substring of a string_view.
633 * @param __t Source object convertible to string view.
634 * @param __pos The index of the first character to copy from __t.
635 * @param __n The number of characters to copy from __t.
636 * @param __a Allocator to use.
637 */
638 template<typename _Tp, typename = _If_sv<_Tp, void>>
639 basic_string(const _Tp& __t, size_type __pos, size_type __n,
640 const _Alloc& __a = _Alloc())
641 : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
642
643 /**
644 * @brief Construct string from a string_view.
645 * @param __t Source object convertible to string view.
646 * @param __a Allocator to use (default is default allocator).
647 */
648 template<typename _Tp, typename = _If_sv<_Tp, void>>
649 explicit
650 basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
651 : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
652#endif // C++17
653
654 /**
655 * @brief Destroy the string instance.
656 */
658 { _M_dispose(); }
659
660 /**
661 * @brief Assign the value of @a str to this string.
662 * @param __str Source string.
663 */
665 operator=(const basic_string& __str)
666 {
667 return this->assign(__str);
668 }
669
670 /**
671 * @brief Copy contents of @a s into this string.
672 * @param __s Source null-terminated string.
673 */
675 operator=(const _CharT* __s)
676 { return this->assign(__s); }
677
678 /**
679 * @brief Set value to string of length 1.
680 * @param __c Source character.
681 *
682 * Assigning to a character makes this string length 1 and
683 * (*this)[0] == @a c.
684 */
686 operator=(_CharT __c)
687 {
688 this->assign(1, __c);
689 return *this;
690 }
691
692#if __cplusplus >= 201103L
693 /**
694 * @brief Move assign the value of @a str to this string.
695 * @param __str Source string.
696 *
697 * The contents of @a str are moved into this string (without copying).
698 * @a str is a valid, but unspecified string.
699 **/
700 // _GLIBCXX_RESOLVE_LIB_DEFECTS
701 // 2063. Contradictory requirements for string move assignment
703 operator=(basic_string&& __str)
704 noexcept(_Alloc_traits::_S_nothrow_move())
705 {
706 if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
707 && !_Alloc_traits::_S_always_equal()
708 && _M_get_allocator() != __str._M_get_allocator())
709 {
710 // Destroy existing storage before replacing allocator.
711 _M_destroy(_M_allocated_capacity);
712 _M_data(_M_local_data());
713 _M_set_length(0);
714 }
715 // Replace allocator if POCMA is true.
716 std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator());
717
718 if (__str._M_is_local())
719 {
720 // We've always got room for a short string, just copy it.
721 if (__str.size())
722 this->_S_copy(_M_data(), __str._M_data(), __str.size());
723 _M_set_length(__str.size());
724 }
725 else if (_Alloc_traits::_S_propagate_on_move_assign()
726 || _Alloc_traits::_S_always_equal()
727 || _M_get_allocator() == __str._M_get_allocator())
728 {
729 // Just move the allocated pointer, our allocator can free it.
730 pointer __data = nullptr;
731 size_type __capacity;
732 if (!_M_is_local())
733 {
734 if (_Alloc_traits::_S_always_equal())
735 {
736 // __str can reuse our existing storage.
737 __data = _M_data();
738 __capacity = _M_allocated_capacity;
739 }
740 else // __str can't use it, so free it.
741 _M_destroy(_M_allocated_capacity);
742 }
743
744 _M_data(__str._M_data());
745 _M_length(__str.length());
746 _M_capacity(__str._M_allocated_capacity);
747 if (__data)
748 {
749 __str._M_data(__data);
750 __str._M_capacity(__capacity);
751 }
752 else
753 __str._M_data(__str._M_local_buf);
754 }
755 else // Need to do a deep copy
756 assign(__str);
757 __str.clear();
758 return *this;
759 }
760
761 /**
762 * @brief Set value to string constructed from initializer %list.
763 * @param __l std::initializer_list.
764 */
766 operator=(initializer_list<_CharT> __l)
767 {
768 this->assign(__l.begin(), __l.size());
769 return *this;
770 }
771#endif // C++11
772
773#if __cplusplus >= 201703L
774 /**
775 * @brief Set value to string constructed from a string_view.
776 * @param __svt An object convertible to string_view.
777 */
778 template<typename _Tp>
779 _If_sv<_Tp, basic_string&>
780 operator=(const _Tp& __svt)
781 { return this->assign(__svt); }
782
783 /**
784 * @brief Convert to a string_view.
785 * @return A string_view.
786 */
787 operator __sv_type() const noexcept
788 { return __sv_type(data(), size()); }
789#endif // C++17
790
791 // Iterators:
792 /**
793 * Returns a read/write iterator that points to the first character in
794 * the %string.
795 */
796 iterator
797 begin() _GLIBCXX_NOEXCEPT
798 { return iterator(_M_data()); }
799
800 /**
801 * Returns a read-only (constant) iterator that points to the first
802 * character in the %string.
803 */
804 const_iterator
805 begin() const _GLIBCXX_NOEXCEPT
806 { return const_iterator(_M_data()); }
807
808 /**
809 * Returns a read/write iterator that points one past the last
810 * character in the %string.
811 */
812 iterator
813 end() _GLIBCXX_NOEXCEPT
814 { return iterator(_M_data() + this->size()); }
815
816 /**
817 * Returns a read-only (constant) iterator that points one past the
818 * last character in the %string.
819 */
820 const_iterator
821 end() const _GLIBCXX_NOEXCEPT
822 { return const_iterator(_M_data() + this->size()); }
823
824 /**
825 * Returns a read/write reverse iterator that points to the last
826 * character in the %string. Iteration is done in reverse element
827 * order.
828 */
829 reverse_iterator
830 rbegin() _GLIBCXX_NOEXCEPT
831 { return reverse_iterator(this->end()); }
832
833 /**
834 * Returns a read-only (constant) reverse iterator that points
835 * to the last character in the %string. Iteration is done in
836 * reverse element order.
837 */
838 const_reverse_iterator
839 rbegin() const _GLIBCXX_NOEXCEPT
840 { return const_reverse_iterator(this->end()); }
841
842 /**
843 * Returns a read/write reverse iterator that points to one before the
844 * first character in the %string. Iteration is done in reverse
845 * element order.
846 */
847 reverse_iterator
848 rend() _GLIBCXX_NOEXCEPT
849 { return reverse_iterator(this->begin()); }
850
851 /**
852 * Returns a read-only (constant) reverse iterator that points
853 * to one before the first character in the %string. Iteration
854 * is done in reverse element order.
855 */
856 const_reverse_iterator
857 rend() const _GLIBCXX_NOEXCEPT
858 { return const_reverse_iterator(this->begin()); }
859
860#if __cplusplus >= 201103L
861 /**
862 * Returns a read-only (constant) iterator that points to the first
863 * character in the %string.
864 */
865 const_iterator
866 cbegin() const noexcept
867 { return const_iterator(this->_M_data()); }
868
869 /**
870 * Returns a read-only (constant) iterator that points one past the
871 * last character in the %string.
872 */
873 const_iterator
874 cend() const noexcept
875 { return const_iterator(this->_M_data() + this->size()); }
876
877 /**
878 * Returns a read-only (constant) reverse iterator that points
879 * to the last character in the %string. Iteration is done in
880 * reverse element order.
881 */
882 const_reverse_iterator
883 crbegin() const noexcept
884 { return const_reverse_iterator(this->end()); }
885
886 /**
887 * Returns a read-only (constant) reverse iterator that points
888 * to one before the first character in the %string. Iteration
889 * is done in reverse element order.
890 */
891 const_reverse_iterator
892 crend() const noexcept
893 { return const_reverse_iterator(this->begin()); }
894#endif
895
896 public:
897 // Capacity:
898 /// Returns the number of characters in the string, not including any
899 /// null-termination.
900 size_type
901 size() const _GLIBCXX_NOEXCEPT
902 { return _M_string_length; }
903
904 /// Returns the number of characters in the string, not including any
905 /// null-termination.
906 size_type
907 length() const _GLIBCXX_NOEXCEPT
908 { return _M_string_length; }
909
910 /// Returns the size() of the largest possible %string.
911 size_type
912 max_size() const _GLIBCXX_NOEXCEPT
913 { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
914
915 /**
916 * @brief Resizes the %string to the specified number of characters.
917 * @param __n Number of characters the %string should contain.
918 * @param __c Character to fill any new elements.
919 *
920 * This function will %resize the %string to the specified
921 * number of characters. If the number is smaller than the
922 * %string's current size the %string is truncated, otherwise
923 * the %string is extended and new elements are %set to @a __c.
924 */
925 void
926 resize(size_type __n, _CharT __c);
927
928 /**
929 * @brief Resizes the %string to the specified number of characters.
930 * @param __n Number of characters the %string should contain.
931 *
932 * This function will resize the %string to the specified length. If
933 * the new size is smaller than the %string's current size the %string
934 * is truncated, otherwise the %string is extended and new characters
935 * are default-constructed. For basic types such as char, this means
936 * setting them to 0.
937 */
938 void
939 resize(size_type __n)
940 { this->resize(__n, _CharT()); }
941
942#if __cplusplus >= 201103L
943 /// A non-binding request to reduce capacity() to size().
944 void
945 shrink_to_fit() noexcept
946 {
947#if __cpp_exceptions
948 if (capacity() > size())
949 {
950 try
951 { reserve(0); }
952 catch(...)
953 { }
954 }
955#endif
956 }
957#endif
958
959 /**
960 * Returns the total number of characters that the %string can hold
961 * before needing to allocate more memory.
962 */
963 size_type
964 capacity() const _GLIBCXX_NOEXCEPT
965 {
966 return _M_is_local() ? size_type(_S_local_capacity)
967 : _M_allocated_capacity;
968 }
969
970 /**
971 * @brief Attempt to preallocate enough memory for specified number of
972 * characters.
973 * @param __res_arg Number of characters required.
974 * @throw std::length_error If @a __res_arg exceeds @c max_size().
975 *
976 * This function attempts to reserve enough memory for the
977 * %string to hold the specified number of characters. If the
978 * number requested is more than max_size(), length_error is
979 * thrown.
980 *
981 * The advantage of this function is that if optimal code is a
982 * necessity and the user can determine the string length that will be
983 * required, the user can reserve the memory in %advance, and thus
984 * prevent a possible reallocation of memory and copying of %string
985 * data.
986 */
987 void
988 reserve(size_type __res_arg = 0);
989
990 /**
991 * Erases the string, making it empty.
992 */
993 void
994 clear() _GLIBCXX_NOEXCEPT
995 { _M_set_length(0); }
996
997 /**
998 * Returns true if the %string is empty. Equivalent to
999 * <code>*this == ""</code>.
1000 */
1001 _GLIBCXX_NODISCARD bool
1002 empty() const _GLIBCXX_NOEXCEPT
1003 { return this->size() == 0; }
1004
1005 // Element access:
1006 /**
1007 * @brief Subscript access to the data contained in the %string.
1008 * @param __pos The index of the character to access.
1009 * @return Read-only (constant) reference to the character.
1010 *
1011 * This operator allows for easy, array-style, data access.
1012 * Note that data access with this operator is unchecked and
1013 * out_of_range lookups are not defined. (For checked lookups
1014 * see at().)
1015 */
1016 const_reference
1017 operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
1018 {
1019 __glibcxx_assert(__pos <= size());
1020 return _M_data()[__pos];
1021 }
1022
1023 /**
1024 * @brief Subscript access to the data contained in the %string.
1025 * @param __pos The index of the character to access.
1026 * @return Read/write reference to the character.
1027 *
1028 * This operator allows for easy, array-style, data access.
1029 * Note that data access with this operator is unchecked and
1030 * out_of_range lookups are not defined. (For checked lookups
1031 * see at().)
1032 */
1033 reference
1034 operator[](size_type __pos)
1035 {
1036 // Allow pos == size() both in C++98 mode, as v3 extension,
1037 // and in C++11 mode.
1038 __glibcxx_assert(__pos <= size());
1039 // In pedantic mode be strict in C++98 mode.
1040 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
1041 return _M_data()[__pos];
1042 }
1043
1044 /**
1045 * @brief Provides access to the data contained in the %string.
1046 * @param __n The index of the character to access.
1047 * @return Read-only (const) reference to the character.
1048 * @throw std::out_of_range If @a n is an invalid index.
1049 *
1050 * This function provides for safer data access. The parameter is
1051 * first checked that it is in the range of the string. The function
1052 * throws out_of_range if the check fails.
1053 */
1054 const_reference
1055 at(size_type __n) const
1056 {
1057 if (__n >= this->size())
1058 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1059 "(which is %zu) >= this->size() "
1060 "(which is %zu)"),
1061 __n, this->size());
1062 return _M_data()[__n];
1063 }
1064
1065 /**
1066 * @brief Provides access to the data contained in the %string.
1067 * @param __n The index of the character to access.
1068 * @return Read/write reference to the character.
1069 * @throw std::out_of_range If @a n is an invalid index.
1070 *
1071 * This function provides for safer data access. The parameter is
1072 * first checked that it is in the range of the string. The function
1073 * throws out_of_range if the check fails.
1074 */
1075 reference
1076 at(size_type __n)
1077 {
1078 if (__n >= size())
1079 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1080 "(which is %zu) >= this->size() "
1081 "(which is %zu)"),
1082 __n, this->size());
1083 return _M_data()[__n];
1084 }
1085
1086#if __cplusplus >= 201103L
1087 /**
1088 * Returns a read/write reference to the data at the first
1089 * element of the %string.
1090 */
1091 reference
1092 front() noexcept
1093 {
1094 __glibcxx_assert(!empty());
1095 return operator[](0);
1096 }
1097
1098 /**
1099 * Returns a read-only (constant) reference to the data at the first
1100 * element of the %string.
1101 */
1102 const_reference
1103 front() const noexcept
1104 {
1105 __glibcxx_assert(!empty());
1106 return operator[](0);
1107 }
1108
1109 /**
1110 * Returns a read/write reference to the data at the last
1111 * element of the %string.
1112 */
1113 reference
1114 back() noexcept
1115 {
1116 __glibcxx_assert(!empty());
1117 return operator[](this->size() - 1);
1118 }
1119
1120 /**
1121 * Returns a read-only (constant) reference to the data at the
1122 * last element of the %string.
1123 */
1124 const_reference
1125 back() const noexcept
1126 {
1127 __glibcxx_assert(!empty());
1128 return operator[](this->size() - 1);
1129 }
1130#endif
1131
1132 // Modifiers:
1133 /**
1134 * @brief Append a string to this string.
1135 * @param __str The string to append.
1136 * @return Reference to this string.
1137 */
1139 operator+=(const basic_string& __str)
1140 { return this->append(__str); }
1141
1142 /**
1143 * @brief Append a C string.
1144 * @param __s The C string to append.
1145 * @return Reference to this string.
1146 */
1148 operator+=(const _CharT* __s)
1149 { return this->append(__s); }
1150
1151 /**
1152 * @brief Append a character.
1153 * @param __c The character to append.
1154 * @return Reference to this string.
1155 */
1157 operator+=(_CharT __c)
1158 {
1159 this->push_back(__c);
1160 return *this;
1161 }
1162
1163#if __cplusplus >= 201103L
1164 /**
1165 * @brief Append an initializer_list of characters.
1166 * @param __l The initializer_list of characters to be appended.
1167 * @return Reference to this string.
1168 */
1170 operator+=(initializer_list<_CharT> __l)
1171 { return this->append(__l.begin(), __l.size()); }
1172#endif // C++11
1173
1174#if __cplusplus >= 201703L
1175 /**
1176 * @brief Append a string_view.
1177 * @param __svt An object convertible to string_view to be appended.
1178 * @return Reference to this string.
1179 */
1180 template<typename _Tp>
1181 _If_sv<_Tp, basic_string&>
1182 operator+=(const _Tp& __svt)
1183 { return this->append(__svt); }
1184#endif // C++17
1185
1186 /**
1187 * @brief Append a string to this string.
1188 * @param __str The string to append.
1189 * @return Reference to this string.
1190 */
1192 append(const basic_string& __str)
1193 { return _M_append(__str._M_data(), __str.size()); }
1194
1195 /**
1196 * @brief Append a substring.
1197 * @param __str The string to append.
1198 * @param __pos Index of the first character of str to append.
1199 * @param __n The number of characters to append.
1200 * @return Reference to this string.
1201 * @throw std::out_of_range if @a __pos is not a valid index.
1202 *
1203 * This function appends @a __n characters from @a __str
1204 * starting at @a __pos to this string. If @a __n is is larger
1205 * than the number of available characters in @a __str, the
1206 * remainder of @a __str is appended.
1207 */
1209 append(const basic_string& __str, size_type __pos, size_type __n = npos)
1210 { return _M_append(__str._M_data()
1211 + __str._M_check(__pos, "basic_string::append"),
1212 __str._M_limit(__pos, __n)); }
1213
1214 /**
1215 * @brief Append a C substring.
1216 * @param __s The C string to append.
1217 * @param __n The number of characters to append.
1218 * @return Reference to this string.
1219 */
1221 append(const _CharT* __s, size_type __n)
1222 {
1223 __glibcxx_requires_string_len(__s, __n);
1224 _M_check_length(size_type(0), __n, "basic_string::append");
1225 return _M_append(__s, __n);
1226 }
1227
1228 /**
1229 * @brief Append a C string.
1230 * @param __s The C string to append.
1231 * @return Reference to this string.
1232 */
1234 append(const _CharT* __s)
1235 {
1236 __glibcxx_requires_string(__s);
1237 const size_type __n = traits_type::length(__s);
1238 _M_check_length(size_type(0), __n, "basic_string::append");
1239 return _M_append(__s, __n);
1240 }
1241
1242 /**
1243 * @brief Append multiple characters.
1244 * @param __n The number of characters to append.
1245 * @param __c The character to use.
1246 * @return Reference to this string.
1247 *
1248 * Appends __n copies of __c to this string.
1249 */
1251 append(size_type __n, _CharT __c)
1252 { return _M_replace_aux(this->size(), size_type(0), __n, __c); }
1253
1254#if __cplusplus >= 201103L
1255 /**
1256 * @brief Append an initializer_list of characters.
1257 * @param __l The initializer_list of characters to append.
1258 * @return Reference to this string.
1259 */
1261 append(initializer_list<_CharT> __l)
1262 { return this->append(__l.begin(), __l.size()); }
1263#endif // C++11
1264
1265 /**
1266 * @brief Append a range of characters.
1267 * @param __first Iterator referencing the first character to append.
1268 * @param __last Iterator marking the end of the range.
1269 * @return Reference to this string.
1270 *
1271 * Appends characters in the range [__first,__last) to this string.
1272 */
1273#if __cplusplus >= 201103L
1274 template<class _InputIterator,
1275 typename = std::_RequireInputIter<_InputIterator>>
1276#else
1277 template<class _InputIterator>
1278#endif
1280 append(_InputIterator __first, _InputIterator __last)
1281 { return this->replace(end(), end(), __first, __last); }
1282
1283#if __cplusplus >= 201703L
1284 /**
1285 * @brief Append a string_view.
1286 * @param __svt An object convertible to string_view to be appended.
1287 * @return Reference to this string.
1288 */
1289 template<typename _Tp>
1290 _If_sv<_Tp, basic_string&>
1291 append(const _Tp& __svt)
1292 {
1293 __sv_type __sv = __svt;
1294 return this->append(__sv.data(), __sv.size());
1295 }
1296
1297 /**
1298 * @brief Append a range of characters from a string_view.
1299 * @param __svt An object convertible to string_view to be appended from.
1300 * @param __pos The position in the string_view to append from.
1301 * @param __n The number of characters to append from the string_view.
1302 * @return Reference to this string.
1303 */
1304 template<typename _Tp>
1305 _If_sv<_Tp, basic_string&>
1306 append(const _Tp& __svt, size_type __pos, size_type __n = npos)
1307 {
1308 __sv_type __sv = __svt;
1309 return _M_append(__sv.data()
1310 + std::__sv_check(__sv.size(), __pos, "basic_string::append"),
1311 std::__sv_limit(__sv.size(), __pos, __n));
1312 }
1313#endif // C++17
1314
1315 /**
1316 * @brief Append a single character.
1317 * @param __c Character to append.
1318 */
1319 void
1320 push_back(_CharT __c)
1321 {
1322 const size_type __size = this->size();
1323 if (__size + 1 > this->capacity())
1324 this->_M_mutate(__size, size_type(0), 0, size_type(1));
1325 traits_type::assign(this->_M_data()[__size], __c);
1326 this->_M_set_length(__size + 1);
1327 }
1328
1329 /**
1330 * @brief Set value to contents of another string.
1331 * @param __str Source string to use.
1332 * @return Reference to this string.
1333 */
1335 assign(const basic_string& __str)
1336 {
1337#if __cplusplus >= 201103L
1338 if (_Alloc_traits::_S_propagate_on_copy_assign())
1339 {
1340 if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
1341 && _M_get_allocator() != __str._M_get_allocator())
1342 {
1343 // Propagating allocator cannot free existing storage so must
1344 // deallocate it before replacing current allocator.
1345 if (__str.size() <= _S_local_capacity)
1346 {
1347 _M_destroy(_M_allocated_capacity);
1348 _M_data(_M_local_data());
1349 _M_set_length(0);
1350 }
1351 else
1352 {
1353 const auto __len = __str.size();
1354 auto __alloc = __str._M_get_allocator();
1355 // If this allocation throws there are no effects:
1356 auto __ptr = _Alloc_traits::allocate(__alloc, __len + 1);
1357 _M_destroy(_M_allocated_capacity);
1358 _M_data(__ptr);
1359 _M_capacity(__len);
1360 _M_set_length(__len);
1361 }
1362 }
1363 std::__alloc_on_copy(_M_get_allocator(), __str._M_get_allocator());
1364 }
1365#endif
1366 this->_M_assign(__str);
1367 return *this;
1368 }
1369
1370#if __cplusplus >= 201103L
1371 /**
1372 * @brief Set value to contents of another string.
1373 * @param __str Source string to use.
1374 * @return Reference to this string.
1375 *
1376 * This function sets this string to the exact contents of @a __str.
1377 * @a __str is a valid, but unspecified string.
1378 */
1380 assign(basic_string&& __str)
1381 noexcept(_Alloc_traits::_S_nothrow_move())
1382 {
1383 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1384 // 2063. Contradictory requirements for string move assignment
1385 return *this = std::move(__str);
1386 }
1387#endif // C++11
1388
1389 /**
1390 * @brief Set value to a substring of a string.
1391 * @param __str The string to use.
1392 * @param __pos Index of the first character of str.
1393 * @param __n Number of characters to use.
1394 * @return Reference to this string.
1395 * @throw std::out_of_range if @a pos is not a valid index.
1396 *
1397 * This function sets this string to the substring of @a __str
1398 * consisting of @a __n characters at @a __pos. If @a __n is
1399 * is larger than the number of available characters in @a
1400 * __str, the remainder of @a __str is used.
1401 */
1403 assign(const basic_string& __str, size_type __pos, size_type __n = npos)
1404 { return _M_replace(size_type(0), this->size(), __str._M_data()
1405 + __str._M_check(__pos, "basic_string::assign"),
1406 __str._M_limit(__pos, __n)); }
1407
1408 /**
1409 * @brief Set value to a C substring.
1410 * @param __s The C string to use.
1411 * @param __n Number of characters to use.
1412 * @return Reference to this string.
1413 *
1414 * This function sets the value of this string to the first @a __n
1415 * characters of @a __s. If @a __n is is larger than the number of
1416 * available characters in @a __s, the remainder of @a __s is used.
1417 */
1419 assign(const _CharT* __s, size_type __n)
1420 {
1421 __glibcxx_requires_string_len(__s, __n);
1422 return _M_replace(size_type(0), this->size(), __s, __n);
1423 }
1424
1425 /**
1426 * @brief Set value to contents of a C string.
1427 * @param __s The C string to use.
1428 * @return Reference to this string.
1429 *
1430 * This function sets the value of this string to the value of @a __s.
1431 * The data is copied, so there is no dependence on @a __s once the
1432 * function returns.
1433 */
1435 assign(const _CharT* __s)
1436 {
1437 __glibcxx_requires_string(__s);
1438 return _M_replace(size_type(0), this->size(), __s,
1439 traits_type::length(__s));
1440 }
1441
1442 /**
1443 * @brief Set value to multiple characters.
1444 * @param __n Length of the resulting string.
1445 * @param __c The character to use.
1446 * @return Reference to this string.
1447 *
1448 * This function sets the value of this string to @a __n copies of
1449 * character @a __c.
1450 */
1452 assign(size_type __n, _CharT __c)
1453 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
1454
1455 /**
1456 * @brief Set value to a range of characters.
1457 * @param __first Iterator referencing the first character to append.
1458 * @param __last Iterator marking the end of the range.
1459 * @return Reference to this string.
1460 *
1461 * Sets value of string to characters in the range [__first,__last).
1462 */
1463#if __cplusplus >= 201103L
1464 template<class _InputIterator,
1465 typename = std::_RequireInputIter<_InputIterator>>
1466#else
1467 template<class _InputIterator>
1468#endif
1470 assign(_InputIterator __first, _InputIterator __last)
1471 { return this->replace(begin(), end(), __first, __last); }
1472
1473#if __cplusplus >= 201103L
1474 /**
1475 * @brief Set value to an initializer_list of characters.
1476 * @param __l The initializer_list of characters to assign.
1477 * @return Reference to this string.
1478 */
1480 assign(initializer_list<_CharT> __l)
1481 { return this->assign(__l.begin(), __l.size()); }
1482#endif // C++11
1483
1484#if __cplusplus >= 201703L
1485 /**
1486 * @brief Set value from a string_view.
1487 * @param __svt The source object convertible to string_view.
1488 * @return Reference to this string.
1489 */
1490 template<typename _Tp>
1491 _If_sv<_Tp, basic_string&>
1492 assign(const _Tp& __svt)
1493 {
1494 __sv_type __sv = __svt;
1495 return this->assign(__sv.data(), __sv.size());
1496 }
1497
1498 /**
1499 * @brief Set value from a range of characters in a string_view.
1500 * @param __svt The source object convertible to string_view.
1501 * @param __pos The position in the string_view to assign from.
1502 * @param __n The number of characters to assign.
1503 * @return Reference to this string.
1504 */
1505 template<typename _Tp>
1506 _If_sv<_Tp, basic_string&>
1507 assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
1508 {
1509 __sv_type __sv = __svt;
1510 return _M_replace(size_type(0), this->size(),
1511 __sv.data()
1512 + std::__sv_check(__sv.size(), __pos, "basic_string::assign"),
1513 std::__sv_limit(__sv.size(), __pos, __n));
1514 }
1515#endif // C++17
1516
1517#if __cplusplus >= 201103L
1518 /**
1519 * @brief Insert multiple characters.
1520 * @param __p Const_iterator referencing location in string to
1521 * insert at.
1522 * @param __n Number of characters to insert
1523 * @param __c The character to insert.
1524 * @return Iterator referencing the first inserted char.
1525 * @throw std::length_error If new length exceeds @c max_size().
1526 *
1527 * Inserts @a __n copies of character @a __c starting at the
1528 * position referenced by iterator @a __p. If adding
1529 * characters causes the length to exceed max_size(),
1530 * length_error is thrown. The value of the string doesn't
1531 * change if an error is thrown.
1532 */
1533 iterator
1534 insert(const_iterator __p, size_type __n, _CharT __c)
1535 {
1536 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1537 const size_type __pos = __p - begin();
1538 this->replace(__p, __p, __n, __c);
1539 return iterator(this->_M_data() + __pos);
1540 }
1541#else
1542 /**
1543 * @brief Insert multiple characters.
1544 * @param __p Iterator referencing location in string to insert at.
1545 * @param __n Number of characters to insert
1546 * @param __c The character to insert.
1547 * @throw std::length_error If new length exceeds @c max_size().
1548 *
1549 * Inserts @a __n copies of character @a __c starting at the
1550 * position referenced by iterator @a __p. If adding
1551 * characters causes the length to exceed max_size(),
1552 * length_error is thrown. The value of the string doesn't
1553 * change if an error is thrown.
1554 */
1555 void
1556 insert(iterator __p, size_type __n, _CharT __c)
1557 { this->replace(__p, __p, __n, __c); }
1558#endif
1559
1560#if __cplusplus >= 201103L
1561 /**
1562 * @brief Insert a range of characters.
1563 * @param __p Const_iterator referencing location in string to
1564 * insert at.
1565 * @param __beg Start of range.
1566 * @param __end End of range.
1567 * @return Iterator referencing the first inserted char.
1568 * @throw std::length_error If new length exceeds @c max_size().
1569 *
1570 * Inserts characters in range [beg,end). If adding characters
1571 * causes the length to exceed max_size(), length_error is
1572 * thrown. The value of the string doesn't change if an error
1573 * is thrown.
1574 */
1575 template<class _InputIterator,
1576 typename = std::_RequireInputIter<_InputIterator>>
1577 iterator
1578 insert(const_iterator __p, _InputIterator __beg, _InputIterator __end)
1579 {
1580 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1581 const size_type __pos = __p - begin();
1582 this->replace(__p, __p, __beg, __end);
1583 return iterator(this->_M_data() + __pos);
1584 }
1585#else
1586 /**
1587 * @brief Insert a range of characters.
1588 * @param __p Iterator referencing location in string to insert at.
1589 * @param __beg Start of range.
1590 * @param __end End of range.
1591 * @throw std::length_error If new length exceeds @c max_size().
1592 *
1593 * Inserts characters in range [__beg,__end). If adding
1594 * characters causes the length to exceed max_size(),
1595 * length_error is thrown. The value of the string doesn't
1596 * change if an error is thrown.
1597 */
1598 template<class _InputIterator>
1599 void
1600 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
1601 { this->replace(__p, __p, __beg, __end); }
1602#endif
1603
1604#if __cplusplus >= 201103L
1605 /**
1606 * @brief Insert an initializer_list of characters.
1607 * @param __p Iterator referencing location in string to insert at.
1608 * @param __l The initializer_list of characters to insert.
1609 * @throw std::length_error If new length exceeds @c max_size().
1610 */
1611 iterator
1612 insert(const_iterator __p, initializer_list<_CharT> __l)
1613 { return this->insert(__p, __l.begin(), __l.end()); }
1614
1615#ifdef _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
1616 // See PR libstdc++/83328
1617 void
1618 insert(iterator __p, initializer_list<_CharT> __l)
1619 {
1620 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1621 this->insert(__p - begin(), __l.begin(), __l.size());
1622 }
1623#endif
1624#endif // C++11
1625
1626 /**
1627 * @brief Insert value of a string.
1628 * @param __pos1 Position in string to insert at.
1629 * @param __str The string to insert.
1630 * @return Reference to this string.
1631 * @throw std::length_error If new length exceeds @c max_size().
1632 *
1633 * Inserts value of @a __str starting at @a __pos1. If adding
1634 * characters causes the length to exceed max_size(),
1635 * length_error is thrown. The value of the string doesn't
1636 * change if an error is thrown.
1637 */
1639 insert(size_type __pos1, const basic_string& __str)
1640 { return this->replace(__pos1, size_type(0),
1641 __str._M_data(), __str.size()); }
1642
1643 /**
1644 * @brief Insert a substring.
1645 * @param __pos1 Position in string to insert at.
1646 * @param __str The string to insert.
1647 * @param __pos2 Start of characters in str to insert.
1648 * @param __n Number of characters to insert.
1649 * @return Reference to this string.
1650 * @throw std::length_error If new length exceeds @c max_size().
1651 * @throw std::out_of_range If @a pos1 > size() or
1652 * @a __pos2 > @a str.size().
1653 *
1654 * Starting at @a pos1, insert @a __n character of @a __str
1655 * beginning with @a __pos2. If adding characters causes the
1656 * length to exceed max_size(), length_error is thrown. If @a
1657 * __pos1 is beyond the end of this string or @a __pos2 is
1658 * beyond the end of @a __str, out_of_range is thrown. The
1659 * value of the string doesn't change if an error is thrown.
1660 */
1662 insert(size_type __pos1, const basic_string& __str,
1663 size_type __pos2, size_type __n = npos)
1664 { return this->replace(__pos1, size_type(0), __str._M_data()
1665 + __str._M_check(__pos2, "basic_string::insert"),
1666 __str._M_limit(__pos2, __n)); }
1667
1668 /**
1669 * @brief Insert a C substring.
1670 * @param __pos Position in string to insert at.
1671 * @param __s The C string to insert.
1672 * @param __n The number of characters to insert.
1673 * @return Reference to this string.
1674 * @throw std::length_error If new length exceeds @c max_size().
1675 * @throw std::out_of_range If @a __pos is beyond the end of this
1676 * string.
1677 *
1678 * Inserts the first @a __n characters of @a __s starting at @a
1679 * __pos. If adding characters causes the length to exceed
1680 * max_size(), length_error is thrown. If @a __pos is beyond
1681 * end(), out_of_range is thrown. The value of the string
1682 * doesn't change if an error is thrown.
1683 */
1685 insert(size_type __pos, const _CharT* __s, size_type __n)
1686 { return this->replace(__pos, size_type(0), __s, __n); }
1687
1688 /**
1689 * @brief Insert a C string.
1690 * @param __pos Position in string to insert at.
1691 * @param __s The C string to insert.
1692 * @return Reference to this string.
1693 * @throw std::length_error If new length exceeds @c max_size().
1694 * @throw std::out_of_range If @a pos is beyond the end of this
1695 * string.
1696 *
1697 * Inserts the first @a n characters of @a __s starting at @a __pos. If
1698 * adding characters causes the length to exceed max_size(),
1699 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
1700 * thrown. The value of the string doesn't change if an error is
1701 * thrown.
1702 */
1704 insert(size_type __pos, const _CharT* __s)
1705 {
1706 __glibcxx_requires_string(__s);
1707 return this->replace(__pos, size_type(0), __s,
1708 traits_type::length(__s));
1709 }
1710
1711 /**
1712 * @brief Insert multiple characters.
1713 * @param __pos Index in string to insert at.
1714 * @param __n Number of characters to insert
1715 * @param __c The character to insert.
1716 * @return Reference to this string.
1717 * @throw std::length_error If new length exceeds @c max_size().
1718 * @throw std::out_of_range If @a __pos is beyond the end of this
1719 * string.
1720 *
1721 * Inserts @a __n copies of character @a __c starting at index
1722 * @a __pos. If adding characters causes the length to exceed
1723 * max_size(), length_error is thrown. If @a __pos > length(),
1724 * out_of_range is thrown. The value of the string doesn't
1725 * change if an error is thrown.
1726 */
1728 insert(size_type __pos, size_type __n, _CharT __c)
1729 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1730 size_type(0), __n, __c); }
1731
1732 /**
1733 * @brief Insert one character.
1734 * @param __p Iterator referencing position in string to insert at.
1735 * @param __c The character to insert.
1736 * @return Iterator referencing newly inserted char.
1737 * @throw std::length_error If new length exceeds @c max_size().
1738 *
1739 * Inserts character @a __c at position referenced by @a __p.
1740 * If adding character causes the length to exceed max_size(),
1741 * length_error is thrown. If @a __p is beyond end of string,
1742 * out_of_range is thrown. The value of the string doesn't
1743 * change if an error is thrown.
1744 */
1745 iterator
1746 insert(__const_iterator __p, _CharT __c)
1747 {
1748 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1749 const size_type __pos = __p - begin();
1750 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1751 return iterator(_M_data() + __pos);
1752 }
1753
1754#if __cplusplus >= 201703L
1755 /**
1756 * @brief Insert a string_view.
1757 * @param __pos Position in string to insert at.
1758 * @param __svt The object convertible to string_view to insert.
1759 * @return Reference to this string.
1760 */
1761 template<typename _Tp>
1762 _If_sv<_Tp, basic_string&>
1763 insert(size_type __pos, const _Tp& __svt)
1764 {
1765 __sv_type __sv = __svt;
1766 return this->insert(__pos, __sv.data(), __sv.size());
1767 }
1768
1769 /**
1770 * @brief Insert a string_view.
1771 * @param __pos1 Position in string to insert at.
1772 * @param __svt The object convertible to string_view to insert from.
1773 * @param __pos2 Start of characters in str to insert.
1774 * @param __n The number of characters to insert.
1775 * @return Reference to this string.
1776 */
1777 template<typename _Tp>
1778 _If_sv<_Tp, basic_string&>
1779 insert(size_type __pos1, const _Tp& __svt,
1780 size_type __pos2, size_type __n = npos)
1781 {
1782 __sv_type __sv = __svt;
1783 return this->replace(__pos1, size_type(0),
1784 __sv.data()
1785 + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"),
1786 std::__sv_limit(__sv.size(), __pos2, __n));
1787 }
1788#endif // C++17
1789
1790 /**
1791 * @brief Remove characters.
1792 * @param __pos Index of first character to remove (default 0).
1793 * @param __n Number of characters to remove (default remainder).
1794 * @return Reference to this string.
1795 * @throw std::out_of_range If @a pos is beyond the end of this
1796 * string.
1797 *
1798 * Removes @a __n characters from this string starting at @a
1799 * __pos. The length of the string is reduced by @a __n. If
1800 * there are < @a __n characters to remove, the remainder of
1801 * the string is truncated. If @a __p is beyond end of string,
1802 * out_of_range is thrown. The value of the string doesn't
1803 * change if an error is thrown.
1804 */
1806 erase(size_type __pos = 0, size_type __n = npos)
1807 {
1808 _M_check(__pos, "basic_string::erase");
1809 if (__n == npos)
1810 this->_M_set_length(__pos);
1811 else if (__n != 0)
1812 this->_M_erase(__pos, _M_limit(__pos, __n));
1813 return *this;
1814 }
1815
1816 /**
1817 * @brief Remove one character.
1818 * @param __position Iterator referencing the character to remove.
1819 * @return iterator referencing same location after removal.
1820 *
1821 * Removes the character at @a __position from this string. The value
1822 * of the string doesn't change if an error is thrown.
1823 */
1824 iterator
1825 erase(__const_iterator __position)
1826 {
1827 _GLIBCXX_DEBUG_PEDASSERT(__position >= begin()
1828 && __position < end());
1829 const size_type __pos = __position - begin();
1830 this->_M_erase(__pos, size_type(1));
1831 return iterator(_M_data() + __pos);
1832 }
1833
1834 /**
1835 * @brief Remove a range of characters.
1836 * @param __first Iterator referencing the first character to remove.
1837 * @param __last Iterator referencing the end of the range.
1838 * @return Iterator referencing location of first after removal.
1839 *
1840 * Removes the characters in the range [first,last) from this string.
1841 * The value of the string doesn't change if an error is thrown.
1842 */
1843 iterator
1844 erase(__const_iterator __first, __const_iterator __last)
1845 {
1846 _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last
1847 && __last <= end());
1848 const size_type __pos = __first - begin();
1849 if (__last == end())
1850 this->_M_set_length(__pos);
1851 else
1852 this->_M_erase(__pos, __last - __first);
1853 return iterator(this->_M_data() + __pos);
1854 }
1855
1856#if __cplusplus >= 201103L
1857 /**
1858 * @brief Remove the last character.
1859 *
1860 * The string must be non-empty.
1861 */
1862 void
1863 pop_back() noexcept
1864 {
1865 __glibcxx_assert(!empty());
1866 _M_erase(size() - 1, 1);
1867 }
1868#endif // C++11
1869
1870 /**
1871 * @brief Replace characters with value from another string.
1872 * @param __pos Index of first character to replace.
1873 * @param __n Number of characters to be replaced.
1874 * @param __str String to insert.
1875 * @return Reference to this string.
1876 * @throw std::out_of_range If @a pos is beyond the end of this
1877 * string.
1878 * @throw std::length_error If new length exceeds @c max_size().
1879 *
1880 * Removes the characters in the range [__pos,__pos+__n) from
1881 * this string. In place, the value of @a __str is inserted.
1882 * If @a __pos is beyond end of string, out_of_range is thrown.
1883 * If the length of the result exceeds max_size(), length_error
1884 * is thrown. The value of the string doesn't change if an
1885 * error is thrown.
1886 */
1888 replace(size_type __pos, size_type __n, const basic_string& __str)
1889 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
1890
1891 /**
1892 * @brief Replace characters with value from another string.
1893 * @param __pos1 Index of first character to replace.
1894 * @param __n1 Number of characters to be replaced.
1895 * @param __str String to insert.
1896 * @param __pos2 Index of first character of str to use.
1897 * @param __n2 Number of characters from str to use.
1898 * @return Reference to this string.
1899 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
1900 * __str.size().
1901 * @throw std::length_error If new length exceeds @c max_size().
1902 *
1903 * Removes the characters in the range [__pos1,__pos1 + n) from this
1904 * string. In place, the value of @a __str is inserted. If @a __pos is
1905 * beyond end of string, out_of_range is thrown. If the length of the
1906 * result exceeds max_size(), length_error is thrown. The value of the
1907 * string doesn't change if an error is thrown.
1908 */
1910 replace(size_type __pos1, size_type __n1, const basic_string& __str,
1911 size_type __pos2, size_type __n2 = npos)
1912 { return this->replace(__pos1, __n1, __str._M_data()
1913 + __str._M_check(__pos2, "basic_string::replace"),
1914 __str._M_limit(__pos2, __n2)); }
1915
1916 /**
1917 * @brief Replace characters with value of a C substring.
1918 * @param __pos Index of first character to replace.
1919 * @param __n1 Number of characters to be replaced.
1920 * @param __s C string to insert.
1921 * @param __n2 Number of characters from @a s to use.
1922 * @return Reference to this string.
1923 * @throw std::out_of_range If @a pos1 > size().
1924 * @throw std::length_error If new length exceeds @c max_size().
1925 *
1926 * Removes the characters in the range [__pos,__pos + __n1)
1927 * from this string. In place, the first @a __n2 characters of
1928 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
1929 * @a __pos is beyond end of string, out_of_range is thrown. If
1930 * the length of result exceeds max_size(), length_error is
1931 * thrown. The value of the string doesn't change if an error
1932 * is thrown.
1933 */
1935 replace(size_type __pos, size_type __n1, const _CharT* __s,
1936 size_type __n2)
1937 {
1938 __glibcxx_requires_string_len(__s, __n2);
1939 return _M_replace(_M_check(__pos, "basic_string::replace"),
1940 _M_limit(__pos, __n1), __s, __n2);
1941 }
1942
1943 /**
1944 * @brief Replace characters with value of a C string.
1945 * @param __pos Index of first character to replace.
1946 * @param __n1 Number of characters to be replaced.
1947 * @param __s C string to insert.
1948 * @return Reference to this string.
1949 * @throw std::out_of_range If @a pos > size().
1950 * @throw std::length_error If new length exceeds @c max_size().
1951 *
1952 * Removes the characters in the range [__pos,__pos + __n1)
1953 * from this string. In place, the characters of @a __s are
1954 * inserted. If @a __pos is beyond end of string, out_of_range
1955 * is thrown. If the length of result exceeds max_size(),
1956 * length_error is thrown. The value of the string doesn't
1957 * change if an error is thrown.
1958 */
1960 replace(size_type __pos, size_type __n1, const _CharT* __s)
1961 {
1962 __glibcxx_requires_string(__s);
1963 return this->replace(__pos, __n1, __s, traits_type::length(__s));
1964 }
1965
1966 /**
1967 * @brief Replace characters with multiple characters.
1968 * @param __pos Index of first character to replace.
1969 * @param __n1 Number of characters to be replaced.
1970 * @param __n2 Number of characters to insert.
1971 * @param __c Character to insert.
1972 * @return Reference to this string.
1973 * @throw std::out_of_range If @a __pos > size().
1974 * @throw std::length_error If new length exceeds @c max_size().
1975 *
1976 * Removes the characters in the range [pos,pos + n1) from this
1977 * string. In place, @a __n2 copies of @a __c are inserted.
1978 * If @a __pos is beyond end of string, out_of_range is thrown.
1979 * If the length of result exceeds max_size(), length_error is
1980 * thrown. The value of the string doesn't change if an error
1981 * is thrown.
1982 */
1984 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
1985 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
1986 _M_limit(__pos, __n1), __n2, __c); }
1987
1988 /**
1989 * @brief Replace range of characters with string.
1990 * @param __i1 Iterator referencing start of range to replace.
1991 * @param __i2 Iterator referencing end of range to replace.
1992 * @param __str String value to insert.
1993 * @return Reference to this string.
1994 * @throw std::length_error If new length exceeds @c max_size().
1995 *
1996 * Removes the characters in the range [__i1,__i2). In place,
1997 * the value of @a __str is inserted. If the length of result
1998 * exceeds max_size(), length_error is thrown. The value of
1999 * the string doesn't change if an error is thrown.
2000 */
2002 replace(__const_iterator __i1, __const_iterator __i2,
2003 const basic_string& __str)
2004 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
2005
2006 /**
2007 * @brief Replace range of characters with C substring.
2008 * @param __i1 Iterator referencing start of range to replace.
2009 * @param __i2 Iterator referencing end of range to replace.
2010 * @param __s C string value to insert.
2011 * @param __n Number of characters from s to insert.
2012 * @return Reference to this string.
2013 * @throw std::length_error If new length exceeds @c max_size().
2014 *
2015 * Removes the characters in the range [__i1,__i2). In place,
2016 * the first @a __n characters of @a __s are inserted. If the
2017 * length of result exceeds max_size(), length_error is thrown.
2018 * The value of the string doesn't change if an error is
2019 * thrown.
2020 */
2022 replace(__const_iterator __i1, __const_iterator __i2,
2023 const _CharT* __s, size_type __n)
2024 {
2025 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2026 && __i2 <= end());
2027 return this->replace(__i1 - begin(), __i2 - __i1, __s, __n);
2028 }
2029
2030 /**
2031 * @brief Replace range of characters with C string.
2032 * @param __i1 Iterator referencing start of range to replace.
2033 * @param __i2 Iterator referencing end of range to replace.
2034 * @param __s C string value to insert.
2035 * @return Reference to this string.
2036 * @throw std::length_error If new length exceeds @c max_size().
2037 *
2038 * Removes the characters in the range [__i1,__i2). In place,
2039 * the characters of @a __s are inserted. If the length of
2040 * result exceeds max_size(), length_error is thrown. The
2041 * value of the string doesn't change if an error is thrown.
2042 */
2044 replace(__const_iterator __i1, __const_iterator __i2, const _CharT* __s)
2045 {
2046 __glibcxx_requires_string(__s);
2047 return this->replace(__i1, __i2, __s, traits_type::length(__s));
2048 }
2049
2050 /**
2051 * @brief Replace range of characters with multiple characters
2052 * @param __i1 Iterator referencing start of range to replace.
2053 * @param __i2 Iterator referencing end of range to replace.
2054 * @param __n Number of characters to insert.
2055 * @param __c Character to insert.
2056 * @return Reference to this string.
2057 * @throw std::length_error If new length exceeds @c max_size().
2058 *
2059 * Removes the characters in the range [__i1,__i2). In place,
2060 * @a __n copies of @a __c are inserted. If the length of
2061 * result exceeds max_size(), length_error is thrown. The
2062 * value of the string doesn't change if an error is thrown.
2063 */
2065 replace(__const_iterator __i1, __const_iterator __i2, size_type __n,
2066 _CharT __c)
2067 {
2068 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2069 && __i2 <= end());
2070 return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __c);
2071 }
2072
2073 /**
2074 * @brief Replace range of characters with range.
2075 * @param __i1 Iterator referencing start of range to replace.
2076 * @param __i2 Iterator referencing end of range to replace.
2077 * @param __k1 Iterator referencing start of range to insert.
2078 * @param __k2 Iterator referencing end of range to insert.
2079 * @return Reference to this string.
2080 * @throw std::length_error If new length exceeds @c max_size().
2081 *
2082 * Removes the characters in the range [__i1,__i2). In place,
2083 * characters in the range [__k1,__k2) are inserted. If the
2084 * length of result exceeds max_size(), length_error is thrown.
2085 * The value of the string doesn't change if an error is
2086 * thrown.
2087 */
2088#if __cplusplus >= 201103L
2089 template<class _InputIterator,
2090 typename = std::_RequireInputIter<_InputIterator>>
2092 replace(const_iterator __i1, const_iterator __i2,
2093 _InputIterator __k1, _InputIterator __k2)
2094 {
2095 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2096 && __i2 <= end());
2097 __glibcxx_requires_valid_range(__k1, __k2);
2098 return this->_M_replace_dispatch(__i1, __i2, __k1, __k2,
2099 std::__false_type());
2100 }
2101#else
2102 template<class _InputIterator>
2103#ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
2104 typename __enable_if_not_native_iterator<_InputIterator>::__type
2105#else
2107#endif
2108 replace(iterator __i1, iterator __i2,
2109 _InputIterator __k1, _InputIterator __k2)
2110 {
2111 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2112 && __i2 <= end());
2113 __glibcxx_requires_valid_range(__k1, __k2);
2114 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
2115 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
2116 }
2117#endif
2118
2119 // Specializations for the common case of pointer and iterator:
2120 // useful to avoid the overhead of temporary buffering in _M_replace.
2122 replace(__const_iterator __i1, __const_iterator __i2,
2123 _CharT* __k1, _CharT* __k2)
2124 {
2125 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2126 && __i2 <= end());
2127 __glibcxx_requires_valid_range(__k1, __k2);
2128 return this->replace(__i1 - begin(), __i2 - __i1,
2129 __k1, __k2 - __k1);
2130 }
2131
2133 replace(__const_iterator __i1, __const_iterator __i2,
2134 const _CharT* __k1, const _CharT* __k2)
2135 {
2136 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2137 && __i2 <= end());
2138 __glibcxx_requires_valid_range(__k1, __k2);
2139 return this->replace(__i1 - begin(), __i2 - __i1,
2140 __k1, __k2 - __k1);
2141 }
2142
2144 replace(__const_iterator __i1, __const_iterator __i2,
2145 iterator __k1, iterator __k2)
2146 {
2147 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2148 && __i2 <= end());
2149 __glibcxx_requires_valid_range(__k1, __k2);
2150 return this->replace(__i1 - begin(), __i2 - __i1,
2151 __k1.base(), __k2 - __k1);
2152 }
2153
2155 replace(__const_iterator __i1, __const_iterator __i2,
2156 const_iterator __k1, const_iterator __k2)
2157 {
2158 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2159 && __i2 <= end());
2160 __glibcxx_requires_valid_range(__k1, __k2);
2161 return this->replace(__i1 - begin(), __i2 - __i1,
2162 __k1.base(), __k2 - __k1);
2163 }
2164
2165#if __cplusplus >= 201103L
2166 /**
2167 * @brief Replace range of characters with initializer_list.
2168 * @param __i1 Iterator referencing start of range to replace.
2169 * @param __i2 Iterator referencing end of range to replace.
2170 * @param __l The initializer_list of characters to insert.
2171 * @return Reference to this string.
2172 * @throw std::length_error If new length exceeds @c max_size().
2173 *
2174 * Removes the characters in the range [__i1,__i2). In place,
2175 * characters in the range [__k1,__k2) are inserted. If the
2176 * length of result exceeds max_size(), length_error is thrown.
2177 * The value of the string doesn't change if an error is
2178 * thrown.
2179 */
2180 basic_string& replace(const_iterator __i1, const_iterator __i2,
2181 initializer_list<_CharT> __l)
2182 { return this->replace(__i1, __i2, __l.begin(), __l.size()); }
2183#endif // C++11
2184
2185#if __cplusplus >= 201703L
2186 /**
2187 * @brief Replace range of characters with string_view.
2188 * @param __pos The position to replace at.
2189 * @param __n The number of characters to replace.
2190 * @param __svt The object convertible to string_view to insert.
2191 * @return Reference to this string.
2192 */
2193 template<typename _Tp>
2194 _If_sv<_Tp, basic_string&>
2195 replace(size_type __pos, size_type __n, const _Tp& __svt)
2196 {
2197 __sv_type __sv = __svt;
2198 return this->replace(__pos, __n, __sv.data(), __sv.size());
2199 }
2200
2201 /**
2202 * @brief Replace range of characters with string_view.
2203 * @param __pos1 The position to replace at.
2204 * @param __n1 The number of characters to replace.
2205 * @param __svt The object convertible to string_view to insert from.
2206 * @param __pos2 The position in the string_view to insert from.
2207 * @param __n2 The number of characters to insert.
2208 * @return Reference to this string.
2209 */
2210 template<typename _Tp>
2211 _If_sv<_Tp, basic_string&>
2212 replace(size_type __pos1, size_type __n1, const _Tp& __svt,
2213 size_type __pos2, size_type __n2 = npos)
2214 {
2215 __sv_type __sv = __svt;
2216 return this->replace(__pos1, __n1,
2217 __sv.data()
2218 + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"),
2219 std::__sv_limit(__sv.size(), __pos2, __n2));
2220 }
2221
2222 /**
2223 * @brief Replace range of characters with string_view.
2224 * @param __i1 An iterator referencing the start position
2225 to replace at.
2226 * @param __i2 An iterator referencing the end position
2227 for the replace.
2228 * @param __svt The object convertible to string_view to insert from.
2229 * @return Reference to this string.
2230 */
2231 template<typename _Tp>
2232 _If_sv<_Tp, basic_string&>
2233 replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
2234 {
2235 __sv_type __sv = __svt;
2236 return this->replace(__i1 - begin(), __i2 - __i1, __sv);
2237 }
2238#endif // C++17
2239
2240 private:
2241 template<class _Integer>
2243 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
2244 _Integer __n, _Integer __val, __true_type)
2245 { return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __val); }
2246
2247 template<class _InputIterator>
2249 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
2250 _InputIterator __k1, _InputIterator __k2,
2251 __false_type);
2252
2254 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
2255 _CharT __c);
2256
2258 _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
2259 const size_type __len2);
2260
2262 _M_append(const _CharT* __s, size_type __n);
2263
2264 public:
2265
2266 /**
2267 * @brief Copy substring into C string.
2268 * @param __s C string to copy value into.
2269 * @param __n Number of characters to copy.
2270 * @param __pos Index of first character to copy.
2271 * @return Number of characters actually copied
2272 * @throw std::out_of_range If __pos > size().
2273 *
2274 * Copies up to @a __n characters starting at @a __pos into the
2275 * C string @a __s. If @a __pos is %greater than size(),
2276 * out_of_range is thrown.
2277 */
2278 size_type
2279 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
2280
2281 /**
2282 * @brief Swap contents with another string.
2283 * @param __s String to swap with.
2284 *
2285 * Exchanges the contents of this string with that of @a __s in constant
2286 * time.
2287 */
2288 void
2289 swap(basic_string& __s) _GLIBCXX_NOEXCEPT;
2290
2291 // String operations:
2292 /**
2293 * @brief Return const pointer to null-terminated contents.
2294 *
2295 * This is a handle to internal data. Do not modify or dire things may
2296 * happen.
2297 */
2298 const _CharT*
2299 c_str() const _GLIBCXX_NOEXCEPT
2300 { return _M_data(); }
2301
2302 /**
2303 * @brief Return const pointer to contents.
2304 *
2305 * This is a pointer to internal data. It is undefined to modify
2306 * the contents through the returned pointer. To get a pointer that
2307 * allows modifying the contents use @c &str[0] instead,
2308 * (or in C++17 the non-const @c str.data() overload).
2309 */
2310 const _CharT*
2311 data() const _GLIBCXX_NOEXCEPT
2312 { return _M_data(); }
2313
2314#if __cplusplus >= 201703L
2315 /**
2316 * @brief Return non-const pointer to contents.
2317 *
2318 * This is a pointer to the character sequence held by the string.
2319 * Modifying the characters in the sequence is allowed.
2320 */
2321 _CharT*
2322 data() noexcept
2323 { return _M_data(); }
2324#endif
2325
2326 /**
2327 * @brief Return copy of allocator used to construct this string.
2328 */
2329 allocator_type
2330 get_allocator() const _GLIBCXX_NOEXCEPT
2331 { return _M_get_allocator(); }
2332
2333 /**
2334 * @brief Find position of a C substring.
2335 * @param __s C string to locate.
2336 * @param __pos Index of character to search from.
2337 * @param __n Number of characters from @a s to search for.
2338 * @return Index of start of first occurrence.
2339 *
2340 * Starting from @a __pos, searches forward for the first @a
2341 * __n characters in @a __s within this string. If found,
2342 * returns the index where it begins. If not found, returns
2343 * npos.
2344 */
2345 size_type
2346 find(const _CharT* __s, size_type __pos, size_type __n) const
2347 _GLIBCXX_NOEXCEPT;
2348
2349 /**
2350 * @brief Find position of a string.
2351 * @param __str String to locate.
2352 * @param __pos Index of character to search from (default 0).
2353 * @return Index of start of first occurrence.
2354 *
2355 * Starting from @a __pos, searches forward for value of @a __str within
2356 * this string. If found, returns the index where it begins. If not
2357 * found, returns npos.
2358 */
2359 size_type
2360 find(const basic_string& __str, size_type __pos = 0) const
2361 _GLIBCXX_NOEXCEPT
2362 { return this->find(__str.data(), __pos, __str.size()); }
2363
2364#if __cplusplus >= 201703L
2365 /**
2366 * @brief Find position of a string_view.
2367 * @param __svt The object convertible to string_view to locate.
2368 * @param __pos Index of character to search from (default 0).
2369 * @return Index of start of first occurrence.
2370 */
2371 template<typename _Tp>
2372 _If_sv<_Tp, size_type>
2373 find(const _Tp& __svt, size_type __pos = 0) const
2374 noexcept(is_same<_Tp, __sv_type>::value)
2375 {
2376 __sv_type __sv = __svt;
2377 return this->find(__sv.data(), __pos, __sv.size());
2378 }
2379#endif // C++17
2380
2381 /**
2382 * @brief Find position of a C string.
2383 * @param __s C string to locate.
2384 * @param __pos Index of character to search from (default 0).
2385 * @return Index of start of first occurrence.
2386 *
2387 * Starting from @a __pos, searches forward for the value of @a
2388 * __s within this string. If found, returns the index where
2389 * it begins. If not found, returns npos.
2390 */
2391 size_type
2392 find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
2393 {
2394 __glibcxx_requires_string(__s);
2395 return this->find(__s, __pos, traits_type::length(__s));
2396 }
2397
2398 /**
2399 * @brief Find position of a character.
2400 * @param __c Character to locate.
2401 * @param __pos Index of character to search from (default 0).
2402 * @return Index of first occurrence.
2403 *
2404 * Starting from @a __pos, searches forward for @a __c within
2405 * this string. If found, returns the index where it was
2406 * found. If not found, returns npos.
2407 */
2408 size_type
2409 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
2410
2411 /**
2412 * @brief Find last position of a string.
2413 * @param __str String to locate.
2414 * @param __pos Index of character to search back from (default end).
2415 * @return Index of start of last occurrence.
2416 *
2417 * Starting from @a __pos, searches backward for value of @a
2418 * __str within this string. If found, returns the index where
2419 * it begins. If not found, returns npos.
2420 */
2421 size_type
2422 rfind(const basic_string& __str, size_type __pos = npos) const
2423 _GLIBCXX_NOEXCEPT
2424 { return this->rfind(__str.data(), __pos, __str.size()); }
2425
2426#if __cplusplus >= 201703L
2427 /**
2428 * @brief Find last position of a string_view.
2429 * @param __svt The object convertible to string_view to locate.
2430 * @param __pos Index of character to search back from (default end).
2431 * @return Index of start of last occurrence.
2432 */
2433 template<typename _Tp>
2434 _If_sv<_Tp, size_type>
2435 rfind(const _Tp& __svt, size_type __pos = npos) const
2436 noexcept(is_same<_Tp, __sv_type>::value)
2437 {
2438 __sv_type __sv = __svt;
2439 return this->rfind(__sv.data(), __pos, __sv.size());
2440 }
2441#endif // C++17
2442
2443 /**
2444 * @brief Find last position of a C substring.
2445 * @param __s C string to locate.
2446 * @param __pos Index of character to search back from.
2447 * @param __n Number of characters from s to search for.
2448 * @return Index of start of last occurrence.
2449 *
2450 * Starting from @a __pos, searches backward for the first @a
2451 * __n characters in @a __s within this string. If found,
2452 * returns the index where it begins. If not found, returns
2453 * npos.
2454 */
2455 size_type
2456 rfind(const _CharT* __s, size_type __pos, size_type __n) const
2457 _GLIBCXX_NOEXCEPT;
2458
2459 /**
2460 * @brief Find last position of a C string.
2461 * @param __s C string to locate.
2462 * @param __pos Index of character to start search at (default end).
2463 * @return Index of start of last occurrence.
2464 *
2465 * Starting from @a __pos, searches backward for the value of
2466 * @a __s within this string. If found, returns the index
2467 * where it begins. If not found, returns npos.
2468 */
2469 size_type
2470 rfind(const _CharT* __s, size_type __pos = npos) const
2471 {
2472 __glibcxx_requires_string(__s);
2473 return this->rfind(__s, __pos, traits_type::length(__s));
2474 }
2475
2476 /**
2477 * @brief Find last position of a character.
2478 * @param __c Character to locate.
2479 * @param __pos Index of character to search back from (default end).
2480 * @return Index of last occurrence.
2481 *
2482 * Starting from @a __pos, searches backward for @a __c within
2483 * this string. If found, returns the index where it was
2484 * found. If not found, returns npos.
2485 */
2486 size_type
2487 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
2488
2489 /**
2490 * @brief Find position of a character of string.
2491 * @param __str String containing characters to locate.
2492 * @param __pos Index of character to search from (default 0).
2493 * @return Index of first occurrence.
2494 *
2495 * Starting from @a __pos, searches forward for one of the
2496 * characters of @a __str within this string. If found,
2497 * returns the index where it was found. If not found, returns
2498 * npos.
2499 */
2500 size_type
2501 find_first_of(const basic_string& __str, size_type __pos = 0) const
2502 _GLIBCXX_NOEXCEPT
2503 { return this->find_first_of(__str.data(), __pos, __str.size()); }
2504
2505#if __cplusplus >= 201703L
2506 /**
2507 * @brief Find position of a character of a string_view.
2508 * @param __svt An object convertible to string_view containing
2509 * characters to locate.
2510 * @param __pos Index of character to search from (default 0).
2511 * @return Index of first occurrence.
2512 */
2513 template<typename _Tp>
2514 _If_sv<_Tp, size_type>
2515 find_first_of(const _Tp& __svt, size_type __pos = 0) const
2516 noexcept(is_same<_Tp, __sv_type>::value)
2517 {
2518 __sv_type __sv = __svt;
2519 return this->find_first_of(__sv.data(), __pos, __sv.size());
2520 }
2521#endif // C++17
2522
2523 /**
2524 * @brief Find position of a character of C substring.
2525 * @param __s String containing characters to locate.
2526 * @param __pos Index of character to search from.
2527 * @param __n Number of characters from s to search for.
2528 * @return Index of first occurrence.
2529 *
2530 * Starting from @a __pos, searches forward for one of the
2531 * first @a __n characters of @a __s within this string. If
2532 * found, returns the index where it was found. If not found,
2533 * returns npos.
2534 */
2535 size_type
2536 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
2537 _GLIBCXX_NOEXCEPT;
2538
2539 /**
2540 * @brief Find position of a character of C string.
2541 * @param __s String containing characters to locate.
2542 * @param __pos Index of character to search from (default 0).
2543 * @return Index of first occurrence.
2544 *
2545 * Starting from @a __pos, searches forward for one of the
2546 * characters of @a __s within this string. If found, returns
2547 * the index where it was found. If not found, returns npos.
2548 */
2549 size_type
2550 find_first_of(const _CharT* __s, size_type __pos = 0) const
2551 _GLIBCXX_NOEXCEPT
2552 {
2553 __glibcxx_requires_string(__s);
2554 return this->find_first_of(__s, __pos, traits_type::length(__s));
2555 }
2556
2557 /**
2558 * @brief Find position of a character.
2559 * @param __c Character to locate.
2560 * @param __pos Index of character to search from (default 0).
2561 * @return Index of first occurrence.
2562 *
2563 * Starting from @a __pos, searches forward for the character
2564 * @a __c within this string. If found, returns the index
2565 * where it was found. If not found, returns npos.
2566 *
2567 * Note: equivalent to find(__c, __pos).
2568 */
2569 size_type
2570 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
2571 { return this->find(__c, __pos); }
2572
2573 /**
2574 * @brief Find last position of a character of string.
2575 * @param __str String containing characters to locate.
2576 * @param __pos Index of character to search back from (default end).
2577 * @return Index of last occurrence.
2578 *
2579 * Starting from @a __pos, searches backward for one of the
2580 * characters of @a __str within this string. If found,
2581 * returns the index where it was found. If not found, returns
2582 * npos.
2583 */
2584 size_type
2585 find_last_of(const basic_string& __str, size_type __pos = npos) const
2586 _GLIBCXX_NOEXCEPT
2587 { return this->find_last_of(__str.data(), __pos, __str.size()); }
2588
2589#if __cplusplus >= 201703L
2590 /**
2591 * @brief Find last position of a character of string.
2592 * @param __svt An object convertible to string_view containing
2593 * characters to locate.
2594 * @param __pos Index of character to search back from (default end).
2595 * @return Index of last occurrence.
2596 */
2597 template<typename _Tp>
2598 _If_sv<_Tp, size_type>
2599 find_last_of(const _Tp& __svt, size_type __pos = npos) const
2600 noexcept(is_same<_Tp, __sv_type>::value)
2601 {
2602 __sv_type __sv = __svt;
2603 return this->find_last_of(__sv.data(), __pos, __sv.size());
2604 }
2605#endif // C++17
2606
2607 /**
2608 * @brief Find last position of a character of C substring.
2609 * @param __s C string containing characters to locate.
2610 * @param __pos Index of character to search back from.
2611 * @param __n Number of characters from s to search for.
2612 * @return Index of last occurrence.
2613 *
2614 * Starting from @a __pos, searches backward for one of the
2615 * first @a __n characters of @a __s within this string. If
2616 * found, returns the index where it was found. If not found,
2617 * returns npos.
2618 */
2619 size_type
2620 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
2621 _GLIBCXX_NOEXCEPT;
2622
2623 /**
2624 * @brief Find last position of a character of C string.
2625 * @param __s C string containing characters to locate.
2626 * @param __pos Index of character to search back from (default end).
2627 * @return Index of last occurrence.
2628 *
2629 * Starting from @a __pos, searches backward for one of the
2630 * characters of @a __s within this string. If found, returns
2631 * the index where it was found. If not found, returns npos.
2632 */
2633 size_type
2634 find_last_of(const _CharT* __s, size_type __pos = npos) const
2635 _GLIBCXX_NOEXCEPT
2636 {
2637 __glibcxx_requires_string(__s);
2638 return this->find_last_of(__s, __pos, traits_type::length(__s));
2639 }
2640
2641 /**
2642 * @brief Find last position of a character.
2643 * @param __c Character to locate.
2644 * @param __pos Index of character to search back from (default end).
2645 * @return Index of last occurrence.
2646 *
2647 * Starting from @a __pos, searches backward for @a __c within
2648 * this string. If found, returns the index where it was
2649 * found. If not found, returns npos.
2650 *
2651 * Note: equivalent to rfind(__c, __pos).
2652 */
2653 size_type
2654 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
2655 { return this->rfind(__c, __pos); }
2656
2657 /**
2658 * @brief Find position of a character not in string.
2659 * @param __str String containing characters to avoid.
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 character not contained
2664 * in @a __str within this string. If found, returns the index where it
2665 * was found. If not found, returns npos.
2666 */
2667 size_type
2668 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
2669 _GLIBCXX_NOEXCEPT
2670 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
2671
2672#if __cplusplus >= 201703L
2673 /**
2674 * @brief Find position of a character not in a string_view.
2675 * @param __svt A object convertible to string_view containing
2676 * characters to avoid.
2677 * @param __pos Index of character to search from (default 0).
2678 * @return Index of first occurrence.
2679 */
2680 template<typename _Tp>
2681 _If_sv<_Tp, size_type>
2682 find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
2683 noexcept(is_same<_Tp, __sv_type>::value)
2684 {
2685 __sv_type __sv = __svt;
2686 return this->find_first_not_of(__sv.data(), __pos, __sv.size());
2687 }
2688#endif // C++17
2689
2690 /**
2691 * @brief Find position of a character not in C substring.
2692 * @param __s C string containing characters to avoid.
2693 * @param __pos Index of character to search from.
2694 * @param __n Number of characters from __s to consider.
2695 * @return Index of first occurrence.
2696 *
2697 * Starting from @a __pos, searches forward for a character not
2698 * contained in the first @a __n characters of @a __s within
2699 * this string. If found, returns the index where it was
2700 * found. If not found, returns npos.
2701 */
2702 size_type
2703 find_first_not_of(const _CharT* __s, size_type __pos,
2704 size_type __n) const _GLIBCXX_NOEXCEPT;
2705
2706 /**
2707 * @brief Find position of a character not in C string.
2708 * @param __s C string containing characters to avoid.
2709 * @param __pos Index of character to search from (default 0).
2710 * @return Index of first occurrence.
2711 *
2712 * Starting from @a __pos, searches forward for a character not
2713 * contained in @a __s within this string. If found, returns
2714 * the index where it was found. If not found, returns npos.
2715 */
2716 size_type
2717 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
2718 _GLIBCXX_NOEXCEPT
2719 {
2720 __glibcxx_requires_string(__s);
2721 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
2722 }
2723
2724 /**
2725 * @brief Find position of a different character.
2726 * @param __c Character to avoid.
2727 * @param __pos Index of character to search from (default 0).
2728 * @return Index of first occurrence.
2729 *
2730 * Starting from @a __pos, searches forward for a character
2731 * other than @a __c within this string. If found, returns the
2732 * index where it was found. If not found, returns npos.
2733 */
2734 size_type
2735 find_first_not_of(_CharT __c, size_type __pos = 0) const
2736 _GLIBCXX_NOEXCEPT;
2737
2738 /**
2739 * @brief Find last position of a character not in string.
2740 * @param __str String containing characters to avoid.
2741 * @param __pos Index of character to search back from (default end).
2742 * @return Index of last occurrence.
2743 *
2744 * Starting from @a __pos, searches backward for a character
2745 * not contained in @a __str within this string. If found,
2746 * returns the index where it was found. If not found, returns
2747 * npos.
2748 */
2749 size_type
2750 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
2751 _GLIBCXX_NOEXCEPT
2752 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
2753
2754#if __cplusplus >= 201703L
2755 /**
2756 * @brief Find last position of a character not in a string_view.
2757 * @param __svt An object convertible to string_view containing
2758 * characters to avoid.
2759 * @param __pos Index of character to search back from (default end).
2760 * @return Index of last occurrence.
2761 */
2762 template<typename _Tp>
2763 _If_sv<_Tp, size_type>
2764 find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
2765 noexcept(is_same<_Tp, __sv_type>::value)
2766 {
2767 __sv_type __sv = __svt;
2768 return this->find_last_not_of(__sv.data(), __pos, __sv.size());
2769 }
2770#endif // C++17
2771
2772 /**
2773 * @brief Find last position of a character not in C substring.
2774 * @param __s C string containing characters to avoid.
2775 * @param __pos Index of character to search back from.
2776 * @param __n Number of characters from s to consider.
2777 * @return Index of last occurrence.
2778 *
2779 * Starting from @a __pos, searches backward for a character not
2780 * contained in the first @a __n characters of @a __s within this string.
2781 * If found, returns the index where it was found. If not found,
2782 * returns npos.
2783 */
2784 size_type
2785 find_last_not_of(const _CharT* __s, size_type __pos,
2786 size_type __n) const _GLIBCXX_NOEXCEPT;
2787 /**
2788 * @brief Find last position of a character not in C string.
2789 * @param __s C string containing characters to avoid.
2790 * @param __pos Index of character to search back from (default end).
2791 * @return Index of last occurrence.
2792 *
2793 * Starting from @a __pos, searches backward for a character
2794 * not contained in @a __s within this string. If found,
2795 * returns the index where it was found. If not found, returns
2796 * npos.
2797 */
2798 size_type
2799 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
2800 _GLIBCXX_NOEXCEPT
2801 {
2802 __glibcxx_requires_string(__s);
2803 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
2804 }
2805
2806 /**
2807 * @brief Find last position of a different character.
2808 * @param __c Character to avoid.
2809 * @param __pos Index of character to search back from (default end).
2810 * @return Index of last occurrence.
2811 *
2812 * Starting from @a __pos, searches backward for a character other than
2813 * @a __c within this string. If found, returns the index where it was
2814 * found. If not found, returns npos.
2815 */
2816 size_type
2817 find_last_not_of(_CharT __c, size_type __pos = npos) const
2818 _GLIBCXX_NOEXCEPT;
2819
2820 /**
2821 * @brief Get a substring.
2822 * @param __pos Index of first character (default 0).
2823 * @param __n Number of characters in substring (default remainder).
2824 * @return The new string.
2825 * @throw std::out_of_range If __pos > size().
2826 *
2827 * Construct and return a new string using the @a __n
2828 * characters starting at @a __pos. If the string is too
2829 * short, use the remainder of the characters. If @a __pos is
2830 * beyond the end of the string, out_of_range is thrown.
2831 */
2833 substr(size_type __pos = 0, size_type __n = npos) const
2834 { return basic_string(*this,
2835 _M_check(__pos, "basic_string::substr"), __n); }
2836
2837 /**
2838 * @brief Compare to a string.
2839 * @param __str String to compare against.
2840 * @return Integer < 0, 0, or > 0.
2841 *
2842 * Returns an integer < 0 if this string is ordered before @a
2843 * __str, 0 if their values are equivalent, or > 0 if this
2844 * string is ordered after @a __str. Determines the effective
2845 * length rlen of the strings to compare as the smallest of
2846 * size() and str.size(). The function then compares the two
2847 * strings by calling traits::compare(data(), str.data(),rlen).
2848 * If the result of the comparison is nonzero returns it,
2849 * otherwise the shorter one is ordered first.
2850 */
2851 int
2852 compare(const basic_string& __str) const
2853 {
2854 const size_type __size = this->size();
2855 const size_type __osize = __str.size();
2856 const size_type __len = std::min(__size, __osize);
2857
2858 int __r = traits_type::compare(_M_data(), __str.data(), __len);
2859 if (!__r)
2860 __r = _S_compare(__size, __osize);
2861 return __r;
2862 }
2863
2864#if __cplusplus >= 201703L
2865 /**
2866 * @brief Compare to a string_view.
2867 * @param __svt An object convertible to string_view to compare against.
2868 * @return Integer < 0, 0, or > 0.
2869 */
2870 template<typename _Tp>
2871 _If_sv<_Tp, int>
2872 compare(const _Tp& __svt) const
2873 noexcept(is_same<_Tp, __sv_type>::value)
2874 {
2875 __sv_type __sv = __svt;
2876 const size_type __size = this->size();
2877 const size_type __osize = __sv.size();
2878 const size_type __len = std::min(__size, __osize);
2879
2880 int __r = traits_type::compare(_M_data(), __sv.data(), __len);
2881 if (!__r)
2882 __r = _S_compare(__size, __osize);
2883 return __r;
2884 }
2885
2886 /**
2887 * @brief Compare to a string_view.
2888 * @param __pos A position in the string to start comparing from.
2889 * @param __n The number of characters to compare.
2890 * @param __svt An object convertible to string_view to compare
2891 * against.
2892 * @return Integer < 0, 0, or > 0.
2893 */
2894 template<typename _Tp>
2895 _If_sv<_Tp, int>
2896 compare(size_type __pos, size_type __n, const _Tp& __svt) const
2897 noexcept(is_same<_Tp, __sv_type>::value)
2898 {
2899 __sv_type __sv = __svt;
2900 return __sv_type(*this).substr(__pos, __n).compare(__sv);
2901 }
2902
2903 /**
2904 * @brief Compare to a string_view.
2905 * @param __pos1 A position in the string to start comparing from.
2906 * @param __n1 The number of characters to compare.
2907 * @param __svt An object convertible to string_view to compare
2908 * against.
2909 * @param __pos2 A position in the string_view to start comparing from.
2910 * @param __n2 The number of characters to compare.
2911 * @return Integer < 0, 0, or > 0.
2912 */
2913 template<typename _Tp>
2914 _If_sv<_Tp, int>
2915 compare(size_type __pos1, size_type __n1, const _Tp& __svt,
2916 size_type __pos2, size_type __n2 = npos) const
2917 noexcept(is_same<_Tp, __sv_type>::value)
2918 {
2919 __sv_type __sv = __svt;
2920 return __sv_type(*this)
2921 .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
2922 }
2923#endif // C++17
2924
2925 /**
2926 * @brief Compare substring to a string.
2927 * @param __pos Index of first character of substring.
2928 * @param __n Number of characters in substring.
2929 * @param __str String to compare against.
2930 * @return Integer < 0, 0, or > 0.
2931 *
2932 * Form the substring of this string from the @a __n characters
2933 * starting at @a __pos. Returns an integer < 0 if the
2934 * substring is ordered before @a __str, 0 if their values are
2935 * equivalent, or > 0 if the substring is ordered after @a
2936 * __str. Determines the effective length rlen of the strings
2937 * to compare as the smallest of the length of the substring
2938 * and @a __str.size(). The function then compares the two
2939 * strings by calling
2940 * traits::compare(substring.data(),str.data(),rlen). If the
2941 * result of the comparison is nonzero returns it, otherwise
2942 * the shorter one is ordered first.
2943 */
2944 int
2945 compare(size_type __pos, size_type __n, const basic_string& __str) const;
2946
2947 /**
2948 * @brief Compare substring to a substring.
2949 * @param __pos1 Index of first character of substring.
2950 * @param __n1 Number of characters in substring.
2951 * @param __str String to compare against.
2952 * @param __pos2 Index of first character of substring of str.
2953 * @param __n2 Number of characters in substring of str.
2954 * @return Integer < 0, 0, or > 0.
2955 *
2956 * Form the substring of this string from the @a __n1
2957 * characters starting at @a __pos1. Form the substring of @a
2958 * __str from the @a __n2 characters starting at @a __pos2.
2959 * Returns an integer < 0 if this substring is ordered before
2960 * the substring of @a __str, 0 if their values are equivalent,
2961 * or > 0 if this substring is ordered after the substring of
2962 * @a __str. Determines the effective length rlen of the
2963 * strings to compare as the smallest of the lengths of the
2964 * substrings. The function then compares the two strings by
2965 * calling
2966 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
2967 * If the result of the comparison is nonzero returns it,
2968 * otherwise the shorter one is ordered first.
2969 */
2970 int
2971 compare(size_type __pos1, size_type __n1, const basic_string& __str,
2972 size_type __pos2, size_type __n2 = npos) const;
2973
2974 /**
2975 * @brief Compare to a C string.
2976 * @param __s C string to compare against.
2977 * @return Integer < 0, 0, or > 0.
2978 *
2979 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
2980 * their values are equivalent, or > 0 if this string is ordered after
2981 * @a __s. Determines the effective length rlen of the strings to
2982 * compare as the smallest of size() and the length of a string
2983 * constructed from @a __s. The function then compares the two strings
2984 * by calling traits::compare(data(),s,rlen). If the result of the
2985 * comparison is nonzero returns it, otherwise the shorter one is
2986 * ordered first.
2987 */
2988 int
2989 compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT;
2990
2991 // _GLIBCXX_RESOLVE_LIB_DEFECTS
2992 // 5 String::compare specification questionable
2993 /**
2994 * @brief Compare substring to a C string.
2995 * @param __pos Index of first character of substring.
2996 * @param __n1 Number of characters in substring.
2997 * @param __s C string to compare against.
2998 * @return Integer < 0, 0, or > 0.
2999 *
3000 * Form the substring of this string from the @a __n1
3001 * characters starting at @a pos. Returns an integer < 0 if
3002 * the substring is ordered before @a __s, 0 if their values
3003 * are equivalent, or > 0 if the substring is ordered after @a
3004 * __s. Determines the effective length rlen of the strings to
3005 * compare as the smallest of the length of the substring and
3006 * the length of a string constructed from @a __s. The
3007 * function then compares the two string by calling
3008 * traits::compare(substring.data(),__s,rlen). If the result of
3009 * the comparison is nonzero returns it, otherwise the shorter
3010 * one is ordered first.
3011 */
3012 int
3013 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
3014
3015 /**
3016 * @brief Compare substring against a character %array.
3017 * @param __pos Index of first character of substring.
3018 * @param __n1 Number of characters in substring.
3019 * @param __s character %array to compare against.
3020 * @param __n2 Number of characters of s.
3021 * @return Integer < 0, 0, or > 0.
3022 *
3023 * Form the substring of this string from the @a __n1
3024 * characters starting at @a __pos. Form a string from the
3025 * first @a __n2 characters of @a __s. Returns an integer < 0
3026 * if this substring is ordered before the string from @a __s,
3027 * 0 if their values are equivalent, or > 0 if this substring
3028 * is ordered after the string from @a __s. Determines the
3029 * effective length rlen of the strings to compare as the
3030 * smallest of the length of the substring and @a __n2. The
3031 * function then compares the two strings by calling
3032 * traits::compare(substring.data(),s,rlen). If the result of
3033 * the comparison is nonzero returns it, otherwise the shorter
3034 * one is ordered first.
3035 *
3036 * NB: s must have at least n2 characters, &apos;\\0&apos; has
3037 * no special meaning.
3038 */
3039 int
3040 compare(size_type __pos, size_type __n1, const _CharT* __s,
3041 size_type __n2) const;
3042
3043#if __cplusplus > 201703L
3044 bool
3045 starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
3046 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3047
3048 bool
3049 starts_with(_CharT __x) const noexcept
3050 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3051
3052 bool
3053 starts_with(const _CharT* __x) const noexcept
3054 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3055
3056 bool
3057 ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept
3058 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3059
3060 bool
3061 ends_with(_CharT __x) const noexcept
3062 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3063
3064 bool
3065 ends_with(const _CharT* __x) const noexcept
3066 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3067#endif // C++20
3068
3069 // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
3070 template<typename, typename, typename> friend class basic_stringbuf;
3071 };
3072_GLIBCXX_END_NAMESPACE_CXX11
3073#else // !_GLIBCXX_USE_CXX11_ABI
3074 // Reference-counted COW string implentation
3075
3076 /**
3077 * @class basic_string basic_string.h <string>
3078 * @brief Managing sequences of characters and character-like objects.
3079 *
3080 * @ingroup strings
3081 * @ingroup sequences
3082 *
3083 * @tparam _CharT Type of character
3084 * @tparam _Traits Traits for character type, defaults to
3085 * char_traits<_CharT>.
3086 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
3087 *
3088 * Meets the requirements of a <a href="tables.html#65">container</a>, a
3089 * <a href="tables.html#66">reversible container</a>, and a
3090 * <a href="tables.html#67">sequence</a>. Of the
3091 * <a href="tables.html#68">optional sequence requirements</a>, only
3092 * @c push_back, @c at, and @c %array access are supported.
3093 *
3094 * @doctodo
3095 *
3096 *
3097 * Documentation? What's that?
3098 * Nathan Myers <ncm@cantrip.org>.
3099 *
3100 * A string looks like this:
3101 *
3102 * @code
3103 * [_Rep]
3104 * _M_length
3105 * [basic_string<char_type>] _M_capacity
3106 * _M_dataplus _M_refcount
3107 * _M_p ----------------> unnamed array of char_type
3108 * @endcode
3109 *
3110 * Where the _M_p points to the first character in the string, and
3111 * you cast it to a pointer-to-_Rep and subtract 1 to get a
3112 * pointer to the header.
3113 *
3114 * This approach has the enormous advantage that a string object
3115 * requires only one allocation. All the ugliness is confined
3116 * within a single %pair of inline functions, which each compile to
3117 * a single @a add instruction: _Rep::_M_data(), and
3118 * string::_M_rep(); and the allocation function which gets a
3119 * block of raw bytes and with room enough and constructs a _Rep
3120 * object at the front.
3121 *
3122 * The reason you want _M_data pointing to the character %array and
3123 * not the _Rep is so that the debugger can see the string
3124 * contents. (Probably we should add a non-inline member to get
3125 * the _Rep for the debugger to use, so users can check the actual
3126 * string length.)
3127 *
3128 * Note that the _Rep object is a POD so that you can have a
3129 * static <em>empty string</em> _Rep object already @a constructed before
3130 * static constructors have run. The reference-count encoding is
3131 * chosen so that a 0 indicates one reference, so you never try to
3132 * destroy the empty-string _Rep object.
3133 *
3134 * All but the last paragraph is considered pretty conventional
3135 * for a C++ string implementation.
3136 */
3137 // 21.3 Template class basic_string
3138 template<typename _CharT, typename _Traits, typename _Alloc>
3140 {
3142 rebind<_CharT>::other _CharT_alloc_type;
3144
3145 // Types:
3146 public:
3147 typedef _Traits traits_type;
3148 typedef typename _Traits::char_type value_type;
3149 typedef _Alloc allocator_type;
3150 typedef typename _CharT_alloc_type::size_type size_type;
3151 typedef typename _CharT_alloc_type::difference_type difference_type;
3152#if __cplusplus < 201103L
3153 typedef typename _CharT_alloc_type::reference reference;
3154 typedef typename _CharT_alloc_type::const_reference const_reference;
3155#else
3156 typedef value_type& reference;
3157 typedef const value_type& const_reference;
3158#endif
3159 typedef typename _CharT_alloc_traits::pointer pointer;
3160 typedef typename _CharT_alloc_traits::const_pointer const_pointer;
3161 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
3162 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
3163 const_iterator;
3166
3167 protected:
3168 // type used for positions in insert, erase etc.
3169 typedef iterator __const_iterator;
3170
3171 private:
3172 // _Rep: string representation
3173 // Invariants:
3174 // 1. String really contains _M_length + 1 characters: due to 21.3.4
3175 // must be kept null-terminated.
3176 // 2. _M_capacity >= _M_length
3177 // Allocated memory is always (_M_capacity + 1) * sizeof(_CharT).
3178 // 3. _M_refcount has three states:
3179 // -1: leaked, one reference, no ref-copies allowed, non-const.
3180 // 0: one reference, non-const.
3181 // n>0: n + 1 references, operations require a lock, const.
3182 // 4. All fields==0 is an empty string, given the extra storage
3183 // beyond-the-end for a null terminator; thus, the shared
3184 // empty string representation needs no constructor.
3185
3186 struct _Rep_base
3187 {
3188 size_type _M_length;
3189 size_type _M_capacity;
3190 _Atomic_word _M_refcount;
3191 };
3192
3193 struct _Rep : _Rep_base
3194 {
3195 // Types:
3197 rebind<char>::other _Raw_bytes_alloc;
3198
3199 // (Public) Data members:
3200
3201 // The maximum number of individual char_type elements of an
3202 // individual string is determined by _S_max_size. This is the
3203 // value that will be returned by max_size(). (Whereas npos
3204 // is the maximum number of bytes the allocator can allocate.)
3205 // If one was to divvy up the theoretical largest size string,
3206 // with a terminating character and m _CharT elements, it'd
3207 // look like this:
3208 // npos = sizeof(_Rep) + (m * sizeof(_CharT)) + sizeof(_CharT)
3209 // Solving for m:
3210 // m = ((npos - sizeof(_Rep))/sizeof(CharT)) - 1
3211 // In addition, this implementation quarters this amount.
3212 static const size_type _S_max_size;
3213 static const _CharT _S_terminal;
3214
3215 // The following storage is init'd to 0 by the linker, resulting
3216 // (carefully) in an empty string with one reference.
3217 static size_type _S_empty_rep_storage[];
3218
3219 static _Rep&
3220 _S_empty_rep() _GLIBCXX_NOEXCEPT
3221 {
3222 // NB: Mild hack to avoid strict-aliasing warnings. Note that
3223 // _S_empty_rep_storage is never modified and the punning should
3224 // be reasonably safe in this case.
3225 void* __p = reinterpret_cast<void*>(&_S_empty_rep_storage);
3226 return *reinterpret_cast<_Rep*>(__p);
3227 }
3228
3229 bool
3230 _M_is_leaked() const _GLIBCXX_NOEXCEPT
3231 {
3232#if defined(__GTHREADS)
3233 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3234 // so we need to use an atomic load. However, _M_is_leaked
3235 // predicate does not change concurrently (i.e. the string is either
3236 // leaked or not), so a relaxed load is enough.
3237 return __atomic_load_n(&this->_M_refcount, __ATOMIC_RELAXED) < 0;
3238#else
3239 return this->_M_refcount < 0;
3240#endif
3241 }
3242
3243 bool
3244 _M_is_shared() const _GLIBCXX_NOEXCEPT
3245 {
3246#if defined(__GTHREADS)
3247 // _M_refcount is mutated concurrently by _M_refcopy/_M_dispose,
3248 // so we need to use an atomic load. Another thread can drop last
3249 // but one reference concurrently with this check, so we need this
3250 // load to be acquire to synchronize with release fetch_and_add in
3251 // _M_dispose.
3252 return __atomic_load_n(&this->_M_refcount, __ATOMIC_ACQUIRE) > 0;
3253#else
3254 return this->_M_refcount > 0;
3255#endif
3256 }
3257
3258 void
3259 _M_set_leaked() _GLIBCXX_NOEXCEPT
3260 { this->_M_refcount = -1; }
3261
3262 void
3263 _M_set_sharable() _GLIBCXX_NOEXCEPT
3264 { this->_M_refcount = 0; }
3265
3266 void
3267 _M_set_length_and_sharable(size_type __n) _GLIBCXX_NOEXCEPT
3268 {
3269#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3270 if (__builtin_expect(this != &_S_empty_rep(), false))
3271#endif
3272 {
3273 this->_M_set_sharable(); // One reference.
3274 this->_M_length = __n;
3275 traits_type::assign(this->_M_refdata()[__n], _S_terminal);
3276 // grrr. (per 21.3.4)
3277 // You cannot leave those LWG people alone for a second.
3278 }
3279 }
3280
3281 _CharT*
3282 _M_refdata() throw()
3283 { return reinterpret_cast<_CharT*>(this + 1); }
3284
3285 _CharT*
3286 _M_grab(const _Alloc& __alloc1, const _Alloc& __alloc2)
3287 {
3288 return (!_M_is_leaked() && __alloc1 == __alloc2)
3289 ? _M_refcopy() : _M_clone(__alloc1);
3290 }
3291
3292 // Create & Destroy
3293 static _Rep*
3294 _S_create(size_type, size_type, const _Alloc&);
3295
3296 void
3297 _M_dispose(const _Alloc& __a) _GLIBCXX_NOEXCEPT
3298 {
3299#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3300 if (__builtin_expect(this != &_S_empty_rep(), false))
3301#endif
3302 {
3303 // Be race-detector-friendly. For more info see bits/c++config.
3304 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&this->_M_refcount);
3305 // Decrement of _M_refcount is acq_rel, because:
3306 // - all but last decrements need to release to synchronize with
3307 // the last decrement that will delete the object.
3308 // - the last decrement needs to acquire to synchronize with
3309 // all the previous decrements.
3310 // - last but one decrement needs to release to synchronize with
3311 // the acquire load in _M_is_shared that will conclude that
3312 // the object is not shared anymore.
3313 if (__gnu_cxx::__exchange_and_add_dispatch(&this->_M_refcount,
3314 -1) <= 0)
3315 {
3316 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&this->_M_refcount);
3317 _M_destroy(__a);
3318 }
3319 }
3320 } // XXX MT
3321
3322 void
3323 _M_destroy(const _Alloc&) throw();
3324
3325 _CharT*
3326 _M_refcopy() throw()
3327 {
3328#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3329 if (__builtin_expect(this != &_S_empty_rep(), false))
3330#endif
3331 __gnu_cxx::__atomic_add_dispatch(&this->_M_refcount, 1);
3332 return _M_refdata();
3333 } // XXX MT
3334
3335 _CharT*
3336 _M_clone(const _Alloc&, size_type __res = 0);
3337 };
3338
3339 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
3340 struct _Alloc_hider : _Alloc
3341 {
3342 _Alloc_hider(_CharT* __dat, const _Alloc& __a) _GLIBCXX_NOEXCEPT
3343 : _Alloc(__a), _M_p(__dat) { }
3344
3345 _CharT* _M_p; // The actual data.
3346 };
3347
3348 public:
3349 // Data Members (public):
3350 // NB: This is an unsigned type, and thus represents the maximum
3351 // size that the allocator can hold.
3352 /// Value returned by various member functions when they fail.
3353 static const size_type npos = static_cast<size_type>(-1);
3354
3355 private:
3356 // Data Members (private):
3357 mutable _Alloc_hider _M_dataplus;
3358
3359 _CharT*
3360 _M_data() const _GLIBCXX_NOEXCEPT
3361 { return _M_dataplus._M_p; }
3362
3363 _CharT*
3364 _M_data(_CharT* __p) _GLIBCXX_NOEXCEPT
3365 { return (_M_dataplus._M_p = __p); }
3366
3367 _Rep*
3368 _M_rep() const _GLIBCXX_NOEXCEPT
3369 { return &((reinterpret_cast<_Rep*> (_M_data()))[-1]); }
3370
3371 // For the internal use we have functions similar to `begin'/`end'
3372 // but they do not call _M_leak.
3373 iterator
3374 _M_ibegin() const _GLIBCXX_NOEXCEPT
3375 { return iterator(_M_data()); }
3376
3377 iterator
3378 _M_iend() const _GLIBCXX_NOEXCEPT
3379 { return iterator(_M_data() + this->size()); }
3380
3381 void
3382 _M_leak() // for use in begin() & non-const op[]
3383 {
3384 if (!_M_rep()->_M_is_leaked())
3385 _M_leak_hard();
3386 }
3387
3388 size_type
3389 _M_check(size_type __pos, const char* __s) const
3390 {
3391 if (__pos > this->size())
3392 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
3393 "this->size() (which is %zu)"),
3394 __s, __pos, this->size());
3395 return __pos;
3396 }
3397
3398 void
3399 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
3400 {
3401 if (this->max_size() - (this->size() - __n1) < __n2)
3402 __throw_length_error(__N(__s));
3403 }
3404
3405 // NB: _M_limit doesn't check for a bad __pos value.
3406 size_type
3407 _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
3408 {
3409 const bool __testoff = __off < this->size() - __pos;
3410 return __testoff ? __off : this->size() - __pos;
3411 }
3412
3413 // True if _Rep and source do not overlap.
3414 bool
3415 _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
3416 {
3417 return (less<const _CharT*>()(__s, _M_data())
3418 || less<const _CharT*>()(_M_data() + this->size(), __s));
3419 }
3420
3421 // When __n = 1 way faster than the general multichar
3422 // traits_type::copy/move/assign.
3423 static void
3424 _M_copy(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
3425 {
3426 if (__n == 1)
3427 traits_type::assign(*__d, *__s);
3428 else
3429 traits_type::copy(__d, __s, __n);
3430 }
3431
3432 static void
3433 _M_move(_CharT* __d, const _CharT* __s, size_type __n) _GLIBCXX_NOEXCEPT
3434 {
3435 if (__n == 1)
3436 traits_type::assign(*__d, *__s);
3437 else
3438 traits_type::move(__d, __s, __n);
3439 }
3440
3441 static void
3442 _M_assign(_CharT* __d, size_type __n, _CharT __c) _GLIBCXX_NOEXCEPT
3443 {
3444 if (__n == 1)
3445 traits_type::assign(*__d, __c);
3446 else
3447 traits_type::assign(__d, __n, __c);
3448 }
3449
3450 // _S_copy_chars is a separate template to permit specialization
3451 // to optimize for the common case of pointers as iterators.
3452 template<class _Iterator>
3453 static void
3454 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
3455 {
3456 for (; __k1 != __k2; ++__k1, (void)++__p)
3457 traits_type::assign(*__p, *__k1); // These types are off.
3458 }
3459
3460 static void
3461 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
3462 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
3463
3464 static void
3465 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
3466 _GLIBCXX_NOEXCEPT
3467 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
3468
3469 static void
3470 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
3471 { _M_copy(__p, __k1, __k2 - __k1); }
3472
3473 static void
3474 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
3475 _GLIBCXX_NOEXCEPT
3476 { _M_copy(__p, __k1, __k2 - __k1); }
3477
3478 static int
3479 _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
3480 {
3481 const difference_type __d = difference_type(__n1 - __n2);
3482
3483 if (__d > __gnu_cxx::__numeric_traits<int>::__max)
3484 return __gnu_cxx::__numeric_traits<int>::__max;
3485 else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
3486 return __gnu_cxx::__numeric_traits<int>::__min;
3487 else
3488 return int(__d);
3489 }
3490
3491 void
3492 _M_mutate(size_type __pos, size_type __len1, size_type __len2);
3493
3494 void
3495 _M_leak_hard();
3496
3497 static _Rep&
3498 _S_empty_rep() _GLIBCXX_NOEXCEPT
3499 { return _Rep::_S_empty_rep(); }
3500
3501#if __cplusplus >= 201703L
3502 // A helper type for avoiding boiler-plate.
3503 typedef basic_string_view<_CharT, _Traits> __sv_type;
3504
3505 template<typename _Tp, typename _Res>
3506 using _If_sv = enable_if_t<
3507 __and_<is_convertible<const _Tp&, __sv_type>,
3508 __not_<is_convertible<const _Tp*, const basic_string*>>,
3509 __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
3510 _Res>;
3511
3512 // Allows an implicit conversion to __sv_type.
3513 static __sv_type
3514 _S_to_string_view(__sv_type __svt) noexcept
3515 { return __svt; }
3516
3517 // Wraps a string_view by explicit conversion and thus
3518 // allows to add an internal constructor that does not
3519 // participate in overload resolution when a string_view
3520 // is provided.
3521 struct __sv_wrapper
3522 {
3523 explicit __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
3524 __sv_type _M_sv;
3525 };
3526
3527 /**
3528 * @brief Only internally used: Construct string from a string view
3529 * wrapper.
3530 * @param __svw string view wrapper.
3531 * @param __a Allocator to use.
3532 */
3533 explicit
3534 basic_string(__sv_wrapper __svw, const _Alloc& __a)
3535 : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
3536#endif
3537
3538 public:
3539 // Construct/copy/destroy:
3540 // NB: We overload ctors in some cases instead of using default
3541 // arguments, per 17.4.4.4 para. 2 item 2.
3542
3543 /**
3544 * @brief Default constructor creates an empty string.
3545 */
3547#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3548 _GLIBCXX_NOEXCEPT
3549 : _M_dataplus(_S_empty_rep()._M_refdata(), _Alloc())
3550#else
3551 : _M_dataplus(_S_construct(size_type(), _CharT(), _Alloc()), _Alloc())
3552#endif
3553 { }
3554
3555 /**
3556 * @brief Construct an empty string using allocator @a a.
3557 */
3558 explicit
3559 basic_string(const _Alloc& __a);
3560
3561 // NB: per LWG issue 42, semantics different from IS:
3562 /**
3563 * @brief Construct string with copy of value of @a str.
3564 * @param __str Source string.
3565 */
3566 basic_string(const basic_string& __str);
3567
3568 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3569 // 2583. no way to supply an allocator for basic_string(str, pos)
3570 /**
3571 * @brief Construct string as copy of a substring.
3572 * @param __str Source string.
3573 * @param __pos Index of first character to copy from.
3574 * @param __a Allocator to use.
3575 */
3576 basic_string(const basic_string& __str, size_type __pos,
3577 const _Alloc& __a = _Alloc());
3578
3579 /**
3580 * @brief Construct string as copy of a substring.
3581 * @param __str Source string.
3582 * @param __pos Index of first character to copy from.
3583 * @param __n Number of characters to copy.
3584 */
3585 basic_string(const basic_string& __str, size_type __pos,
3586 size_type __n);
3587 /**
3588 * @brief Construct string as copy of a substring.
3589 * @param __str Source string.
3590 * @param __pos Index of first character to copy from.
3591 * @param __n Number of characters to copy.
3592 * @param __a Allocator to use.
3593 */
3594 basic_string(const basic_string& __str, size_type __pos,
3595 size_type __n, const _Alloc& __a);
3596
3597 /**
3598 * @brief Construct string initialized by a character %array.
3599 * @param __s Source character %array.
3600 * @param __n Number of characters to copy.
3601 * @param __a Allocator to use (default is default allocator).
3602 *
3603 * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
3604 * has no special meaning.
3605 */
3606 basic_string(const _CharT* __s, size_type __n,
3607 const _Alloc& __a = _Alloc());
3608 /**
3609 * @brief Construct string as copy of a C string.
3610 * @param __s Source C string.
3611 * @param __a Allocator to use (default is default allocator).
3612 */
3613 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc());
3614 /**
3615 * @brief Construct string as multiple characters.
3616 * @param __n Number of characters.
3617 * @param __c Character to use.
3618 * @param __a Allocator to use (default is default allocator).
3619 */
3620 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc());
3621
3622#if __cplusplus >= 201103L
3623 /**
3624 * @brief Move construct string.
3625 * @param __str Source string.
3626 *
3627 * The newly-created string contains the exact contents of @a __str.
3628 * @a __str is a valid, but unspecified string.
3629 **/
3631#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3632 noexcept // FIXME C++11: should always be noexcept.
3633#endif
3634 : _M_dataplus(std::move(__str._M_dataplus))
3635 {
3636#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3637 __str._M_data(_S_empty_rep()._M_refdata());
3638#else
3639 __str._M_data(_S_construct(size_type(), _CharT(), get_allocator()));
3640#endif
3641 }
3642
3643 /**
3644 * @brief Construct string from an initializer %list.
3645 * @param __l std::initializer_list of characters.
3646 * @param __a Allocator to use (default is default allocator).
3647 */
3648 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc());
3649
3650 basic_string(const basic_string& __str, const _Alloc& __a)
3651 : _M_dataplus(__str._M_rep()->_M_grab(__a, __str.get_allocator()), __a)
3652 { }
3653
3654 basic_string(basic_string&& __str, const _Alloc& __a)
3655 : _M_dataplus(__str._M_data(), __a)
3656 {
3657 if (__a == __str.get_allocator())
3658 {
3659#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3660 __str._M_data(_S_empty_rep()._M_refdata());
3661#else
3662 __str._M_data(_S_construct(size_type(), _CharT(), __a));
3663#endif
3664 }
3665 else
3666 _M_dataplus._M_p = _S_construct(__str.begin(), __str.end(), __a);
3667 }
3668#endif // C++11
3669
3670 /**
3671 * @brief Construct string as copy of a range.
3672 * @param __beg Start of range.
3673 * @param __end End of range.
3674 * @param __a Allocator to use (default is default allocator).
3675 */
3676 template<class _InputIterator>
3677 basic_string(_InputIterator __beg, _InputIterator __end,
3678 const _Alloc& __a = _Alloc());
3679
3680#if __cplusplus >= 201703L
3681 /**
3682 * @brief Construct string from a substring of a string_view.
3683 * @param __t Source object convertible to string view.
3684 * @param __pos The index of the first character to copy from __t.
3685 * @param __n The number of characters to copy from __t.
3686 * @param __a Allocator to use.
3687 */
3688 template<typename _Tp, typename = _If_sv<_Tp, void>>
3689 basic_string(const _Tp& __t, size_type __pos, size_type __n,
3690 const _Alloc& __a = _Alloc())
3691 : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
3692
3693 /**
3694 * @brief Construct string from a string_view.
3695 * @param __t Source object convertible to string view.
3696 * @param __a Allocator to use (default is default allocator).
3697 */
3698 template<typename _Tp, typename = _If_sv<_Tp, void>>
3699 explicit
3700 basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
3701 : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
3702#endif // C++17
3703
3704 /**
3705 * @brief Destroy the string instance.
3706 */
3707 ~basic_string() _GLIBCXX_NOEXCEPT
3708 { _M_rep()->_M_dispose(this->get_allocator()); }
3709
3710 /**
3711 * @brief Assign the value of @a str to this string.
3712 * @param __str Source string.
3713 */
3716 { return this->assign(__str); }
3717
3718 /**
3719 * @brief Copy contents of @a s into this string.
3720 * @param __s Source null-terminated string.
3721 */
3723 operator=(const _CharT* __s)
3724 { return this->assign(__s); }
3725
3726 /**
3727 * @brief Set value to string of length 1.
3728 * @param __c Source character.
3729 *
3730 * Assigning to a character makes this string length 1 and
3731 * (*this)[0] == @a c.
3732 */
3734 operator=(_CharT __c)
3735 {
3736 this->assign(1, __c);
3737 return *this;
3738 }
3739
3740#if __cplusplus >= 201103L
3741 /**
3742 * @brief Move assign the value of @a str to this string.
3743 * @param __str Source string.
3744 *
3745 * The contents of @a str are moved into this string (without copying).
3746 * @a str is a valid, but unspecified string.
3747 **/
3751 {
3752 // NB: DR 1204.
3753 this->swap(__str);
3754 return *this;
3755 }
3756
3757 /**
3758 * @brief Set value to string constructed from initializer %list.
3759 * @param __l std::initializer_list.
3760 */
3763 {
3764 this->assign(__l.begin(), __l.size());
3765 return *this;
3766 }
3767#endif // C++11
3768
3769#if __cplusplus >= 201703L
3770 /**
3771 * @brief Set value to string constructed from a string_view.
3772 * @param __svt An object convertible to string_view.
3773 */
3774 template<typename _Tp>
3775 _If_sv<_Tp, basic_string&>
3776 operator=(const _Tp& __svt)
3777 { return this->assign(__svt); }
3778
3779 /**
3780 * @brief Convert to a string_view.
3781 * @return A string_view.
3782 */
3783 operator __sv_type() const noexcept
3784 { return __sv_type(data(), size()); }
3785#endif // C++17
3786
3787 // Iterators:
3788 /**
3789 * Returns a read/write iterator that points to the first character in
3790 * the %string. Unshares the string.
3791 */
3792 iterator
3793 begin() // FIXME C++11: should be noexcept.
3794 {
3795 _M_leak();
3796 return iterator(_M_data());
3797 }
3798
3799 /**
3800 * Returns a read-only (constant) iterator that points to the first
3801 * character in the %string.
3802 */
3803 const_iterator
3804 begin() const _GLIBCXX_NOEXCEPT
3805 { return const_iterator(_M_data()); }
3806
3807 /**
3808 * Returns a read/write iterator that points one past the last
3809 * character in the %string. Unshares the string.
3810 */
3811 iterator
3812 end() // FIXME C++11: should be noexcept.
3813 {
3814 _M_leak();
3815 return iterator(_M_data() + this->size());
3816 }
3817
3818 /**
3819 * Returns a read-only (constant) iterator that points one past the
3820 * last character in the %string.
3821 */
3822 const_iterator
3823 end() const _GLIBCXX_NOEXCEPT
3824 { return const_iterator(_M_data() + this->size()); }
3825
3826 /**
3827 * Returns a read/write reverse iterator that points to the last
3828 * character in the %string. Iteration is done in reverse element
3829 * order. Unshares the string.
3830 */
3831 reverse_iterator
3832 rbegin() // FIXME C++11: should be noexcept.
3833 { return reverse_iterator(this->end()); }
3834
3835 /**
3836 * Returns a read-only (constant) reverse iterator that points
3837 * to the last character in the %string. Iteration is done in
3838 * reverse element order.
3839 */
3840 const_reverse_iterator
3841 rbegin() const _GLIBCXX_NOEXCEPT
3842 { return const_reverse_iterator(this->end()); }
3843
3844 /**
3845 * Returns a read/write reverse iterator that points to one before the
3846 * first character in the %string. Iteration is done in reverse
3847 * element order. Unshares the string.
3848 */
3849 reverse_iterator
3850 rend() // FIXME C++11: should be noexcept.
3851 { return reverse_iterator(this->begin()); }
3852
3853 /**
3854 * Returns a read-only (constant) reverse iterator that points
3855 * to one before the first character in the %string. Iteration
3856 * is done in reverse element order.
3857 */
3858 const_reverse_iterator
3859 rend() const _GLIBCXX_NOEXCEPT
3860 { return const_reverse_iterator(this->begin()); }
3861
3862#if __cplusplus >= 201103L
3863 /**
3864 * Returns a read-only (constant) iterator that points to the first
3865 * character in the %string.
3866 */
3867 const_iterator
3868 cbegin() const noexcept
3869 { return const_iterator(this->_M_data()); }
3870
3871 /**
3872 * Returns a read-only (constant) iterator that points one past the
3873 * last character in the %string.
3874 */
3875 const_iterator
3876 cend() const noexcept
3877 { return const_iterator(this->_M_data() + this->size()); }
3878
3879 /**
3880 * Returns a read-only (constant) reverse iterator that points
3881 * to the last character in the %string. Iteration is done in
3882 * reverse element order.
3883 */
3884 const_reverse_iterator
3885 crbegin() const noexcept
3886 { return const_reverse_iterator(this->end()); }
3887
3888 /**
3889 * Returns a read-only (constant) reverse iterator that points
3890 * to one before the first character in the %string. Iteration
3891 * is done in reverse element order.
3892 */
3893 const_reverse_iterator
3894 crend() const noexcept
3895 { return const_reverse_iterator(this->begin()); }
3896#endif
3897
3898 public:
3899 // Capacity:
3900 /// Returns the number of characters in the string, not including any
3901 /// null-termination.
3902 size_type
3903 size() const _GLIBCXX_NOEXCEPT
3904 { return _M_rep()->_M_length; }
3905
3906 /// Returns the number of characters in the string, not including any
3907 /// null-termination.
3908 size_type
3909 length() const _GLIBCXX_NOEXCEPT
3910 { return _M_rep()->_M_length; }
3911
3912 /// Returns the size() of the largest possible %string.
3913 size_type
3914 max_size() const _GLIBCXX_NOEXCEPT
3915 { return _Rep::_S_max_size; }
3916
3917 /**
3918 * @brief Resizes the %string to the specified number of characters.
3919 * @param __n Number of characters the %string should contain.
3920 * @param __c Character to fill any new elements.
3921 *
3922 * This function will %resize the %string to the specified
3923 * number of characters. If the number is smaller than the
3924 * %string's current size the %string is truncated, otherwise
3925 * the %string is extended and new elements are %set to @a __c.
3926 */
3927 void
3928 resize(size_type __n, _CharT __c);
3929
3930 /**
3931 * @brief Resizes the %string to the specified number of characters.
3932 * @param __n Number of characters the %string should contain.
3933 *
3934 * This function will resize the %string to the specified length. If
3935 * the new size is smaller than the %string's current size the %string
3936 * is truncated, otherwise the %string is extended and new characters
3937 * are default-constructed. For basic types such as char, this means
3938 * setting them to 0.
3939 */
3940 void
3941 resize(size_type __n)
3942 { this->resize(__n, _CharT()); }
3943
3944#if __cplusplus >= 201103L
3945 /// A non-binding request to reduce capacity() to size().
3946 void
3947 shrink_to_fit() _GLIBCXX_NOEXCEPT
3948 {
3949#if __cpp_exceptions
3950 if (capacity() > size())
3951 {
3952 try
3953 { reserve(0); }
3954 catch(...)
3955 { }
3956 }
3957#endif
3958 }
3959#endif
3960
3961 /**
3962 * Returns the total number of characters that the %string can hold
3963 * before needing to allocate more memory.
3964 */
3965 size_type
3966 capacity() const _GLIBCXX_NOEXCEPT
3967 { return _M_rep()->_M_capacity; }
3968
3969 /**
3970 * @brief Attempt to preallocate enough memory for specified number of
3971 * characters.
3972 * @param __res_arg Number of characters required.
3973 * @throw std::length_error If @a __res_arg exceeds @c max_size().
3974 *
3975 * This function attempts to reserve enough memory for the
3976 * %string to hold the specified number of characters. If the
3977 * number requested is more than max_size(), length_error is
3978 * thrown.
3979 *
3980 * The advantage of this function is that if optimal code is a
3981 * necessity and the user can determine the string length that will be
3982 * required, the user can reserve the memory in %advance, and thus
3983 * prevent a possible reallocation of memory and copying of %string
3984 * data.
3985 */
3986 void
3987 reserve(size_type __res_arg = 0);
3988
3989 /**
3990 * Erases the string, making it empty.
3991 */
3992#if _GLIBCXX_FULLY_DYNAMIC_STRING == 0
3993 void
3994 clear() _GLIBCXX_NOEXCEPT
3995 {
3996 if (_M_rep()->_M_is_shared())
3997 {
3998 _M_rep()->_M_dispose(this->get_allocator());
3999 _M_data(_S_empty_rep()._M_refdata());
4000 }
4001 else
4002 _M_rep()->_M_set_length_and_sharable(0);
4003 }
4004#else
4005 // PR 56166: this should not throw.
4006 void
4007 clear()
4008 { _M_mutate(0, this->size(), 0); }
4009#endif
4010
4011 /**
4012 * Returns true if the %string is empty. Equivalent to
4013 * <code>*this == ""</code>.
4014 */
4015 _GLIBCXX_NODISCARD bool
4016 empty() const _GLIBCXX_NOEXCEPT
4017 { return this->size() == 0; }
4018
4019 // Element access:
4020 /**
4021 * @brief Subscript access to the data contained in the %string.
4022 * @param __pos The index of the character to access.
4023 * @return Read-only (constant) reference to the character.
4024 *
4025 * This operator allows for easy, array-style, data access.
4026 * Note that data access with this operator is unchecked and
4027 * out_of_range lookups are not defined. (For checked lookups
4028 * see at().)
4029 */
4030 const_reference
4031 operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
4032 {
4033 __glibcxx_assert(__pos <= size());
4034 return _M_data()[__pos];
4035 }
4036
4037 /**
4038 * @brief Subscript access to the data contained in the %string.
4039 * @param __pos The index of the character to access.
4040 * @return Read/write reference to the character.
4041 *
4042 * This operator allows for easy, array-style, data access.
4043 * Note that data access with this operator is unchecked and
4044 * out_of_range lookups are not defined. (For checked lookups
4045 * see at().) Unshares the string.
4046 */
4047 reference
4048 operator[](size_type __pos)
4049 {
4050 // Allow pos == size() both in C++98 mode, as v3 extension,
4051 // and in C++11 mode.
4052 __glibcxx_assert(__pos <= size());
4053 // In pedantic mode be strict in C++98 mode.
4054 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
4055 _M_leak();
4056 return _M_data()[__pos];
4057 }
4058
4059 /**
4060 * @brief Provides access to the data contained in the %string.
4061 * @param __n The index of the character to access.
4062 * @return Read-only (const) reference to the character.
4063 * @throw std::out_of_range If @a n is an invalid index.
4064 *
4065 * This function provides for safer data access. The parameter is
4066 * first checked that it is in the range of the string. The function
4067 * throws out_of_range if the check fails.
4068 */
4069 const_reference
4070 at(size_type __n) const
4071 {
4072 if (__n >= this->size())
4073 __throw_out_of_range_fmt(__N("basic_string::at: __n "
4074 "(which is %zu) >= this->size() "
4075 "(which is %zu)"),
4076 __n, this->size());
4077 return _M_data()[__n];
4078 }
4079
4080 /**
4081 * @brief Provides access to the data contained in the %string.
4082 * @param __n The index of the character to access.
4083 * @return Read/write reference to the character.
4084 * @throw std::out_of_range If @a n is an invalid index.
4085 *
4086 * This function provides for safer data access. The parameter is
4087 * first checked that it is in the range of the string. The function
4088 * throws out_of_range if the check fails. Success results in
4089 * unsharing the string.
4090 */
4091 reference
4092 at(size_type __n)
4093 {
4094 if (__n >= size())
4095 __throw_out_of_range_fmt(__N("basic_string::at: __n "
4096 "(which is %zu) >= this->size() "
4097 "(which is %zu)"),
4098 __n, this->size());
4099 _M_leak();
4100 return _M_data()[__n];
4101 }
4102
4103#if __cplusplus >= 201103L
4104 /**
4105 * Returns a read/write reference to the data at the first
4106 * element of the %string.
4107 */
4108 reference
4110 {
4111 __glibcxx_assert(!empty());
4112 return operator[](0);
4113 }
4114
4115 /**
4116 * Returns a read-only (constant) reference to the data at the first
4117 * element of the %string.
4118 */
4119 const_reference
4120 front() const noexcept
4121 {
4122 __glibcxx_assert(!empty());
4123 return operator[](0);
4124 }
4125
4126 /**
4127 * Returns a read/write reference to the data at the last
4128 * element of the %string.
4129 */
4130 reference
4132 {
4133 __glibcxx_assert(!empty());
4134 return operator[](this->size() - 1);
4135 }
4136
4137 /**
4138 * Returns a read-only (constant) reference to the data at the
4139 * last element of the %string.
4140 */
4141 const_reference
4142 back() const noexcept
4143 {
4144 __glibcxx_assert(!empty());
4145 return operator[](this->size() - 1);
4146 }
4147#endif
4148
4149 // Modifiers:
4150 /**
4151 * @brief Append a string to this string.
4152 * @param __str The string to append.
4153 * @return Reference to this string.
4154 */
4157 { return this->append(__str); }
4158
4159 /**
4160 * @brief Append a C string.
4161 * @param __s The C string to append.
4162 * @return Reference to this string.
4163 */
4165 operator+=(const _CharT* __s)
4166 { return this->append(__s); }
4167
4168 /**
4169 * @brief Append a character.
4170 * @param __c The character to append.
4171 * @return Reference to this string.
4172 */
4174 operator+=(_CharT __c)
4175 {
4176 this->push_back(__c);
4177 return *this;
4178 }
4179
4180#if __cplusplus >= 201103L
4181 /**
4182 * @brief Append an initializer_list of characters.
4183 * @param __l The initializer_list of characters to be appended.
4184 * @return Reference to this string.
4185 */
4188 { return this->append(__l.begin(), __l.size()); }
4189#endif // C++11
4190
4191#if __cplusplus >= 201703L
4192 /**
4193 * @brief Append a string_view.
4194 * @param __svt The object convertible to string_view to be appended.
4195 * @return Reference to this string.
4196 */
4197 template<typename _Tp>
4198 _If_sv<_Tp, basic_string&>
4199 operator+=(const _Tp& __svt)
4200 { return this->append(__svt); }
4201#endif // C++17
4202
4203 /**
4204 * @brief Append a string to this string.
4205 * @param __str The string to append.
4206 * @return Reference to this string.
4207 */
4209 append(const basic_string& __str);
4210
4211 /**
4212 * @brief Append a substring.
4213 * @param __str The string to append.
4214 * @param __pos Index of the first character of str to append.
4215 * @param __n The number of characters to append.
4216 * @return Reference to this string.
4217 * @throw std::out_of_range if @a __pos is not a valid index.
4218 *
4219 * This function appends @a __n characters from @a __str
4220 * starting at @a __pos to this string. If @a __n is is larger
4221 * than the number of available characters in @a __str, the
4222 * remainder of @a __str is appended.
4223 */
4225 append(const basic_string& __str, size_type __pos, size_type __n = npos);
4226
4227 /**
4228 * @brief Append a C substring.
4229 * @param __s The C string to append.
4230 * @param __n The number of characters to append.
4231 * @return Reference to this string.
4232 */
4234 append(const _CharT* __s, size_type __n);
4235
4236 /**
4237 * @brief Append a C string.
4238 * @param __s The C string to append.
4239 * @return Reference to this string.
4240 */
4242 append(const _CharT* __s)
4243 {
4244 __glibcxx_requires_string(__s);
4245 return this->append(__s, traits_type::length(__s));
4246 }
4247
4248 /**
4249 * @brief Append multiple characters.
4250 * @param __n The number of characters to append.
4251 * @param __c The character to use.
4252 * @return Reference to this string.
4253 *
4254 * Appends __n copies of __c to this string.
4255 */
4257 append(size_type __n, _CharT __c);
4258
4259#if __cplusplus >= 201103L
4260 /**
4261 * @brief Append an initializer_list of characters.
4262 * @param __l The initializer_list of characters to append.
4263 * @return Reference to this string.
4264 */
4267 { return this->append(__l.begin(), __l.size()); }
4268#endif // C++11
4269
4270 /**
4271 * @brief Append a range of characters.
4272 * @param __first Iterator referencing the first character to append.
4273 * @param __last Iterator marking the end of the range.
4274 * @return Reference to this string.
4275 *
4276 * Appends characters in the range [__first,__last) to this string.
4277 */
4278 template<class _InputIterator>
4280 append(_InputIterator __first, _InputIterator __last)
4281 { return this->replace(_M_iend(), _M_iend(), __first, __last); }
4282
4283#if __cplusplus >= 201703L
4284 /**
4285 * @brief Append a string_view.
4286 * @param __svt The object convertible to string_view to be appended.
4287 * @return Reference to this string.
4288 */
4289 template<typename _Tp>
4290 _If_sv<_Tp, basic_string&>
4291 append(const _Tp& __svt)
4292 {
4293 __sv_type __sv = __svt;
4294 return this->append(__sv.data(), __sv.size());
4295 }
4296
4297 /**
4298 * @brief Append a range of characters from a string_view.
4299 * @param __svt The object convertible to string_view to be appended
4300 * from.
4301 * @param __pos The position in the string_view to append from.
4302 * @param __n The number of characters to append from the string_view.
4303 * @return Reference to this string.
4304 */
4305 template<typename _Tp>
4306 _If_sv<_Tp, basic_string&>
4307 append(const _Tp& __svt, size_type __pos, size_type __n = npos)
4308 {
4309 __sv_type __sv = __svt;
4310 return append(__sv.data()
4311 + std::__sv_check(__sv.size(), __pos, "basic_string::append"),
4312 std::__sv_limit(__sv.size(), __pos, __n));
4313 }
4314#endif // C++17
4315
4316 /**
4317 * @brief Append a single character.
4318 * @param __c Character to append.
4319 */
4320 void
4321 push_back(_CharT __c)
4322 {
4323 const size_type __len = 1 + this->size();
4324 if (__len > this->capacity() || _M_rep()->_M_is_shared())
4325 this->reserve(__len);
4326 traits_type::assign(_M_data()[this->size()], __c);
4327 _M_rep()->_M_set_length_and_sharable(__len);
4328 }
4329
4330 /**
4331 * @brief Set value to contents of another string.
4332 * @param __str Source string to use.
4333 * @return Reference to this string.
4334 */
4336 assign(const basic_string& __str);
4337
4338#if __cplusplus >= 201103L
4339 /**
4340 * @brief Set value to contents of another string.
4341 * @param __str Source string to use.
4342 * @return Reference to this string.
4343 *
4344 * This function sets this string to the exact contents of @a __str.
4345 * @a __str is a valid, but unspecified string.
4346 */
4350 {
4351 this->swap(__str);
4352 return *this;
4353 }
4354#endif // C++11
4355
4356 /**
4357 * @brief Set value to a substring of a string.
4358 * @param __str The string to use.
4359 * @param __pos Index of the first character of str.
4360 * @param __n Number of characters to use.
4361 * @return Reference to this string.
4362 * @throw std::out_of_range if @a pos is not a valid index.
4363 *
4364 * This function sets this string to the substring of @a __str
4365 * consisting of @a __n characters at @a __pos. If @a __n is
4366 * is larger than the number of available characters in @a
4367 * __str, the remainder of @a __str is used.
4368 */
4370 assign(const basic_string& __str, size_type __pos, size_type __n = npos)
4371 { return this->assign(__str._M_data()
4372 + __str._M_check(__pos, "basic_string::assign"),
4373 __str._M_limit(__pos, __n)); }
4374
4375 /**
4376 * @brief Set value to a C substring.
4377 * @param __s The C string to use.
4378 * @param __n Number of characters to use.
4379 * @return Reference to this string.
4380 *
4381 * This function sets the value of this string to the first @a __n
4382 * characters of @a __s. If @a __n is is larger than the number of
4383 * available characters in @a __s, the remainder of @a __s is used.
4384 */
4386 assign(const _CharT* __s, size_type __n);
4387
4388 /**
4389 * @brief Set value to contents of a C string.
4390 * @param __s The C string to use.
4391 * @return Reference to this string.
4392 *
4393 * This function sets the value of this string to the value of @a __s.
4394 * The data is copied, so there is no dependence on @a __s once the
4395 * function returns.
4396 */
4398 assign(const _CharT* __s)
4399 {
4400 __glibcxx_requires_string(__s);
4401 return this->assign(__s, traits_type::length(__s));
4402 }
4403
4404 /**
4405 * @brief Set value to multiple characters.
4406 * @param __n Length of the resulting string.
4407 * @param __c The character to use.
4408 * @return Reference to this string.
4409 *
4410 * This function sets the value of this string to @a __n copies of
4411 * character @a __c.
4412 */
4414 assign(size_type __n, _CharT __c)
4415 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
4416
4417 /**
4418 * @brief Set value to a range of characters.
4419 * @param __first Iterator referencing the first character to append.
4420 * @param __last Iterator marking the end of the range.
4421 * @return Reference to this string.
4422 *
4423 * Sets value of string to characters in the range [__first,__last).
4424 */
4425 template<class _InputIterator>
4427 assign(_InputIterator __first, _InputIterator __last)
4428 { return this->replace(_M_ibegin(), _M_iend(), __first, __last); }
4429
4430#if __cplusplus >= 201103L
4431 /**
4432 * @brief Set value to an initializer_list of characters.
4433 * @param __l The initializer_list of characters to assign.
4434 * @return Reference to this string.
4435 */
4438 { return this->assign(__l.begin(), __l.size()); }
4439#endif // C++11
4440
4441#if __cplusplus >= 201703L
4442 /**
4443 * @brief Set value from a string_view.
4444 * @param __svt The source object convertible to string_view.
4445 * @return Reference to this string.
4446 */
4447 template<typename _Tp>
4448 _If_sv<_Tp, basic_string&>
4449 assign(const _Tp& __svt)
4450 {
4451 __sv_type __sv = __svt;
4452 return this->assign(__sv.data(), __sv.size());
4453 }
4454
4455 /**
4456 * @brief Set value from a range of characters in a string_view.
4457 * @param __svt The source object convertible to string_view.
4458 * @param __pos The position in the string_view to assign from.
4459 * @param __n The number of characters to assign.
4460 * @return Reference to this string.
4461 */
4462 template<typename _Tp>
4463 _If_sv<_Tp, basic_string&>
4464 assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
4465 {
4466 __sv_type __sv = __svt;
4467 return assign(__sv.data()
4468 + std::__sv_check(__sv.size(), __pos, "basic_string::assign"),
4469 std::__sv_limit(__sv.size(), __pos, __n));
4470 }
4471#endif // C++17
4472
4473 /**
4474 * @brief Insert multiple characters.
4475 * @param __p Iterator referencing location in string to insert at.
4476 * @param __n Number of characters to insert
4477 * @param __c The character to insert.
4478 * @throw std::length_error If new length exceeds @c max_size().
4479 *
4480 * Inserts @a __n copies of character @a __c starting at the
4481 * position referenced by iterator @a __p. If adding
4482 * characters causes the length to exceed max_size(),
4483 * length_error is thrown. The value of the string doesn't
4484 * change if an error is thrown.
4485 */
4486 void
4487 insert(iterator __p, size_type __n, _CharT __c)
4488 { this->replace(__p, __p, __n, __c); }
4489
4490 /**
4491 * @brief Insert a range of characters.
4492 * @param __p Iterator referencing location in string to insert at.
4493 * @param __beg Start of range.
4494 * @param __end End of range.
4495 * @throw std::length_error If new length exceeds @c max_size().
4496 *
4497 * Inserts characters in range [__beg,__end). If adding
4498 * characters causes the length to exceed max_size(),
4499 * length_error is thrown. The value of the string doesn't
4500 * change if an error is thrown.
4501 */
4502 template<class _InputIterator>
4503 void
4504 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
4505 { this->replace(__p, __p, __beg, __end); }
4506
4507#if __cplusplus >= 201103L
4508 /**
4509 * @brief Insert an initializer_list of characters.
4510 * @param __p Iterator referencing location in string to insert at.
4511 * @param __l The initializer_list of characters to insert.
4512 * @throw std::length_error If new length exceeds @c max_size().
4513 */
4514 void
4516 {
4517 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
4518 this->insert(__p - _M_ibegin(), __l.begin(), __l.size());
4519 }
4520#endif // C++11
4521
4522 /**
4523 * @brief Insert value of a string.
4524 * @param __pos1 Position in string to insert at.
4525 * @param __str The string to insert.
4526 * @return Reference to this string.
4527 * @throw std::length_error If new length exceeds @c max_size().
4528 *
4529 * Inserts value of @a __str starting at @a __pos1. If adding
4530 * characters causes the length to exceed max_size(),
4531 * length_error is thrown. The value of the string doesn't
4532 * change if an error is thrown.
4533 */
4535 insert(size_type __pos1, const basic_string& __str)
4536 { return this->insert(__pos1, __str, size_type(0), __str.size()); }
4537
4538 /**
4539 * @brief Insert a substring.
4540 * @param __pos1 Position in string to insert at.
4541 * @param __str The string to insert.
4542 * @param __pos2 Start of characters in str to insert.
4543 * @param __n Number of characters to insert.
4544 * @return Reference to this string.
4545 * @throw std::length_error If new length exceeds @c max_size().
4546 * @throw std::out_of_range If @a pos1 > size() or
4547 * @a __pos2 > @a str.size().
4548 *
4549 * Starting at @a pos1, insert @a __n character of @a __str
4550 * beginning with @a __pos2. If adding characters causes the
4551 * length to exceed max_size(), length_error is thrown. If @a
4552 * __pos1 is beyond the end of this string or @a __pos2 is
4553 * beyond the end of @a __str, out_of_range is thrown. The
4554 * value of the string doesn't change if an error is thrown.
4555 */
4557 insert(size_type __pos1, const basic_string& __str,
4558 size_type __pos2, size_type __n = npos)
4559 { return this->insert(__pos1, __str._M_data()
4560 + __str._M_check(__pos2, "basic_string::insert"),
4561 __str._M_limit(__pos2, __n)); }
4562
4563 /**
4564 * @brief Insert a C substring.
4565 * @param __pos Position in string to insert at.
4566 * @param __s The C string to insert.
4567 * @param __n The number of characters to insert.
4568 * @return Reference to this string.
4569 * @throw std::length_error If new length exceeds @c max_size().
4570 * @throw std::out_of_range If @a __pos is beyond the end of this
4571 * string.
4572 *
4573 * Inserts the first @a __n characters of @a __s starting at @a
4574 * __pos. If adding characters causes the length to exceed
4575 * max_size(), length_error is thrown. If @a __pos is beyond
4576 * end(), out_of_range is thrown. The value of the string
4577 * doesn't change if an error is thrown.
4578 */
4580 insert(size_type __pos, const _CharT* __s, size_type __n);
4581
4582 /**
4583 * @brief Insert a C string.
4584 * @param __pos Position in string to insert at.
4585 * @param __s The C string to insert.
4586 * @return Reference to this string.
4587 * @throw std::length_error If new length exceeds @c max_size().
4588 * @throw std::out_of_range If @a pos is beyond the end of this
4589 * string.
4590 *
4591 * Inserts the first @a n characters of @a __s starting at @a __pos. If
4592 * adding characters causes the length to exceed max_size(),
4593 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
4594 * thrown. The value of the string doesn't change if an error is
4595 * thrown.
4596 */
4598 insert(size_type __pos, const _CharT* __s)
4599 {
4600 __glibcxx_requires_string(__s);
4601 return this->insert(__pos, __s, traits_type::length(__s));
4602 }
4603
4604 /**
4605 * @brief Insert multiple characters.
4606 * @param __pos Index in string to insert at.
4607 * @param __n Number of characters to insert
4608 * @param __c The character to insert.
4609 * @return Reference to this string.
4610 * @throw std::length_error If new length exceeds @c max_size().
4611 * @throw std::out_of_range If @a __pos is beyond the end of this
4612 * string.
4613 *
4614 * Inserts @a __n copies of character @a __c starting at index
4615 * @a __pos. If adding characters causes the length to exceed
4616 * max_size(), length_error is thrown. If @a __pos > length(),
4617 * out_of_range is thrown. The value of the string doesn't
4618 * change if an error is thrown.
4619 */
4621 insert(size_type __pos, size_type __n, _CharT __c)
4622 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
4623 size_type(0), __n, __c); }
4624
4625 /**
4626 * @brief Insert one character.
4627 * @param __p Iterator referencing position in string to insert at.
4628 * @param __c The character to insert.
4629 * @return Iterator referencing newly inserted char.
4630 * @throw std::length_error If new length exceeds @c max_size().
4631 *
4632 * Inserts character @a __c at position referenced by @a __p.
4633 * If adding character causes the length to exceed max_size(),
4634 * length_error is thrown. If @a __p is beyond end of string,
4635 * out_of_range is thrown. The value of the string doesn't
4636 * change if an error is thrown.
4637 */
4638 iterator
4639 insert(iterator __p, _CharT __c)
4640 {
4641 _GLIBCXX_DEBUG_PEDASSERT(__p >= _M_ibegin() && __p <= _M_iend());
4642 const size_type __pos = __p - _M_ibegin();
4643 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
4644 _M_rep()->_M_set_leaked();
4645 return iterator(_M_data() + __pos);
4646 }
4647
4648#if __cplusplus >= 201703L
4649 /**
4650 * @brief Insert a string_view.
4651 * @param __pos Position in string to insert at.
4652 * @param __svt The object convertible to string_view to insert.
4653 * @return Reference to this string.
4654 */
4655 template<typename _Tp>
4656 _If_sv<_Tp, basic_string&>
4657 insert(size_type __pos, const _Tp& __svt)
4658 {
4659 __sv_type __sv = __svt;
4660 return this->insert(__pos, __sv.data(), __sv.size());
4661 }
4662
4663 /**
4664 * @brief Insert a string_view.
4665 * @param __pos Position in string to insert at.
4666 * @param __svt The object convertible to string_view to insert from.
4667 * @param __pos Position in string_view to insert
4668 * from.
4669 * @param __n The number of characters to insert.
4670 * @return Reference to this string.
4671 */
4672 template<typename _Tp>
4673 _If_sv<_Tp, basic_string&>
4674 insert(size_type __pos1, const _Tp& __svt,
4675 size_type __pos2, size_type __n = npos)
4676 {
4677 __sv_type __sv = __svt;
4678 return this->replace(__pos1, size_type(0), __sv.data()
4679 + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"),
4680 std::__sv_limit(__sv.size(), __pos2, __n));
4681 }
4682#endif // C++17
4683
4684 /**
4685 * @brief Remove characters.
4686 * @param __pos Index of first character to remove (default 0).
4687 * @param __n Number of characters to remove (default remainder).
4688 * @return Reference to this string.
4689 * @throw std::out_of_range If @a pos is beyond the end of this
4690 * string.
4691 *
4692 * Removes @a __n characters from this string starting at @a
4693 * __pos. The length of the string is reduced by @a __n. If
4694 * there are < @a __n characters to remove, the remainder of
4695 * the string is truncated. If @a __p is beyond end of string,
4696 * out_of_range is thrown. The value of the string doesn't
4697 * change if an error is thrown.
4698 */
4700 erase(size_type __pos = 0, size_type __n = npos)
4701 {
4702 _M_mutate(_M_check(__pos, "basic_string::erase"),
4703 _M_limit(__pos, __n), size_type(0));
4704 return *this;
4705 }
4706
4707 /**
4708 * @brief Remove one character.
4709 * @param __position Iterator referencing the character to remove.
4710 * @return iterator referencing same location after removal.
4711 *
4712 * Removes the character at @a __position from this string. The value
4713 * of the string doesn't change if an error is thrown.
4714 */
4715 iterator
4716 erase(iterator __position)
4717 {
4718 _GLIBCXX_DEBUG_PEDASSERT(__position >= _M_ibegin()
4719 && __position < _M_iend());
4720 const size_type __pos = __position - _M_ibegin();
4721 _M_mutate(__pos, size_type(1), size_type(0));
4722 _M_rep()->_M_set_leaked();
4723 return iterator(_M_data() + __pos);
4724 }
4725
4726 /**
4727 * @brief Remove a range of characters.
4728 * @param __first Iterator referencing the first character to remove.
4729 * @param __last Iterator referencing the end of the range.
4730 * @return Iterator referencing location of first after removal.
4731 *
4732 * Removes the characters in the range [first,last) from this string.
4733 * The value of the string doesn't change if an error is thrown.
4734 */
4735 iterator
4736 erase(iterator __first, iterator __last);
4737
4738#if __cplusplus >= 201103L
4739 /**
4740 * @brief Remove the last character.
4741 *
4742 * The string must be non-empty.
4743 */
4744 void
4745 pop_back() // FIXME C++11: should be noexcept.
4746 {
4747 __glibcxx_assert(!empty());
4748 erase(size() - 1, 1);
4749 }
4750#endif // C++11
4751
4752 /**
4753 * @brief Replace characters with value from another string.
4754 * @param __pos Index of first character to replace.
4755 * @param __n Number of characters to be replaced.
4756 * @param __str String to insert.
4757 * @return Reference to this string.
4758 * @throw std::out_of_range If @a pos is beyond the end of this
4759 * string.
4760 * @throw std::length_error If new length exceeds @c max_size().
4761 *
4762 * Removes the characters in the range [__pos,__pos+__n) from
4763 * this string. In place, the value of @a __str is inserted.
4764 * If @a __pos is beyond end of string, out_of_range is thrown.
4765 * If the length of the result exceeds max_size(), length_error
4766 * is thrown. The value of the string doesn't change if an
4767 * error is thrown.
4768 */
4770 replace(size_type __pos, size_type __n, const basic_string& __str)
4771 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
4772
4773 /**
4774 * @brief Replace characters with value from another string.
4775 * @param __pos1 Index of first character to replace.
4776 * @param __n1 Number of characters to be replaced.
4777 * @param __str String to insert.
4778 * @param __pos2 Index of first character of str to use.
4779 * @param __n2 Number of characters from str to use.
4780 * @return Reference to this string.
4781 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
4782 * __str.size().
4783 * @throw std::length_error If new length exceeds @c max_size().
4784 *
4785 * Removes the characters in the range [__pos1,__pos1 + n) from this
4786 * string. In place, the value of @a __str is inserted. If @a __pos is
4787 * beyond end of string, out_of_range is thrown. If the length of the
4788 * result exceeds max_size(), length_error is thrown. The value of the
4789 * string doesn't change if an error is thrown.
4790 */
4792 replace(size_type __pos1, size_type __n1, const basic_string& __str,
4793 size_type __pos2, size_type __n2 = npos)
4794 { return this->replace(__pos1, __n1, __str._M_data()
4795 + __str._M_check(__pos2, "basic_string::replace"),
4796 __str._M_limit(__pos2, __n2)); }
4797
4798 /**
4799 * @brief Replace characters with value of a C substring.
4800 * @param __pos Index of first character to replace.
4801 * @param __n1 Number of characters to be replaced.
4802 * @param __s C string to insert.
4803 * @param __n2 Number of characters from @a s to use.
4804 * @return Reference to this string.
4805 * @throw std::out_of_range If @a pos1 > size().
4806 * @throw std::length_error If new length exceeds @c max_size().
4807 *
4808 * Removes the characters in the range [__pos,__pos + __n1)
4809 * from this string. In place, the first @a __n2 characters of
4810 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
4811 * @a __pos is beyond end of string, out_of_range is thrown. If
4812 * the length of result exceeds max_size(), length_error is
4813 * thrown. The value of the string doesn't change if an error
4814 * is thrown.
4815 */
4817 replace(size_type __pos, size_type __n1, const _CharT* __s,
4818 size_type __n2);
4819
4820 /**
4821 * @brief Replace characters with value of a C string.
4822 * @param __pos Index of first character to replace.
4823 * @param __n1 Number of characters to be replaced.
4824 * @param __s C string to insert.
4825 * @return Reference to this string.
4826 * @throw std::out_of_range If @a pos > size().
4827 * @throw std::length_error If new length exceeds @c max_size().
4828 *
4829 * Removes the characters in the range [__pos,__pos + __n1)
4830 * from this string. In place, the characters of @a __s are
4831 * inserted. If @a __pos is beyond end of string, out_of_range
4832 * is thrown. If the length of result exceeds max_size(),
4833 * length_error is thrown. The value of the string doesn't
4834 * change if an error is thrown.
4835 */
4837 replace(size_type __pos, size_type __n1, const _CharT* __s)
4838 {
4839 __glibcxx_requires_string(__s);
4840 return this->replace(__pos, __n1, __s, traits_type::length(__s));
4841 }
4842
4843 /**
4844 * @brief Replace characters with multiple characters.
4845 * @param __pos Index of first character to replace.
4846 * @param __n1 Number of characters to be replaced.
4847 * @param __n2 Number of characters to insert.
4848 * @param __c Character to insert.
4849 * @return Reference to this string.
4850 * @throw std::out_of_range If @a __pos > size().
4851 * @throw std::length_error If new length exceeds @c max_size().
4852 *
4853 * Removes the characters in the range [pos,pos + n1) from this
4854 * string. In place, @a __n2 copies of @a __c are inserted.
4855 * If @a __pos is beyond end of string, out_of_range is thrown.
4856 * If the length of result exceeds max_size(), length_error is
4857 * thrown. The value of the string doesn't change if an error
4858 * is thrown.
4859 */
4861 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
4862 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
4863 _M_limit(__pos, __n1), __n2, __c); }
4864
4865 /**
4866 * @brief Replace range of characters with string.
4867 * @param __i1 Iterator referencing start of range to replace.
4868 * @param __i2 Iterator referencing end of range to replace.
4869 * @param __str String value to insert.
4870 * @return Reference to this string.
4871 * @throw std::length_error If new length exceeds @c max_size().
4872 *
4873 * Removes the characters in the range [__i1,__i2). In place,
4874 * the value of @a __str is inserted. If the length of result
4875 * exceeds max_size(), length_error is thrown. The value of
4876 * the string doesn't change if an error is thrown.
4877 */
4879 replace(iterator __i1, iterator __i2, const basic_string& __str)
4880 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
4881
4882 /**
4883 * @brief Replace range of characters with C substring.
4884 * @param __i1 Iterator referencing start of range to replace.
4885 * @param __i2 Iterator referencing end of range to replace.
4886 * @param __s C string value to insert.
4887 * @param __n Number of characters from s to insert.
4888 * @return Reference to this string.
4889 * @throw std::length_error If new length exceeds @c max_size().
4890 *
4891 * Removes the characters in the range [__i1,__i2). In place,
4892 * the first @a __n characters of @a __s are inserted. If the
4893 * length of result exceeds max_size(), length_error is thrown.
4894 * The value of the string doesn't change if an error is
4895 * thrown.
4896 */
4898 replace(iterator __i1, iterator __i2, const _CharT* __s, size_type __n)
4899 {
4900 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4901 && __i2 <= _M_iend());
4902 return this->replace(__i1 - _M_ibegin(), __i2 - __i1, __s, __n);
4903 }
4904
4905 /**
4906 * @brief Replace range of characters with C string.
4907 * @param __i1 Iterator referencing start of range to replace.
4908 * @param __i2 Iterator referencing end of range to replace.
4909 * @param __s C string value to insert.
4910 * @return Reference to this string.
4911 * @throw std::length_error If new length exceeds @c max_size().
4912 *
4913 * Removes the characters in the range [__i1,__i2). In place,
4914 * the characters of @a __s are inserted. If the length of
4915 * result exceeds max_size(), length_error is thrown. The
4916 * value of the string doesn't change if an error is thrown.
4917 */
4919 replace(iterator __i1, iterator __i2, const _CharT* __s)
4920 {
4921 __glibcxx_requires_string(__s);
4922 return this->replace(__i1, __i2, __s, traits_type::length(__s));
4923 }
4924
4925 /**
4926 * @brief Replace range of characters with multiple characters
4927 * @param __i1 Iterator referencing start of range to replace.
4928 * @param __i2 Iterator referencing end of range to replace.
4929 * @param __n Number of characters to insert.
4930 * @param __c Character to insert.
4931 * @return Reference to this string.
4932 * @throw std::length_error If new length exceeds @c max_size().
4933 *
4934 * Removes the characters in the range [__i1,__i2). In place,
4935 * @a __n copies of @a __c are inserted. If the length of
4936 * result exceeds max_size(), length_error is thrown. The
4937 * value of the string doesn't change if an error is thrown.
4938 */
4940 replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
4941 {
4942 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4943 && __i2 <= _M_iend());
4944 return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __c);
4945 }
4946
4947 /**
4948 * @brief Replace range of characters with range.
4949 * @param __i1 Iterator referencing start of range to replace.
4950 * @param __i2 Iterator referencing end of range to replace.
4951 * @param __k1 Iterator referencing start of range to insert.
4952 * @param __k2 Iterator referencing end of range to insert.
4953 * @return Reference to this string.
4954 * @throw std::length_error If new length exceeds @c max_size().
4955 *
4956 * Removes the characters in the range [__i1,__i2). In place,
4957 * characters in the range [__k1,__k2) are inserted. If the
4958 * length of result exceeds max_size(), length_error is thrown.
4959 * The value of the string doesn't change if an error is
4960 * thrown.
4961 */
4962 template<class _InputIterator>
4964 replace(iterator __i1, iterator __i2,
4965 _InputIterator __k1, _InputIterator __k2)
4966 {
4967 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4968 && __i2 <= _M_iend());
4969 __glibcxx_requires_valid_range(__k1, __k2);
4970 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
4971 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
4972 }
4973
4974 // Specializations for the common case of pointer and iterator:
4975 // useful to avoid the overhead of temporary buffering in _M_replace.
4977 replace(iterator __i1, iterator __i2, _CharT* __k1, _CharT* __k2)
4978 {
4979 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4980 && __i2 <= _M_iend());
4981 __glibcxx_requires_valid_range(__k1, __k2);
4982 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4983 __k1, __k2 - __k1);
4984 }
4985
4987 replace(iterator __i1, iterator __i2,
4988 const _CharT* __k1, const _CharT* __k2)
4989 {
4990 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
4991 && __i2 <= _M_iend());
4992 __glibcxx_requires_valid_range(__k1, __k2);
4993 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
4994 __k1, __k2 - __k1);
4995 }
4996
4998 replace(iterator __i1, iterator __i2, iterator __k1, iterator __k2)
4999 {
5000 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
5001 && __i2 <= _M_iend());
5002 __glibcxx_requires_valid_range(__k1, __k2);
5003 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
5004 __k1.base(), __k2 - __k1);
5005 }
5006
5008 replace(iterator __i1, iterator __i2,
5009 const_iterator __k1, const_iterator __k2)
5010 {
5011 _GLIBCXX_DEBUG_PEDASSERT(_M_ibegin() <= __i1 && __i1 <= __i2
5012 && __i2 <= _M_iend());
5013 __glibcxx_requires_valid_range(__k1, __k2);
5014 return this->replace(__i1 - _M_ibegin(), __i2 - __i1,
5015 __k1.base(), __k2 - __k1);
5016 }
5017
5018#if __cplusplus >= 201103L
5019 /**
5020 * @brief Replace range of characters with initializer_list.
5021 * @param __i1 Iterator referencing start of range to replace.
5022 * @param __i2 Iterator referencing end of range to replace.
5023 * @param __l The initializer_list of characters to insert.
5024 * @return Reference to this string.
5025 * @throw std::length_error If new length exceeds @c max_size().
5026 *
5027 * Removes the characters in the range [__i1,__i2). In place,
5028 * characters in the range [__k1,__k2) are inserted. If the
5029 * length of result exceeds max_size(), length_error is thrown.
5030 * The value of the string doesn't change if an error is
5031 * thrown.
5032 */
5033 basic_string& replace(iterator __i1, iterator __i2,
5035 { return this->replace(__i1, __i2, __l.begin(), __l.end()); }
5036#endif // C++11
5037
5038#if __cplusplus >= 201703L
5039 /**
5040 * @brief Replace range of characters with string_view.
5041 * @param __pos The position to replace at.
5042 * @param __n The number of characters to replace.
5043 * @param __svt The object convertible to string_view to insert.
5044 * @return Reference to this string.
5045 */
5046 template<typename _Tp>
5047 _If_sv<_Tp, basic_string&>
5048 replace(size_type __pos, size_type __n, const _Tp& __svt)
5049 {
5050 __sv_type __sv = __svt;
5051 return this->replace(__pos, __n, __sv.data(), __sv.size());
5052 }
5053
5054 /**
5055 * @brief Replace range of characters with string_view.
5056 * @param __pos1 The position to replace at.
5057 * @param __n1 The number of characters to replace.
5058 * @param __svt The object convertible to string_view to insert from.
5059 * @param __pos2 The position in the string_view to insert from.
5060 * @param __n2 The number of characters to insert.
5061 * @return Reference to this string.
5062 */
5063 template<typename _Tp>
5064 _If_sv<_Tp, basic_string&>
5065 replace(size_type __pos1, size_type __n1, const _Tp& __svt,
5066 size_type __pos2, size_type __n2 = npos)
5067 {
5068 __sv_type __sv = __svt;
5069 return this->replace(__pos1, __n1,
5070 __sv.data()
5071 + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"),
5072 std::__sv_limit(__sv.size(), __pos2, __n2));
5073 }
5074
5075 /**
5076 * @brief Replace range of characters with string_view.
5077 * @param __i1 An iterator referencing the start position
5078 to replace at.
5079 * @param __i2 An iterator referencing the end position
5080 for the replace.
5081 * @param __svt The object convertible to string_view to insert from.
5082 * @return Reference to this string.
5083 */
5084 template<typename _Tp>
5085 _If_sv<_Tp, basic_string&>
5086 replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
5087 {
5088 __sv_type __sv = __svt;
5089 return this->replace(__i1 - begin(), __i2 - __i1, __sv);
5090 }
5091#endif // C++17
5092
5093 private:
5094 template<class _Integer>
5096 _M_replace_dispatch(iterator __i1, iterator __i2, _Integer __n,
5097 _Integer __val, __true_type)
5098 { return _M_replace_aux(__i1 - _M_ibegin(), __i2 - __i1, __n, __val); }
5099
5100 template<class _InputIterator>
5102 _M_replace_dispatch(iterator __i1, iterator __i2, _InputIterator __k1,
5103 _InputIterator __k2, __false_type);
5104
5106 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
5107 _CharT __c);
5108
5110 _M_replace_safe(size_type __pos1, size_type __n1, const _CharT* __s,
5111 size_type __n2);
5112
5113 // _S_construct_aux is used to implement the 21.3.1 para 15 which
5114 // requires special behaviour if _InIter is an integral type
5115 template<class _InIterator>
5116 static _CharT*
5117 _S_construct_aux(_InIterator __beg, _InIterator __end,
5118 const _Alloc& __a, __false_type)
5119 {
5120 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
5121 return _S_construct(__beg, __end, __a, _Tag());
5122 }
5123
5124 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5125 // 438. Ambiguity in the "do the right thing" clause
5126 template<class _Integer>
5127 static _CharT*
5128 _S_construct_aux(_Integer __beg, _Integer __end,
5129 const _Alloc& __a, __true_type)
5130 { return _S_construct_aux_2(static_cast<size_type>(__beg),
5131 __end, __a); }
5132
5133 static _CharT*
5134 _S_construct_aux_2(size_type __req, _CharT __c, const _Alloc& __a)
5135 { return _S_construct(__req, __c, __a); }
5136
5137 template<class _InIterator>
5138 static _CharT*
5139 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a)
5140 {
5141 typedef typename std::__is_integer<_InIterator>::__type _Integral;
5142 return _S_construct_aux(__beg, __end, __a, _Integral());
5143 }
5144
5145 // For Input Iterators, used in istreambuf_iterators, etc.
5146 template<class _InIterator>
5147 static _CharT*
5148 _S_construct(_InIterator __beg, _InIterator __end, const _Alloc& __a,
5149 input_iterator_tag);
5150
5151 // For forward_iterators up to random_access_iterators, used for
5152 // string::iterator, _CharT*, etc.
5153 template<class _FwdIterator>
5154 static _CharT*
5155 _S_construct(_FwdIterator __beg, _FwdIterator __end, const _Alloc& __a,
5156 forward_iterator_tag);
5157
5158 static _CharT*
5159 _S_construct(size_type __req, _CharT __c, const _Alloc& __a);
5160
5161 public:
5162
5163 /**
5164 * @brief Copy substring into C string.
5165 * @param __s C string to copy value into.
5166 * @param __n Number of characters to copy.
5167 * @param __pos Index of first character to copy.
5168 * @return Number of characters actually copied
5169 * @throw std::out_of_range If __pos > size().
5170 *
5171 * Copies up to @a __n characters starting at @a __pos into the
5172 * C string @a __s. If @a __pos is %greater than size(),
5173 * out_of_range is thrown.
5174 */
5175 size_type
5176 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
5177
5178 /**
5179 * @brief Swap contents with another string.
5180 * @param __s String to swap with.
5181 *
5182 * Exchanges the contents of this string with that of @a __s in constant
5183 * time.
5184 */
5185 void
5186 swap(basic_string& __s)
5187 _GLIBCXX_NOEXCEPT_IF(allocator_traits<_Alloc>::is_always_equal::value);
5188
5189 // String operations:
5190 /**
5191 * @brief Return const pointer to null-terminated contents.
5192 *
5193 * This is a handle to internal data. Do not modify or dire things may
5194 * happen.
5195 */
5196 const _CharT*
5197 c_str() const _GLIBCXX_NOEXCEPT
5198 { return _M_data(); }
5199
5200 /**
5201 * @brief Return const pointer to contents.
5202 *
5203 * This is a pointer to internal data. It is undefined to modify
5204 * the contents through the returned pointer. To get a pointer that
5205 * allows modifying the contents use @c &str[0] instead,
5206 * (or in C++17 the non-const @c str.data() overload).
5207 */
5208 const _CharT*
5209 data() const _GLIBCXX_NOEXCEPT
5210 { return _M_data(); }
5211
5212#if __cplusplus >= 201703L
5213 /**
5214 * @brief Return non-const pointer to contents.
5215 *
5216 * This is a pointer to the character sequence held by the string.
5217 * Modifying the characters in the sequence is allowed.
5218 */
5219 _CharT*
5220 data() noexcept
5221 {
5222 _M_leak();
5223 return _M_data();
5224 }
5225#endif
5226
5227 /**
5228 * @brief Return copy of allocator used to construct this string.
5229 */
5230 allocator_type
5231 get_allocator() const _GLIBCXX_NOEXCEPT
5232 { return _M_dataplus; }
5233
5234 /**
5235 * @brief Find position of a C substring.
5236 * @param __s C string to locate.
5237 * @param __pos Index of character to search from.
5238 * @param __n Number of characters from @a s to search for.
5239 * @return Index of start of first occurrence.
5240 *
5241 * Starting from @a __pos, searches forward for the first @a
5242 * __n characters in @a __s within this string. If found,
5243 * returns the index where it begins. If not found, returns
5244 * npos.
5245 */
5246 size_type
5247 find(const _CharT* __s, size_type __pos, size_type __n) const
5248 _GLIBCXX_NOEXCEPT;
5249
5250 /**
5251 * @brief Find position of a string.
5252 * @param __str String to locate.
5253 * @param __pos Index of character to search from (default 0).
5254 * @return Index of start of first occurrence.
5255 *
5256 * Starting from @a __pos, searches forward for value of @a __str within
5257 * this string. If found, returns the index where it begins. If not
5258 * found, returns npos.
5259 */
5260 size_type
5261 find(const basic_string& __str, size_type __pos = 0) const
5262 _GLIBCXX_NOEXCEPT
5263 { return this->find(__str.data(), __pos, __str.size()); }
5264
5265 /**
5266 * @brief Find position of a C string.
5267 * @param __s C string to locate.
5268 * @param __pos Index of character to search from (default 0).
5269 * @return Index of start of first occurrence.
5270 *
5271 * Starting from @a __pos, searches forward for the value of @a
5272 * __s within this string. If found, returns the index where
5273 * it begins. If not found, returns npos.
5274 */
5275 size_type
5276 find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
5277 {
5278 __glibcxx_requires_string(__s);
5279 return this->find(__s, __pos, traits_type::length(__s));
5280 }
5281
5282 /**
5283 * @brief Find position of a character.
5284 * @param __c Character to locate.
5285 * @param __pos Index of character to search from (default 0).
5286 * @return Index of first occurrence.
5287 *
5288 * Starting from @a __pos, searches forward for @a __c within
5289 * this string. If found, returns the index where it was
5290 * found. If not found, returns npos.
5291 */
5292 size_type
5293 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
5294
5295#if __cplusplus >= 201703L
5296 /**
5297 * @brief Find position of a string_view.
5298 * @param __svt The object convertible to string_view to locate.
5299 * @param __pos Index of character to search from (default 0).
5300 * @return Index of start of first occurrence.
5301 */
5302 template<typename _Tp>
5303 _If_sv<_Tp, size_type>
5304 find(const _Tp& __svt, size_type __pos = 0) const
5305 noexcept(is_same<_Tp, __sv_type>::value)
5306 {
5307 __sv_type __sv = __svt;
5308 return this->find(__sv.data(), __pos, __sv.size());
5309 }
5310#endif // C++17
5311
5312 /**
5313 * @brief Find last position of a string.
5314 * @param __str String to locate.
5315 * @param __pos Index of character to search back from (default end).
5316 * @return Index of start of last occurrence.
5317 *
5318 * Starting from @a __pos, searches backward for value of @a
5319 * __str within this string. If found, returns the index where
5320 * it begins. If not found, returns npos.
5321 */
5322 size_type
5323 rfind(const basic_string& __str, size_type __pos = npos) const
5324 _GLIBCXX_NOEXCEPT
5325 { return this->rfind(__str.data(), __pos, __str.size()); }
5326
5327 /**
5328 * @brief Find last position of a C substring.
5329 * @param __s C string to locate.
5330 * @param __pos Index of character to search back from.
5331 * @param __n Number of characters from s to search for.
5332 * @return Index of start of last occurrence.
5333 *
5334 * Starting from @a __pos, searches backward for the first @a
5335 * __n characters in @a __s within this string. If found,
5336 * returns the index where it begins. If not found, returns
5337 * npos.
5338 */
5339 size_type
5340 rfind(const _CharT* __s, size_type __pos, size_type __n) const
5341 _GLIBCXX_NOEXCEPT;
5342
5343 /**
5344 * @brief Find last position of a C string.
5345 * @param __s C string to locate.
5346 * @param __pos Index of character to start search at (default end).
5347 * @return Index of start of last occurrence.
5348 *
5349 * Starting from @a __pos, searches backward for the value of
5350 * @a __s within this string. If found, returns the index
5351 * where it begins. If not found, returns npos.
5352 */
5353 size_type
5354 rfind(const _CharT* __s, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
5355 {
5356 __glibcxx_requires_string(__s);
5357 return this->rfind(__s, __pos, traits_type::length(__s));
5358 }
5359
5360 /**
5361 * @brief Find last position of a character.
5362 * @param __c Character to locate.
5363 * @param __pos Index of character to search back from (default end).
5364 * @return Index of last occurrence.
5365 *
5366 * Starting from @a __pos, searches backward for @a __c within
5367 * this string. If found, returns the index where it was
5368 * found. If not found, returns npos.
5369 */
5370 size_type
5371 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
5372
5373#if __cplusplus >= 201703L
5374 /**
5375 * @brief Find last position of a string_view.
5376 * @param __svt The object convertible to string_view to locate.
5377 * @param __pos Index of character to search back from (default end).
5378 * @return Index of start of last occurrence.
5379 */
5380 template<typename _Tp>
5381 _If_sv<_Tp, size_type>
5382 rfind(const _Tp& __svt, size_type __pos = npos) const
5384 {
5385 __sv_type __sv = __svt;
5386 return this->rfind(__sv.data(), __pos, __sv.size());
5387 }
5388#endif // C++17
5389
5390 /**
5391 * @brief Find position of a character of string.
5392 * @param __str String containing characters to locate.
5393 * @param __pos Index of character to search from (default 0).
5394 * @return Index of first occurrence.
5395 *
5396 * Starting from @a __pos, searches forward for one of the
5397 * characters of @a __str within this string. If found,
5398 * returns the index where it was found. If not found, returns
5399 * npos.
5400 */
5401 size_type
5402 find_first_of(const basic_string& __str, size_type __pos = 0) const
5403 _GLIBCXX_NOEXCEPT
5404 { return this->find_first_of(__str.data(), __pos, __str.size()); }
5405
5406 /**
5407 * @brief Find position of a character of C substring.
5408 * @param __s String containing characters to locate.
5409 * @param __pos Index of character to search from.
5410 * @param __n Number of characters from s to search for.
5411 * @return Index of first occurrence.
5412 *
5413 * Starting from @a __pos, searches forward for one of the
5414 * first @a __n characters of @a __s within this string. If
5415 * found, returns the index where it was found. If not found,
5416 * returns npos.
5417 */
5418 size_type
5419 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
5420 _GLIBCXX_NOEXCEPT;
5421
5422 /**
5423 * @brief Find position of a character of C string.
5424 * @param __s String containing characters to locate.
5425 * @param __pos Index of character to search from (default 0).
5426 * @return Index of first occurrence.
5427 *
5428 * Starting from @a __pos, searches forward for one of the
5429 * characters of @a __s within this string. If found, returns
5430 * the index where it was found. If not found, returns npos.
5431 */
5432 size_type
5433 find_first_of(const _CharT* __s, size_type __pos = 0) const
5434 _GLIBCXX_NOEXCEPT
5435 {
5436 __glibcxx_requires_string(__s);
5437 return this->find_first_of(__s, __pos, traits_type::length(__s));
5438 }
5439
5440 /**
5441 * @brief Find position of a character.
5442 * @param __c Character to locate.
5443 * @param __pos Index of character to search from (default 0).
5444 * @return Index of first occurrence.
5445 *
5446 * Starting from @a __pos, searches forward for the character
5447 * @a __c within this string. If found, returns the index
5448 * where it was found. If not found, returns npos.
5449 *
5450 * Note: equivalent to find(__c, __pos).
5451 */
5452 size_type
5453 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
5454 { return this->find(__c, __pos); }
5455
5456#if __cplusplus >= 201703L
5457 /**
5458 * @brief Find position of a character of a string_view.
5459 * @param __svt An object convertible to string_view containing
5460 * characters to locate.
5461 * @param __pos Index of character to search from (default 0).
5462 * @return Index of first occurrence.
5463 */
5464 template<typename _Tp>
5465 _If_sv<_Tp, size_type>
5466 find_first_of(const _Tp& __svt, size_type __pos = 0) const
5467 noexcept(is_same<_Tp, __sv_type>::value)
5468 {
5469 __sv_type __sv = __svt;
5470 return this->find_first_of(__sv.data(), __pos, __sv.size());
5471 }
5472#endif // C++17
5473
5474 /**
5475 * @brief Find last position of a character of string.
5476 * @param __str String containing characters to locate.
5477 * @param __pos Index of character to search back from (default end).
5478 * @return Index of last occurrence.
5479 *
5480 * Starting from @a __pos, searches backward for one of the
5481 * characters of @a __str within this string. If found,
5482 * returns the index where it was found. If not found, returns
5483 * npos.
5484 */
5485 size_type
5486 find_last_of(const basic_string& __str, size_type __pos = npos) const
5487 _GLIBCXX_NOEXCEPT
5488 { return this->find_last_of(__str.data(), __pos, __str.size()); }
5489
5490 /**
5491 * @brief Find last position of a character of C substring.
5492 * @param __s C string containing characters to locate.
5493 * @param __pos Index of character to search back from.
5494 * @param __n Number of characters from s to search for.
5495 * @return Index of last occurrence.
5496 *
5497 * Starting from @a __pos, searches backward for one of the
5498 * first @a __n characters of @a __s within this string. If
5499 * found, returns the index where it was found. If not found,
5500 * returns npos.
5501 */
5502 size_type
5503 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
5504 _GLIBCXX_NOEXCEPT;
5505
5506 /**
5507 * @brief Find last position of a character of C string.
5508 * @param __s C string containing characters to locate.
5509 * @param __pos Index of character to search back from (default end).
5510 * @return Index of last occurrence.
5511 *
5512 * Starting from @a __pos, searches backward for one of the
5513 * characters of @a __s within this string. If found, returns
5514 * the index where it was found. If not found, returns npos.
5515 */
5516 size_type
5517 find_last_of(const _CharT* __s, size_type __pos = npos) const
5518 _GLIBCXX_NOEXCEPT
5519 {
5520 __glibcxx_requires_string(__s);
5521 return this->find_last_of(__s, __pos, traits_type::length(__s));
5522 }
5523
5524 /**
5525 * @brief Find last position of a character.
5526 * @param __c Character to locate.
5527 * @param __pos Index of character to search back from (default end).
5528 * @return Index of last occurrence.
5529 *
5530 * Starting from @a __pos, searches backward for @a __c within
5531 * this string. If found, returns the index where it was
5532 * found. If not found, returns npos.
5533 *
5534 * Note: equivalent to rfind(__c, __pos).
5535 */
5536 size_type
5537 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
5538 { return this->rfind(__c, __pos); }
5539
5540#if __cplusplus >= 201703L
5541 /**
5542 * @brief Find last position of a character of string.
5543 * @param __svt An object convertible to string_view containing
5544 * characters to locate.
5545 * @param __pos Index of character to search back from (default end).
5546 * @return Index of last occurrence.
5547 */
5548 template<typename _Tp>
5549 _If_sv<_Tp, size_type>
5550 find_last_of(const _Tp& __svt, size_type __pos = npos) const
5552 {
5553 __sv_type __sv = __svt;
5554 return this->find_last_of(__sv.data(), __pos, __sv.size());
5555 }
5556#endif // C++17
5557
5558 /**
5559 * @brief Find position of a character not in string.
5560 * @param __str String containing characters to avoid.
5561 * @param __pos Index of character to search from (default 0).
5562 * @return Index of first occurrence.
5563 *
5564 * Starting from @a __pos, searches forward for a character not contained
5565 * in @a __str within this string. If found, returns the index where it
5566 * was found. If not found, returns npos.
5567 */
5568 size_type
5569 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
5570 _GLIBCXX_NOEXCEPT
5571 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
5572
5573 /**
5574 * @brief Find position of a character not in C substring.
5575 * @param __s C string containing characters to avoid.
5576 * @param __pos Index of character to search from.
5577 * @param __n Number of characters from __s to consider.
5578 * @return Index of first occurrence.
5579 *
5580 * Starting from @a __pos, searches forward for a character not
5581 * contained in the first @a __n characters of @a __s within
5582 * this string. If found, returns the index where it was
5583 * found. If not found, returns npos.
5584 */
5585 size_type
5586 find_first_not_of(const _CharT* __s, size_type __pos,
5587 size_type __n) const _GLIBCXX_NOEXCEPT;
5588
5589 /**
5590 * @brief Find position of a character not in C string.
5591 * @param __s C string containing characters to avoid.
5592 * @param __pos Index of character to search from (default 0).
5593 * @return Index of first occurrence.
5594 *
5595 * Starting from @a __pos, searches forward for a character not
5596 * contained in @a __s within this string. If found, returns
5597 * the index where it was found. If not found, returns npos.
5598 */
5599 size_type
5600 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
5601 _GLIBCXX_NOEXCEPT
5602 {
5603 __glibcxx_requires_string(__s);
5604 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
5605 }
5606
5607 /**
5608 * @brief Find position of a different character.
5609 * @param __c Character to avoid.
5610 * @param __pos Index of character to search from (default 0).
5611 * @return Index of first occurrence.
5612 *
5613 * Starting from @a __pos, searches forward for a character
5614 * other than @a __c within this string. If found, returns the
5615 * index where it was found. If not found, returns npos.
5616 */
5617 size_type
5618 find_first_not_of(_CharT __c, size_type __pos = 0) const
5619 _GLIBCXX_NOEXCEPT;
5620
5621#if __cplusplus >= 201703L
5622 /**
5623 * @brief Find position of a character not in a string_view.
5624 * @param __svt An object convertible to string_view containing
5625 * characters to avoid.
5626 * @param __pos Index of character to search from (default 0).
5627 * @return Index of first occurrence.
5628 */
5629 template<typename _Tp>
5630 _If_sv<_Tp, size_type>
5631 find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
5632 noexcept(is_same<_Tp, __sv_type>::value)
5633 {
5634 __sv_type __sv = __svt;
5635 return this->find_first_not_of(__sv.data(), __pos, __sv.size());
5636 }
5637#endif // C++17
5638
5639 /**
5640 * @brief Find last position of a character not in string.
5641 * @param __str String containing characters to avoid.
5642 * @param __pos Index of character to search back from (default end).
5643 * @return Index of last occurrence.
5644 *
5645 * Starting from @a __pos, searches backward for a character
5646 * not contained in @a __str within this string. If found,
5647 * returns the index where it was found. If not found, returns
5648 * npos.
5649 */
5650 size_type
5651 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
5652 _GLIBCXX_NOEXCEPT
5653 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
5654
5655 /**
5656 * @brief Find last position of a character not in C substring.
5657 * @param __s C string containing characters to avoid.
5658 * @param __pos Index of character to search back from.
5659 * @param __n Number of characters from s to consider.
5660 * @return Index of last occurrence.
5661 *
5662 * Starting from @a __pos, searches backward for a character not
5663 * contained in the first @a __n characters of @a __s within this string.
5664 * If found, returns the index where it was found. If not found,
5665 * returns npos.
5666 */
5667 size_type
5668 find_last_not_of(const _CharT* __s, size_type __pos,
5669 size_type __n) const _GLIBCXX_NOEXCEPT;
5670 /**
5671 * @brief Find last position of a character not in C string.
5672 * @param __s C string containing characters to avoid.
5673 * @param __pos Index of character to search back from (default end).
5674 * @return Index of last occurrence.
5675 *
5676 * Starting from @a __pos, searches backward for a character
5677 * not contained in @a __s within this string. If found,
5678 * returns the index where it was found. If not found, returns
5679 * npos.
5680 */
5681 size_type
5682 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
5683 _GLIBCXX_NOEXCEPT
5684 {
5685 __glibcxx_requires_string(__s);
5686 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
5687 }
5688
5689 /**
5690 * @brief Find last position of a different character.
5691 * @param __c Character to avoid.
5692 * @param __pos Index of character to search back from (default end).
5693 * @return Index of last occurrence.
5694 *
5695 * Starting from @a __pos, searches backward for a character other than
5696 * @a __c within this string. If found, returns the index where it was
5697 * found. If not found, returns npos.
5698 */
5699 size_type
5700 find_last_not_of(_CharT __c, size_type __pos = npos) const
5701 _GLIBCXX_NOEXCEPT;
5702
5703#if __cplusplus >= 201703L
5704 /**
5705 * @brief Find last position of a character not in a string_view.
5706 * @param __svt An object convertible to string_view containing
5707 * characters to avoid.
5708 * @param __pos Index of character to search back from (default end).
5709 * @return Index of last occurrence.
5710 */
5711 template<typename _Tp>
5712 _If_sv<_Tp, size_type>
5713 find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
5715 {
5716 __sv_type __sv = __svt;
5717 return this->find_last_not_of(__sv.data(), __pos, __sv.size());
5718 }
5719#endif // C++17
5720
5721 /**
5722 * @brief Get a substring.
5723 * @param __pos Index of first character (default 0).
5724 * @param __n Number of characters in substring (default remainder).
5725 * @return The new string.
5726 * @throw std::out_of_range If __pos > size().
5727 *
5728 * Construct and return a new string using the @a __n
5729 * characters starting at @a __pos. If the string is too
5730 * short, use the remainder of the characters. If @a __pos is
5731 * beyond the end of the string, out_of_range is thrown.
5732 */
5734 substr(size_type __pos = 0, size_type __n = npos) const
5735 { return basic_string(*this,
5736 _M_check(__pos, "basic_string::substr"), __n); }
5737
5738 /**
5739 * @brief Compare to a string.
5740 * @param __str String to compare against.
5741 * @return Integer < 0, 0, or > 0.
5742 *
5743 * Returns an integer < 0 if this string is ordered before @a
5744 * __str, 0 if their values are equivalent, or > 0 if this
5745 * string is ordered after @a __str. Determines the effective
5746 * length rlen of the strings to compare as the smallest of
5747 * size() and str.size(). The function then compares the two
5748 * strings by calling traits::compare(data(), str.data(),rlen).
5749 * If the result of the comparison is nonzero returns it,
5750 * otherwise the shorter one is ordered first.
5751 */
5752 int
5753 compare(const basic_string& __str) const
5754 {
5755 const size_type __size = this->size();
5756 const size_type __osize = __str.size();
5757 const size_type __len = std::min(__size, __osize);
5758
5759 int __r = traits_type::compare(_M_data(), __str.data(), __len);
5760 if (!__r)
5761 __r = _S_compare(__size, __osize);
5762 return __r;
5763 }
5764
5765#if __cplusplus >= 201703L
5766 /**
5767 * @brief Compare to a string_view.
5768 * @param __svt An object convertible to string_view to compare against.
5769 * @return Integer < 0, 0, or > 0.
5770 */
5771 template<typename _Tp>
5772 _If_sv<_Tp, int>
5773 compare(const _Tp& __svt) const
5775 {
5776 __sv_type __sv = __svt;
5777 const size_type __size = this->size();
5778 const size_type __osize = __sv.size();
5779 const size_type __len = std::min(__size, __osize);
5780
5781 int __r = traits_type::compare(_M_data(), __sv.data(), __len);
5782 if (!__r)
5783 __r = _S_compare(__size, __osize);
5784 return __r;
5785 }
5786
5787 /**
5788 * @brief Compare to a string_view.
5789 * @param __pos A position in the string to start comparing from.
5790 * @param __n The number of characters to compare.
5791 * @param __svt An object convertible to string_view to compare
5792 * against.
5793 * @return Integer < 0, 0, or > 0.
5794 */
5795 template<typename _Tp>
5796 _If_sv<_Tp, int>
5797 compare(size_type __pos, size_type __n, const _Tp& __svt) const
5798 noexcept(is_same<_Tp, __sv_type>::value)
5799 {
5800 __sv_type __sv = __svt;
5801 return __sv_type(*this).substr(__pos, __n).compare(__sv);
5802 }
5803
5804 /**
5805 * @brief Compare to a string_view.
5806 * @param __pos1 A position in the string to start comparing from.
5807 * @param __n1 The number of characters to compare.
5808 * @param __svt An object convertible to string_view to compare
5809 * against.
5810 * @param __pos2 A position in the string_view to start comparing from.
5811 * @param __n2 The number of characters to compare.
5812 * @return Integer < 0, 0, or > 0.
5813 */
5814 template<typename _Tp>
5815 _If_sv<_Tp, int>
5816 compare(size_type __pos1, size_type __n1, const _Tp& __svt,
5817 size_type __pos2, size_type __n2 = npos) const
5818 noexcept(is_same<_Tp, __sv_type>::value)
5819 {
5820 __sv_type __sv = __svt;
5821 return __sv_type(*this)
5822 .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
5823 }
5824#endif // C++17
5825
5826 /**
5827 * @brief Compare substring to a string.
5828 * @param __pos Index of first character of substring.
5829 * @param __n Number of characters in substring.
5830 * @param __str String to compare against.
5831 * @return Integer < 0, 0, or > 0.
5832 *
5833 * Form the substring of this string from the @a __n characters
5834 * starting at @a __pos. Returns an integer < 0 if the
5835 * substring is ordered before @a __str, 0 if their values are
5836 * equivalent, or > 0 if the substring is ordered after @a
5837 * __str. Determines the effective length rlen of the strings
5838 * to compare as the smallest of the length of the substring
5839 * and @a __str.size(). The function then compares the two
5840 * strings by calling
5841 * traits::compare(substring.data(),str.data(),rlen). If the
5842 * result of the comparison is nonzero returns it, otherwise
5843 * the shorter one is ordered first.
5844 */
5845 int
5846 compare(size_type __pos, size_type __n, const basic_string& __str) const;
5847
5848 /**
5849 * @brief Compare substring to a substring.
5850 * @param __pos1 Index of first character of substring.
5851 * @param __n1 Number of characters in substring.
5852 * @param __str String to compare against.
5853 * @param __pos2 Index of first character of substring of str.
5854 * @param __n2 Number of characters in substring of str.
5855 * @return Integer < 0, 0, or > 0.
5856 *
5857 * Form the substring of this string from the @a __n1
5858 * characters starting at @a __pos1. Form the substring of @a
5859 * __str from the @a __n2 characters starting at @a __pos2.
5860 * Returns an integer < 0 if this substring is ordered before
5861 * the substring of @a __str, 0 if their values are equivalent,
5862 * or > 0 if this substring is ordered after the substring of
5863 * @a __str. Determines the effective length rlen of the
5864 * strings to compare as the smallest of the lengths of the
5865 * substrings. The function then compares the two strings by
5866 * calling
5867 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
5868 * If the result of the comparison is nonzero returns it,
5869 * otherwise the shorter one is ordered first.
5870 */
5871 int
5872 compare(size_type __pos1, size_type __n1, const basic_string& __str,
5873 size_type __pos2, size_type __n2 = npos) const;
5874
5875 /**
5876 * @brief Compare to a C string.
5877 * @param __s C string to compare against.
5878 * @return Integer < 0, 0, or > 0.
5879 *
5880 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
5881 * their values are equivalent, or > 0 if this string is ordered after
5882 * @a __s. Determines the effective length rlen of the strings to
5883 * compare as the smallest of size() and the length of a string
5884 * constructed from @a __s. The function then compares the two strings
5885 * by calling traits::compare(data(),s,rlen). If the result of the
5886 * comparison is nonzero returns it, otherwise the shorter one is
5887 * ordered first.
5888 */
5889 int
5890 compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT;
5891
5892 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5893 // 5 String::compare specification questionable
5894 /**
5895 * @brief Compare substring to a C string.
5896 * @param __pos Index of first character of substring.
5897 * @param __n1 Number of characters in substring.
5898 * @param __s C string to compare against.
5899 * @return Integer < 0, 0, or > 0.
5900 *
5901 * Form the substring of this string from the @a __n1
5902 * characters starting at @a pos. Returns an integer < 0 if
5903 * the substring is ordered before @a __s, 0 if their values
5904 * are equivalent, or > 0 if the substring is ordered after @a
5905 * __s. Determines the effective length rlen of the strings to
5906 * compare as the smallest of the length of the substring and
5907 * the length of a string constructed from @a __s. The
5908 * function then compares the two string by calling
5909 * traits::compare(substring.data(),__s,rlen). If the result of
5910 * the comparison is nonzero returns it, otherwise the shorter
5911 * one is ordered first.
5912 */
5913 int
5914 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
5915
5916 /**
5917 * @brief Compare substring against a character %array.
5918 * @param __pos Index of first character of substring.
5919 * @param __n1 Number of characters in substring.
5920 * @param __s character %array to compare against.
5921 * @param __n2 Number of characters of s.
5922 * @return Integer < 0, 0, or > 0.
5923 *
5924 * Form the substring of this string from the @a __n1
5925 * characters starting at @a __pos. Form a string from the
5926 * first @a __n2 characters of @a __s. Returns an integer < 0
5927 * if this substring is ordered before the string from @a __s,
5928 * 0 if their values are equivalent, or > 0 if this substring
5929 * is ordered after the string from @a __s. Determines the
5930 * effective length rlen of the strings to compare as the
5931 * smallest of the length of the substring and @a __n2. The
5932 * function then compares the two strings by calling
5933 * traits::compare(substring.data(),s,rlen). If the result of
5934 * the comparison is nonzero returns it, otherwise the shorter
5935 * one is ordered first.
5936 *
5937 * NB: s must have at least n2 characters, &apos;\\0&apos; has
5938 * no special meaning.
5939 */
5940 int
5941 compare(size_type __pos, size_type __n1, const _CharT* __s,
5942 size_type __n2) const;
5943
5944#if __cplusplus > 201703L
5945 bool
5946 starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
5947 { return __sv_type(this->data(), this->size()).starts_with(__x); }
5948
5949 bool
5950 starts_with(_CharT __x) const noexcept
5951 { return __sv_type(this->data(), this->size()).starts_with(__x); }
5952
5953 bool
5954 starts_with(const _CharT* __x) const noexcept
5955 { return __sv_type(this->data(), this->size()).starts_with(__x); }
5956
5957 bool
5958 ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept
5959 { return __sv_type(this->data(), this->size()).ends_with(__x); }
5960
5961 bool
5962 ends_with(_CharT __x) const noexcept
5963 { return __sv_type(this->data(), this->size()).ends_with(__x); }
5964
5965 bool
5966 ends_with(const _CharT* __x) const noexcept
5967 { return __sv_type(this->data(), this->size()).ends_with(__x); }
5968#endif // C++20
5969
5970# ifdef _GLIBCXX_TM_TS_INTERNAL
5971 friend void
5972 ::_txnal_cow_string_C1_for_exceptions(void* that, const char* s,
5973 void* exc);
5974 friend const char*
5975 ::_txnal_cow_string_c_str(const void *that);
5976 friend void
5977 ::_txnal_cow_string_D1(void *that);
5978 friend void
5979 ::_txnal_cow_string_D1_commit(void *that);
5980# endif
5981 };
5982#endif // !_GLIBCXX_USE_CXX11_ABI
5983
5984#if __cpp_deduction_guides >= 201606
5985_GLIBCXX_BEGIN_NAMESPACE_CXX11
5986 template<typename _InputIterator, typename _CharT
5987 = typename iterator_traits<_InputIterator>::value_type,
5988 typename _Allocator = allocator<_CharT>,
5989 typename = _RequireInputIter<_InputIterator>,
5990 typename = _RequireAllocator<_Allocator>>
5991 basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
5992 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
5993
5994 // _GLIBCXX_RESOLVE_LIB_DEFECTS
5995 // 3075. basic_string needs deduction guides from basic_string_view
5996 template<typename _CharT, typename _Traits,
5997 typename _Allocator = allocator<_CharT>,
5998 typename = _RequireAllocator<_Allocator>>
5999 basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
6000 -> basic_string<_CharT, _Traits, _Allocator>;
6001
6002 template<typename _CharT, typename _Traits,
6003 typename _Allocator = allocator<_CharT>,
6004 typename = _RequireAllocator<_Allocator>>
6005 basic_string(basic_string_view<_CharT, _Traits>,
6006 typename basic_string<_CharT, _Traits, _Allocator>::size_type,
6007 typename basic_string<_CharT, _Traits, _Allocator>::size_type,
6008 const _Allocator& = _Allocator())
6009 -> basic_string<_CharT, _Traits, _Allocator>;
6010_GLIBCXX_END_NAMESPACE_CXX11
6011#endif
6012
6013 // operator+
6014 /**
6015 * @brief Concatenate two strings.
6016 * @param __lhs First string.
6017 * @param __rhs Last string.
6018 * @return New string with value of @a __lhs followed by @a __rhs.
6019 */
6020 template<typename _CharT, typename _Traits, typename _Alloc>
6021 basic_string<_CharT, _Traits, _Alloc>
6024 {
6026 __str.append(__rhs);
6027 return __str;
6028 }
6029
6030 /**
6031 * @brief Concatenate C string and string.
6032 * @param __lhs First string.
6033 * @param __rhs Last string.
6034 * @return New string with value of @a __lhs followed by @a __rhs.
6035 */
6036 template<typename _CharT, typename _Traits, typename _Alloc>
6037 basic_string<_CharT,_Traits,_Alloc>
6038 operator+(const _CharT* __lhs,
6039 const basic_string<_CharT,_Traits,_Alloc>& __rhs);
6040
6041 /**
6042 * @brief Concatenate character and string.
6043 * @param __lhs First string.
6044 * @param __rhs Last string.
6045 * @return New string with @a __lhs followed by @a __rhs.
6046 */
6047 template<typename _CharT, typename _Traits, typename _Alloc>
6048 basic_string<_CharT,_Traits,_Alloc>
6049 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
6050
6051 /**
6052 * @brief Concatenate string and C string.
6053 * @param __lhs First string.
6054 * @param __rhs Last string.
6055 * @return New string with @a __lhs followed by @a __rhs.
6056 */
6057 template<typename _CharT, typename _Traits, typename _Alloc>
6058 inline basic_string<_CharT, _Traits, _Alloc>
6060 const _CharT* __rhs)
6061 {
6063 __str.append(__rhs);
6064 return __str;
6065 }
6066
6067 /**
6068 * @brief Concatenate string and character.
6069 * @param __lhs First string.
6070 * @param __rhs Last string.
6071 * @return New string with @a __lhs followed by @a __rhs.
6072 */
6073 template<typename _CharT, typename _Traits, typename _Alloc>
6074 inline basic_string<_CharT, _Traits, _Alloc>
6076 {
6077 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
6078 typedef typename __string_type::size_type __size_type;
6079 __string_type __str(__lhs);
6080 __str.append(__size_type(1), __rhs);
6081 return __str;
6082 }
6083
6084#if __cplusplus >= 201103L
6085 template<typename _CharT, typename _Traits, typename _Alloc>
6086 inline basic_string<_CharT, _Traits, _Alloc>
6087 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
6088 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
6089 { return std::move(__lhs.append(__rhs)); }
6090
6091 template<typename _CharT, typename _Traits, typename _Alloc>
6092 inline basic_string<_CharT, _Traits, _Alloc>
6093 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6094 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
6095 { return std::move(__rhs.insert(0, __lhs)); }
6096
6097 template<typename _CharT, typename _Traits, typename _Alloc>
6098 inline basic_string<_CharT, _Traits, _Alloc>
6099 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
6100 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
6101 {
6102#if _GLIBCXX_USE_CXX11_ABI
6103 using _Alloc_traits = allocator_traits<_Alloc>;
6104 bool __use_rhs = false;
6105 if _GLIBCXX17_CONSTEXPR (typename _Alloc_traits::is_always_equal{})
6106 __use_rhs = true;
6107 else if (__lhs.get_allocator() == __rhs.get_allocator())
6108 __use_rhs = true;
6109 if (__use_rhs)
6110#endif
6111 {
6112 const auto __size = __lhs.size() + __rhs.size();
6113 if (__size > __lhs.capacity() && __size <= __rhs.capacity())
6114 return std::move(__rhs.insert(0, __lhs));
6115 }
6116 return std::move(__lhs.append(__rhs));
6117 }
6118
6119 template<typename _CharT, typename _Traits, typename _Alloc>
6120 inline basic_string<_CharT, _Traits, _Alloc>
6121 operator+(const _CharT* __lhs,
6122 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
6123 { return std::move(__rhs.insert(0, __lhs)); }
6124
6125 template<typename _CharT, typename _Traits, typename _Alloc>
6126 inline basic_string<_CharT, _Traits, _Alloc>
6127 operator+(_CharT __lhs,
6128 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
6129 { return std::move(__rhs.insert(0, 1, __lhs)); }
6130
6131 template<typename _CharT, typename _Traits, typename _Alloc>
6132 inline basic_string<_CharT, _Traits, _Alloc>
6133 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
6134 const _CharT* __rhs)
6135 { return std::move(__lhs.append(__rhs)); }
6136
6137 template<typename _CharT, typename _Traits, typename _Alloc>
6138 inline basic_string<_CharT, _Traits, _Alloc>
6139 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
6140 _CharT __rhs)
6141 { return std::move(__lhs.append(1, __rhs)); }
6142#endif
6143
6144 // operator ==
6145 /**
6146 * @brief Test equivalence of two strings.
6147 * @param __lhs First string.
6148 * @param __rhs Second string.
6149 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
6150 */
6151 template<typename _CharT, typename _Traits, typename _Alloc>
6152 inline bool
6155 _GLIBCXX_NOEXCEPT
6156 { return __lhs.compare(__rhs) == 0; }
6157
6158 template<typename _CharT>
6159 inline
6160 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
6161 operator==(const basic_string<_CharT>& __lhs,
6162 const basic_string<_CharT>& __rhs) _GLIBCXX_NOEXCEPT
6163 { return (__lhs.size() == __rhs.size()
6164 && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
6165 __lhs.size())); }
6166
6167 /**
6168 * @brief Test equivalence of C string and string.
6169 * @param __lhs C string.
6170 * @param __rhs String.
6171 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
6172 */
6173 template<typename _CharT, typename _Traits, typename _Alloc>
6174 inline bool
6175 operator==(const _CharT* __lhs,
6177 { return __rhs.compare(__lhs) == 0; }
6178
6179 /**
6180 * @brief Test equivalence of string and C string.
6181 * @param __lhs String.
6182 * @param __rhs C string.
6183 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
6184 */
6185 template<typename _CharT, typename _Traits, typename _Alloc>
6186 inline bool
6188 const _CharT* __rhs)
6189 { return __lhs.compare(__rhs) == 0; }
6190
6191 // operator !=
6192 /**
6193 * @brief Test difference of two strings.
6194 * @param __lhs First string.
6195 * @param __rhs Second string.
6196 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
6197 */
6198 template<typename _CharT, typename _Traits, typename _Alloc>
6199 inline bool
6202 _GLIBCXX_NOEXCEPT
6203 { return !(__lhs == __rhs); }
6204
6205 /**
6206 * @brief Test difference of C string and string.
6207 * @param __lhs C string.
6208 * @param __rhs String.
6209 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
6210 */
6211 template<typename _CharT, typename _Traits, typename _Alloc>
6212 inline bool
6213 operator!=(const _CharT* __lhs,
6215 { return !(__lhs == __rhs); }
6216
6217 /**
6218 * @brief Test difference of string and C string.
6219 * @param __lhs String.
6220 * @param __rhs C string.
6221 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
6222 */
6223 template<typename _CharT, typename _Traits, typename _Alloc>
6224 inline bool
6226 const _CharT* __rhs)
6227 { return !(__lhs == __rhs); }
6228
6229 // operator <
6230 /**
6231 * @brief Test if string precedes string.
6232 * @param __lhs First string.
6233 * @param __rhs Second string.
6234 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6235 */
6236 template<typename _CharT, typename _Traits, typename _Alloc>
6237 inline bool
6238 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6240 _GLIBCXX_NOEXCEPT
6241 { return __lhs.compare(__rhs) < 0; }
6242
6243 /**
6244 * @brief Test if string precedes C string.
6245 * @param __lhs String.
6246 * @param __rhs C string.
6247 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6248 */
6249 template<typename _CharT, typename _Traits, typename _Alloc>
6250 inline bool
6251 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6252 const _CharT* __rhs)
6253 { return __lhs.compare(__rhs) < 0; }
6254
6255 /**
6256 * @brief Test if C string precedes string.
6257 * @param __lhs C string.
6258 * @param __rhs String.
6259 * @return True if @a __lhs precedes @a __rhs. False otherwise.
6260 */
6261 template<typename _CharT, typename _Traits, typename _Alloc>
6262 inline bool
6263 operator<(const _CharT* __lhs,
6265 { return __rhs.compare(__lhs) > 0; }
6266
6267 // operator >
6268 /**
6269 * @brief Test if string follows string.
6270 * @param __lhs First string.
6271 * @param __rhs Second string.
6272 * @return True if @a __lhs follows @a __rhs. False otherwise.
6273 */
6274 template<typename _CharT, typename _Traits, typename _Alloc>
6275 inline bool
6278 _GLIBCXX_NOEXCEPT
6279 { return __lhs.compare(__rhs) > 0; }
6280
6281 /**
6282 * @brief Test if string follows C string.
6283 * @param __lhs String.
6284 * @param __rhs C string.
6285 * @return True if @a __lhs follows @a __rhs. False otherwise.
6286 */
6287 template<typename _CharT, typename _Traits, typename _Alloc>
6288 inline bool
6290 const _CharT* __rhs)
6291 { return __lhs.compare(__rhs) > 0; }
6292
6293 /**
6294 * @brief Test if C string follows string.
6295 * @param __lhs C string.
6296 * @param __rhs String.
6297 * @return True if @a __lhs follows @a __rhs. False otherwise.
6298 */
6299 template<typename _CharT, typename _Traits, typename _Alloc>
6300 inline bool
6301 operator>(const _CharT* __lhs,
6303 { return __rhs.compare(__lhs) < 0; }
6304
6305 // operator <=
6306 /**
6307 * @brief Test if string doesn't follow string.
6308 * @param __lhs First string.
6309 * @param __rhs Second string.
6310 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6311 */
6312 template<typename _CharT, typename _Traits, typename _Alloc>
6313 inline bool
6314 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6316 _GLIBCXX_NOEXCEPT
6317 { return __lhs.compare(__rhs) <= 0; }
6318
6319 /**
6320 * @brief Test if string doesn't follow C string.
6321 * @param __lhs String.
6322 * @param __rhs C string.
6323 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6324 */
6325 template<typename _CharT, typename _Traits, typename _Alloc>
6326 inline bool
6327 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
6328 const _CharT* __rhs)
6329 { return __lhs.compare(__rhs) <= 0; }
6330
6331 /**
6332 * @brief Test if C string doesn't follow string.
6333 * @param __lhs C string.
6334 * @param __rhs String.
6335 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
6336 */
6337 template<typename _CharT, typename _Traits, typename _Alloc>
6338 inline bool
6339 operator<=(const _CharT* __lhs,
6341 { return __rhs.compare(__lhs) >= 0; }
6342
6343 // operator >=
6344 /**
6345 * @brief Test if string doesn't precede string.
6346 * @param __lhs First string.
6347 * @param __rhs Second string.
6348 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6349 */
6350 template<typename _CharT, typename _Traits, typename _Alloc>
6351 inline bool
6354 _GLIBCXX_NOEXCEPT
6355 { return __lhs.compare(__rhs) >= 0; }
6356
6357 /**
6358 * @brief Test if string doesn't precede C string.
6359 * @param __lhs String.
6360 * @param __rhs C string.
6361 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6362 */
6363 template<typename _CharT, typename _Traits, typename _Alloc>
6364 inline bool
6366 const _CharT* __rhs)
6367 { return __lhs.compare(__rhs) >= 0; }
6368
6369 /**
6370 * @brief Test if C string doesn't precede string.
6371 * @param __lhs C string.
6372 * @param __rhs String.
6373 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
6374 */
6375 template<typename _CharT, typename _Traits, typename _Alloc>
6376 inline bool
6377 operator>=(const _CharT* __lhs,
6379 { return __rhs.compare(__lhs) <= 0; }
6380
6381 /**
6382 * @brief Swap contents of two strings.
6383 * @param __lhs First string.
6384 * @param __rhs Second string.
6385 *
6386 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
6387 */
6388 template<typename _CharT, typename _Traits, typename _Alloc>
6389 inline void
6392 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
6393 { __lhs.swap(__rhs); }
6394
6395
6396 /**
6397 * @brief Read stream into a string.
6398 * @param __is Input stream.
6399 * @param __str Buffer to store into.
6400 * @return Reference to the input stream.
6401 *
6402 * Stores characters from @a __is into @a __str until whitespace is
6403 * found, the end of the stream is encountered, or str.max_size()
6404 * is reached. If is.width() is non-zero, that is the limit on the
6405 * number of characters stored into @a __str. Any previous
6406 * contents of @a __str are erased.
6407 */
6408 template<typename _CharT, typename _Traits, typename _Alloc>
6409 basic_istream<_CharT, _Traits>&
6410 operator>>(basic_istream<_CharT, _Traits>& __is,
6411 basic_string<_CharT, _Traits, _Alloc>& __str);
6412
6413 template<>
6414 basic_istream<char>&
6416
6417 /**
6418 * @brief Write string to a stream.
6419 * @param __os Output stream.
6420 * @param __str String to write out.
6421 * @return Reference to the output stream.
6422 *
6423 * Output characters of @a __str into os following the same rules as for
6424 * writing a C string.
6425 */
6426 template<typename _CharT, typename _Traits, typename _Alloc>
6428 operator<<(basic_ostream<_CharT, _Traits>& __os,
6430 {
6431 // _GLIBCXX_RESOLVE_LIB_DEFECTS
6432 // 586. string inserter not a formatted function
6433 return __ostream_insert(__os, __str.data(), __str.size());
6434 }
6435
6436 /**
6437 * @brief Read a line from stream into a string.
6438 * @param __is Input stream.
6439 * @param __str Buffer to store into.
6440 * @param __delim Character marking end of line.
6441 * @return Reference to the input stream.
6442 *
6443 * Stores characters from @a __is into @a __str until @a __delim is
6444 * found, the end of the stream is encountered, or str.max_size()
6445 * is reached. Any previous contents of @a __str are erased. If
6446 * @a __delim is encountered, it is extracted but not stored into
6447 * @a __str.
6448 */
6449 template<typename _CharT, typename _Traits, typename _Alloc>
6450 basic_istream<_CharT, _Traits>&
6451 getline(basic_istream<_CharT, _Traits>& __is,
6452 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
6453
6454 /**
6455 * @brief Read a line from stream into a string.
6456 * @param __is Input stream.
6457 * @param __str Buffer to store into.
6458 * @return Reference to the input stream.
6459 *
6460 * Stores characters from is into @a __str until &apos;\n&apos; is
6461 * found, the end of the stream is encountered, or str.max_size()
6462 * is reached. Any previous contents of @a __str are erased. If
6463 * end of line is encountered, it is extracted but not stored into
6464 * @a __str.
6465 */
6466 template<typename _CharT, typename _Traits, typename _Alloc>
6467 inline basic_istream<_CharT, _Traits>&
6470 { return std::getline(__is, __str, __is.widen('\n')); }
6471
6472#if __cplusplus >= 201103L
6473 /// Read a line from an rvalue stream into a string.
6474 template<typename _CharT, typename _Traits, typename _Alloc>
6475 inline basic_istream<_CharT, _Traits>&
6477 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
6478 { return std::getline(__is, __str, __delim); }
6479
6480 /// Read a line from an rvalue stream into a string.
6481 template<typename _CharT, typename _Traits, typename _Alloc>
6482 inline basic_istream<_CharT, _Traits>&
6485 { return std::getline(__is, __str); }
6486#endif
6487
6488 template<>
6489 basic_istream<char>&
6490 getline(basic_istream<char>& __in, basic_string<char>& __str,
6491 char __delim);
6492
6493#ifdef _GLIBCXX_USE_WCHAR_T
6494 template<>
6495 basic_istream<wchar_t>&
6496 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
6497 wchar_t __delim);
6498#endif
6499
6500_GLIBCXX_END_NAMESPACE_VERSION
6501} // namespace
6502
6503#if __cplusplus >= 201103L
6504
6505#include <ext/string_conversions.h>
6506#include <bits/charconv.h>
6507
6508namespace std _GLIBCXX_VISIBILITY(default)
6509{
6510_GLIBCXX_BEGIN_NAMESPACE_VERSION
6511_GLIBCXX_BEGIN_NAMESPACE_CXX11
6512
6513#if _GLIBCXX_USE_C99_STDLIB
6514 // 21.4 Numeric Conversions [string.conversions].
6515 inline int
6516 stoi(const string& __str, size_t* __idx = 0, int __base = 10)
6517 { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
6518 __idx, __base); }
6519
6520 inline long
6521 stol(const string& __str, size_t* __idx = 0, int __base = 10)
6522 { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
6523 __idx, __base); }
6524
6525 inline unsigned long
6526 stoul(const string& __str, size_t* __idx = 0, int __base = 10)
6527 { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
6528 __idx, __base); }
6529
6530 inline long long
6531 stoll(const string& __str, size_t* __idx = 0, int __base = 10)
6532 { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
6533 __idx, __base); }
6534
6535 inline unsigned long long
6536 stoull(const string& __str, size_t* __idx = 0, int __base = 10)
6537 { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
6538 __idx, __base); }
6539
6540 // NB: strtof vs strtod.
6541 inline float
6542 stof(const string& __str, size_t* __idx = 0)
6543 { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
6544
6545 inline double
6546 stod(const string& __str, size_t* __idx = 0)
6547 { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
6548
6549 inline long double
6550 stold(const string& __str, size_t* __idx = 0)
6551 { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
6552#endif // _GLIBCXX_USE_C99_STDLIB
6553
6554 // DR 1261. Insufficent overloads for to_string / to_wstring
6555
6556 inline string
6557 to_string(int __val)
6558 {
6559 const bool __neg = __val < 0;
6560 const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val;
6561 const auto __len = __detail::__to_chars_len(__uval);
6562 string __str(__neg + __len, '-');
6563 __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
6564 return __str;
6565 }
6566
6567 inline string
6568 to_string(unsigned __val)
6569 {
6570 string __str(__detail::__to_chars_len(__val), '\0');
6571 __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
6572 return __str;
6573 }
6574
6575 inline string
6576 to_string(long __val)
6577 {
6578 const bool __neg = __val < 0;
6579 const unsigned long __uval = __neg ? (unsigned long)~__val + 1ul : __val;
6580 const auto __len = __detail::__to_chars_len(__uval);
6581 string __str(__neg + __len, '-');
6582 __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
6583 return __str;
6584 }
6585
6586 inline string
6587 to_string(unsigned long __val)
6588 {
6589 string __str(__detail::__to_chars_len(__val), '\0');
6590 __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
6591 return __str;
6592 }
6593
6594 inline string
6595 to_string(long long __val)
6596 {
6597 const bool __neg = __val < 0;
6598 const unsigned long long __uval
6599 = __neg ? (unsigned long long)~__val + 1ull : __val;
6600 const auto __len = __detail::__to_chars_len(__uval);
6601 string __str(__neg + __len, '-');
6602 __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
6603 return __str;
6604 }
6605
6606 inline string
6607 to_string(unsigned long long __val)
6608 {
6609 string __str(__detail::__to_chars_len(__val), '\0');
6610 __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
6611 return __str;
6612 }
6613
6614#if _GLIBCXX_USE_C99_STDIO
6615 // NB: (v)snprintf vs sprintf.
6616
6617 inline string
6618 to_string(float __val)
6619 {
6620 const int __n =
6621 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
6622 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
6623 "%f", __val);
6624 }
6625
6626 inline string
6627 to_string(double __val)
6628 {
6629 const int __n =
6630 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
6631 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
6632 "%f", __val);
6633 }
6634
6635 inline string
6636 to_string(long double __val)
6637 {
6638 const int __n =
6639 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
6640 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
6641 "%Lf", __val);
6642 }
6643#endif // _GLIBCXX_USE_C99_STDIO
6644
6645#if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
6646 inline int
6647 stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
6648 { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
6649 __idx, __base); }
6650
6651 inline long
6652 stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
6653 { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
6654 __idx, __base); }
6655
6656 inline unsigned long
6657 stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
6658 { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
6659 __idx, __base); }
6660
6661 inline long long
6662 stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
6663 { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
6664 __idx, __base); }
6665
6666 inline unsigned long long
6667 stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
6668 { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
6669 __idx, __base); }
6670
6671 // NB: wcstof vs wcstod.
6672 inline float
6673 stof(const wstring& __str, size_t* __idx = 0)
6674 { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
6675
6676 inline double
6677 stod(const wstring& __str, size_t* __idx = 0)
6678 { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
6679
6680 inline long double
6681 stold(const wstring& __str, size_t* __idx = 0)
6682 { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
6683
6684#ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6685 // DR 1261.
6686 inline wstring
6687 to_wstring(int __val)
6688 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
6689 L"%d", __val); }
6690
6691 inline wstring
6692 to_wstring(unsigned __val)
6693 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6694 4 * sizeof(unsigned),
6695 L"%u", __val); }
6696
6697 inline wstring
6698 to_wstring(long __val)
6699 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
6700 L"%ld", __val); }
6701
6702 inline wstring
6703 to_wstring(unsigned long __val)
6704 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6705 4 * sizeof(unsigned long),
6706 L"%lu", __val); }
6707
6708 inline wstring
6709 to_wstring(long long __val)
6710 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6711 4 * sizeof(long long),
6712 L"%lld", __val); }
6713
6714 inline wstring
6715 to_wstring(unsigned long long __val)
6716 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
6717 4 * sizeof(unsigned long long),
6718 L"%llu", __val); }
6719
6720 inline wstring
6721 to_wstring(float __val)
6722 {
6723 const int __n =
6724 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
6725 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6726 L"%f", __val);
6727 }
6728
6729 inline wstring
6730 to_wstring(double __val)
6731 {
6732 const int __n =
6733 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
6734 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6735 L"%f", __val);
6736 }
6737
6738 inline wstring
6739 to_wstring(long double __val)
6740 {
6741 const int __n =
6742 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
6743 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
6744 L"%Lf", __val);
6745 }
6746#endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
6747#endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
6748
6749_GLIBCXX_END_NAMESPACE_CXX11
6750_GLIBCXX_END_NAMESPACE_VERSION
6751} // namespace
6752
6753#endif /* C++11 */
6754
6755#if __cplusplus >= 201103L
6756
6757#include <bits/functional_hash.h>
6758
6759namespace std _GLIBCXX_VISIBILITY(default)
6760{
6761_GLIBCXX_BEGIN_NAMESPACE_VERSION
6762
6763 // DR 1182.
6764
6765#ifndef _GLIBCXX_COMPATIBILITY_CXX0X
6766 /// std::hash specialization for string.
6767 template<>
6768 struct hash<string>
6769 : public __hash_base<size_t, string>
6770 {
6771 size_t
6772 operator()(const string& __s) const noexcept
6773 { return std::_Hash_impl::hash(__s.data(), __s.length()); }
6774 };
6775
6776 template<>
6777 struct __is_fast_hash<hash<string>> : std::false_type
6778 { };
6779
6780#ifdef _GLIBCXX_USE_WCHAR_T
6781 /// std::hash specialization for wstring.
6782 template<>
6784 : public __hash_base<size_t, wstring>
6785 {
6786 size_t
6787 operator()(const wstring& __s) const noexcept
6788 { return std::_Hash_impl::hash(__s.data(),
6789 __s.length() * sizeof(wchar_t)); }
6790 };
6791
6792 template<>
6793 struct __is_fast_hash<hash<wstring>> : std::false_type
6794 { };
6795#endif
6796#endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
6797
6798#ifdef _GLIBCXX_USE_CHAR8_T
6799 /// std::hash specialization for u8string.
6800 template<>
6801 struct hash<u8string>
6802 : public __hash_base<size_t, u8string>
6803 {
6804 size_t
6805 operator()(const u8string& __s) const noexcept
6806 { return std::_Hash_impl::hash(__s.data(),
6807 __s.length() * sizeof(char8_t)); }
6808 };
6809
6810 template<>
6811 struct __is_fast_hash<hash<u8string>> : std::false_type
6812 { };
6813#endif
6814
6815 /// std::hash specialization for u16string.
6816 template<>
6818 : public __hash_base<size_t, u16string>
6819 {
6820 size_t
6821 operator()(const u16string& __s) const noexcept
6822 { return std::_Hash_impl::hash(__s.data(),
6823 __s.length() * sizeof(char16_t)); }
6824 };
6825
6826 template<>
6827 struct __is_fast_hash<hash<u16string>> : std::false_type
6828 { };
6829
6830 /// std::hash specialization for u32string.
6831 template<>
6833 : public __hash_base<size_t, u32string>
6834 {
6835 size_t
6836 operator()(const u32string& __s) const noexcept
6837 { return std::_Hash_impl::hash(__s.data(),
6838 __s.length() * sizeof(char32_t)); }
6839 };
6840
6841 template<>
6842 struct __is_fast_hash<hash<u32string>> : std::false_type
6843 { };
6844
6845#if __cplusplus >= 201402L
6846
6847#define __cpp_lib_string_udls 201304
6848
6849 inline namespace literals
6850 {
6851 inline namespace string_literals
6852 {
6853#pragma GCC diagnostic push
6854#pragma GCC diagnostic ignored "-Wliteral-suffix"
6855 _GLIBCXX_DEFAULT_ABI_TAG
6856 inline basic_string<char>
6857 operator""s(const char* __str, size_t __len)
6858 { return basic_string<char>{__str, __len}; }
6859
6860#ifdef _GLIBCXX_USE_WCHAR_T
6861 _GLIBCXX_DEFAULT_ABI_TAG
6862 inline basic_string<wchar_t>
6863 operator""s(const wchar_t* __str, size_t __len)
6864 { return basic_string<wchar_t>{__str, __len}; }
6865#endif
6866
6867#ifdef _GLIBCXX_USE_CHAR8_T
6868 _GLIBCXX_DEFAULT_ABI_TAG
6869 inline basic_string<char8_t>
6870 operator""s(const char8_t* __str, size_t __len)
6871 { return basic_string<char8_t>{__str, __len}; }
6872#endif
6873
6874 _GLIBCXX_DEFAULT_ABI_TAG
6875 inline basic_string<char16_t>
6876 operator""s(const char16_t* __str, size_t __len)
6877 { return basic_string<char16_t>{__str, __len}; }
6878
6879 _GLIBCXX_DEFAULT_ABI_TAG
6880 inline basic_string<char32_t>
6881 operator""s(const char32_t* __str, size_t __len)
6882 { return basic_string<char32_t>{__str, __len}; }
6883
6884#pragma GCC diagnostic pop
6885 } // inline namespace string_literals
6886 } // inline namespace literals
6887
6888#if __cplusplus >= 201703L
6889 namespace __detail::__variant
6890 {
6891 template<typename> struct _Never_valueless_alt; // see <variant>
6892
6893 // Provide the strong exception-safety guarantee when emplacing a
6894 // basic_string into a variant, but only if moving the string cannot throw.
6895 template<typename _Tp, typename _Traits, typename _Alloc>
6896 struct _Never_valueless_alt<std::basic_string<_Tp, _Traits, _Alloc>>
6897 : __and_<
6898 is_nothrow_move_constructible<std::basic_string<_Tp, _Traits, _Alloc>>,
6899 is_nothrow_move_assignable<std::basic_string<_Tp, _Traits, _Alloc>>
6900 >::type
6901 { };
6902 } // namespace __detail::__variant
6903#endif // C++17
6904#endif // C++14
6905
6906_GLIBCXX_END_NAMESPACE_VERSION
6907} // namespace std
6908
6909#endif // C++11
6910
6911#endif /* _BASIC_STRING_H */
constexpr complex< _Tp > operator+(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x plus y.
Definition: complex:331
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
Definition: type_traits:2541
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:101
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:230
basic_string< wchar_t > wstring
A string of wchar_t.
Definition: stringfwd.h:83
ISO C++ entities toplevel namespace is std.
basic_istream< _CharT, _Traits > & getline(basic_istream< _CharT, _Traits > &__is, basic_string< _CharT, _Traits, _Alloc > &__str, _CharT __delim)
Read a line from stream into a string.
std::basic_istream< _CharT, _Traits > & operator>>(std::basic_istream< _CharT, _Traits > &__is, bitset< _Nb > &__x)
Global I/O operators for bitsets.
Definition: bitset:1470
constexpr _Iterator __base(_Iterator __it)
initializer_list
char_type widen(char __c) const
Widens characters.
Definition: basic_ios.h:449
Template class basic_ostream.
Definition: ostream:59
Primary class template hash.
integral_constant
Definition: type_traits:58
is_same
Definition: type_traits:1388
Uniform interface to all allocator types.
Managing sequences of characters and character-like objects.
const_reverse_iterator crbegin() const noexcept
void swap(basic_string &__s) noexcept(/*conditional */)
Swap contents with another string.
basic_string & operator=(const _CharT *__s)
Copy contents of s into this string.
void push_back(_CharT __c)
Append a single character.
const_iterator cend() const noexcept
basic_string & operator+=(initializer_list< _CharT > __l)
Append an initializer_list of characters.
size_type find(const _CharT *__s, size_type __pos=0) const noexcept
Find position of a C string.
basic_string & replace(iterator __i1, iterator __i2, const _CharT *__s, size_type __n)
Replace range of characters with C substring.
basic_string & assign(initializer_list< _CharT > __l)
Set value to an initializer_list of characters.
size_type find_first_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character of string.
const _CharT * data() const noexcept
Return const pointer to contents.
size_type find_last_of(const _CharT *__s, size_type __pos=npos) const noexcept
Find last position of a character of C string.
void insert(iterator __p, initializer_list< _CharT > __l)
Insert an initializer_list of characters.
basic_string & insert(size_type __pos1, const basic_string &__str)
Insert value of a string.
size_type rfind(const _CharT *__s, size_type __pos=npos) const noexcept
Find last position of a C string.
basic_string substr(size_type __pos=0, size_type __n=npos) const
Get a substring.
iterator erase(iterator __position)
Remove one character.
size_type find(const _CharT *__s, size_type __pos, size_type __n) const noexcept
Find position of a C substring.
size_type find(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a string.
size_type find_last_not_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character not in string.
int compare(const basic_string &__str) const
Compare to a string.
reverse_iterator rend()
void reserve(size_type __res_arg=0)
Attempt to preallocate enough memory for specified number of characters.
basic_string & replace(iterator __i1, iterator __i2, const basic_string &__str)
Replace range of characters with string.
size_type find_first_not_of(const basic_string &__str, size_type __pos=0) const noexcept
Find position of a character not in string.
const_reference front() const noexcept
size_type find_last_not_of(const _CharT *__s, size_type __pos=npos) const noexcept
Find last position of a character not in C string.
void insert(iterator __p, size_type __n, _CharT __c)
Insert multiple characters.
basic_string & assign(const basic_string &__str)
Set value to contents of another string.
reverse_iterator rbegin()
basic_string & insert(size_type __pos1, const basic_string &__str, size_type __pos2, size_type __n=npos)
Insert a substring.
reference front()
basic_string & replace(iterator __i1, iterator __i2, _InputIterator __k1, _InputIterator __k2)
Replace range of characters with range.
basic_string & assign(basic_string &&__str) noexcept(allocator_traits< _Alloc >::is_always_equal::value)
Set value to contents of another string.
void pop_back()
Remove the last character.
basic_string(basic_string &&__str) noexcept
Move construct string.
size_type copy(_CharT *__s, size_type __n, size_type __pos=0) const
Copy substring into C string.
size_type length() const noexcept
Returns the number of characters in the string, not including any null-termination.
size_type find_last_of(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a character of string.
basic_string & operator+=(const basic_string &__str)
Append a string to this string.
size_type size() const noexcept
Returns the number of characters in the string, not including any null-termination.
size_type rfind(const basic_string &__str, size_type __pos=npos) const noexcept
Find last position of a string.
basic_string & operator+=(const _CharT *__s)
Append a C string.
void shrink_to_fit() noexcept
A non-binding request to reduce capacity() to size().
void resize(size_type __n, _CharT __c)
Resizes the string to the specified number of characters.
const_reference at(size_type __n) const
Provides access to the data contained in the string.
const_reference back() const noexcept
const_reverse_iterator rend() const noexcept
const_iterator end() const noexcept
size_type find_last_of(_CharT __c, size_type __pos=npos) const noexcept
Find last position of a character.
basic_string & append(const basic_string &__str)
Append a string to this string.
const_iterator begin() const noexcept
basic_string & operator=(basic_string &&__str) noexcept(/*conditional */)
Move assign the value of str to this string.
basic_string & replace(iterator __i1, iterator __i2, initializer_list< _CharT > __l)
Replace range of characters with initializer_list.
basic_string & operator+=(_CharT __c)
Append a character.
const_reverse_iterator crend() const noexcept
basic_string & operator=(const basic_string &__str)
Assign the value of str to this string.
basic_string & operator=(_CharT __c)
Set value to string of length 1.
void resize(size_type __n)
Resizes the string to the specified number of characters.
const_reverse_iterator rbegin() const noexcept
basic_string & assign(_InputIterator __first, _InputIterator __last)
Set value to a range of characters.
basic_string & append(initializer_list< _CharT > __l)
Append an initializer_list of characters.
basic_string & append(_InputIterator __first, _InputIterator __last)
Append a range of characters.
const_reference operator[](size_type __pos) const noexcept
Subscript access to the data contained in the string.
void clear() noexcept
size_type find_first_of(_CharT __c, size_type __pos=0) const noexcept
Find position of a character.
basic_string & assign(size_type __n, _CharT __c)
Set value to multiple characters.
basic_string & replace(iterator __i1, iterator __i2, const _CharT *__s)
Replace range of characters with C string.
bool empty() const noexcept
basic_string & insert(size_type __pos, const _CharT *__s)
Insert a C string.
basic_string & assign(const basic_string &__str, size_type __pos, size_type __n=npos)
Set value to a substring of a string.
const _CharT * c_str() const noexcept
Return const pointer to null-terminated contents.
reference back()
basic_string & replace(size_type __pos, size_type __n1, const _CharT *__s)
Replace characters with value of a C string.
static const size_type npos
Value returned by various member functions when they fail.
allocator_type get_allocator() const noexcept
Return copy of allocator used to construct this string.
basic_string & assign(const _CharT *__s)
Set value to contents of a C string.
basic_string & replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
Replace characters with multiple characters.
const_iterator cbegin() const noexcept
basic_string & replace(iterator __i1, iterator __i2, size_type __n, _CharT __c)
Replace range of characters with multiple characters.
basic_string & operator=(initializer_list< _CharT > __l)
Set value to string constructed from initializer list.
~basic_string() noexcept
Destroy the string instance.
size_type capacity() const noexcept
basic_string() noexcept
Default constructor creates an empty string.
void insert(iterator __p, _InputIterator __beg, _InputIterator __end)
Insert a range of characters.
size_type find_first_of(const _CharT *__s, size_type __pos=0) const noexcept
Find position of a character of C string.
size_type max_size() const noexcept
Returns the size() of the largest possible string.
reference operator[](size_type __pos)
Subscript access to the data contained in the string.
basic_string & insert(size_type __pos, size_type __n, _CharT __c)
Insert multiple characters.
basic_string & erase(size_type __pos=0, size_type __n=npos)
Remove characters.
basic_string & replace(size_type __pos1, size_type __n1, const basic_string &__str, size_type __pos2, size_type __n2=npos)
Replace characters with value from another string.
basic_string & append(const _CharT *__s)
Append a C string.
basic_string & replace(size_type __pos, size_type __n, const basic_string &__str)
Replace characters with value from another string.
size_type find_first_not_of(const _CharT *__s, size_type __pos=0) const noexcept
Find position of a character not in C string.
reference at(size_type __n)
Provides access to the data contained in the string.
iterator insert(iterator __p, _CharT __c)
Insert one character.
Basis for explicit traits specializations.
Definition: char_traits.h:298
Uniform interface to all pointer-like types.
Definition: ptr_traits.h:83
Marking input iterators.
Forward iterators support a superset of input iterator operations.
Common iterator class.
Uniform interface to C++98 and C++11 allocators.