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