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