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