]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/basic_string.h
libstdc++: Fix std::type_info::before for ARM [PR103240]
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / basic_string.h
CommitLineData
725dc051
BK
1// Components for manipulating sequences of characters -*- C++ -*-
2
99dee823 3// Copyright (C) 1997-2021 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
7b527614
JW
51#if ! _GLIBCXX_USE_CXX11_ABI
52# include "cow_string.h"
53#else
12ffa228
BK
54namespace std _GLIBCXX_VISIBILITY(default)
55{
56_GLIBCXX_BEGIN_NAMESPACE_VERSION
7b527614 57_GLIBCXX_BEGIN_NAMESPACE_CXX11
3cbc7af0 58
b376b1ef 59#ifdef __cpp_lib_is_constant_evaluated
b96e2ff9
ML
60// Support P0980R1 in C++20.
61# define __cpp_lib_constexpr_string 201907L
b376b1ef 62#elif __cplusplus >= 201703L && _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
3da80ed7
JW
63// Support P0426R1 changes to char_traits in C++17.
64# define __cpp_lib_constexpr_string 201611L
3da80ed7
JW
65#endif
66
34a2b755
JW
67 /**
68 * @class basic_string basic_string.h <string>
69 * @brief Managing sequences of characters and character-like objects.
70 *
71 * @ingroup strings
72 * @ingroup sequences
73 *
74 * @tparam _CharT Type of character
75 * @tparam _Traits Traits for character type, defaults to
76 * char_traits<_CharT>.
77 * @tparam _Alloc Allocator type, defaults to allocator<_CharT>.
78 *
79 * Meets the requirements of a <a href="tables.html#65">container</a>, a
80 * <a href="tables.html#66">reversible container</a>, and a
81 * <a href="tables.html#67">sequence</a>. Of the
82 * <a href="tables.html#68">optional sequence requirements</a>, only
83 * @c push_back, @c at, and @c %array access are supported.
84 */
85 template<typename _CharT, typename _Traits, typename _Alloc>
86 class basic_string
87 {
88 typedef typename __gnu_cxx::__alloc_traits<_Alloc>::template
89 rebind<_CharT>::other _Char_alloc_type;
90 typedef __gnu_cxx::__alloc_traits<_Char_alloc_type> _Alloc_traits;
91
92 // Types:
93 public:
94 typedef _Traits traits_type;
95 typedef typename _Traits::char_type value_type;
96 typedef _Char_alloc_type allocator_type;
97 typedef typename _Alloc_traits::size_type size_type;
98 typedef typename _Alloc_traits::difference_type difference_type;
99 typedef typename _Alloc_traits::reference reference;
100 typedef typename _Alloc_traits::const_reference const_reference;
101 typedef typename _Alloc_traits::pointer pointer;
102 typedef typename _Alloc_traits::const_pointer const_pointer;
103 typedef __gnu_cxx::__normal_iterator<pointer, basic_string> iterator;
104 typedef __gnu_cxx::__normal_iterator<const_pointer, basic_string>
105 const_iterator;
106 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
107 typedef std::reverse_iterator<iterator> reverse_iterator;
108
109 /// Value returned by various member functions when they fail.
110 static const size_type npos = static_cast<size_type>(-1);
111
3eb1eda1 112 protected:
34a2b755
JW
113 // type used for positions in insert, erase etc.
114#if __cplusplus < 201103L
115 typedef iterator __const_iterator;
116#else
117 typedef const_iterator __const_iterator;
118#endif
119
3eb1eda1 120 private:
1a289fa3 121#if __cplusplus >= 201703L
ca8f2cb1
VV
122 // A helper type for avoiding boiler-plate.
123 typedef basic_string_view<_CharT, _Traits> __sv_type;
92daf2de
JW
124
125 template<typename _Tp, typename _Res>
126 using _If_sv = enable_if_t<
127 __and_<is_convertible<const _Tp&, __sv_type>,
4cf5930f 128 __not_<is_convertible<const _Tp*, const basic_string*>>,
92daf2de
JW
129 __not_<is_convertible<const _Tp&, const _CharT*>>>::value,
130 _Res>;
df66af3b
DK
131
132 // Allows an implicit conversion to __sv_type.
b96e2ff9 133 _GLIBCXX20_CONSTEXPR
df66af3b
DK
134 static __sv_type
135 _S_to_string_view(__sv_type __svt) noexcept
136 { return __svt; }
137
138 // Wraps a string_view by explicit conversion and thus
139 // allows to add an internal constructor that does not
140 // participate in overload resolution when a string_view
141 // is provided.
142 struct __sv_wrapper
143 {
b96e2ff9
ML
144 _GLIBCXX20_CONSTEXPR explicit
145 __sv_wrapper(__sv_type __sv) noexcept : _M_sv(__sv) { }
146
df66af3b
DK
147 __sv_type _M_sv;
148 };
1a289fa3
JW
149
150 /**
151 * @brief Only internally used: Construct string from a string view
152 * wrapper.
153 * @param __svw string view wrapper.
154 * @param __a Allocator to use.
155 */
b96e2ff9 156 _GLIBCXX20_CONSTEXPR
1a289fa3
JW
157 explicit
158 basic_string(__sv_wrapper __svw, const _Alloc& __a)
159 : basic_string(__svw._M_sv.data(), __svw._M_sv.size(), __a) { }
ca8f2cb1
VV
160#endif
161
34a2b755
JW
162 // Use empty-base optimization: http://www.cantrip.org/emptyopt.html
163 struct _Alloc_hider : allocator_type // TODO check __is_final
164 {
8cab3d18 165#if __cplusplus < 201103L
34a2b755
JW
166 _Alloc_hider(pointer __dat, const _Alloc& __a = _Alloc())
167 : allocator_type(__a), _M_p(__dat) { }
8cab3d18 168#else
b96e2ff9 169 _GLIBCXX20_CONSTEXPR
8cab3d18
JW
170 _Alloc_hider(pointer __dat, const _Alloc& __a)
171 : allocator_type(__a), _M_p(__dat) { }
172
b96e2ff9 173 _GLIBCXX20_CONSTEXPR
8cab3d18
JW
174 _Alloc_hider(pointer __dat, _Alloc&& __a = _Alloc())
175 : allocator_type(std::move(__a)), _M_p(__dat) { }
176#endif
34a2b755
JW
177
178 pointer _M_p; // The actual data.
179 };
180
181 _Alloc_hider _M_dataplus;
182 size_type _M_string_length;
183
59aa28e8 184 enum { _S_local_capacity = 15 / sizeof(_CharT) };
34a2b755
JW
185
186 union
187 {
188 _CharT _M_local_buf[_S_local_capacity + 1];
189 size_type _M_allocated_capacity;
190 };
191
b96e2ff9 192 _GLIBCXX20_CONSTEXPR
34a2b755
JW
193 void
194 _M_data(pointer __p)
195 { _M_dataplus._M_p = __p; }
196
b96e2ff9 197 _GLIBCXX20_CONSTEXPR
34a2b755
JW
198 void
199 _M_length(size_type __length)
200 { _M_string_length = __length; }
201
b96e2ff9 202 _GLIBCXX20_CONSTEXPR
34a2b755
JW
203 pointer
204 _M_data() const
205 { return _M_dataplus._M_p; }
206
b96e2ff9 207 _GLIBCXX20_CONSTEXPR
34a2b755
JW
208 pointer
209 _M_local_data()
210 {
211#if __cplusplus >= 201103L
212 return std::pointer_traits<pointer>::pointer_to(*_M_local_buf);
213#else
214 return pointer(_M_local_buf);
215#endif
216 }
217
b96e2ff9 218 _GLIBCXX20_CONSTEXPR
34a2b755
JW
219 const_pointer
220 _M_local_data() const
221 {
222#if __cplusplus >= 201103L
223 return std::pointer_traits<const_pointer>::pointer_to(*_M_local_buf);
224#else
225 return const_pointer(_M_local_buf);
226#endif
227 }
228
b96e2ff9 229 _GLIBCXX20_CONSTEXPR
34a2b755
JW
230 void
231 _M_capacity(size_type __capacity)
232 { _M_allocated_capacity = __capacity; }
233
b96e2ff9 234 _GLIBCXX20_CONSTEXPR
34a2b755
JW
235 void
236 _M_set_length(size_type __n)
237 {
238 _M_length(__n);
239 traits_type::assign(_M_data()[__n], _CharT());
240 }
241
b96e2ff9 242 _GLIBCXX20_CONSTEXPR
34a2b755
JW
243 bool
244 _M_is_local() const
245 { return _M_data() == _M_local_data(); }
246
247 // Create & Destroy
b96e2ff9 248 _GLIBCXX20_CONSTEXPR
34a2b755
JW
249 pointer
250 _M_create(size_type&, size_type);
251
b96e2ff9 252 _GLIBCXX20_CONSTEXPR
34a2b755
JW
253 void
254 _M_dispose()
255 {
256 if (!_M_is_local())
257 _M_destroy(_M_allocated_capacity);
258 }
259
b96e2ff9 260 _GLIBCXX20_CONSTEXPR
34a2b755
JW
261 void
262 _M_destroy(size_type __size) throw()
263 { _Alloc_traits::deallocate(_M_get_allocator(), _M_data(), __size + 1); }
264
265 // _M_construct_aux is used to implement the 21.3.1 para 15 which
266 // requires special behaviour if _InIterator is an integral type
267 template<typename _InIterator>
b96e2ff9 268 _GLIBCXX20_CONSTEXPR
34a2b755
JW
269 void
270 _M_construct_aux(_InIterator __beg, _InIterator __end,
271 std::__false_type)
272 {
273 typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
274 _M_construct(__beg, __end, _Tag());
275 }
276
277 // _GLIBCXX_RESOLVE_LIB_DEFECTS
278 // 438. Ambiguity in the "do the right thing" clause
279 template<typename _Integer>
b96e2ff9 280 _GLIBCXX20_CONSTEXPR
34a2b755
JW
281 void
282 _M_construct_aux(_Integer __beg, _Integer __end, std::__true_type)
283 { _M_construct_aux_2(static_cast<size_type>(__beg), __end); }
284
b96e2ff9 285 _GLIBCXX20_CONSTEXPR
34a2b755
JW
286 void
287 _M_construct_aux_2(size_type __req, _CharT __c)
288 { _M_construct(__req, __c); }
289
290 template<typename _InIterator>
b96e2ff9 291 _GLIBCXX20_CONSTEXPR
34a2b755
JW
292 void
293 _M_construct(_InIterator __beg, _InIterator __end)
294 {
295 typedef typename std::__is_integer<_InIterator>::__type _Integral;
296 _M_construct_aux(__beg, __end, _Integral());
297 }
298
299 // For Input Iterators, used in istreambuf_iterators, etc.
300 template<typename _InIterator>
b96e2ff9 301 _GLIBCXX20_CONSTEXPR
34a2b755
JW
302 void
303 _M_construct(_InIterator __beg, _InIterator __end,
304 std::input_iterator_tag);
305
306 // For forward_iterators up to random_access_iterators, used for
307 // string::iterator, _CharT*, etc.
308 template<typename _FwdIterator>
b96e2ff9 309 _GLIBCXX20_CONSTEXPR
34a2b755
JW
310 void
311 _M_construct(_FwdIterator __beg, _FwdIterator __end,
312 std::forward_iterator_tag);
313
b96e2ff9 314 _GLIBCXX20_CONSTEXPR
34a2b755
JW
315 void
316 _M_construct(size_type __req, _CharT __c);
317
b96e2ff9 318 _GLIBCXX20_CONSTEXPR
34a2b755
JW
319 allocator_type&
320 _M_get_allocator()
321 { return _M_dataplus; }
322
b96e2ff9 323 _GLIBCXX20_CONSTEXPR
34a2b755
JW
324 const allocator_type&
325 _M_get_allocator() const
326 { return _M_dataplus; }
327
328 private:
329
330#ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
331 // The explicit instantiations in misc-inst.cc require this due to
332 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64063
333 template<typename _Tp, bool _Requires =
334 !__are_same<_Tp, _CharT*>::__value
335 && !__are_same<_Tp, const _CharT*>::__value
336 && !__are_same<_Tp, iterator>::__value
337 && !__are_same<_Tp, const_iterator>::__value>
338 struct __enable_if_not_native_iterator
339 { typedef basic_string& __type; };
340 template<typename _Tp>
341 struct __enable_if_not_native_iterator<_Tp, false> { };
342#endif
343
b96e2ff9 344 _GLIBCXX20_CONSTEXPR
34a2b755
JW
345 size_type
346 _M_check(size_type __pos, const char* __s) const
347 {
348 if (__pos > this->size())
349 __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
350 "this->size() (which is %zu)"),
351 __s, __pos, this->size());
352 return __pos;
353 }
354
b96e2ff9 355 _GLIBCXX20_CONSTEXPR
34a2b755
JW
356 void
357 _M_check_length(size_type __n1, size_type __n2, const char* __s) const
358 {
359 if (this->max_size() - (this->size() - __n1) < __n2)
360 __throw_length_error(__N(__s));
361 }
362
363
364 // NB: _M_limit doesn't check for a bad __pos value.
b96e2ff9 365 _GLIBCXX20_CONSTEXPR
34a2b755
JW
366 size_type
367 _M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT
368 {
369 const bool __testoff = __off < this->size() - __pos;
370 return __testoff ? __off : this->size() - __pos;
371 }
372
373 // True if _Rep and source do not overlap.
374 bool
375 _M_disjunct(const _CharT* __s) const _GLIBCXX_NOEXCEPT
376 {
2d60da10
JW
377 return (less<const _CharT*>()(__s, _M_data())
378 || less<const _CharT*>()(_M_data() + this->size(), __s));
34a2b755
JW
379 }
380
381 // When __n = 1 way faster than the general multichar
382 // traits_type::copy/move/assign.
b96e2ff9 383 _GLIBCXX20_CONSTEXPR
34a2b755
JW
384 static void
385 _S_copy(_CharT* __d, const _CharT* __s, size_type __n)
386 {
387 if (__n == 1)
388 traits_type::assign(*__d, *__s);
389 else
390 traits_type::copy(__d, __s, __n);
391 }
392
b96e2ff9 393 _GLIBCXX20_CONSTEXPR
34a2b755
JW
394 static void
395 _S_move(_CharT* __d, const _CharT* __s, size_type __n)
396 {
397 if (__n == 1)
398 traits_type::assign(*__d, *__s);
399 else
400 traits_type::move(__d, __s, __n);
401 }
402
b96e2ff9 403 _GLIBCXX20_CONSTEXPR
34a2b755
JW
404 static void
405 _S_assign(_CharT* __d, size_type __n, _CharT __c)
406 {
407 if (__n == 1)
408 traits_type::assign(*__d, __c);
409 else
410 traits_type::assign(__d, __n, __c);
411 }
412
413 // _S_copy_chars is a separate template to permit specialization
414 // to optimize for the common case of pointers as iterators.
415 template<class _Iterator>
b96e2ff9 416 _GLIBCXX20_CONSTEXPR
34a2b755
JW
417 static void
418 _S_copy_chars(_CharT* __p, _Iterator __k1, _Iterator __k2)
34a2b755 419 {
f970a17d 420 for (; __k1 != __k2; ++__k1, (void)++__p)
34a2b755
JW
421 traits_type::assign(*__p, *__k1); // These types are off.
422 }
423
b96e2ff9 424 _GLIBCXX20_CONSTEXPR
34a2b755
JW
425 static void
426 _S_copy_chars(_CharT* __p, iterator __k1, iterator __k2) _GLIBCXX_NOEXCEPT
427 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
428
b96e2ff9 429 _GLIBCXX20_CONSTEXPR
34a2b755
JW
430 static void
431 _S_copy_chars(_CharT* __p, const_iterator __k1, const_iterator __k2)
432 _GLIBCXX_NOEXCEPT
433 { _S_copy_chars(__p, __k1.base(), __k2.base()); }
434
b96e2ff9 435 _GLIBCXX20_CONSTEXPR
34a2b755
JW
436 static void
437 _S_copy_chars(_CharT* __p, _CharT* __k1, _CharT* __k2) _GLIBCXX_NOEXCEPT
438 { _S_copy(__p, __k1, __k2 - __k1); }
439
b96e2ff9 440 _GLIBCXX20_CONSTEXPR
34a2b755
JW
441 static void
442 _S_copy_chars(_CharT* __p, const _CharT* __k1, const _CharT* __k2)
443 _GLIBCXX_NOEXCEPT
444 { _S_copy(__p, __k1, __k2 - __k1); }
445
b96e2ff9 446 _GLIBCXX20_CONSTEXPR
34a2b755
JW
447 static int
448 _S_compare(size_type __n1, size_type __n2) _GLIBCXX_NOEXCEPT
449 {
450 const difference_type __d = difference_type(__n1 - __n2);
451
452 if (__d > __gnu_cxx::__numeric_traits<int>::__max)
453 return __gnu_cxx::__numeric_traits<int>::__max;
454 else if (__d < __gnu_cxx::__numeric_traits<int>::__min)
455 return __gnu_cxx::__numeric_traits<int>::__min;
456 else
457 return int(__d);
458 }
459
b96e2ff9 460 _GLIBCXX20_CONSTEXPR
34a2b755 461 void
a7d47f35 462 _M_assign(const basic_string&);
34a2b755 463
b96e2ff9 464 _GLIBCXX20_CONSTEXPR
34a2b755
JW
465 void
466 _M_mutate(size_type __pos, size_type __len1, const _CharT* __s,
467 size_type __len2);
468
b96e2ff9 469 _GLIBCXX20_CONSTEXPR
34a2b755
JW
470 void
471 _M_erase(size_type __pos, size_type __n);
472
473 public:
474 // Construct/copy/destroy:
475 // NB: We overload ctors in some cases instead of using default
476 // arguments, per 17.4.4.4 para. 2 item 2.
477
478 /**
479 * @brief Default constructor creates an empty string.
480 */
b96e2ff9 481 _GLIBCXX20_CONSTEXPR
bcb896ab 482 basic_string()
5caff414 483 _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Alloc>::value)
34a2b755
JW
484 : _M_dataplus(_M_local_data())
485 { _M_set_length(0); }
486
487 /**
488 * @brief Construct an empty string using allocator @a a.
489 */
b96e2ff9 490 _GLIBCXX20_CONSTEXPR
34a2b755 491 explicit
5caff414 492 basic_string(const _Alloc& __a) _GLIBCXX_NOEXCEPT
34a2b755
JW
493 : _M_dataplus(_M_local_data(), __a)
494 { _M_set_length(0); }
495
496 /**
497 * @brief Construct string with copy of value of @a __str.
498 * @param __str Source string.
499 */
b96e2ff9 500 _GLIBCXX20_CONSTEXPR
34a2b755 501 basic_string(const basic_string& __str)
5caff414
JW
502 : _M_dataplus(_M_local_data(),
503 _Alloc_traits::_S_select_on_copy(__str._M_get_allocator()))
34a2b755
JW
504 { _M_construct(__str._M_data(), __str._M_data() + __str.length()); }
505
86bbf15b
JW
506 // _GLIBCXX_RESOLVE_LIB_DEFECTS
507 // 2583. no way to supply an allocator for basic_string(str, pos)
34a2b755
JW
508 /**
509 * @brief Construct string as copy of a substring.
510 * @param __str Source string.
511 * @param __pos Index of first character to copy from.
86bbf15b 512 * @param __a Allocator to use.
34a2b755 513 */
b96e2ff9 514 _GLIBCXX20_CONSTEXPR
34a2b755 515 basic_string(const basic_string& __str, size_type __pos,
86bbf15b
JW
516 const _Alloc& __a = _Alloc())
517 : _M_dataplus(_M_local_data(), __a)
518 {
519 const _CharT* __start = __str._M_data()
520 + __str._M_check(__pos, "basic_string::basic_string");
521 _M_construct(__start, __start + __str._M_limit(__pos, npos));
522 }
523
524 /**
525 * @brief Construct string as copy of a substring.
526 * @param __str Source string.
527 * @param __pos Index of first character to copy from.
528 * @param __n Number of characters to copy.
529 */
b96e2ff9 530 _GLIBCXX20_CONSTEXPR
86bbf15b
JW
531 basic_string(const basic_string& __str, size_type __pos,
532 size_type __n)
34a2b755
JW
533 : _M_dataplus(_M_local_data())
534 {
2d60da10 535 const _CharT* __start = __str._M_data()
34a2b755
JW
536 + __str._M_check(__pos, "basic_string::basic_string");
537 _M_construct(__start, __start + __str._M_limit(__pos, __n));
538 }
539
540 /**
541 * @brief Construct string as copy of a substring.
542 * @param __str Source string.
543 * @param __pos Index of first character to copy from.
86bbf15b 544 * @param __n Number of characters to copy.
34a2b755
JW
545 * @param __a Allocator to use.
546 */
b96e2ff9 547 _GLIBCXX20_CONSTEXPR
34a2b755
JW
548 basic_string(const basic_string& __str, size_type __pos,
549 size_type __n, const _Alloc& __a)
550 : _M_dataplus(_M_local_data(), __a)
551 {
2d60da10
JW
552 const _CharT* __start
553 = __str._M_data() + __str._M_check(__pos, "string::string");
34a2b755
JW
554 _M_construct(__start, __start + __str._M_limit(__pos, __n));
555 }
556
557 /**
558 * @brief Construct string initialized by a character %array.
559 * @param __s Source character %array.
560 * @param __n Number of characters to copy.
561 * @param __a Allocator to use (default is default allocator).
562 *
563 * NB: @a __s must have at least @a __n characters, &apos;\\0&apos;
564 * has no special meaning.
565 */
b96e2ff9 566 _GLIBCXX20_CONSTEXPR
34a2b755
JW
567 basic_string(const _CharT* __s, size_type __n,
568 const _Alloc& __a = _Alloc())
569 : _M_dataplus(_M_local_data(), __a)
570 { _M_construct(__s, __s + __n); }
571
572 /**
573 * @brief Construct string as copy of a C string.
574 * @param __s Source C string.
575 * @param __a Allocator to use (default is default allocator).
576 */
5d84e6c5
JW
577#if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
578 // _GLIBCXX_RESOLVE_LIB_DEFECTS
579 // 3076. basic_string CTAD ambiguity
580 template<typename = _RequireAllocator<_Alloc>>
581#endif
b96e2ff9 582 _GLIBCXX20_CONSTEXPR
34a2b755
JW
583 basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
584 : _M_dataplus(_M_local_data(), __a)
789c57bc
JW
585 {
586 const _CharT* __end = __s ? __s + traits_type::length(__s)
587 // We just need a non-null pointer here to get an exception:
588 : reinterpret_cast<const _CharT*>(__alignof__(_CharT));
589 _M_construct(__s, __end, random_access_iterator_tag());
590 }
34a2b755
JW
591
592 /**
593 * @brief Construct string as multiple characters.
594 * @param __n Number of characters.
595 * @param __c Character to use.
596 * @param __a Allocator to use (default is default allocator).
597 */
5d84e6c5
JW
598#if __cpp_deduction_guides && ! defined _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
599 // _GLIBCXX_RESOLVE_LIB_DEFECTS
600 // 3076. basic_string CTAD ambiguity
601 template<typename = _RequireAllocator<_Alloc>>
602#endif
b96e2ff9 603 _GLIBCXX20_CONSTEXPR
34a2b755
JW
604 basic_string(size_type __n, _CharT __c, const _Alloc& __a = _Alloc())
605 : _M_dataplus(_M_local_data(), __a)
606 { _M_construct(__n, __c); }
607
608#if __cplusplus >= 201103L
609 /**
610 * @brief Move construct string.
611 * @param __str Source string.
612 *
613 * The newly-created string contains the exact contents of @a __str.
614 * @a __str is a valid, but unspecified string.
252c9967 615 */
b96e2ff9 616 _GLIBCXX20_CONSTEXPR
34a2b755
JW
617 basic_string(basic_string&& __str) noexcept
618 : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator()))
619 {
620 if (__str._M_is_local())
621 {
faa7d78e 622 traits_type::copy(_M_local_buf, __str._M_local_buf,
b96e2ff9 623 __str.length() + 1);
34a2b755
JW
624 }
625 else
626 {
627 _M_data(__str._M_data());
628 _M_capacity(__str._M_allocated_capacity);
629 }
630
631 // Must use _M_length() here not _M_set_length() because
632 // basic_stringbuf relies on writing into unallocated capacity so
633 // we mess up the contents if we put a '\0' in the string.
634 _M_length(__str.length());
635 __str._M_data(__str._M_local_data());
636 __str._M_set_length(0);
637 }
638
639 /**
640 * @brief Construct string from an initializer %list.
641 * @param __l std::initializer_list of characters.
642 * @param __a Allocator to use (default is default allocator).
643 */
b96e2ff9 644 _GLIBCXX20_CONSTEXPR
34a2b755
JW
645 basic_string(initializer_list<_CharT> __l, const _Alloc& __a = _Alloc())
646 : _M_dataplus(_M_local_data(), __a)
647 { _M_construct(__l.begin(), __l.end()); }
648
b96e2ff9 649 _GLIBCXX20_CONSTEXPR
34a2b755
JW
650 basic_string(const basic_string& __str, const _Alloc& __a)
651 : _M_dataplus(_M_local_data(), __a)
652 { _M_construct(__str.begin(), __str.end()); }
653
b96e2ff9 654 _GLIBCXX20_CONSTEXPR
34a2b755 655 basic_string(basic_string&& __str, const _Alloc& __a)
5caff414 656 noexcept(_Alloc_traits::_S_always_equal())
34a2b755
JW
657 : _M_dataplus(_M_local_data(), __a)
658 {
5caff414
JW
659 if (__str._M_is_local())
660 {
661 traits_type::copy(_M_local_buf, __str._M_local_buf,
b96e2ff9 662 __str.length() + 1);
5caff414
JW
663 _M_length(__str.length());
664 __str._M_set_length(0);
665 }
666 else if (_Alloc_traits::_S_always_equal()
667 || __str.get_allocator() == __a)
668 {
669 _M_data(__str._M_data());
670 _M_length(__str.length());
671 _M_capacity(__str._M_allocated_capacity);
672 __str._M_data(__str._M_local_buf);
673 __str._M_set_length(0);
674 }
34a2b755
JW
675 else
676 _M_construct(__str.begin(), __str.end());
677 }
678
cf876562
JW
679 basic_string(nullptr_t) = delete;
680 basic_string& operator=(nullptr_t) = delete;
34a2b755
JW
681#endif // C++11
682
683 /**
684 * @brief Construct string as copy of a range.
685 * @param __beg Start of range.
686 * @param __end End of range.
687 * @param __a Allocator to use (default is default allocator).
688 */
689#if __cplusplus >= 201103L
690 template<typename _InputIterator,
691 typename = std::_RequireInputIter<_InputIterator>>
692#else
693 template<typename _InputIterator>
694#endif
b96e2ff9 695 _GLIBCXX20_CONSTEXPR
34a2b755
JW
696 basic_string(_InputIterator __beg, _InputIterator __end,
697 const _Alloc& __a = _Alloc())
698 : _M_dataplus(_M_local_data(), __a)
699 { _M_construct(__beg, __end); }
700
1a289fa3 701#if __cplusplus >= 201703L
bf56b0b8
JW
702 /**
703 * @brief Construct string from a substring of a string_view.
df66af3b 704 * @param __t Source object convertible to string view.
bf56b0b8
JW
705 * @param __pos The index of the first character to copy from __t.
706 * @param __n The number of characters to copy from __t.
707 * @param __a Allocator to use.
708 */
3663671a 709 template<typename _Tp, typename = _If_sv<_Tp, void>>
b96e2ff9 710 _GLIBCXX20_CONSTEXPR
bf56b0b8
JW
711 basic_string(const _Tp& __t, size_type __pos, size_type __n,
712 const _Alloc& __a = _Alloc())
df66af3b 713 : basic_string(_S_to_string_view(__t).substr(__pos, __n), __a) { }
bf56b0b8 714
ca8f2cb1
VV
715 /**
716 * @brief Construct string from a string_view.
df66af3b 717 * @param __t Source object convertible to string view.
ca8f2cb1
VV
718 * @param __a Allocator to use (default is default allocator).
719 */
df66af3b 720 template<typename _Tp, typename = _If_sv<_Tp, void>>
b96e2ff9 721 _GLIBCXX20_CONSTEXPR
df66af3b
DK
722 explicit
723 basic_string(const _Tp& __t, const _Alloc& __a = _Alloc())
724 : basic_string(__sv_wrapper(_S_to_string_view(__t)), __a) { }
ca8f2cb1
VV
725#endif // C++17
726
34a2b755
JW
727 /**
728 * @brief Destroy the string instance.
729 */
b96e2ff9 730 _GLIBCXX20_CONSTEXPR
34a2b755
JW
731 ~basic_string()
732 { _M_dispose(); }
733
734 /**
735 * @brief Assign the value of @a str to this string.
736 * @param __str Source string.
737 */
b96e2ff9 738 _GLIBCXX20_CONSTEXPR
34a2b755
JW
739 basic_string&
740 operator=(const basic_string& __str)
5caff414 741 {
11d10beb 742 return this->assign(__str);
5caff414 743 }
34a2b755
JW
744
745 /**
746 * @brief Copy contents of @a s into this string.
747 * @param __s Source null-terminated string.
748 */
b96e2ff9 749 _GLIBCXX20_CONSTEXPR
34a2b755
JW
750 basic_string&
751 operator=(const _CharT* __s)
752 { return this->assign(__s); }
753
754 /**
755 * @brief Set value to string of length 1.
756 * @param __c Source character.
757 *
758 * Assigning to a character makes this string length 1 and
759 * (*this)[0] == @a c.
760 */
b96e2ff9 761 _GLIBCXX20_CONSTEXPR
34a2b755
JW
762 basic_string&
763 operator=(_CharT __c)
764 {
765 this->assign(1, __c);
766 return *this;
767 }
768
769#if __cplusplus >= 201103L
770 /**
771 * @brief Move assign the value of @a str to this string.
772 * @param __str Source string.
773 *
774 * The contents of @a str are moved into this string (without copying).
775 * @a str is a valid, but unspecified string.
252c9967 776 */
34a2b755
JW
777 // _GLIBCXX_RESOLVE_LIB_DEFECTS
778 // 2063. Contradictory requirements for string move assignment
b96e2ff9 779 _GLIBCXX20_CONSTEXPR
34a2b755
JW
780 basic_string&
781 operator=(basic_string&& __str)
5caff414 782 noexcept(_Alloc_traits::_S_nothrow_move())
34a2b755 783 {
5caff414
JW
784 if (!_M_is_local() && _Alloc_traits::_S_propagate_on_move_assign()
785 && !_Alloc_traits::_S_always_equal()
786 && _M_get_allocator() != __str._M_get_allocator())
787 {
788 // Destroy existing storage before replacing allocator.
789 _M_destroy(_M_allocated_capacity);
790 _M_data(_M_local_data());
791 _M_set_length(0);
792 }
793 // Replace allocator if POCMA is true.
794 std::__alloc_on_move(_M_get_allocator(), __str._M_get_allocator());
795
71e09389
JW
796 if (__str._M_is_local())
797 {
c2fb0a1a
JW
798 // We've always got room for a short string, just copy it
799 // (unless this is a self-move, because that would violate the
800 // char_traits::copy precondition that the ranges don't overlap).
801 if (__builtin_expect(std::__addressof(__str) != this, true))
802 {
803 if (__str.size())
804 this->_S_copy(_M_data(), __str._M_data(), __str.size());
805 _M_set_length(__str.size());
806 }
71e09389
JW
807 }
808 else if (_Alloc_traits::_S_propagate_on_move_assign()
809 || _Alloc_traits::_S_always_equal()
810 || _M_get_allocator() == __str._M_get_allocator())
5caff414 811 {
71e09389 812 // Just move the allocated pointer, our allocator can free it.
5caff414
JW
813 pointer __data = nullptr;
814 size_type __capacity;
815 if (!_M_is_local())
816 {
817 if (_Alloc_traits::_S_always_equal())
818 {
71e09389 819 // __str can reuse our existing storage.
5caff414
JW
820 __data = _M_data();
821 __capacity = _M_allocated_capacity;
822 }
71e09389 823 else // __str can't use it, so free it.
5caff414
JW
824 _M_destroy(_M_allocated_capacity);
825 }
826
827 _M_data(__str._M_data());
828 _M_length(__str.length());
829 _M_capacity(__str._M_allocated_capacity);
830 if (__data)
831 {
832 __str._M_data(__data);
833 __str._M_capacity(__capacity);
834 }
835 else
836 __str._M_data(__str._M_local_buf);
837 }
71e09389
JW
838 else // Need to do a deep copy
839 assign(__str);
5caff414 840 __str.clear();
34a2b755
JW
841 return *this;
842 }
843
844 /**
845 * @brief Set value to string constructed from initializer %list.
846 * @param __l std::initializer_list.
847 */
b96e2ff9 848 _GLIBCXX20_CONSTEXPR
34a2b755
JW
849 basic_string&
850 operator=(initializer_list<_CharT> __l)
851 {
852 this->assign(__l.begin(), __l.size());
853 return *this;
854 }
855#endif // C++11
856
1a289fa3 857#if __cplusplus >= 201703L
ca8f2cb1
VV
858 /**
859 * @brief Set value to string constructed from a string_view.
df66af3b 860 * @param __svt An object convertible to string_view.
ca8f2cb1 861 */
df66af3b 862 template<typename _Tp>
b96e2ff9 863 _GLIBCXX20_CONSTEXPR
df66af3b
DK
864 _If_sv<_Tp, basic_string&>
865 operator=(const _Tp& __svt)
866 { return this->assign(__svt); }
ca8f2cb1
VV
867
868 /**
869 * @brief Convert to a string_view.
870 * @return A string_view.
871 */
b96e2ff9 872 _GLIBCXX20_CONSTEXPR
ca8f2cb1 873 operator __sv_type() const noexcept
df66af3b 874 { return __sv_type(data(), size()); }
ca8f2cb1
VV
875#endif // C++17
876
34a2b755
JW
877 // Iterators:
878 /**
879 * Returns a read/write iterator that points to the first character in
880 * the %string.
881 */
b96e2ff9 882 _GLIBCXX20_CONSTEXPR
34a2b755
JW
883 iterator
884 begin() _GLIBCXX_NOEXCEPT
885 { return iterator(_M_data()); }
886
887 /**
888 * Returns a read-only (constant) iterator that points to the first
889 * character in the %string.
890 */
b96e2ff9 891 _GLIBCXX20_CONSTEXPR
34a2b755
JW
892 const_iterator
893 begin() const _GLIBCXX_NOEXCEPT
894 { return const_iterator(_M_data()); }
895
896 /**
897 * Returns a read/write iterator that points one past the last
898 * character in the %string.
899 */
b96e2ff9 900 _GLIBCXX20_CONSTEXPR
34a2b755
JW
901 iterator
902 end() _GLIBCXX_NOEXCEPT
903 { return iterator(_M_data() + this->size()); }
904
905 /**
906 * Returns a read-only (constant) iterator that points one past the
907 * last character in the %string.
908 */
b96e2ff9 909 _GLIBCXX20_CONSTEXPR
34a2b755
JW
910 const_iterator
911 end() const _GLIBCXX_NOEXCEPT
912 { return const_iterator(_M_data() + this->size()); }
913
914 /**
915 * Returns a read/write reverse iterator that points to the last
916 * character in the %string. Iteration is done in reverse element
917 * order.
918 */
b96e2ff9 919 _GLIBCXX20_CONSTEXPR
34a2b755
JW
920 reverse_iterator
921 rbegin() _GLIBCXX_NOEXCEPT
922 { return reverse_iterator(this->end()); }
923
924 /**
925 * Returns a read-only (constant) reverse iterator that points
926 * to the last character in the %string. Iteration is done in
927 * reverse element order.
928 */
b96e2ff9 929 _GLIBCXX20_CONSTEXPR
34a2b755
JW
930 const_reverse_iterator
931 rbegin() const _GLIBCXX_NOEXCEPT
932 { return const_reverse_iterator(this->end()); }
933
934 /**
935 * Returns a read/write reverse iterator that points to one before the
936 * first character in the %string. Iteration is done in reverse
937 * element order.
938 */
b96e2ff9 939 _GLIBCXX20_CONSTEXPR
34a2b755
JW
940 reverse_iterator
941 rend() _GLIBCXX_NOEXCEPT
942 { return reverse_iterator(this->begin()); }
943
944 /**
945 * Returns a read-only (constant) reverse iterator that points
946 * to one before the first character in the %string. Iteration
947 * is done in reverse element order.
948 */
b96e2ff9 949 _GLIBCXX20_CONSTEXPR
34a2b755
JW
950 const_reverse_iterator
951 rend() const _GLIBCXX_NOEXCEPT
952 { return const_reverse_iterator(this->begin()); }
953
954#if __cplusplus >= 201103L
955 /**
956 * Returns a read-only (constant) iterator that points to the first
957 * character in the %string.
958 */
b96e2ff9 959 _GLIBCXX20_CONSTEXPR
34a2b755
JW
960 const_iterator
961 cbegin() const noexcept
962 { return const_iterator(this->_M_data()); }
963
964 /**
965 * Returns a read-only (constant) iterator that points one past the
966 * last character in the %string.
967 */
b96e2ff9 968 _GLIBCXX20_CONSTEXPR
34a2b755
JW
969 const_iterator
970 cend() const noexcept
971 { return const_iterator(this->_M_data() + this->size()); }
972
973 /**
974 * Returns a read-only (constant) reverse iterator that points
975 * to the last character in the %string. Iteration is done in
976 * reverse element order.
977 */
b96e2ff9 978 _GLIBCXX20_CONSTEXPR
34a2b755
JW
979 const_reverse_iterator
980 crbegin() const noexcept
981 { return const_reverse_iterator(this->end()); }
982
983 /**
984 * Returns a read-only (constant) reverse iterator that points
985 * to one before the first character in the %string. Iteration
986 * is done in reverse element order.
987 */
b96e2ff9 988 _GLIBCXX20_CONSTEXPR
34a2b755
JW
989 const_reverse_iterator
990 crend() const noexcept
991 { return const_reverse_iterator(this->begin()); }
992#endif
993
994 public:
995 // Capacity:
996 /// Returns the number of characters in the string, not including any
997 /// null-termination.
b96e2ff9 998 _GLIBCXX20_CONSTEXPR
34a2b755
JW
999 size_type
1000 size() const _GLIBCXX_NOEXCEPT
1001 { return _M_string_length; }
1002
1003 /// Returns the number of characters in the string, not including any
1004 /// null-termination.
b96e2ff9 1005 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1006 size_type
1007 length() const _GLIBCXX_NOEXCEPT
1008 { return _M_string_length; }
1009
1010 /// Returns the size() of the largest possible %string.
b96e2ff9 1011 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1012 size_type
1013 max_size() const _GLIBCXX_NOEXCEPT
1014 { return (_Alloc_traits::max_size(_M_get_allocator()) - 1) / 2; }
1015
1016 /**
1017 * @brief Resizes the %string to the specified number of characters.
1018 * @param __n Number of characters the %string should contain.
1019 * @param __c Character to fill any new elements.
1020 *
1021 * This function will %resize the %string to the specified
1022 * number of characters. If the number is smaller than the
1023 * %string's current size the %string is truncated, otherwise
1024 * the %string is extended and new elements are %set to @a __c.
1025 */
b96e2ff9 1026 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1027 void
1028 resize(size_type __n, _CharT __c);
1029
1030 /**
1031 * @brief Resizes the %string to the specified number of characters.
1032 * @param __n Number of characters the %string should contain.
1033 *
1034 * This function will resize the %string to the specified length. If
1035 * the new size is smaller than the %string's current size the %string
1036 * is truncated, otherwise the %string is extended and new characters
1037 * are default-constructed. For basic types such as char, this means
1038 * setting them to 0.
1039 */
b96e2ff9 1040 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1041 void
1042 resize(size_type __n)
1043 { this->resize(__n, _CharT()); }
1044
1045#if __cplusplus >= 201103L
140cf935
AL
1046#pragma GCC diagnostic push
1047#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
34a2b755 1048 /// A non-binding request to reduce capacity() to size().
b96e2ff9 1049 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1050 void
1051 shrink_to_fit() noexcept
140cf935
AL
1052 { reserve(); }
1053#pragma GCC diagnostic pop
34a2b755
JW
1054#endif
1055
929abc7f
JW
1056#if __cplusplus > 202002L
1057#define __cpp_lib_string_resize_and_overwrite 202110L
1058 template<typename _Operation>
1059 constexpr void
1060 resize_and_overwrite(size_type __n, _Operation __op);
1061#endif
1062
34a2b755
JW
1063 /**
1064 * Returns the total number of characters that the %string can hold
1065 * before needing to allocate more memory.
1066 */
b96e2ff9 1067 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1068 size_type
1069 capacity() const _GLIBCXX_NOEXCEPT
1070 {
1071 return _M_is_local() ? size_type(_S_local_capacity)
1072 : _M_allocated_capacity;
1073 }
1074
1075 /**
1076 * @brief Attempt to preallocate enough memory for specified number of
1077 * characters.
1078 * @param __res_arg Number of characters required.
1079 * @throw std::length_error If @a __res_arg exceeds @c max_size().
1080 *
1081 * This function attempts to reserve enough memory for the
1082 * %string to hold the specified number of characters. If the
1083 * number requested is more than max_size(), length_error is
1084 * thrown.
1085 *
1086 * The advantage of this function is that if optimal code is a
1087 * necessity and the user can determine the string length that will be
1088 * required, the user can reserve the memory in %advance, and thus
1089 * prevent a possible reallocation of memory and copying of %string
1090 * data.
1091 */
b96e2ff9 1092 _GLIBCXX20_CONSTEXPR
34a2b755 1093 void
140cf935
AL
1094 reserve(size_type __res_arg);
1095
1096 /**
1097 * Equivalent to shrink_to_fit().
1098 */
1099#if __cplusplus > 201703L
1100 [[deprecated("use shrink_to_fit() instead")]]
1101#endif
b96e2ff9 1102 _GLIBCXX20_CONSTEXPR
140cf935
AL
1103 void
1104 reserve();
34a2b755
JW
1105
1106 /**
1107 * Erases the string, making it empty.
1108 */
b96e2ff9 1109 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1110 void
1111 clear() _GLIBCXX_NOEXCEPT
1112 { _M_set_length(0); }
1113
1114 /**
1115 * Returns true if the %string is empty. Equivalent to
1116 * <code>*this == ""</code>.
1117 */
b96e2ff9
ML
1118 _GLIBCXX_NODISCARD _GLIBCXX20_CONSTEXPR
1119 bool
34a2b755
JW
1120 empty() const _GLIBCXX_NOEXCEPT
1121 { return this->size() == 0; }
1122
1123 // Element access:
1124 /**
1125 * @brief Subscript access to the data contained in the %string.
1126 * @param __pos The index of the character to access.
1127 * @return Read-only (constant) reference to the character.
1128 *
1129 * This operator allows for easy, array-style, data access.
1130 * Note that data access with this operator is unchecked and
1131 * out_of_range lookups are not defined. (For checked lookups
1132 * see at().)
1133 */
b96e2ff9 1134 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1135 const_reference
1136 operator[] (size_type __pos) const _GLIBCXX_NOEXCEPT
1137 {
2f1e8e7c 1138 __glibcxx_assert(__pos <= size());
34a2b755
JW
1139 return _M_data()[__pos];
1140 }
1141
1142 /**
1143 * @brief Subscript access to the data contained in the %string.
1144 * @param __pos The index of the character to access.
1145 * @return Read/write reference to the character.
1146 *
1147 * This operator allows for easy, array-style, data access.
1148 * Note that data access with this operator is unchecked and
1149 * out_of_range lookups are not defined. (For checked lookups
1150 * see at().)
1151 */
b96e2ff9 1152 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1153 reference
1154 operator[](size_type __pos)
1155 {
1156 // Allow pos == size() both in C++98 mode, as v3 extension,
1157 // and in C++11 mode.
2f1e8e7c 1158 __glibcxx_assert(__pos <= size());
34a2b755
JW
1159 // In pedantic mode be strict in C++98 mode.
1160 _GLIBCXX_DEBUG_PEDASSERT(__cplusplus >= 201103L || __pos < size());
1161 return _M_data()[__pos];
1162 }
1163
1164 /**
1165 * @brief Provides access to the data contained in the %string.
1166 * @param __n The index of the character to access.
1167 * @return Read-only (const) reference to the character.
1168 * @throw std::out_of_range If @a n is an invalid index.
1169 *
1170 * This function provides for safer data access. The parameter is
1171 * first checked that it is in the range of the string. The function
1172 * throws out_of_range if the check fails.
1173 */
b96e2ff9 1174 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1175 const_reference
1176 at(size_type __n) const
1177 {
1178 if (__n >= this->size())
1179 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1180 "(which is %zu) >= this->size() "
1181 "(which is %zu)"),
1182 __n, this->size());
1183 return _M_data()[__n];
1184 }
1185
1186 /**
1187 * @brief Provides access to the data contained in the %string.
1188 * @param __n The index of the character to access.
1189 * @return Read/write reference to the character.
1190 * @throw std::out_of_range If @a n is an invalid index.
1191 *
1192 * This function provides for safer data access. The parameter is
1193 * first checked that it is in the range of the string. The function
1194 * throws out_of_range if the check fails.
1195 */
b96e2ff9 1196 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1197 reference
1198 at(size_type __n)
1199 {
1200 if (__n >= size())
1201 __throw_out_of_range_fmt(__N("basic_string::at: __n "
1202 "(which is %zu) >= this->size() "
1203 "(which is %zu)"),
1204 __n, this->size());
1205 return _M_data()[__n];
1206 }
1207
1208#if __cplusplus >= 201103L
1209 /**
1210 * Returns a read/write reference to the data at the first
1211 * element of the %string.
1212 */
b96e2ff9 1213 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1214 reference
1215 front() noexcept
e25d2617 1216 {
2f1e8e7c 1217 __glibcxx_assert(!empty());
e25d2617
FD
1218 return operator[](0);
1219 }
34a2b755
JW
1220
1221 /**
1222 * Returns a read-only (constant) reference to the data at the first
1223 * element of the %string.
1224 */
b96e2ff9 1225 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1226 const_reference
1227 front() const noexcept
e25d2617 1228 {
2f1e8e7c 1229 __glibcxx_assert(!empty());
e25d2617
FD
1230 return operator[](0);
1231 }
34a2b755
JW
1232
1233 /**
1234 * Returns a read/write reference to the data at the last
1235 * element of the %string.
1236 */
b96e2ff9 1237 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1238 reference
1239 back() noexcept
e25d2617 1240 {
2f1e8e7c 1241 __glibcxx_assert(!empty());
e25d2617
FD
1242 return operator[](this->size() - 1);
1243 }
34a2b755
JW
1244
1245 /**
1246 * Returns a read-only (constant) reference to the data at the
1247 * last element of the %string.
1248 */
b96e2ff9 1249 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1250 const_reference
1251 back() const noexcept
e25d2617 1252 {
2f1e8e7c 1253 __glibcxx_assert(!empty());
e25d2617
FD
1254 return operator[](this->size() - 1);
1255 }
34a2b755
JW
1256#endif
1257
1258 // Modifiers:
1259 /**
1260 * @brief Append a string to this string.
1261 * @param __str The string to append.
1262 * @return Reference to this string.
1263 */
b96e2ff9 1264 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1265 basic_string&
1266 operator+=(const basic_string& __str)
1267 { return this->append(__str); }
1268
1269 /**
1270 * @brief Append a C string.
1271 * @param __s The C string to append.
1272 * @return Reference to this string.
1273 */
b96e2ff9 1274 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1275 basic_string&
1276 operator+=(const _CharT* __s)
1277 { return this->append(__s); }
1278
1279 /**
1280 * @brief Append a character.
1281 * @param __c The character to append.
1282 * @return Reference to this string.
1283 */
b96e2ff9 1284 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1285 basic_string&
1286 operator+=(_CharT __c)
1287 {
1288 this->push_back(__c);
1289 return *this;
1290 }
1291
1292#if __cplusplus >= 201103L
1293 /**
1294 * @brief Append an initializer_list of characters.
1295 * @param __l The initializer_list of characters to be appended.
1296 * @return Reference to this string.
1297 */
b96e2ff9 1298 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1299 basic_string&
1300 operator+=(initializer_list<_CharT> __l)
1301 { return this->append(__l.begin(), __l.size()); }
1302#endif // C++11
1303
1a289fa3 1304#if __cplusplus >= 201703L
ca8f2cb1
VV
1305 /**
1306 * @brief Append a string_view.
df66af3b 1307 * @param __svt An object convertible to string_view to be appended.
ca8f2cb1
VV
1308 * @return Reference to this string.
1309 */
df66af3b 1310 template<typename _Tp>
b96e2ff9 1311 _GLIBCXX20_CONSTEXPR
df66af3b
DK
1312 _If_sv<_Tp, basic_string&>
1313 operator+=(const _Tp& __svt)
1314 { return this->append(__svt); }
ca8f2cb1
VV
1315#endif // C++17
1316
34a2b755
JW
1317 /**
1318 * @brief Append a string to this string.
1319 * @param __str The string to append.
1320 * @return Reference to this string.
1321 */
b96e2ff9 1322 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1323 basic_string&
1324 append(const basic_string& __str)
2d60da10 1325 { return _M_append(__str._M_data(), __str.size()); }
34a2b755
JW
1326
1327 /**
1328 * @brief Append a substring.
1329 * @param __str The string to append.
1330 * @param __pos Index of the first character of str to append.
1331 * @param __n The number of characters to append.
1332 * @return Reference to this string.
1333 * @throw std::out_of_range if @a __pos is not a valid index.
1334 *
1335 * This function appends @a __n characters from @a __str
1336 * starting at @a __pos to this string. If @a __n is is larger
1337 * than the number of available characters in @a __str, the
1338 * remainder of @a __str is appended.
1339 */
b96e2ff9 1340 _GLIBCXX20_CONSTEXPR
34a2b755 1341 basic_string&
852ee53c 1342 append(const basic_string& __str, size_type __pos, size_type __n = npos)
2d60da10 1343 { return _M_append(__str._M_data()
34a2b755
JW
1344 + __str._M_check(__pos, "basic_string::append"),
1345 __str._M_limit(__pos, __n)); }
1346
1347 /**
1348 * @brief Append a C substring.
1349 * @param __s The C string to append.
1350 * @param __n The number of characters to append.
1351 * @return Reference to this string.
1352 */
b96e2ff9 1353 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1354 basic_string&
1355 append(const _CharT* __s, size_type __n)
1356 {
1357 __glibcxx_requires_string_len(__s, __n);
1358 _M_check_length(size_type(0), __n, "basic_string::append");
1359 return _M_append(__s, __n);
1360 }
1361
1362 /**
1363 * @brief Append a C string.
1364 * @param __s The C string to append.
1365 * @return Reference to this string.
1366 */
b96e2ff9 1367 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1368 basic_string&
1369 append(const _CharT* __s)
1370 {
1371 __glibcxx_requires_string(__s);
1372 const size_type __n = traits_type::length(__s);
1373 _M_check_length(size_type(0), __n, "basic_string::append");
1374 return _M_append(__s, __n);
1375 }
1376
1377 /**
1378 * @brief Append multiple characters.
1379 * @param __n The number of characters to append.
1380 * @param __c The character to use.
1381 * @return Reference to this string.
1382 *
1383 * Appends __n copies of __c to this string.
1384 */
b96e2ff9 1385 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1386 basic_string&
1387 append(size_type __n, _CharT __c)
1388 { return _M_replace_aux(this->size(), size_type(0), __n, __c); }
1389
1390#if __cplusplus >= 201103L
1391 /**
1392 * @brief Append an initializer_list of characters.
1393 * @param __l The initializer_list of characters to append.
1394 * @return Reference to this string.
1395 */
b96e2ff9 1396 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1397 basic_string&
1398 append(initializer_list<_CharT> __l)
1399 { return this->append(__l.begin(), __l.size()); }
1400#endif // C++11
1401
1402 /**
1403 * @brief Append a range of characters.
1404 * @param __first Iterator referencing the first character to append.
1405 * @param __last Iterator marking the end of the range.
1406 * @return Reference to this string.
1407 *
1408 * Appends characters in the range [__first,__last) to this string.
1409 */
1410#if __cplusplus >= 201103L
1411 template<class _InputIterator,
1412 typename = std::_RequireInputIter<_InputIterator>>
b96e2ff9 1413 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1414#else
1415 template<class _InputIterator>
1416#endif
1417 basic_string&
1418 append(_InputIterator __first, _InputIterator __last)
1419 { return this->replace(end(), end(), __first, __last); }
1420
1a289fa3 1421#if __cplusplus >= 201703L
ca8f2cb1
VV
1422 /**
1423 * @brief Append a string_view.
df66af3b 1424 * @param __svt An object convertible to string_view to be appended.
ca8f2cb1
VV
1425 * @return Reference to this string.
1426 */
df66af3b 1427 template<typename _Tp>
b96e2ff9 1428 _GLIBCXX20_CONSTEXPR
df66af3b
DK
1429 _If_sv<_Tp, basic_string&>
1430 append(const _Tp& __svt)
1431 {
1432 __sv_type __sv = __svt;
1433 return this->append(__sv.data(), __sv.size());
1434 }
ca8f2cb1
VV
1435
1436 /**
1437 * @brief Append a range of characters from a string_view.
df66af3b 1438 * @param __svt An object convertible to string_view to be appended from.
ca8f2cb1
VV
1439 * @param __pos The position in the string_view to append from.
1440 * @param __n The number of characters to append from the string_view.
1441 * @return Reference to this string.
1442 */
df66af3b 1443 template<typename _Tp>
b96e2ff9 1444 _GLIBCXX20_CONSTEXPR
df66af3b 1445 _If_sv<_Tp, basic_string&>
657213f7
JW
1446 append(const _Tp& __svt, size_type __pos, size_type __n = npos)
1447 {
1448 __sv_type __sv = __svt;
1449 return _M_append(__sv.data()
fb8b3e29
JW
1450 + std::__sv_check(__sv.size(), __pos, "basic_string::append"),
1451 std::__sv_limit(__sv.size(), __pos, __n));
657213f7 1452 }
ca8f2cb1
VV
1453#endif // C++17
1454
34a2b755
JW
1455 /**
1456 * @brief Append a single character.
1457 * @param __c Character to append.
1458 */
b96e2ff9 1459 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1460 void
1461 push_back(_CharT __c)
1462 {
1463 const size_type __size = this->size();
1464 if (__size + 1 > this->capacity())
1465 this->_M_mutate(__size, size_type(0), 0, size_type(1));
1466 traits_type::assign(this->_M_data()[__size], __c);
1467 this->_M_set_length(__size + 1);
1468 }
1469
1470 /**
1471 * @brief Set value to contents of another string.
1472 * @param __str Source string to use.
1473 * @return Reference to this string.
1474 */
b96e2ff9 1475 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1476 basic_string&
1477 assign(const basic_string& __str)
1478 {
db33efde
NDR
1479#if __cplusplus >= 201103L
1480 if (_Alloc_traits::_S_propagate_on_copy_assign())
1481 {
1482 if (!_Alloc_traits::_S_always_equal() && !_M_is_local()
1483 && _M_get_allocator() != __str._M_get_allocator())
1484 {
1485 // Propagating allocator cannot free existing storage so must
1486 // deallocate it before replacing current allocator.
1487 if (__str.size() <= _S_local_capacity)
1488 {
1489 _M_destroy(_M_allocated_capacity);
1490 _M_data(_M_local_data());
1491 _M_set_length(0);
1492 }
1493 else
1494 {
1495 const auto __len = __str.size();
1496 auto __alloc = __str._M_get_allocator();
1497 // If this allocation throws there are no effects:
1498 auto __ptr = _Alloc_traits::allocate(__alloc, __len + 1);
1499 _M_destroy(_M_allocated_capacity);
1500 _M_data(__ptr);
1501 _M_capacity(__len);
1502 _M_set_length(__len);
1503 }
1504 }
1505 std::__alloc_on_copy(_M_get_allocator(), __str._M_get_allocator());
1506 }
1507#endif
34a2b755
JW
1508 this->_M_assign(__str);
1509 return *this;
1510 }
1511
1512#if __cplusplus >= 201103L
1513 /**
1514 * @brief Set value to contents of another string.
1515 * @param __str Source string to use.
1516 * @return Reference to this string.
1517 *
1518 * This function sets this string to the exact contents of @a __str.
1519 * @a __str is a valid, but unspecified string.
1520 */
b96e2ff9 1521 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1522 basic_string&
1523 assign(basic_string&& __str)
5caff414 1524 noexcept(_Alloc_traits::_S_nothrow_move())
34a2b755
JW
1525 {
1526 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1527 // 2063. Contradictory requirements for string move assignment
1528 return *this = std::move(__str);
1529 }
1530#endif // C++11
1531
1532 /**
1533 * @brief Set value to a substring of a string.
1534 * @param __str The string to use.
1535 * @param __pos Index of the first character of str.
1536 * @param __n Number of characters to use.
1537 * @return Reference to this string.
1538 * @throw std::out_of_range if @a pos is not a valid index.
1539 *
1540 * This function sets this string to the substring of @a __str
1541 * consisting of @a __n characters at @a __pos. If @a __n is
1542 * is larger than the number of available characters in @a
1543 * __str, the remainder of @a __str is used.
1544 */
b96e2ff9 1545 _GLIBCXX20_CONSTEXPR
34a2b755 1546 basic_string&
852ee53c 1547 assign(const basic_string& __str, size_type __pos, size_type __n = npos)
2d60da10 1548 { return _M_replace(size_type(0), this->size(), __str._M_data()
34a2b755
JW
1549 + __str._M_check(__pos, "basic_string::assign"),
1550 __str._M_limit(__pos, __n)); }
1551
1552 /**
1553 * @brief Set value to a C substring.
1554 * @param __s The C string to use.
1555 * @param __n Number of characters to use.
1556 * @return Reference to this string.
1557 *
1558 * This function sets the value of this string to the first @a __n
1559 * characters of @a __s. If @a __n is is larger than the number of
1560 * available characters in @a __s, the remainder of @a __s is used.
1561 */
b96e2ff9 1562 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1563 basic_string&
1564 assign(const _CharT* __s, size_type __n)
1565 {
1566 __glibcxx_requires_string_len(__s, __n);
1567 return _M_replace(size_type(0), this->size(), __s, __n);
1568 }
1569
1570 /**
1571 * @brief Set value to contents of a C string.
1572 * @param __s The C string to use.
1573 * @return Reference to this string.
1574 *
1575 * This function sets the value of this string to the value of @a __s.
1576 * The data is copied, so there is no dependence on @a __s once the
1577 * function returns.
1578 */
b96e2ff9 1579 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1580 basic_string&
1581 assign(const _CharT* __s)
1582 {
1583 __glibcxx_requires_string(__s);
1584 return _M_replace(size_type(0), this->size(), __s,
1585 traits_type::length(__s));
1586 }
1587
1588 /**
1589 * @brief Set value to multiple characters.
1590 * @param __n Length of the resulting string.
1591 * @param __c The character to use.
1592 * @return Reference to this string.
1593 *
1594 * This function sets the value of this string to @a __n copies of
1595 * character @a __c.
1596 */
b96e2ff9 1597 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1598 basic_string&
1599 assign(size_type __n, _CharT __c)
1600 { return _M_replace_aux(size_type(0), this->size(), __n, __c); }
1601
1602 /**
1603 * @brief Set value to a range of characters.
1604 * @param __first Iterator referencing the first character to append.
1605 * @param __last Iterator marking the end of the range.
1606 * @return Reference to this string.
1607 *
1608 * Sets value of string to characters in the range [__first,__last).
1609 */
1610#if __cplusplus >= 201103L
1611 template<class _InputIterator,
1612 typename = std::_RequireInputIter<_InputIterator>>
b96e2ff9 1613 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1614#else
1615 template<class _InputIterator>
1616#endif
1617 basic_string&
1618 assign(_InputIterator __first, _InputIterator __last)
1619 { return this->replace(begin(), end(), __first, __last); }
1620
1621#if __cplusplus >= 201103L
1622 /**
1623 * @brief Set value to an initializer_list of characters.
1624 * @param __l The initializer_list of characters to assign.
1625 * @return Reference to this string.
1626 */
b96e2ff9 1627 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1628 basic_string&
1629 assign(initializer_list<_CharT> __l)
1630 { return this->assign(__l.begin(), __l.size()); }
1631#endif // C++11
1632
1a289fa3 1633#if __cplusplus >= 201703L
ca8f2cb1
VV
1634 /**
1635 * @brief Set value from a string_view.
df66af3b 1636 * @param __svt The source object convertible to string_view.
ca8f2cb1
VV
1637 * @return Reference to this string.
1638 */
df66af3b 1639 template<typename _Tp>
b96e2ff9 1640 _GLIBCXX20_CONSTEXPR
df66af3b
DK
1641 _If_sv<_Tp, basic_string&>
1642 assign(const _Tp& __svt)
1643 {
1644 __sv_type __sv = __svt;
1645 return this->assign(__sv.data(), __sv.size());
1646 }
ca8f2cb1
VV
1647
1648 /**
1649 * @brief Set value from a range of characters in a string_view.
df66af3b 1650 * @param __svt The source object convertible to string_view.
ca8f2cb1
VV
1651 * @param __pos The position in the string_view to assign from.
1652 * @param __n The number of characters to assign.
1653 * @return Reference to this string.
1654 */
df66af3b 1655 template<typename _Tp>
b96e2ff9 1656 _GLIBCXX20_CONSTEXPR
657213f7
JW
1657 _If_sv<_Tp, basic_string&>
1658 assign(const _Tp& __svt, size_type __pos, size_type __n = npos)
1659 {
1660 __sv_type __sv = __svt;
fb8b3e29
JW
1661 return _M_replace(size_type(0), this->size(),
1662 __sv.data()
1663 + std::__sv_check(__sv.size(), __pos, "basic_string::assign"),
1664 std::__sv_limit(__sv.size(), __pos, __n));
657213f7 1665 }
ca8f2cb1
VV
1666#endif // C++17
1667
34a2b755
JW
1668#if __cplusplus >= 201103L
1669 /**
1670 * @brief Insert multiple characters.
1671 * @param __p Const_iterator referencing location in string to
1672 * insert at.
1673 * @param __n Number of characters to insert
1674 * @param __c The character to insert.
1675 * @return Iterator referencing the first inserted char.
1676 * @throw std::length_error If new length exceeds @c max_size().
1677 *
1678 * Inserts @a __n copies of character @a __c starting at the
1679 * position referenced by iterator @a __p. If adding
1680 * characters causes the length to exceed max_size(),
1681 * length_error is thrown. The value of the string doesn't
1682 * change if an error is thrown.
1683 */
b96e2ff9 1684 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1685 iterator
1686 insert(const_iterator __p, size_type __n, _CharT __c)
1687 {
1688 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1689 const size_type __pos = __p - begin();
1690 this->replace(__p, __p, __n, __c);
1691 return iterator(this->_M_data() + __pos);
1692 }
1693#else
1694 /**
1695 * @brief Insert multiple characters.
1696 * @param __p Iterator referencing location in string to insert at.
1697 * @param __n Number of characters to insert
1698 * @param __c The character to insert.
1699 * @throw std::length_error If new length exceeds @c max_size().
1700 *
1701 * Inserts @a __n copies of character @a __c starting at the
1702 * position referenced by iterator @a __p. If adding
1703 * characters causes the length to exceed max_size(),
1704 * length_error is thrown. The value of the string doesn't
1705 * change if an error is thrown.
1706 */
1707 void
1708 insert(iterator __p, size_type __n, _CharT __c)
1709 { this->replace(__p, __p, __n, __c); }
1710#endif
1711
1712#if __cplusplus >= 201103L
1713 /**
1714 * @brief Insert a range of characters.
1715 * @param __p Const_iterator referencing location in string to
1716 * insert at.
1717 * @param __beg Start of range.
1718 * @param __end End of range.
1719 * @return Iterator referencing the first inserted char.
1720 * @throw std::length_error If new length exceeds @c max_size().
1721 *
1722 * Inserts characters in range [beg,end). If adding characters
1723 * causes the length to exceed max_size(), length_error is
1724 * thrown. The value of the string doesn't change if an error
1725 * is thrown.
1726 */
1727 template<class _InputIterator,
1728 typename = std::_RequireInputIter<_InputIterator>>
b96e2ff9 1729 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1730 iterator
1731 insert(const_iterator __p, _InputIterator __beg, _InputIterator __end)
1732 {
1733 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1734 const size_type __pos = __p - begin();
1735 this->replace(__p, __p, __beg, __end);
1736 return iterator(this->_M_data() + __pos);
1737 }
1738#else
1739 /**
1740 * @brief Insert a range of characters.
1741 * @param __p Iterator referencing location in string to insert at.
1742 * @param __beg Start of range.
1743 * @param __end End of range.
1744 * @throw std::length_error If new length exceeds @c max_size().
1745 *
1746 * Inserts characters in range [__beg,__end). If adding
1747 * characters causes the length to exceed max_size(),
1748 * length_error is thrown. The value of the string doesn't
1749 * change if an error is thrown.
1750 */
1751 template<class _InputIterator>
1752 void
1753 insert(iterator __p, _InputIterator __beg, _InputIterator __end)
1754 { this->replace(__p, __p, __beg, __end); }
1755#endif
1756
1757#if __cplusplus >= 201103L
1758 /**
1759 * @brief Insert an initializer_list of characters.
1760 * @param __p Iterator referencing location in string to insert at.
1761 * @param __l The initializer_list of characters to insert.
1762 * @throw std::length_error If new length exceeds @c max_size().
1763 */
b96e2ff9 1764 _GLIBCXX20_CONSTEXPR
cda121ac
JW
1765 iterator
1766 insert(const_iterator __p, initializer_list<_CharT> __l)
1767 { return this->insert(__p, __l.begin(), __l.end()); }
1768
1769#ifdef _GLIBCXX_DEFINING_STRING_INSTANTIATIONS
1770 // See PR libstdc++/83328
34a2b755
JW
1771 void
1772 insert(iterator __p, initializer_list<_CharT> __l)
1773 {
1774 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1775 this->insert(__p - begin(), __l.begin(), __l.size());
1776 }
cda121ac 1777#endif
34a2b755
JW
1778#endif // C++11
1779
1780 /**
1781 * @brief Insert value of a string.
8c6a71e4 1782 * @param __pos1 Position in string to insert at.
34a2b755
JW
1783 * @param __str The string to insert.
1784 * @return Reference to this string.
1785 * @throw std::length_error If new length exceeds @c max_size().
1786 *
1787 * Inserts value of @a __str starting at @a __pos1. If adding
1788 * characters causes the length to exceed max_size(),
1789 * length_error is thrown. The value of the string doesn't
1790 * change if an error is thrown.
1791 */
b96e2ff9 1792 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1793 basic_string&
1794 insert(size_type __pos1, const basic_string& __str)
1795 { return this->replace(__pos1, size_type(0),
2d60da10 1796 __str._M_data(), __str.size()); }
34a2b755
JW
1797
1798 /**
1799 * @brief Insert a substring.
8c6a71e4
JW
1800 * @param __pos1 Position in string to insert at.
1801 * @param __str The string to insert.
34a2b755
JW
1802 * @param __pos2 Start of characters in str to insert.
1803 * @param __n Number of characters to insert.
1804 * @return Reference to this string.
1805 * @throw std::length_error If new length exceeds @c max_size().
1806 * @throw std::out_of_range If @a pos1 > size() or
1807 * @a __pos2 > @a str.size().
1808 *
1809 * Starting at @a pos1, insert @a __n character of @a __str
1810 * beginning with @a __pos2. If adding characters causes the
1811 * length to exceed max_size(), length_error is thrown. If @a
1812 * __pos1 is beyond the end of this string or @a __pos2 is
1813 * beyond the end of @a __str, out_of_range is thrown. The
1814 * value of the string doesn't change if an error is thrown.
1815 */
b96e2ff9 1816 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1817 basic_string&
1818 insert(size_type __pos1, const basic_string& __str,
852ee53c 1819 size_type __pos2, size_type __n = npos)
2d60da10 1820 { return this->replace(__pos1, size_type(0), __str._M_data()
34a2b755
JW
1821 + __str._M_check(__pos2, "basic_string::insert"),
1822 __str._M_limit(__pos2, __n)); }
1823
1824 /**
1825 * @brief Insert a C substring.
8c6a71e4 1826 * @param __pos Position in string to insert at.
34a2b755
JW
1827 * @param __s The C string to insert.
1828 * @param __n The number of characters to insert.
1829 * @return Reference to this string.
1830 * @throw std::length_error If new length exceeds @c max_size().
1831 * @throw std::out_of_range If @a __pos is beyond the end of this
1832 * string.
1833 *
1834 * Inserts the first @a __n characters of @a __s starting at @a
1835 * __pos. If adding characters causes the length to exceed
1836 * max_size(), length_error is thrown. If @a __pos is beyond
1837 * end(), out_of_range is thrown. The value of the string
1838 * doesn't change if an error is thrown.
1839 */
b96e2ff9 1840 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1841 basic_string&
1842 insert(size_type __pos, const _CharT* __s, size_type __n)
1843 { return this->replace(__pos, size_type(0), __s, __n); }
1844
1845 /**
1846 * @brief Insert a C string.
8c6a71e4 1847 * @param __pos Position in string to insert at.
34a2b755
JW
1848 * @param __s The C string to insert.
1849 * @return Reference to this string.
1850 * @throw std::length_error If new length exceeds @c max_size().
1851 * @throw std::out_of_range If @a pos is beyond the end of this
1852 * string.
1853 *
1854 * Inserts the first @a n characters of @a __s starting at @a __pos. If
1855 * adding characters causes the length to exceed max_size(),
1856 * length_error is thrown. If @a __pos is beyond end(), out_of_range is
1857 * thrown. The value of the string doesn't change if an error is
1858 * thrown.
1859 */
b96e2ff9 1860 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1861 basic_string&
1862 insert(size_type __pos, const _CharT* __s)
1863 {
1864 __glibcxx_requires_string(__s);
1865 return this->replace(__pos, size_type(0), __s,
1866 traits_type::length(__s));
1867 }
1868
1869 /**
1870 * @brief Insert multiple characters.
1871 * @param __pos Index in string to insert at.
1872 * @param __n Number of characters to insert
1873 * @param __c The character to insert.
1874 * @return Reference to this string.
1875 * @throw std::length_error If new length exceeds @c max_size().
1876 * @throw std::out_of_range If @a __pos is beyond the end of this
1877 * string.
1878 *
1879 * Inserts @a __n copies of character @a __c starting at index
1880 * @a __pos. If adding characters causes the length to exceed
1881 * max_size(), length_error is thrown. If @a __pos > length(),
1882 * out_of_range is thrown. The value of the string doesn't
1883 * change if an error is thrown.
1884 */
b96e2ff9 1885 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1886 basic_string&
1887 insert(size_type __pos, size_type __n, _CharT __c)
1888 { return _M_replace_aux(_M_check(__pos, "basic_string::insert"),
1889 size_type(0), __n, __c); }
1890
1891 /**
1892 * @brief Insert one character.
1893 * @param __p Iterator referencing position in string to insert at.
1894 * @param __c The character to insert.
1895 * @return Iterator referencing newly inserted char.
1896 * @throw std::length_error If new length exceeds @c max_size().
1897 *
1898 * Inserts character @a __c at position referenced by @a __p.
1899 * If adding character causes the length to exceed max_size(),
1900 * length_error is thrown. If @a __p is beyond end of string,
1901 * out_of_range is thrown. The value of the string doesn't
1902 * change if an error is thrown.
1903 */
b96e2ff9 1904 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1905 iterator
1906 insert(__const_iterator __p, _CharT __c)
1907 {
1908 _GLIBCXX_DEBUG_PEDASSERT(__p >= begin() && __p <= end());
1909 const size_type __pos = __p - begin();
1910 _M_replace_aux(__pos, size_type(0), size_type(1), __c);
1911 return iterator(_M_data() + __pos);
1912 }
1913
1a289fa3 1914#if __cplusplus >= 201703L
ca8f2cb1
VV
1915 /**
1916 * @brief Insert a string_view.
8c6a71e4 1917 * @param __pos Position in string to insert at.
df66af3b 1918 * @param __svt The object convertible to string_view to insert.
ca8f2cb1
VV
1919 * @return Reference to this string.
1920 */
df66af3b 1921 template<typename _Tp>
b96e2ff9 1922 _GLIBCXX20_CONSTEXPR
df66af3b
DK
1923 _If_sv<_Tp, basic_string&>
1924 insert(size_type __pos, const _Tp& __svt)
1925 {
1926 __sv_type __sv = __svt;
1927 return this->insert(__pos, __sv.data(), __sv.size());
1928 }
ca8f2cb1
VV
1929
1930 /**
1931 * @brief Insert a string_view.
8c6a71e4
JW
1932 * @param __pos1 Position in string to insert at.
1933 * @param __svt The object convertible to string_view to insert from.
1934 * @param __pos2 Start of characters in str to insert.
ca8f2cb1
VV
1935 * @param __n The number of characters to insert.
1936 * @return Reference to this string.
1937 */
df66af3b 1938 template<typename _Tp>
b96e2ff9 1939 _GLIBCXX20_CONSTEXPR
657213f7
JW
1940 _If_sv<_Tp, basic_string&>
1941 insert(size_type __pos1, const _Tp& __svt,
1942 size_type __pos2, size_type __n = npos)
1943 {
1944 __sv_type __sv = __svt;
fb8b3e29
JW
1945 return this->replace(__pos1, size_type(0),
1946 __sv.data()
1947 + std::__sv_check(__sv.size(), __pos2, "basic_string::insert"),
1948 std::__sv_limit(__sv.size(), __pos2, __n));
657213f7 1949 }
ca8f2cb1
VV
1950#endif // C++17
1951
34a2b755
JW
1952 /**
1953 * @brief Remove characters.
1954 * @param __pos Index of first character to remove (default 0).
1955 * @param __n Number of characters to remove (default remainder).
1956 * @return Reference to this string.
1957 * @throw std::out_of_range If @a pos is beyond the end of this
1958 * string.
1959 *
1960 * Removes @a __n characters from this string starting at @a
1961 * __pos. The length of the string is reduced by @a __n. If
1962 * there are < @a __n characters to remove, the remainder of
1963 * the string is truncated. If @a __p is beyond end of string,
1964 * out_of_range is thrown. The value of the string doesn't
1965 * change if an error is thrown.
1966 */
b96e2ff9 1967 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1968 basic_string&
1969 erase(size_type __pos = 0, size_type __n = npos)
1970 {
a922c5ff
JW
1971 _M_check(__pos, "basic_string::erase");
1972 if (__n == npos)
1973 this->_M_set_length(__pos);
1974 else if (__n != 0)
1975 this->_M_erase(__pos, _M_limit(__pos, __n));
34a2b755
JW
1976 return *this;
1977 }
1978
1979 /**
1980 * @brief Remove one character.
1981 * @param __position Iterator referencing the character to remove.
1982 * @return iterator referencing same location after removal.
1983 *
1984 * Removes the character at @a __position from this string. The value
1985 * of the string doesn't change if an error is thrown.
1986 */
b96e2ff9 1987 _GLIBCXX20_CONSTEXPR
34a2b755
JW
1988 iterator
1989 erase(__const_iterator __position)
1990 {
1991 _GLIBCXX_DEBUG_PEDASSERT(__position >= begin()
1992 && __position < end());
1993 const size_type __pos = __position - begin();
1994 this->_M_erase(__pos, size_type(1));
1995 return iterator(_M_data() + __pos);
1996 }
1997
1998 /**
1999 * @brief Remove a range of characters.
2000 * @param __first Iterator referencing the first character to remove.
2001 * @param __last Iterator referencing the end of the range.
2002 * @return Iterator referencing location of first after removal.
2003 *
2004 * Removes the characters in the range [first,last) from this string.
2005 * The value of the string doesn't change if an error is thrown.
2006 */
b96e2ff9 2007 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2008 iterator
2009 erase(__const_iterator __first, __const_iterator __last)
2010 {
2011 _GLIBCXX_DEBUG_PEDASSERT(__first >= begin() && __first <= __last
2012 && __last <= end());
2013 const size_type __pos = __first - begin();
a922c5ff
JW
2014 if (__last == end())
2015 this->_M_set_length(__pos);
2016 else
2017 this->_M_erase(__pos, __last - __first);
34a2b755
JW
2018 return iterator(this->_M_data() + __pos);
2019 }
2020
2021#if __cplusplus >= 201103L
2022 /**
2023 * @brief Remove the last character.
2024 *
2025 * The string must be non-empty.
2026 */
b96e2ff9 2027 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2028 void
2029 pop_back() noexcept
e25d2617 2030 {
2f1e8e7c 2031 __glibcxx_assert(!empty());
e25d2617
FD
2032 _M_erase(size() - 1, 1);
2033 }
34a2b755
JW
2034#endif // C++11
2035
2036 /**
2037 * @brief Replace characters with value from another string.
2038 * @param __pos Index of first character to replace.
2039 * @param __n Number of characters to be replaced.
2040 * @param __str String to insert.
2041 * @return Reference to this string.
2042 * @throw std::out_of_range If @a pos is beyond the end of this
2043 * string.
2044 * @throw std::length_error If new length exceeds @c max_size().
2045 *
2046 * Removes the characters in the range [__pos,__pos+__n) from
2047 * this string. In place, the value of @a __str is inserted.
2048 * If @a __pos is beyond end of string, out_of_range is thrown.
2049 * If the length of the result exceeds max_size(), length_error
2050 * is thrown. The value of the string doesn't change if an
2051 * error is thrown.
2052 */
b96e2ff9 2053 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2054 basic_string&
2055 replace(size_type __pos, size_type __n, const basic_string& __str)
2d60da10 2056 { return this->replace(__pos, __n, __str._M_data(), __str.size()); }
34a2b755
JW
2057
2058 /**
2059 * @brief Replace characters with value from another string.
2060 * @param __pos1 Index of first character to replace.
2061 * @param __n1 Number of characters to be replaced.
2062 * @param __str String to insert.
2063 * @param __pos2 Index of first character of str to use.
2064 * @param __n2 Number of characters from str to use.
2065 * @return Reference to this string.
2066 * @throw std::out_of_range If @a __pos1 > size() or @a __pos2 >
2067 * __str.size().
2068 * @throw std::length_error If new length exceeds @c max_size().
2069 *
2070 * Removes the characters in the range [__pos1,__pos1 + n) from this
2071 * string. In place, the value of @a __str is inserted. If @a __pos is
2072 * beyond end of string, out_of_range is thrown. If the length of the
2073 * result exceeds max_size(), length_error is thrown. The value of the
2074 * string doesn't change if an error is thrown.
2075 */
b96e2ff9 2076 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2077 basic_string&
2078 replace(size_type __pos1, size_type __n1, const basic_string& __str,
852ee53c 2079 size_type __pos2, size_type __n2 = npos)
2d60da10 2080 { return this->replace(__pos1, __n1, __str._M_data()
34a2b755
JW
2081 + __str._M_check(__pos2, "basic_string::replace"),
2082 __str._M_limit(__pos2, __n2)); }
2083
2084 /**
2085 * @brief Replace characters with value of a C substring.
2086 * @param __pos Index of first character to replace.
2087 * @param __n1 Number of characters to be replaced.
2088 * @param __s C string to insert.
2089 * @param __n2 Number of characters from @a s to use.
2090 * @return Reference to this string.
2091 * @throw std::out_of_range If @a pos1 > size().
2092 * @throw std::length_error If new length exceeds @c max_size().
2093 *
2094 * Removes the characters in the range [__pos,__pos + __n1)
2095 * from this string. In place, the first @a __n2 characters of
2096 * @a __s are inserted, or all of @a __s if @a __n2 is too large. If
2097 * @a __pos is beyond end of string, out_of_range is thrown. If
2098 * the length of result exceeds max_size(), length_error is
2099 * thrown. The value of the string doesn't change if an error
2100 * is thrown.
2101 */
b96e2ff9 2102 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2103 basic_string&
2104 replace(size_type __pos, size_type __n1, const _CharT* __s,
2105 size_type __n2)
2106 {
2107 __glibcxx_requires_string_len(__s, __n2);
2108 return _M_replace(_M_check(__pos, "basic_string::replace"),
2109 _M_limit(__pos, __n1), __s, __n2);
2110 }
2111
2112 /**
2113 * @brief Replace characters with value of a C string.
2114 * @param __pos Index of first character to replace.
2115 * @param __n1 Number of characters to be replaced.
2116 * @param __s C string to insert.
2117 * @return Reference to this string.
2118 * @throw std::out_of_range If @a pos > size().
2119 * @throw std::length_error If new length exceeds @c max_size().
2120 *
2121 * Removes the characters in the range [__pos,__pos + __n1)
2122 * from this string. In place, the characters of @a __s are
2123 * inserted. If @a __pos is beyond end of string, out_of_range
2124 * is thrown. If the length of result exceeds max_size(),
2125 * length_error is thrown. The value of the string doesn't
2126 * change if an error is thrown.
2127 */
b96e2ff9 2128 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2129 basic_string&
2130 replace(size_type __pos, size_type __n1, const _CharT* __s)
2131 {
2132 __glibcxx_requires_string(__s);
2133 return this->replace(__pos, __n1, __s, traits_type::length(__s));
2134 }
2135
2136 /**
2137 * @brief Replace characters with multiple characters.
2138 * @param __pos Index of first character to replace.
2139 * @param __n1 Number of characters to be replaced.
2140 * @param __n2 Number of characters to insert.
2141 * @param __c Character to insert.
2142 * @return Reference to this string.
2143 * @throw std::out_of_range If @a __pos > size().
2144 * @throw std::length_error If new length exceeds @c max_size().
2145 *
2146 * Removes the characters in the range [pos,pos + n1) from this
2147 * string. In place, @a __n2 copies of @a __c are inserted.
2148 * If @a __pos is beyond end of string, out_of_range is thrown.
2149 * If the length of result exceeds max_size(), length_error is
2150 * thrown. The value of the string doesn't change if an error
2151 * is thrown.
2152 */
b96e2ff9 2153 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2154 basic_string&
2155 replace(size_type __pos, size_type __n1, size_type __n2, _CharT __c)
2156 { return _M_replace_aux(_M_check(__pos, "basic_string::replace"),
2157 _M_limit(__pos, __n1), __n2, __c); }
2158
2159 /**
2160 * @brief Replace range of characters with string.
2161 * @param __i1 Iterator referencing start of range to replace.
2162 * @param __i2 Iterator referencing end of range to replace.
2163 * @param __str String value to insert.
2164 * @return Reference to this string.
2165 * @throw std::length_error If new length exceeds @c max_size().
2166 *
2167 * Removes the characters in the range [__i1,__i2). In place,
2168 * the value of @a __str is inserted. If the length of result
2169 * exceeds max_size(), length_error is thrown. The value of
2170 * the string doesn't change if an error is thrown.
2171 */
b96e2ff9 2172 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2173 basic_string&
2174 replace(__const_iterator __i1, __const_iterator __i2,
2175 const basic_string& __str)
2d60da10 2176 { return this->replace(__i1, __i2, __str._M_data(), __str.size()); }
34a2b755
JW
2177
2178 /**
2179 * @brief Replace range of characters with C substring.
2180 * @param __i1 Iterator referencing start of range to replace.
2181 * @param __i2 Iterator referencing end of range to replace.
2182 * @param __s C string value to insert.
2183 * @param __n Number of characters from s to insert.
2184 * @return Reference to this string.
2185 * @throw std::length_error If new length exceeds @c max_size().
2186 *
2187 * Removes the characters in the range [__i1,__i2). In place,
2188 * the first @a __n characters of @a __s are inserted. If the
2189 * length of result exceeds max_size(), length_error is thrown.
2190 * The value of the string doesn't change if an error is
2191 * thrown.
2192 */
b96e2ff9 2193 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2194 basic_string&
2195 replace(__const_iterator __i1, __const_iterator __i2,
2196 const _CharT* __s, size_type __n)
2197 {
2198 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2199 && __i2 <= end());
2200 return this->replace(__i1 - begin(), __i2 - __i1, __s, __n);
2201 }
2202
2203 /**
2204 * @brief Replace range of characters with C string.
2205 * @param __i1 Iterator referencing start of range to replace.
2206 * @param __i2 Iterator referencing end of range to replace.
2207 * @param __s C string value to insert.
2208 * @return Reference to this string.
2209 * @throw std::length_error If new length exceeds @c max_size().
2210 *
2211 * Removes the characters in the range [__i1,__i2). In place,
2212 * the characters of @a __s are inserted. If the length of
2213 * result exceeds max_size(), length_error is thrown. The
2214 * value of the string doesn't change if an error is thrown.
2215 */
b96e2ff9 2216 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2217 basic_string&
2218 replace(__const_iterator __i1, __const_iterator __i2, const _CharT* __s)
2219 {
2220 __glibcxx_requires_string(__s);
2221 return this->replace(__i1, __i2, __s, traits_type::length(__s));
2222 }
2223
2224 /**
2225 * @brief Replace range of characters with multiple characters
2226 * @param __i1 Iterator referencing start of range to replace.
2227 * @param __i2 Iterator referencing end of range to replace.
2228 * @param __n Number of characters to insert.
2229 * @param __c Character to insert.
2230 * @return Reference to this string.
2231 * @throw std::length_error If new length exceeds @c max_size().
2232 *
2233 * Removes the characters in the range [__i1,__i2). In place,
2234 * @a __n copies of @a __c are inserted. If the length of
2235 * result exceeds max_size(), length_error is thrown. The
2236 * value of the string doesn't change if an error is thrown.
2237 */
b96e2ff9 2238 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2239 basic_string&
2240 replace(__const_iterator __i1, __const_iterator __i2, size_type __n,
2241 _CharT __c)
2242 {
2243 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2244 && __i2 <= end());
2245 return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __c);
2246 }
2247
2248 /**
2249 * @brief Replace range of characters with range.
2250 * @param __i1 Iterator referencing start of range to replace.
2251 * @param __i2 Iterator referencing end of range to replace.
2252 * @param __k1 Iterator referencing start of range to insert.
2253 * @param __k2 Iterator referencing end of range to insert.
2254 * @return Reference to this string.
2255 * @throw std::length_error If new length exceeds @c max_size().
2256 *
2257 * Removes the characters in the range [__i1,__i2). In place,
2258 * characters in the range [__k1,__k2) are inserted. If the
2259 * length of result exceeds max_size(), length_error is thrown.
2260 * The value of the string doesn't change if an error is
2261 * thrown.
2262 */
2263#if __cplusplus >= 201103L
2264 template<class _InputIterator,
2265 typename = std::_RequireInputIter<_InputIterator>>
b96e2ff9 2266 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2267 basic_string&
2268 replace(const_iterator __i1, const_iterator __i2,
2269 _InputIterator __k1, _InputIterator __k2)
2270 {
2271 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2272 && __i2 <= end());
2273 __glibcxx_requires_valid_range(__k1, __k2);
2274 return this->_M_replace_dispatch(__i1, __i2, __k1, __k2,
2275 std::__false_type());
2276 }
2277#else
2278 template<class _InputIterator>
2279#ifdef _GLIBCXX_DISAMBIGUATE_REPLACE_INST
2280 typename __enable_if_not_native_iterator<_InputIterator>::__type
2281#else
2282 basic_string&
2283#endif
2284 replace(iterator __i1, iterator __i2,
2285 _InputIterator __k1, _InputIterator __k2)
2286 {
2287 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2288 && __i2 <= end());
2289 __glibcxx_requires_valid_range(__k1, __k2);
2290 typedef typename std::__is_integer<_InputIterator>::__type _Integral;
2291 return _M_replace_dispatch(__i1, __i2, __k1, __k2, _Integral());
2292 }
2293#endif
2294
2295 // Specializations for the common case of pointer and iterator:
2296 // useful to avoid the overhead of temporary buffering in _M_replace.
b96e2ff9 2297 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2298 basic_string&
2299 replace(__const_iterator __i1, __const_iterator __i2,
2300 _CharT* __k1, _CharT* __k2)
2301 {
2302 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2303 && __i2 <= end());
2304 __glibcxx_requires_valid_range(__k1, __k2);
2305 return this->replace(__i1 - begin(), __i2 - __i1,
2306 __k1, __k2 - __k1);
2307 }
2308
b96e2ff9 2309 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2310 basic_string&
2311 replace(__const_iterator __i1, __const_iterator __i2,
2312 const _CharT* __k1, const _CharT* __k2)
2313 {
2314 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2315 && __i2 <= end());
2316 __glibcxx_requires_valid_range(__k1, __k2);
2317 return this->replace(__i1 - begin(), __i2 - __i1,
2318 __k1, __k2 - __k1);
2319 }
2320
b96e2ff9 2321 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2322 basic_string&
2323 replace(__const_iterator __i1, __const_iterator __i2,
2324 iterator __k1, iterator __k2)
2325 {
2326 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2327 && __i2 <= end());
2328 __glibcxx_requires_valid_range(__k1, __k2);
2329 return this->replace(__i1 - begin(), __i2 - __i1,
2330 __k1.base(), __k2 - __k1);
2331 }
2332
b96e2ff9 2333 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2334 basic_string&
2335 replace(__const_iterator __i1, __const_iterator __i2,
2336 const_iterator __k1, const_iterator __k2)
2337 {
2338 _GLIBCXX_DEBUG_PEDASSERT(begin() <= __i1 && __i1 <= __i2
2339 && __i2 <= end());
2340 __glibcxx_requires_valid_range(__k1, __k2);
2341 return this->replace(__i1 - begin(), __i2 - __i1,
2342 __k1.base(), __k2 - __k1);
2343 }
2344
2345#if __cplusplus >= 201103L
2346 /**
2347 * @brief Replace range of characters with initializer_list.
2348 * @param __i1 Iterator referencing start of range to replace.
2349 * @param __i2 Iterator referencing end of range to replace.
2350 * @param __l The initializer_list of characters to insert.
2351 * @return Reference to this string.
2352 * @throw std::length_error If new length exceeds @c max_size().
2353 *
2354 * Removes the characters in the range [__i1,__i2). In place,
2355 * characters in the range [__k1,__k2) are inserted. If the
2356 * length of result exceeds max_size(), length_error is thrown.
2357 * The value of the string doesn't change if an error is
2358 * thrown.
2359 */
b96e2ff9 2360 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2361 basic_string& replace(const_iterator __i1, const_iterator __i2,
2362 initializer_list<_CharT> __l)
17f8dc93 2363 { return this->replace(__i1, __i2, __l.begin(), __l.size()); }
34a2b755
JW
2364#endif // C++11
2365
1a289fa3 2366#if __cplusplus >= 201703L
ca8f2cb1
VV
2367 /**
2368 * @brief Replace range of characters with string_view.
2369 * @param __pos The position to replace at.
2370 * @param __n The number of characters to replace.
df66af3b 2371 * @param __svt The object convertible to string_view to insert.
ca8f2cb1
VV
2372 * @return Reference to this string.
2373 */
df66af3b 2374 template<typename _Tp>
b96e2ff9 2375 _GLIBCXX20_CONSTEXPR
df66af3b
DK
2376 _If_sv<_Tp, basic_string&>
2377 replace(size_type __pos, size_type __n, const _Tp& __svt)
2378 {
2379 __sv_type __sv = __svt;
2380 return this->replace(__pos, __n, __sv.data(), __sv.size());
2381 }
ca8f2cb1
VV
2382
2383 /**
2384 * @brief Replace range of characters with string_view.
2385 * @param __pos1 The position to replace at.
2386 * @param __n1 The number of characters to replace.
df66af3b 2387 * @param __svt The object convertible to string_view to insert from.
ca8f2cb1
VV
2388 * @param __pos2 The position in the string_view to insert from.
2389 * @param __n2 The number of characters to insert.
2390 * @return Reference to this string.
2391 */
df66af3b 2392 template<typename _Tp>
b96e2ff9 2393 _GLIBCXX20_CONSTEXPR
657213f7
JW
2394 _If_sv<_Tp, basic_string&>
2395 replace(size_type __pos1, size_type __n1, const _Tp& __svt,
2396 size_type __pos2, size_type __n2 = npos)
2397 {
2398 __sv_type __sv = __svt;
fb8b3e29
JW
2399 return this->replace(__pos1, __n1,
2400 __sv.data()
2401 + std::__sv_check(__sv.size(), __pos2, "basic_string::replace"),
2402 std::__sv_limit(__sv.size(), __pos2, __n2));
657213f7 2403 }
ca8f2cb1
VV
2404
2405 /**
2406 * @brief Replace range of characters with string_view.
2407 * @param __i1 An iterator referencing the start position
2408 to replace at.
2409 * @param __i2 An iterator referencing the end position
2410 for the replace.
df66af3b 2411 * @param __svt The object convertible to string_view to insert from.
ca8f2cb1
VV
2412 * @return Reference to this string.
2413 */
df66af3b 2414 template<typename _Tp>
b96e2ff9 2415 _GLIBCXX20_CONSTEXPR
df66af3b
DK
2416 _If_sv<_Tp, basic_string&>
2417 replace(const_iterator __i1, const_iterator __i2, const _Tp& __svt)
2418 {
2419 __sv_type __sv = __svt;
2420 return this->replace(__i1 - begin(), __i2 - __i1, __sv);
2421 }
ca8f2cb1
VV
2422#endif // C++17
2423
34a2b755
JW
2424 private:
2425 template<class _Integer>
b96e2ff9 2426 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2427 basic_string&
2428 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
2429 _Integer __n, _Integer __val, __true_type)
2430 { return _M_replace_aux(__i1 - begin(), __i2 - __i1, __n, __val); }
2431
2432 template<class _InputIterator>
b96e2ff9 2433 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2434 basic_string&
2435 _M_replace_dispatch(const_iterator __i1, const_iterator __i2,
2436 _InputIterator __k1, _InputIterator __k2,
2437 __false_type);
2438
b96e2ff9 2439 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2440 basic_string&
2441 _M_replace_aux(size_type __pos1, size_type __n1, size_type __n2,
2442 _CharT __c);
2443
b96e2ff9 2444 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2445 basic_string&
2446 _M_replace(size_type __pos, size_type __len1, const _CharT* __s,
2447 const size_type __len2);
2448
b96e2ff9 2449 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2450 basic_string&
2451 _M_append(const _CharT* __s, size_type __n);
2452
2453 public:
2454
2455 /**
2456 * @brief Copy substring into C string.
2457 * @param __s C string to copy value into.
2458 * @param __n Number of characters to copy.
2459 * @param __pos Index of first character to copy.
2460 * @return Number of characters actually copied
2461 * @throw std::out_of_range If __pos > size().
2462 *
2463 * Copies up to @a __n characters starting at @a __pos into the
2464 * C string @a __s. If @a __pos is %greater than size(),
2465 * out_of_range is thrown.
2466 */
b96e2ff9 2467 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2468 size_type
2469 copy(_CharT* __s, size_type __n, size_type __pos = 0) const;
2470
2471 /**
2472 * @brief Swap contents with another string.
2473 * @param __s String to swap with.
2474 *
2475 * Exchanges the contents of this string with that of @a __s in constant
2476 * time.
2477 */
b96e2ff9 2478 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2479 void
2480 swap(basic_string& __s) _GLIBCXX_NOEXCEPT;
2481
2482 // String operations:
2483 /**
2484 * @brief Return const pointer to null-terminated contents.
2485 *
2486 * This is a handle to internal data. Do not modify or dire things may
2487 * happen.
2488 */
b96e2ff9 2489 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2490 const _CharT*
2491 c_str() const _GLIBCXX_NOEXCEPT
2d60da10 2492 { return _M_data(); }
34a2b755
JW
2493
2494 /**
2495 * @brief Return const pointer to contents.
2496 *
92d58dee
JW
2497 * This is a pointer to internal data. It is undefined to modify
2498 * the contents through the returned pointer. To get a pointer that
2499 * allows modifying the contents use @c &str[0] instead,
2500 * (or in C++17 the non-const @c str.data() overload).
34a2b755 2501 */
b96e2ff9 2502 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2503 const _CharT*
2504 data() const _GLIBCXX_NOEXCEPT
2d60da10 2505 { return _M_data(); }
34a2b755 2506
1a289fa3 2507#if __cplusplus >= 201703L
92d58dee
JW
2508 /**
2509 * @brief Return non-const pointer to contents.
2510 *
2511 * This is a pointer to the character sequence held by the string.
2512 * Modifying the characters in the sequence is allowed.
2513 */
b96e2ff9 2514 _GLIBCXX20_CONSTEXPR
92d58dee
JW
2515 _CharT*
2516 data() noexcept
2517 { return _M_data(); }
2518#endif
2519
34a2b755
JW
2520 /**
2521 * @brief Return copy of allocator used to construct this string.
2522 */
b96e2ff9 2523 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2524 allocator_type
2525 get_allocator() const _GLIBCXX_NOEXCEPT
2526 { return _M_get_allocator(); }
2527
2528 /**
2529 * @brief Find position of a C substring.
2530 * @param __s C string to locate.
2531 * @param __pos Index of character to search from.
2532 * @param __n Number of characters from @a s to search for.
2533 * @return Index of start of first occurrence.
2534 *
2535 * Starting from @a __pos, searches forward for the first @a
2536 * __n characters in @a __s within this string. If found,
2537 * returns the index where it begins. If not found, returns
2538 * npos.
2539 */
b96e2ff9 2540 _GLIBCXX20_CONSTEXPR
34a2b755 2541 size_type
39a03251
JW
2542 find(const _CharT* __s, size_type __pos, size_type __n) const
2543 _GLIBCXX_NOEXCEPT;
34a2b755
JW
2544
2545 /**
2546 * @brief Find position of a string.
2547 * @param __str String to locate.
2548 * @param __pos Index of character to search from (default 0).
2549 * @return Index of start of first occurrence.
2550 *
2551 * Starting from @a __pos, searches forward for value of @a __str within
2552 * this string. If found, returns the index where it begins. If not
2553 * found, returns npos.
2554 */
b96e2ff9 2555 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2556 size_type
2557 find(const basic_string& __str, size_type __pos = 0) const
39a03251 2558 _GLIBCXX_NOEXCEPT
34a2b755
JW
2559 { return this->find(__str.data(), __pos, __str.size()); }
2560
1a289fa3 2561#if __cplusplus >= 201703L
ca8f2cb1
VV
2562 /**
2563 * @brief Find position of a string_view.
df66af3b 2564 * @param __svt The object convertible to string_view to locate.
ca8f2cb1
VV
2565 * @param __pos Index of character to search from (default 0).
2566 * @return Index of start of first occurrence.
2567 */
df66af3b 2568 template<typename _Tp>
b96e2ff9 2569 _GLIBCXX20_CONSTEXPR
df66af3b
DK
2570 _If_sv<_Tp, size_type>
2571 find(const _Tp& __svt, size_type __pos = 0) const
2572 noexcept(is_same<_Tp, __sv_type>::value)
2573 {
2574 __sv_type __sv = __svt;
2575 return this->find(__sv.data(), __pos, __sv.size());
2576 }
ca8f2cb1
VV
2577#endif // C++17
2578
34a2b755
JW
2579 /**
2580 * @brief Find position of a C string.
2581 * @param __s C string to locate.
2582 * @param __pos Index of character to search from (default 0).
2583 * @return Index of start of first occurrence.
2584 *
2585 * Starting from @a __pos, searches forward for the value of @a
2586 * __s within this string. If found, returns the index where
2587 * it begins. If not found, returns npos.
2588 */
b96e2ff9 2589 _GLIBCXX20_CONSTEXPR
34a2b755 2590 size_type
39a03251 2591 find(const _CharT* __s, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
34a2b755
JW
2592 {
2593 __glibcxx_requires_string(__s);
2594 return this->find(__s, __pos, traits_type::length(__s));
2595 }
2596
2597 /**
2598 * @brief Find position of a character.
2599 * @param __c Character to locate.
2600 * @param __pos Index of character to search from (default 0).
2601 * @return Index of first occurrence.
2602 *
2603 * Starting from @a __pos, searches forward for @a __c within
2604 * this string. If found, returns the index where it was
2605 * found. If not found, returns npos.
2606 */
b96e2ff9 2607 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2608 size_type
2609 find(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT;
2610
2611 /**
2612 * @brief Find last position of a string.
2613 * @param __str String to locate.
2614 * @param __pos Index of character to search back from (default end).
2615 * @return Index of start of last occurrence.
2616 *
2617 * Starting from @a __pos, searches backward for value of @a
2618 * __str within this string. If found, returns the index where
2619 * it begins. If not found, returns npos.
2620 */
b96e2ff9 2621 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2622 size_type
2623 rfind(const basic_string& __str, size_type __pos = npos) const
39a03251 2624 _GLIBCXX_NOEXCEPT
34a2b755
JW
2625 { return this->rfind(__str.data(), __pos, __str.size()); }
2626
1a289fa3 2627#if __cplusplus >= 201703L
ca8f2cb1
VV
2628 /**
2629 * @brief Find last position of a string_view.
df66af3b 2630 * @param __svt The object convertible to string_view to locate.
ca8f2cb1
VV
2631 * @param __pos Index of character to search back from (default end).
2632 * @return Index of start of last occurrence.
2633 */
df66af3b 2634 template<typename _Tp>
b96e2ff9 2635 _GLIBCXX20_CONSTEXPR
df66af3b
DK
2636 _If_sv<_Tp, size_type>
2637 rfind(const _Tp& __svt, size_type __pos = npos) const
2638 noexcept(is_same<_Tp, __sv_type>::value)
2639 {
2640 __sv_type __sv = __svt;
2641 return this->rfind(__sv.data(), __pos, __sv.size());
2642 }
ca8f2cb1
VV
2643#endif // C++17
2644
34a2b755
JW
2645 /**
2646 * @brief Find last position of a C substring.
2647 * @param __s C string to locate.
2648 * @param __pos Index of character to search back from.
2649 * @param __n Number of characters from s to search for.
2650 * @return Index of start of last occurrence.
2651 *
2652 * Starting from @a __pos, searches backward for the first @a
2653 * __n characters in @a __s within this string. If found,
2654 * returns the index where it begins. If not found, returns
2655 * npos.
2656 */
b96e2ff9 2657 _GLIBCXX20_CONSTEXPR
34a2b755 2658 size_type
39a03251
JW
2659 rfind(const _CharT* __s, size_type __pos, size_type __n) const
2660 _GLIBCXX_NOEXCEPT;
34a2b755
JW
2661
2662 /**
2663 * @brief Find last position of a C string.
2664 * @param __s C string to locate.
2665 * @param __pos Index of character to start search at (default end).
2666 * @return Index of start of last occurrence.
2667 *
2668 * Starting from @a __pos, searches backward for the value of
2669 * @a __s within this string. If found, returns the index
2670 * where it begins. If not found, returns npos.
2671 */
b96e2ff9 2672 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2673 size_type
2674 rfind(const _CharT* __s, size_type __pos = npos) const
2675 {
2676 __glibcxx_requires_string(__s);
2677 return this->rfind(__s, __pos, traits_type::length(__s));
2678 }
2679
2680 /**
2681 * @brief Find last position of a character.
2682 * @param __c Character to locate.
2683 * @param __pos Index of character to search back from (default end).
2684 * @return Index of last occurrence.
2685 *
2686 * Starting from @a __pos, searches backward for @a __c within
2687 * this string. If found, returns the index where it was
2688 * found. If not found, returns npos.
2689 */
b96e2ff9 2690 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2691 size_type
2692 rfind(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT;
2693
2694 /**
2695 * @brief Find position of a character of string.
2696 * @param __str String containing characters to locate.
2697 * @param __pos Index of character to search from (default 0).
2698 * @return Index of first occurrence.
2699 *
2700 * Starting from @a __pos, searches forward for one of the
2701 * characters of @a __str within this string. If found,
2702 * returns the index where it was found. If not found, returns
2703 * npos.
2704 */
b96e2ff9 2705 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2706 size_type
2707 find_first_of(const basic_string& __str, size_type __pos = 0) const
39a03251 2708 _GLIBCXX_NOEXCEPT
34a2b755
JW
2709 { return this->find_first_of(__str.data(), __pos, __str.size()); }
2710
1a289fa3 2711#if __cplusplus >= 201703L
ca8f2cb1
VV
2712 /**
2713 * @brief Find position of a character of a string_view.
df66af3b
DK
2714 * @param __svt An object convertible to string_view containing
2715 * characters to locate.
ca8f2cb1
VV
2716 * @param __pos Index of character to search from (default 0).
2717 * @return Index of first occurrence.
2718 */
df66af3b 2719 template<typename _Tp>
b96e2ff9 2720 _GLIBCXX20_CONSTEXPR
df66af3b
DK
2721 _If_sv<_Tp, size_type>
2722 find_first_of(const _Tp& __svt, size_type __pos = 0) const
2723 noexcept(is_same<_Tp, __sv_type>::value)
2724 {
2725 __sv_type __sv = __svt;
2726 return this->find_first_of(__sv.data(), __pos, __sv.size());
2727 }
ca8f2cb1
VV
2728#endif // C++17
2729
34a2b755
JW
2730 /**
2731 * @brief Find position of a character of C substring.
2732 * @param __s String containing characters to locate.
2733 * @param __pos Index of character to search from.
2734 * @param __n Number of characters from s to search for.
2735 * @return Index of first occurrence.
2736 *
2737 * Starting from @a __pos, searches forward for one of the
2738 * first @a __n characters of @a __s within this string. If
2739 * found, returns the index where it was found. If not found,
2740 * returns npos.
2741 */
b96e2ff9 2742 _GLIBCXX20_CONSTEXPR
34a2b755 2743 size_type
39a03251
JW
2744 find_first_of(const _CharT* __s, size_type __pos, size_type __n) const
2745 _GLIBCXX_NOEXCEPT;
34a2b755
JW
2746
2747 /**
2748 * @brief Find position of a character of C string.
2749 * @param __s String containing characters to locate.
2750 * @param __pos Index of character to search from (default 0).
2751 * @return Index of first occurrence.
2752 *
2753 * Starting from @a __pos, searches forward for one of the
2754 * characters of @a __s within this string. If found, returns
2755 * the index where it was found. If not found, returns npos.
2756 */
b96e2ff9 2757 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2758 size_type
2759 find_first_of(const _CharT* __s, size_type __pos = 0) const
39a03251 2760 _GLIBCXX_NOEXCEPT
34a2b755
JW
2761 {
2762 __glibcxx_requires_string(__s);
2763 return this->find_first_of(__s, __pos, traits_type::length(__s));
2764 }
2765
2766 /**
2767 * @brief Find position of a character.
2768 * @param __c Character to locate.
2769 * @param __pos Index of character to search from (default 0).
2770 * @return Index of first occurrence.
2771 *
2772 * Starting from @a __pos, searches forward for the character
2773 * @a __c within this string. If found, returns the index
2774 * where it was found. If not found, returns npos.
2775 *
2776 * Note: equivalent to find(__c, __pos).
2777 */
b96e2ff9 2778 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2779 size_type
2780 find_first_of(_CharT __c, size_type __pos = 0) const _GLIBCXX_NOEXCEPT
2781 { return this->find(__c, __pos); }
2782
2783 /**
2784 * @brief Find last position of a character of string.
2785 * @param __str String containing characters to locate.
2786 * @param __pos Index of character to search back from (default end).
2787 * @return Index of last occurrence.
2788 *
2789 * Starting from @a __pos, searches backward for one of the
2790 * characters of @a __str within this string. If found,
2791 * returns the index where it was found. If not found, returns
2792 * npos.
2793 */
b96e2ff9 2794 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2795 size_type
2796 find_last_of(const basic_string& __str, size_type __pos = npos) const
39a03251 2797 _GLIBCXX_NOEXCEPT
34a2b755
JW
2798 { return this->find_last_of(__str.data(), __pos, __str.size()); }
2799
1a289fa3 2800#if __cplusplus >= 201703L
ca8f2cb1
VV
2801 /**
2802 * @brief Find last position of a character of string.
df66af3b
DK
2803 * @param __svt An object convertible to string_view containing
2804 * characters to locate.
ca8f2cb1
VV
2805 * @param __pos Index of character to search back from (default end).
2806 * @return Index of last occurrence.
2807 */
df66af3b 2808 template<typename _Tp>
b96e2ff9 2809 _GLIBCXX20_CONSTEXPR
df66af3b
DK
2810 _If_sv<_Tp, size_type>
2811 find_last_of(const _Tp& __svt, size_type __pos = npos) const
2812 noexcept(is_same<_Tp, __sv_type>::value)
2813 {
2814 __sv_type __sv = __svt;
2815 return this->find_last_of(__sv.data(), __pos, __sv.size());
2816 }
ca8f2cb1
VV
2817#endif // C++17
2818
34a2b755
JW
2819 /**
2820 * @brief Find last position of a character of C substring.
2821 * @param __s C string containing characters to locate.
2822 * @param __pos Index of character to search back from.
2823 * @param __n Number of characters from s to search for.
2824 * @return Index of last occurrence.
2825 *
2826 * Starting from @a __pos, searches backward for one of the
2827 * first @a __n characters of @a __s within this string. If
2828 * found, returns the index where it was found. If not found,
2829 * returns npos.
2830 */
b96e2ff9 2831 _GLIBCXX20_CONSTEXPR
34a2b755 2832 size_type
39a03251
JW
2833 find_last_of(const _CharT* __s, size_type __pos, size_type __n) const
2834 _GLIBCXX_NOEXCEPT;
34a2b755
JW
2835
2836 /**
2837 * @brief Find last position of a character of C string.
2838 * @param __s C string containing characters to locate.
2839 * @param __pos Index of character to search back from (default end).
2840 * @return Index of last occurrence.
2841 *
2842 * Starting from @a __pos, searches backward for one of the
2843 * characters of @a __s within this string. If found, returns
2844 * the index where it was found. If not found, returns npos.
2845 */
b96e2ff9 2846 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2847 size_type
2848 find_last_of(const _CharT* __s, size_type __pos = npos) const
39a03251 2849 _GLIBCXX_NOEXCEPT
34a2b755
JW
2850 {
2851 __glibcxx_requires_string(__s);
2852 return this->find_last_of(__s, __pos, traits_type::length(__s));
2853 }
2854
2855 /**
2856 * @brief Find last position of a character.
2857 * @param __c Character to locate.
2858 * @param __pos Index of character to search back from (default end).
2859 * @return Index of last occurrence.
2860 *
2861 * Starting from @a __pos, searches backward for @a __c within
2862 * this string. If found, returns the index where it was
2863 * found. If not found, returns npos.
2864 *
2865 * Note: equivalent to rfind(__c, __pos).
2866 */
b96e2ff9 2867 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2868 size_type
2869 find_last_of(_CharT __c, size_type __pos = npos) const _GLIBCXX_NOEXCEPT
2870 { return this->rfind(__c, __pos); }
2871
2872 /**
2873 * @brief Find position of a character not in string.
2874 * @param __str String containing characters to avoid.
2875 * @param __pos Index of character to search from (default 0).
2876 * @return Index of first occurrence.
2877 *
2878 * Starting from @a __pos, searches forward for a character not contained
2879 * in @a __str within this string. If found, returns the index where it
2880 * was found. If not found, returns npos.
2881 */
b96e2ff9 2882 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2883 size_type
2884 find_first_not_of(const basic_string& __str, size_type __pos = 0) const
39a03251 2885 _GLIBCXX_NOEXCEPT
34a2b755
JW
2886 { return this->find_first_not_of(__str.data(), __pos, __str.size()); }
2887
1a289fa3 2888#if __cplusplus >= 201703L
ca8f2cb1
VV
2889 /**
2890 * @brief Find position of a character not in a string_view.
df66af3b
DK
2891 * @param __svt A object convertible to string_view containing
2892 * characters to avoid.
ca8f2cb1
VV
2893 * @param __pos Index of character to search from (default 0).
2894 * @return Index of first occurrence.
2895 */
df66af3b
DK
2896 template<typename _Tp>
2897 _If_sv<_Tp, size_type>
b96e2ff9 2898 _GLIBCXX20_CONSTEXPR
df66af3b
DK
2899 find_first_not_of(const _Tp& __svt, size_type __pos = 0) const
2900 noexcept(is_same<_Tp, __sv_type>::value)
2901 {
2902 __sv_type __sv = __svt;
2903 return this->find_first_not_of(__sv.data(), __pos, __sv.size());
2904 }
ca8f2cb1
VV
2905#endif // C++17
2906
34a2b755
JW
2907 /**
2908 * @brief Find position of a character not in C substring.
2909 * @param __s C string containing characters to avoid.
2910 * @param __pos Index of character to search from.
2911 * @param __n Number of characters from __s to consider.
2912 * @return Index of first occurrence.
2913 *
2914 * Starting from @a __pos, searches forward for a character not
2915 * contained in the first @a __n characters of @a __s within
2916 * this string. If found, returns the index where it was
2917 * found. If not found, returns npos.
2918 */
b96e2ff9 2919 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2920 size_type
2921 find_first_not_of(const _CharT* __s, size_type __pos,
39a03251 2922 size_type __n) const _GLIBCXX_NOEXCEPT;
34a2b755
JW
2923
2924 /**
2925 * @brief Find position of a character not in C string.
2926 * @param __s C string containing characters to avoid.
2927 * @param __pos Index of character to search from (default 0).
2928 * @return Index of first occurrence.
2929 *
2930 * Starting from @a __pos, searches forward for a character not
2931 * contained in @a __s within this string. If found, returns
2932 * the index where it was found. If not found, returns npos.
2933 */
b96e2ff9 2934 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2935 size_type
2936 find_first_not_of(const _CharT* __s, size_type __pos = 0) const
39a03251 2937 _GLIBCXX_NOEXCEPT
34a2b755
JW
2938 {
2939 __glibcxx_requires_string(__s);
2940 return this->find_first_not_of(__s, __pos, traits_type::length(__s));
2941 }
2942
2943 /**
2944 * @brief Find position of a different character.
2945 * @param __c Character to avoid.
2946 * @param __pos Index of character to search from (default 0).
2947 * @return Index of first occurrence.
2948 *
2949 * Starting from @a __pos, searches forward for a character
2950 * other than @a __c within this string. If found, returns the
2951 * index where it was found. If not found, returns npos.
2952 */
b96e2ff9 2953 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2954 size_type
2955 find_first_not_of(_CharT __c, size_type __pos = 0) const
39a03251 2956 _GLIBCXX_NOEXCEPT;
34a2b755
JW
2957
2958 /**
2959 * @brief Find last position of a character not in string.
2960 * @param __str String containing characters to avoid.
2961 * @param __pos Index of character to search back from (default end).
2962 * @return Index of last occurrence.
2963 *
2964 * Starting from @a __pos, searches backward for a character
2965 * not contained in @a __str within this string. If found,
2966 * returns the index where it was found. If not found, returns
2967 * npos.
2968 */
b96e2ff9 2969 _GLIBCXX20_CONSTEXPR
34a2b755
JW
2970 size_type
2971 find_last_not_of(const basic_string& __str, size_type __pos = npos) const
39a03251 2972 _GLIBCXX_NOEXCEPT
34a2b755
JW
2973 { return this->find_last_not_of(__str.data(), __pos, __str.size()); }
2974
1a289fa3 2975#if __cplusplus >= 201703L
ca8f2cb1
VV
2976 /**
2977 * @brief Find last position of a character not in a string_view.
df66af3b
DK
2978 * @param __svt An object convertible to string_view containing
2979 * characters to avoid.
ca8f2cb1
VV
2980 * @param __pos Index of character to search back from (default end).
2981 * @return Index of last occurrence.
2982 */
df66af3b 2983 template<typename _Tp>
b96e2ff9 2984 _GLIBCXX20_CONSTEXPR
df66af3b
DK
2985 _If_sv<_Tp, size_type>
2986 find_last_not_of(const _Tp& __svt, size_type __pos = npos) const
2987 noexcept(is_same<_Tp, __sv_type>::value)
2988 {
2989 __sv_type __sv = __svt;
2990 return this->find_last_not_of(__sv.data(), __pos, __sv.size());
2991 }
ca8f2cb1
VV
2992#endif // C++17
2993
34a2b755
JW
2994 /**
2995 * @brief Find last position of a character not in C substring.
2996 * @param __s C string containing characters to avoid.
2997 * @param __pos Index of character to search back from.
2998 * @param __n Number of characters from s to consider.
2999 * @return Index of last occurrence.
3000 *
3001 * Starting from @a __pos, searches backward for a character not
3002 * contained in the first @a __n characters of @a __s within this string.
3003 * If found, returns the index where it was found. If not found,
3004 * returns npos.
3005 */
b96e2ff9 3006 _GLIBCXX20_CONSTEXPR
34a2b755
JW
3007 size_type
3008 find_last_not_of(const _CharT* __s, size_type __pos,
39a03251 3009 size_type __n) const _GLIBCXX_NOEXCEPT;
34a2b755
JW
3010 /**
3011 * @brief Find last position of a character not in C string.
3012 * @param __s C string containing characters to avoid.
3013 * @param __pos Index of character to search back from (default end).
3014 * @return Index of last occurrence.
3015 *
3016 * Starting from @a __pos, searches backward for a character
3017 * not contained in @a __s within this string. If found,
3018 * returns the index where it was found. If not found, returns
3019 * npos.
3020 */
b96e2ff9 3021 _GLIBCXX20_CONSTEXPR
34a2b755
JW
3022 size_type
3023 find_last_not_of(const _CharT* __s, size_type __pos = npos) const
39a03251 3024 _GLIBCXX_NOEXCEPT
34a2b755
JW
3025 {
3026 __glibcxx_requires_string(__s);
3027 return this->find_last_not_of(__s, __pos, traits_type::length(__s));
3028 }
3029
3030 /**
3031 * @brief Find last position of a different character.
3032 * @param __c Character to avoid.
3033 * @param __pos Index of character to search back from (default end).
3034 * @return Index of last occurrence.
3035 *
3036 * Starting from @a __pos, searches backward for a character other than
3037 * @a __c within this string. If found, returns the index where it was
3038 * found. If not found, returns npos.
3039 */
b96e2ff9 3040 _GLIBCXX20_CONSTEXPR
34a2b755
JW
3041 size_type
3042 find_last_not_of(_CharT __c, size_type __pos = npos) const
39a03251 3043 _GLIBCXX_NOEXCEPT;
34a2b755
JW
3044
3045 /**
3046 * @brief Get a substring.
3047 * @param __pos Index of first character (default 0).
3048 * @param __n Number of characters in substring (default remainder).
3049 * @return The new string.
3050 * @throw std::out_of_range If __pos > size().
3051 *
3052 * Construct and return a new string using the @a __n
3053 * characters starting at @a __pos. If the string is too
3054 * short, use the remainder of the characters. If @a __pos is
3055 * beyond the end of the string, out_of_range is thrown.
3056 */
b96e2ff9 3057 _GLIBCXX20_CONSTEXPR
34a2b755
JW
3058 basic_string
3059 substr(size_type __pos = 0, size_type __n = npos) const
3060 { return basic_string(*this,
3061 _M_check(__pos, "basic_string::substr"), __n); }
3062
3063 /**
3064 * @brief Compare to a string.
3065 * @param __str String to compare against.
3066 * @return Integer < 0, 0, or > 0.
3067 *
3068 * Returns an integer < 0 if this string is ordered before @a
3069 * __str, 0 if their values are equivalent, or > 0 if this
3070 * string is ordered after @a __str. Determines the effective
3071 * length rlen of the strings to compare as the smallest of
3072 * size() and str.size(). The function then compares the two
3073 * strings by calling traits::compare(data(), str.data(),rlen).
3074 * If the result of the comparison is nonzero returns it,
3075 * otherwise the shorter one is ordered first.
3076 */
b96e2ff9 3077 _GLIBCXX20_CONSTEXPR
34a2b755
JW
3078 int
3079 compare(const basic_string& __str) const
3080 {
3081 const size_type __size = this->size();
3082 const size_type __osize = __str.size();
3083 const size_type __len = std::min(__size, __osize);
3084
2d60da10 3085 int __r = traits_type::compare(_M_data(), __str.data(), __len);
34a2b755
JW
3086 if (!__r)
3087 __r = _S_compare(__size, __osize);
3088 return __r;
3089 }
3090
1a289fa3 3091#if __cplusplus >= 201703L
ca8f2cb1
VV
3092 /**
3093 * @brief Compare to a string_view.
df66af3b 3094 * @param __svt An object convertible to string_view to compare against.
ca8f2cb1
VV
3095 * @return Integer < 0, 0, or > 0.
3096 */
df66af3b 3097 template<typename _Tp>
b96e2ff9 3098 _GLIBCXX20_CONSTEXPR
df66af3b
DK
3099 _If_sv<_Tp, int>
3100 compare(const _Tp& __svt) const
3101 noexcept(is_same<_Tp, __sv_type>::value)
3102 {
3103 __sv_type __sv = __svt;
3104 const size_type __size = this->size();
3105 const size_type __osize = __sv.size();
3106 const size_type __len = std::min(__size, __osize);
3107
3108 int __r = traits_type::compare(_M_data(), __sv.data(), __len);
3109 if (!__r)
3110 __r = _S_compare(__size, __osize);
3111 return __r;
3112 }
ca8f2cb1
VV
3113
3114 /**
3115 * @brief Compare to a string_view.
3116 * @param __pos A position in the string to start comparing from.
3117 * @param __n The number of characters to compare.
df66af3b
DK
3118 * @param __svt An object convertible to string_view to compare
3119 * against.
ca8f2cb1
VV
3120 * @return Integer < 0, 0, or > 0.
3121 */
df66af3b 3122 template<typename _Tp>
b96e2ff9 3123 _GLIBCXX20_CONSTEXPR
df66af3b
DK
3124 _If_sv<_Tp, int>
3125 compare(size_type __pos, size_type __n, const _Tp& __svt) const
3126 noexcept(is_same<_Tp, __sv_type>::value)
3127 {
3128 __sv_type __sv = __svt;
3129 return __sv_type(*this).substr(__pos, __n).compare(__sv);
3130 }
ca8f2cb1
VV
3131
3132 /**
3133 * @brief Compare to a string_view.
3134 * @param __pos1 A position in the string to start comparing from.
3135 * @param __n1 The number of characters to compare.
df66af3b
DK
3136 * @param __svt An object convertible to string_view to compare
3137 * against.
ca8f2cb1
VV
3138 * @param __pos2 A position in the string_view to start comparing from.
3139 * @param __n2 The number of characters to compare.
3140 * @return Integer < 0, 0, or > 0.
3141 */
df66af3b 3142 template<typename _Tp>
b96e2ff9 3143 _GLIBCXX20_CONSTEXPR
657213f7
JW
3144 _If_sv<_Tp, int>
3145 compare(size_type __pos1, size_type __n1, const _Tp& __svt,
3146 size_type __pos2, size_type __n2 = npos) const
df66af3b 3147 noexcept(is_same<_Tp, __sv_type>::value)
657213f7
JW
3148 {
3149 __sv_type __sv = __svt;
3150 return __sv_type(*this)
3151 .substr(__pos1, __n1).compare(__sv.substr(__pos2, __n2));
3152 }
ca8f2cb1
VV
3153#endif // C++17
3154
34a2b755
JW
3155 /**
3156 * @brief Compare substring to a string.
3157 * @param __pos Index of first character of substring.
3158 * @param __n Number of characters in substring.
3159 * @param __str String to compare against.
3160 * @return Integer < 0, 0, or > 0.
3161 *
3162 * Form the substring of this string from the @a __n characters
3163 * starting at @a __pos. Returns an integer < 0 if the
3164 * substring is ordered before @a __str, 0 if their values are
3165 * equivalent, or > 0 if the substring is ordered after @a
3166 * __str. Determines the effective length rlen of the strings
3167 * to compare as the smallest of the length of the substring
3168 * and @a __str.size(). The function then compares the two
3169 * strings by calling
3170 * traits::compare(substring.data(),str.data(),rlen). If the
3171 * result of the comparison is nonzero returns it, otherwise
3172 * the shorter one is ordered first.
3173 */
b96e2ff9 3174 _GLIBCXX20_CONSTEXPR
34a2b755
JW
3175 int
3176 compare(size_type __pos, size_type __n, const basic_string& __str) const;
3177
3178 /**
3179 * @brief Compare substring to a substring.
3180 * @param __pos1 Index of first character of substring.
3181 * @param __n1 Number of characters in substring.
3182 * @param __str String to compare against.
3183 * @param __pos2 Index of first character of substring of str.
3184 * @param __n2 Number of characters in substring of str.
3185 * @return Integer < 0, 0, or > 0.
3186 *
3187 * Form the substring of this string from the @a __n1
3188 * characters starting at @a __pos1. Form the substring of @a
3189 * __str from the @a __n2 characters starting at @a __pos2.
3190 * Returns an integer < 0 if this substring is ordered before
3191 * the substring of @a __str, 0 if their values are equivalent,
3192 * or > 0 if this substring is ordered after the substring of
3193 * @a __str. Determines the effective length rlen of the
3194 * strings to compare as the smallest of the lengths of the
3195 * substrings. The function then compares the two strings by
3196 * calling
3197 * traits::compare(substring.data(),str.substr(pos2,n2).data(),rlen).
3198 * If the result of the comparison is nonzero returns it,
3199 * otherwise the shorter one is ordered first.
3200 */
b96e2ff9 3201 _GLIBCXX20_CONSTEXPR
34a2b755
JW
3202 int
3203 compare(size_type __pos1, size_type __n1, const basic_string& __str,
852ee53c 3204 size_type __pos2, size_type __n2 = npos) const;
34a2b755
JW
3205
3206 /**
3207 * @brief Compare to a C string.
3208 * @param __s C string to compare against.
3209 * @return Integer < 0, 0, or > 0.
3210 *
3211 * Returns an integer < 0 if this string is ordered before @a __s, 0 if
3212 * their values are equivalent, or > 0 if this string is ordered after
3213 * @a __s. Determines the effective length rlen of the strings to
3214 * compare as the smallest of size() and the length of a string
3215 * constructed from @a __s. The function then compares the two strings
3216 * by calling traits::compare(data(),s,rlen). If the result of the
3217 * comparison is nonzero returns it, otherwise the shorter one is
3218 * ordered first.
3219 */
b96e2ff9 3220 _GLIBCXX20_CONSTEXPR
34a2b755 3221 int
39a03251 3222 compare(const _CharT* __s) const _GLIBCXX_NOEXCEPT;
34a2b755
JW
3223
3224 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3225 // 5 String::compare specification questionable
3226 /**
3227 * @brief Compare substring to a C string.
3228 * @param __pos Index of first character of substring.
3229 * @param __n1 Number of characters in substring.
3230 * @param __s C string to compare against.
3231 * @return Integer < 0, 0, or > 0.
3232 *
3233 * Form the substring of this string from the @a __n1
3234 * characters starting at @a pos. Returns an integer < 0 if
3235 * the substring is ordered before @a __s, 0 if their values
3236 * are equivalent, or > 0 if the substring is ordered after @a
3237 * __s. Determines the effective length rlen of the strings to
3238 * compare as the smallest of the length of the substring and
3239 * the length of a string constructed from @a __s. The
3240 * function then compares the two string by calling
3241 * traits::compare(substring.data(),__s,rlen). If the result of
3242 * the comparison is nonzero returns it, otherwise the shorter
3243 * one is ordered first.
3244 */
b96e2ff9 3245 _GLIBCXX20_CONSTEXPR
34a2b755
JW
3246 int
3247 compare(size_type __pos, size_type __n1, const _CharT* __s) const;
3248
3249 /**
3250 * @brief Compare substring against a character %array.
3251 * @param __pos Index of first character of substring.
3252 * @param __n1 Number of characters in substring.
3253 * @param __s character %array to compare against.
3254 * @param __n2 Number of characters of s.
3255 * @return Integer < 0, 0, or > 0.
3256 *
3257 * Form the substring of this string from the @a __n1
3258 * characters starting at @a __pos. Form a string from the
3259 * first @a __n2 characters of @a __s. Returns an integer < 0
3260 * if this substring is ordered before the string from @a __s,
3261 * 0 if their values are equivalent, or > 0 if this substring
3262 * is ordered after the string from @a __s. Determines the
3263 * effective length rlen of the strings to compare as the
3264 * smallest of the length of the substring and @a __n2. The
3265 * function then compares the two strings by calling
3266 * traits::compare(substring.data(),s,rlen). If the result of
3267 * the comparison is nonzero returns it, otherwise the shorter
3268 * one is ordered first.
3269 *
3270 * NB: s must have at least n2 characters, &apos;\\0&apos; has
3271 * no special meaning.
3272 */
b96e2ff9 3273 _GLIBCXX20_CONSTEXPR
34a2b755
JW
3274 int
3275 compare(size_type __pos, size_type __n1, const _CharT* __s,
3276 size_type __n2) const;
93ef155b 3277
b96e2ff9
ML
3278#if __cplusplus >= 202002L
3279 constexpr bool
5bd624fb
ESR
3280 starts_with(basic_string_view<_CharT, _Traits> __x) const noexcept
3281 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3282
b96e2ff9 3283 constexpr bool
5bd624fb
ESR
3284 starts_with(_CharT __x) const noexcept
3285 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3286
b96e2ff9 3287 constexpr bool
5bd624fb
ESR
3288 starts_with(const _CharT* __x) const noexcept
3289 { return __sv_type(this->data(), this->size()).starts_with(__x); }
3290
b96e2ff9 3291 constexpr bool
5bd624fb
ESR
3292 ends_with(basic_string_view<_CharT, _Traits> __x) const noexcept
3293 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3294
b96e2ff9 3295 constexpr bool
5bd624fb
ESR
3296 ends_with(_CharT __x) const noexcept
3297 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3298
b96e2ff9 3299 constexpr bool
5bd624fb
ESR
3300 ends_with(const _CharT* __x) const noexcept
3301 { return __sv_type(this->data(), this->size()).ends_with(__x); }
3302#endif // C++20
3303
f004d6d9 3304#if __cplusplus > 202002L
b96e2ff9 3305 constexpr bool
f004d6d9
PF
3306 contains(basic_string_view<_CharT, _Traits> __x) const noexcept
3307 { return __sv_type(this->data(), this->size()).contains(__x); }
3308
b96e2ff9 3309 constexpr bool
f004d6d9
PF
3310 contains(_CharT __x) const noexcept
3311 { return __sv_type(this->data(), this->size()).contains(__x); }
3312
b96e2ff9 3313 constexpr bool
f004d6d9
PF
3314 contains(const _CharT* __x) const noexcept
3315 { return __sv_type(this->data(), this->size()).contains(__x); }
3316#endif // C++23
3317
93ef155b
JW
3318 // Allow basic_stringbuf::__xfer_bufptrs to call _M_length:
3319 template<typename, typename, typename> friend class basic_stringbuf;
3320 };
34a2b755 3321_GLIBCXX_END_NAMESPACE_CXX11
7b527614
JW
3322_GLIBCXX_END_NAMESPACE_VERSION
3323} // namespace std
3324#endif // _GLIBCXX_USE_CXX11_ABI
ec61e852 3325
7b527614
JW
3326namespace std _GLIBCXX_VISIBILITY(default)
3327{
3328_GLIBCXX_BEGIN_NAMESPACE_VERSION
725dc051 3329
6d82c562
JW
3330#if __cpp_deduction_guides >= 201606
3331_GLIBCXX_BEGIN_NAMESPACE_CXX11
3332 template<typename _InputIterator, typename _CharT
3333 = typename iterator_traits<_InputIterator>::value_type,
3334 typename _Allocator = allocator<_CharT>,
3335 typename = _RequireInputIter<_InputIterator>,
3336 typename = _RequireAllocator<_Allocator>>
3337 basic_string(_InputIterator, _InputIterator, _Allocator = _Allocator())
3338 -> basic_string<_CharT, char_traits<_CharT>, _Allocator>;
53e926c8
JW
3339
3340 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3341 // 3075. basic_string needs deduction guides from basic_string_view
3342 template<typename _CharT, typename _Traits,
3343 typename _Allocator = allocator<_CharT>,
3344 typename = _RequireAllocator<_Allocator>>
3345 basic_string(basic_string_view<_CharT, _Traits>, const _Allocator& = _Allocator())
3346 -> basic_string<_CharT, _Traits, _Allocator>;
3347
3348 template<typename _CharT, typename _Traits,
3349 typename _Allocator = allocator<_CharT>,
3350 typename = _RequireAllocator<_Allocator>>
3351 basic_string(basic_string_view<_CharT, _Traits>,
3352 typename basic_string<_CharT, _Traits, _Allocator>::size_type,
3353 typename basic_string<_CharT, _Traits, _Allocator>::size_type,
3354 const _Allocator& = _Allocator())
3355 -> basic_string<_CharT, _Traits, _Allocator>;
6d82c562
JW
3356_GLIBCXX_END_NAMESPACE_CXX11
3357#endif
3358
725dc051 3359 // operator+
284f19bf
JQ
3360 /**
3361 * @brief Concatenate two strings.
93c66bc6
BK
3362 * @param __lhs First string.
3363 * @param __rhs Last string.
3364 * @return New string with value of @a __lhs followed by @a __rhs.
ed6814f7 3365 */
41b8e86c 3366 template<typename _CharT, typename _Traits, typename _Alloc>
b96e2ff9 3367 _GLIBCXX20_CONSTEXPR
725dc051
BK
3368 basic_string<_CharT, _Traits, _Alloc>
3369 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3370 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
3371 {
3372 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
3373 __str.append(__rhs);
3374 return __str;
3375 }
3376
284f19bf
JQ
3377 /**
3378 * @brief Concatenate C string and string.
93c66bc6
BK
3379 * @param __lhs First string.
3380 * @param __rhs Last string.
3381 * @return New string with value of @a __lhs followed by @a __rhs.
ed6814f7 3382 */
725dc051 3383 template<typename _CharT, typename _Traits, typename _Alloc>
b96e2ff9 3384 _GLIBCXX20_CONSTEXPR
725dc051
BK
3385 basic_string<_CharT,_Traits,_Alloc>
3386 operator+(const _CharT* __lhs,
3387 const basic_string<_CharT,_Traits,_Alloc>& __rhs);
3388
284f19bf
JQ
3389 /**
3390 * @brief Concatenate character and string.
93c66bc6
BK
3391 * @param __lhs First string.
3392 * @param __rhs Last string.
3393 * @return New string with @a __lhs followed by @a __rhs.
ed6814f7 3394 */
725dc051 3395 template<typename _CharT, typename _Traits, typename _Alloc>
b96e2ff9 3396 _GLIBCXX20_CONSTEXPR
725dc051
BK
3397 basic_string<_CharT,_Traits,_Alloc>
3398 operator+(_CharT __lhs, const basic_string<_CharT,_Traits,_Alloc>& __rhs);
3399
284f19bf
JQ
3400 /**
3401 * @brief Concatenate string and C string.
93c66bc6
BK
3402 * @param __lhs First string.
3403 * @param __rhs Last string.
3404 * @return New string with @a __lhs followed by @a __rhs.
ed6814f7 3405 */
725dc051 3406 template<typename _CharT, typename _Traits, typename _Alloc>
b96e2ff9 3407 _GLIBCXX20_CONSTEXPR
725dc051
BK
3408 inline basic_string<_CharT, _Traits, _Alloc>
3409 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
34a2b755 3410 const _CharT* __rhs)
725dc051
BK
3411 {
3412 basic_string<_CharT, _Traits, _Alloc> __str(__lhs);
3413 __str.append(__rhs);
3414 return __str;
3415 }
3416
284f19bf
JQ
3417 /**
3418 * @brief Concatenate string and character.
93c66bc6
BK
3419 * @param __lhs First string.
3420 * @param __rhs Last string.
3421 * @return New string with @a __lhs followed by @a __rhs.
ed6814f7 3422 */
725dc051 3423 template<typename _CharT, typename _Traits, typename _Alloc>
b96e2ff9 3424 _GLIBCXX20_CONSTEXPR
725dc051
BK
3425 inline basic_string<_CharT, _Traits, _Alloc>
3426 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)
3427 {
ed6814f7 3428 typedef basic_string<_CharT, _Traits, _Alloc> __string_type;
725dc051
BK
3429 typedef typename __string_type::size_type __size_type;
3430 __string_type __str(__lhs);
3431 __str.append(__size_type(1), __rhs);
3432 return __str;
3433 }
3434
734f5023 3435#if __cplusplus >= 201103L
ce99f498 3436 template<typename _CharT, typename _Traits, typename _Alloc>
b96e2ff9 3437 _GLIBCXX20_CONSTEXPR
ce99f498
PC
3438 inline basic_string<_CharT, _Traits, _Alloc>
3439 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
3440 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
3441 { return std::move(__lhs.append(__rhs)); }
3442
3443 template<typename _CharT, typename _Traits, typename _Alloc>
b96e2ff9 3444 _GLIBCXX20_CONSTEXPR
ce99f498
PC
3445 inline basic_string<_CharT, _Traits, _Alloc>
3446 operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3447 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
3448 { return std::move(__rhs.insert(0, __lhs)); }
3449
3450 template<typename _CharT, typename _Traits, typename _Alloc>
b96e2ff9 3451 _GLIBCXX20_CONSTEXPR
ce99f498
PC
3452 inline basic_string<_CharT, _Traits, _Alloc>
3453 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
3454 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
37a68925 3455 {
f4e678ef
NDR
3456#if _GLIBCXX_USE_CXX11_ABI
3457 using _Alloc_traits = allocator_traits<_Alloc>;
3458 bool __use_rhs = false;
3459 if _GLIBCXX17_CONSTEXPR (typename _Alloc_traits::is_always_equal{})
3460 __use_rhs = true;
3461 else if (__lhs.get_allocator() == __rhs.get_allocator())
3462 __use_rhs = true;
3463 if (__use_rhs)
3464#endif
3465 {
3466 const auto __size = __lhs.size() + __rhs.size();
3467 if (__size > __lhs.capacity() && __size <= __rhs.capacity())
3468 return std::move(__rhs.insert(0, __lhs));
3469 }
3470 return std::move(__lhs.append(__rhs));
37a68925 3471 }
ce99f498
PC
3472
3473 template<typename _CharT, typename _Traits, typename _Alloc>
b96e2ff9 3474 _GLIBCXX20_CONSTEXPR
ce99f498
PC
3475 inline basic_string<_CharT, _Traits, _Alloc>
3476 operator+(const _CharT* __lhs,
3477 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
3478 { return std::move(__rhs.insert(0, __lhs)); }
3479
3480 template<typename _CharT, typename _Traits, typename _Alloc>
b96e2ff9 3481 _GLIBCXX20_CONSTEXPR
ce99f498 3482 inline basic_string<_CharT, _Traits, _Alloc>
37a68925
PC
3483 operator+(_CharT __lhs,
3484 basic_string<_CharT, _Traits, _Alloc>&& __rhs)
ce99f498
PC
3485 { return std::move(__rhs.insert(0, 1, __lhs)); }
3486
3487 template<typename _CharT, typename _Traits, typename _Alloc>
b96e2ff9 3488 _GLIBCXX20_CONSTEXPR
ce99f498
PC
3489 inline basic_string<_CharT, _Traits, _Alloc>
3490 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
3491 const _CharT* __rhs)
3492 { return std::move(__lhs.append(__rhs)); }
3493
3494 template<typename _CharT, typename _Traits, typename _Alloc>
b96e2ff9 3495 _GLIBCXX20_CONSTEXPR
ce99f498
PC
3496 inline basic_string<_CharT, _Traits, _Alloc>
3497 operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
3498 _CharT __rhs)
3499 { return std::move(__lhs.append(1, __rhs)); }
3500#endif
3501
725dc051 3502 // operator ==
284f19bf
JQ
3503 /**
3504 * @brief Test equivalence of two strings.
93c66bc6
BK
3505 * @param __lhs First string.
3506 * @param __rhs Second string.
3507 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
ed6814f7 3508 */
725dc051 3509 template<typename _CharT, typename _Traits, typename _Alloc>
b96e2ff9 3510 _GLIBCXX20_CONSTEXPR
725dc051
BK
3511 inline bool
3512 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3513 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5caff414 3514 _GLIBCXX_NOEXCEPT
725dc051
BK
3515 { return __lhs.compare(__rhs) == 0; }
3516
bd12160a 3517 template<typename _CharT>
b96e2ff9 3518 _GLIBCXX20_CONSTEXPR
bd12160a
PC
3519 inline
3520 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, bool>::__type
3521 operator==(const basic_string<_CharT>& __lhs,
5caff414 3522 const basic_string<_CharT>& __rhs) _GLIBCXX_NOEXCEPT
bd12160a
PC
3523 { return (__lhs.size() == __rhs.size()
3524 && !std::char_traits<_CharT>::compare(__lhs.data(), __rhs.data(),
3525 __lhs.size())); }
3526
284f19bf
JQ
3527 /**
3528 * @brief Test equivalence of string and C string.
93c66bc6
BK
3529 * @param __lhs String.
3530 * @param __rhs C string.
3531 * @return True if @a __lhs.compare(@a __rhs) == 0. False otherwise.
ed6814f7 3532 */
725dc051 3533 template<typename _CharT, typename _Traits, typename _Alloc>
b96e2ff9 3534 _GLIBCXX20_CONSTEXPR
725dc051
BK
3535 inline bool
3536 operator==(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3537 const _CharT* __rhs)
3538 { return __lhs.compare(__rhs) == 0; }
3539
875d6cb3
JW
3540#if __cpp_lib_three_way_comparison
3541 /**
3542 * @brief Three-way comparison of a string and a C string.
3543 * @param __lhs A string.
3544 * @param __rhs A null-terminated string.
3545 * @return A value indicating whether `__lhs` is less than, equal to,
3546 * greater than, or incomparable with `__rhs`.
3547 */
3548 template<typename _CharT, typename _Traits, typename _Alloc>
73e4d9f1 3549 constexpr auto
875d6cb3
JW
3550 operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3551 const basic_string<_CharT, _Traits, _Alloc>& __rhs) noexcept
3552 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
3553 { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); }
3554
3555 /**
3556 * @brief Three-way comparison of a string and a C string.
3557 * @param __lhs A string.
3558 * @param __rhs A null-terminated string.
3559 * @return A value indicating whether `__lhs` is less than, equal to,
3560 * greater than, or incomparable with `__rhs`.
3561 */
3562 template<typename _CharT, typename _Traits, typename _Alloc>
73e4d9f1 3563 constexpr auto
875d6cb3
JW
3564 operator<=>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3565 const _CharT* __rhs) noexcept
3566 -> decltype(__detail::__char_traits_cmp_cat<_Traits>(0))
3567 { return __detail::__char_traits_cmp_cat<_Traits>(__lhs.compare(__rhs)); }
3568#else
3569 /**
3570 * @brief Test equivalence of C string and string.
3571 * @param __lhs C string.
3572 * @param __rhs String.
3573 * @return True if @a __rhs.compare(@a __lhs) == 0. False otherwise.
3574 */
3575 template<typename _CharT, typename _Traits, typename _Alloc>
3576 inline bool
3577 operator==(const _CharT* __lhs,
3578 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
3579 { return __rhs.compare(__lhs) == 0; }
3580
725dc051 3581 // operator !=
284f19bf
JQ
3582 /**
3583 * @brief Test difference of two strings.
93c66bc6
BK
3584 * @param __lhs First string.
3585 * @param __rhs Second string.
3586 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
ed6814f7 3587 */
725dc051
BK
3588 template<typename _CharT, typename _Traits, typename _Alloc>
3589 inline bool
3590 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3591 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5caff414 3592 _GLIBCXX_NOEXCEPT
bd12160a 3593 { return !(__lhs == __rhs); }
725dc051 3594
284f19bf
JQ
3595 /**
3596 * @brief Test difference of C string and string.
93c66bc6
BK
3597 * @param __lhs C string.
3598 * @param __rhs String.
3599 * @return True if @a __rhs.compare(@a __lhs) != 0. False otherwise.
ed6814f7 3600 */
725dc051
BK
3601 template<typename _CharT, typename _Traits, typename _Alloc>
3602 inline bool
3603 operator!=(const _CharT* __lhs,
3604 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
bd12160a 3605 { return !(__lhs == __rhs); }
725dc051 3606
284f19bf
JQ
3607 /**
3608 * @brief Test difference of string and C string.
93c66bc6
BK
3609 * @param __lhs String.
3610 * @param __rhs C string.
3611 * @return True if @a __lhs.compare(@a __rhs) != 0. False otherwise.
ed6814f7 3612 */
725dc051
BK
3613 template<typename _CharT, typename _Traits, typename _Alloc>
3614 inline bool
3615 operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3616 const _CharT* __rhs)
bd12160a 3617 { return !(__lhs == __rhs); }
725dc051
BK
3618
3619 // operator <
284f19bf
JQ
3620 /**
3621 * @brief Test if string precedes string.
93c66bc6
BK
3622 * @param __lhs First string.
3623 * @param __rhs Second string.
3624 * @return True if @a __lhs precedes @a __rhs. False otherwise.
ed6814f7 3625 */
725dc051
BK
3626 template<typename _CharT, typename _Traits, typename _Alloc>
3627 inline bool
3628 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3629 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5caff414 3630 _GLIBCXX_NOEXCEPT
725dc051
BK
3631 { return __lhs.compare(__rhs) < 0; }
3632
284f19bf
JQ
3633 /**
3634 * @brief Test if string precedes C string.
93c66bc6
BK
3635 * @param __lhs String.
3636 * @param __rhs C string.
3637 * @return True if @a __lhs precedes @a __rhs. False otherwise.
ed6814f7 3638 */
725dc051
BK
3639 template<typename _CharT, typename _Traits, typename _Alloc>
3640 inline bool
3641 operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3642 const _CharT* __rhs)
3643 { return __lhs.compare(__rhs) < 0; }
3644
284f19bf
JQ
3645 /**
3646 * @brief Test if C string precedes string.
93c66bc6
BK
3647 * @param __lhs C string.
3648 * @param __rhs String.
3649 * @return True if @a __lhs precedes @a __rhs. False otherwise.
ed6814f7 3650 */
725dc051
BK
3651 template<typename _CharT, typename _Traits, typename _Alloc>
3652 inline bool
3653 operator<(const _CharT* __lhs,
3654 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
3655 { return __rhs.compare(__lhs) > 0; }
3656
3657 // operator >
284f19bf
JQ
3658 /**
3659 * @brief Test if string follows string.
93c66bc6
BK
3660 * @param __lhs First string.
3661 * @param __rhs Second string.
3662 * @return True if @a __lhs follows @a __rhs. False otherwise.
ed6814f7 3663 */
725dc051
BK
3664 template<typename _CharT, typename _Traits, typename _Alloc>
3665 inline bool
3666 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3667 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5caff414 3668 _GLIBCXX_NOEXCEPT
725dc051
BK
3669 { return __lhs.compare(__rhs) > 0; }
3670
284f19bf
JQ
3671 /**
3672 * @brief Test if string follows C string.
93c66bc6
BK
3673 * @param __lhs String.
3674 * @param __rhs C string.
3675 * @return True if @a __lhs follows @a __rhs. False otherwise.
ed6814f7 3676 */
725dc051
BK
3677 template<typename _CharT, typename _Traits, typename _Alloc>
3678 inline bool
3679 operator>(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3680 const _CharT* __rhs)
3681 { return __lhs.compare(__rhs) > 0; }
3682
284f19bf
JQ
3683 /**
3684 * @brief Test if C string follows string.
93c66bc6
BK
3685 * @param __lhs C string.
3686 * @param __rhs String.
3687 * @return True if @a __lhs follows @a __rhs. False otherwise.
ed6814f7 3688 */
725dc051
BK
3689 template<typename _CharT, typename _Traits, typename _Alloc>
3690 inline bool
3691 operator>(const _CharT* __lhs,
3692 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
3693 { return __rhs.compare(__lhs) < 0; }
3694
3695 // operator <=
284f19bf
JQ
3696 /**
3697 * @brief Test if string doesn't follow string.
93c66bc6
BK
3698 * @param __lhs First string.
3699 * @param __rhs Second string.
3700 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
ed6814f7 3701 */
725dc051
BK
3702 template<typename _CharT, typename _Traits, typename _Alloc>
3703 inline bool
3704 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3705 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5caff414 3706 _GLIBCXX_NOEXCEPT
725dc051
BK
3707 { return __lhs.compare(__rhs) <= 0; }
3708
284f19bf
JQ
3709 /**
3710 * @brief Test if string doesn't follow C string.
93c66bc6
BK
3711 * @param __lhs String.
3712 * @param __rhs C string.
3713 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
ed6814f7 3714 */
725dc051
BK
3715 template<typename _CharT, typename _Traits, typename _Alloc>
3716 inline bool
3717 operator<=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3718 const _CharT* __rhs)
3719 { return __lhs.compare(__rhs) <= 0; }
3720
284f19bf
JQ
3721 /**
3722 * @brief Test if C string doesn't follow string.
93c66bc6
BK
3723 * @param __lhs C string.
3724 * @param __rhs String.
3725 * @return True if @a __lhs doesn't follow @a __rhs. False otherwise.
ed6814f7 3726 */
725dc051
BK
3727 template<typename _CharT, typename _Traits, typename _Alloc>
3728 inline bool
3729 operator<=(const _CharT* __lhs,
3730 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
e9fb72e8 3731 { return __rhs.compare(__lhs) >= 0; }
725dc051
BK
3732
3733 // operator >=
284f19bf
JQ
3734 /**
3735 * @brief Test if string doesn't precede string.
93c66bc6
BK
3736 * @param __lhs First string.
3737 * @param __rhs Second string.
3738 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
ed6814f7 3739 */
725dc051
BK
3740 template<typename _CharT, typename _Traits, typename _Alloc>
3741 inline bool
3742 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3743 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
5caff414 3744 _GLIBCXX_NOEXCEPT
725dc051
BK
3745 { return __lhs.compare(__rhs) >= 0; }
3746
284f19bf
JQ
3747 /**
3748 * @brief Test if string doesn't precede C string.
93c66bc6
BK
3749 * @param __lhs String.
3750 * @param __rhs C string.
3751 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
ed6814f7 3752 */
725dc051
BK
3753 template<typename _CharT, typename _Traits, typename _Alloc>
3754 inline bool
3755 operator>=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
3756 const _CharT* __rhs)
3757 { return __lhs.compare(__rhs) >= 0; }
3758
284f19bf
JQ
3759 /**
3760 * @brief Test if C string doesn't precede string.
93c66bc6
BK
3761 * @param __lhs C string.
3762 * @param __rhs String.
3763 * @return True if @a __lhs doesn't precede @a __rhs. False otherwise.
ed6814f7 3764 */
725dc051
BK
3765 template<typename _CharT, typename _Traits, typename _Alloc>
3766 inline bool
3767 operator>=(const _CharT* __lhs,
3768 const basic_string<_CharT, _Traits, _Alloc>& __rhs)
3769 { return __rhs.compare(__lhs) <= 0; }
875d6cb3 3770#endif // three-way comparison
725dc051 3771
284f19bf
JQ
3772 /**
3773 * @brief Swap contents of two strings.
93c66bc6
BK
3774 * @param __lhs First string.
3775 * @param __rhs Second string.
284f19bf 3776 *
93c66bc6 3777 * Exchanges the contents of @a __lhs and @a __rhs in constant time.
284f19bf 3778 */
725dc051 3779 template<typename _CharT, typename _Traits, typename _Alloc>
b96e2ff9 3780 _GLIBCXX20_CONSTEXPR
725dc051
BK
3781 inline void
3782 swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
3783 basic_string<_CharT, _Traits, _Alloc>& __rhs)
5caff414 3784 _GLIBCXX_NOEXCEPT_IF(noexcept(__lhs.swap(__rhs)))
725dc051
BK
3785 { __lhs.swap(__rhs); }
3786
34a2b755 3787
284f19bf
JQ
3788 /**
3789 * @brief Read stream into a string.
93c66bc6
BK
3790 * @param __is Input stream.
3791 * @param __str Buffer to store into.
284f19bf
JQ
3792 * @return Reference to the input stream.
3793 *
93c66bc6
BK
3794 * Stores characters from @a __is into @a __str until whitespace is
3795 * found, the end of the stream is encountered, or str.max_size()
3796 * is reached. If is.width() is non-zero, that is the limit on the
3797 * number of characters stored into @a __str. Any previous
3798 * contents of @a __str are erased.
284f19bf 3799 */
725dc051
BK
3800 template<typename _CharT, typename _Traits, typename _Alloc>
3801 basic_istream<_CharT, _Traits>&
3802 operator>>(basic_istream<_CharT, _Traits>& __is,
3803 basic_string<_CharT, _Traits, _Alloc>& __str);
3804
ceed88b1
PC
3805 template<>
3806 basic_istream<char>&
3807 operator>>(basic_istream<char>& __is, basic_string<char>& __str);
3808
284f19bf
JQ
3809 /**
3810 * @brief Write string to a stream.
93c66bc6
BK
3811 * @param __os Output stream.
3812 * @param __str String to write out.
284f19bf
JQ
3813 * @return Reference to the output stream.
3814 *
93c66bc6 3815 * Output characters of @a __str into os following the same rules as for
284f19bf
JQ
3816 * writing a C string.
3817 */
725dc051 3818 template<typename _CharT, typename _Traits, typename _Alloc>
70c99f6c 3819 inline basic_ostream<_CharT, _Traits>&
725dc051 3820 operator<<(basic_ostream<_CharT, _Traits>& __os,
ec2061a9
PC
3821 const basic_string<_CharT, _Traits, _Alloc>& __str)
3822 {
3823 // _GLIBCXX_RESOLVE_LIB_DEFECTS
3824 // 586. string inserter not a formatted function
11202768 3825 return __ostream_insert(__os, __str.data(), __str.size());
ec2061a9 3826 }
725dc051 3827
284f19bf
JQ
3828 /**
3829 * @brief Read a line from stream into a string.
93c66bc6
BK
3830 * @param __is Input stream.
3831 * @param __str Buffer to store into.
3832 * @param __delim Character marking end of line.
284f19bf
JQ
3833 * @return Reference to the input stream.
3834 *
93c66bc6
BK
3835 * Stores characters from @a __is into @a __str until @a __delim is
3836 * found, the end of the stream is encountered, or str.max_size()
3b2453a9
JW
3837 * is reached. Any previous contents of @a __str are erased. If
3838 * @a __delim is encountered, it is extracted but not stored into
3839 * @a __str.
284f19bf 3840 */
725dc051 3841 template<typename _CharT, typename _Traits, typename _Alloc>
e9fb72e8 3842 basic_istream<_CharT, _Traits>&
725dc051
BK
3843 getline(basic_istream<_CharT, _Traits>& __is,
3844 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim);
3845
284f19bf
JQ
3846 /**
3847 * @brief Read a line from stream into a string.
93c66bc6
BK
3848 * @param __is Input stream.
3849 * @param __str Buffer to store into.
284f19bf
JQ
3850 * @return Reference to the input stream.
3851 *
93c66bc6 3852 * Stores characters from is into @a __str until &apos;\n&apos; is
2a60a9f6 3853 * found, the end of the stream is encountered, or str.max_size()
3b2453a9
JW
3854 * is reached. Any previous contents of @a __str are erased. If
3855 * end of line is encountered, it is extracted but not stored into
3856 * @a __str.
284f19bf 3857 */
725dc051 3858 template<typename _CharT, typename _Traits, typename _Alloc>
e9fb72e8 3859 inline basic_istream<_CharT, _Traits>&
725dc051 3860 getline(basic_istream<_CharT, _Traits>& __is,
70c99f6c 3861 basic_string<_CharT, _Traits, _Alloc>& __str)
19173661
JW
3862 { return std::getline(__is, __str, __is.widen('\n')); }
3863
3864#if __cplusplus >= 201103L
3865 /// Read a line from an rvalue stream into a string.
3866 template<typename _CharT, typename _Traits, typename _Alloc>
caff45a6 3867 inline basic_istream<_CharT, _Traits>&
19173661
JW
3868 getline(basic_istream<_CharT, _Traits>&& __is,
3869 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
3870 { return std::getline(__is, __str, __delim); }
3871
3872 /// Read a line from an rvalue stream into a string.
3873 template<typename _CharT, typename _Traits, typename _Alloc>
3874 inline basic_istream<_CharT, _Traits>&
3875 getline(basic_istream<_CharT, _Traits>&& __is,
3876 basic_string<_CharT, _Traits, _Alloc>& __str)
3877 { return std::getline(__is, __str); }
3878#endif
70c99f6c 3879
e9fb72e8
PC
3880 template<>
3881 basic_istream<char>&
3882 getline(basic_istream<char>& __in, basic_string<char>& __str,
3883 char __delim);
3884
3885#ifdef _GLIBCXX_USE_WCHAR_T
3886 template<>
3887 basic_istream<wchar_t>&
3888 getline(basic_istream<wchar_t>& __in, basic_string<wchar_t>& __str,
3889 wchar_t __delim);
3890#endif
3cbc7af0 3891
12ffa228
BK
3892_GLIBCXX_END_NAMESPACE_VERSION
3893} // namespace
7364f286 3894
23c64853 3895#if __cplusplus >= 201103L
7364f286 3896
a5a6b586 3897#include <ext/string_conversions.h>
cd0b94e6 3898#include <bits/charconv.h>
7364f286 3899
12ffa228
BK
3900namespace std _GLIBCXX_VISIBILITY(default)
3901{
3902_GLIBCXX_BEGIN_NAMESPACE_VERSION
34a2b755 3903_GLIBCXX_BEGIN_NAMESPACE_CXX11
7364f286 3904
23c64853 3905#if _GLIBCXX_USE_C99_STDLIB
a5a6b586
PC
3906 // 21.4 Numeric Conversions [string.conversions].
3907 inline int
3908 stoi(const string& __str, size_t* __idx = 0, int __base = 10)
3909 { return __gnu_cxx::__stoa<long, int>(&std::strtol, "stoi", __str.c_str(),
3910 __idx, __base); }
3911
3912 inline long
3913 stol(const string& __str, size_t* __idx = 0, int __base = 10)
3914 { return __gnu_cxx::__stoa(&std::strtol, "stol", __str.c_str(),
3915 __idx, __base); }
3916
3917 inline unsigned long
3918 stoul(const string& __str, size_t* __idx = 0, int __base = 10)
3919 { return __gnu_cxx::__stoa(&std::strtoul, "stoul", __str.c_str(),
3920 __idx, __base); }
3921
3922 inline long long
3923 stoll(const string& __str, size_t* __idx = 0, int __base = 10)
3924 { return __gnu_cxx::__stoa(&std::strtoll, "stoll", __str.c_str(),
3925 __idx, __base); }
3926
3927 inline unsigned long long
3928 stoull(const string& __str, size_t* __idx = 0, int __base = 10)
3929 { return __gnu_cxx::__stoa(&std::strtoull, "stoull", __str.c_str(),
3930 __idx, __base); }
3931
3932 // NB: strtof vs strtod.
3933 inline float
3934 stof(const string& __str, size_t* __idx = 0)
3935 { return __gnu_cxx::__stoa(&std::strtof, "stof", __str.c_str(), __idx); }
3936
3937 inline double
3938 stod(const string& __str, size_t* __idx = 0)
3939 { return __gnu_cxx::__stoa(&std::strtod, "stod", __str.c_str(), __idx); }
3940
3941 inline long double
3942 stold(const string& __str, size_t* __idx = 0)
3943 { return __gnu_cxx::__stoa(&std::strtold, "stold", __str.c_str(), __idx); }
23c64853 3944#endif // _GLIBCXX_USE_C99_STDLIB
a5a6b586 3945
cd0b94e6 3946 // DR 1261. Insufficent overloads for to_string / to_wstring
a4ecd144 3947
a4ecd144
PC
3948 inline string
3949 to_string(int __val)
9d813ddd
JW
3950#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32
3951 noexcept // any 32-bit value fits in the SSO buffer
3952#endif
cd0b94e6
JW
3953 {
3954 const bool __neg = __val < 0;
3955 const unsigned __uval = __neg ? (unsigned)~__val + 1u : __val;
3956 const auto __len = __detail::__to_chars_len(__uval);
3957 string __str(__neg + __len, '-');
3958 __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
3959 return __str;
3960 }
a4ecd144
PC
3961
3962 inline string
3963 to_string(unsigned __val)
9d813ddd
JW
3964#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_INT__) <= 32
3965 noexcept // any 32-bit value fits in the SSO buffer
3966#endif
cd0b94e6
JW
3967 {
3968 string __str(__detail::__to_chars_len(__val), '\0');
3969 __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
3970 return __str;
3971 }
a4ecd144
PC
3972
3973 inline string
3974 to_string(long __val)
9d813ddd
JW
3975#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32
3976 noexcept // any 32-bit value fits in the SSO buffer
3977#endif
cd0b94e6
JW
3978 {
3979 const bool __neg = __val < 0;
3980 const unsigned long __uval = __neg ? (unsigned long)~__val + 1ul : __val;
3981 const auto __len = __detail::__to_chars_len(__uval);
3982 string __str(__neg + __len, '-');
3983 __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
3984 return __str;
3985 }
a4ecd144
PC
3986
3987 inline string
3988 to_string(unsigned long __val)
9d813ddd
JW
3989#if _GLIBCXX_USE_CXX11_ABI && (__CHAR_BIT__ * __SIZEOF_LONG__) <= 32
3990 noexcept // any 32-bit value fits in the SSO buffer
3991#endif
cd0b94e6
JW
3992 {
3993 string __str(__detail::__to_chars_len(__val), '\0');
3994 __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
3995 return __str;
3996 }
a4ecd144 3997
a5a6b586
PC
3998 inline string
3999 to_string(long long __val)
cd0b94e6
JW
4000 {
4001 const bool __neg = __val < 0;
4002 const unsigned long long __uval
4003 = __neg ? (unsigned long long)~__val + 1ull : __val;
4004 const auto __len = __detail::__to_chars_len(__uval);
4005 string __str(__neg + __len, '-');
4006 __detail::__to_chars_10_impl(&__str[__neg], __len, __uval);
4007 return __str;
4008 }
a5a6b586
PC
4009
4010 inline string
4011 to_string(unsigned long long __val)
cd0b94e6
JW
4012 {
4013 string __str(__detail::__to_chars_len(__val), '\0');
4014 __detail::__to_chars_10_impl(&__str[0], __str.size(), __val);
4015 return __str;
4016 }
4017
4018#if _GLIBCXX_USE_C99_STDIO
4019 // NB: (v)snprintf vs sprintf.
a5a6b586 4020
a4ecd144
PC
4021 inline string
4022 to_string(float __val)
4023 {
4024 const int __n =
4025 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
4026 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
4027 "%f", __val);
4028 }
4029
4030 inline string
4031 to_string(double __val)
4032 {
4033 const int __n =
4034 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
4035 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
4036 "%f", __val);
4037 }
4038
a5a6b586
PC
4039 inline string
4040 to_string(long double __val)
4041 {
4042 const int __n =
4043 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
4044 return __gnu_cxx::__to_xstring<string>(&std::vsnprintf, __n,
4045 "%Lf", __val);
4046 }
23c64853 4047#endif // _GLIBCXX_USE_C99_STDIO
7364f286 4048
356510ac 4049#if defined(_GLIBCXX_USE_WCHAR_T) && _GLIBCXX_USE_C99_WCHAR
a5a6b586
PC
4050 inline int
4051 stoi(const wstring& __str, size_t* __idx = 0, int __base = 10)
4052 { return __gnu_cxx::__stoa<long, int>(&std::wcstol, "stoi", __str.c_str(),
4053 __idx, __base); }
4054
4055 inline long
4056 stol(const wstring& __str, size_t* __idx = 0, int __base = 10)
4057 { return __gnu_cxx::__stoa(&std::wcstol, "stol", __str.c_str(),
4058 __idx, __base); }
4059
4060 inline unsigned long
4061 stoul(const wstring& __str, size_t* __idx = 0, int __base = 10)
4062 { return __gnu_cxx::__stoa(&std::wcstoul, "stoul", __str.c_str(),
4063 __idx, __base); }
4064
4065 inline long long
4066 stoll(const wstring& __str, size_t* __idx = 0, int __base = 10)
4067 { return __gnu_cxx::__stoa(&std::wcstoll, "stoll", __str.c_str(),
4068 __idx, __base); }
4069
4070 inline unsigned long long
4071 stoull(const wstring& __str, size_t* __idx = 0, int __base = 10)
4072 { return __gnu_cxx::__stoa(&std::wcstoull, "stoull", __str.c_str(),
4073 __idx, __base); }
4074
4075 // NB: wcstof vs wcstod.
4076 inline float
4077 stof(const wstring& __str, size_t* __idx = 0)
4078 { return __gnu_cxx::__stoa(&std::wcstof, "stof", __str.c_str(), __idx); }
4079
4080 inline double
4081 stod(const wstring& __str, size_t* __idx = 0)
4082 { return __gnu_cxx::__stoa(&std::wcstod, "stod", __str.c_str(), __idx); }
4083
4084 inline long double
4085 stold(const wstring& __str, size_t* __idx = 0)
4086 { return __gnu_cxx::__stoa(&std::wcstold, "stold", __str.c_str(), __idx); }
4087
f37f5fb8 4088#ifndef _GLIBCXX_HAVE_BROKEN_VSWPRINTF
a4ecd144
PC
4089 // DR 1261.
4090 inline wstring
4091 to_wstring(int __val)
4092 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(int),
4093 L"%d", __val); }
4094
4095 inline wstring
4096 to_wstring(unsigned __val)
4097 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
4098 4 * sizeof(unsigned),
4099 L"%u", __val); }
4100
4101 inline wstring
4102 to_wstring(long __val)
4103 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, 4 * sizeof(long),
4104 L"%ld", __val); }
4105
4106 inline wstring
4107 to_wstring(unsigned long __val)
4108 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
4109 4 * sizeof(unsigned long),
4110 L"%lu", __val); }
4111
a5a6b586
PC
4112 inline wstring
4113 to_wstring(long long __val)
4114 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
4115 4 * sizeof(long long),
4116 L"%lld", __val); }
4117
4118 inline wstring
4119 to_wstring(unsigned long long __val)
4120 { return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf,
4121 4 * sizeof(unsigned long long),
4122 L"%llu", __val); }
4123
a4ecd144
PC
4124 inline wstring
4125 to_wstring(float __val)
4126 {
4127 const int __n =
4128 __gnu_cxx::__numeric_traits<float>::__max_exponent10 + 20;
4129 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
4130 L"%f", __val);
4131 }
4132
4133 inline wstring
4134 to_wstring(double __val)
4135 {
4136 const int __n =
4137 __gnu_cxx::__numeric_traits<double>::__max_exponent10 + 20;
4138 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
4139 L"%f", __val);
4140 }
4141
a5a6b586
PC
4142 inline wstring
4143 to_wstring(long double __val)
4144 {
4145 const int __n =
4146 __gnu_cxx::__numeric_traits<long double>::__max_exponent10 + 20;
4147 return __gnu_cxx::__to_xstring<wstring>(&std::vswprintf, __n,
4148 L"%Lf", __val);
4149 }
f37f5fb8 4150#endif // _GLIBCXX_HAVE_BROKEN_VSWPRINTF
23c64853 4151#endif // _GLIBCXX_USE_WCHAR_T && _GLIBCXX_USE_C99_WCHAR
7364f286 4152
34a2b755 4153_GLIBCXX_END_NAMESPACE_CXX11
12ffa228
BK
4154_GLIBCXX_END_NAMESPACE_VERSION
4155} // namespace
725dc051 4156
23c64853 4157#endif /* C++11 */
15d81a3c 4158
734f5023 4159#if __cplusplus >= 201103L
15d81a3c
PC
4160
4161#include <bits/functional_hash.h>
4162
12ffa228
BK
4163namespace std _GLIBCXX_VISIBILITY(default)
4164{
4165_GLIBCXX_BEGIN_NAMESPACE_VERSION
15d81a3c
PC
4166
4167 // DR 1182.
4168
4169#ifndef _GLIBCXX_COMPATIBILITY_CXX0X
4170 /// std::hash specialization for string.
4171 template<>
4172 struct hash<string>
5d64ee19 4173 : public __hash_base<size_t, string>
15d81a3c
PC
4174 {
4175 size_t
72f1c34b 4176 operator()(const string& __s) const noexcept
e7f72940 4177 { return std::_Hash_impl::hash(__s.data(), __s.length()); }
15d81a3c
PC
4178 };
4179
4df047dd
FD
4180 template<>
4181 struct __is_fast_hash<hash<string>> : std::false_type
4182 { };
4183
15d81a3c
PC
4184 /// std::hash specialization for wstring.
4185 template<>
4186 struct hash<wstring>
5d64ee19 4187 : public __hash_base<size_t, wstring>
15d81a3c
PC
4188 {
4189 size_t
72f1c34b 4190 operator()(const wstring& __s) const noexcept
e7f72940
MA
4191 { return std::_Hash_impl::hash(__s.data(),
4192 __s.length() * sizeof(wchar_t)); }
15d81a3c 4193 };
4df047dd
FD
4194
4195 template<>
4196 struct __is_fast_hash<hash<wstring>> : std::false_type
4197 { };
15d81a3c
PC
4198#endif /* _GLIBCXX_COMPATIBILITY_CXX0X */
4199
c124af93
TH
4200#ifdef _GLIBCXX_USE_CHAR8_T
4201 /// std::hash specialization for u8string.
4202 template<>
4203 struct hash<u8string>
4204 : public __hash_base<size_t, u8string>
4205 {
4206 size_t
4207 operator()(const u8string& __s) const noexcept
4208 { return std::_Hash_impl::hash(__s.data(),
4209 __s.length() * sizeof(char8_t)); }
4210 };
4211
4212 template<>
4213 struct __is_fast_hash<hash<u8string>> : std::false_type
4214 { };
4215#endif
4216
15d81a3c
PC
4217 /// std::hash specialization for u16string.
4218 template<>
4219 struct hash<u16string>
5d64ee19 4220 : public __hash_base<size_t, u16string>
15d81a3c
PC
4221 {
4222 size_t
72f1c34b 4223 operator()(const u16string& __s) const noexcept
e7f72940
MA
4224 { return std::_Hash_impl::hash(__s.data(),
4225 __s.length() * sizeof(char16_t)); }
15d81a3c
PC
4226 };
4227
4df047dd
FD
4228 template<>
4229 struct __is_fast_hash<hash<u16string>> : std::false_type
4230 { };
4231
15d81a3c
PC
4232 /// std::hash specialization for u32string.
4233 template<>
4234 struct hash<u32string>
5d64ee19 4235 : public __hash_base<size_t, u32string>
15d81a3c
PC
4236 {
4237 size_t
72f1c34b 4238 operator()(const u32string& __s) const noexcept
e7f72940
MA
4239 { return std::_Hash_impl::hash(__s.data(),
4240 __s.length() * sizeof(char32_t)); }
15d81a3c 4241 };
4df047dd
FD
4242
4243 template<>
4244 struct __is_fast_hash<hash<u32string>> : std::false_type
4245 { };
15d81a3c 4246
10f26de9 4247#if __cplusplus >= 201402L
1c9f675f 4248
a15f7cb8
ESR
4249#define __cpp_lib_string_udls 201304
4250
0372af98
ESR
4251 inline namespace literals
4252 {
4253 inline namespace string_literals
4254 {
f03858e5
JW
4255#pragma GCC diagnostic push
4256#pragma GCC diagnostic ignored "-Wliteral-suffix"
b96e2ff9
ML
4257
4258#if __cpp_lib_constexpr_string >= 201907L
4259# define _GLIBCXX_STRING_CONSTEXPR constexpr
4260#else
4261# define _GLIBCXX_STRING_CONSTEXPR
4262#endif
4263
4264 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
88c4d6b7 4265 inline basic_string<char>
e9a64492 4266 operator""s(const char* __str, size_t __len)
88c4d6b7 4267 { return basic_string<char>{__str, __len}; }
1c9f675f 4268
b96e2ff9 4269 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
88c4d6b7 4270 inline basic_string<wchar_t>
e9a64492 4271 operator""s(const wchar_t* __str, size_t __len)
88c4d6b7 4272 { return basic_string<wchar_t>{__str, __len}; }
1c9f675f 4273
c124af93 4274#ifdef _GLIBCXX_USE_CHAR8_T
b96e2ff9 4275 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
c124af93
TH
4276 inline basic_string<char8_t>
4277 operator""s(const char8_t* __str, size_t __len)
4278 { return basic_string<char8_t>{__str, __len}; }
4279#endif
4280
b96e2ff9 4281 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
88c4d6b7 4282 inline basic_string<char16_t>
e9a64492 4283 operator""s(const char16_t* __str, size_t __len)
88c4d6b7 4284 { return basic_string<char16_t>{__str, __len}; }
1c9f675f 4285
b96e2ff9 4286 _GLIBCXX_DEFAULT_ABI_TAG _GLIBCXX_STRING_CONSTEXPR
88c4d6b7 4287 inline basic_string<char32_t>
e9a64492 4288 operator""s(const char32_t* __str, size_t __len)
88c4d6b7 4289 { return basic_string<char32_t>{__str, __len}; }
1c9f675f 4290
b96e2ff9 4291#undef _GLIBCXX_STRING_CONSTEXPR
f03858e5 4292#pragma GCC diagnostic pop
88c4d6b7
ESR
4293 } // inline namespace string_literals
4294 } // inline namespace literals
4295
10f26de9
JW
4296#if __cplusplus >= 201703L
4297 namespace __detail::__variant
4298 {
4299 template<typename> struct _Never_valueless_alt; // see <variant>
4300
4301 // Provide the strong exception-safety guarantee when emplacing a
47a468bd 4302 // basic_string into a variant, but only if moving the string cannot throw.
10f26de9
JW
4303 template<typename _Tp, typename _Traits, typename _Alloc>
4304 struct _Never_valueless_alt<std::basic_string<_Tp, _Traits, _Alloc>>
47a468bd
JW
4305 : __and_<
4306 is_nothrow_move_constructible<std::basic_string<_Tp, _Traits, _Alloc>>,
4307 is_nothrow_move_assignable<std::basic_string<_Tp, _Traits, _Alloc>>
4308 >::type
10f26de9
JW
4309 { };
4310 } // namespace __detail::__variant
4311#endif // C++17
4312#endif // C++14
1c9f675f 4313
4a15d842 4314_GLIBCXX_END_NAMESPACE_VERSION
1c9f675f
ESR
4315} // namespace std
4316
734f5023 4317#endif // C++11
a5a6b586 4318
3d7c150e 4319#endif /* _BASIC_STRING_H */