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