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