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