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