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